/** * Constructs a GroupRequestQueue with entries matching the specified game and step. * * @param Game $game the desired game * @param Round $round the desired round */ public function __construct(Game $game, Round $round) { $this->requests = array(); $dbh = Database::handle(); $sth = $dbh->prepare('SELECT * FROM group_requests WHERE game = :game AND step = :step'); $sth->bindValue(':game', $game->getID()); $sth->bindValue(':step', strval($round)); $sth->execute(); while ($row = $sth->fetch(PDO::FETCH_ASSOC)) { $expires = Util::sql2unixtime($row['expires']); if ($expires == 0) { $expires = NULL; } $this->requests[] = new GroupRequest(Session::fromSessionID($row['session']), $expires); } }
/** * Returns the Group from the database identified by the given ID. * * @param mixed $id the id of the group in the database * @throws InvalidArgumentException if no group was found matching this ID * @return Group group matching given ID */ public static function getGroupByID($id) { $dbh = Database::handle(); $sth = $dbh->prepare('SELECT * FROM groups where id = ?'); $sth->execute(array($id)); if ($row = $sth->fetch(PDO::FETCH_ASSOC)) { $session_ids = unserialize($row['sessions']); // convert session ids to session objects $sessions = array_map(function ($session_id) { return Session::fromSessionID($session_id); }, $session_ids); return new Group($row['id'], $sessions, unserialize($row['data'])); } else { throw new InvalidArgumentException('no groups matching given id'); } }
function getResponse() { global $RESPONSE, $SESSION; if (!isset($_POST['sessionID'])) { $RESPONSE = 'missing session ID'; return; } try { $SESSION = Session::fromSessionID($_POST['sessionID']); } catch (DoesNotExistException $e) { $RESPONSE = 'invalid session ID'; return; } switch ($SESSION->getStatus()) { case Session::awaiting_user_input: processInput(); break; case Session::group_request_pending: // assuming a request is pending in the queue, has it expired? if ($request = GroupRequestQueue::retrieveRequest($SESSION)) { if ($request->expired()) { // okay, we're done with this request // delete it from the database GroupRequestQueue::deleteRequests(array($request)); // $RESPONSE = 'no partner found! exiting. (TODO: exit trail)'; //TODO: exit trail $SESSION->startExitTrail(); startStep(); return; } } // nothing to do, except wait, because if a suitable partner comes along, the request pending status will be changed $RESPONSE = array('action' => 'wait'); // TODO: countdown? break; case Session::group_request_fulfilled: startStep(); break; case Session::finished_step: if (readyToMoveOn()) { executeGroupCallbacks(); advanceStep(); } else { /* * readyToMoveOn sets the RESPONSE to the HTML of the waiting file. * But because their status was already finished_step, * we can assume that the waiting screen has already been loaded. * So we just tell them to wait (overriding the response). */ // $RESPONSE = array('action' => 'wait'); } break; case Session::callback_done: advanceStep(); break; case Session::finished: case Session::terminated: $RESPONSE = 'session ended'; break; default: throw new Exception('unknown session status'); break; } }