Passed
Push — master ( 9137c7...d6bc1c )
by Paweł
03:29
created

Player   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Test Coverage

Coverage 23.26%

Importance

Changes 7
Bugs 0 Features 1
Metric Value
wmc 13
eloc 48
c 7
b 0
f 1
dl 0
loc 109
ccs 10
cts 43
cp 0.2326
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getLevelProgress() 0 3 1
A new() 0 17 1
A increaseExperience() 0 3 1
A calculateCorruptionBoundary() 0 17 3
A getTalentPoints() 0 3 1
A getAttributePoints() 0 3 1
A __construct() 0 22 1
A increaseEtherum() 0 19 4
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\Level;
13
use AardsGerds\Game\Build\LevelProgress;
14
use AardsGerds\Game\Build\Talent\SecretKnowledge\Ascension;
15
use AardsGerds\Game\Build\Talent\TalentCollection;
16
use AardsGerds\Game\Build\Talent\TalentPoints;
17
use AardsGerds\Game\Entity\Entity;
18
use AardsGerds\Game\Inventory\Inventory;
19
use AardsGerds\Game\Inventory\Weapon\Weapon;
20
use AardsGerds\Game\Shared\IntegerValueException;
21
22
final class Player extends Entity
23
{
24 1
    public function __construct(
25
        string $name,
26
        Health $health,
27
        Etherum $etherum,
28
        Strength $strength,
29
        TalentCollection $talentCollection,
30
        Inventory $inventory,
31
        ?Weapon $weapon,
32
        bool $corrupted,
33
        private LevelProgress $levelProgress,
34
        private AttributePoints $attributePoints,
35
        private TalentPoints $talentPoints,
36
    ) {
37 1
        parent::__construct(
38 1
            $name,
39
            $health,
40
            $etherum,
41
            $strength,
42
            $talentCollection,
43
            $inventory,
44
            $weapon,
45
            $corrupted,
46
        );
47 1
    }
48
49
    public static function new(string $name): self
50
    {
51
        return new self(
52
            $name,
53
            new Health(100),
54
            new Etherum(1),
55
            new Strength(5),
56
            new TalentCollection([]),
57
            new Inventory([]),
58
            null,
59
            false,
60
            new LevelProgress(
61
                new Level(1),
62
                new Experience(0),
63
            ),
64
            new AttributePoints(0),
65
            new TalentPoints(0),
66
        );
67
    }
68
69 1
    public function getLevelProgress(): LevelProgress
70
    {
71 1
        return $this->levelProgress;
72
    }
73
74
    public function increaseExperience(Experience $experience): void
75
    {
76
        $this->levelProgress->increase($experience, $this);
77
    }
78
79
    /**
80
     * @throws PlayerException
81
     */
82
    public function increaseEtherum(Etherum $etherum): void
83
    {
84
        $this->etherum->increaseBy($etherum);
85
86
        if ($this->isCorrupted()) {
87
            return;
88
        }
89
90
        $playerAscension = $this->talentCollection->findSecretKnowledge()?->getAscension()
91
            ?? throw PlayerException::etherumOverdose();
92
93
        $corruptionBoundary = $this->calculateCorruptionBoundary();
94
95
        if ($this->etherum->isGreaterThanOrEqual($corruptionBoundary)) {
96
            if ($playerAscension->isLowerThan(Ascension::sixthAscension())) {
97
                throw PlayerException::etherumOverdose();
98
            }
99
100
            $this->corrupted = true;
101
        }
102
    }
103
104 1
    public function getAttributePoints(): AttributePoints
105
    {
106 1
        return $this->attributePoints;
107
    }
108
109 1
    public function getTalentPoints(): TalentPoints
110
    {
111 1
        return $this->talentPoints;
112
    }
113
114
    private function calculateCorruptionBoundary(): Etherum
115
    {
116
        $ascension = $this->talentCollection->findSecretKnowledge()?->getAscension();
117
        if ($ascension === null) {
118
            return new Etherum(2);
119
        }
120
121
        try {
122
            $nextAscensionEtherum = $ascension->increment()->getRequiredEtherum();
123
        } catch (IntegerValueException $exception) {
124
            // entity has 8th ascension
125
            $nextAscensionEtherum = new Etherum($ascension->getRequiredEtherum()->get() * 2);
126
        }
127
128
        // etherum required by next ascension + 0.5 x etherum required by next ascension
129
        return $nextAscensionEtherum->increaseBy(
130
            new Etherum((int) ($nextAscensionEtherum->get() * 0.5)),
131
        );
132
    }
133
}
134