/** * Called when user wants to start match. * If noone is waiting on this table, puts this user on waiting for opponent * If someone is already waiting, create a new match * @return status => waiting | match => integer */ public function startMatch() { $team = Session::get('team'); if (empty($team)) { return array('error' => 'You are not logged in!'); } $tableKey = Input::get('table'); if (empty($tableKey)) { return array('error' => 'Table parameter is missing!'); } // check if there is already a match on this table $match = TableModel::getMatchOnTable($tableKey); if (!empty($match)) { $match = $match[0]; // check if our team is in this match if ($team->id != $match->home_team_id && $team->id != $match->away_team_id) { return array('error' => 'Someone else is already playing on this table!'); } return array('match' => $match->id); } // check if someone waits on this table $wait = TableModel::getTeamWaitingOnTable($tableKey); if (empty($wait)) { // no-one is waiting, so insert this team on waiting TableModel::insertTeamWaiting($team->id, $tableKey); return array('status' => 'wait'); } else { if ($wait[0]->team_id == $team->id) { // this user is already waiting for opponent return array('status' => 'wait'); } else { // another user is already waiting, so we can start match return TableModel::startMatchOnTable($wait[0]->team_id, $team->id, $tableKey); } } }