Passed
Push — master ( 48c747...6d626d )
by Nico
18:33 queued 08:41
created

FightLib::ready()   C

Complexity

Conditions 12
Paths 67

Size

Total Lines 47
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 12

Importance

Changes 0
Metric Value
cc 12
eloc 28
nc 67
nop 2
dl 0
loc 47
ccs 25
cts 25
cp 1
crap 12
rs 6.9666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Spacecraft\Lib\Battle;
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 Stu\Component\Spacecraft\Repair\CancelRepairInterface;
9
use Stu\Component\Ship\Retrofit\CancelRetrofitInterface;
10
use Stu\Component\Spacecraft\SpacecraftTypeEnum;
11
use Stu\Component\Spacecraft\System\Exception\SpacecraftSystemException;
12
use Stu\Component\Spacecraft\System\SpacecraftSystemManagerInterface;
13
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
14
use Stu\Lib\Information\InformationFactoryInterface;
15
use Stu\Lib\Information\InformationInterface;
16
use Stu\Module\Spacecraft\Lib\Battle\Party\BattlePartyFactoryInterface;
17
use Stu\Module\Ship\Lib\FleetWrapperInterface;
18
use Stu\Module\Spacecraft\Lib\SpacecraftNfsItem;
19
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
20
use Stu\Module\Spacecraft\Lib\TrumfieldNfsItem;
21
use Stu\Orm\Entity\Ship;
22
use Stu\Orm\Entity\Spacecraft;
23
use Stu\Orm\Entity\User;
24
25
final class FightLib implements FightLibInterface
26
{
27 31
    public function __construct(
28
        private SpacecraftSystemManagerInterface $spacecraftSystemManager,
29
        private  CancelRepairInterface $cancelRepair,
30
        private CancelRetrofitInterface $cancelRetrofit,
31
        private AlertLevelBasedReactionInterface $alertLevelBasedReaction,
32
        private InformationFactoryInterface $informationFactory
33 31
    ) {}
34
35 6
    #[Override]
36
    public function ready(SpacecraftWrapperInterface $wrapper, InformationInterface $informations): void
37
    {
38 6
        $spacecraft = $wrapper->get();
39
40
        if (
41 6
            $spacecraft->getCondition()->isDestroyed()
42 6
            || $spacecraft->getRump()->isEscapePods()
43
        ) {
44 2
            return;
45
        }
46 4
        if ($spacecraft->getBuildplan() === null) {
47 1
            return;
48
        }
49 3
        if (!$spacecraft->hasEnoughCrew()) {
50 1
            return;
51
        }
52
53 2
        $informationWrapper = $this->informationFactory->createInformationWrapper();
54
55 2
        if ($spacecraft instanceof Ship && $spacecraft->getDockedTo() !== null) {
56 1
            $spacecraft->setDockedTo(null);
57 1
            $informationWrapper->addInformation("- Das Schiff hat abgedockt");
58
        }
59
60
        try {
61 2
            $this->spacecraftSystemManager->deactivate($wrapper, SpacecraftSystemTypeEnum::WARPDRIVE);
62 1
        } catch (SpacecraftSystemException) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
63
        }
64
        try {
65 2
            $this->spacecraftSystemManager->deactivate($wrapper, SpacecraftSystemTypeEnum::CLOAK);
66 1
        } catch (SpacecraftSystemException) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
67
        }
68
69 2
        $this->cancelRepair->cancelRepair($spacecraft);
70
71 2
        if ($spacecraft instanceof Ship) {
72 2
            $this->cancelRetrofit->cancelRetrofit($spacecraft);
73
        }
74
75 2
        if ($spacecraft->hasComputer()) {
76 1
            $this->alertLevelBasedReaction->react($wrapper, $informationWrapper);
77
        }
78
79 2
        if (!$informationWrapper->isEmpty()) {
80 1
            $informations->addInformationf('Aktionen der %s', $spacecraft->getName());
81 1
            $informationWrapper->dumpTo($informations);
82
        }
