|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
7
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
|
9
|
|
|
use Exception; |
|
10
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
|
11
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
12
|
|
|
|
|
13
|
|
|
use App\Card\Deck; |
|
14
|
|
|
use App\Game\BlackjackGame; |
|
15
|
|
|
use App\Entity\Book; |
|
16
|
|
|
use App\Repository\BookRepository; |
|
17
|
|
|
use App\Trait\DeckManagerTrait; |
|
18
|
|
|
|
|
19
|
|
|
class ControllerJsonApi extends AbstractController |
|
20
|
|
|
{ |
|
21
|
|
|
use DeckManagerTrait; |
|
22
|
|
|
|
|
23
|
|
|
#[Route("/api/deck", methods: ['GET'])] |
|
24
|
|
|
public function getDeck(): Response |
|
25
|
|
|
{ |
|
26
|
|
|
$deck = new Deck(); |
|
27
|
|
|
|
|
28
|
|
|
$cards = []; |
|
29
|
|
|
foreach ($deck->getCards() as $suitCards) { |
|
30
|
|
|
foreach ($suitCards as $card) { |
|
31
|
|
|
$cards[] = [ |
|
32
|
|
|
'suit' => $card->getSuit(), |
|
33
|
|
|
'value' => $card->getValue() |
|
34
|
|
|
]; |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$response = new JsonResponse(['cards' => $cards]); |
|
39
|
|
|
$response->setEncodingOptions(JSON_PRETTY_PRINT); |
|
40
|
|
|
|
|
41
|
|
|
return $response; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
#[Route("/api/deck/shuffle", methods: ['POST', 'GET'])] |
|
46
|
|
|
public function shuffleDeck(SessionInterface $session): Response |
|
47
|
|
|
{ |
|
48
|
|
|
// Retrieve deck from session |
|
49
|
|
|
$deck = $this->getOrCreateDeck($session); |
|
50
|
|
|
$deck->shuffle(); |
|
51
|
|
|
|
|
52
|
|
|
// Update the session |
|
53
|
|
|
$session->set('deck', $deck); |
|
54
|
|
|
|
|
55
|
|
|
$cards = []; |
|
56
|
|
|
foreach ($deck->getCards() as $suitCards) { |
|
57
|
|
|
foreach ($suitCards as $card) { |
|
58
|
|
|
$cards[] = [ |
|
59
|
|
|
'suit' => $card->getSuit(), |
|
60
|
|
|
'value' => $card->getValue() |
|
61
|
|
|
]; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$response = new JsonResponse(['cards' => $cards]); |
|
66
|
|
|
$response->setEncodingOptions(JSON_PRETTY_PRINT); |
|
67
|
|
|
|
|
68
|
|
|
return $response; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
#[Route('/api/deck/draw/{number}', name: 'draw_cards_api', methods: ['POST', 'GET'])] |
|
72
|
|
|
public function drawCards(int $number, SessionInterface $session): Response |
|
73
|
|
|
{ |
|
74
|
|
|
// Retrieve deck from session |
|
75
|
|
|
$deck = $this->getOrCreateDeck($session); |
|
76
|
|
|
|
|
77
|
|
|
if ($number > $deck->remainingCardsCount()) { |
|
78
|
|
|
throw new Exception("Cannot draw $number cards. Only {$deck->remainingCardsCount()} cards left in deck."); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// Draw the number of cards from the deck |
|
82
|
|
|
$drawnCards = []; |
|
83
|
|
|
for ($i = 0; $i < $number; $i++) { |
|
84
|
|
|
$drawnCard = $deck->drawCard(); |
|
85
|
|
|
$drawnCards[] = [ |
|
86
|
|
|
'suit' => $drawnCard->getSuit(), |
|
87
|
|
|
'value' => $drawnCard->getValue() |
|
88
|
|
|
]; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$session->set('deck', $deck); |
|
92
|
|
|
|
|
93
|
|
|
$data = [ |
|
94
|
|
|
'drawnCards' => $drawnCards, |
|
95
|
|
|
'remainingCardsCount' => $deck->remainingCardsCount() |
|
96
|
|
|
]; |
|
97
|
|
|
|
|
98
|
|
|
return new JsonResponse($data); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
#[Route('/api/game/standings', methods: ['GET'])] |
|
102
|
|
|
public function getStandings(SessionInterface $session): Response |
|
103
|
|
|
{ |
|
104
|
|
|
if (!$session->has('game')) { |
|
105
|
|
|
return new JsonResponse(['No game in progress']); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$game = $session->get('game'); |
|
109
|
|
|
|
|
110
|
|
|
$data = [ |
|
111
|
|
|
'playerValue' => $game->getHandValue($game->getPlayerHand()), |
|
112
|
|
|
'dealerValue' => $game->getHandValue($game->getDealerHand()) |
|
113
|
|
|
]; |
|
114
|
|
|
|
|
115
|
|
|
return new JsonResponse($data); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
#[Route('/api/library/books', name: 'api_library_books', methods: ['GET'])] |
|
119
|
|
|
public function getAllBooks(ManagerRegistry $doctrine): JsonResponse |
|
120
|
|
|
{ |
|
121
|
|
|
$books = $doctrine->getRepository(Book::class)->findAll(); |
|
122
|
|
|
|
|
123
|
|
|
$response = $this->json($books); |
|
124
|
|
|
$response->setEncodingOptions( |
|
125
|
|
|
$response->getEncodingOptions() | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE |
|
126
|
|
|
); |
|
127
|
|
|
return $response; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
#[Route('/api/library/book/{isbn}', name: 'api_library_book', methods: ['GET'])] |
|
131
|
|
|
public function getBookByIsbn(string $isbn, BookRepository $bookRepository): JsonResponse |
|
132
|
|
|
{ |
|
133
|
|
|
$book = $bookRepository->findByIsbn($isbn); |
|
134
|
|
|
|
|
135
|
|
|
$response = $this->json($book); |
|
136
|
|
|
$response->setEncodingOptions( |
|
137
|
|
|
$response->getEncodingOptions() | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE |
|
138
|
|
|
); |
|
139
|
|
|
return $response; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|