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\JsonResponse; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
14
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
15
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
16
|
|
|
use Symfony\Component\Routing\RouterInterface; |
17
|
|
|
|
18
|
|
|
class Kmom02Json extends AbstractController |
19
|
|
|
{ |
20
|
|
|
#[Route("/api/deck", name: 'deck', methods: ['GET'], defaults: ['description' => 'Returnerar en JSON-struktur med hela kortleken sorterad per färg och värde.'])] |
21
|
|
|
public function deck( |
22
|
|
|
SessionInterface $session |
23
|
|
|
): Response { |
24
|
|
|
//Hämta kortlek från session |
25
|
|
|
$carddeck = $session->get("api-deck"); |
26
|
|
|
|
27
|
|
|
if (!$carddeck) { |
28
|
|
|
$carddeck = new DeckOfCards(); |
29
|
|
|
} |
30
|
|
|
/** @var DeckOfCards $carddeck */ |
31
|
|
|
|
32
|
|
|
//Sortera kortlek |
33
|
|
|
$carddeck->sortCards(); |
34
|
|
|
|
35
|
|
|
//Returnera kortlek som JSON struktur |
36
|
|
|
$data = [ |
37
|
|
|
'deck' => $carddeck->getValues() |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
$response = new JsonResponse($data); |
41
|
|
|
$response->setEncodingOptions( |
42
|
|
|
$response->getEncodingOptions() | JSON_PRETTY_PRINT |
43
|
|
|
); |
44
|
|
|
return $response; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
#[Route("/api/deck/shuffle", name: 'shuffle', methods: ['GET', 'POST'], defaults: ['description' => 'Blandar kortleken och returnerar den som en JSON-struktur.'])] |
48
|
|
|
public function shuffle( |
49
|
|
|
SessionInterface $session |
50
|
|
|
): Response { |
51
|
|
|
//Hämta kortlek från session |
52
|
|
|
$carddeck = $session->get("api-deck"); |
53
|
|
|
|
54
|
|
|
if (!$carddeck) { |
55
|
|
|
$carddeck = new DeckOfCards(); |
56
|
|
|
} |
57
|
|
|
/** @var DeckOfCards $carddeck */ |
58
|
|
|
|
59
|
|
|
//Blanda kortlek |
60
|
|
|
$carddeck->shuffleCards(); |
61
|
|
|
|
62
|
|
|
//Spara blandad kortlek till session |
63
|
|
|
$session->set("api-deck", $carddeck); |
64
|
|
|
|
65
|
|
|
//Returnera kortlek som JSON struktur |
66
|
|
|
$data = [ |
67
|
|
|
'deck' => $carddeck->getValues() |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
$response = new JsonResponse($data); |
71
|
|
|
$response->setEncodingOptions( |
72
|
|
|
$response->getEncodingOptions() | JSON_PRETTY_PRINT |
73
|
|
|
); |
74
|
|
|
return $response; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
#[Route("/api/deck/draw", name: 'draw', methods: ['GET', 'POST'], defaults: ['description' => 'Drar ett kort ur kortleken, och sparar den nya kortleken till session.'])] |
78
|
|
|
public function draw( |
79
|
|
|
SessionInterface $session |
80
|
|
|
): Response { |
81
|
|
|
//Hämta kortlek från session |
82
|
|
|
$carddeck = $session->get("api-deck"); |
83
|
|
|
|
84
|
|
|
if (!$carddeck) { |
85
|
|
|
$carddeck = new DeckOfCards(); |
86
|
|
|
} |
87
|
|
|
/** @var DeckOfCards $carddeck */ |
88
|
|
|
|
89
|
|
|
//Dra 1 kort ur kortleken |
90
|
|
|
$values = $carddeck->drawCard(); |
91
|
|
|
$card = new Card($values); |
92
|
|
|
|
93
|
|
|
//Spara kortlek till sessionen |
94
|
|
|
$session->set("api-deck", $carddeck); |
95
|
|
|
|
96
|
|
|
//Returnera kortlek som JSON struktur |
97
|
|
|
$data = [ |
98
|
|
|
'num-deck' => $carddeck->getNumberCards(), |
99
|
|
|
'card' => $card->getValue() |
100
|
|
|
]; |
101
|
|
|
|
102
|
|
|
$response = new JsonResponse($data); |
103
|
|
|
$response->setEncodingOptions( |
104
|
|
|
$response->getEncodingOptions() | JSON_PRETTY_PRINT |
105
|
|
|
); |
106
|
|
|
return $response; |
107
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
#[Route("/api/deck/draw/{num<\d+>}", name: 'draw_number', methods: ['GET', 'POST'], defaults: ['description' => 'Drar flera kort ur kortleken och sparar den nya kortleken till session.'])] |
111
|
|
|
public function drawNumber( |
112
|
|
|
int $num, |
113
|
|
|
SessionInterface $session |
114
|
|
|
): Response { |
115
|
|
|
//Hämta kortlek från session |
116
|
|
|
$carddeck = $session->get("api-deck"); |
117
|
|
|
|
118
|
|
|
if (!$carddeck) { |
119
|
|
|
$carddeck = new DeckOfCards(); |
120
|
|
|
} |
121
|
|
|
/** @var DeckOfCards $carddeck */ |
122
|
|
|
|
123
|
|
|
if ($num > $carddeck->getNumberCards()) { |
124
|
|
|
throw new Exception("Can not draw more cards than available in deck!"); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
//Dra :number kort från kortlek |
128
|
|
|
$hand = new cardHand(); |
129
|
|
|
for ($i = 0; $i < $num; $i++) { |
130
|
|
|
$value = $carddeck->drawCard(); |
131
|
|
|
$card = new Card($value); |
132
|
|
|
$hand->add($card); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
//Spara kortlek till sessionen |
136
|
|
|
$session->set("api-deck", $carddeck); |
137
|
|
|
|
138
|
|
|
//Returnera kort och antal kort i lek som JSON struktur |
139
|
|
|
$data = [ |
140
|
|
|
'num-deck' => $carddeck->getNumberCards(), |
141
|
|
|
'card' => $hand->getValues() |
142
|
|
|
]; |
143
|
|
|
|
144
|
|
|
$response = new JsonResponse($data); |
145
|
|
|
$response->setEncodingOptions( |
146
|
|
|
$response->getEncodingOptions() | JSON_PRETTY_PRINT |
147
|
|
|
); |
148
|
|
|
return $response; |
149
|
|
|
|
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|