function check_game() { global $bot; // Check if we have now been assigned a game. $res = pg_query_params('SELECT b.game, b.cards, g.bot1, g.bot2, g.bid1, g.bid2 FROM bots AS b JOIN games AS g ON b.game=g.id WHERE b.id=$1;', [$bot["id"]]); if ($res === FALSE) { failure("Failed to query the database to check whether you have been assigned a game."); } if (pg_affected_rows($res) !== 1) { return; } $row = pg_fetch_assoc($res); pg_free_result($res); // If our bid is null, we can return successfully and not confuse the bot. if ($row["bot1"] === $bot["id"] && !is_null($row["bid1"]) || $row["bot2"] === $bot["id"] && !is_null($row["bid2"])) { failure("You have already started playing a game, #{$row['game']}, and must finish it before attempting to start a new game."); } // Parse the cards the bot has been dealt into an array. $cards = explode(",", substr($row["cards"], 1, -1)); // Determine our opponent. if ($row["bot1"] === $bot["id"]) { $opponent = (int) $row["bot2"]; } else { $opponent = (int) $row["bot1"]; } // Return the game information to the client. pg_query("BEGIN;"); reset_timeout("session"); success(["cards" => $cards, "game" => (int) $row["game"], "opponent" => $opponent]); }
// Retrieve the password hash for the bot. $res = pg_query_params('SELECT id,password FROM bots WHERE name=$1;', [$req["name"]]); if ($res === FALSE) { failure("Failed to query the database for the existence of your name."); } if (pg_affected_rows($res) !== 1) { failure("The 'name' you provided, '{$req['name']}', has not been registered."); } $row = pg_fetch_assoc($res); $id = (int) $row["id"]; $pass = $row["password"]; pg_free_result($res); // Validate the password from the request against the database. if (!password_verify($req["password"], $pass)) { failure("The 'password' you provided did not match the one given during registration."); } // Generate a shoddy but workable session identifier. $sess = md5($req["name"] . openssl_random_pseudo_bytes(128)); // Record the session in the database. $res = pg_query_params('UPDATE bots SET session=$1, session_timeout=now()+\'30 minutes\'::interval WHERE name=$2;', [$sess, $req["name"]]); if ($res === FALSE || pg_affected_rows($res) !== 1) { failure("Failed to record your session in the database."); } pg_free_result($res); // Extract the full session information from the database. $bot = get_session($sess); // Be sure that we don't have an active game. forfeit(); // Return successfully. reset_timeout("session"); success(["session" => $sess, "id" => $id]);
//////////////////////////////////////////////////////////////////////////////// // All done - put the site back online //////////////////////////////////////////////////////////////////////////////// echo '<li>', WT_I18N::translate('Place the site online, by deleting the file %s…', $lock_file_html); if (WT_File::delete($lock_file)) { echo '<br>', WT_I18N::translate('The file %s was deleted.', '<span dir="ltr">' . $lock_file . '</span>'), $icon_success; } else { echo '<br>', WT_I18N::translate('The file %s could not be deleted.', '<span dir="ltr">' . $lock_file . '</span>'), $icon_failure; } echo '</li>'; flush(); //////////////////////////////////////////////////////////////////////////////// // Clean up //////////////////////////////////////////////////////////////////////////////// echo '<li>', WT_I18N::translate('Delete temporary files…'); reset_timeout(); if (WT_File::delete($zip_dir)) { echo '<br>', WT_I18N::translate('The folder %s was deleted.', '<span dir="auto">' . $zip_dir . '</span>'), $icon_success; } else { echo '<br>', WT_I18N::translate('The folder %s could not be deleted.', '<span dir="auto">' . $zip_dir . '</span>'), $icon_failure; } if (WT_File::delete($zip_file)) { echo '<br>', WT_I18N::translate('The file %s was deleted.', '<span dir="auto">' . $zip_file . '</span>'), $icon_success; } else { echo '<br>', WT_I18N::translate('The file %s could not be deleted.', '<span dir="auto">' . $zip_file . '</span>'), $icon_failure; } echo '</li>'; echo '</ul>'; echo '<p>', WT_I18N::translate('The upgrade is complete.'), '</p>'; // Reset the time limit, as timeouts in this script could leave the upgrade incomplete. function reset_timeout()