1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller; |
4
|
|
|
|
5
|
|
|
use App\Card\Deck; |
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
|
|
|
* Controller to handle card game. |
13
|
|
|
*/ |
14
|
|
|
class CardGameController extends AbstractController |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Render the home page of the card game. |
18
|
|
|
* |
19
|
|
|
* @return Response |
20
|
|
|
*/ |
21
|
|
|
#[Route("/card", name: "card_start")] |
22
|
|
|
public function home(): Response |
23
|
|
|
{ |
24
|
|
|
return $this->render('card/home.html.twig'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Show current session data. |
29
|
|
|
* |
30
|
|
|
* @param SessionInterface $session |
31
|
|
|
* @return Response |
32
|
|
|
*/ |
33
|
|
|
#[Route("/card/session", name: "show_session")] |
34
|
|
|
public function showSession(SessionInterface $session): Response |
35
|
|
|
{ |
36
|
|
|
$sessionData = $session->all(); |
37
|
|
|
|
38
|
|
|
return $this->render('card/session.html.twig', [ |
39
|
|
|
'sessionData' => $sessionData, |
40
|
|
|
]); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Clear session data and redirect. |
45
|
|
|
* |
46
|
|
|
* @param SessionInterface $session |
47
|
|
|
* @return Response |
48
|
|
|
*/ |
49
|
|
|
#[Route("/card/session/delete", name: "delete_session")] |
50
|
|
|
public function deleteSession(SessionInterface $session): Response |
51
|
|
|
{ |
52
|
|
|
$session->clear(); // Clear all session data |
53
|
|
|
|
54
|
|
|
// Add a flash message |
55
|
|
|
$this->addFlash('success', 'Session data has been deleted.'); |
56
|
|
|
|
57
|
|
|
return $this->redirectToRoute('show_session'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Display the deck in sorted order. |
62
|
|
|
* |
63
|
|
|
* @param SessionInterface $session |
64
|
|
|
* @return Response |
65
|
|
|
*/ |
66
|
|
|
#[Route("/card/deck/", name: "sorted_deck")] |
67
|
|
|
public function sorted(SessionInterface $session): Response |
68
|
|
|
{ |
69
|
|
|
$deck = $this->getOrInitDeck($session); |
70
|
|
|
|
71
|
|
|
return $this->render('card/deck/sorted.html.twig', [ |
72
|
|
|
'deck' => $deck->getCards() |
73
|
|
|
]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Shuffle the deck. |
78
|
|
|
* |
79
|
|
|
* @param SessionInterface $session |
80
|
|
|
* @return Response |
81
|
|
|
*/ |
82
|
|
|
#[Route("/card/deck/shuffle", name: "shuffle_deck")] |
83
|
|
|
public function shuffle(SessionInterface $session): Response |
84
|
|
|
{ |
85
|
|
|
$deck = $this->getOrInitDeck($session); |
86
|
|
|
$deck->shuffle(); |
87
|
|
|
$session->set('deck', $deck); |
88
|
|
|
|
89
|
|
|
return $this->render('card/deck/shuffle.html.twig', [ |
90
|
|
|
'deck' => $deck->getCards() |
91
|
|
|
]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Draw a single card from the deck. |
96
|
|
|
* |
97
|
|
|
* @param SessionInterface $session |
98
|
|
|
* @return Response |
99
|
|
|
*/ |
100
|
|
|
#[Route("/card/deck/draw", name: "draw_card")] |
101
|
|
|
public function drawCard(SessionInterface $session): Response |
102
|
|
|
{ |
103
|
|
|
$deck = $this->getOrInitDeck($session); |
104
|
|
|
$drawnCard = $deck->drawCard(); |
105
|
|
|
if ($drawnCard !== null) { |
106
|
|
|
$remainingCardsCount = $deck->remainingCards(); |
107
|
|
|
|
108
|
|
|
$session->set('drawnCard', $drawnCard); |
109
|
|
|
$session->set('remainingCardsCount', $remainingCardsCount); |
110
|
|
|
$session->set('remainingCards', $deck->getCards()); |
111
|
|
|
|
112
|
|
|
return $this->render('card/deck/draw.html.twig', [ |
113
|
|
|
'drawnCard' => $drawnCard, |
114
|
|
|
'remainingCardsCount' => $remainingCardsCount, |
115
|
|
|
'remainingCards' => $deck->getCards() |
116
|
|
|
]); |
117
|
|
|
} |
118
|
|
|
// If no more cards in the deck |
119
|
|
|
// Redirect to empty deck page |
120
|
|
|
return $this->redirectToRoute('empty_deck'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Draw multiple cards from the deck. |
125
|
|
|
* |
126
|
|
|
* @param int $num Number of cards to draw |
127
|
|
|
* @param SessionInterface $session |
128
|
|
|
* @return Response |
129
|
|
|
*/ |
130
|
|
|
#[Route("/card/deck/draw/{num<\d+>}", name: "draw_cards")] |
131
|
|
|
public function drawCards(int $num, SessionInterface $session): Response |
132
|
|
|
{ |
133
|
|
|
$deck = $this->getOrInitDeck($session); |
134
|
|
|
// Draw the specified number of cards from the deck |
135
|
|
|
$drawnCards = []; |
136
|
|
|
for ($i = 0; $i < $num; $i++) { |
137
|
|
|
$drawnCard = $deck->drawCard(); |
138
|
|
|
if ($drawnCard !== null) { |
139
|
|
|
$drawnCards[] = $drawnCard; |
140
|
|
|
continue; |
141
|
|
|
} |
142
|
|
|
return $this->redirectToRoute('empty_deck'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$remainingCardsCount = $deck->remainingCards(); |
146
|
|
|
|
147
|
|
|
$session->set('drawnCards', $drawnCards); |
148
|
|
|
$session->set('remainingCardsCount', $remainingCardsCount); |
149
|
|
|
$session->set('remainingCards', $deck->getCards()); |
150
|
|
|
|
151
|
|
|
$session->set('deck', $deck); |
152
|
|
|
|
153
|
|
|
return $this->render('card/deck/draw_few.html.twig', [ |
154
|
|
|
'drawnCards' => $drawnCards, |
155
|
|
|
'remainingCards' => $deck->getCards(), |
156
|
|
|
'remainingCardsCount' => $deck->remainingCards() |
157
|
|
|
]); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Render a page that the deck is empty. |
162
|
|
|
* |
163
|
|
|
* @return Response |
164
|
|
|
*/ |
165
|
|
|
#[Route("/card/deck/empty", name: "empty_deck")] |
166
|
|
|
public function emptyDeck(): Response |
167
|
|
|
{ |
168
|
|
|
return $this->render('card/deck/empty_deck.html.twig'); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** Helper method to get or init the deck */ |
172
|
|
|
private function getOrInitDeck(SessionInterface $session): Deck |
173
|
|
|
{ |
174
|
|
|
$deck = $session->get('deck'); |
175
|
|
|
|
176
|
|
|
if (!$deck instanceof Deck) { |
177
|
|
|
$deck = new Deck(); |
178
|
|
|
$session->set('deck', $deck); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $deck; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|