src/Controller/GameController.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Game;
  4. use App\Entity\Player;
  5. use App\Form\GameType;
  6. use App\Form\PlayerType;
  7. use App\Form\SelectGameType;
  8. use App\Service\GameService;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  11. use Symfony\Component\Form\FormError;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. class GameController extends AbstractController
  16. {
  17.     /**
  18.      * @Route("/", name="game_index")
  19.      */
  20.     public function index(Request $request): Response
  21.     {
  22.         $game = (new Game())
  23.             ->setMaxTurnTime(new \DateInterval('PT1M'))
  24.             ->setMaxPlayerTime(new \DateInterval('PT15M'))
  25.         ;
  26.         $newGameForm $this->createForm(GameType::class, $game);
  27.         $joinGameForm $this->get('form.factory')->createNamed('select_game_join'SelectGameType::class);
  28.         $deleteGameForm $this->get('form.factory')->createNamed('select_game_delete'SelectGameType::class);
  29.         $newGameForm->handleRequest($request);
  30.         if($newGameForm->isSubmitted() and $newGameForm->isValid()) {
  31.             $em $this->getDoctrine()->getManager();
  32.             $em->persist($game);
  33.             $em->flush();
  34.             return $this->redirectToRoute('game_landing', ['id' => $game->getId()]);
  35.         }
  36.         $joinGameForm->handleRequest($request);
  37.         if($joinGameForm->isSubmitted() and $joinGameForm->isValid())
  38.             return $this->redirectToRoute('game_landing', ['id' => $joinGameForm->get('game')->getData()->getId()]);
  39.         $deleteGameForm->handleRequest($request);
  40.         if($deleteGameForm->isSubmitted() and $deleteGameForm->isValid())
  41.             return $this->redirectToRoute('game_delete', ['id' => $deleteGameForm->get('game')->getData()->getId()]);
  42.         return $this->render('game/index.html.twig', [
  43.             "new_game_form" => $newGameForm->createView(),
  44.             "join_game_form" => $joinGameForm->createView(),
  45.             "delete_game_form" => $deleteGameForm->createView(),
  46.         ]);
  47.     }
  48.     /**
  49.      * @Route("/landing/{id}", name="game_landing")
  50.      */
  51.     public function landing(Game $gameRequest $request): Response
  52.     {
  53.         $player = (new Player())->setIsHuman(true);
  54.         $playerForm $this->createForm(PlayerType::class, $player);
  55.         $playerForm->handleRequest($request);
  56.         if($playerForm->isSubmitted() and $playerForm->isValid()) {
  57.             if(count($game->getPlayers()) < 2) {
  58.                 $game->addPlayer($player);
  59.                 $this->getDoctrine()->getManager()->flush();
  60.                 return $this->redirectToRoute('game_play', ['id' => $player->getId()]);
  61.             }
  62.             else
  63.                 $playerForm->addError(new FormError("There are already two players in this game."));
  64.         }
  65.         return $this->render('game/landing.html.twig', [
  66.             'game' => $game,
  67.             'player_form' => $playerForm->createView(),
  68.         ]);
  69.     }
  70.     /**
  71.      * @Route("/delete/{id}", name="game_delete")
  72.      */
  73.     public function delete(Game $gameRequest $request): Response
  74.     {
  75.         $form $this->createFormBuilder()
  76.             ->add('password'PasswordType::class)
  77.             ->getForm();
  78.         $form->handleRequest($request);
  79.         if($form->isSubmitted() and $form->isValid())
  80.             if($form->get('password')->getData() == $this->getParameter('password')) {
  81.                 $em $this->getDoctrine()->getManager();
  82.                 $em->remove($game);
  83.                 $em->flush();
  84.                 $this->addFlash('success'"The specified game has been deleted successfully.");
  85.                 return $this->redirectToRoute('game_index');
  86.             }
  87.             else
  88.                 $form->get('password')->addError(new FormError("The entered password is incorrect."));
  89.         return $this->render('game/delete.html.twig', [
  90.             'game' => $game,
  91.             'form' => $form->createView(),
  92.         ]);
  93.     }
  94.     /**
  95.      * @Route("/play/{id}", name="game_play")
  96.      */
  97.     public function play(Player $playerGameService $gs): Response
  98.     {
  99.         $game $player->getGame();
  100.         $opponent $player->getOpponent() ?? null;
  101.         if($opponent) {
  102.             try { $gs->initGame($game$opponent); }
  103.             catch(\Exception $e) { }
  104.         }
  105.         return $this->render('game/play.html.twig', [
  106.             'game' => $game,
  107.             'player' => $player,
  108.             'opponent' => $opponent,
  109.         ]);
  110.     }
  111.     /**
  112.      * @Route("/spectate/{id}", name="game_spectate")
  113.      */
  114.     public function spectate(Game $game): Response
  115.     {
  116.         return $this->render('game/play.html.twig', [
  117.             'spectate' => true,
  118.             'game' => $game,
  119.         ]);
  120.     }
  121. }