1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller; |
4
|
|
|
|
5
|
|
|
use App\Card\Card; |
6
|
|
|
use App\Card\CardGraphic; |
7
|
|
|
use App\Card\CardHand; |
8
|
|
|
use App\Card\DeckOfCards; |
9
|
|
|
use Exception; |
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
13
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
14
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
15
|
|
|
|
16
|
|
|
class Kmom02 extends AbstractController |
17
|
|
|
{ |
18
|
1 |
|
#[Route("/session", name: "session", methods: ['GET'])] |
19
|
|
|
public function session( |
20
|
|
|
SessionInterface $session |
21
|
|
|
): Response { |
22
|
|
|
|
23
|
|
|
//Get all session data |
24
|
1 |
|
$allData = $session->all(); |
25
|
|
|
|
26
|
1 |
|
$data = [ |
27
|
1 |
|
"session" => $allData |
28
|
1 |
|
]; |
29
|
|
|
|
30
|
1 |
|
return $this->render('session.html.twig', $data); |
31
|
|
|
} |
32
|
|
|
|
33
|
1 |
|
#[Route("/session/delete", name: "delete_session", methods: ['POST'])] |
34
|
|
|
public function deleteSession( |
35
|
|
|
SessionInterface $session |
36
|
|
|
): Response { |
37
|
|
|
//Clear session content and destroy session |
38
|
1 |
|
$session->clear(); |
39
|
1 |
|
$session->invalidate(); |
40
|
|
|
|
41
|
|
|
//Add info flash that session is deleted |
42
|
1 |
|
$this->addFlash( |
43
|
1 |
|
'notice', |
44
|
1 |
|
'Session is deleted!' |
45
|
1 |
|
); |
46
|
1 |
|
return $this->redirectToRoute('session'); |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
#[Route("/card", name: "card", methods: ['GET'])] |
50
|
|
|
public function card(): Response |
51
|
|
|
{ |
52
|
1 |
|
return $this->render('card.html.twig'); |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
#[Route("/card/deck", name: "card_deck", methods: ['GET', 'POST'])] |
56
|
|
|
public function cardDeck( |
57
|
|
|
SessionInterface $session |
58
|
|
|
): Response { |
59
|
|
|
//Visar samtliga kort i kortleken sorterade per färg och värde. |
60
|
1 |
|
$carddeck = $session->get("card_deck"); |
61
|
|
|
|
62
|
1 |
|
if (!$carddeck) { |
63
|
1 |
|
$carddeck = new DeckOfCards(); |
64
|
|
|
} |
65
|
|
|
/** @var DeckOfCards $carddeck */ |
66
|
|
|
|
67
|
|
|
//Add cards sorted |
68
|
1 |
|
$carddeck->sortCards(); |
69
|
|
|
|
70
|
|
|
//Add deck to session |
71
|
1 |
|
$session->set("card_deck", $carddeck); |
72
|
|
|
|
73
|
1 |
|
$cards = []; |
74
|
1 |
|
foreach ($carddeck->getValues() as $card) { |
75
|
1 |
|
$newCard = new CardGraphic($card); |
76
|
1 |
|
$cards[] = $newCard->getValue(); |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
$data = [ |
80
|
1 |
|
"header" => "All cards sorted", |
81
|
1 |
|
"cardValues" => $cards |
82
|
1 |
|
]; |
83
|
|
|
|
84
|
1 |
|
return $this->render('show_deck.html.twig', $data); |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
#[Route("/card/deck/shuffle", name: "card_shuffle", methods: ['POST'])] |
88
|
|
|
public function cardShuffle( |
89
|
|
|
SessionInterface $session |
90
|
|
|
): Response { |
91
|
|
|
//Återställ kortlek |
92
|
1 |
|
$carddeck = new DeckOfCards(); |
93
|
|
|
|
94
|
|
|
//shuffle carddeck |
95
|
1 |
|
$carddeck->shuffleCards(); |
96
|
|
|
|
97
|
1 |
|
$cards = []; |
98
|
1 |
|
foreach ($carddeck->getValues() as $card) { |
99
|
1 |
|
$newCard = new CardGraphic($card); |
100
|
1 |
|
$cards[] = $newCard->getValue(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
//Sätt uppdaterad deck till session |
104
|
1 |
|
$session->remove('card_deck'); |
105
|
1 |
|
$session->set("card_deck", $carddeck); |
106
|
|
|
|
107
|
1 |
|
$data = [ |
108
|
1 |
|
"header" => "All cards shuffled", |
109
|
1 |
|
"cardValues" => $cards |
110
|
1 |
|
]; |
111
|
|
|
|
112
|
1 |
|
return $this->render('show_deck.html.twig', $data); |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
#[Route("/card/deck/draw", name: "card_draw", methods: ['GET', 'POST'])] |
116
|
|
|
public function cardDraw( |
117
|
|
|
SessionInterface $session |
118
|
|
|
): Response { |
119
|
|
|
//Hämta kortlek från session |
120
|
1 |
|
$carddeck = $session->get("card_deck"); |
121
|
|
|
|
122
|
1 |
|
if (!$carddeck) { |
123
|
1 |
|
$carddeck = new DeckOfCards(); |
124
|
|
|
} |
125
|
|
|
/** @var DeckOfCards $carddeck */ |
126
|
|
|
|
127
|
|
|
//Dra 1 kort från kortleken |
128
|
1 |
|
$values = $carddeck->drawCard(); |
129
|
1 |
|
$card = new CardGraphic($values); |
130
|
|
|
|
131
|
|
|
//Spara uppdaterad deck till session |
132
|
1 |
|
$session->set("card_deck", $carddeck); |
133
|
|
|
|
134
|
|
|
//Visa kort och antal kvar i kortlek |
135
|
1 |
|
$data = [ |
136
|
1 |
|
"header" => "Draw one card", |
137
|
1 |
|
"num_cards" => $carddeck->getNumberCards(), |
138
|
1 |
|
"value" => $card->getValue(), |
139
|
1 |
|
]; |
140
|
|
|
|
141
|
1 |
|
return $this->render('draw_card.html.twig', $data); |
142
|
|
|
} |
143
|
|
|
|
144
|
1 |
|
#[Route("/card/deck/draw/{num<\d+>}", name: "card_hand", methods: ['GET', 'POST'])] |
145
|
|
|
public function cardHand( |
146
|
|
|
int $num, |
147
|
|
|
SessionInterface $session |
148
|
|
|
): Response { |
149
|
|
|
//Hämta kortlek från session |
150
|
1 |
|
$carddeck = $session->get("card_deck"); |
151
|
|
|
|
152
|
1 |
|
if (!$carddeck) { |
153
|
1 |
|
$carddeck = new DeckOfCards(); |
154
|
|
|
} |
155
|
|
|
/** @var DeckOfCards $carddeck */ |
156
|
|
|
|
157
|
1 |
|
if ($num > $carddeck->getNumberCards()) { |
158
|
|
|
throw new Exception("Can not draw more cards than available in deck!"); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
//Dra flera kort från kortlek |
162
|
1 |
|
$hand = new cardHand(); |
163
|
1 |
|
for ($i = 0; $i < $num; $i++) { |
164
|
1 |
|
$value = $carddeck->drawCard(); |
165
|
1 |
|
$card = new CardGraphic($value); |
166
|
1 |
|
$hand->add($card); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
//Spara uppdaterad deck till session |
170
|
1 |
|
$session->set("card_deck", $carddeck); |
171
|
|
|
|
172
|
1 |
|
$data = [ |
173
|
1 |
|
"header" => "Draw multiple cards", |
174
|
1 |
|
"num_cards" => $carddeck->getNumberCards(), |
175
|
1 |
|
"values" => $hand->getValues(), |
176
|
1 |
|
]; |
177
|
|
|
|
178
|
1 |
|
return $this->render('draw_cards.html.twig', $data); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|