Scoring::sortHand()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 0
cts 22
cp 0
rs 9.328
c 0
b 0
f 0
cc 4
nc 6
nop 1
crap 20
1
<?php
2
3
declare(strict_types=1);
4
5
// Folder \Controllers containing classes
6
namespace Joki20\Http\Controllers;
7
8
// use Joki20\Http\Controllers\PokerSquares;
9
use Joki20\Http\Controllers\Setup;
10
11
/**
12
 * Class Scoring;
13
 */
14
15
16
class Scoring extends Setup
17
{
18
    private $currentSession = '';
19
    private $suitsRow = [];
20
    private $valuesRow = [];
21
    private $suitsColumn = [];
22
    private $valuesColumn = [];
23
    private $scoreSession;
24
    private $sortedValues = [];
25
    private $consecutiveArray = false;
26
    private $times;
27
    private $occurrencesValues = [];
28
    private $notScoredYet;
29
    private $countNothing;
30
    private $countPair;
31
    private $countTwopairs;
32
    private $countThreeofakind;
33
    private $countStraight;
34
    private $countFlush;
35
    private $countFullhouse;
36
    private $countFourofakind;
37
    private $countStraightflush;
38
    private $countRoyalflush;
39
40 1
    public function setPointsSessions(): void
41
    {
42
        // row data (suits array and values array)
43 1
        session()->put('dataRow0', []);
44 1
        session()->put('dataRow1', []);
45 1
        session()->put('dataRow2', []);
46 1
        session()->put('dataRow3', []);
47 1
        session()->put('dataRow4', []);
48
        // column data (suits array and values array)
49 1
        session()->put('dataColumn0', []);
50 1
        session()->put('dataColumn1', []);
51 1
        session()->put('dataColumn2', []);
52 1
        session()->put('dataColumn3', []);
53 1
        session()->put('dataColumn4', []);
54
        // counting times of each result for highscore list
55
        // has to reset to 0 each time score is calculated, and not null according to database
56 1
        session()->put('count', [
57 1
            'nothing' => 0, 'pair' => 0,
58
            'twoPairs' => 0, 'threeofakind' => 0,
59
            'straight' => 0, 'flush' => 0,
60
            'fullhouse' => 0, 'fourofakind' => 0,
61
            'straightflush' => 0, 'royalstraightflush' => 0
62
        ]);
63 1
    }
64
65 View Code Duplication
    public function checkFullRow(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
    {
67
        // LOOP FOR EACH ROW
68
        for ($row = 0; $row < 5; $row++) {
69
            for ($column = 0; $column < 5; $column++) {
70
                $this->notScoredYet = true;
71
                // initial length is 0, if not then session contians card
72
                // ROW SUITS AND VALUES
73
                // if a card is existing (form length has 235)
74
                $this->currentSession = session('grid.' . $row . $column);
75
                if (strlen($this->currentSession) != 235) {
76
                    // collect suit HDSC and value 02-14 for row
77
                    array_push($this->suitsRow, substr($this->currentSession, 23, 1));
78
                    array_push($this->valuesRow, substr($this->currentSession, 21, 2));
79
                }
80
                // ROW SAVE AND SCORE DATA
81
                // if five cards (five suits), push suits and values arrays
82
                if ($column == 4 && count($this->suitsRow) == 5) {
83
                    // session('dataRow00', [[H,D,C,S,D],[03,05,01,13,10]])
84
                    session()->push('dataRow' . $row, $this->suitsRow);
85
                    session()->push('dataRow' . $row, $this->valuesRow);
86
                    // send array of suits and values for row to score function
87
                    $this->sortHand('Row' . $row);
88
                    // when match is found, $this->notScoredYet = false and rest of functions will not proceed
89
                    $this->scoreSameSuit('Row' . $row);
90
                    $this->checkValueOccurrence('Row' . $row);
0 ignored issues
show
Unused Code introduced by
The call to Scoring::checkValueOccurrence() has too many arguments starting with 'Row' . $row.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
91
                    $this->fourOfaKind('Row' . $row);
92
                    $this->fullHouse('Row' . $row);
93
                    $this->threeOfAKind('Row' . $row);
94
                    $this->twoPairsOrPair('Row' . $row);
95
                    $this->straightOrNothing('Row' . $row);
96
97
                    // before next loop, reset to false
98
                    $this->setNotScoredYetBackToFalse();
99
                }
100
                // reset suits and values before next column
101
                if ($column == 4) {
102
                    $this->suitsRow = [];
103
                    $this->valuesRow = [];
104
                }
105
            }
106
        }
107
    }
108
109 View Code Duplication
    public function checkFullColumn(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
    {
111
        // LOOP FOR EACH COLUMN
112
        for ($column = 0; $column < 5; $column++) {
113
            for ($row = 0; $row < 5; $row++) {
114
                $this->notScoredYet = true;
115
                // ROW SUITS AND VALUES
116
                // if a card is existing (form length has 233)
117
                $this->currentSession = session('grid.' . $row . $column);
118
                if (strlen($this->currentSession) != 235) {
119
                    // collect suit HDSC and value 02-14 for column
120
                    array_push($this->suitsColumn, substr($this->currentSession, 23, 1));
121
                    array_push($this->valuesColumn, substr($this->currentSession, 21, 2));
122
                }
123
                // COLUMN SAVE AND SCORE DATA
124
                if ($row == 4 && count($this->suitsColumn) == 5) {
125
                    session()->push('dataColumn' . $column, $this->suitsColumn);
126
                    session()->push('dataColumn' . $column, $this->valuesColumn);
127
128
                    //begin with sorting hand and set
129
                    $this->sortHand('Column' . $column);
130
                    // when match is found, $this->notScoredYet = false and rest of functions will not proceed
131
                    $this->scoreSameSuit('Column' . $column);
132
                    $this->checkValueOccurrence('Column' . $column);
0 ignored issues
show
Unused Code introduced by
The call to Scoring::checkValueOccurrence() has too many arguments starting with 'Column' . $column.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
133
                    $this->fourOfaKind('Column' . $column);
134
                    $this->fullHouse('Column' . $column);
135
                    $this->threeOfAKind('Column' . $column);
136
                    $this->twoPairsOrPair('Column' . $column);
137
                    $this->straightOrNothing('Column' . $column);
138
                    // before next loop, reset to false
139
                    $this->setNotScoredYetBackToFalse();
140
                }
141
                // reset suits and values before next column
142
                if ($row == 4) {
143
                    $this->suitsColumn = [];
144
                    $this->valuesColumn = [];
145
                }
146
            }
147
        }
148
    }
149
150
    public function sortHand($type)
151
    {
152
        // set initial value to 0, increment with ++ when a hand fulfills
153
        $this->countNothing = 0;
154
        $this->countPair = 0;
155
        $this->countTwopairs = 0;
156
        $this->countThreeofakind = 0;
157
        $this->countStraight = 0;
158
        $this->countFlush = 0;
159
        $this->countFullhouse = 0;
160
        $this->countFourofakind = 0;
161
        $this->countStraightflush = 0;
162
        $this->countRoyalflush = 0;
163
        $this->consecutiveArray = false;
164
165
       // type is string dataRowX/dataColumnX array with suits. Used with session()->put()
166
       // scoreSession[0] is suits, scoreSession[1] is values
167
        $this->scoreSession = session('data' . $type); // i e dataRow21
168
       // set consecutive to true
169
        $this->consecutiveArray = true;
170
        // CREATE COPY ARRAY AND SORT VALUES
171
        $this->sortedValues = $this->scoreSession[1];
172
        sort($this->sortedValues);
173
       // check if consecutive numbers
174
        for ($i = 0; $i < 4; $i++) {
175
            if (intval($this->sortedValues[$i + 1]) != intval($this->sortedValues[$i]) + 1) {
176
                $this->consecutiveArray = false;
177
            }
178
        }
179
180
        if ($this->sortedValues === ["14", "01", "02", "03", "04"]) {
181
            $this->consecutiveArray = true;
182
        }
183
184
        return $this->sortedValues;
185
       // END OF SORT
186
    }
187
188
    public function scoreSameSuit($type)
189
    {
190
        if ($this->notScoredYet == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
191
            // ///////////// SAME SUIT /////////////
192
            // how many different suits. 1 means all are same
193
            if (count(array_count_values($this->scoreSession[0])) == 1) {
194
                // if straight
195
                if ($this->consecutiveArray == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
196
                    // ROYAL STRAIGHT FLUSH 10-A
197
                    if ($this->sortedValues[0] == "10") {
198
                        session()->put('score' . $type, ['score' => 100, 'feedback' => 'ROYAL STRAIGHT FLUSH']);
199
                        session()->put('count.royalstraightflush', session('count.royalstraightflush') + 1);
200
                        $this->notScoredYet = false;
201
                    // STRAIGHT FLUSH
202
                    } elseif (($this->sortedValues[0] != "10")) {
203
                        session()->put('score' . $type, ['score' => 75, 'feedback' => 'STRAIGHT FLUSH']);
204
                        session()->put('count.straightflush', session('count.straightflush') + 1);
205
                        $this->notScoredYet = false;
206
                    } // FLUSH
207
                } elseif ($this->consecutiveArray == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
208
                    session()->put('score' . $type, ['score' => 20, 'feedback' => 'FLUSH']);
209
                    session()->put('count.flush', session('count.flush') + 1);
210
                    $this->notScoredYet = false;
211
                }
212
            }
213
        }
214
    }
215
216
    public function checkValueOccurrence(): void
217
    {
218
        if ($this->notScoredYet == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
219
            if (count(array_count_values($this->scoreSession[1])) > 1) {
220
                $this->occurrencesValues = [];
221
                // count occurrences of each value, insert into $occurrences array
222
                $this->times = 0;
223
                for ($val = 0; $val < 5; $val++) {
224
                    for ($match = 0; $match < 5; $match++) {
225
                        if ($this->scoreSession[1][$val] == $this->scoreSession[1][$match]) {
226
                            $this->times++;
227
                        }
228
                    }
229
                    array_push($this->occurrencesValues, $this->times);
230
                    $this->times = 0;
231
                }
232
            }
233
        }
234
    }
235
236 View Code Duplication
    public function fourOfAKind($type): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
237
    {
238
        if ($this->notScoredYet == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
239
            // 4 OF A KIND
240
            if (in_array(4, $this->occurrencesValues)) {
241
                session()->put('score' . $type, ['score' => 50, 'feedback' => 'FOUR OF A KIND']);
242
                session()->put('count.fourofakind', session('count.fourofakind') + 1);
243
                $this->notScoredYet = false;
244
            }
245
        }
246
    }
247 View Code Duplication
    public function fullHouse($type): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
248
    {
249
        if ($this->notScoredYet == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
250
            // FULL HOUSE
251
            if (in_array(3, $this->occurrencesValues) && in_array(2, $this->occurrencesValues)) {
252
                session()->put('score' . $type, ['score' => 25, 'feedback' => 'FULL HOUSE']);
253
                session()->put('count.fullhouse', session('count.fullhouse') + 1);
254
                $this->notScoredYet = false;
255
            }
256
        }
257
    }
258
259 View Code Duplication
    public function threeOfAKind($type): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
260
    {
261
        if ($this->notScoredYet == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
262
            // 3 OF A KIND
263
            if (in_array(3, $this->occurrencesValues) && in_array(1, $this->occurrencesValues)) {
264
                session()->put('score' . $type, ['score' => 10, 'feedback' => 'THREE OF A KIND']);
265
                session()->put('count.threeofakind', session('count.threeofakind') + 1);
266
                $this->notScoredYet = false;
267
            }
268
        }
269
    }
270
271
    public function twoPairsOrPair($type): void
272
    {
273
        if ($this->notScoredYet == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
274
            // 2 PAIRS OR 1 PAIR
275
            if (in_array(2, $this->occurrencesValues) && in_array(1, $this->occurrencesValues)) {
276
                // 2 PAIRS
277
                if (array_count_values($this->occurrencesValues)[2] == 4) {
278
                    session()->put('score' . $type, ['score' => 5, 'feedback' => 'TWO PAIRS']);
279
                    session()->put('count.twopairs', session('count.twopairs') + 1);
280
                    $this->notScoredYet = false;
281
                }
282
            }
283
        }
284
285
        // PAIR
286
        if ($this->notScoredYet == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
287
            // 2 PAIRS OR 1 PAIR
288
            if (in_array(2, $this->occurrencesValues) && in_array(1, $this->occurrencesValues)) {
289
                // 1 PAIR
290
                if (array_count_values($this->occurrencesValues)[2] == 2) {
291
                    session()->put('score' . $type, ['score' => 2, 'feedback' => 'PAIR']);
292
                    session()->put('count.pair', session('count.pair') + 1);
293
                    $this->notScoredYet = false;
294
                }
295
            }
296
        }
297
    }
298
299
    public function straightOrNothing($type): void
300
    {
301
        if ($this->notScoredYet == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
302
            // CHECK FOR STRAIGHT
303
            if ($this->consecutiveArray == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
304
                session()->put('score' . $type, ['score' => 15, 'feedback' => 'STRAIGHT']);
305
                session()->put('count.straight', session('count.straight') + 1);
306
                $this->notScoredYet = false;
307
            }
308
        }
309
310
        if ($this->notScoredYet == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
311
            // NO POINTS
312
            session()->put('score' . $type, ['score' => 0, 'feedback' => 'NOTHING']);
313
            session()->put('count.nothing', session('count.nothing') + 1);
314
            $this->notScoredYet = false;
315
        }
316
    }
317
318
    public function setNotScoredYetBackToFalse(): void
319
    {
320
        $this->notScoredYet = true;
321
    }
322
}
323