Exemplo n.º 1
0
 public function viewAction()
 {
     $id = $this->params()->fromRoute('id');
     $player = $this->playerRepository->find($id);
     $pointLogs = $this->pointLogPlayerRepository->getForPlayer($player);
     return array('player' => $player, 'logs' => $pointLogs);
 }
Exemplo n.º 2
0
 /**
  * Hydrate $object with the provided $data.
  *
  * @param array $data
  * @param \Application\Model\Entity\Match $match
  *
  * @return \Application\Model\Entity\Match
  */
 public function hydrate(array $data, $match)
 {
     $match->setGames($data['games']);
     if ($match instanceof DoubleMatch) {
         $match->setTeamOne($this->repository->find($data['teamOneAttack']), $this->repository->find($data['teamOneDefence']));
         $match->setTeamTwo($this->repository->find($data['teamTwoAttack']), $this->repository->find($data['teamTwoDefence']));
     }
     return $match;
 }
Exemplo n.º 3
0
 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()));
     }
 }
Exemplo n.º 4
0
 public function setupTournamentAction()
 {
     $id = $this->params()->fromRoute('id');
     $tournament = $this->tournamentRepository->find($id);
     if (!$tournament instanceof \Application\Model\Entity\Tournament) {
         throw new \RuntimeException('Invalid tournament ID');
     }
     $form = $this->getAddPlayerForm($tournament);
     if ($this->request->isPost()) {
         $form->setData($this->request->getPost());
         if ($form->isValid()) {
             $playerId = $form->get('player')->getValue();
             $player = $this->playerRepository->find($playerId);
             $tournament->addPlayer($player);
             $this->tournamentRepository->persist($tournament);
             return $this->redirect()->toRoute('setup', array('id' => $tournament->getId()));
         }
     }
     return array('tournament' => $tournament, 'addPlayerForm' => $form);
 }
Exemplo n.º 5
0
 /**
  * @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;
 }