Passed
Pull Request — master (#1969)
by Janko
22:34 queued 10:03
created

FightLib::isBoardingPossible()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 7

Importance

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