Test Failed
Push — master ( b2aa45...7d2376 )
by Johan
04:19
created

Setup::storeToDatabase()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 0
cts 17
cp 0
rs 9.52
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Joki20\Http\Controllers;
6
7
use Joki20\Http\Controllers\Deck;
8
use Joki20\Models\Pokerhighscore;
9
10
/**
11
 * Class Setup;
12
 */
13
14
class Setup extends Deck
15
{
16
    private $grid = '';
17
    private $cells = '';
18
19 5
    public function name()
20
    {
21 5
        return '<div class="enterName"><form method="POST" autocomplete="off">
22
                <input type="text" name="setName" placeholder="Name" minlength=3 onfocus=this.placeholder = required >
23
                <input type="submit" value="GO">
24
            </form></div>
25
        ';
26
    }
27
28 1
    public function prepareSessions(): void
29
    {
30
31 1
        session()->put('name', $_POST['setName']);
32 1
        session()->put('points', 0);
33 1
        session()->put('round', 0);
34
        // initialize session grid
35 1
        session()->put('grid', [
36 1
            '00' => '', '01' => '', '02' => '', '03' => '', '04' => '', '05' => '', '06' => '',
37
            '10' => '', '11' => '', '12' => '', '13' => '', '14' => '', '15' => '', '16' => '',
38
            '20' => '', '21' => '', '22' => '', '23' => '', '24' => '', '25' => '', '26' => '',
39
            '30' => '', '31' => '', '32' => '', '33' => '', '34' => '', '35' => '', '36' => '',
40
            '40' => '', '41' => '', '42' => '', '43' => '', '44' => '', '45' => '', '46' => '',
41
            '50' => '', '51' => '', '52' => '', '53' => '', '54' => '', '55' => '', '56' => ''
42
        ]);
43
        // create sessions 00-06, 10-14, ... 40-44 with button for card placement
44 1
        for ($row = 0; $row < 5; $row++) {
45 1
            for ($col = 0; $col < 5; $col++) {
46 1
                $cell = strval($row) . strval($col);
47 1
                $form = '
48
                    <form method="POST">
49 1
                        <input type="hidden" name="position" value="' . $cell . '">
50
                        <input type="submit" name="placeCard" value="">
51
                    </form>
52
                    ';
53
                // session()->put('counter', ['countStraight' => $this->countStraight]);
54 1
                session()->put('grid.' . strval($cell), $form);
55
            }
56
        }
57
58
        // row data (suits array and values array)
59 1
        session()->put('dataRow0', []);
60 1
        session()->put('dataRow1', []);
61 1
        session()->put('dataRow2', []);
62 1
        session()->put('dataRow3', []);
63 1
        session()->put('dataRow4', []);
64
        // column data (suits array and values array)
65 1
        session()->put('dataColumn0', []);
66 1
        session()->put('dataColumn1', []);
67 1
        session()->put('dataColumn2', []);
68 1
        session()->put('dataColumn3', []);
69 1
        session()->put('dataColumn4', []);
70
        // row scores
71 1
        session()->put('scoreRow0', ['score' => null, 'feedback' => '']);
72 1
        session()->put('scoreRow1', ['score' => null, 'feedback' => '']);
73 1
        session()->put('scoreRow2', ['score' => null, 'feedback' => '']);
74 1
        session()->put('scoreRow3', ['score' => null, 'feedback' => '']);
75 1
        session()->put('scoreRow4', ['score' => null, 'feedback' => '']);
76
        // column scores
77 1
        session()->put('scoreColumn0', ['score' => null, 'feedback' => '']);
78 1
        session()->put('scoreColumn1', ['score' => null, 'feedback' => '']);
79 1
        session()->put('scoreColumn2', ['score' => null, 'feedback' => '']);
80 1
        session()->put('scoreColumn3', ['score' => null, 'feedback' => '']);
81 1
        session()->put('scoreColumn4', ['score' => null, 'feedback' => '']);
82
        // needs to be set also here (also set in Scoring) since setup
83 1
        session()->put('count', [
84 1
            'nothing' => 0,
85
            'pair' => 0,
86
            'twopairs' => 0,
87
            'threeofakind' => 0,
88
            'straight' => 0,
89
            'flush' => 0,
90
            'fullhouse' => 0,
91
            'fourofakind' => 0,
92
            'straightflush' => 0,
93
            'royalstraightflush' => 0,
94
        ]);
95 1
    }
96
97 1
    public function shuffleDeck(): void
98
    {
99
        // save grid in session
100 1
        shuffle($this->deck);
101 1
        session()->put('deck', $this->deck);
102 1
    }
103
104 1
    public function prepareStack(): void
105
    {
106 1
        $this->stack = '<div class="cardstack">';
107
        // reversed order compared to array
108 1
        foreach (session('deck') as $card) {
109 1
            $this->stack .= $card;
110
        }
111 1
        $this->stack .= '</div>';
112
113 1
        session()->put('stack', $this->stack);
114
        // reverse array (top card is session('deck')[0])
115 1
        session()->put('deck', array_reverse(session('deck')));
116
        // insert shuffled game deck at position 06 (top right)
117
118 1
        session()->put('06', $this->stack);
119 1
    }
120
121 1
    public function displayGrid()
122
    {
123 1
        session()->put(
124 1
            'totalScore',
125 1
            session('scoreRow0.score') +
126 1
            session('scoreRow1.score') +
127 1
            session('scoreRow2.score') +
128 1
            session('scoreRow3.score') +
129 1
            session('scoreRow4.score') +
130 1
            session('scoreColumn0.score') +
131 1
            session('scoreColumn1.score') +
132 1
            session('scoreColumn2.score') +
133 1
            session('scoreColumn3.score') +
134 1
            session('scoreColumn4.score')
135
        );
136
137 1
        print_r('<p class="totalScore">' . (session('round') == 25 ? 'Final ' : 'Current ') . ' score: ' . session('totalScore') . '</p>');
138
139 1
        for ($row = 0; $row < 6; $row++) {
140
            // if not last row
141 1
            if ($row < 5) {
142
                $this->cells .= '
143
                <tr>
144 1
                    <td>' . session('grid.' . $row . '0') . '</td>
145 1
                    <td>' . session('grid.' . $row . '1') . '</td>
146 1
                    <td>' . session('grid.' . $row . '2') . '</td>
147 1
                    <td>' . session('grid.' . $row . '3') . '</td>
148 1
                    <td>' . session('grid.' . $row . '4') . '</td>
149 1
                    <td><br><p>' . session('scoreRow' . $row . '.feedback') . '</p><p>' . session('scoreRow' . $row . '.score')  . '</p></td>';
150
                    // show stack if still rounds left
151 1
                if ($row == 0 && session('round') < 25) {
152 1
                    $this->cells .= '<td>' . session('06') . '</td></tr>';
153 1
                } elseif ($row != 0) {
154 1
                    $this->cells .= '<td></td></tr>';
155
                }
156
            }
157
158 1
            if ($row == 5) {
159
                $this->cells .= '
160
                <tr>
161 1
                    <td><p>' . session('scoreColumn0.feedback') . '</p><p>' . session('scoreColumn0.score') . '</p></td>
162
163 1
                    <td><p>' . session('scoreColumn1.feedback') . '</p><p>' . session('scoreColumn1.score') . '</p></td>
164
165 1
                    <td><p>' . session('scoreColumn2.feedback') . '</p><p>' . session('scoreColumn2.score') . '</p></td>
166
167 1
                    <td><p>' . session('scoreColumn3.feedback') . '</p><p>' . session('scoreColumn3.score') . '</p></td>
168
169 1
                    <td><p>' . session('scoreColumn4.feedback') . '</p><p>' . session('scoreColumn4.score') . '</p></td>
170
171
                    <td></td>
172
                    <td></td>
173
                </tr>';
174
            }
175
        }
176
177
        $this->grid = '
178
            <table id="grid">
179 1
                ' . $this->cells . '
180
            </table>
181
            ';
182
183 1
        session()->put('06', session('stack'));
184
185
186 1
        return $this->grid;
187
    }
188
189 1
    public function placeCard(): void
190
    {
191
192
        // 1. remove first card in session('deck');
193 1
        $arr = session('deck');
194 1
        $currentCard = array_shift($arr);
195 1
        session()->put('deck', $arr);
196
        // 2. place card at correct place
197 1
        $pos = $_POST['position'];
198 1
        session()->put('grid.' . $pos, $currentCard);
199
200
        // if last card placed, write to database
201 1
        session()->put('round', session('round') + 1);
202 1
    }
203
204
    public function storeToDatabase()
205
    {
206
        // if end of game, add game data to highscore table
207
        if (session('round') == 25) {
208
            // create Highscore instance
209
            $highscore = new Pokerhighscore();
210
211
            // insert into database
212
            $highscore->score = session('totalScore');
213
            $highscore->player = session('name');
214
            $highscore->count_nothing = session('count.nothing');
215
            $highscore->count_pair = session('count.pair');
216
            $highscore->count_twopairs = session('count.twopairs');
217
            $highscore->count_threeofakind = session('count.threeofakind');
218
            $highscore->count_straight = session('count.straight');
219
            $highscore->count_flush = session('count.flush');
220
            $highscore->count_fullhouse = session('count.fullhouse');
221
            $highscore->count_fourofakind = session('count.fourofakind');
222
            $highscore->count_straightflush = session('count.straightflush');
223
            $highscore->count_royalstraightflush = session('count.royalstraightflush');
224
            // insert name
225
            // save to db
226
            $highscore->save();
227
        }
228
    }
229
}
230