103 lines
4.1 KiB
PHP
103 lines
4.1 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Game\Controller;
|
|
|
|
use App\Game\Entity\Session;
|
|
use App\Game\Repository\GameRepository;
|
|
use App\Game\Repository\SessionRepository;
|
|
use App\Game\Service\GameDashboardService;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Bundle\SecurityBundle\Security;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Routing\Annotation\Route;
|
|
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
|
use Symfony\Component\ExpressionLanguage\Expression;
|
|
|
|
final class GameController extends AbstractController
|
|
{
|
|
#[Route(path: '', name: 'game_dashboard', methods: ['GET', 'POST'])]
|
|
#[IsGranted(new Expression("is_granted('ROLE_PLAYER') or is_granted('ROLE_ADMIN')"))]
|
|
public function dashboard(
|
|
Request $request,
|
|
GameRepository $gameRepository,
|
|
SessionRepository $sessionRepository,
|
|
GameDashboardService $dashboardService,
|
|
Security $security
|
|
): Response {
|
|
$user = $security->getUser();
|
|
$isAdmin = $this->isGranted('ROLE_ADMIN');
|
|
|
|
if ($request->isMethod('POST')) {
|
|
if ($request->request->has('create_session')) {
|
|
$gameId = $request->request->get('game_id');
|
|
$game = $gameRepository->find($gameId);
|
|
|
|
if ($game) {
|
|
if ($dashboardService->createSession($game, $user, $isAdmin)) {
|
|
$this->addFlash('success', 'New session created!');
|
|
}
|
|
}
|
|
} elseif ($request->request->has('join_session')) {
|
|
$inviteCode = $request->request->get('invite_code');
|
|
if ($dashboardService->joinSession($inviteCode, $user)) {
|
|
$this->addFlash('success', 'Joined session successfully!');
|
|
} else {
|
|
$this->addFlash('error', 'Invalid invite code or session full.');
|
|
}
|
|
} elseif ($request->request->has('create_invite')) {
|
|
$sessionId = $request->request->get('session_id');
|
|
$session = $sessionRepository->find($sessionId);
|
|
|
|
if (!$session) {
|
|
$this->addFlash('error', 'Session not found.');
|
|
return $this->redirectToRoute('game_dashboard');
|
|
}
|
|
|
|
$inviteCode = $dashboardService->generateInviteCode($session, $user, $isAdmin);
|
|
if ($inviteCode) {
|
|
$this->addFlash('success', 'Invite link created: ' . $inviteCode);
|
|
}
|
|
} elseif ($request->request->has('leave_session')) {
|
|
$sessionId = $request->request->get('session_id');
|
|
$session = $sessionRepository->find($sessionId);
|
|
|
|
if ($session) {
|
|
if ($dashboardService->leaveSession($session, $user)) {
|
|
$this->addFlash('success', 'Left session successfully.');
|
|
} else {
|
|
$this->addFlash('error', 'Could not leave session (game might have started).');
|
|
}
|
|
}
|
|
}
|
|
|
|
return $this->redirectToRoute('game_dashboard');
|
|
}
|
|
|
|
return $this->render('game/dashboard.html.twig', [
|
|
'sessions' => $dashboardService->getSessionsForUser($user),
|
|
'availableGames' => $dashboardService->getAvailableGames($isAdmin),
|
|
]);
|
|
}
|
|
|
|
#[Route(path: '/{session}', name: 'game')]
|
|
#[IsGranted(new Expression("is_granted('ROLE_PLAYER') or is_granted('ROLE_ADMIN')"))]
|
|
#[IsGranted('SESSION_VIEW', subject: 'session')]
|
|
public function index(
|
|
Session $session,
|
|
Security $security,
|
|
\App\Game\Repository\PlayerRepository $playerRepository
|
|
): Response
|
|
{
|
|
$user = $security->getUser();
|
|
$player = $playerRepository->findOneBy(['session' => $session, 'user' => $user]);
|
|
$screen = $player ? $player->getScreen() : 0;
|
|
|
|
return $this->render('game/index.html.twig', [
|
|
'session' => $session,
|
|
'screen' => $screen,
|
|
]);
|
|
}
|
|
}
|