/** * Checks the group request queue for partners for the given session's current step. * If a group can be formed, sets it as the current group for all of its members, * then returns that group. * Otherwise, adds this session to the group request queue, * then returns FALSE. * * @param Session $session * @return Group if a group can be formed, FALSE otherwise */ public static function getNewGroup(Session $session) { // set given group's satus as "request pending". this is for internal state checks. $session->setStatus(Session::group_request_pending); // get all the requests for this game and step $queue = new GroupRequestQueue($session->game, $session->currentRound()); $request_expires = time() + $session->game->group_wait_limit; // try getting a match for this session $sessions = $queue->newRequest(new GroupRequest($session, $request_expires)); if (is_bool($sessions) && $sessions == FALSE) { // no match return FALSE; } else { // found match return self::newGroupAtCurrentStep($sessions); } }
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; } }