Passed
Pull Request — master (#1833)
by Nico
34:40
created

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