if ($params[0] == 'queue') { //player started search if (GameMaker::queue(array('link' => $link, 'subjects' => $_POST['subjects']))) { echo "success"; } else { setAlert('danger', 'could not queue for a game!'); echo "error"; } } else { if ($params[0] == 'unqueue') { GameMaker::unqueue(array('link' => $link)); echo "unqueued"; } else { if ($params[0] == 'status') { //fetching status to play if (!empty(GameMaker::getQueueId())) { if (!User::isPlaying(array('link' => $link))) { if (GameMaker::findOpponent(array('link' => $link))) { //okey we found an oponent echo "opponentFound"; } else { echo "opponentNotFound"; } } else { //player already playing GameMaker::BattleInit(array('link' => $link)); GameMaker::sessionPlayerID($link); //echo $_SESSION["player"];//just for debug and it work echo "inGame"; } } else {
public static function allocateBattle($input) { //player1 $user_id = User::getUserId(); $user_queueId = GameMaker::getQueueId(); //player2 $user2_Id = $input['userId']; $user2_queueId = $input['queueId']; //subject $subject_Id = $input['subjectId']; //db link $link = $input['link']; //ofc the player shouldn't be playing,i think it unecessary but let's leave it here for now. if (!User::isPlaying(array('link' => $link)) && !empty($user_queueId)) { //remove both players from the queue $link->query("DELETE FROM queue WHERE idUser IN ({$user_id},{$user2_Id})"); //change both players status to isPlaying = true $link->query("UPDATE users SET isPlaying=true WHERE id IN ({$user_id},{$user2_Id})"); //all is good now let's actually create a battle. $link->query("INSERT INTO battles(idPlayer1,idPlayer2) VALUES('{$user_id}','{$user2_Id}')"); $battleId = $link->insert_id; //battle created, let's create games //fetch questions related to subjectId :D //awesome, randomly orders the result, plus, it only returns exactly how many games i'll need,beautiful $result = $link->query("SELECT questions.id as id from questions,questionrelated where idRelated=questionrelated.id AND idSubject='{$subject_Id}' ORDER BY RAND() LIMIT " . G4K_GAMES_COUNT); $questions = array(); for ($i = 0; $i < G4K_GAMES_COUNT; $i++) { //i am really trusting that there will be at least G4K_GAMES_COUNT for every subjectId $row = $result->fetch_assoc(); //i don't want to add further tests for that because , $questions[$i] = $row['id']; //it makes sense that we should have enough questions } //add them questions to the battledetails, yooho for ($i = 0; $i < G4K_GAMES_COUNT; $i++) { $link->query("INSERT INTO games(idQuestion) VALUES('" . $questions[$i] . "')"); $gameId = $link->insert_id; $link->query("INSERT INTO battledetail(idBattle,idGame) VALUES('{$battleId}','{$gameId}')"); } //i guess this is all? OMG finally return true; //TODO when at the refactoring phase, make this a procedure (transaction please?) } return false; }