CardGameControllerJson::jsonShuffle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 19
rs 9.9332
ccs 0
cts 12
cp 0
crap 2
1
<?php
2
3
namespace App\Controller;
4
5
use App\Card\DeckOfCards;
6
use App\Card\Card;
7
use App\Game\GameManager;
8
use App\Game\BettingManager;
9
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\HttpFoundation\JsonResponse;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\Routing\Annotation\Route;
14
use Symfony\Component\HttpFoundation\Session\SessionInterface;
15
16
class CardGameControllerJson
17
{
18
    #[Route("/api/deck", name: "json_deck", methods: ['GET'])]
19
    public function jsonDeck(SessionInterface $session): JsonResponse
20
    {
21
        /** @var DeckOfCards $deck */
22
        $deck = new DeckOfCards();
23
24
        $session->set('currentDeck', $deck);
25
26
        $data = [
27
            "deck" => $deck->getString(),
28
        ];
29
30
        $response = new JsonResponse($data);
31
        $response->setEncodingOptions(
32
            $response->getEncodingOptions() | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT,
33
        );
34
        return $response;
35
    }
36
37
    #[Route("/api/deck/shuffle", name: "json_shuffle", methods: ['POST'])]
38
    public function jsonShuffle(SessionInterface $session): JsonResponse
39
    {
40
        /** @var DeckOfCards $deck */
41
        $deck = new DeckOfCards();
42
43
        $deck->shuffleCards();
44
45
        $session->set('currentDeck', $deck);
46
47
        $data = [
48
            "deck" => $deck->getString(),
49
        ];
50
51
        $response = new JsonResponse($data);
52
        $response->setEncodingOptions(
53
            $response->getEncodingOptions() | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT,
54
        );
55
        return $response;
56
    }
57
58
    #[Route("/api/deck/draw/{number<\d+>}", name: "json_draw_many", methods: ['POST'])]
59
    public function jsonDrawMany(SessionInterface $session, int $number): JsonResponse
60
    {
61
        /** @var DeckOfCards $deck */
62
        $deck = $session->get('currentDeck', new DeckOfCards());
63
64
        $draw = array_map('strval', $deck->draw($number));
65
66
        $session->set('currentDeck', $deck); // Save updated deck
67
68
        $data = [
69
            "draw"  => $draw,
70
            "count" => $deck->getCount(),
71
        ];
72
73
        $response = new JsonResponse($data);
74
        $response->setEncodingOptions(
75
            $response->getEncodingOptions() | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT,
76
        );
77
        return $response;
78
    }
79
80
    #[Route("/api/deck/draw", name: "json_draw", methods: ['POST'])]
81
    public function jsonDraw(SessionInterface $session): JsonResponse
82
    {
83
        /** @var DeckOfCards $deck */
84
        $deck = $session->get('currentDeck', new DeckOfCards());
85
86
        $draw = array_map('strval', $deck->draw(1));
87
88
        $session->set('currentDeck', $deck); // Save updated deck
89
90
        $data = [
91
            "draw"  => $draw,
92
            "count" => $deck->getCount(),
93
        ];
94
95
        $response = new JsonResponse($data);
96
        $response->setEncodingOptions(
97
            $response->getEncodingOptions() | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT,
98
        );
99
        return $response;
100
    }
101
102
    #[Route("/api/deck/deal/{players<\d+>}/{cards<\d+>}", name: "json_deal", methods: ['POST'])]
103
    public function jsonDeal(SessionInterface $session, int $players, int $cards): JsonResponse
104
    {
105
        /** @var DeckOfCards $deck */
106
        $deck = $session->get('currentDeck', new DeckOfCards());
107
108
        $deal = [];
109
110
        for ($i = 0; $i < $players; $i++) {
111
            $deal[] = array_map('strval', $deck->draw($cards));
112
        }
113
114
        $session->set('currentDeck', $deck); // Save updated deck
115
116
        $data = [
117
            "deal"  => $deal,
118
            "count" => $deck->getCount(),
119
        ];
120
121
        $response = new JsonResponse($data);
122
        $response->setEncodingOptions(
123
            $response->getEncodingOptions() | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT,
124
        );
125
        return $response;
126
    }
127
128
    #[Route("/api/game", name: "json_game", methods: ['GET'])]
129
    public function jsonGame(SessionInterface $session): JsonResponse
130
    {
131
        /** @var GameManager|null $gameManager */
132
        $gameManager = $session->get('gameManager', null);
133
134
        if ($gameManager === null) {
135
            return new JsonResponse(['notice' => 'No active game to display']);
136
        }
137
138
        $gameState = $gameManager->getState();
139
140
        /** @var Card[] $playerCards */
141
        $playerCards = $gameState['playerCards'];
142
143
        /** @var Card[] $bankerCards */
144
        $bankerCards = $gameState['bankerCards'];
145
146
        // Represent card objects as strings
147
        $gameState['playerCards'] = array_map('strval', $playerCards);
148
        $gameState['bankerCards'] = array_map('strval', $bankerCards);
149
150
        // Turn assistance number into a sensible message
151
        $gameState['assistance'] = "Player has {$gameState['assistance']}% risk of bursting";
152
153
        // Turn has won status number into a sensible message
154
        $gameState['hasWon'] = ($gameState['hasWon'] === 1) ? 'Player won' : (($gameState['hasWon'] === -1) ? 'Banker won' : 'No winner');
155
156
        /** @var BettingManager|null $bettingManager */
157
        $bettingManager = $session->get('bettingManager', null);
158
159
        // Get state of betting... but only if betting is on
160
        if ($bettingManager !== null and $bettingManager->isBetting() === true) {
161
            $bettingState = $bettingManager->getState();
162
163
            $gameState['playerCoins'] = $bettingState['playerCoins'];
164
            $gameState['bankerCoins'] = $bettingState['bankerCoins'];
165
            $gameState['currentBet'] = $bettingState['stake'];
166
            $gameState['gameOver'] = $bettingState['gameOver'] ? 'Yes' : 'No';
167
        }
168
169
        $data = $gameState;
170
171
        $response = new JsonResponse($data);
172
        $response->setEncodingOptions(
173
            $response->getEncodingOptions() | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT,
174
        );
175
176
        return $response;
177
    }
178
}
179