Passed
Push — dev ( fc77d3...501ee6 )
by Janko
33:44
created

ShipAttacker::getTorpedoCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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