Test Setup Failed
Push — master ( 647a95...23a558 )
by Jesse
02:13
created

Turn::prohibitsStartingCombat()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 4
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\CardGame\Match;
4
5
use DateTimeInterface;
6
7
final class Turn
8
{
9
    private $currentPlayer;
10
    private $since;
11
    private $phase;
12
13
    public function __construct(
14
        int $player,
15
        DateTimeInterface $since,
16
        TurnPhase $phase = null
17
    ) {
18
        $this->currentPlayer = $player;
19
        $this->since = $since;
20
        $this->phase = $phase ?: TurnPhase::play();
21
    }
22
23
    /** @throws NotYourTurn */
24
    public function mustAllowCardPlaying(
25
        int $player,
26
        DateTimeInterface $when
27
    ): void {
28
        if (
29
            $this->currentPlayer !== $player ||
30
            $this->phase->prohibitsPlaying() ||
31
            $when->getTimestamp() - $this->since->getTimestamp() >= 20
32
        ) {
33
            throw NotYourTurn::cannotPlayCards();
34
        }
35
    }
36
37
    /** @throws NotYourTurn */
38
    public function mustAllowAttacking(
39
        int $player,
40
        DateTimeInterface $when
41
    ): void {
42
        if (
43
            $this->currentPlayer !== $player ||
44
            $this->phase->prohibitsAttacking() ||
45
            $when->getTimestamp() - $this->since->getTimestamp() >= 10
46
        ) {
47
            throw NotYourTurn::cannotAttack();
48
        }
49
    }
50
51
    /** @throws NotYourTurn */
52
    public function mustAllowDefending(
53
        int $player,
54
        DateTimeInterface $when
55
    ): void {
56
        if (
57
            $this->currentPlayer !== $player ||
58
            $this->phase->prohibitsDefending() ||
59
            $when->getTimestamp() - $this->since->getTimestamp() >= 20
60
        ) {
61
            throw NotYourTurn::cannotDefend();
62
        }
63
    }
64
65
    /** @throws NotYourTurn */
66
    public function mustAllowStartingCombat(
67
        int $player,
68
        DateTimeInterface $when
69
    ): void {
70
        // @todo check if turn phase allows for starting combat
71
        if (
72
            $this->currentPlayer !== $player ||
73
            $when->getTimestamp() - $this->since->getTimestamp() >= 20
74
        ) {
75
            throw NotYourTurn::cannotStartCombat();
76
        }
77
    }
78
79
    /** @throws NotYourTurn */
80
    public function endCardPlayingPhaseFor(
81
        int $player,
82
        DateTimeInterface $when
83
    ): Turn {
84
        // @todo check if time ran out? (should we?)
85
        if ($this->currentPlayer !== $player) {
86
            throw NotYourTurn::cannotEndCardPlayingPhase();
87
        }
88
        return new Turn(
89
            $this->currentPlayer,
90
            $when,
91
            $this->phase->endCardPlaying()
92
        );
93
    }
94
95
    public function endCombatPhase(DateTimeInterface $when): Turn
96
    {
97
        return new Turn(
98
            $this->currentPlayer,
99
            $when,
100
            $this->phase->endCombat()
101
        );
102
    }
103
104
    public function hasNotHadCombatYet(): bool
105
    {
106
        return !$this->phase->isAfterCombat();
107
    }
108
109
    /** @throws NotYourTurn */
110
    public function beginTheTurnOf(
111
        int $player,
112
        DateTimeInterface $since,
113
        int $previousPlayer,
114
        bool $shouldDefendFirst
115
    ): Turn {
116
        if (
117
            $this->currentPlayer !== $previousPlayer ||
118
            $since->getTimestamp() - $this->since->getTimestamp() >= 10
119
        ) {
120
            throw NotYourTurn::cannotEndTurn();
121
        }
122
        return new Turn(
123
            $player,
124
            $since,
125
            $shouldDefendFirst ? TurnPhase::defend() : TurnPhase::play()
126
        );
127
    }
128
}
129