protected function execute(InputInterface $input, OutputInterface $output) { // Get the current game $entityManager = $this->getContainer()->get('doctrine')->getManager(); $repo = $entityManager->getRepository("HackathonGameBundle:Game"); $currentGame = $repo->findCurrentGame(); // Check if the round finished $timeTillRoundFinished = $currentGame->secondsUntilRoundEnd(); if ($timeTillRoundFinished <= 0) { // The round finished. So let's find out on what the crowd decided $userRepo = $entityManager->getRepository("HackathonGameBundle:User"); $result = $userRepo->findRoundResult($currentGame); echo "Round result:\n "; var_dump($result); if ($result !== NULL) { // Create a new GameTurn object $turn = new GameTurn(); $turn->setMove($result["selection"]); $turn->setTeam($currentGame->getCurrentTeam()); $currentGame->addTurn($turn); $entityManager->persist($turn); // Reset the user selection $currentGame->resetUserSelections(); $gameLogic = new ConnectFour($currentGame); $winnerTeam = $gameLogic->checkWin(); if ($winnerTeam !== NULL) { // We have a winner echo "The winner is: " . $winnerTeam . "\n"; $currentGame->setWinnerTeam($winnerTeam); $currentGame->setIsFinished(true); // And need to start a new game echo "Starting new Game\n"; $newGame = new Game(); $entityManager->persist($newGame); } else { // The round ended! Let's start a new one! $currentGame->startNewTurn(); $formatter = new TeletextFormatter($currentGame); $formattedText = $formatter->getFormat(); TeletextCommunicator::post($formattedText); echo "Starting new round\n"; } } else { // Users did not select anything... $currentGame->startNewTurn(); echo "No move selected. Repeating turn."; } } $entityManager->flush(); echo "Ended update\n"; }
/** * Creates the data array of moves. See the README on what is expected * * If game is not specified, the current game is used */ protected function createMoveData($game = NULL) { if ($game == NULL) { $entityManager = $this->getDoctrine()->getManager(); $repo = $entityManager->getRepository("HackathonGameBundle:Game"); $game = $repo->findCurrentGame(); } $gameLogic = new ConnectFour($game); $options = $gameLogic->getOptions(); // Prepare the data $formattedOptions = array(); foreach ($options as $key) { $option = array("id" => $key, "name" => "Column " . ($key + 1)); $formattedOptions[] = $option; } // Prepare final objects $data = array("moves" => $formattedOptions); return $data; }