Passed
Push — dev ( 37f80a...a18392 )
by Janko
15:21
created

ApplyDamage::damage()   B

Complexity

Conditions 9
Paths 22

Size

Total Lines 42
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
cc 9
eloc 25
c 0
b 0
f 0
nc 22
nop 3
dl 0
loc 42
ccs 0
cts 23
cp 0
crap 90
rs 8.0555
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Spacecraft\Lib\Damage;
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\SpacecraftSystemManagerInterface;
10
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
11
use Stu\Lib\Damage\DamageModeEnum;
12
use Stu\Lib\Damage\DamageWrapper;
13
use Stu\Lib\Information\InformationInterface;
14
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
15
16
//TODO unit tests
17
final class ApplyDamage implements ApplyDamageInterface
18
{
19 1
    public function __construct(
20
        private SpacecraftSystemManagerInterface $spacecraftSystemManager,
21
        private SystemDamageInterface $systemDamage
22 1
    ) {}
23
24
    #[Override]
25
    public function damage(
26
        DamageWrapper $damageWrapper,
27
        SpacecraftWrapperInterface $wrapper,
28
        InformationInterface $informations
29
    ): void {
30
31
        if ($damageWrapper->getNetDamage() <= 0) {
32
            throw new RuntimeException('this should not happen');
33
        }
34
35
        $spacecraft = $wrapper->get();
36
37
        if ($spacecraft->isShielded()) {
38
39
            if ($damageWrapper->isShieldPenetration()) {
40
                $informations->addInformationf('- Projektil hat Schilde durchdrungen!');
41
            } else {
42
                $this->damageShields($wrapper, $damageWrapper, $informations);
43
            }
44
        }
45
        if ($damageWrapper->getNetDamage() <= 0) {
46
            return;
47
        }
48
49
        $disablemessage = false;
50
        $damage = (int) $damageWrapper->getDamageRelative($spacecraft, DamageModeEnum::HULL);
51
        if ($spacecraft->getSystemState(SpacecraftSystemTypeEnum::RPG_MODULE) && $spacecraft->getHull() - $damage < round($spacecraft->getMaxHull() / 100 * 10)) {
52
            $damage = (int) round($spacecraft->getHull() - $spacecraft->getMaxHull() / 100 * 10);
53
            $disablemessage = _('-- Das Schiff wurde kampfunfähig gemacht');
54
            $spacecraft->setDisabled(true);
55
        }
56
        if ($spacecraft->getHull() > $damage) {
57
            $this->damageHull($wrapper, $damageWrapper, $damage, $informations);
58
59
            if ($disablemessage) {
60
                $informations->addInformation($disablemessage);
61
            }
62
        } else {
63
            $informations->addInformation("- Hüllenschaden: " . $damage);
64
            $informations->addInformation("-- Das Schiff wurde zerstört!");
65
            $spacecraft->setIsDestroyed(true);
66
        }
67
    }
68
69
    private function damageShields(SpacecraftWrapperInterface $wrapper, DamageWrapper $damageWrapper, InformationInterface $informations): void
70
    {
71
        $spacecraft = $wrapper->get();
72
73
        $damage = (int) $damageWrapper->getDamageRelative($spacecraft, DamageModeEnum::SHIELDS);
74
        if ($damage >= $spacecraft->getShield()) {
75
            $informations->addInformation("- Schildschaden: " . $spacecraft->getShield());
76
            $informations->addInformation("-- Schilde brechen zusammen!");
77
78
            $this->spacecraftSystemManager->deactivate($wrapper, SpacecraftSystemTypeEnum::SHIELDS);
79
80
            $spacecraft->setShield(0);
81
        } else {
82
            $spacecraft->setShield($spacecraft->getShield() - $damage);
83
            $informations->addInformation("- Schildschaden: " . $damage . " - Status: " . $spacecraft->getShield());
84
        }
85
86
        $shieldSystemData = $wrapper->getShieldSystemData();
87
        if ($shieldSystemData === null) {
88
            throw new RuntimeException('this should not happen');
89
        }
90
91
        $shieldSystemData->setShieldRegenerationTimer(time())->update();
92
    }
93
94
    private function damageHull(SpacecraftWrapperInterface $wrapper, DamageWrapper $damageWrapper, int $damage, InformationInterface $informations): void
95
    {
96
        $spacecraft = $wrapper->get();
97
98
        if ($damageWrapper->isCrit()) {
99
            $systemName = $this->systemDamage->destroyRandomShipSystem($wrapper, $damageWrapper);
100
101
            if ($systemName !== null) {
102
                $informations->addInformationf("- Kritischer Hüllen-Treffer zerstört System: %s", $systemName);
103
            }
104
        }
105
        $huelleVorher = $spacecraft->getHull();
106
        $spacecraft->setHuell($huelleVorher - $damage);
107
        $informations->addInformationf("- Hüllenschaden: %d - Status: %d", $damage, $spacecraft->getHull());
108
109
        if (!$this->systemDamage->checkForDamagedShipSystems(
110
            $wrapper,
111
            $damageWrapper,
112
            $huelleVorher,
113
            $informations
114
        )) {
115
            $this->systemDamage->damageRandomShipSystem(
116
                $wrapper,
117
                $damageWrapper,
118
                $informations,
119
                (int)ceil((100 * $damage * random_int(1, 5)) / $spacecraft->getMaxHull())
120
            );
121
        }
122
    }
123
}
124