Passed
Branch main (02626f)
by Vedrana
05:25
created

DiceGameController::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
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\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Session\SessionInterface;
10
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\Routing\Annotation\Route;
13
use Exception;
14
15
class DiceGameController extends AbstractController
16
{
17
    /**
18
     * Display the Pig game start page.
19
     *
20
     * @return Response
21
     */
22
    #[Route("/game/pig", name: "pig_start")]
23
    public function home(): Response
24
    {
25
        return $this->render('pig/home.html.twig');
26
    }
27
28
    /**
29
     * Test rolling a single dice.
30
     *
31
     * @return Response
32
     */
33
    #[Route("/game/pig/test/roll", name: "test_roll_dice")]
34
    public function testRollDice(): Response
35
    {
36
        $die = new Dice();
37
38
        return $this->render('pig/test/roll.html.twig', [
39
            "dice" => $die->roll(),
40
            "diceString" => $die->getAsString(),
41
        ]);
42
    }
43
44
    /**
45
     * Test rolling a number of DiceGraphic dice.
46
     *
47
     * @param int $num Number of dice to roll
48
     * @return Response
49
     */
50
    #[Route("/game/pig/test/roll/{num<\d+>}", name: "test_roll_num_dices")]
51
    public function testRollDices(int $num): Response
52
    {
53
        if ($num > 99) {
54
            throw new Exception("Can not roll more than 99 dices!");
55
        }
56
57
        $diceRoll = $this->rollDiceGraphics($num);
58
59
        return $this->render('pig/test/roll_many.html.twig', [
60
            "num_dices" => count($diceRoll),
61
            "diceRoll" => $diceRoll,
62
        ]);
63
    }
64
65
    /**
66
     * Helper to roll multiple DiceGraphic and return their string representations.
67
     *
68
     * @param int $num Number of dice
69
     * @return string[] Array of dice as strings
70
     */
71
    private function rollDiceGraphics(int $num): array
72
    {
73
        $diceRoll = [];
74
        for ($i = 1; $i <= $num; $i++) {
75
            $die = new DiceGraphic();
76
            $die->roll();
77
            $diceRoll[] = $die->getAsString();
78
        }
79
        return $diceRoll;
80
    }
81
82
    /**
83
     * Test a DiceHand with a mix of Dice and DiceGraphic objects.
84
     *
85
     * @param int $num Number of dice
86
     * @return Response
87
     */
88
    #[Route("/game/pig/test/dicehand/{num<\d+>}", name: "test_dicehand")]
89
    public function testDiceHand(int $num): Response
90
    {
91
        if ($num > 99) {
92
            throw new Exception("Can not roll more than 99 dices!");
93
        }
94
95
        $hand = new DiceHand();
96
97
        for ($i = 1; $i <= $num; $i++) {
98
            $die = ($i % 2 === 1) ? new DiceGraphic() : new Dice();
99
            $hand->add($die);
100
        }
101
102
        $hand->roll();
103
104
        return $this->render('pig/test/dicehand.html.twig', [
105
            "num_dices" => $hand->getNumberDices(),
106
            "diceRoll" => $hand->getString(),
107
        ]);
108
    }
109
110
    /**
111
     * Show the form to init a new Pig game.
112
     *
113
     * @return Response
114
     */
115
    #[Route("/game/pig/init", name: "pig_init_get", methods: ['GET'])]
116
    public function init(): Response
117
    {
118
        return $this->render('pig/init.html.twig');
119
    }
120
121
    /**
122
     * Handle form submission and init the game in session.
123
     *
124
     * @param Request $request
125
     * @param SessionInterface $session
126
     * @return Response
127
     */
128
    #[Route("/game/pig/init", name: "pig_init_post", methods: ['POST'])]
129
    public function initCallback(
130
        Request $request,
131
        SessionInterface $session
132
    ): Response {
133
        $numDice = $request->request->get('num_dices');
134
135
        $hand = new DiceHand();
136
        for ($i = 1; $i <= $numDice; $i++) {
137
            $hand->add(new DiceGraphic());
138
        }
139
        $hand->roll();
140
141
        $session->set("pig_dicehand", $hand);
142
        $session->set("pig_dices", $numDice);
143
        $session->set("pig_round", 0);
144
        $session->set("pig_total", 0);
145
146
        return $this->redirectToRoute('pig_play');
147
    }
148
149
    /**
150
     * Display the current game.
151
     *
152
     * @param SessionInterface $session
153
     * @return Response
154
     */
155
    #[Route("/game/pig/play", name: "pig_play", methods: ['GET'])]
156
    public function play(
157
        SessionInterface $session
158
    ): Response {
159
        /** @var DiceHand $dicehand */
160
        $dicehand = $session->get("pig_dicehand");
161
162
        $data = [
163
            "pigDices" => $session->get("pig_dices"),
164
            "pigRound" => $session->get("pig_round"),
165
            "pigTotal" => $session->get("pig_total"),
166
            "diceValues" => $dicehand->getString()
167
        ];
168
169
        return $this->render('pig/play.html.twig', $data);
170
    }
171
172
173
    /**
174
     * Handle rolling the dice and update round score.
175
     *
176
     * @param SessionInterface $session
177
     * @return Response
178
     */
179
    #[Route("/game/pig/roll", name: "pig_roll", methods: ['POST'])]
180
    public function roll(
181
        SessionInterface $session
182
    ): Response {
183
        /** @var \App\Dice\DiceHand $hand */
184
        $hand = $session->get("pig_dicehand");
185
        $hand->roll();
186
187
        $roundTotal = $session->get("pig_round");
188
        $roundTotal = is_numeric($roundTotal) ? (int) $roundTotal : 0;
189
        $round = 0;
190
        $values = $hand->getValues();
191
        foreach ($values as $value) {
192
            if ($value === 1) {
193
                $round = 0;
194
                $roundTotal = 0;
195
196
                $this->addFlash('warning', 'You got a 1 and you lost the round points!');
197
                break;
198
            }
199
            $round += $value;
200
        }
201
202
        $session->set("pig_round", $roundTotal + $round);
203
204
        return $this->redirectToRoute('pig_play');
205
    }
206
207
    /**
208
     * Save the current round score to the total and reset the round.
209
     *
210
     * @param SessionInterface $session
211
     * @return Response
212
     */
213
    #[Route("/game/pig/save", name: "pig_save", methods: ['POST'])]
214
    public function save(
215
        SessionInterface $session
216
    ): Response {
217
        $roundTotal = (int) (is_numeric($session->get("pig_round")) ? $session->get("pig_round") : 0);
218
        $gameTotal = (int) (is_numeric($session->get("pig_total")) ? $session->get("pig_total") : 0);
219
220
        $session->set("pig_total", $roundTotal + $gameTotal);
221
        $session->set("pig_round", 0);
222
223
        $this->addFlash('notice', 'Your round was saved to the total!');
224
225
        return $this->redirectToRoute('pig_play');
226
    }
227
}
228