1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
7
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
8
|
|
|
use App\Card\Card; |
9
|
|
|
use App\Card\CardGraphic; |
10
|
|
|
use App\Card\CardHand; |
11
|
|
|
use App\Card\DeckOfCards; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
14
|
|
|
|
15
|
|
|
class CardGameController extends AbstractController |
16
|
|
|
{ |
17
|
|
|
#[Route("/card", name: "card_start")] |
18
|
|
|
public function home( |
19
|
|
|
SessionInterface $session |
|
|
|
|
20
|
|
|
): Response { |
21
|
|
|
// $session->set("status", "Started"); |
22
|
|
|
|
23
|
|
|
return $this->render('card/home.html.twig'); |
24
|
|
|
} |
25
|
|
|
#[Route("/card/deck", name: "card_deck")] |
26
|
|
|
public function deck( |
27
|
|
|
SessionInterface $session |
28
|
|
|
): Response { |
29
|
|
|
|
30
|
|
|
$session->clear("card_deck"); |
|
|
|
|
31
|
|
|
$session->clear("hand"); |
32
|
|
|
$deck = new DeckOfCards(); |
33
|
|
|
$this->addFlash( |
34
|
|
|
'notice', |
35
|
|
|
'Full deck restored!' |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
$session->set("card_deck", $deck); |
39
|
|
|
$data = [ |
40
|
|
|
"deck" => $deck->getDeck() |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
return $this->render('card/deck.html.twig', $data); |
44
|
|
|
} |
45
|
|
|
#[Route("/card/deck/shuffle", name: "card_deck_shuffle")] |
46
|
|
|
public function shuffle( |
47
|
|
|
SessionInterface $session |
48
|
|
|
): Response { |
49
|
|
|
$deck = $session->get("card_deck"); |
50
|
|
|
if ($deck === null || $deck->getNumberOfCards() < 52) { |
51
|
|
|
$session->clear("card_deck"); |
|
|
|
|
52
|
|
|
$session->clear("hand"); |
53
|
|
|
$deck = new DeckOfCards(); |
54
|
|
|
$this->addFlash( |
55
|
|
|
'notice', |
56
|
|
|
'Full deck restored!' |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
$deck->shuffle(); |
60
|
|
|
$session->set("card_deck", $deck); |
61
|
|
|
$data = [ |
62
|
|
|
"deck" => $deck->getDeck() |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
return $this->render('card/deck.html.twig', $data); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
#[Route("/card/deck/draw", name: "card_deck_draw")] |
69
|
|
|
public function draw( |
70
|
|
|
SessionInterface $session |
71
|
|
|
): Response { |
72
|
|
|
$cardsStr = []; |
73
|
|
|
$deck = $session->get("card_deck"); |
74
|
|
|
if ($deck == null) { |
75
|
|
|
throw new \Exception("Ingen kortlek finns. Skapa en ny kortlek innan du drar ett kort."); |
76
|
|
|
} |
77
|
|
|
$hand = $session->get("hand"); |
78
|
|
|
if ($hand == null) { |
79
|
|
|
$hand = new CardHand(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$card = $deck->drawCard(); |
83
|
|
|
$cardsStr[] = $card->getAsString(); |
84
|
|
|
$session->set("card_deck", $deck); |
85
|
|
|
$hand->add($card); |
86
|
|
|
$session->set("hand", $hand); |
87
|
|
|
|
88
|
|
|
$data = [ |
89
|
|
|
"cardsStr" => $cardsStr, |
90
|
|
|
"cards_left" => $deck->getNumberOfCards(), |
91
|
|
|
"hand" => $hand->getString() |
92
|
|
|
]; |
93
|
|
|
|
94
|
|
|
return $this->render('card/cards.html.twig', $data); |
95
|
|
|
} |
96
|
|
|
#[Route("/card/deck/draw/{num<\d+>}", name: "card_deck_draw_multiple")] |
97
|
|
|
public function draw_multiple( |
98
|
|
|
int $num, |
99
|
|
|
SessionInterface $session |
100
|
|
|
): Response { |
101
|
|
|
$deck = $session->get("card_deck"); |
102
|
|
|
|
103
|
|
|
if ($deck == null) { |
104
|
|
|
throw new \Exception("Ingen kortlek finns. Skapa en ny kortlek innan du drar ett kort."); |
105
|
|
|
} |
106
|
|
|
$cardsleft = $deck->getNumberOfCards(); |
107
|
|
|
if ($num > $cardsleft) { |
108
|
|
|
throw new \Exception("Du kan inte dra fler kort än det finns kvar i kortleken! (" . $cardsleft . " st)"); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$hand = $session->get("hand"); |
112
|
|
|
if ($hand == null) { |
113
|
|
|
$hand = new CardHand(); |
114
|
|
|
} |
115
|
|
|
$cards = []; |
116
|
|
|
$cardsStr = []; |
117
|
|
|
for ($i = 0; $i < $num; $i++) { |
118
|
|
|
$cards[] = $deck->drawCard(); |
119
|
|
|
$cardsStr[] = $cards[$i]->getAsString(); |
120
|
|
|
$hand->add($cards[$i]); |
121
|
|
|
} |
122
|
|
|
$session->set("card_deck", $deck); |
123
|
|
|
$session->set("hand", $hand); |
124
|
|
|
$data = [ |
125
|
|
|
"cardsStr" => $cardsStr, |
126
|
|
|
"cards_left" => $deck->getNumberOfCards(), |
127
|
|
|
"hand" => $hand->getString() |
128
|
|
|
]; |
129
|
|
|
|
130
|
|
|
return $this->render('card/cards.html.twig', $data); |
131
|
|
|
} |
132
|
|
|
#[Route("/card/deck/cardsleft", name: "cardsleft")] |
133
|
|
|
public function cardsleft( |
134
|
|
|
SessionInterface $session |
135
|
|
|
): Response { |
136
|
|
|
$deck = $session->get("card_deck"); |
137
|
|
|
|
138
|
|
|
$data = [ |
139
|
|
|
"deck" => $deck->getDeck(), |
140
|
|
|
"cards_left" => $deck->getNumberOfCards() |
141
|
|
|
]; |
142
|
|
|
|
143
|
|
|
return $this->render('card/cardsleft.html.twig', $data); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
#[Route("/session", name: "session")] |
148
|
|
|
public function session( |
149
|
|
|
SessionInterface $session |
150
|
|
|
): Response { |
151
|
|
|
date_default_timezone_set('CET'); |
152
|
|
|
// $session->set("last_check", date('D M j G:i:s T Y')); |
153
|
|
|
$sessionMetadata = $session->getMetadataBag(); |
154
|
|
|
|
155
|
|
|
$sessionData = $session->all(); |
156
|
|
|
|
157
|
|
|
$metaData = [ |
|
|
|
|
158
|
|
|
"Created" => date('D M j G:i:s T Y', $sessionMetadata->getCreated()), |
159
|
|
|
"Last used" => date('D M j G:i:s T Y', $sessionMetadata->getLastUsed()), |
160
|
|
|
"Lifetime (seconds, where 0 = none)" => $sessionMetadata->getLifetime() |
161
|
|
|
]; |
162
|
|
|
|
163
|
|
|
$data = [ |
164
|
|
|
"sessionData" => $sessionData, |
165
|
|
|
// "metaData" => $metaData |
166
|
|
|
]; |
167
|
|
|
|
168
|
|
|
return $this->render('session.html.twig', $data); |
169
|
|
|
} |
170
|
|
|
#[Route("/session/delete", name: "session_delete")] |
171
|
|
|
public function deleteSession( |
172
|
|
|
SessionInterface $session |
173
|
|
|
): Response { |
174
|
|
|
$session->clear(); |
175
|
|
|
$this->addFlash( |
176
|
|
|
'notice', |
177
|
|
|
'Session deleted!' |
178
|
|
|
); |
179
|
|
|
return $this->redirectToRoute('session'); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
} |
183
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.