public static function theWinerIS($input) { //need the link and $link = $input['link']; $result = $link->query("SELECT idQuestion,player1Answer ,player2Answer from games where id in(SELECT idGame from battledetail where idBattle = " . GameMaker::getBattleId() . " ) "); // if (!$result->num_rows) { //to be safe. return; //but we should think of this later :D } $player1 = 0; $player2 = 0; while ($row = $result->fetch_assoc()) { $res = $link->query("SELECT answer from questions,questionoptions where questionoptions.id = idAnswer and questions.id =" . $row["idQuestion"] . " "); // $row2 = $res->fetch_assoc(); if ($row["answer"] == $row["player1Answer"]) { $player1 += 1; //until we discuss scoring policy } if ($row["answer"] == $row["player2Answer"]) { $player2 += 1; //until we discuss scoring policy } } if ($player2 > $player1) { $score = $player2 * 5; $userID = 0; // User::setScore(array("link" => $link, "score" => $score, "userID" => $userID)); return 2; //player 2 win } elseif ($player1 > $player2) { $score = $player1 * 5; $userID = 0; // User::setScore(array("link" => $link, "score" => $score, "userID" => $userID)); return 1; //player 1 win } else { return 0; //egality } }
if ($params[1] == 'question') { $question = GameMaker::fetchQuestion(array('link' => $link, 'question' => $params[2])); echo json_encode($question); } } else { if ($params[0] == 'save') { //save player's answer to the database if ($params[1] !== NULL) { //params[1] is the answer $question = GameMaker::saveResponse(array('link' => $link, 'response' => $params[1])); echo "save success"; } } else { if ($params[0] == 'winner') { //get the winner id $winner = GameMaker::theWinerIS(array('link' => $link)); if ($winner == 1) { if (GameMAker::getSessionPlayerId() == 1) { echo "you win"; } echo "you lose"; } else { if ($winner == 2) { if (GameMAker::getSessionPlayerId() == 2) { echo "you win"; } echo "you lose"; } else { echo "withdraw"; } }
<?php if (!isset($isIndex)) { die(''); } require_once './php/GameMaker.class.php'; $subjects = empty($_POST['selected']) ? '0' : $_POST['selected']; //TODO clean $subjects if (User::isPlaying(array('link' => $link))) { GameMaker::unqueue(array('link' => $link)); $link->query("UPDATE users SET isPlaying=false where id='" . User::getUserId() . "'"); } ?> <script type="text/javascript"> $(document).ready(function(){ var subs = '<?php echo $subjects; ?> '; var go = new GameOn({ subjects:subs, delay:1000//TODO delay need to be view :D }); window.addEventListener("beforeunload", function(event) { event.returnValue = "do you really want to leave ?"; }); window.addEventListener("unload", function(event) { //i know it's ugly.. couldn't do it otherwise. sight //it's because other functions will not work //this works because it will be called async $.ajax({