|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use App\Card\Card; |
|
6
|
|
|
use App\Card\CardGraphic; |
|
7
|
|
|
use App\Card\CardHand; |
|
8
|
|
|
use App\Card\DeckOfCards; |
|
9
|
|
|
use App\Game\Game; |
|
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
|
14
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
15
|
|
|
|
|
16
|
|
|
class Kmom03 extends AbstractController |
|
17
|
|
|
{ |
|
18
|
|
|
#[Route("/game", name: "game", methods: ['GET'])] |
|
19
|
|
|
public function game(): Response |
|
20
|
|
|
{ |
|
21
|
|
|
return $this->render('game.html.twig'); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
#[Route("/game/doc", name: "game-doc", methods: ['GET'])] |
|
25
|
|
|
public function gameDoc(): Response |
|
26
|
|
|
{ |
|
27
|
|
|
return $this->render('game_doc.html.twig'); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
#[Route("/game/init", name: "game-plan", methods: ['POST'])] |
|
31
|
|
|
public function init( |
|
32
|
|
|
SessionInterface $session |
|
33
|
|
|
): Response { |
|
34
|
|
|
//rensa session |
|
35
|
|
|
$session->clear(); |
|
36
|
|
|
|
|
37
|
|
|
// Initiera spelet och spara till session |
|
38
|
|
|
/** @var Game $game */ |
|
39
|
|
|
$game = new Game(); |
|
40
|
|
|
$session->set('game', $game); |
|
41
|
|
|
|
|
42
|
|
|
// redirect till spelaren |
|
43
|
|
|
return $this->redirectToRoute('game-player'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
#[Route("/game/player", name: "game-player", methods: ['GET', 'POST'])] |
|
47
|
|
|
public function gamePlayer( |
|
48
|
|
|
SessionInterface $session |
|
49
|
|
|
): Response { |
|
50
|
|
|
// Hämta spelomgång från session |
|
51
|
|
|
/** @var Game $game */ |
|
52
|
|
|
$game = $session->get('game'); |
|
53
|
|
|
|
|
54
|
|
|
// Draw card and set to hand |
|
55
|
|
|
$currentRound = $game->playerTurn(); |
|
56
|
|
|
|
|
57
|
|
|
// Check if over 21? |
|
58
|
|
|
if ($currentRound['lost']) { |
|
59
|
|
|
$this->addFlash( |
|
60
|
|
|
'notice', |
|
61
|
|
|
'Game over! Banken vann!' |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
// Sätt spelomgång till session |
|
66
|
|
|
$session->set('game', $game); |
|
67
|
|
|
|
|
68
|
|
|
$data = [ |
|
69
|
|
|
"header" => "Spelarens runda", |
|
70
|
|
|
"values" => $currentRound['cards'], |
|
71
|
|
|
"score" => $currentRound['score'], |
|
72
|
|
|
"isLost" => $currentRound['lost'] |
|
73
|
|
|
]; |
|
74
|
|
|
|
|
75
|
|
|
return $this->render('game_plan.html.twig', $data); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
#[Route("/game/bank", name: "game-bank", methods: ['GET', 'POST'])] |
|
79
|
|
|
public function gameBank( |
|
80
|
|
|
SessionInterface $session |
|
81
|
|
|
): Response { |
|
82
|
|
|
// Hämta spelomgång från session |
|
83
|
|
|
/** @var Game $game */ |
|
84
|
|
|
$game = $session->get('game'); |
|
85
|
|
|
|
|
86
|
|
|
// Bankens runda |
|
87
|
|
|
$bankRound = $game->bankTurn(); |
|
88
|
|
|
|
|
89
|
|
|
// Check if over 21? |
|
90
|
|
|
if ($bankRound['lost']) { |
|
91
|
|
|
$this->addFlash( |
|
92
|
|
|
'notice', |
|
93
|
|
|
'Game over! Spelaren vann!' |
|
94
|
|
|
); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
// Sätt spelomgång till session |
|
98
|
|
|
$session->set('game', $game); |
|
99
|
|
|
|
|
100
|
|
|
$data = [ |
|
101
|
|
|
"header" => "Bankens runda", |
|
102
|
|
|
"values" => $bankRound['cards'], |
|
103
|
|
|
"score" => $bankRound['score'], |
|
104
|
|
|
"isLost" => $bankRound['lost'] |
|
105
|
|
|
]; |
|
106
|
|
|
return $this->render('game_plan.html.twig', $data); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
#[Route("/game/winner", name: "game-winner", methods: ['GET', 'POST'])] |
|
110
|
|
|
public function gameWinner( |
|
111
|
|
|
SessionInterface $session |
|
112
|
|
|
): Response { |
|
113
|
|
|
// Hämta spelomgång från session |
|
114
|
|
|
/** @var Game $game */ |
|
115
|
|
|
$game = $session->get('game'); |
|
116
|
|
|
$winner = $game->isWinner(); |
|
117
|
|
|
|
|
118
|
|
|
$data = [ |
|
119
|
|
|
"text" => $winner . " har vunnit!", |
|
120
|
|
|
"playerScore" => $game->getPlayerScore(), |
|
121
|
|
|
"bankScore" => $game->getBankScore() |
|
122
|
|
|
]; |
|
123
|
|
|
return $this->render('winner.html.twig', $data); |
|
124
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|