DiceGameController::initCallback()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 2
b 0
f 0
nc 1
nop 2
dl 0
loc 19
rs 9.9332
1
<?php
2
3
namespace App\Controller;
4
5
use App\Dice\Dice;
6
use App\Dice\DiceGraphic;
7
use App\Dice\DiceGame;
8
use App\Dice\DiceHand;
9
use Exception;
10
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\Routing\Annotation\Route;
14
use Symfony\Component\HttpFoundation\Session\SessionInterface;
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
        $game = new DiceGame();
45
        $diceRoll = $game->rollSeveral($num);
46
47
        $data = [
48
            "num_dices" => count($diceRoll),
49
            "diceRoll" => $diceRoll,
50
        ];
51
52
        return $this->render('pig/test/roll_many.html.twig', $data);
53
    }
54
55
    #[Route("/game/pig/test/dicehand/{num<\d+>}", name: "test_dicehand")]
56
    public function testDiceHand(int $num): Response
57
    {
58
        if ($num > 99) {
59
            throw new Exception("Can not roll more than 99 dices!");
60
        }
61
62
        $game = new DiceGame();
63
        $hand = $game->rollMixedHand($num);
64
65
        $data = [
66
            "num_dices" => $hand->getNumberDices(),
67
            "diceRoll" => $hand->getString(),
68
        ];
69
70
        return $this->render('pig/test/dicehand.html.twig', $data);
71
    }
72
73
    #[Route("/game/pig/init", name: "pig_init_get", methods: ['GET'])]
74
    public function init(): Response
75
    {
76
        // Logic to play the game
77
        return $this->render('pig/init.html.twig');
78
    }
79
80
    #[Route("/game/pig/init", name: "pig_init_post", methods: ['POST'])]
81
    public function initCallback(
82
        Request $request,
83
        SessionInterface $session
84
    ): Response {
85
        //Hämtar från request hur många tärningar ska ha
86
        $numDice = $request->request->get('num_dices');
87
        $numDice = (int) $numDice;
88
89
        $game = new DiceGame();
90
        $hand = $game->rollHand($numDice);
91
92
        //Sätter alla värden i sessionen
93
        $session->set("pig_dicehand", $hand);
94
        $session->set("pig_dices", $numDice);
95
        $session->set("pig_round", 0);
96
        $session->set("pig_total", 0);
97
98
        return $this->redirectToRoute('pig_play');
99
    }
100
101
    #[Route("/game/pig/play", name: "pig_play", methods: ['GET'])]
102
    public function play(
103
        SessionInterface $session
104
    ): Response {
105
        /** @var DiceHand $dicehand */
106
        $dicehand = $session->get("pig_dicehand");
107
108
        $data = [
109
            "pigDices" => $session->get("pig_dices"),
110
            "pigRound" => $session->get("pig_round"),
111
            "pigTotal" => $session->get("pig_total"),
112
            "diceValues" => $dicehand->getString()
113
        ];
114
115
        return $this->render('pig/play.html.twig', $data);
116
    }
117
118
    #[Route("/game/pig/roll", name: "pig_roll", methods: ['POST'])]
119
    public function roll(
120
        SessionInterface $session
121
    ): Response {
122
        //Hämtar tärningshand från session
123
        /** @var DiceHand $hand */
124
        $hand = $session->get("pig_dicehand");
125
        //Rullar den hämtade tärningen
126
        $hand->roll();
127
128
        //Uppdaterar spelets ställning
129
        /** @var int $roundTotal */
130
        $roundTotal = $session->get("pig_round");
131
132
        $game = new DiceGame();
133
        $round = $game->gameRound($hand);
134
135
        if ($round == 0) {
136
            $roundTotal = 0;
137
            //ge varningsmeddelande när får en etta
138
            $this->addFlash(
139
                'warning',
140
                'You got a 1 and you lost the round points!'
141
            );
142
        }
143
144
        //Sätter värdet från spelrundan
145
        $session->set("pig_round", $roundTotal + $round);
146
147
        return $this->redirectToRoute('pig_play');
148
    }
149
150
    #[Route("/game/pig/save", name: "pig_save", methods: ['POST'])]
151
    public function save(
152
        SessionInterface $session
153
    ): Response {
154
        /** @var int $roundTotal */
155
        $roundTotal = $session->get("pig_round");
156
        /** @var int $gameTotal */
157
        $gameTotal = $session->get("pig_total");
158
159
        $session->set("pig_round", 0);
160
        $session->set("pig_total", $roundTotal + $gameTotal);
161
162
        $this->addFlash(
163
            'notice',
164
            'Your round was saved to the total!'
165
        );
166
167
        return $this->redirectToRoute('pig_play');
168
    }
169
}
170