Completed
Push — master ( c23c95...90e58a )
by Jesse
02:01
created

Player::cannotPay()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\CardGame\Match;
4
5
use function count;
6
use Stratadox\CardGame\DomainEventRecorder;
7
use Stratadox\CardGame\DomainEventRecording;
8
use Throwable;
9
10
final class Player implements DomainEventRecorder
11
{
12
    use DomainEventRecording;
13
14
    private $playerNumber;
15
    private $cards;
16
    private $maxHandSize;
17
    private $mana;
18
19
    public function __construct(
20
        int $id,
21
        Cards $cards,
22
        int $maxHandSize,
23
        Mana $mana
24
    ) {
25
        $this->playerNumber = $id;
26
        $this->cards = $cards;
27
        $this->maxHandSize = $maxHandSize;
28
        $this->mana = $mana;
29
    }
30
31
    public static function from(int $playerId, Cards $cards): self
32
    {
33
        return new self($playerId, $cards, 7, new Mana(4));
34
    }
35
36
    public function number(): int
37
    {
38
        return $this->playerNumber;
39
    }
40
41
    public function cardInHand(int $number): Card
42
    {
43
        return $this->cards->inHand()[$number];
44
    }
45
46
    /** @throws NoSuchCard */
47
    public function cardInPlay(int $number): Card
48
    {
49
        try {
50
            return $this->cards->inPlay()[$number];
51
        } catch (Throwable $notFound) {
52
            throw NoSuchCard::atPosition($number, $notFound);
53
        }
54
    }
55
56
    public function cardsInPlay(): int
57
    {
58
        return count($this->cards->inPlay());
59
    }
60
61
    public function attackers(): Cards
62
    {
63
        return $this->cards->thatAttack();
64
    }
65
66
    public function drawOpeningHand(MatchId $match): void
67
    {
68
        for ($i = 0; $i < $this->maxHandSize; $i++) {
69
            $this->cards->drawFromTopOfDeck($match, $this->playerNumber);
70
        }
71
        foreach ($this->cards->inHand() as $card) {
72
            $this->happened(...$card->domainEvents());
73
            $card->eraseEvents();
74
        }
75
    }
76
77
    public function cannotPay(Mana $theCosts): bool
78
    {
79
        return $this->mana->isLessThan($theCosts);
80
    }
81
82
    public function pay(Mana $theCostOfTheCard): void
83
    {
84
        $this->mana = $this->mana->minus($theCostOfTheCard);
85
    }
86
87
    public function counterTheAttackersOf(
88
        int $attackingPlayer,
89
        MatchId $match,
90
        Cards $attackers
91
    ): void {
92
        foreach ($this->cards->thatDefend() as $theDefender) {
93
            $theDefender->counterAttack(
94
                $match,
95
                $attackers->theOneThatAttacksTheAmbushOf($theDefender),
96
                $attackingPlayer
97
            );
98
            $this->happened(...$theDefender->domainEvents());
99
            $theDefender->eraseEvents();
100
        }
101
    }
102
}
103