Passed
Push — master ( 69410e...6e6431 )
by Nico
42:43 queued 11:08
created

BeamToColony::performSessionCheck()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Action\BeamToColony;
6
7
use request;
8
use Stu\Lib\BeamUtil\BeamUtilInterface;
9
use Stu\Module\Colony\Lib\ColonyLibFactoryInterface;
10
use Stu\Module\Colony\View\ShowColony\ShowColony;
11
use Stu\Module\Control\ActionControllerInterface;
12
use Stu\Module\Control\GameControllerInterface;
13
use Stu\Module\Message\Lib\PrivateMessageFolderSpecialEnum;
14
use Stu\Module\Ship\Lib\Interaction\InteractionChecker;
15
use Stu\Module\Ship\Lib\ShipLoaderInterface;
16
use Stu\Module\Ship\Lib\ShipWrapperInterface;
17
use Stu\Module\Ship\View\ShowShip\ShowShip;
18
use Stu\Orm\Entity\ColonyInterface;
19
use Stu\Orm\Repository\ColonyRepositoryInterface;
20
21
final class BeamToColony implements ActionControllerInterface
22
{
23
    public const ACTION_IDENTIFIER = 'B_BEAMTO_COLONY';
24
25
    private ShipLoaderInterface $shipLoader;
26
27
    private BeamUtilInterface $beamUtil;
28
29
    private ColonyRepositoryInterface $colonyRepository;
30
31
    private ColonyLibFactoryInterface $colonyLibFactory;
32
33
    public function __construct(
34
        ShipLoaderInterface $shipLoader,
35
        BeamUtilInterface $beamUtil,
36
        ColonyRepositoryInterface $colonyRepository,
37
        ColonyLibFactoryInterface $colonyLibFactory
38
    ) {
39
        $this->shipLoader = $shipLoader;
40
        $this->beamUtil = $beamUtil;
41
        $this->colonyRepository = $colonyRepository;
42
        $this->colonyLibFactory = $colonyLibFactory;
43
    }
44
45
    public function handle(GameControllerInterface $game): void
46
    {
47
        $game->setView(ShowShip::VIEW_IDENTIFIER);
48
49
        $userId = $game->getUser()->getId();
50
51
        $wrapper = $this->shipLoader->getWrapperByIdAndUser(
52
            request::indInt('id'),
53
            $userId
54
        );
55
        $ship = $wrapper->get();
56
57
        $colony = $this->colonyRepository->find(request::postIntFatal('target'));
58
        if ($colony === null || !InteractionChecker::canInteractWith($ship, $colony, $game)) {
59
            return;
60
        }
61
62
        $commodities = request::postArray('commodities');
63
        $gcount = request::postArray('count');
64
        if (count($commodities) == 0 || count($gcount) == 0) {
65
            $game->addInformation(_("Es wurden keine Waren zum Beamen ausgewählt"));
66
            return;
67
        }
68
69
        if ($colony->getUserId() !== $userId && $this->colonyLibFactory->createColonyShieldingManager($colony)->isShieldingEnabled() && $colony->getShieldFrequency() !== 0) {
70
            $frequency = request::postInt('frequency');
71
            if ($frequency !== $colony->getShieldFrequency()) {
72
                $game->addInformation(_("Die Schildfrequenz ist nicht korrekt"));
73
                return;
74
            }
75
        }
76
77
        // check for fleet option
78
        $fleetWrapper = $wrapper->getFleetWrapper();
79
        if (request::postInt('isfleet') && $fleetWrapper !== null) {
80
            foreach ($fleetWrapper->getShipWrappers() as $wrapper) {
81
                $this->beamToTarget(
82
                    $wrapper,
83
                    $colony,
84
                    $game
85
                );
86
            }
87
        } else {
88
            $this->beamToTarget($wrapper, $colony, $game);
89
        }
90
    }
91
92
    private function beamToTarget(ShipWrapperInterface $wrapper, ColonyInterface $colony, GameControllerInterface $game): void
93
    {
94
        $ship = $wrapper->get();
95
        $epsSystem = $wrapper->getEpsSystemData();
96
97
        if (!$ship->hasEnoughCrew($game)) {
98
            return;
99
        }
100
        if ($epsSystem === null || $epsSystem->getEps() == 0) {
101
            $game->addInformation(_("Keine Energie vorhanden"));
102
            return;
103
        }
104
        if ($ship->getCloakState()) {
105
            $game->addInformation(_("Die Tarnung ist aktiviert"));
106
            return;
107
        }
108
        if ($ship->getShieldState()) {
109
            $game->addInformation(_('Die Schilde sind aktiviert'));
110
            return;
111
        }
112
        if ($ship->getWarpState()) {
113
            $game->addInformation(_("Der Warpantrieb ist aktiviert"));
114
            return;
115
        }
116
117
        if (!$colony->storagePlaceLeft()) {
118
            $game->addInformation(sprintf(_('Der Lagerraum der Kolonie %s ist voll'), $colony->getName()));
119
            return;
120
        }
121
        $commodities = request::postArray('commodities');
122
        $gcount = request::postArray('count');
123
124
        $shipStorage = $ship->getStorage();
125
126
        if ($shipStorage->isEmpty()) {
127
            $game->addInformation(_("Keine Waren zum Beamen vorhanden"));
128
            return;
129
        }
130
        $game->addInformation(sprintf(
131
            _('Die %s hat folgende Waren zur Kolonie %s transferiert'),
132
            $ship->getName(),
133
            $colony->getName()
134
        ));
135
        foreach ($commodities as $key => $value) {
136
            $commodityId = (int) $value;
137
138
            if (!array_key_exists($key, $gcount)) {
139
                continue;
140
            }
141
142
            $this->beamUtil->transferCommodity(
143
                $commodityId,
144
                $gcount[$key],
145
                $wrapper,
146
                $wrapper->get(),
147
                $colony,
148
                $game
149
            );
150
        }
151
152
        $game->sendInformation(
153
            $colony->getUser()->getId(),
154
            $ship->getUser()->getId(),
155
            PrivateMessageFolderSpecialEnum::PM_SPECIAL_TRADE,
156
            sprintf(_('colony.php?%s=1&id=%d'), ShowColony::VIEW_IDENTIFIER, $colony->getId())
157
        );
158
    }
159
160
    public function performSessionCheck(): bool
161
    {
162
        return true;
163
    }
164
}
165