Created a dashboard and created an invite code for game sessions.
This commit is contained in:
@@ -3,17 +3,72 @@ 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')]
|
||||
public function index(): Response
|
||||
{
|
||||
return $this->render('game/index.html.twig', [
|
||||
'user_id' => $this->getUser()?->getId(),
|
||||
#[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('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);
|
||||
}
|
||||
}
|
||||
|
||||
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): Response
|
||||
{
|
||||
return $this->render('game/index.html.twig', ['session' => $session]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user