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

SpacecraftAttacker::getUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
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\ModuleInterface;
14
use Stu\Orm\Entity\SpacecraftInterface;
15
use Stu\Orm\Entity\TorpedoTypeInterface;
16
17
class SpacecraftAttacker extends AbstractEnergyAttacker implements ProjectileAttackerInterface
18
{
19 24
    public function __construct(
20
        private SpacecraftWrapperInterface $wrapper,
21
        private ShipTorpedoManagerInterface $shipTorpedoManager,
22
        private StuRandom $stuRandom
23 24
    ) {}
24
25 1
    #[Override]
26
    public function getPhaserVolleys(): int
27
    {
28 1
        return $this->get()->getRump()->getPhaserVolleys();
29
    }
30
31 1
    #[Override]
32
    public function getPhaserState(): bool
33
    {
34 1
        return $this->get()->getPhaserState();
35
    }
36
37 3
    #[Override]
38
    public function hasSufficientEnergy(int $amount): bool
39
    {
40 3
        $epsSystemData = $this->wrapper->getEpsSystemData();
41 3
        if ($epsSystemData === null) {
42 1
            return false;
43
        }
44 2
        return $epsSystemData->getEps() >= $amount;
45
    }
46
47 2
    #[Override]
48
    public function getWeaponModule(): ModuleInterface
49
    {
50 2
        if ($this->module === null) {
51 2
            $shipSystem = $this->get()->getSpacecraftSystem(SpacecraftSystemTypeEnum::PHASER);
52
53 2
            $module = $shipSystem->getModule();
54 2
            if ($module === null) {
55 1
                throw new RuntimeException('weapon system should have a module');
56
            }
57
58 1
            $this->module = $module;
59
        }
60
61 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...
62
    }
63
64 1
    #[Override]
65
    public function getEnergyWeaponBaseDamage(): int
66
    {
67 1
        return $this->get()->getBaseDamage();
68
    }
69
70 2
    #[Override]
71
    public function reduceEps(int $amount): void
72
    {
73 2
        $epsSystemData = $this->wrapper->getEpsSystemData();
74 2
        if ($epsSystemData === null) {
75 1
            return;
76
        }
77 1
        $epsSystemData->lowerEps($amount)->update();
78
    }
79
80 1
    #[Override]
81
    public function getName(): string
82
    {
83 1
        return $this->get()->getName();
84
    }
85
86 1
    #[Override]
87
    public function getUserId(): int
88
    {
89 1
        return $this->get()->getUser()->getId();
90
    }
91
92 17
    private function get(): SpacecraftInterface
93
    {
94 17
        return $this->wrapper->get();
95
    }
96
97 1
    #[Override]
98
    public function getHitChance(): int
99
    {
100 1
        return $this->get()->getHitChance();
101
    }
102
103 1
    #[Override]
104
    public function getPhaserShieldDamageFactor(): int
105
    {
106 1
        return $this->get()->getRump()->getPhaserShieldDamageFactor();
107
    }
108
109 1
    #[Override]
110
    public function getPhaserHullDamageFactor(): int
111
    {
112 1
        return $this->get()->getRump()->getPhaserHullDamageFactor();
113
    }
114
115 1
    #[Override]
116
    public function getTorpedoVolleys(): int
117
    {
118 1
        return $this->get()->getRump()->getTorpedoVolleys();
119
    }
120
121 1
    #[Override]
122
    public function getTorpedoState(): bool
123
    {
124 1
        return $this->get()->getTorpedoState();
125
    }
126
127 1
    #[Override]
128
    public function getTorpedoCount(): int
129
    {
130 1
        return $this->get()->getTorpedoCount();
131
    }
132
133 4
    #[Override]
134
    public function getTorpedo(): ?TorpedoTypeInterface
135
    {
136 4
        return $this->get()->getTorpedo();
137
    }
138
139 1
    #[Override]
140
    public function lowerTorpedoCount(int $amount): void
141
    {
142 1
        $this->shipTorpedoManager->changeTorpedo($this->wrapper, -$amount);
143
    }
144
145
    #[Override]
146
    public function isShieldPenetration(): bool
147
    {
148
        $systemData = $this->wrapper->getProjectileLauncherSystemData();
149
        if ($systemData === null) {
150
            throw new RuntimeException('this should not happen');
151
        }
152
153
        return $this->stuRandom->rand(1, 10000) <= $systemData->getShieldPenetration();
154
    }
155
156 3
    #[Override]
157
    public function getProjectileWeaponDamage(bool $isCritical): int
158
    {
159 3
        $torpedo = $this->getTorpedo();
160 3
        if ($torpedo === null) {
161 1
            return 0;
162
        }
163
164 2
        $module = $this->get()->getSpacecraftSystem(SpacecraftSystemTypeEnum::TORPEDO)->getModule();
165 2
        if ($module === null) {
166 1
            return 0;
167
        }
168
169 1
        $variance = (int) round($torpedo->getBaseDamage() / 100 * $torpedo->getVariance());
170 1
        $basedamage = $torpedo->getBaseDamage();
171 1
        $damage = random_int($basedamage - $variance, $basedamage + $variance);
172
173 1
        return $isCritical ? $damage * 2 : $damage;
174
    }
175
}
176