Ejemplo n.º 1
0
 /**
  * @param TeamGame $teamGame
  * @param GameTask $gameTask
  * @param TaskCode $taskCode
  * @param User $user
  * @return array
  */
 public function processGame(TeamGame $teamGame, GameTask $gameTask, TaskCode $taskCode, User $user)
 {
     $teamGameCodeRepository = $this->_em->getRepository(TeamGameCode::class);
     $teamGameCode = $teamGameCodeRepository->findOneBy(['teamGame' => $teamGame, 'taskCode' => $taskCode]);
     if ($teamGameCode) {
         $flashMessage = sprintf("Код уже был введен игроком %s", $teamGameCode->getInductedUser()->getUsername());
         $flashType = 'success';
     } else {
         $flashType = 'success';
         $flashMessage = 'Код принят';
         $user->increaseLevel();
         $this->_em->persist($user);
         $this->_em->flush();
         $teamGameCodeRepository->createNew($teamGame, $taskCode, $user);
         $result = true;
         //проверяем есть ли введеный код с спике кодов задания
         foreach ($gameTask->getCodes() as $taskCode) {
             if (!$teamGame->containsTaskCode($taskCode)) {
                 $result = false;
             }
         }
         if ($result) {
             $flashType = 'success';
             $flashMessage = 'Поздравляем! Вы перешли к следующему заданию.';
             $teamGame->setTaskLevel($gameTask->getLevel() + 1);
             $this->_em->persist($teamGame);
             $this->_em->flush();
             $gameTask = $this->_em->getRepository(GameTask::class)->findCurrentTask($teamGame);
             //Если следующее задание не найдено, значит игра завершена
             if (!$gameTask) {
                 $flashType = 'success';
                 $flashMessage = 'Поздравляем! Вы завершили игру!';
                 $team = $teamGame->getTeam();
                 $team->increaseWins();
                 $teamGame->setFinishedAt(new \DateTime());
                 $teamGame->setStatus(TeamGame::STATUS_FINISHED);
                 $finishedGame = $this->_em->getRepository(TeamGame::class)->findOneBy(['game' => $teamGame->getGame(), 'status' => TeamGame::STATUS_FINISHED]);
                 foreach ($team->getMembers() as $user) {
                     $user->increaseGamesPlayed();
                     if (!$finishedGame) {
                         $user->increaseGamesWon();
                     }
                     $this->_em->persist($user);
                 }
                 $this->_em->persist($teamGame);
                 $this->_em->persist($team);
                 $this->_em->flush();
             }
         }
     }
     return [$flashType, $flashMessage];
 }
 /**
  * @Route("/{game}/edit/task/{gameTask}", name="admin_game_edit_task")
  * @param Game $game
  * @param GameTask $gameTask
  * @param Request $request
  * @return Response
  */
 public function gameEditTaskAction(Game $game, GameTask $gameTask, Request $request)
 {
     $gameTask->setGame($game);
     $form = $this->createForm(GameTaskType::class, $gameTask);
     $form->handleRequest($request);
     if ($form->isValid()) {
         $listCodes = [];
         $error = false;
         foreach ($form->getData()->getCodes() as $taskCode) {
             $code = strtolower($taskCode->getCode());
             if (!isset($listCodes[$code])) {
                 $listCodes[$code] = $taskCode;
             } else {
                 $error = true;
                 $this->addFlash('danger', 'Все коды должны быть уникальными (регистр не учитывается)');
             }
         }
         if (!$error) {
             $this->addFlash('success', 'Изменения сохранены');
             $manager = $this->getDoctrine()->getManager();
             $manager->persist($gameTask);
             $manager->flush();
         }
     }
     return $this->render('AdminBundle:GameTask:edit.html.twig', ['form' => $form->createView(), 'game' => $game, 'gameTask' => $gameTask]);
 }