Passed
Push — master ( e661f4...30021e )
by Nico
25:16 queued 10:59
created

SpacecraftAttacker::reduceEps()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Spacecraft\Lib\Battle\Provider;
6
7
use Override;
0 ignored issues
show
Bug introduced by
The type Override 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 RuntimeException;
9
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
10
use Stu\Module\Control\StuRandom;
11
use Stu\Module\Spacecraft\Lib\Torpedo\ShipTorpedoManagerInterface;
12
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
13
use Stu\Orm\Entity\LocationInterface;
14
use Stu\Orm\Entity\ModuleInterface;
15
use Stu\Orm\Entity\SpacecraftInterface;
16
use Stu\Orm\Entity\TorpedoTypeInterface;
17
18
class SpacecraftAttacker extends AbstractEnergyAttacker implements ProjectileAttackerInterface
19
{
20 24
    public function __construct(
21
        private SpacecraftWrapperInterface $wrapper,
22
        private ShipTorpedoManagerInterface $shipTorpedoManager,
23
        private bool $isAttackingShieldsOnly,
24
        StuRandom $stuRandom
25
    ) {
26 24
        parent::__construct($stuRandom);
27
    }
28
29
    #[Override]
30
    public function isAvoidingHullHits(SpacecraftInterface $target): bool
31
    {
32
        return $this->isAttackingShieldsOnly && !$target->isShielded();
33
    }
34
35 1
    #[Override]
36
    public function getPhaserVolleys(): int
37
    {
38 1
        return $this->get()->getRump()->getPhaserVolleys();
39
    }
40
41 1
    #[Override]
42
    public function getPhaserState(): bool
43
    {
44 1
        return $this->get()->getPhaserState();
45
    }
46
47 3
    #[Override]
48
    public function hasSufficientEnergy(int $amount): bool
49
    {
50 3
        $epsSystemData = $this->wrapper->getEpsSystemData();
51 3
        if ($epsSystemData === null) {
52 1
            return false;
53
        }
54 2
        return $epsSystemData->getEps() >= $amount;
55
    }
56
57 2
    #[Override]
58
    public function getWeaponModule(): ModuleInterface
59
    {
60 2
        if ($this->module === null) {
61 2
            $shipSystem = $this->get()->getSpacecraftSystem(SpacecraftSystemTypeEnum::PHASER);
62
63 2
            $module = $shipSystem->getModule();
64 2
            if ($module === null) {
65 1
                throw new RuntimeException('weapon system should have a module');
66
            }
67
68 1
            $this->module = $module;
69
        }
70
71 1
        return $this->module;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->module could return the type null which is incompatible with the type-hinted return Stu\Orm\Entity\ModuleInterface. Consider adding an additional type-check to rule them out.
Loading history...
72
    }
73
74 1
    #[Override]
75
    public function getEnergyWeaponBaseDamage(): int
76
    {
77 1
        return $this->get()->getBaseDamage();
78
    }
79
80 2
    #[Override]
81
    public function reduceEps(int $amount): void
82
    {
83 2
        $epsSystemData = $this->wrapper->getEpsSystemData();
84 2
        if ($epsSystemData === null) {
85 1
            return;
86
        }
87 1
        $epsSystemData->lowerEps($amount)->update();
88
    }
89
90 1
    #[Override]
91
    public function getName(): string
92
    {
93 1
        return $this->get()->getName();
94
    }
95
96 1
    #[Override]
97
    public function getUserId(): int
98
    {
99 1
        return $this->get()->getUser()->getId();
100
    }
101
102 17
    private function get(): SpacecraftInterface
103
    {
104 17
        return $this->wrapper->get();
105
    }
106
107 1
    #[Override]
108
    public function getHitChance(): int
109
    {
110 1
        return $this->get()->getHitChance();
111
    }
112
113 1
    #[Override]
114
    public function getPhaserShieldDamageFactor(): int
115
    {
116 1
        return $this->get()->getRump()->getPhaserShieldDamageFactor();
117
    }
118
119 1
    #[Override]
120
    public function getPhaserHullDamageFactor(): int
121
    {
122 1
        return $this->get()->getRump()->getPhaserHullDamageFactor();
123
    }
124
125 1
    #[Override]
126
    public function getTorpedoVolleys(): int
127
    {
128 1
        return $this->get()->getRump()->getTorpedoVolleys();
129
    }
130
131 1
    #[Override]
132
    public function getTorpedoState(): bool
133
    {
134 1
        return $this->get()->getTorpedoState();
135
    }
136
137 1
    #[Override]
138
    public function getTorpedoCount(): int
139
    {
140 1
        return $this->get()->getTorpedoCount();
141
    }
142
143 4
    #[Override]
144
    public function getTorpedo(): ?TorpedoTypeInterface
145
    {
146 4
        return $this->get()->getTorpedo();
147
    }
148
149 1
    #[Override]
150
    public function lowerTorpedoCount(int $amount): void
151
    {
152 1
        $this->shipTorpedoManager->changeTorpedo($this->wrapper, -$amount);
153
    }
154
155
    #[Override]
156
    public function isShieldPenetration(): bool
157
    {
158
        $systemData = $this->wrapper->getProjectileLauncherSystemData();
159
        if ($systemData === null) {
160
            throw new RuntimeException('this should not happen');
161
        }
162
163
        return $this->stuRandom->rand(1, 10000) <= $systemData->getShieldPenetration();
164
    }
165
166 3
    #[Override]
167
    public function getProjectileWeaponDamage(bool $isCritical): int
168
    {
169 3
        $torpedo = $this->getTorpedo();
170 3
        if ($torpedo === null) {
171 1
            return 0;
172
        }
173
174 2
        $module = $this->get()->getSpacecraftSystem(SpacecraftSystemTypeEnum::TORPEDO)->getModule();
175 2
        if ($module === null) {
176 1
            return 0;
177
        }
178
179 1
        $variance = (int) round($torpedo->getBaseDamage() / 100 * $torpedo->getVariance());
180 1
        $basedamage = $torpedo->getBaseDamage();
181 1
        $damage = random_int($basedamage - $variance, $basedamage + $variance);
182
183 1
        return $isCritical ? $damage * 2 : $damage;
184
    }
185
186
    #[Override]
187
    public function getLocation(): LocationInterface
188
    {
189
        return $this->get()->getLocation();
190
    }
191
}
192