|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cysha\Casino\Holdem\Game; |
|
4
|
|
|
|
|
5
|
|
|
use Cysha\Casino\Game\Chips; |
|
6
|
|
|
use Cysha\Casino\Game\ChipStackCollection; |
|
7
|
|
|
use Cysha\Casino\Game\Contracts\Player; |
|
|
|
|
|
|
8
|
|
|
use Cysha\Casino\Game\PlayerCollection; |
|
9
|
|
|
|
|
10
|
|
|
class ChipPot |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var ChipStackCollection |
|
14
|
|
|
*/ |
|
15
|
|
|
private $chips; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var PlayerCollection |
|
19
|
|
|
*/ |
|
20
|
|
|
private $players; |
|
21
|
|
|
|
|
22
|
52 |
|
private function __construct() |
|
23
|
|
|
{ |
|
24
|
52 |
|
$this->chips = new ChipStackCollection(); |
|
25
|
52 |
|
$this->players = new PlayerCollection(); |
|
26
|
52 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @return ChipPot |
|
30
|
|
|
*/ |
|
31
|
52 |
|
public static function create(): ChipPot |
|
32
|
|
|
{ |
|
33
|
52 |
|
return new self(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param Chips $chips |
|
38
|
|
|
* @param Player $player |
|
39
|
|
|
* |
|
40
|
|
|
* @return ChipPot |
|
41
|
|
|
*/ |
|
42
|
21 |
|
public function addChips(Chips $chips, Player $player): ChipPot |
|
43
|
|
|
{ |
|
44
|
21 |
|
$existingChips = $this->chips()->get($player->name()) ?? Chips::zero(); |
|
45
|
|
|
|
|
46
|
21 |
|
$this->chips->put($player->name(), Chips::fromAmount($existingChips->amount() + $chips->amount())); |
|
47
|
|
|
|
|
48
|
21 |
|
if ($this->players()->findByName($player->name()) === null) { |
|
49
|
21 |
|
$this->players->push($player); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
21 |
|
return $this; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return ChipStackCollection |
|
57
|
|
|
*/ |
|
58
|
21 |
|
public function chips(): ChipStackCollection |
|
59
|
|
|
{ |
|
60
|
21 |
|
return $this->chips; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return PlayerCollection |
|
65
|
|
|
*/ |
|
66
|
21 |
|
public function players(): PlayerCollection |
|
67
|
|
|
{ |
|
68
|
21 |
|
return $this->players; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return Chips |
|
73
|
|
|
*/ |
|
74
|
13 |
|
public function total(): Chips |
|
75
|
|
|
{ |
|
76
|
13 |
|
return $this->chips->total(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return int |
|
81
|
|
|
*/ |
|
82
|
3 |
|
public function totalAmount(): int |
|
83
|
|
|
{ |
|
84
|
3 |
|
return $this->chips->total()->amount(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param ChipPot $object |
|
89
|
|
|
* |
|
90
|
|
|
* @return bool |
|
91
|
|
|
*/ |
|
92
|
11 |
|
public function equals(ChipPot $object): bool |
|
93
|
|
|
{ |
|
94
|
11 |
|
return static::class === get_class($object) |
|
95
|
11 |
|
&& $this->chips() === $object->chips() |
|
96
|
11 |
|
&& $this->players() === $object->players() |
|
97
|
11 |
|
&& $this->total() === $object->total(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
1 |
|
public function __toString() |
|
104
|
|
|
{ |
|
105
|
1 |
|
$players = $this->players() |
|
106
|
1 |
|
->map(function (Player $player) { |
|
107
|
1 |
|
return $player->name(); |
|
108
|
1 |
|
}) |
|
109
|
1 |
|
->implode(', '); |
|
110
|
|
|
|
|
111
|
1 |
|
return sprintf('[%d] [%s]', $this->total()->amount(), $players); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/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: