|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Stratadox\CardGame\Match; |
|
4
|
|
|
|
|
5
|
|
|
use DateTimeInterface; |
|
6
|
|
|
use Stratadox\CardGame\DomainEvent; |
|
7
|
|
|
use Stratadox\CardGame\Match\Event\AttackPhaseStarted; |
|
8
|
|
|
use Stratadox\CardGame\Match\Event\DefendPhaseStarted; |
|
9
|
|
|
use Stratadox\CardGame\Match\Event\PlayPhaseStarted; |
|
10
|
|
|
|
|
11
|
|
|
final class TurnPhase |
|
12
|
|
|
{ |
|
13
|
|
|
private const DEFEND = 0; |
|
14
|
|
|
private const PLAY = 1; |
|
15
|
|
|
private const ATTACK = 2; |
|
16
|
|
|
private const ALLOWED_TIME_FOR = [ |
|
17
|
|
|
self::DEFEND => 20, |
|
18
|
|
|
self::PLAY => 20, |
|
19
|
|
|
self::ATTACK => 10, |
|
20
|
|
|
]; |
|
21
|
|
|
|
|
22
|
|
|
/** @var int */ |
|
23
|
|
|
private $phase; |
|
24
|
|
|
/** @var DateTimeInterface */ |
|
25
|
|
|
private $since; |
|
26
|
|
|
/** @var DomainEvent[] */ |
|
27
|
|
|
private $events; |
|
28
|
|
|
|
|
29
|
|
|
private function __construct( |
|
30
|
|
|
int $phase, |
|
31
|
|
|
DateTimeInterface $since, |
|
32
|
|
|
DomainEvent ...$events |
|
33
|
|
|
) { |
|
34
|
|
|
$this->phase = $phase; |
|
35
|
|
|
$this->since = $since; |
|
36
|
|
|
$this->events = $events; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public static function defendOrPlay( |
|
40
|
|
|
bool $shouldWeDefend, |
|
41
|
|
|
DateTimeInterface $now, |
|
42
|
|
|
MatchId $match |
|
43
|
|
|
): self { |
|
44
|
|
|
return $shouldWeDefend ? |
|
45
|
|
|
self::defend($now, $match) : |
|
46
|
|
|
self::play($now, $match); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public static function play(DateTimeInterface $now, MatchId $match): self |
|
50
|
|
|
{ |
|
51
|
|
|
return new self(TurnPhase::PLAY, $now, new PlayPhaseStarted($match)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
private static function defend(DateTimeInterface $now, MatchId $match): self |
|
55
|
|
|
{ |
|
56
|
|
|
return new self(TurnPhase::DEFEND, $now, new DefendPhaseStarted($match)); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
public function prohibitsDefending(DateTimeInterface $now): bool |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->phase !== TurnPhase::DEFEND || $this->hasExpired($now); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function prohibitsPlaying(DateTimeInterface $now): bool |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->phase !== TurnPhase::PLAY || $this->hasExpired($now); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function prohibitsAttacking(DateTimeInterface $now): bool |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->phase !== TurnPhase::ATTACK || $this->hasExpired($now); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function prohibitsCombat(): bool |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->phase !== TurnPhase::DEFEND; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function isAfterCombat(): bool |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->phase === TurnPhase::PLAY |
|
83
|
|
|
|| $this->phase === TurnPhase::ATTACK; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function hasExpired(DateTimeInterface $now): bool |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->isTooFarApart( |
|
89
|
|
|
$now->getTimestamp(), |
|
90
|
|
|
$this->since->getTimestamp() |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function startPlay(DateTimeInterface $now, MatchId $match): TurnPhase |
|
95
|
|
|
{ |
|
96
|
|
|
return new self(TurnPhase::PLAY, $now, new PlayPhaseStarted($match)); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function startAttack(DateTimeInterface $now, MatchId $match): TurnPhase |
|
100
|
|
|
{ |
|
101
|
|
|
return new self(TurnPhase::ATTACK, $now, new AttackPhaseStarted($match)); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** @throws NoNextPhase|NeedCombatFirst */ |
|
105
|
|
|
public function next(DateTimeInterface $now, MatchId $match): TurnPhase |
|
106
|
|
|
{ |
|
107
|
|
|
switch ($this->phase) { |
|
108
|
|
|
case self::DEFEND: throw NeedCombatFirst::cannotJustSwitchPhase(); |
|
|
|
|
|
|
109
|
|
|
case self::PLAY: return $this->startAttack($now, $match); |
|
|
|
|
|
|
110
|
|
|
default: throw NoNextPhase::available(); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function events(): array |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->events; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
private function isTooFarApart(int $nowTime, int $backThenTime): bool |
|
120
|
|
|
{ |
|
121
|
|
|
return $nowTime - $backThenTime >= self::ALLOWED_TIME_FOR[$this->phase]; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.