83
    }
84
85 14
    #[Override]
86
    public function canAttackTarget(
87
        Spacecraft $spacecraft,
88
        Spacecraft|SpacecraftNfsItem $target,
89
        bool $checkCloaked = false,
90
        bool $checkActiveWeapons = true,
91
        bool $checkWarped = true
92
    ): bool {
93 14
        if ($checkActiveWeapons && !$spacecraft->hasActiveWeapon()) {
94 2
            return false;
95
        }
96
97
        //can't attack itself
98 12
        if ($target === $spacecraft) {
99 1
            return false;
100
        }
101
102
        //can't attack cloaked target
103 11
        if ($checkCloaked && $target->isCloaked()) {
104 1
            return false;
105
        }
106
107
        //if tractored, can only attack tractoring ship
108 10
        $tractoringShip = $spacecraft instanceof Ship ? $spacecraft->getTractoringSpacecraft() : null;
109 10
        if ($tractoringShip !== null) {
110 3
            return $target->getId() === $tractoringShip->getId();
111
        }
112
113
        //can't attack target under warp
114 7
        if ($checkWarped && $target->isWarped()) {
115 1
            return false;
116
        }
117
118
        //can't attack own target under cloak
119
        if (
120 6
            $target->getUserId() === $spacecraft->getUserId()
121 6
            && $target->isCloaked()
122
        ) {
123 1
            return false;
124
        }
125
126
        //can't attack same fleet
127 5
        $ownFleetId = $spacecraft instanceof Ship ? $spacecraft->getFleetId() : null;
128 5
        $targetFleetId = ($target instanceof Ship || $target instanceof SpacecraftNfsItem) ? $target->getFleetId() : null;
129 5
        if ($ownFleetId === null || $targetFleetId === null) {
130 2
            return true;
131
        }
132
133 3
        return $ownFleetId !== $targetFleetId;
134
    }
135
136 3
    #[Override]
137
    public function getAttackersAndDefenders(
138
        SpacecraftWrapperInterface|FleetWrapperInterface $wrapper,
139
        SpacecraftWrapperInterface $targetWrapper,
140
        bool $isAttackingShieldsOnly,
141
        BattlePartyFactoryInterface $battlePartyFactory
142
    ): array {
143 3
        $attackers = $battlePartyFactory->createAttackingBattleParty($wrapper, $isAttackingShieldsOnly);
144 3
        $defenders = $battlePartyFactory->createAttackedBattleParty($targetWrapper);
145
146 3
        return [
147 3
            $attackers,
148 3
            $defenders,
149 3
            count($attackers) + count($defenders) > 2
150 3
        ];
151
    }
152
153 8
    public static function isBoardingPossible(Spacecraft|SpacecraftNfsItem|TrumfieldNfsItem $object): bool
154
    {
155 8
        if ($object instanceof TrumfieldNfsItem) {
156 1
            return false;
157
        }
158
159 7
        $type = $object->getType();
160 7
        if ($type !== SpacecraftTypeEnum::SHIP) {
161 3
            return false;
162
        }
163
164 4
        return !(User::isUserNpc($object->getUserId())
165 4
            || $object->isCloaked()
166 4
            || $object->isShielded()
167 4
            || $object->isWarped());
168
    }
169
170
    #[Override]
171
    public function calculateHealthPercentage(Ship $target): int
172
    {
173
        $shipCount = 0;
174
        $healthSum = 0;
175
176
        $fleet = $target->getFleet();
177
        if ($fleet !== null) {
178
            foreach ($fleet->getShips() as $ship) {
179
                $shipCount++;
180
                $healthSum += $ship->getHealthPercentage();
181
            }
182
        } else {
183
            $shipCount++;
184
            $healthSum += $target->getHealthPercentage();
185
        }
186
187
        return (int)($healthSum / $shipCount);
188
    }
189
}
190