Passed
Push — master ( f4068b...77f637 )
by Nico
40:27 queued 14:09
created

AttackTrackedShip::getAttackerDefender()   C

Complexity

Conditions 13
Paths 8

Size

Total Lines 53
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 0
Metric Value
cc 13
eloc 30
nc 8
nop 2
dl 0
loc 53
ccs 0
cts 27
cp 0
crap 182
rs 6.6166
c 0
b 0
f 0

How to fix   Long Method    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\Ship\Action\AttackTrackedShip;
6
7
use request;
8
use Stu\Exception\SanityCheckException;
9
use Stu\Module\Control\ActionControllerInterface;
10
use Stu\Module\Control\GameControllerInterface;
11
use Stu\Module\Message\Lib\PrivateMessageFolderSpecialEnum;
12
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
13
use Stu\Module\Ship\Lib\Battle\AlertRedHelperInterface;
14
use Stu\Module\Ship\Lib\Battle\FightLibInterface;
15
use Stu\Module\Ship\Lib\Battle\ShipAttackCycleInterface;
16
use Stu\Module\Ship\Lib\Interaction\InteractionCheckerInterface;
17
use Stu\Module\Ship\Lib\ShipLoaderInterface;
18
use Stu\Module\Ship\View\ShowShip\ShowShip;
19
20
final class AttackTrackedShip implements ActionControllerInterface
21
{
22
    public const ACTION_IDENTIFIER = 'B_ATTACK_TRACKED';
23
24
    private ShipLoaderInterface $shipLoader;
25
26
    private PrivateMessageSenderInterface $privateMessageSender;
27
28
    private ShipAttackCycleInterface $shipAttackCycle;
29
30
    private InteractionCheckerInterface $interactionChecker;
31
32
    private AlertRedHelperInterface $alertRedHelper;
33
34
    private FightLibInterface $fightLib;
35
36
    public function __construct(
37
        ShipLoaderInterface $shipLoader,
38
        PrivateMessageSenderInterface $privateMessageSender,
39
        ShipAttackCycleInterface $shipAttackCycle,
40
        InteractionCheckerInterface $interactionChecker,
41
        AlertRedHelperInterface $alertRedHelper,
42
        FightLibInterface $fightLib
43
    ) {
44
        $this->shipLoader = $shipLoader;
45
        $this->privateMessageSender = $privateMessageSender;
46
        $this->shipAttackCycle = $shipAttackCycle;
47
        $this->interactionChecker = $interactionChecker;
48
        $this->alertRedHelper = $alertRedHelper;
49
        $this->fightLib = $fightLib;
50
    }
51
52
    public function handle(GameControllerInterface $game): void
53
    {
54
        $userId = $game->getUser()->getId();
55
56
        $shipId = request::getIntFatal('id');
57
        $targetId = request::getIntFatal('target');
58
59
        $wrappers = $this->shipLoader->getWrappersBySourceAndUserAndTarget(
60
            $shipId,
61
            $userId,
62
            $targetId
63
        );
64
65
        $wrapper = $wrappers->getSource();
66
        $ship = $wrapper->get();
67
68
        $targetWrapper = $wrappers->getTarget();
69
        if ($targetWrapper === null) {
70
            return;
71
        }
72
        $target = $targetWrapper->get();
73
74
        $tracker = $wrapper->getTrackerSystemData();
75
        if ($tracker === null || $tracker->targetId !== $target->getId()) {
76
            return;
77
        }
78
79
        if (!$ship->hasActiveWeapon()) {
80
            $game->addInformation(_('Waffen sind offline'));
81
            return;
82
        }
83
84
        if ($target->getUser()->isVacationRequestOldEnough()) {
85
            $game->addInformation(_('Aktion nicht möglich, der Spieler befindet sich im Urlaubsmodus!'));
86
            return;
87
        }
88
89
        if (!$ship->hasEnoughCrew($game)) {
90
            return;
91
        }
92
        if (!$this->interactionChecker->checkPosition($target, $ship)) {
93
            throw new SanityCheckException('InteractionChecker->checkPosition failed', self::ACTION_IDENTIFIER);
94
        }
95
96
        $epsSystemData = $wrapper->getEpsSystemData();
97
        if ($epsSystemData === null || $epsSystemData->getEps() === 0) {
98
            $game->addInformation(_('Keine Energie vorhanden'));
99
            return;
100
        }
101
        if ($ship->isDisabled()) {
102
            $game->addInformation(_('Das Schiff ist kampfunfähig'));
103
            return;
104
        }
105
        if ($ship->getDockedTo() !== null) {
106
            $ship->setDockedTo(null);
107
        }
108
109
        $isTargetBase = $target->isBase();
110
        $isShipWarped = $ship->getWarpState();
111
        $isTargetWarped = $target->getWarpState();
112
113
        [$attacker, $defender, $fleet] = $this->fightLib->getAttackersAndDefenders($wrapper, $targetWrapper);
114
115
        $messageCollection = $this->shipAttackCycle->cycle($attacker, $defender);
116
117
        $informations = $messageCollection->getInformationDump();
118
119
        $pm = sprintf(
120
            _("Kampf in Sektor %s\n%s"),
121
            $ship->getSectorString(),
122
            $informations->getInformationsAsString()
123
        );
124
        $this->privateMessageSender->send(
125
            $userId,
126
            $target->getUser()->getId(),
127
            $pm,
128
            $isTargetBase ? PrivateMessageFolderSpecialEnum::PM_SPECIAL_STATION : PrivateMessageFolderSpecialEnum::PM_SPECIAL_SHIP
129
        );
130
131
        //Alarm-Rot check for ship
132
        if ($isShipWarped && !$ship->isDestroyed()) {
133
            $informations->addInformationWrapper($this->alertRedHelper->doItAll($ship));
134
        }
135
136
        //Alarm-Rot check for traktor ship
137
        if ($isTargetWarped && !$target->isDestroyed()) {
138
            $informations->addInformationWrapper($this->alertRedHelper->doItAll($target));
139
        }
140
141
        if ($ship->isDestroyed()) {
142
            $game->addInformationWrapper($informations);
143
            return;
144
        }
145
        $game->setView(ShowShip::VIEW_IDENTIFIER);
146
147
        if ($fleet) {
148
            $game->addInformation(_("Angriff durchgeführt"));
149
            $game->setTemplateVar('FIGHT_RESULTS', $informations->getInformations());
150
        } else {
151
            $game->addInformationWrapper($informations);
152
        }
153
    }
154
155
    public function performSessionCheck(): bool
156
    {
157
        return true;
158
    }
159
}
160