<?php
namespace App\Controller;
use App\Entity\Room;
use App\Form\GameType;
use App\Services\GameManager;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class GameModeController extends AbstractController
{
public function __construct(
private readonly EntityManagerInterface $_em,
)
{}
/**
* @param GameManager $gm
* @return Response
* @throws Exception
*/
#[Route("/game", name: "game_room")]
public function index(GameManager $gm): Response
{
$user = $this->getUser();
if (!$user) {
return $this->redirectToRoute('app_login');
} else {
$room = $user->getRoom();
if (!$room) {
$room = $this->roomCreate();
$gm->saveUserOnRoom($user, $room);
}
}
return $this->render('game/index.html.twig', [
'page_title' => 'Phasmophobia Randomizer',
'page_description' => 'Welcome back, we\'ve got some work ready for you!',
'meta_title' => 'Randomize your Phasmophobia game',
'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!',
'roomNumber' => $room->getRoomId()
]);
}
/**
* @return Response
*/
#[Route("/game/offline", name: "offline_game_room")]
public function indexOffline(): Response
{
$classicForm = $this->createForm(GameType::class);
return $this->render('game/unlogged.html.twig', [
'page_title' => 'Phasmophobia Randomizer',
'page_description' => 'Welcome back, we\'ve got some work ready for you!',
'meta_title' => 'Randomize your Phasmophobia game',
'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!',
'classicForm' => $classicForm->createView(),
'roomNumber' => 'Room 666'
]);
}
/**
* @param string $roomId
* @return Response
*/
#[Route("/close-game/{roomId<\d{6}>}", name: "close_game")]
public function closeRoom(string $roomId): Response
{
$response = $this->roomRemove($roomId);
if ($response) {
return $this->redirectToRoute('index');
}
return $this->redirectToRoute('game_room');
}
/**
* @param GameManager $gm
* @param int $roomId
* @return Response
*/
#[Route("/room/{roomId<\d{6}>}", name: "public_room")]
public function room(GameManager $gm, int $roomId): Response
{
$room = $this->_em->getRepository(Room::class)->findOneBy(['roomId' => $roomId]);
if (!$room) {
$this->addFlash('danger', 'Room not found !');
return $this->redirectToRoute('index');
}
if ($gm->isRoomFull($room)) {
$this->addFlash('danger', 'The selected room is full !');
return $this->redirectToRoute('index');
}
if ($gm->isRoomClosed($room)) {
$this->addFlash('danger', 'The selected room is closed !');
return $this->redirectToRoute('index');
}
$user = $this->getUser();
if ($gm->isUserInOtherRoom($user)) {
$gm->saveUserOnRoom($user, $room);
}
return $this->render('room/index.html.twig', [
'page_title' => 'Phasmophobia Randomizer',
'page_description' => 'Your are entered inner a shared room !',
'meta_title' => 'Randomize your Phasmophobia game',
'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!',
"roomNumber" => $room->getRoomId(),
"hunters" => $room->getHunters()
]);
}
/**
* @return Response
*/
#[Route("/room-closed", name: "room_closed")]
public function roomClosed(): Response
{
return $this->render('/room/room_closed.html.twig', [
'page_title' => 'Phasmophobia Randomizer',
'page_description' => 'Server connection failed',
'meta_title' => 'Randomize your Phasmophobia game',
'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!'
]);
}
/**
* @throws Exception
*/
private function roomCreate(): Room
{
/** @var Room $rooms */
$rooms = $this->_em->getRepository(Room::class)->findAll();
$roomList = [];
foreach ($rooms as $room) {
$roomList[] = $room->getRoomId();
}
$room = new Room();
$room->setRoomId(0);
while ($room->getRoomId() === 0) {
$nb = random_int(100000, 999999);
if (!in_array($nb, $roomList)) {
$room->setRoomId($nb)
->setOwner($this->getUser()->getUsername());
$this->_em->persist($room);
$this->_em->flush();
}
}
return $room;
}
/**
* @param string $roomId
* @return bool
*/
private function roomRemove(string $roomId): bool
{
$roomId = htmlentities($roomId);
$room = $this->_em->getRepository(Room::class)->findOneBy(["roomId" => $roomId]);
if ($room && $room->getOwner() === $this->getUser()->getUsername()) {
foreach ($room->getHunters() as $hunter) {
$hunter->setItems([]);
$this->_em->persist($hunter);
}
$this->_em->remove($room);
$this->_em->flush();
return true;
}
return false;
}
}