1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller; |
4
|
|
|
|
5
|
|
|
use App\Dice\Dice; |
6
|
|
|
use App\Dice\DiceGraphic; |
7
|
|
|
use App\Dice\DiceHand; |
8
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
11
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
12
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
13
|
|
|
|
14
|
|
|
class DiceGameController extends AbstractController |
15
|
|
|
{ |
16
|
1 |
|
#[Route('/game/pig', name: 'pig_start')] |
17
|
|
|
public function home(): Response |
18
|
|
|
{ |
19
|
1 |
|
return $this->render('pig/home.html.twig'); |
20
|
|
|
} |
21
|
|
|
|
22
|
1 |
|
#[Route('/game/pig/init', name: 'pig_init_get', methods: ['GET'])] |
23
|
|
|
public function init(): Response |
24
|
|
|
{ |
25
|
1 |
|
return $this->render('pig/init.html.twig'); |
26
|
|
|
} |
27
|
|
|
|
28
|
2 |
|
#[Route('/game/pig/init', name: 'pig_init_post', methods: ['POST'])] |
29
|
|
|
public function initCallback( |
30
|
|
|
Request $request, |
31
|
|
|
SessionInterface $session, |
32
|
|
|
): Response { |
33
|
2 |
|
$numDice = $request->request->get('num_dices'); |
34
|
|
|
|
35
|
|
|
/** @var DiceHand $hand */ |
36
|
2 |
|
$hand = new DiceHand(); |
37
|
2 |
|
for ($i = 1; $i <= $numDice; ++$i) { |
38
|
2 |
|
$hand->addDie(new DiceGraphic()); |
39
|
|
|
} |
40
|
2 |
|
$hand->roll(); |
41
|
|
|
|
42
|
2 |
|
$session->set('pig_diceHand', $hand); |
43
|
2 |
|
$session->set('pig_dices', $numDice); |
44
|
2 |
|
$session->set('pig_round', 0); |
45
|
2 |
|
$session->set('pig_total', 0); |
46
|
|
|
|
47
|
2 |
|
return $this->redirectToRoute('pig_play'); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
#[Route('/game/pig/play', name: 'pig_play', methods: ['GET'])] |
51
|
|
|
public function play( |
52
|
|
|
SessionInterface $session, |
53
|
|
|
): Response { |
54
|
|
|
/** @var DiceHand $hand */ |
55
|
1 |
|
$hand = $session->get('pig_diceHand'); |
56
|
|
|
|
57
|
1 |
|
$data = [ |
58
|
1 |
|
'pigDices' => $session->get('pig_dices'), |
59
|
1 |
|
'pigRound' => $session->get('pig_round'), |
60
|
1 |
|
'pigTotal' => $session->get('pig_total'), |
61
|
1 |
|
'diceValues' => $hand->getString(), |
62
|
1 |
|
]; |
63
|
|
|
|
64
|
1 |
|
return $this->render('pig/play.html.twig', $data); |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
#[Route('/game/pig/roll', name: 'pig_roll', methods: ['POST'])] |
68
|
|
|
public function roll( |
69
|
|
|
SessionInterface $session, |
70
|
|
|
): Response { |
71
|
|
|
/** @var DiceHand $hand */ |
72
|
1 |
|
$hand = $session->get('pig_diceHand'); |
73
|
|
|
|
74
|
1 |
|
$hand->roll(); |
75
|
|
|
|
76
|
|
|
/** @var int $ roundTotal */ |
77
|
1 |
|
$roundTotal = $session->get('pig_round'); |
78
|
1 |
|
$round = 0; |
79
|
|
|
|
80
|
1 |
|
$values = $hand->getValues(); |
81
|
1 |
|
foreach ($values as $value) { |
82
|
1 |
|
if (1 === $value) { |
83
|
1 |
|
$round = 0; |
84
|
1 |
|
$roundTotal = 0; |
85
|
|
|
|
86
|
1 |
|
$this->addFlash( |
87
|
1 |
|
'warning', |
88
|
1 |
|
'You got a 1 and you lost the round points!' |
89
|
1 |
|
); |
90
|
|
|
|
91
|
1 |
|
break; |
92
|
|
|
} |
93
|
1 |
|
$round += $value; |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
$session->set('pig_round', $roundTotal + $round); |
97
|
|
|
|
98
|
1 |
|
return $this->redirectToRoute('pig_play'); |
99
|
|
|
} |
100
|
|
|
|
101
|
1 |
|
#[Route('/game/pig/save', name: 'pig_save', methods: ['POST'])] |
102
|
|
|
public function save( |
103
|
|
|
SessionInterface $session, |
104
|
|
|
): Response { |
105
|
|
|
/** @var int $ roundTotal */ |
106
|
1 |
|
$roundTotal = $session->get('pig_round'); |
107
|
|
|
/** @var int $ gameTotal */ |
108
|
1 |
|
$gameTotal = $session->get('pig_total'); |
109
|
|
|
|
110
|
1 |
|
$session->set('pig_round', 0); |
111
|
|
|
|
112
|
1 |
|
$session->set('pig_total', $roundTotal + $gameTotal); |
113
|
|
|
|
114
|
1 |
|
$this->addFlash( |
115
|
1 |
|
'notice', |
116
|
1 |
|
'Your round was saved to the total!' |
117
|
1 |
|
); |
118
|
|
|
|
119
|
1 |
|
return $this->redirectToRoute('pig_play'); |
120
|
|
|
} |
121
|
|
|
|
122
|
1 |
|
#[Route('/game/pig/test/roll', name: 'test_roll_dice')] |
123
|
|
|
public function testRollDice(): Response |
124
|
|
|
{ |
125
|
|
|
// $die = new Dice(); |
126
|
1 |
|
$die = new DiceGraphic(); |
127
|
|
|
|
128
|
1 |
|
$data = [ |
129
|
1 |
|
'dice' => $die->roll(), |
130
|
1 |
|
'diceString' => $die->getString(), |
131
|
1 |
|
]; |
132
|
|
|
|
133
|
1 |
|
return $this->render('pig/test/roll.html.twig', $data); |
134
|
|
|
} |
135
|
|
|
|
136
|
3 |
|
#[Route("/game/pig/test/roll/{num<\d+>}", name: 'test_roll_num_dices')] |
137
|
|
|
public function testRollDices(int $num): Response |
138
|
|
|
{ |
139
|
3 |
|
if ($num > 99) { |
140
|
1 |
|
throw new \RuntimeException("Can't roll more than 99 dices!"); |
141
|
|
|
} |
142
|
2 |
|
if ($num < 1) { |
143
|
1 |
|
throw new \RuntimeException("Can't roll less than 1 die!"); |
144
|
|
|
} |
145
|
|
|
|
146
|
1 |
|
$diceRolls = []; |
147
|
1 |
|
for ($i = 0; $i < $num; ++$i) { |
148
|
|
|
// $die = new Dice(); |
149
|
1 |
|
$die = new DiceGraphic(); |
150
|
1 |
|
$die->roll(); |
151
|
1 |
|
$diceRolls[] = $die->getString(); |
152
|
|
|
} |
153
|
|
|
|
154
|
1 |
|
$data = [ |
155
|
1 |
|
'num_dices' => count($diceRolls), |
156
|
1 |
|
'diceRolls' => $diceRolls, |
157
|
1 |
|
]; |
158
|
|
|
|
159
|
1 |
|
return $this->render('pig/test/roll_many.html.twig', $data); |
160
|
|
|
} |
161
|
|
|
|
162
|
3 |
|
#[Route("/game/pig/test/dicehand/{num<\d+>}", name: 'test_dicehand')] |
163
|
|
|
public function testDiceHand(int $num): Response |
164
|
|
|
{ |
165
|
3 |
|
if ($num > 99) { |
166
|
1 |
|
throw new \RuntimeException("Can't roll more than 99 dices!"); |
167
|
|
|
} |
168
|
2 |
|
if ($num < 1) { |
169
|
1 |
|
throw new \RuntimeException("Can't roll less than 1 die!"); |
170
|
|
|
} |
171
|
|
|
|
172
|
1 |
|
$hand = new DiceHand(); |
173
|
|
|
|
174
|
1 |
|
for ($i = 1; $i <= $num; ++$i) { |
175
|
1 |
|
(1 === $i % 2) ? $hand->addDie(new DiceGraphic()) : $hand->addDie(new Dice()); |
176
|
|
|
} |
177
|
|
|
|
178
|
1 |
|
$hand->roll(); |
179
|
|
|
|
180
|
1 |
|
$data = [ |
181
|
1 |
|
'num_dices' => $hand->getNumberDices(), |
182
|
1 |
|
'diceRoll' => $hand->getString(), |
183
|
1 |
|
]; |
184
|
|
|
|
185
|
1 |
|
return $this->render('pig/test/dicehand.html.twig', $data); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|