1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cysha\Casino\Holdem\Game; |
4
|
|
|
|
5
|
|
|
use Cysha\Casino\Cards\Deck; |
6
|
|
|
use Cysha\Casino\Exceptions\GameException; |
7
|
|
|
use Cysha\Casino\Game\Chips; |
8
|
|
|
use Cysha\Casino\Game\Client; |
9
|
|
|
use Cysha\Casino\Game\Contracts\Game; |
10
|
|
|
use Cysha\Casino\Game\Contracts\GameParameters; |
11
|
|
|
use Cysha\Casino\Game\PlayerCollection; |
12
|
|
|
use Cysha\Casino\Game\TableCollection; |
13
|
|
|
use Cysha\Casino\Holdem\Cards\Evaluators\SevenCard; |
14
|
|
|
use Ramsey\Uuid\Uuid; |
15
|
|
|
use Ramsey\Uuid\UuidInterface; |
16
|
|
|
|
17
|
|
|
final class CashGame implements Game |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var UuidInterface |
21
|
|
|
*/ |
22
|
|
|
private $id; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $name; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var DefaultParameters |
31
|
|
|
*/ |
32
|
|
|
private $rules; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var PlayerCollection |
36
|
|
|
*/ |
37
|
|
|
private $players; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var TableCollection |
41
|
|
|
*/ |
42
|
|
|
protected $tables; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* CashGame constructor. |
46
|
|
|
* |
47
|
|
|
* @param UuidInterface $id |
48
|
|
|
* @param string $name |
49
|
66 |
|
* @param GameParameters $rules |
50
|
|
|
*/ |
51
|
66 |
|
public function __construct(UuidInterface $id, string $name, GameParameters $rules) |
52
|
66 |
|
{ |
53
|
66 |
|
$this->id = $id; |
54
|
66 |
|
$this->name = $name; |
55
|
66 |
|
$this->players = PlayerCollection::make(); |
56
|
66 |
|
$this->tables = TableCollection::make(); |
57
|
|
|
$this->rules = $rules; |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param UuidInterface $id |
62
|
|
|
* @param string $name |
63
|
|
|
* @param GameParameters $rules |
64
|
|
|
* |
65
|
66 |
|
* @return CashGame |
66
|
|
|
*/ |
67
|
66 |
|
public static function setUp(UuidInterface $id, string $name, GameParameters $rules) |
68
|
|
|
{ |
69
|
|
|
return new self($id, $name, $rules); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
1 |
|
* @return UuidInterface |
74
|
|
|
*/ |
75
|
1 |
|
public function id(): UuidInterface |
76
|
|
|
{ |
77
|
|
|
return $this->id; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
1 |
|
* @return string |
82
|
|
|
*/ |
83
|
1 |
|
public function name(): string |
84
|
|
|
{ |
85
|
|
|
return $this->name; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
4 |
|
* @return GameParameters |
90
|
|
|
*/ |
91
|
4 |
|
public function rules(): GameParameters |
92
|
|
|
{ |
93
|
|
|
return $this->rules; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
63 |
|
* @return PlayerCollection |
98
|
|
|
*/ |
99
|
63 |
|
public function players(): PlayerCollection |
100
|
|
|
{ |
101
|
|
|
return $this->players; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function __toString(): string |
108
|
62 |
|
{ |
109
|
|
|
return $this->name; |
110
|
62 |
|
} |
111
|
|
|
|
112
|
62 |
|
/** |
113
|
|
|
* @return TableCollection |
114
|
59 |
|
*/ |
115
|
62 |
|
public function tables(): TableCollection |
116
|
|
|
{ |
117
|
62 |
|
return $this->tables; |
118
|
1 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
62 |
|
* @param Client $client |
122
|
1 |
|
* @param Chips $buyinAmount |
123
|
|
|
* |
124
|
|
|
* @throws GameException |
125
|
61 |
|
*/ |
126
|
|
|
public function registerPlayer(Client $client, Chips $buyinAmount = null) |
127
|
61 |
|
{ |
128
|
61 |
|
$buyinAmount = $buyinAmount ?? $this->rules()->minimumBuyIn(); |
|
|
|
|
129
|
61 |
|
|
130
|
|
|
$playerRegistered = $this->players() |
131
|
|
|
->filter(function (Client $player) use ($client) { |
132
|
|
|
return $client->name() === $player->name(); |
133
|
|
|
}); |
134
|
2 |
|
|
135
|
|
|
if ($playerRegistered->count() !== 0) { |
136
|
2 |
|
throw GameException::alreadyRegistered($client, $this); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
if ($buyinAmount->amount() > $client->wallet()->amount()) { |
140
|
|
|
throw GameException::insufficientFunds($client, $this); |
141
|
|
|
} |
142
|
57 |
|
|
143
|
|
|
$client->wallet()->subtract($buyinAmount); |
144
|
|
|
|
145
|
57 |
|
$addPlayer = Player::fromClient($client, $buyinAmount); |
146
|
|
|
$this->players()->push($addPlayer); |
147
|
57 |
|
} |
148
|
57 |
|
|
149
|
|
|
public function removePlayer(Client $client) |
150
|
57 |
|
{ |
151
|
57 |
|
$player = $this->players() |
152
|
57 |
|
->filter(function (Player $player) use ($client) { |
153
|
|
|
return $player->name() === $client->name(); |
154
|
|
|
}) |
155
|
|
|
->first() |
156
|
|
|
; |
157
|
54 |
|
|
158
|
|
|
if ($player === null) { |
159
|
54 |
|
throw GameException::notRegistered($client, $this); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$client->wallet()->add($player->chipstack()); |
163
|
|
|
|
164
|
|
|
$this->players = $this->players() |
165
|
|
|
->reject(function (Player $player) use ($client) { |
166
|
|
|
return $player->name() === $client->name(); |
167
|
|
|
}) |
168
|
|
|
->values(); |
169
|
|
|
|
170
|
|
|
$this->tables() |
171
|
|
|
->each(function (Table $table) use ($client) { |
172
|
|
|
try { |
173
|
|
|
$table->removePlayer($client); |
174
|
|
|
} catch (Cysha\Casino\Holdem\Exceptions\TableException $e) { |
|
|
|
|
175
|
|
|
|
176
|
|
|
} |
177
|
|
|
}); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function assignPlayersToTables() |
181
|
|
|
{ |
182
|
|
|
$groupedPlayers = $this->players() |
183
|
|
|
//->shuffle() |
184
|
|
|
->chunk($this->rules()->tableSize()) |
185
|
|
|
->map(function (PlayerCollection $players) { |
186
|
|
|
$dealer = Dealer::startWork(new Deck(), new SevenCard()); |
187
|
|
|
|
188
|
|
|
return Table::setUp(Uuid::uuid4(), $dealer, $players); |
189
|
|
|
}) |
190
|
|
|
->toArray(); |
191
|
|
|
|
192
|
|
|
$this->tables = TableCollection::make($groupedPlayers); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..