Passed
Push — master ( e96706...5a7d8c )
by Paweł
02:40
created

Player::calculateCorruptionBoundary()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 17
ccs 0
cts 9
cp 0
crap 12
rs 9.9666
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\Strength;
11
use AardsGerds\Game\Build\Experience;
12
use AardsGerds\Game\Build\LevelProgress;
13
use AardsGerds\Game\Build\Talent\SecretKnowledge\Ascension;
14
use AardsGerds\Game\Build\Talent\TalentCollection;
15
use AardsGerds\Game\Build\Talent\TalentPoints;
16
use AardsGerds\Game\Entity\Entity;
17
use AardsGerds\Game\Inventory\Inventory;
18
use AardsGerds\Game\Inventory\Weapon\Weapon;
19
use AardsGerds\Game\Shared\IntegerValueException;
20
21
final class Player extends Entity
22
{
23
    public function __construct(
24
        string $name,
25
        Health $health,
26
        Etherum $etherum,
27
        Strength $strength,
28
        TalentCollection $talentCollection,
29
        Inventory $inventory,
30
        ?Weapon $weapon,
31
        bool $corrupted,
32
        private LevelProgress $levelProgress,
33
        private AttributePoints $attributePoints,
34
        private TalentPoints $talentPoints,
35
    ) {
36
        parent::__construct(
37
            $name,
38
            $health,
39
            $etherum,
40
            $strength,
41
            $talentCollection,
42
            $inventory,
43
            $weapon,
44
            $corrupted,
45
        );
46
    }
47
48
    public function getLevelProgress(): LevelProgress
49
    {
50
        return $this->levelProgress;
51
    }
52
53
    public function increaseExperience(Experience $experience): void
54
    {
55
        $this->levelProgress->increase($experience, $this);
56
    }
57
58
    /**
59
     * @throws PlayerException
60
     */
61
    public function increaseEtherum(Etherum $etherum): void
62
    {
63
        $this->etherum->increaseBy($etherum);
64
65
        if ($this->isCorrupted()) {
66
            return;
67
        }
68
69
        $playerAscension = $this->talentCollection->findSecretKnowledge()?->getAscension()
70
            ?? throw PlayerException::etherumOverdose();
71
72
        $corruptionBoundary = $this->calculateCorruptionBoundary();
73
74
        if ($this->etherum->isGreaterThanOrEqual($corruptionBoundary)) {
75
            if ($playerAscension->isLowerThan(Ascension::sixthAscension())) {
76
                throw PlayerException::etherumOverdose();
77
            }
78
79
            $this->corrupted = true;
80
        }
81
    }
82
83
    public function getAttributePoints(): AttributePoints
84
    {
85
        return $this->attributePoints;
86
    }
87
88
    public function getTalentPoints(): TalentPoints
89
    {
90
        return $this->talentPoints;
91
    }
92
93
    private function calculateCorruptionBoundary(): Etherum
94
    {
95
        $ascension = $this->talentCollection->findSecretKnowledge()?->getAscension();
96
        if ($ascension === null) {
97
            return new Etherum(2);
98
        }
99
100
        try {
101
            $nextAscensionEtherum = $ascension->increment()->getRequiredEtherum();
102
        } catch (IntegerValueException $exception) {
103
            // entity has 8th ascension
104
            $nextAscensionEtherum = new Etherum($ascension->getRequiredEtherum()->get() * 2);
105
        }
106
107
        // etherum required by next ascension + 0.5 x etherum required by next ascension
108
        return $nextAscensionEtherum->increaseBy(
109
            new Etherum((int) ($nextAscensionEtherum->get() * 0.5)),
110
        );
111
    }
112
}
113