DiceGameController   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Test Coverage

Coverage 97.3%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 69
c 2
b 0
f 0
dl 0
loc 152
ccs 72
cts 74
cp 0.973
rs 10
wmc 12

9 Methods

Rating   Name   Duplication   Size   Complexity  
A initCallback() 0 19 1
A init() 0 5 1
A testRollDice() 0 11 1
A roll() 0 30 2
A home() 0 4 1
A testDiceHand() 0 16 2
A save() 0 18 1
A testRollDices() 0 16 2
A play() 0 15 1
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 1
    #[Route("/game/pig", name: "pig_start")]
19
    public function home(): Response
20
    {
21 1
        return $this->render('pig/home.html.twig');
22
    }
23
24 1
    #[Route("/game/pig/test/roll", name: "test_roll_dice")]
25
    public function testRollDice(): Response
26
    {
27 1
        $die = new Dice();
28
29 1
        $data = [
30 1
            "dice" => $die->roll(),
31 1
            "diceString" => $die->getAsString(),
32 1
        ];
33
34 1
        return $this->render('pig/test/roll.html.twig', $data);
35
    }
36
37 1
    #[Route("/game/pig/test/roll/{num<\d+>}", name: "test_roll_num_dices")]
38
    public function testRollDices(int $num): Response
39
    {
40 1
        if ($num > 99) {
41
            throw new Exception("Can not roll more than 99 dices!");
42
        }
43
44 1
        $game = new DiceGame();
45 1
        $diceRoll = $game->rollSeveral($num);
46
47 1
        $data = [
48 1
            "num_dices" => count($diceRoll),
49 1
            "diceRoll" => $diceRoll,
50 1
        ];
51
52 1
        return $this->render('pig/test/roll_many.html.twig', $data);
53
    }
54
55 1
    #[Route("/game/pig/test/dicehand/{num<\d+>}", name: "test_dicehand")]
56
    public function testDiceHand(int $num): Response
57
    {
58 1
        if ($num > 99) {
59
            throw new Exception("Can not roll more than 99 dices!");
60
        }
61
62 1
        $game = new DiceGame();
63 1
        $hand = $game->rollMixedHand($num);
64
65 1
        $data = [
66 1
            "num_dices" => $hand->getNumberDices(),
67 1
            "diceRoll" => $hand->getString(),
68 1
        ];
69
70 1
        return $this->render('pig/test/dicehand.html.twig', $data);
71
    }
72
73 1
    #[Route("/game/pig/init", name: "pig_init_get", methods: ['GET'])]
74
    public function init(): Response
75
    {
76
        // Logic to play the game
77 1
        return $this->render('pig/init.html.twig');
78
    }
79
80 1
    #[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 1
        $numDice = $request->request->get('num_dices');
87 1
        $numDice = (int) $numDice;
88
89 1
        $game = new DiceGame();
90 1
        $hand = $game->rollHand($numDice);
91
92
        //Sätter alla värden i sessionen
93 1
        $session->set("pig_dicehand", $hand);
94 1
        $session->set("pig_dices", $numDice);
95 1
        $session->set("pig_round", 0);
96 1
        $session->set("pig_total", 0);
97
98 1
        return $this->redirectToRoute('pig_play');
99
    }
100
101 1
    #[Route("/game/pig/play", name: "pig_play", methods: ['GET'])]
102
    public function play(
103
        SessionInterface $session
104
    ): Response {
105
        /** @var DiceHand $dicehand */
106 1
        $dicehand = $session->get("pig_dicehand");
107
108 1
        $data = [
109 1
            "pigDices" => $session->get("pig_dices"),
110 1
            "pigRound" => $session->get("pig_round"),
111 1
            "pigTotal" => $session->get("pig_total"),
112 1
            "diceValues" => $dicehand->getString()
113 1
        ];
114
115 1
        return $this->render('pig/play.html.twig', $data);
116
    }
117
118 1
    #[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 1
        $hand = $session->get("pig_dicehand");
125
        //Rullar den hämtade tärningen
126 1
        $hand->roll();
127
128
        //Uppdaterar spelets ställning
129
        /** @var int $roundTotal */
130 1
        $roundTotal = $session->get("pig_round");
131
132 1
        $game = new DiceGame();
133 1
        $round = $game->gameRound($hand);
134
135 1
        if ($round == 0) {
136 1
            $roundTotal = 0;
137
            //ge varningsmeddelande när får en etta
138 1
            $this->addFlash(
139 1
                'warning',
140 1
                'You got a 1 and you lost the round points!'
141 1
            );
142
        }
143
144
        //Sätter värdet från spelrundan
145 1
        $session->set("pig_round", $roundTotal + $round);
146
147 1
        return $this->redirectToRoute('pig_play');
148
    }
149
150 1
    #[Route("/game/pig/save", name: "pig_save", methods: ['POST'])]
151
    public function save(
152
        SessionInterface $session
153
    ): Response {
154
        /** @var int $roundTotal */
155 1
        $roundTotal = $session->get("pig_round");
156
        /** @var int $gameTotal */
157 1
        $gameTotal = $session->get("pig_total");
158
159 1
        $session->set("pig_round", 0);
160 1
        $session->set("pig_total", $roundTotal + $gameTotal);
161
162 1
        $this->addFlash(
163 1
            'notice',
164 1
            'Your round was saved to the total!'
165 1
        );
166
167 1
        return $this->redirectToRoute('pig_play');
168
    }
169
}
170