1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller; |
4
|
|
|
|
5
|
|
|
use App\Card\Game21; |
6
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
7
|
|
|
use Symfony\Component\HttpFoundation\Response; |
8
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
9
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* API Controller for Game21. |
14
|
|
|
*/ |
15
|
|
|
class Game21ControllerJson extends AbstractController |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Returns the current state of the Game21 session. |
19
|
|
|
* |
20
|
|
|
* @param SessionInterface $session |
21
|
|
|
* @return JsonResponse The game state. |
22
|
|
|
*/ |
23
|
|
|
#[Route("/api/game", name: "api_game21", methods: ["GET"])] |
24
|
|
|
public function apiGame21(SessionInterface $session): Response |
25
|
|
|
{ |
26
|
|
|
$game = $session->get('game21'); |
27
|
|
|
|
28
|
|
|
if (!$game instanceof Game21) { |
29
|
|
|
return new JsonResponse([ |
30
|
|
|
'error' => 'No active game found in session.' |
31
|
|
|
], Response::HTTP_NOT_FOUND); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$playerHand = $game->getPlayerhand(); |
35
|
|
|
$bankHand = $game->getBankHand(); |
36
|
|
|
|
37
|
|
|
$data = [ |
38
|
|
|
'player' => $playerHand->toArray(), |
39
|
|
|
'playerTotal' => $playerHand->getTotal(), |
40
|
|
|
'bank' => $bankHand->toArray(), |
41
|
|
|
'bankTotal' => $bankHand->getTotal(), |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
$response = new JsonResponse($data); |
45
|
|
|
$response->setEncodingOptions( |
46
|
|
|
$response->getEncodingOptions() | JSON_PRETTY_PRINT |
47
|
|
|
); |
48
|
|
|
return $response; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|