BlackjackApiController::getGameResult()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 18
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 28
ccs 0
cts 20
cp 0
crap 20
rs 9.6666
1
<?php
2
3
namespace App\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
6
use Symfony\Component\HttpFoundation\JsonResponse;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\HttpFoundation\Response;
9
use Symfony\Component\HttpFoundation\Session\SessionInterface;
10
use Symfony\Component\Routing\Annotation\Route;
11
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
12
use App\GameProj\BlackjackGameProj;
13
use App\Card\Card;
14
15
class BlackjackApiController extends AbstractController
16
{
17
    #[Route("/proj/api", name: "api_landing_proj")]
18
    public function landingPage(UrlGeneratorInterface $urlGenerator): Response
19
    {
20
        $jsonRoutes = [
21
            [
22
                'path' => '/api/game/start',
23
                'description' => 'Starta ett nytt Blackjack-spel',
24
                'url' => $urlGenerator->generate('api_start_game', [], UrlGeneratorInterface::ABSOLUTE_URL),
25
            ],
26
            [
27
                'path' => '/api/game/status',
28
                'description' => 'Hämta aktuell spelstatus',
29
                'url' => $urlGenerator->generate('api_game_status', [], UrlGeneratorInterface::ABSOLUTE_URL),
30
            ],
31
            [
32
                'path' => '/api/game/hit',
33
                'description' => 'Utför en "hit"-åtgärd på nuvarande hand',
34
                'url' => $urlGenerator->generate('api_game_hit', [], UrlGeneratorInterface::ABSOLUTE_URL),
35
            ],
36
            [
37
                'path' => '/api/game/stand',
38
                'description' => 'Utför en "stand"-åtgärd på nuvarande hand',
39
                'url' => $urlGenerator->generate('api_game_stand', [], UrlGeneratorInterface::ABSOLUTE_URL),
40
            ],
41
            [
42
                'path' => '/api/game/result',
43
                'description' => 'Hämta spelresultat',
44
                'url' => $urlGenerator->generate('api_game_result', [], UrlGeneratorInterface::ABSOLUTE_URL),
45
            ],
46
        ];
47
        return $this->render('gameproj/api_landing_proj.html.twig', [
48
            'jsonRoutes' => $jsonRoutes,
49
        ]);
50
    }
51
52
    #[Route("/proj/api/game/start", name: "api_start_game", methods: ['POST', 'GET'])]
53
    public function startGame(Request $request, SessionInterface $session): JsonResponse
54
    {
55
        $data = $request->request->all();
56
        $playerName = $data['playerName'] ?? 'Player';
57
        $numberOfHands = $data['numberOfHands'] ?? 2;
58
        $bet = $data['bet'] ?? 10;
59
60
        $playerBank = $session->get('player_bank', 1000);
61
62
        $game = new BlackjackGameProj($playerName, $playerBank, $numberOfHands, $bet);
63
        $game->dealInitialCards();
64
        $game->checkForBlackjack();
65
        $session->set('game', $game);
66
67
        return $this->json(['message' => 'Game started successfully']);
68
    }
69
70
    #[Route("/proj/api/game/status", name: "api_game_status", methods: ["GET"])]
71
    public function getGameStatus(SessionInterface $session): JsonResponse
72
    {
73
        if (!$session->has('game')) {
74
            return new JsonResponse(['No game in progress']);
75
        }
76
77
        $game = $session->get('game');
78
79
        $hands = $game->getHands();
80
        $handInfo = [];
81
        foreach ($hands as $hand) {
82
            $handInfo[] = [
83
                'value' => $game->getHandValue($hand['hand']),
84
                'status' => $hand['status'],
85
                'bet' => $hand['bet'],
86
            ];
87
        }
88
89
        $data = [
90
            'playerName' => $game->getPlayerName(),
91
            'playerBank' => $game->getPlayerBank(),
92
            'hand' => $handInfo,
93
            'dealerValue' => $game->getHandValue($game->getDealerHand()),
94
            'currentHand' => $game->getCurrentHand(),
95
        ];
96
97
        return new JsonResponse($data);
98
    }
99
100
    #[Route("/proj/api/game/hit", name: "api_game_hit", methods: ["GET"])]
101
    public function hit(SessionInterface $session): JsonResponse
102
    {
103
        if (!$session->has('game')) {
104
            return new JsonResponse(['No game in progress']);
105
        }
106
107
        $game = $session->get('game');
108
109
        $currentHand = $game->getCurrentHand();
110
        if ($currentHand === null) {
111
            return $this->json(['error' => 'No active hand'], 400);
112
        }
113
114
        $game->hitHand($currentHand);
115
116
        if ($game->isHandBusted($currentHand)) {
117
118
            if ($game->isGameOver()) {
119
                $game->finishGame();
120
            }
121
        }
122
123
        $session->set('game', $game);
124
125
        $data = [
126
            'message' => 'Hit successful',
127
            'currentHand' => $game->getCurrentHand(),
128
            'handValue' => $game->getHandValue($game->getHands()[$currentHand]['hand']),
129
            'isGameOver' => $game->isGameOver()
130
        ];
131
132
        return new JsonResponse($data);
133
    }
134
135
    #[Route("/proj/api/game/stand", name: "api_game_stand", methods: ["GET"])]
136
    public function stand(SessionInterface $session): JsonResponse
137
    {
138
        if (!$session->has('game')) {
139
            return new JsonResponse(['No game in progress']);
140
        }
141
142
        $game = $session->get('game');
143
144
        $currentHand = $game->getCurrentHand();
145
        if ($currentHand === null) {
146
            return $this->json(['error' => 'No active hand'], 400);
147
        }
148
149
        $game->standHand($currentHand);
150
        $game->nextHand();
151
152
        if ($game->isGameOver()) {
153
            $game->finishGame();
154
        }
155
156
        $session->set('game', $game);
157
158
        $data = [
159
            'message' => 'Stand successful',
160
            'currentHand' => $game->getCurrentHand(),
161
            'isGameOver' => $game->isGameOver()
162
        ];
163
164
        return new JsonResponse($data);
165
    }
166
167
    #[Route("/proj/api/game/result", name: "api_game_result", methods: ["GET"])]
168
    public function getGameResult(SessionInterface $session): JsonResponse
169
    {
170
        if (!$session->has('game')) {
171
            return new JsonResponse(['No game in progress']);
172
        }
173
174
        $game = $session->get('game');
175
176
        $hands = $game->getHands();
177
        $handResults = [];
178
        foreach ($hands as $hand) {
179
            $handResults[] = [
180
                'value' => $game->getHandValue($hand['hand']),
181
                'status' => $hand['status'],
182
                'bet' => $hand['bet'],
183
            ];
184
        }
185
186
        $data = [
187
            'playerName' => $game->getPlayerName(),
188
            'playerBank' => $game->getPlayerBank(),
189
            'hand' => $handResults,
190
            'dealerValue' => $game->getHandValue($game->getDealerHand()),
191
            'gameStatus' => $game->isGameOver() ? 'finished' : 'in progress'
192
        ];
193
194
        return new JsonResponse($data);
195
    }
196
197
}
198