Passed
Push — dev ( 0aa780...31f66c )
by Janko
11:20
created

SpacecraftAttackCore::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 7
dl 0
loc 9
ccs 2
cts 2
cp 1
crap 1
rs 10
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 RuntimeException;
9
use Stu\Lib\Information\InformationWrapper;
10
use Stu\Module\Message\Lib\DistributedMessageSenderInterface;
11
use Stu\Module\Message\Lib\PrivateMessageFolderTypeEnum;
12
use Stu\Module\Spacecraft\Lib\Battle\AlertDetection\AlertReactionFacadeInterface;
13
use Stu\Module\Spacecraft\Lib\Battle\Party\AttackingBattleParty;
14
use Stu\Module\Spacecraft\Lib\Battle\Party\BattlePartyFactoryInterface;
15
use Stu\Module\Spacecraft\Lib\Battle\Party\BattlePartyInterface;
16
use Stu\Module\Ship\Lib\FleetWrapperInterface;
17
use Stu\Module\Ship\Lib\TholianWebUtilInterface;
18
use Stu\Module\Spacecraft\Lib\Message\MessageCollectionInterface;
19
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperFactoryInterface;
20
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
21
use Stu\Orm\Entity\ShipInterface;
22
use Stu\Orm\Entity\SpacecraftInterface;
23
24
final class SpacecraftAttackCore implements SpacecraftAttackCoreInterface
25
{
26 1
    public function __construct(
27
        private DistributedMessageSenderInterface $distributedMessageSender,
28
        private SpacecraftAttackCycleInterface $spacecraftAttackCycle,
29
        private AlertReactionFacadeInterface $alertReactionFacade,
30
        private FightLibInterface $fightLib,
31
        private TholianWebUtilInterface $tholianWebUtil,
32
        private SpacecraftWrapperFactoryInterface $spacecraftWrapperFactory,
33
        private BattlePartyFactoryInterface $battlePartyFactory
34 1
    ) {}
35
36
    #[Override]
37
    public function attack(
38
        SpacecraftWrapperInterface|FleetWrapperInterface $sourceWrapper,
39
        SpacecraftWrapperInterface $targetWrapper,
40
        bool &$isFleetFight,
41
        InformationWrapper $informations
42
    ): void {
43
        $wrapper = $sourceWrapper instanceof SpacecraftWrapperInterface ? $sourceWrapper : $sourceWrapper->getLeadWrapper();
44
        $ship = $wrapper->get();
45
46
        $target = $targetWrapper->get();
47
        $userId = $ship->getUser()->getId();
48
        $isTargetBase = $target->isStation();
49
50
        $isActiveTractorShipWarped = $this->isActiveTractorShipWarped($ship, $target);
51
52
        [$attacker, $defender, $isFleetFight, $attackCause] = $this->getAttackersAndDefenders(
53
            $wrapper,
54
            $targetWrapper
55
        );
56
57
        $messageCollection = $this->spacecraftAttackCycle->cycle($attacker, $defender, $attackCause);
58
59
        $this->sendPms(
60
            $userId,
61
            $ship->getSectorString(),
62
            $messageCollection,
63
            ($attackCause !== SpacecraftAttackCauseEnum::THOLIAN_WEB_REFLECTION) && $isTargetBase
64
        );
65
66
        $informations->addInformationWrapper($messageCollection->getInformationDump());
67
68
        if ($isActiveTractorShipWarped) {
69
            //Alarm-Rot check for ship
70
            if (!$ship->isDestroyed()) {
71
                $this->alertReactionFacade->doItAll($wrapper, $informations);
0 ignored issues
show
Bug introduced by
It seems like $wrapper can also be of type Stu\Module\Ship\Lib\FleetWrapperInterface; however, parameter $incomingWrapper of Stu\Module\Spacecraft\Li...adeInterface::doItAll() does only seem to accept Stu\Module\Spacecraft\Li...cecraftWrapperInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
                $this->alertReactionFacade->doItAll(/** @scrutinizer ignore-type */ $wrapper, $informations);
Loading history...
72
            }
73
74
            //Alarm-Rot check for traktor ship
75
            if (!$target->isDestroyed()) {
76
                $this->alertReactionFacade->doItAll($targetWrapper, $informations);
77
            }
78
        }
79
    }
80
81
    private function isActiveTractorShipWarped(SpacecraftInterface $spacecraft, SpacecraftInterface $target): bool
82
    {
83
        $tractoringShip = $spacecraft instanceof ShipInterface ? $spacecraft->getTractoringSpacecraft() : null;
84
        if ($tractoringShip === null) {
85
            return false;
86
        }
87
88
        if ($tractoringShip !== $target) {
89
            return false;
90
        } else {
91
            return $target->getWarpDriveState();
92
        }
93
    }
94
95
    private function sendPms(
96
        int $userId,
97
        string $sectorString,
98
        MessageCollectionInterface $messageCollection,
99
        bool $isTargetBase
100
    ): void {
101
102
        $header = sprintf(
103
            _("Kampf in Sektor %s"),
104
            $sectorString
105
        );
106
107
        $this->distributedMessageSender->distributeMessageCollection(
108
            $messageCollection,
109
            $userId,
110
            $isTargetBase ? PrivateMessageFolderTypeEnum::SPECIAL_STATION : PrivateMessageFolderTypeEnum::SPECIAL_SHIP,
111
            $header
112
        );
113
    }
114
115
    /**
116
     * @return array{0: AttackingBattleParty, 1: BattlePartyInterface, 2: bool, 3: SpacecraftAttackCauseEnum}
117
     */
118
    private function getAttackersAndDefenders(SpacecraftWrapperInterface|FleetWrapperInterface $wrapper, SpacecraftWrapperInterface $targetWrapper): array
119
    {
120
        $attackCause = SpacecraftAttackCauseEnum::SHIP_FIGHT;
121
        $ship = $wrapper instanceof SpacecraftWrapperInterface ? $wrapper->get() : $wrapper->get()->getLeadShip();
122
123
        [$attacker, $defender, $isFleetFight] = $this->fightLib->getAttackersAndDefenders($wrapper, $targetWrapper, $this->battlePartyFactory);
124
125
        $isTargetOutsideFinishedWeb = $this->tholianWebUtil->isTargetOutsideFinishedTholianWeb($ship, $targetWrapper->get());
126
127
        //if in tholian web and defenders outside, reflect damage
128
        if ($isTargetOutsideFinishedWeb) {
129
            $attackCause = SpacecraftAttackCauseEnum::THOLIAN_WEB_REFLECTION;
130
            $holdingWeb = $ship->getHoldingWeb();
131
            if ($holdingWeb === null) {
132
                throw new RuntimeException('this should not happen');
133
            }
134
135
            $defender = $this->battlePartyFactory->createMixedBattleParty(
136
                $this->spacecraftWrapperFactory->wrapSpacecrafts($holdingWeb->getCapturedSpacecrafts()->toArray())
137
            );
138
        } elseif ($this->tholianWebUtil->isTargetInsideFinishedTholianWeb(
139
            $ship,
140
            $targetWrapper->get()
141
        )) {
142
            $attackCause = SpacecraftAttackCauseEnum::THOLIAN_WEB_REFLECTION;
143
        }
144
145
        return [
146
            $attacker,
147
            $defender,
148
            $isFleetFight,
149
            $attackCause
150
        ];
151
    }
152
}
153