$maxCount = $count;
        $tokenToStart = $token;
    }
}
if ($tokenToStart != '') {
    // make game
    $gameName = makeGameName();
    trace("Pending game {$gameName}");
    $sql = "INSERT INTO games (name, state, startDate, endDate, winnerId, timeoutHours, wantedHours, token) VALUES ";
    $sql .= "('{$gameName}', 'PENDING', DATE_ADD('{$date}', INTERVAL 24 HOUR), '0000-00-00 00:00:00', '0', '144', '72', '{$tokenToStart}');";
    mysql_query($sql) or sql_error_report($sql, $_SERVER["SCRIPT_NAME"]);
    $sql = "SELECT gameId FROM games WHERE name = '{$gameName}' LIMIT 1;";
    $result = mysql_query($sql) or sql_error_report($sql, $_SERVER["SCRIPT_NAME"]);
    $row = mysql_fetch_assoc($result);
    $gameId = $row['gameId'];
    $codewords = codewords();
    shuffle($codewords);
    $participationList = '';
    $players = $tokenPlayers[$tokenToStart];
    foreach ($players as $playerId) {
        if ($participationList != '') {
            $participationList .= ', ';
        }
        $codeword = array_pop($codewords);
        $participationList .= "('{$gameId}', '{$playerId}', 'ACTIVE', '{$playerAliasMap[$playerId]}', '{$codeword}')";
        trace("Adding player {$playerNameMap[$playerId]}");
    }
    $sql = "INSERT INTO participations (gameId, playerId, state, alias, codeword) VALUES {$participationList};";
    mysql_query($sql) or sql_error_report($sql, $_SERVER["SCRIPT_NAME"]);
    // Update players in DB
    $playerList = join(',', $players);
Example #2
0
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;
        }
    }
}