Passed
Push — dev ( 45bf8d...5b8f46 )
by Janko
10:07
created

DamageWrapper::canDamageSystem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Stu\Lib\Damage;
4
5
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
6
use Stu\Lib\Pirate\Component\PirateWrathManager;
7
use Stu\Module\PlayerSetting\Lib\UserEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Module\PlayerSetting\Lib\UserEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Stu\Orm\Entity\ColonyInterface;
9
use Stu\Orm\Entity\SpacecraftInterface;
10
use Stu\Orm\Entity\UserInterface;
11
12
class DamageWrapper
13
{
14
    private bool $isCrit = false;
15
    private bool $isShieldPenetration = false;
16
    private int $modificator = 100;
17
    private ?int $pirateWrath = null;
18
19
    /** @var null|array<SpacecraftSystemTypeEnum> */
20
    private ?array $targetSystemTypes = null;
21
22 17
    public function __construct(private float $netDamage) {}
23
24
    private int $hull_damage_factor = 100;
25
26
27 1
    public function setHullDamageFactor(int $value): DamageWrapper
28
    {
29 1
        $this->hull_damage_factor = $value;
30
31 1
        return $this;
32
    }
33
34
35 5
    public function getHullDamageFactor(): int
36
    {
37 5
        return $this->hull_damage_factor;
38
    }
39
40 1
    public function setCrit(bool $isCrit): DamageWrapper
41
    {
42 1
        $this->isCrit = $isCrit;
43
44 1
        return $this;
45
    }
46
47
    public function isCrit(): bool
48
    {
49
        return $this->isCrit;
50
    }
51
52
    public function setShieldPenetration(bool $isShieldPenetration): void
53
    {
54
        $this->isShieldPenetration = $isShieldPenetration;
55
    }
56
57
    public function isShieldPenetration(): bool
58
    {
59
        return $this->isShieldPenetration;
60
    }
61
62
    private int $shield_damage_factor = 100;
63
64
65 1
    public function setShieldDamageFactor(int $value): DamageWrapper
66
    {
67 1
        $this->shield_damage_factor = $value;
68
69 1
        return $this;
70
    }
71
72
73 6
    public function getShieldDamageFactor(): int
74
    {
75 6
        return $this->shield_damage_factor;
76
    }
77
78
    private bool $is_phaser_damage = false;
79
80
81 7
    public function setIsPhaserDamage(bool $value): void
82
    {
83 7
        $this->is_phaser_damage = $value;
84
    }
85
86
87 6
    public function getIsPhaserDamage(): bool
88
    {
89 6
        return $this->is_phaser_damage;
90
    }
91
92
    private bool $is_torpedo_damage = false;
93
94
95 5
    public function setIsTorpedoDamage(bool $value): void
96
    {
97 5
        $this->is_torpedo_damage = $value;
98
    }
99
100
101 5
    public function getIsTorpedoDamage(): bool
102
    {
103 5
        return $this->is_torpedo_damage;
104
    }
105
106 11
    public function setNetDamage(float $value): void
107
    {
108 11
        $this->netDamage = $value;
109
    }
110
111 13
    public function getNetDamage(): float
112
    {
113 13
        return $this->netDamage;
114
    }
115
116 3
    public function getModificator(): int
117
    {
118 3
        return $this->modificator;
119
    }
120
121 11
    public function setModificator(int $value): void
122
    {
123 11
        $this->modificator = $value;
124
    }
125
126 1
    public function setPirateWrath(UserInterface $attacker, SpacecraftInterface $target): void
127
    {
128 1
        if ($attacker->getId() !== UserEnum::USER_NPC_KAZON) {
129 1
            return;
130
        }
131
132
        $pirateWrath = $target->getUser()->getPirateWrath();
133
        if ($pirateWrath === null) {
134
            return;
135
        }
136
137
        $this->pirateWrath = $pirateWrath->getWrath();
138
    }
139
140 3
    public function canDamageSystem(SpacecraftSystemTypeEnum $type): bool
141
    {
142 3
        return $this->targetSystemTypes === null
143 3
            || in_array($type, $this->targetSystemTypes);
144
    }
145
146
    /** @param array<SpacecraftSystemTypeEnum> $targetSystemTypes */
147 2
    public function setTargetSystemTypes(array $targetSystemTypes): DamageWrapper
148
    {
149 2
        $this->targetSystemTypes = $targetSystemTypes;
150
151 2
        return $this;
152
    }
153
154 11
    public function getDamageRelative(ColonyInterface|SpacecraftInterface $target, DamageModeEnum $mode): float
155
    {
156 11
        if ($target instanceof ColonyInterface) {
157
            if ($mode === DamageModeEnum::HULL) {
158
                return $this->calculateDamageBuilding();
159
            }
160
            return $this->calculateDamageColonyShields($target);
161
        }
162 11
        if ($mode === DamageModeEnum::HULL) {
163 5
            return $this->calculateDamageHull();
164
        }
165
166 6
        return $this->calculateDamageShields($target);
167
    }
168
169
170 6
    private function calculateDamageShields(SpacecraftInterface $target): float
171
    {
172 6
        $netDamage = $this->getNetDamage();
173 6
        $netDamage = $this->mindPirateWrath($netDamage);
174
175 6
        $targetShields = $target->getShield();
176
177 6
        $grossModificator = $this->getShieldDamageFactor() / 100;
178 6
        if ($this->getIsPhaserDamage() === true) {
179 3
            $grossModificator = $grossModificator * $this->modificator / 100;
180
        }
181
182 6
        $neededNetDamageForShields = min($netDamage, (int)ceil($targetShields / $grossModificator));
183 6
        $grossDamage = min($targetShields, $neededNetDamageForShields * $grossModificator);
184
185 6
        if ($neededNetDamageForShields >= $netDamage) {
186 3
            $this->setNetDamage(0);
187
        } else {
188 3
            $this->setNetDamage($netDamage - $neededNetDamageForShields);
189
        }
190
191 6
        return $grossDamage;
192
    }
193
194
195
    private function calculateDamageColonyShields(ColonyInterface $target): float
196
    {
197
        $damage = round($this->getNetDamage() / 100 * $this->getShieldDamageFactor());
198
199
        if ($damage < $target->getShields()) {
200
            $this->setNetDamage(0);
201
        } else {
202
            $this->setNetDamage(round($damage - $target->getShields() / $this->getShieldDamageFactor() * 100));
203
        }
204
        return $damage;
205
    }
206
207
208 5
    private function calculateDamageHull(): float
209
    {
210 5
        $damage = round($this->getNetDamage() / 100 * $this->getHullDamageFactor());
211 5
        $damage = $this->mindPirateWrath($damage);
212
213 5
        if ($this->getIsTorpedoDamage() === true) {
214 3
            $damage = round($damage * ($this->getModificator() / 100));
215
        }
216 5
        return $damage;
217
    }
218
219
220
    private function calculateDamageBuilding(): float
221
    {
222
        return round($this->getNetDamage() / 100 * $this->getHullDamageFactor());
223
    }
224
225 11
    private function mindPirateWrath(float $damage): float
226
    {
227 11
        if ($this->pirateWrath === null) {
228 11
            return $damage;
229
        }
230
231
        return round($damage / PirateWrathManager::DEFAULT_WRATH * $this->pirateWrath);
232
    }
233
}
234