Test Failed
Push — master ( e50059...70b57f )
by Nico
22:13 queued 12:55
created

SelfDestruct::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 7
dl 0
loc 16
ccs 0
cts 12
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Action\SelfDestruct;
6
7
use request;
8
use Stu\Module\Control\ActionControllerInterface;
9
use Stu\Module\Control\GameControllerInterface;
10
use Stu\Module\History\Lib\EntryCreatorInterface;
11
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
12
use Stu\Module\PlayerSetting\Lib\UserEnum;
13
use Stu\Module\Prestige\Lib\CreatePrestigeLogInterface;
14
use Stu\Module\Ship\Lib\Battle\AlertRedHelperInterface;
15
use Stu\Module\Ship\Lib\ShipLoaderInterface;
16
use Stu\Module\Ship\Lib\ShipRemoverInterface;
17
use Stu\Module\Ship\Lib\ShipRumpSpecialAbilityEnum;
18
use Stu\Module\Ship\View\Overview\Overview;
19
use Stu\Module\Ship\View\ShowShip\ShowShip;
20
use Stu\Orm\Entity\UserInterface;
21
use Stu\Orm\Repository\ShipRepositoryInterface;
22
23
final class SelfDestruct implements ActionControllerInterface
24
{
25
    public const ACTION_IDENTIFIER = 'B_SELFDESTRUCT';
26
27
    private ShipLoaderInterface $shipLoader;
28
29
    private EntryCreatorInterface $entryCreator;
30
31
    private ShipRemoverInterface $shipRemover;
32
33
    private ShipRepositoryInterface $shipRepository;
34
35
    private AlertRedHelperInterface $alertRedHelper;
36
37
    private CreatePrestigeLogInterface $createPrestigeLog;
38
39
    private PrivateMessageSenderInterface $privateMessageSender;
40
41
    public function __construct(
42
        ShipLoaderInterface $shipLoader,
43
        EntryCreatorInterface $entryCreator,
44
        ShipRemoverInterface $shipRemover,
45
        ShipRepositoryInterface $shipRepository,
46
        AlertRedHelperInterface $alertRedHelper,
47
        CreatePrestigeLogInterface $createPrestigeLog,
48
        PrivateMessageSenderInterface $privateMessageSender
49
    ) {
50
        $this->shipLoader = $shipLoader;
51
        $this->entryCreator = $entryCreator;
52
        $this->shipRemover = $shipRemover;
53
        $this->shipRepository = $shipRepository;
54
        $this->alertRedHelper = $alertRedHelper;
55
        $this->createPrestigeLog = $createPrestigeLog;
56
        $this->privateMessageSender = $privateMessageSender;
57
    }
58
59
    public function handle(GameControllerInterface $game): void
60
    {
61
        $userId = $game->getUser()->getId();
62
        $user = $game->getUser();
63
64
        $wrapper = $this->shipLoader->getWrapperByIdAndUser(
65
            request::indInt('id'),
66
            $userId
67
        );
68
        $ship = $wrapper->get();
69
70
        if ($ship->isConstruction()) {
71
            return;
72
        }
73
74
        if (!$ship->hasEnoughCrew($game)) {
75
            return;
76
        }
77
78
        $code = request::postString('destructioncode');
79
        if ($code === false) {
0 ignored issues
show
introduced by
The condition $code === false is always true.
Loading history...
80
            return;
81
        }
82
83
        $trimmedCode = trim($code);
84
        if ($trimmedCode !== substr(md5($ship->getName()), 0, 6)) {
85
            $game->addInformation(_('Der Selbstzerstörungscode war fehlerhaft'));
86
            $game->setView(ShowShip::VIEW_IDENTIFIER);
87
            return;
88
        }
89
90
        $game->setView(Overview::VIEW_IDENTIFIER);
91
92
        $tractoredShipToTriggerAlertRed = ($ship->isTractoring() && $ship->getWarpState()) ? $ship->getTractoredShip() : null;
93
94
        $game->addInformation(_('Die Selbstzerstörung war erfolgreich'));
95
        $msg = sprintf(
96
            _('Die %s (%s) hat sich in Sektor %s selbst zerstört'),
97
            $ship->getName(),
98
            $ship->getRump()->getName(),
99
            $ship->getSectorString()
100
        );
101
        if ($ship->isBase()) {
102
            $this->entryCreator->addStationEntry(
103
                $msg,
104
                $userId
105
            );
106
        } else {
107
            $this->entryCreator->addShipEntry(
108
                $msg,
109
                $userId
110
            );
111
        }
112
113
        $prestigeAmount = $ship->getRump()->getPrestige();
114
        $rumpName = $ship->getRump()->getName();
115
116
        $destroyMsg = $this->shipRemover->destroy($wrapper);
117
        if ($destroyMsg !== null) {
118
            $game->addInformation($destroyMsg);
119
        }
120
121
        //Alarm-Rot check for tractor ship
122
        if ($tractoredShipToTriggerAlertRed !== null) {
123
            $this->alertRedHelper->doItAll($tractoredShipToTriggerAlertRed, $game);
124
        }
125
126
        if ($user->getState() == UserEnum::USER_STATE_COLONIZATION_SHIP && $this->shipRepository->getAmountByUserAndSpecialAbility($userId, ShipRumpSpecialAbilityEnum::COLONIZE) === 1) {
127
            $user->setState(UserEnum::USER_STATE_UNCOLONIZED);
128
        }
129
130
        $this->createNegativePrestigeLog($prestigeAmount, $rumpName, $user);
131
    }
132
133
    private function createNegativePrestigeLog(int $prestigeAmount, string $rumpName, UserInterface $user): void
134
    {
135
        $amount = -(int)abs($prestigeAmount);
136
137
        $description = sprintf(
138
            '[b][color=red]%d[/color][/b] Prestige erhalten für die Selbstzerstörung von: %s',
139
            $amount,
140
            $rumpName
141
        );
142
143
        $this->createPrestigeLog->createLog($amount, $description, $user, time());
144
        $this->sendSystemMessage($description, $user);
145
    }
146
147
    private function sendSystemMessage(string $description, UserInterface $user): void
148
    {
149
        $this->privateMessageSender->send(
150
            UserEnum::USER_NOONE,
151
            $user->getId(),
152
            $description
153
        );
154
    }
155
156
    public function performSessionCheck(): bool
157
    {
158
        return true;
159
    }
160
}
161