Passed
Push — master ( 9d73f4...fd4f11 )
by Nico
25:31
created

CommodityTransferStrategy::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 5
ccs 0
cts 1
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Lib\Transfer\Strategy;
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 request;
9
use Stu\Lib\Information\InformationWrapper;
10
use Stu\Lib\Pirate\PirateReactionInterface;
11
use Stu\Lib\Pirate\PirateReactionTriggerEnum;
12
use Stu\Lib\Transfer\BeamUtilInterface;
13
use Stu\Module\Colony\Lib\ColonyLibFactoryInterface;
14
use Stu\Module\Control\GameControllerInterface;
15
use Stu\Module\Ship\Lib\ShipWrapperInterface;
16
use Stu\Orm\Entity\ColonyInterface;
17
use Stu\Orm\Entity\ShipInterface;
18
19
class CommodityTransferStrategy implements TransferStrategyInterface
20
{
21
    public function __construct(
22
        private ColonyLibFactoryInterface $colonyLibFactory,
23
        private BeamUtilInterface $beamUtil,
24
        private PirateReactionInterface $pirateReaction
25
    ) {
26
    }
27
28
    #[Override]
29
    public function setTemplateVariables(
30
        bool $isUnload,
31
        ShipInterface $ship,
32
        ShipInterface|ColonyInterface $target,
33
        GameControllerInterface $game
34
    ): void {
35
36
        $game->setTemplateVar(
37
            'BEAMABLE_STORAGE',
38
            $isUnload ? $ship->getBeamableStorage() : $target->getBeamableStorage()
39
        );
40
41
        if ($target instanceof ColonyInterface) {
42
            $game->setTemplateVar(
43
                'SHOW_SHIELD_FREQUENCY',
44
                $this->colonyLibFactory->createColonyShieldingManager($target)->isShieldingEnabled() && $target->getUser() !== $ship->getUser()
45
            );
46
        }
47
    }
48
49
    #[Override]
50
    public function transfer(
51
        bool $isUnload,
52
        ShipWrapperInterface $wrapper,
53
        ShipInterface|ColonyInterface $target,
54
        InformationWrapper $informations
55
    ): void {
56
57
        $commodities = request::postArray('commodities');
58
        $gcount = request::postArray('count');
59
        if (count($commodities) == 0 || count($gcount) == 0) {
60
            $informations->addInformation(_("Es wurden keine Waren zum Beamen ausgewählt"));
61
            return;
62
        }
63
64
        $user = $wrapper->get()->getUser();
65
66
        if (
67
            $target instanceof ColonyInterface
68
            && $target->getUser() !== $user
69
            && $this->colonyLibFactory->createColonyShieldingManager($target)->isShieldingEnabled()
70
            && $target->getShieldFrequency() !== 0
71
        ) {
72
            $frequency = request::postInt('frequency');
73
            if ($frequency !== $target->getShieldFrequency()) {
74
                $informations->addInformation(_("Die Schildfrequenz ist nicht korrekt"));
75
                return;
76
            }
77
        }
78
79
        $hasTransfered = false;
80
81
        // check for fleet option
82
        $fleetWrapper = $wrapper->getFleetWrapper();
83
        if (request::postInt('isfleet') && $fleetWrapper !== null) {
84
            foreach ($fleetWrapper->getShipWrappers() as $wrapper) {
85
                if ($this->transferPerShip(
86
                    $isUnload,
87
                    $wrapper,
88
                    $target,
89
                    $informations
90
                )) {
91
                    $hasTransfered = true;
92
                }
93
            }
94
        } else {
95
            $hasTransfered =  $this->transferPerShip($isUnload, $wrapper, $target, $informations);
96
        }
97
98
        if (
99
            !$isUnload
100
            && $hasTransfered
101
            && $target instanceof ShipInterface
102
        ) {
103
            $this->pirateReaction->checkForPirateReaction(
104
                $target,
105
                PirateReactionTriggerEnum::ON_BEAM,
106
                $wrapper->get()
107
            );
108
        }
109
    }
110
111
    private function transferPerShip(
112
        bool $isUnload,
113
        ShipWrapperInterface $wrapper,
114
        ShipInterface|ColonyInterface $target,
115
        InformationWrapper $informations
116
    ): bool {
117
118
        $ship = $wrapper->get();
119
        $epsSystem = $wrapper->getEpsSystemData();
120
121
        //sanity checks
122
        $isDockTransfer = $this->beamUtil->isDockTransfer($ship, $target);
123
        if (!$isDockTransfer && ($epsSystem === null || $epsSystem->getEps() === 0)) {
124
            $informations->addInformation(_("Keine Energie vorhanden"));
125
            return false;
126
        }
127
        if ($ship->getCloakState()) {
128
            $informations->addInformation(_("Die Tarnung ist aktiviert"));
129
            return false;
130
        }
131
        if ($ship->isWarped()) {
132
            $informations->addInformation("Schiff befindet sich im Warp");
133
            return false;
134
        }
135
        if ($target instanceof ShipInterface && $target->isWarped()) {
136
            $informations->addInformation(sprintf(_('Die %s befindet sich im Warp'), $target->getName()));
137
            return false;
138
        }
139
140
        $transferTarget = $isUnload ? $target : $ship;
141
        if ($transferTarget->getMaxStorage() <= $transferTarget->getStorageSum()) {
142
            $informations->addInformation(sprintf(_('%s: Der Lagerraum ist voll'), $transferTarget->getName()));
143
            return false;
144
        }
145
146
        $commodities = request::postArray('commodities');
147
        $gcount = request::postArray('count');
148
149
        $storage = $isUnload ? $ship->getStorage() : $target->getStorage();
150
151
        if ($storage->isEmpty()) {
152
            $informations->addInformation(_("Keine Waren zum Beamen vorhanden"));
153
            return false;
154
        }
155
        if (count($commodities) == 0 || count($gcount) == 0) {
156
            $informations->addInformation(_("Es wurden keine Waren zum Beamen ausgewählt"));
157
            return false;
158
        }
159
        $informations->addInformation(sprintf(
160
            _('Die %s hat folgende Waren %s %s %s transferiert'),
161
            $ship->getName(),
162
            $isUnload ? 'zur' : 'von der',
163
            $target instanceof ColonyInterface ? 'Kolonie' : '',
164
            $target->getName()
165
        ));
166
167
        $hasTransfered = false;
168
        foreach ($commodities as $key => $value) {
169
            $commodityId = (int) $value;
170
171
            if (!array_key_exists($key, $gcount)) {
172
                continue;
173
            }
174
175
            if ($this->beamUtil->transferCommodity(
176
                $commodityId,
177
                $gcount[$key],
178
                $wrapper,
179
                $isUnload ? $ship : $target,
180
                $transferTarget,
181
                $informations
182
            )) {
183
                $hasTransfered = true;
184
            }
185
        }
186
187
        return $hasTransfered;
188
    }
189
}
190