src/Controller/GameModeController.php line 54

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Room;
  4. use App\Form\GameType;
  5. use App\Services\GameManager;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Exception;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. class GameModeController extends AbstractController
  12. {
  13.     public function __construct(
  14.         private readonly EntityManagerInterface $_em,
  15.     )
  16.     {}
  17.     /**
  18.      * @param GameManager $gm
  19.      * @return Response
  20.      * @throws Exception
  21.      */
  22.     #[Route("/game"name"game_room")]
  23.     public function index(GameManager $gm): Response
  24.     {
  25.         $user $this->getUser();
  26.         if (!$user) {
  27.             return $this->redirectToRoute('app_login');
  28.         } else {
  29.             $room $user->getRoom();
  30.             if (!$room) {
  31.                 $room $this->roomCreate();
  32.                 $gm->saveUserOnRoom($user$room);
  33.             }
  34.         }
  35.         return $this->render('game/index.html.twig', [
  36.             'page_title' => 'Phasmophobia Randomizer',
  37.             'page_description' => 'Welcome back, we\'ve got some work ready for you!',
  38.             'meta_title' => 'Randomize your Phasmophobia game',
  39.             'meta_description' => 'Randomize your Phasmophobia games among various game modes such as classic, photo safari, night fever from 1 to 4 players. It’s a great way to make your Phasmophobia games more challenging!',
  40.             'roomNumber' => $room->getRoomId()
  41.         ]);
  42.     }
  43.     /**
  44.      * @return Response
  45.      */
  46.     #[Route("/game/offline"name"offline_game_room")]
  47.     public function indexOffline(): Response
  48.     {
  49.         $classicForm $this->createForm(GameType::class);
  50.         return $this->render('game/unlogged.html.twig', [
  51.             'page_title' => 'Phasmophobia Randomizer',
  52.             'page_description' => 'Welcome back, we\'ve got some work ready for you!',
  53.             'meta_title' => 'Randomize your Phasmophobia game',
  54.             'meta_description' => 'Randomize your Phasmophobia games among various game modes such as classic, photo safari, night fever from 1 to 4 players. It’s a great way to make your Phasmophobia games more challenging!',
  55.             'classicForm' => $classicForm->createView(),
  56.             'roomNumber' => 'Room 666'
  57.         ]);
  58.     }
  59.     /**
  60.      * @param string $roomId
  61.      * @return Response
  62.      */
  63.     #[Route("/close-game/{roomId<\d{6}>}"name"close_game")]
  64.     public function closeRoom(string $roomId): Response
  65.     {
  66.         $response $this->roomRemove($roomId);
  67.         if ($response) {
  68.             return $this->redirectToRoute('index');
  69.         }
  70.         return $this->redirectToRoute('game_room');
  71.     }
  72.     /**
  73.      * @param GameManager $gm
  74.      * @param int $roomId
  75.      * @return Response
  76.      */
  77.     #[Route("/room/{roomId<\d{6}>}"name"public_room")]
  78.     public function room(GameManager $gmint $roomId): Response
  79.     {
  80.         $room $this->_em->getRepository(Room::class)->findOneBy(['roomId' => $roomId]);
  81.         if (!$room) {
  82.             $this->addFlash('danger''Room not found !');
  83.             return $this->redirectToRoute('index');
  84.         }
  85.         if ($gm->isRoomFull($room)) {
  86.             $this->addFlash('danger''The selected room is full !');
  87.             return $this->redirectToRoute('index');
  88.         }
  89.         if ($gm->isRoomClosed($room)) {
  90.             $this->addFlash('danger''The selected room is closed !');
  91.             return $this->redirectToRoute('index');
  92.         }
  93.         $user $this->getUser();
  94.         if ($gm->isUserInOtherRoom($user)) {
  95.             $gm->saveUserOnRoom($user$room);
  96.         }
  97.         return $this->render('room/index.html.twig', [
  98.             'page_title' => 'Phasmophobia Randomizer',
  99.             'page_description' => 'Your are entered inner a shared room !',
  100.             'meta_title' => 'Randomize your Phasmophobia game',
  101.             'meta_description' => 'Randomize your Phasmophobia games among various game modes such as classic, photo safari, night fever from 1 to 4 players. It’s a great way to make your Phasmophobia games more challenging!',
  102.             "roomNumber" => $room->getRoomId(),
  103.             "hunters" => $room->getHunters()
  104.         ]);
  105.     }
  106.     /**
  107.      * @return Response
  108.      */
  109.     #[Route("/room-closed"name"room_closed")]
  110.     public function roomClosed(): Response
  111.     {
  112.         return $this->render('/room/room_closed.html.twig', [
  113.             'page_title' => 'Phasmophobia Randomizer',
  114.             'page_description' => 'Server connection failed',
  115.             'meta_title' => 'Randomize your Phasmophobia game',
  116.             'meta_description' => 'Randomize your Phasmophobia games among various game modes such as classic, photo safari, night fever from 1 to 4 players. It’s a great way to make your Phasmophobia games more challenging!'
  117.         ]);
  118.     }
  119.     /**
  120.      * @throws Exception
  121.      */
  122.     private function roomCreate(): Room
  123.     {
  124.         /** @var Room $rooms */
  125.         $rooms $this->_em->getRepository(Room::class)->findAll();
  126.         $roomList = [];
  127.         foreach ($rooms as $room) {
  128.             $roomList[] = $room->getRoomId();
  129.         }
  130.         $room = new Room();
  131.         $room->setRoomId(0);
  132.         while ($room->getRoomId() === 0) {
  133.             $nb random_int(100000999999);
  134.             if (!in_array($nb$roomList)) {
  135.                 $room->setRoomId($nb)
  136.                     ->setOwner($this->getUser()->getUsername());
  137.                 $this->_em->persist($room);
  138.                 $this->_em->flush();
  139.             }
  140.         }
  141.         return $room;
  142.     }
  143.     /**
  144.      * @param string $roomId
  145.      * @return bool
  146.      */
  147.     private function roomRemove(string $roomId): bool
  148.     {
  149.         $roomId htmlentities($roomId);
  150.         $room $this->_em->getRepository(Room::class)->findOneBy(["roomId" => $roomId]);
  151.         if ($room && $room->getOwner() === $this->getUser()->getUsername()) {
  152.             foreach ($room->getHunters() as $hunter) {
  153.                 $hunter->setItems([]);
  154.                 $this->_em->persist($hunter);
  155.             }
  156.             $this->_em->remove($room);
  157.             $this->_em->flush();
  158.             return true;
  159.         }
  160.         return false;
  161.     }
  162. }