1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller; |
4
|
|
|
|
5
|
|
|
use App\Card\CardHand; |
6
|
|
|
use App\Card\Deck; |
7
|
|
|
use App\Card\Game21; |
8
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
use Symfony\Component\HttpFoundation\Response; |
12
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
13
|
|
|
use Symfony\Component\Yaml\Exception\RuntimeException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Controller to handle Game21 logic. |
17
|
|
|
*/ |
18
|
|
|
class Game21Controller extends AbstractController |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Landing page of the game. |
22
|
|
|
* |
23
|
|
|
* @return Response Renders the home page Twig template. |
24
|
|
|
*/ |
25
|
|
|
#[Route("/game", name: "init_page")] |
26
|
|
|
public function landingPage(): Response |
27
|
|
|
{ |
28
|
|
|
return $this->render('game/home.html.twig'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Documentation page for the game. |
33
|
|
|
* |
34
|
|
|
* @return Response Renders the documentation Twig template. |
35
|
|
|
*/ |
36
|
|
|
#[Route("/game/doc", name: "documentation")] |
37
|
|
|
public function doc(): Response |
38
|
|
|
{ |
39
|
|
|
return $this->render('game/doc.html.twig'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Handles the display of the game play page. |
44
|
|
|
* |
45
|
|
|
* @param SessionInterface $session Session to store game state. |
46
|
|
|
* |
47
|
|
|
* @return Response Renders the game play Twig template. |
48
|
|
|
* @throws RuntimeException if the stored session game object is invalid. |
49
|
|
|
*/ |
50
|
|
|
#[Route("/game/play", name: "play_game", methods: ["GET"])] |
51
|
|
|
public function play(SessionInterface $session): Response |
52
|
|
|
{ |
53
|
|
|
//Init the game |
54
|
|
|
if (!$session->has('game21')) { |
55
|
|
|
$deck = new Deck(); |
56
|
|
|
$deck->shuffle(); |
57
|
|
|
$playerHand = new CardHand(); |
58
|
|
|
$bankHand = new CardHand(); |
59
|
|
|
$game = new Game21($deck, $playerHand, $bankHand); |
60
|
|
|
$session->set('game21', $game); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// Retrieve game object from session |
64
|
|
|
$game = $session->get('game21'); |
65
|
|
|
|
66
|
|
|
// Validate the object type |
67
|
|
|
if (!$game instanceof Game21) { |
68
|
|
|
throw new RuntimeException("Invalid game instance in session"); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this->render('game/play.html.twig', [ |
72
|
|
|
'game' => $game, |
73
|
|
|
'gameOver' => false, |
74
|
|
|
'result' => '', |
75
|
|
|
]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Handles the game play. |
80
|
|
|
* |
81
|
|
|
* @param SessionInterface $session Session to store game state. |
82
|
|
|
* |
83
|
|
|
* @return Response Renders the game play Twig template. |
84
|
|
|
* @throws RuntimeException if the stored session game object is invalid. |
85
|
|
|
*/ |
86
|
|
|
#[Route("/game/play", name: "play_game_post", methods: ["POST"])] |
87
|
|
|
public function handlePlayer(SessionInterface $session, Request $request): Response |
88
|
|
|
{ |
89
|
|
|
$game = $session->get('game21'); |
90
|
|
|
|
91
|
|
|
if (!$game instanceof Game21) { |
92
|
|
|
throw new RuntimeException("Invalid game instance in session"); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// Get the action by the player (draw or stay) |
96
|
|
|
$action = $request->request->get('action'); |
97
|
|
|
|
98
|
|
|
// Process player action |
99
|
|
|
if (!$game->isGameOver()) { |
100
|
|
|
if ($action === 'draw') { |
101
|
|
|
$game->drawForPlayer(); |
102
|
|
|
} elseif ($action === 'stay') { |
103
|
|
|
$game->drawForBank(); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$session->set('game21', $game); |
108
|
|
|
|
109
|
|
|
return $this->render('game/play.html.twig', [ |
110
|
|
|
'game' => $game, |
111
|
|
|
'gameOver' => $game->isGameOver(), |
112
|
|
|
'result' => $game->getResult(), |
113
|
|
|
]); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Restart the game route. |
118
|
|
|
* |
119
|
|
|
* @param SessionInterface $session Session to store game state. |
120
|
|
|
* |
121
|
|
|
* @return Response Redirects to the game play route. |
122
|
|
|
*/ |
123
|
|
|
#[Route("/game/restart", name: "game_restart")] |
124
|
|
|
public function restart(SessionInterface $session): Response |
125
|
|
|
{ |
126
|
|
|
$session->remove('game21'); |
127
|
|
|
return $this->redirectToRoute('play_game'); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|