58 lines
1.6 KiB
PHP
58 lines
1.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Entity\Game;
|
|
use App\Entity\User;
|
|
use App\Repository\GameRepository;
|
|
use App\Service\GameGridGenerator;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Routing\Attribute\Route;
|
|
|
|
class HomepageController extends AbstractController
|
|
{
|
|
#[Route('/', name: 'app_homepage')]
|
|
public function index(
|
|
Request $request,
|
|
GameRepository $gameRepository,
|
|
GameGridGenerator $gridGenerator,
|
|
): Response {
|
|
/** @var User|null $user */
|
|
$user = $this->getUser();
|
|
|
|
$game = null;
|
|
|
|
if ($user) {
|
|
$game = $gameRepository->findActiveForUser($user);
|
|
} else {
|
|
$gameId = $request->getSession()->get('current_game_id');
|
|
if ($gameId) {
|
|
$game = $gameRepository->find($gameId);
|
|
if (!$game || $game->getStatus() !== Game::STATUS_IN_PROGRESS) {
|
|
$request->getSession()->remove('current_game_id');
|
|
$game = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!$game) {
|
|
return $this->render('homepage/index.html.twig', [
|
|
'game' => null,
|
|
]);
|
|
}
|
|
|
|
$gridData = $gridGenerator->computeGridData($game);
|
|
|
|
return $this->render('homepage/index.html.twig', [
|
|
'game' => $game,
|
|
'grid' => $gridData['grid'],
|
|
'width' => $gridData['width'],
|
|
'middle' => $gridData['middle'],
|
|
]);
|
|
}
|
|
}
|