1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
7
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
8
|
|
|
use App\Card\Game21; |
9
|
|
|
use Exception; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
12
|
|
|
|
13
|
|
|
class GameController extends AbstractController |
14
|
|
|
{ |
15
|
|
|
#[Route("/game", name: "game_home")] |
16
|
|
|
public function game(): Response |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
return $this->render('game/home.html.twig'); |
20
|
|
|
} |
21
|
|
|
#[Route("/game/doc", name: "game_doc")] |
22
|
|
|
public function doc(): Response |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
return $this->render('game/doc.html.twig'); |
26
|
|
|
} |
27
|
|
|
#[Route("/game/start", name: "game_start")] |
28
|
|
|
public function gameStart( |
29
|
|
|
SessionInterface $session |
30
|
|
|
): Response { |
31
|
|
|
|
32
|
|
|
$game = new Game21(); |
33
|
|
|
$turn = $game->getTurn(); |
34
|
|
|
$this->addFlash( |
35
|
|
|
'notice', |
36
|
|
|
'Game restarted!' |
37
|
|
|
); |
38
|
|
|
$session->set("game", $game); |
39
|
|
|
|
40
|
|
|
$data = [ |
41
|
|
|
"player_hand" => "", |
42
|
|
|
"bank_hand" => "", |
43
|
|
|
"winner" => "", |
44
|
|
|
"turn" => $turn |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
return $this->render('game/gameboard.html.twig', $data); |
48
|
|
|
} |
49
|
|
|
#[Route("/game/player_draw", name: "game_player_draw")] |
50
|
|
|
public function gamePlayerDraw( |
51
|
|
|
SessionInterface $session |
52
|
|
|
): Response { |
53
|
|
|
$game = $session->get("game"); |
54
|
|
|
if ($game instanceof Game21 === false) { |
55
|
|
|
throw new Exception("Game not found in session, starting a new game."); |
56
|
|
|
} |
57
|
|
|
$turn = $game->getTurn(); |
58
|
|
|
$handValue = $game->playerDraw(); |
59
|
|
|
$winner = $game->getWinner(); |
60
|
|
|
$session->set("game", $game); |
61
|
|
|
$playerHand = $game->getPlayerHand(); |
62
|
|
|
|
63
|
|
|
$data = [ |
64
|
|
|
"player_hand" => $playerHand->getString(), |
65
|
|
|
"player_handValue" => $handValue, |
66
|
|
|
"bank_hand" => "", |
67
|
|
|
"winner" => $winner, |
68
|
|
|
"turn" => $turn |
69
|
|
|
]; |
70
|
|
|
return $this->render('game/gameboard.html.twig', $data); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
#[Route("/game/bank_draw", name: "game_bank_draw")] |
74
|
|
|
public function gameBankDraw( |
75
|
|
|
SessionInterface $session |
76
|
|
|
): Response { |
77
|
|
|
$game = $session->get("game"); |
78
|
|
|
if (($game instanceof Game21) === false) { |
79
|
|
|
throw new Exception("Game not found in session, starting a new game."); |
80
|
|
|
} |
81
|
|
|
$game->playerStop(); |
82
|
|
|
$turn = $game->getTurn(); |
83
|
|
|
$playerHand = $game->getPlayerHand(); |
84
|
|
|
$handValue = $game->getHandValue($playerHand); |
85
|
|
|
$bankHandValue = $game->bankDraw(); |
86
|
|
|
$bankHand = $game->getBankHand(); |
87
|
|
|
$winner = $game->getWinner(); |
88
|
|
|
$session->set("game", $game); |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
$data = [ |
92
|
|
|
|
93
|
|
|
"player_hand" => $playerHand->getString(), |
94
|
|
|
"player_handValue" => $handValue, |
95
|
|
|
"bank_hand" => $bankHand->getString(), |
96
|
|
|
"bank_handValue" => $bankHandValue, |
97
|
|
|
"turn" => $turn, |
98
|
|
|
"winner" => $winner |
99
|
|
|
]; |
100
|
|
|
|
101
|
|
|
return $this->render('game/gameboard.html.twig', $data); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
|