<?php
namespace App\Controller;
use App\Entity\Game;
use App\Entity\Player;
use App\Form\GameType;
use App\Form\PlayerType;
use App\Form\SelectGameType;
use App\Service\GameService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class GameController extends AbstractController
{
/**
* @Route("/", name="game_index")
*/
public function index(Request $request): Response
{
$game = (new Game())
->setMaxTurnTime(new \DateInterval('PT1M'))
->setMaxPlayerTime(new \DateInterval('PT15M'))
;
$newGameForm = $this->createForm(GameType::class, $game);
$joinGameForm = $this->get('form.factory')->createNamed('select_game_join', SelectGameType::class);
$deleteGameForm = $this->get('form.factory')->createNamed('select_game_delete', SelectGameType::class);
$newGameForm->handleRequest($request);
if($newGameForm->isSubmitted() and $newGameForm->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($game);
$em->flush();
return $this->redirectToRoute('game_landing', ['id' => $game->getId()]);
}
$joinGameForm->handleRequest($request);
if($joinGameForm->isSubmitted() and $joinGameForm->isValid())
return $this->redirectToRoute('game_landing', ['id' => $joinGameForm->get('game')->getData()->getId()]);
$deleteGameForm->handleRequest($request);
if($deleteGameForm->isSubmitted() and $deleteGameForm->isValid())
return $this->redirectToRoute('game_delete', ['id' => $deleteGameForm->get('game')->getData()->getId()]);
return $this->render('game/index.html.twig', [
"new_game_form" => $newGameForm->createView(),
"join_game_form" => $joinGameForm->createView(),
"delete_game_form" => $deleteGameForm->createView(),
]);
}
/**
* @Route("/landing/{id}", name="game_landing")
*/
public function landing(Game $game, Request $request): Response
{
$player = (new Player())->setIsHuman(true);
$playerForm = $this->createForm(PlayerType::class, $player);
$playerForm->handleRequest($request);
if($playerForm->isSubmitted() and $playerForm->isValid()) {
if(count($game->getPlayers()) < 2) {
$game->addPlayer($player);
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('game_play', ['id' => $player->getId()]);
}
else
$playerForm->addError(new FormError("There are already two players in this game."));
}
return $this->render('game/landing.html.twig', [
'game' => $game,
'player_form' => $playerForm->createView(),
]);
}
/**
* @Route("/delete/{id}", name="game_delete")
*/
public function delete(Game $game, Request $request): Response
{
$form = $this->createFormBuilder()
->add('password', PasswordType::class)
->getForm();
$form->handleRequest($request);
if($form->isSubmitted() and $form->isValid())
if($form->get('password')->getData() == $this->getParameter('password')) {
$em = $this->getDoctrine()->getManager();
$em->remove($game);
$em->flush();
$this->addFlash('success', "The specified game has been deleted successfully.");
return $this->redirectToRoute('game_index');
}
else
$form->get('password')->addError(new FormError("The entered password is incorrect."));
return $this->render('game/delete.html.twig', [
'game' => $game,
'form' => $form->createView(),
]);
}
/**
* @Route("/play/{id}", name="game_play")
*/
public function play(Player $player, GameService $gs): Response
{
$game = $player->getGame();
$opponent = $player->getOpponent() ?? null;
if($opponent) {
try { $gs->initGame($game, $opponent); }
catch(\Exception $e) { }
}
return $this->render('game/play.html.twig', [
'game' => $game,
'player' => $player,
'opponent' => $opponent,
]);
}
/**
* @Route("/spectate/{id}", name="game_spectate")
*/
public function spectate(Game $game): Response
{
return $this->render('game/play.html.twig', [
'spectate' => true,
'game' => $game,
]);
}
}