public function eloAction() { $request = $this->getRequest(); // Make sure that we are running in a console and the user has not tricked our // application into running this action from a public web server. if (!$request instanceof ConsoleRequest) { throw new \RuntimeException('You can only use this action from a console!'); } $matches = $this->matchRepository->getAllOrderedByDate(); $this->pointLogRepository->reset(); $this->playerRepository->resetRankings(); foreach ($matches as $match) { foreach ($match->getPlayer() as $player) { $this->addPlayerToRanking($player); } $ranking = new Elo(); $log = $ranking->calculateMatch($match); foreach ($match->getPlayer() as $player) { $this->playerRepository->persist($player, false); } if ($match instanceof SingleMatch) { $participant1 = $match->getPlayer1(); $participant2 = $match->getPlayer2(); } else { if ($match instanceof DoubleMatch) { $participant1 = $match->getTeamOne(); $participant2 = $match->getTeamTwo(); } } $this->pointLogRepository->persist($log, false); $this->console->writeLine(sprintf('%s vs. %s - Chances %s%%/%s%%. Points %d (%+d) / %d (%+d)', $participant1->getName(), $participant2->getName(), $log->getChance1(), $log->getChance2(), $log->getNewPoints1(), $log->getDifference1(), $log->getNewPoints2(), $log->getDifference2())); } $this->pointLogRepository->flush(); $this->console->writeLine(str_repeat("-", $this->console->getWidth() / 2)); usort($this->ranking, array($this, 'compareRanking')); $this->console->writeLine("Rankings:"); $i = 1; foreach ($this->ranking as $player) { $this->console->writeLine(sprintf("%d: %d - %s (%d matches)", $i++, $player->getPoints(), $player->getName(), $player->getMatchCount())); } }
/** * @param Match $match * @param PlannedMatch $plannedMatch * * @return ViewModel */ protected function handleForm($match, $plannedMatch = null) { $tournament = $match->getTournament(); $factory = new \Application\Form\Factory($this->playerRepository); $form = $factory->getMatchForm($tournament, $plannedMatch); $form->bind($match); if ($this->request->isPost()) { $form->setData($this->request->getPost()); if ($form->isValid()) { if ($match->isResultValid()) { $tournament->matchPlayed($match, $plannedMatch); $this->matchRepository->persist($match); if ($plannedMatch != null) { $this->plannedMatchRepository->persist($plannedMatch); } $ranking = new Elo(); $log = $ranking->calculateMatch($match); $this->pointLogRepository->persist($log); foreach ($match->getPlayer() as $player) { $this->playerRepository->persist($player); } return $this->redirect()->toRoute('tournament/show', array('id' => $tournament->getId())); } else { $form->setMessages(array('submit' => array('You need to play more matches'))); } } } if ($match->getId() != null) { $url = $this->url()->fromRoute('match/edit/', array('mid' => $match->getId())); $form->setAttribute('action', $url); } $view = new ViewModel(array('form' => $form, 'tournament' => $tournament)); $view->setTemplate('application/match/edit'); if ($this->getRequest()->isXmlHttpRequest()) { $view->setTerminal(true); } return $view; }