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