function makeGameName() { $sql = "Select COUNT(*) AS c from gameNameNoun"; $result = mysql_query($sql) or sql_error_report($sql, $_SERVER["SCRIPT_NAME"]); $row = mysql_fetch_assoc($result); $numNouns = intval($row['c']); $sql = "Select COUNT(*) AS c from gameNameAdj"; $result = mysql_query($sql) or sql_error_report($sql, $_SERVER["SCRIPT_NAME"]); $row = mysql_fetch_assoc($result); $numAdjs = intval($row['c']); do { $random = rand(0, $numNouns - 1); $sql = "Select word from gameNameNoun LIMIT 1 OFFSET %s"; $sql = sprintf($sql, $random); $result = mysql_query($sql) or sql_error_report($sql, $_SERVER["SCRIPT_NAME"]); $row = mysql_fetch_assoc($result); $noun = $row['word']; $random = rand(0, $numAdjs - 1); $sql = "Select word from gameNameAdj LIMIT 1 OFFSET %s"; $sql = sprintf($sql, $random); $result = mysql_query($sql) or sql_error_report($sql, $_SERVER["SCRIPT_NAME"]); $row = mysql_fetch_assoc($result); $adjective = $row['word']; $gameName = "Operation {$adjective} {$noun}"; $sql = "SELECT * FROM games WHERE name = '{$gameName}' LIMIT 1;"; $result = mysql_query($sql) or sql_error_report($sql, $_SERVER["SCRIPT_NAME"]); } while (mysql_num_rows($result) > 0); return $gameName; }
function getPlayerName($playerId) { $sql = "SELECT email FROM players WHERE playerId = '{$playerId}' LIMIT 1;"; $result = mysql_query($sql) or sql_error_report($sql, $_SERVER["SCRIPT_NAME"]); if ($row = mysql_fetch_assoc($result)) { return $row['email']; } else { $ret = array("status" => "IMPOSSIBLE4"); die(json_encode($ret)); } }
function joinGame($playerId, $alias, $tokens) { // check to see that the player's state is NOTHING $sql = "SELECT state FROM players WHERE playerId='{$playerId}' LIMIT 1;"; $result = mysql_query($sql) or sql_error_report($sql, $_SERVER["SCRIPT_NAME"]); if ($row = mysql_fetch_assoc($result)) { $state = $row['state']; if ($state != 'NOTHING') { $ret = array("status" => "BAD_STATE", "state" => $state); print json_encode($ret); return; } } else { $ret = array("status" => "IMPOSSIBLE7"); die(json_encode($ret)); } // check that the game with the correct token exists $tokenList = "'" . implode("','", explode(";\n", $tokens)) . "'"; $sql = "SELECT gameId FROM games WHERE state = 'PENDING' AND token IN ({$tokenList}) LIMIT 1;"; $result = mysql_query($sql) or sql_error_report($sql, $_SERVER["SCRIPT_NAME"]); if ($row = mysql_fetch_assoc($result)) { $gameId = $row['gameId']; // check to see if alias exists $sql = "SELECT * FROM participations WHERE alias='{$alias}' AND gameId = '{$gameId}' LIMIT 1;"; $result = mysql_query($sql) or sql_error_report($sql, $_SERVER["SCRIPT_NAME"]); if (mysql_num_rows($result) == 1) { $ret = array("status" => "ALIAS_TAKEN"); print json_encode($ret); return; } // get the code word $codewords = codewords(); shuffle($codewords); do { $codeword = array_pop($codewords); $sql = "SELECT * FROM participations WHERE codeword = '{$codeword}' AND gameId = '{$gameId}' LIMIT 1;"; $result = mysql_query($sql) or sql_error_report($sql, $_SERVER["SCRIPT_NAME"]); } while (mysql_num_rows($result) > 0); $sql = "INSERT INTO participations (gameId, playerId, state, alias, codeword) VALUES ('{$gameId}', '{$playerId}', 'ACTIVE', '{$alias}', '{$codeword}');"; mysql_query($sql) or sql_error_report($sql, $_SERVER["SCRIPT_NAME"]); $sql = "UPDATE players SET state='PLAYING', waitingAlias='', waitingStart='0000-00-00 00:00:00', tokens='{$tokens}' WHERE playerId = '{$playerId}' LIMIT 1;"; mysql_query($sql) or sql_error_report($sql, $_SERVER["SCRIPT_NAME"]); if (mysql_affected_rows() == 1) { $player = getPlayerObject($playerId); $news = getNews($playerId); $game = getGameObject($playerId, $gameId); $ret = array("status" => "OK", "player" => $player, "news" => $news, "game" => $game); print json_encode($ret); return; } else { $ret = array("status" => "IMPOSSIBLE8a"); print json_encode($ret); return; } } else { // check to see if alias exists $sql = "SELECT * FROM players WHERE waitingAlias='{$alias}' LIMIT 1;"; $result = mysql_query($sql) or sql_error_report($sql, $_SERVER["SCRIPT_NAME"]); if (mysql_num_rows($result) == 1) { $ret = array("status" => "ALIAS_TAKEN"); print json_encode($ret); return; } $date = getDateNow(); $sql = "UPDATE players SET waitingAlias='{$alias}', state='WAITING', waitingStart='{$date}', tokens='{$tokens}' WHERE playerId='{$playerId}' LIMIT 1;"; mysql_query($sql) or sql_error_report($sql, $_SERVER["SCRIPT_NAME"]); if (mysql_affected_rows() == 1) { $player = getPlayerObject($playerId); $ret = array("status" => "OK", "player" => $player); print json_encode($ret); return; } else { $ret = array("status" => "IMPOSSIBLE8b"); print json_encode($ret); return; } } }