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

BeamTo   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 72
dl 0
loc 121
ccs 0
cts 73
cp 0
rs 10
c 0
b 0
f 0
wmc 16

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A performSessionCheck() 0 3 1
C handle() 0 91 14
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Colony\Action\BeamTo;
6
7
use request;
8
use Stu\Lib\BeamUtil\BeamUtilInterface;
9
use Stu\Module\Colony\Lib\ColonyLoaderInterface;
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\InteractionCheckerInterface;
15
use Stu\Module\Ship\Lib\ShipLoaderInterface;
16
use Stu\Module\Ship\View\ShowShip\ShowShip;
17
18
final class BeamTo implements ActionControllerInterface
19
{
20
    public const ACTION_IDENTIFIER = 'B_BEAMTO';
21
22
    private ColonyLoaderInterface $colonyLoader;
23
24
    private BeamUtilInterface $beamUtil;
25
26
    private ShipLoaderInterface $shipLoader;
27
28
    private InteractionCheckerInterface $interactionChecker;
29
30
    public function __construct(
31
        ColonyLoaderInterface $colonyLoader,
32
        BeamUtilInterface $beamUtil,
33
        ShipLoaderInterface $shipLoader,
34
        InteractionCheckerInterface $interactionChecker
35
    ) {
36
        $this->colonyLoader = $colonyLoader;
37
        $this->beamUtil = $beamUtil;
38
        $this->shipLoader = $shipLoader;
39
        $this->interactionChecker = $interactionChecker;
40
    }
41
42
    public function handle(GameControllerInterface $game): void
43
    {
44
        $game->setView(ShowColony::VIEW_IDENTIFIER);
45
46
        $userId = $game->getUser()->getId();
47
48
        $colony = $this->colonyLoader->byIdAndUser(
49
            request::indInt('id'),
50
            $userId
51
        );
52
53
        if ($colony->getEps() == 0) {
54
            $game->addInformation(_('Keine Energie vorhanden'));
55
            return;
56
        }
57
        $wrapper = $this->shipLoader->find(request::postIntFatal('target'));
58
59
        if ($wrapper === null) {
60
            return;
61
        }
62
63
        $ship = $wrapper->get();
64
65
        if (!$this->interactionChecker->checkColonyPosition($colony, $ship)) {
66
            return;
67
        }
68
69
        if ($ship->getUser()->isVacationRequestOldEnough()) {
70
            $game->addInformation(_('Aktion nicht möglich, der Spieler befindet sich im Urlaubsmodus!'));
71
            return;
72
        }
73
74
        if ($ship->getShieldState() && $ship->getUser()->getId() !== $userId) {
75
            $game->addInformationf(_('Die %s hat die Schilde aktiviert'), $ship->getName());
76
            return;
77
        }
78
        if ($ship->getMaxStorage() <= $ship->getStorageSum()) {
79
            $game->addInformationf(_('Der Lagerraum der %s ist voll'), $ship->getName());
80
            return;
81
        }
82
        $commodities = request::postArray('commodities');
83
        $gcount = request::postArray('count');
84
        $storages = $colony->getStorage();
85
        if ($storages->isEmpty()) {
86
            $game->addInformation(_('Keine Waren zum Beamen vorhanden'));
87
            return;
88
        }
89
        if (count($commodities) == 0 || count($gcount) == 0) {
90
            $game->addInformation(_('Es wurden keine Waren zum Beamen ausgewählt'));
91
            return;
92
        }
93
94
        $isOwnedByCurrentUser = $game->getUser() === $ship->getUser();
95
        if ($isOwnedByCurrentUser) {
96
            $link = sprintf("ship.php?%s=1&id=%d", ShowShip::VIEW_IDENTIFIER, $ship->getId());
97
98
            $game->addInformationfWithLink(
99
                _('Die Kolonie %s hat folgende Waren zur %s transferiert'),
100
                $link,
101
                $colony->getName(),
102
                $ship->getName()
103
            );
104
        } else {
105
            $game->addInformationf(
106
                _('Die Kolonie %s hat folgende Waren zur %s transferiert'),
107
                $colony->getName(),
108
                $ship->getName()
109
            );
110
        }
111
112
        foreach ($commodities as $key => $value) {
113
            $commodityId = (int) $value;
114
            if (!array_key_exists($key, $gcount)) {
115
                continue;
116
            }
117
118
            $this->beamUtil->transferCommodity(
119
                $commodityId,
120
                $gcount[$key],
121
                $colony,
122
                $colony,
123
                $ship,
124
                $game
125
            );
126
        }
127
128
        $game->sendInformation(
129
            $ship->getUser()->getId(),
130
            $userId,
131
            PrivateMessageFolderSpecialEnum::PM_SPECIAL_TRADE,
132
            sprintf('ship.php?%s=1&id=%d', ShowShip::VIEW_IDENTIFIER, $ship->getId())
133
        );
134
    }
135
136
    public function performSessionCheck(): bool
137
    {
138
        return true;
139
    }
140
}
141