public static function newPendingGame($session, $size) { $session_info = session::get_session_by_hash($session); if ($session_info != null && safe_input::is_number($size) && $size > 1 && $size < 21) { $add_result = pending_game::add_new_pending_game($session_info['userID'], $size); GameControl::matchPendingGames(); return $add_result; } else { Report::warning(__METHOD__ . "," . __LINE__, "trying to create a pending game with an invalid size:" . $size); return false; } }
public static function addNewLog($user_id, $ip, $uuid) { if (safe_input::is_number($user_id) && safe_input::is_valid_ip($ip) && safe_input::is_valid_uuid($uuid)) { $date = microtime(true); $query = "INSERT INTO `log` (`userID`, `date`, `uuid`, `ipAddress`) VALUES ( '{$user_id}', '{$date}', '{$uuid}', '{$ip}');"; $db = new database(); $res = $db->query($query); log::$last_inserted_id = $db->insert_id(); return $res; } else { return false; //invalid input } }
public static function delete_move($move_id) { if (safe_input::is_number($move_id)) { $query = "DELETE FROM `move` WHERE `id` = '{$move_id}' ;"; $db = new database(); return $db->query($query); } else { return false; } }
public function startNewGame() { if (XmlRequestValidator::isValidStartPendingGameRequest($this->requestData)) { $session = $this->requestData->body->session; $size = $this->requestData->body->size; if (safe_input::is_valid_session_hash($session) && safe_input::is_number($size) && $size > 1) { //chkec if the session hash exists $session_info = session::get_session_by_hash($session); if ($session_info != null) { $res = Execute::newPendingGame($session, $size); if ($res) { $this->response = XmlBuilder::startNewPendingGameSuccessfullResponse("plain", $session); } else { //faild to add new game Report::error(__METHOD__ . "," . __LINE__, "failed to add new pending game"); $this->response = XmlBuilder::failed_response("plain", 5, 0, "failed to add new pending game, try again"); } } else { //the given hash doesn't exist in the database Report::warning(__METHOD__ . "," . __LINE__, "start new pending game request contains a session hash that does not exist in the database: hash=" . $session); $this->response = XmlBuilder::failed_response("plain", 5, 1, "expired session"); } } else { //invalid data passed Report::error(__METHOD__ . "," . __LINE__, "start new pending game request contains an incorrectly formatted session hash or game size, size:" . $size); $this->response = XmlBuilder::failed_response("plain", 5, 0, "invalid session or gcm id"); } } else { //xml request was not formatted correctly Report::error(__METHOD__ . "," . __LINE__, "invalid new pending game request!"); $this->invalidRequest(); } }
public static function match($game_id1, $game_id2) { $game1 = pending_game::get_pending_game_by_id($game_id1); $game2 = pending_game::get_pending_game_by_id($game_id2); if (safe_input::is_number($game_id1) && safe_input::is_number($game_id2) && $game1 != null && $game2 != null && $game1['size'] == $game2['size']) { $date = time(); $player1_id = $game1['userID']; $player2_id = $game2['userID']; if ($player1_id == $player2_id) { return false; } $size = $game1['size']; $g1_id = $game1['id']; $g2_id = $game2['id']; $db = new database(); $query[] = "INSERT INTO `game` (`winnerID`, `createDate`, `currentTurnPlayerID`, `size`, `lastActivityDate`, `player1ID` , `player2ID`) VALUES ( null, '{$date}', '{$player1_id}', '{$size}', null , '{$player1_id}', '{$player2_id}');"; $query[] = "DELETE FROM `pending_game` WHERE `id` = '{$g1_id}' or `id` = '{$g2_id}'"; $res = $db->execute_transaction($query); return $res; } else { return false; } }
public static function delete_message($message_id) { if (safe_input::is_number($message_id)) { $query = "DELETE FROM `message` WHERE `id` = '{$message_id}' ;"; $db = new database(); $res = $db->query($query); return $res; } else { return false; //invalid $message_id } }
public static function delete_session_by_id($session_id) { if (safe_input::is_number($session_id)) { $query = "DELETE FROM `session` WHERE `id` = '{$session_id}'"; $db = new database(); return $db->query($query); } else { return false; //invalid user_id } }
public static function getOpponentId($game_id, $player_id) { if (safe_input::is_number($game_id) && safe_input::is_number($player_id)) { $game = game::getGameById($game_id); if ($game == false) { //no game with that id return null; } else { if ($game['player1ID'] == $player_id) { return $game['player2ID']; } elseif ($game['player2ID'] == $player_id) { return $game['player1ID']; } else { return null; //player id is not a player in the game with the id $game_id } } } else { return null; //ERROR: either the game id or player id or both are not numbers! (invalid) } }
public static function setGCM($user_id, $gcm_id) { if (safe_input::is_number($user_id) && safe_input::is_valid_gcm_id($gcm_id)) { $db = new database(); $query = "UPDATE `user` SET `gcmID` = '{$gcm_id}' WHERE `id` = '{$user_id}'"; return $db->query($query); } else { return false; } }