1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cysha\Casino\Holdem\Game; |
4
|
|
|
|
5
|
|
|
use Cysha\Casino\Cards\CardCollection; |
6
|
|
|
use Cysha\Casino\Game\Chips; |
7
|
|
|
use Cysha\Casino\Game\Contracts\Name as NameContract; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
|
10
|
|
|
class Action |
11
|
|
|
{ |
12
|
|
|
const DEALT_RIVER = 9; |
13
|
|
|
const DEALT_TURN = 8; |
14
|
|
|
const DEALT_FLOP = 7; |
15
|
|
|
|
16
|
|
|
const BIG_BLIND = 6; |
17
|
|
|
const SMALL_BLIND = 5; |
18
|
|
|
|
19
|
|
|
const ALLIN = 4; |
20
|
|
|
const FOLD = 3; |
21
|
|
|
const RAISE = 2; |
22
|
|
|
const CALL = 1; |
23
|
|
|
const CHECK = 0; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var |
27
|
|
|
*/ |
28
|
|
|
private $player; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var int |
32
|
45 |
|
*/ |
33
|
|
|
private $action; |
34
|
45 |
|
|
35
|
45 |
|
/** |
36
|
45 |
|
* @var Chips |
37
|
45 |
|
*/ |
38
|
|
|
private $chips; |
39
|
|
|
|
40
|
|
|
public function __construct(NameContract $player, int $action, $attributes = []) |
41
|
|
|
{ |
42
|
1 |
|
if (isset($attributes['chips']) && !($attributes['chips'] instanceof Chips)) { |
43
|
|
|
throw new InvalidArgumentException('Chip attribute should be instance of Chips'); |
44
|
1 |
|
} |
45
|
|
|
|
46
|
|
|
if (isset($attributes['communityCards']) && !($attributes['communityCards'] instanceof CardCollection)) { |
47
|
|
|
throw new InvalidArgumentException('communityCards attribute should be instance of CardCollection'); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
$this->player = $player; |
51
|
|
|
$this->action = $action; |
52
|
1 |
|
$this->communityCards = $attributes['communityCards'] ?? CardCollection::make(); |
|
|
|
|
53
|
|
|
$this->chips = $attributes['chips'] ?? Chips::zero(); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return int |
58
|
1 |
|
*/ |
59
|
|
|
public function action(): int |
60
|
1 |
|
{ |
61
|
|
|
return $this->action; |
62
|
|
|
} |
63
|
7 |
|
|
64
|
|
|
/** |
65
|
7 |
|
* @return |
66
|
7 |
|
*/ |
67
|
7 |
|
public function player() |
68
|
1 |
|
{ |
69
|
1 |
|
return $this->player; |
70
|
|
|
} |
71
|
6 |
|
|
72
|
1 |
|
/** |
73
|
1 |
|
* @return Chips |
74
|
|
|
*/ |
75
|
5 |
|
public function chips(): Chips |
76
|
1 |
|
{ |
77
|
1 |
|
return $this->chips; |
78
|
|
|
} |
79
|
4 |
|
|
80
|
1 |
|
/** |
81
|
1 |
|
* @return CardCollection |
82
|
|
|
*/ |
83
|
3 |
|
public function communityCards(): CardCollection |
84
|
1 |
|
{ |
85
|
1 |
|
return $this->communityCards; |
86
|
|
|
} |
87
|
2 |
|
|
88
|
1 |
|
public function toString() |
89
|
1 |
|
{ |
90
|
|
|
$message = null; |
91
|
1 |
|
switch ($this->action) { |
92
|
1 |
|
case static::DEALT_RIVER: |
|
|
|
|
93
|
1 |
|
$message = sprintf('%s has dealt the river (%s).', $this->player->name(), $this->communityCards()->__toString()); |
94
|
|
|
break; |
95
|
|
|
|
96
|
7 |
|
case static::DEALT_TURN: |
97
|
|
|
$message = sprintf('%s has dealt the turn (%s).', $this->player->name(), $this->communityCards()->__toString()); |
98
|
|
|
break; |
99
|
7 |
|
|
100
|
|
|
case static::DEALT_FLOP: |
101
|
7 |
|
$message = sprintf('%s has dealt the flop (%s).', $this->player->name(), $this->communityCards()->__toString()); |
102
|
|
|
break; |
103
|
|
|
|
104
|
|
|
case static::BIG_BLIND: |
105
|
|
|
$message = sprintf('%s has posted Big Blind (%d).', $this->player->name(), $this->chips->amount()); |
106
|
|
|
break; |
107
|
|
|
|
108
|
|
|
case static::SMALL_BLIND: |
109
|
|
|
$message = sprintf('%s has posted Small Blind (%d).', $this->player->name(), $this->chips->amount()); |
110
|
|
|
break; |
111
|
|
|
|
112
|
|
|
case static::ALLIN: |
113
|
|
|
$message = sprintf('%s has pushed ALL IN (%d).', $this->player->name(), $this->chips->amount()); |
114
|
|
|
break; |
115
|
|
|
|
116
|
|
|
case static::FOLD: |
117
|
|
|
$message = sprintf('%s has folded.', $this->player->name()); |
118
|
|
|
break; |
119
|
|
|
|
120
|
|
|
case static::RAISE: |
121
|
|
|
$message = sprintf('%s has raised %d.', $this->player->name(), $this->chips->amount()); |
122
|
|
|
break; |
123
|
|
|
|
124
|
|
|
case static::CALL: |
125
|
|
|
$message = sprintf('%s has called %d.', $this->player->name(), $this->chips->amount()); |
126
|
|
|
break; |
127
|
|
|
|
128
|
|
|
case static::CHECK: |
129
|
|
|
$message = sprintf('%s has checked.', $this->player->name()); |
130
|
|
|
break; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $message; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function __toString() |
137
|
|
|
{ |
138
|
|
|
return $this->toString(); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: