Passed
Push — master ( 955384...f3a3f5 )
by Paweł
11:23
created

Player::calculateCorruptionBoundary()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 17
ccs 8
cts 9
cp 0.8889
rs 9.9666
cc 3
nc 3
nop 0
crap 3.0123
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AardsGerds\Game\Player;
6
7
use AardsGerds\Game\Build\Attribute\AttributePoints;
8
use AardsGerds\Game\Build\Attribute\Etherum;
9
use AardsGerds\Game\Build\Attribute\Health;
10
use AardsGerds\Game\Build\Attribute\Initiative;
11
use AardsGerds\Game\Build\Attribute\Strength;
12
use AardsGerds\Game\Build\Experience;
13
use AardsGerds\Game\Build\Level;
14
use AardsGerds\Game\Build\LevelProgress;
15
use AardsGerds\Game\Build\Talent\SecretKnowledge\Ascension;
16
use AardsGerds\Game\Build\Talent\TalentCollection;
17
use AardsGerds\Game\Build\Talent\TalentPoints;
18
use AardsGerds\Game\Build\Talent\WeaponMastery\ShortSword\Novice\Slash;
19
use AardsGerds\Game\Build\Talent\WeaponMastery\WeaponMastery;
20
use AardsGerds\Game\Build\Talent\WeaponMastery\WeaponMasteryLevel;
21
use AardsGerds\Game\Entity\Entity;
22
use AardsGerds\Game\Event\Story\FirstChapter\MercenaryCamp\MercenaryCampVisitEvent;
23
use AardsGerds\Game\Event\VisitEvent;
24
use AardsGerds\Game\Inventory\Alchemy\Potion\HealthPotion;
25
use AardsGerds\Game\Inventory\Inventory;
26
use AardsGerds\Game\Inventory\Weapon\ShortSword\RustyShortSword;
27
use AardsGerds\Game\Inventory\Weapon\Weapon;
28
use AardsGerds\Game\Shared\IntegerValueException;
29
30
final class Player extends Entity
31
{
32 4
    public function __construct(
33
        string $name,
34
        Health $health,
35
        Etherum $etherum,
36
        Strength $strength,
37
        Initiative $initiative,
38
        TalentCollection $talentCollection,
39
        Inventory $inventory,
40
        ?Weapon $weapon,
41
        bool $corrupted,
42
        private LevelProgress $levelProgress,
43
        private Health $maximumHealth,
44
        private AttributePoints $attributePoints,
45
        private TalentPoints $talentPoints,
46
        private VisitEvent $checkpoint,
47
    ) {
48 4
        parent::__construct(
49 4
            $name,
50
            $health,
51
            $etherum,
52
            $strength,
53
            $initiative,
54
            $talentCollection,
55
            $inventory,
56
            $weapon,
57
            $corrupted,
58
        );
59 4
    }
60
61 3
    public static function new(string $name): self
62
    {
63 3
        return new self(
64 3
            $name,
65 3
            new Health(100),
66 3
            new Etherum(1),
67 3
            new Strength(5),
68 3
            new Initiative(10),
69 3
            new TalentCollection([
70 3
                WeaponMastery::shortSword(WeaponMasteryLevel::novice()),
71 3
                new Slash(),
72
            ]),
73 3
            new Inventory([new HealthPotion(), new HealthPotion()]),
74 3
            new RustyShortSword(),
75 3
            false,
76 3
            new LevelProgress(
77 3
                new Level(1),
78 3
                new Experience(0),
79
            ),
80 3
            new Health(100),
81 3
            new AttributePoints(0),
82 3
            new TalentPoints(0),
83 3
            new MercenaryCampVisitEvent(),
84
        );
85
    }
86
87 1
    public function getLevelProgress(): LevelProgress
88
    {
89 1
        return $this->levelProgress;
90
    }
91
92
    public function increaseExperience(Experience $experience, PlayerAction $playerAction): void
93
    {
94
        $this->levelProgress->increase($experience, $this, $playerAction);
95
    }
96
97 1
    public function getMaximumHealth(): Health
98
    {
99 1
        return $this->maximumHealth;
100
    }
101
102 1
    public function getAttributePoints(): AttributePoints
103
    {
104 1
        return $this->attributePoints;
105
    }
106
107 1
    public function getTalentPoints(): TalentPoints
108
    {
109 1
        return $this->talentPoints;
110
    }
111
112 1
    public function getCheckpoint(): VisitEvent
113
    {
114 1
        return $this->checkpoint;
115
    }
116
117
    public function setCheckpoint(VisitEvent $checkpoint): void
118
    {
119
        $this->checkpoint = $checkpoint;
120
    }
121
122 1
    public function heal(Health $health): void
123
    {
124 1
        $this->health->increaseBy($health);
125
126 1
        if ($this->health->isGreaterThan($this->maximumHealth)) {
127 1
            $this->healCompletely();
128
        }
129 1
    }
130
131 1
    public function healCompletely(): void
132
    {
133 1
        $this->health->replaceWith($this->maximumHealth);
134 1
    }
135
136
    /**
137
     * @throws PlayerException
138
     */
139 4
    public function increaseEtherum(Etherum $etherum): void
140
    {
141 4
        $this->etherum->increaseBy($etherum);
142
143 4
        if ($this->isCorrupted()) {
144
            return;
145
        }
146
147 4
        $playerAscension = $this->talentCollection->findSecretKnowledge()?->getAscension()
148 1
            ?? throw PlayerException::etherumOverdose();
149
150 3
        $corruptionBoundary = $this->calculateCorruptionBoundary();
151
152 3
        if ($this->etherum->isGreaterThanOrEqual($corruptionBoundary)) {
153 3
            if ($playerAscension->isLowerThan(Ascension::sixthAscension())) {
154 1
                throw PlayerException::etherumOverdose();
155
            }
156
157 2
            $this->corrupted = true;
158
        }
159 2
    }
160
}
161