/** * Return all matches for an event in the correct bracket order. * * @param array $event * @return array */ public function getBrackets($event) { $matches = $this->find('all', array('conditions' => array('Match.event_id' => $event['Event']['id']), 'order' => array('Match.order' => 'ASC'), 'contain' => array('MatchScore'), 'cache' => array(__METHOD__, $event['Event']['id']))); return Tournament::factory($event)->organizeBrackets($matches); }
/** * Loop through pending matches and declare a winner and award points. */ public function advance() { $event_id = $this->args[0]; $event = $this->Event->getById($event_id); $settings = Configure::read('Tournament.settings'); if (!$event) { $this->err('Invalid event'); return; } // Update matches with fake data $this->out('Advancing current round matches'); if ($matches = $this->Match->getPendingMatches($event_id, $event['Event']['round'])) { foreach ($matches as $match) { $home_id = $match['Match']['home_id']; $away_id = $match['Match']['away_id']; if ($event['Event']['type'] == Event::SINGLE_ELIM || $event['Event']['type'] == Event::DOUBLE_ELIM) { $winner = rand(1, 2); } else { $winner = rand(1, 3); } $query = array('winner' => $winner); if ($winner == Match::HOME) { $query['homeOutcome'] = Match::WIN; $query['awayOutcome'] = Match::LOSS; $query['homePoints'] = $settings['defaultWinPoints']; $query['awayPoints'] = $settings['defaultLossPoints']; $this->EventParticipant->updateStatistics($event_id, $home_id, array('wins' => 1, 'points' => $query['homePoints'])); $this->EventParticipant->updateStatistics($event_id, $away_id, array('losses' => 1, 'points' => $query['awayPoints'])); } else { if ($winner == Match::AWAY) { $query['homeOutcome'] = Match::LOSS; $query['awayOutcome'] = Match::WIN; $query['homePoints'] = $settings['defaultLossPoints']; $query['awayPoints'] = $settings['defaultWinPoints']; $this->EventParticipant->updateStatistics($event_id, $home_id, array('losses' => 1, 'points' => $query['homePoints'])); $this->EventParticipant->updateStatistics($event_id, $away_id, array('wins' => 1, 'points' => $query['awayPoints'])); } else { $query['homeOutcome'] = Match::TIE; $query['awayOutcome'] = Match::TIE; $query['homePoints'] = $settings['defaultTiePoints']; $query['awayPoints'] = $settings['defaultTiePoints']; $this->EventParticipant->updateStatistics($event_id, $home_id, array('ties' => 1, 'points' => $query['homePoints'])); $this->EventParticipant->updateStatistics($event_id, $away_id, array('ties' => 1, 'points' => $query['awayPoints'])); } } $this->Match->id = $match['Match']['id']; $this->Match->save($query, false); } } // Generate the next round $this->out('Generating next round matches'); try { Tournament::factory($event)->generateMatches(); } catch (Exception $e) { $this->out($e->getMessage()); } }