1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller; |
4
|
|
|
|
5
|
|
|
use App\Card\BlackJack; |
6
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
9
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Project Controller. |
13
|
|
|
*/ |
14
|
|
|
final class ProjectController extends AbstractController |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Renders the project index page. |
18
|
|
|
*/ |
19
|
|
|
#[Route('/proj', name: 'project')] |
20
|
|
|
public function proj(SessionInterface $session): Response |
21
|
|
|
{ |
22
|
|
|
$playerName = $session->get('playerName', null); |
23
|
|
|
|
24
|
|
|
return $this->render('proj/home.html.twig', [ |
25
|
|
|
'playerName' => $playerName, |
26
|
|
|
]); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Renders the project about page. |
31
|
|
|
*/ |
32
|
|
|
#[Route("/proj/about", name: "project_about")] |
33
|
|
|
public function about(): Response |
34
|
|
|
{ |
35
|
|
|
return $this->render('proj/about.html.twig'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Show current session data. |
40
|
|
|
* |
41
|
|
|
* @param SessionInterface $session |
42
|
|
|
* @return Response |
43
|
|
|
*/ |
44
|
|
|
#[Route("/proj/session", name: "proj_show_session")] |
45
|
|
|
public function showSession(SessionInterface $session): Response |
46
|
|
|
{ |
47
|
|
|
$sessionData = $session->all(); |
48
|
|
|
|
49
|
|
|
return $this->render('proj/session.html.twig', [ |
50
|
|
|
'sessionData' => $sessionData, |
51
|
|
|
]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Clear session data and redirect. |
56
|
|
|
* |
57
|
|
|
* @param SessionInterface $session |
58
|
|
|
* @return Response |
59
|
|
|
*/ |
60
|
|
|
#[Route("/proj/session/delete", name: "proj_delete_session")] |
61
|
|
|
public function deleteSession(SessionInterface $session): Response |
62
|
|
|
{ |
63
|
|
|
$session->clear(); // Clear all session data |
64
|
|
|
|
65
|
|
|
// Add a flash message |
66
|
|
|
$this->addFlash('success', 'Session data has been deleted.'); |
67
|
|
|
|
68
|
|
|
return $this->redirectToRoute('proj_show_session'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Display the home page where player enters name and bet. |
73
|
|
|
* |
74
|
|
|
* @param SessionInterface $session The current session. |
75
|
|
|
* |
76
|
|
|
* @return Response The rendered home page. |
77
|
|
|
*/ |
78
|
|
|
#[Route('/proj/bet', name: 'enter_bet', methods: ['GET'])] |
79
|
|
|
public function blackJackBet(SessionInterface $session): Response |
80
|
|
|
{ |
81
|
|
|
$playerName = $session->get('playerName', null); |
82
|
|
|
|
83
|
|
|
return $this->render('proj/home.html.twig', [ |
84
|
|
|
'playerName' => $playerName, |
85
|
|
|
]); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Display the game over page when the player has no funds left. |
90
|
|
|
* |
91
|
|
|
* @param SessionInterface $session The current session. |
92
|
|
|
* |
93
|
|
|
* @return Response The rendered game over page. |
94
|
|
|
*/ |
95
|
|
|
#[Route("/proj/gameover", name: "proj_game_over")] |
96
|
|
|
public function gameOver(SessionInterface $session): Response |
97
|
|
|
{ |
98
|
|
|
$blackJack = $session->get('blackJack'); |
99
|
|
|
$balance = ($blackJack instanceof BlackJack) ? $blackJack->getBalance() : 0; |
100
|
|
|
|
101
|
|
|
return $this->render('proj/gameover.html.twig', [ |
102
|
|
|
'balance' => $balance, |
103
|
|
|
'playerName' => $session->get('playerName'), |
104
|
|
|
]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Handle cashing out: clears session and thanks the player. |
109
|
|
|
* |
110
|
|
|
* @param SessionInterface $session The current session. |
111
|
|
|
* |
112
|
|
|
* @return Response The rendered cashout page. |
113
|
|
|
*/ |
114
|
|
|
#[Route("/proj/cashout", name: "proj_cashout")] |
115
|
|
|
public function cashOut(SessionInterface $session): Response |
116
|
|
|
{ |
117
|
|
|
$blackJack = $session->get('blackJack'); |
118
|
|
|
$balance = ($blackJack instanceof BlackJack) ? $blackJack->getBalance() : 0; |
119
|
|
|
|
120
|
|
|
$playerName = $session->get('playerName', 'Player'); |
121
|
|
|
$playerName = is_string($playerName) ? $playerName : 'Player'; |
122
|
|
|
|
123
|
|
|
$session->clear(); |
124
|
|
|
|
125
|
|
|
$this->addFlash('success', "$playerName cashed out with $balance credits. Thanks for playing!"); |
126
|
|
|
|
127
|
|
|
return $this->render('proj/cashout.html.twig'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Clear session data to start a new player. |
132
|
|
|
* |
133
|
|
|
* @param SessionInterface $session |
134
|
|
|
* @return Response |
135
|
|
|
*/ |
136
|
|
|
#[Route("/proj/new", name: "proj_new_player", methods: ["POST"])] |
137
|
|
|
public function newPlayer(SessionInterface $session): Response |
138
|
|
|
{ |
139
|
|
|
$session->clear(); |
140
|
|
|
|
141
|
|
|
return $this->redirectToRoute('project'); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|