DiceGameController::home()   A
last analyzed

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