Passed
Push — dev ( f29cfd...d7eff6 )
by Janko
11:00
created

FightLib::canAttackTarget()   C

Complexity

Conditions 17
Paths 41

Size

Total Lines 49
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 17

Importance

Changes 0
Metric Value
cc 17
eloc 21
nc 41
nop 5
dl 0
loc 49
ccs 20
cts 20
cp 1
crap 17
rs 5.2166
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\ShipInterface;
22
use Stu\Orm\Entity\SpacecraftInterface;
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->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 ShipInterface && $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 ShipInterface) {
72 2
            $this->cancelRetrofit->cancelRetrofit($spacecraft);
73
        }
74
75 2
        $this->alertLevelBasedReaction->react($wrapper, $informationWrapper);
76
77 2
        if (!$informationWrapper->isEmpty()) {
78 1
            $informations->addInformationf('Aktionen der %s', $spacecraft->getName());
79 1
            $informationWrapper->dumpTo($informations);
80
        }
81
    }
82
83 14
    #[Override]
84
    public function canAttackTarget(
85
        SpacecraftInterface $spacecraft,
86
        SpacecraftInterface|SpacecraftNfsItem $target,
87
        bool $checkCloaked = false,
88
        bool $checkActiveWeapons = true,
89
        bool $checkWarped = true
90
    ): bool {
91 14
        if ($checkActiveWeapons && !$spacecraft->hasActiveWeapon()) {
92 2
            return false;
93
        }
94
95
        //can't attack itself
96 12
        if ($target === $spacecraft) {
97 1
            return false;
98
        }
99
100
        //can't attack cloaked target
101 11
        if ($checkCloaked && $target->getCloakState()) {
102 1
            return false;
103
        }
104
105
        //if tractored, can only attack tractoring ship
106 10
        $tractoringShip = $spacecraft instanceof ShipInterface ? $spacecraft->getTractoringSpacecraft() : null;
107 10
        if ($tractoringShip !== null) {
108 3
            return $target->getId() === $tractoringShip->getId();
109
        }
110
111
        //can't attack target under warp
112 7
        if ($checkWarped && $target->isWarped()) {
113 1
            return false;
114
        }
115
116
        //can't attack own target under cloak
117
        if (
118 6
            $target->getUserId() === $spacecraft->getUserId()
119 6
            && $target->getCloakState()
120
        ) {
121 1
            return false;
122
        }
123
124
        //can't attack same fleet
125 5
        $ownFleetId = $spacecraft instanceof ShipInterface ? $spacecraft->getFleetId() : null;
126 5
        $targetFleetId = ($target instanceof ShipInterface || $target instanceof SpacecraftNfsItem) ? $target->getFleetId() : null;
127 5
        if ($ownFleetId === null || $targetFleetId === null) {
128 2
            return true;
129
        }
130
131 3
        return $ownFleetId !== $targetFleetId;
132
    }
133
134 3
    #[Override]
135
    public function getAttackersAndDefenders(
136
        SpacecraftWrapperInterface|FleetWrapperInterface $wrapper,
137
        SpacecraftWrapperInterface $targetWrapper,
138
        BattlePartyFactoryInterface $battlePartyFactory
139
    ): array {
140 3
        $attackers = $battlePartyFactory->createAttackingBattleParty($wrapper);
141 3
        $defenders = $battlePartyFactory->createAttackedBattleParty($targetWrapper);
142
143 3
        return [
144 3
            $attackers,
145 3
            $defenders,
146 3
            count($attackers) + count($defenders) > 2
147 3
        ];
148
    }
149
150 8
    public static function isBoardingPossible(SpacecraftInterface|SpacecraftNfsItem|TrumfieldNfsItem $object): bool
151
    {
152 8
        if ($object instanceof TrumfieldNfsItem) {
153 1
            return false;
154
        }
155
156 7
        $type = $object->getType();
157 7
        if ($type !== SpacecraftTypeEnum::SHIP) {
158 3
            return false;
159
        }
160
161 4
        return !(User::isUserNpc($object->getUserId())
162 4
            || $object->getCloakState()
163 4
            || $object->getShieldState()
164 4
            || $object->isWarped());
165
    }
166
167
    #[Override]
168
    public function calculateHealthPercentage(ShipInterface $target): int
169
    {
170
        $shipCount = 0;
171
        $healthSum = 0;
172
173
        $fleet = $target->getFleet();
174
        if ($fleet !== null) {
175
            foreach ($fleet->getShips() as $ship) {
176
                $shipCount++;
177
                $healthSum += $ship->getHealthPercentage();
178
            }
179
        } else {
180
            $shipCount++;
181
            $healthSum += $target->getHealthPercentage();
182
        }
183
184
        return (int)($healthSum / $shipCount);
185
    }
186
}
187