Completed
Push — master ( 41c1b8...07a877 )
by Dan
02:04
created

LeftToAct::findByAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Cysha\Casino\Holdem\Game;
4
5
use Cysha\Casino\Game\Contracts\Player;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Cysha\Casino\Holdem\Game\Player.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use Cysha\Casino\Game\PlayerCollection;
7
use Illuminate\Support\Collection;
8
9
class LeftToAct extends Collection
10
{
11
    const BIG_BLIND = 6;
12
    const SMALL_BLIND = 5;
13
    const ALL_IN = 3;
14
    const AGGRESSIVELY_ACTIONED = 2;
15
    const STILL_TO_ACT = 1;
16
    const ACTIONED = 0;
17
18
    /**
19
     * Sets all players actions to STILL_TO_ACT.
20
     *
21
     * @param PlayerCollection $players
22
     *
23
     * @return LeftToAct
24
     */
25 58
    public function setup(PlayerCollection $players): self
26
    {
27
        $collection = $players->map(function (Player $player, $seatNumber) {
28
            // everyone is still left to act
29
            return [
30 58
                'seat' => $seatNumber,
31 58
                'player' => $player->name(),
32 58
                'action' => self::STILL_TO_ACT,
33
            ];
34 58
        });
35
36 58
        return self::make($collection->toArray());
37
    }
38
39
    /**
40
     * Resets all players, and move dealer to the bottom of queue.
41
     *
42
     * @param PlayerCollection $players
43
     *
44
     * @return LeftToAct
45
     */
46 1
    public function setupWithoutDealer(PlayerCollection $players): self
47
    {
48 1
        $collection = $this->setup($players);
49
50
        // move the dealer to last
51 1
        $collection = $collection->movePlayerToLastInQueue();
52
53 1
        return self::make($collection->values()->toArray());
54
    }
55
56
    /**
57
     * @return LeftToAct
58
     */
59 27
    public function resetActions(): self
60
    {
61
        $collection = $this
62
            ->transform(function ($array) {
63 27
                if ($array['action'] !== self::ALL_IN) {
64 27
                    $array['action'] = self::STILL_TO_ACT;
65
                }
66
67 27
                return $array;
68 27
            })
69 27
            ->values();
70
71 27
        return self::make($collection->values()->toArray());
72
    }
73
74
    /**
75
     * @return LeftToAct
76
     */
77 16
    public function sortBySeats(): self
78
    {
79
        $collection = $this
80
            ->sortBy(function ($array) {
81 16
                return $array['seat'];
82 16
            }, SORT_NUMERIC)
83 16
            ->values();
84
85 16
        return self::make($collection->values()->toArray());
86
    }
87
88
    /**
89
     * @return LeftToAct
90
     */
91 39
    public function playerHasActioned(Player $player, int $value = 0): self
92
    {
93 39
        $collection = $this->movePlayerToLastInQueue();
94 39
        if (in_array($value, [self::AGGRESSIVELY_ACTIONED, self::ALL_IN])) {
95 16
            $collection = $collection->resetActions();
96
        }
97
98 39
        $collection->setActivity($player->name(), $value);
99
100 39
        return self::make($collection->values()->toArray());
101
    }
102
103
    /**
104
     * @param Player $player
105
     * @param int    $activity
106
     *
107
     * @return LeftToAct
108
     */
109 40
    public function setActivity($player, int $activity): self
110
    {
111
        // var_dump($this);
112
        $result = $this
113
            ->filter(function ($array) use ($player) {
114 40
                return $array['player'] === $player;
115 40
            })
116
        ;
117
118 40
        $array = $result->first();
119 40
        $array['action'] = $activity;
120 40
        $this->put($result->keys()->first(), $array);
121
122 40
        return self::make($this->values()->toArray());
123
    }
124
125
    /**
126
     * @return LeftToAct
127
     */
128 41
    public function movePlayerToLastInQueue(): self
129
    {
130 41
        $collection = $this->splice(1)->merge($this->splice(0, 1));
131
132 41
        return self::make($collection->values()->toArray());
133
    }
134
135
    /**
136
     * @return LeftToAct
137
     */
138 50
    public function resetPlayerListFromSeat(int $seatNumber): self
139
    {
140 50
        $firstPlayer = $this->first();
141 50
        if ($seatNumber === $firstPlayer['seat']) {
142 5
            return self::make($this->values()->toArray());
143
        }
144
145
        $new = $this->sortBy(function ($value) use ($seatNumber) {
146 47
            if ($value['seat'] < $seatNumber) {
147 47
                return ($value['seat'] + 1) * 10;
148
            }
149
150 47
            return $value['seat'];
151 47
        });
152
153 47
        return new self($new->values());
154
    }
155
156
    /**
157
     * @return LeftToAct
158
     */
159 14
    public function removePlayer(Player $player): self
160
    {
161
        $collection = $this
162
            ->reject(function ($array) use ($player) {
163 14
                return $array['player'] === $player->name();
164 14
            });
165
166 14
        return self::make($collection->values()->toArray());
167
    }
168
169
    /**
170
     * @return self
171
     */
172
    public function findByAction(int $action)
173
    {
174
        return $this->filter(function ($value) use ($action) {
175 36
            return $value['action'] === $action;
176 36
        });
177 36
    }
178 36
179
    /**
180
     * @return array
181
     */
182
    public function getRemainingPlayers()
183
    {
184
        return $this
185
            ->reject(function ($value) {
186
                return in_array($value['action'], [self::ACTIONED, self::AGGRESSIVELY_ACTIONED, self::ALL_IN]);
187
            });
188
    }
189
190
    /**
191
     * @return array
192
     */
193
    public function getNextPlayer()
194
    {
195
        return $this->getRemainingPlayers()->first();
196
    }
197
}
198