DiceGameController::save()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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