Issues (7)

src/Controller/DiceGameController.php (3 issues)

Labels
Severity
1
<?php
2
3
namespace App\Controller;
4
5
use Mos\Dice\Dice;
0 ignored issues
show
The type Mos\Dice\Dice was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Mos\Dice\DiceGraphic;
0 ignored issues
show
The type Mos\Dice\DiceGraphic was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Mos\Dice\DiceHand;
0 ignored issues
show
The type Mos\Dice\DiceHand was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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