Passed
Push — master ( f544cb...b3a3d9 )
by Nico
36:43 queued 09:10
created

ColonyGuiHelper::getColonyMenu()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 17
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 15
nc 7
nop 1
dl 0
loc 17
ccs 0
cts 14
cp 0
crap 56
rs 8.8333
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A ColonyGuiHelper::__construct() 0 10 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Colony\Lib;
6
7
use request;
8
use Stu\Component\Colony\Shields\ColonyShieldingManagerInterface;
9
use Stu\Lib\Colony\PlanetFieldHostInterface;
10
use Stu\Lib\ColonyProduction\ColonyProduction;
11
use Stu\Module\Colony\Lib\Gui\GuiComponentEnum;
12
use Stu\Module\Commodity\CommodityTypeEnum;
13
use Stu\Module\Commodity\Lib\CommodityCacheInterface;
14
use Stu\Module\Control\GameControllerInterface;
15
use Stu\Module\Tal\StatusBarColorEnum;
16
use Stu\Orm\Entity\ColonyInterface;
17
use Stu\Orm\Entity\ColonySandboxInterface;
18
use Stu\Orm\Repository\CommodityRepositoryInterface;
19
use Stu\Orm\Repository\PlanetFieldRepositoryInterface;
20
21
final class ColonyGuiHelper implements ColonyGuiHelperInterface
22
{
23
    private PlanetFieldRepositoryInterface $planetFieldRepository;
24
25
    private CommodityRepositoryInterface $commodityRepository;
26
27
    private ColonyLibFactoryInterface $colonyLibFactory;
28
29
    private CommodityCacheInterface $commodityCache;
30
31
    /** @var array<int, ColonyProduction> */
32
    private ?array $production = null;
33
34
    private ?ColonyShieldingManagerInterface $shieldingManager = null;
35
36
    public function __construct(
37
        PlanetFieldRepositoryInterface $planetFieldRepository,
38
        CommodityRepositoryInterface $commodityRepository,
39
        ColonyLibFactoryInterface $colonyLibFactory,
40
        CommodityCacheInterface $commodityCache
41
    ) {
42
        $this->planetFieldRepository = $planetFieldRepository;
43
        $this->commodityRepository = $commodityRepository;
44
        $this->colonyLibFactory = $colonyLibFactory;
45
        $this->commodityCache = $commodityCache;
46
    }
47
48
    public function registerComponents(
49
        PlanetFieldHostInterface $host,
50
        GameControllerInterface $game,
51
        array $whitelist = null
52
    ): void {
53
54
        $components = $whitelist ?? GuiComponentEnum::cases();
55
        foreach ($components as $component) {
56
            $method = $component->value;
57
58
            $this->$method($host, $game);
59
        }
60
61
        $game->setTemplateVar('HOST', $host);
62
63
        if ($host instanceof ColonyInterface) {
64
            $game->setTemplateVar('COLONY', $host);
65
            $game->setTemplateVar('FORM_ACTION', 'colony.php');
66
        }
67
        if ($host instanceof ColonySandboxInterface) {
68
            $game->setTemplateVar('COLONY', $host->getColony());
69
            $game->setTemplateVar('FORM_ACTION', '/admin/index.php');
70
        }
71
    }
72
73
    /** @return array<int, ColonyProduction> */
74
    private function getProduction(PlanetFieldHostInterface $host): array
75
    {
76
        if ($this->production === null) {
77
            $this->production = $this->colonyLibFactory->createColonyCommodityProduction($host)->getProduction();
78
        }
79
80
        return $this->production;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->production could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
81
    }
82
83
    private function registerSurface(PlanetFieldHostInterface $host, GameControllerInterface $game): void
84
    {
85
        $game->setTemplateVar(
86
            'COLONY_SURFACE',
87
            $this->colonyLibFactory->createColonySurface($host, request::getInt('bid') !== 0 ? request::getInt('bid') : null)
88
        );
89
    }
90
91
    private function registerEffects(PlanetFieldHostInterface $host, GameControllerInterface $game): void
92
    {
93
        $commodities = $this->commodityCache->getAll(CommodityTypeEnum::COMMODITY_TYPE_EFFECT);
94
        $depositMinings = $host instanceof ColonyInterface ? $host->getUserDepositMinings() : [];
95
        $prod = $this->getProduction($host);
96
97
        $effects = [];
98
        foreach ($commodities as $value) {
99
            $commodityId = $value->getId();
100
101
            //skip deposit effects on asteroid
102
            if (array_key_exists($commodityId, $depositMinings)) {
103
                continue;
104
            }
105
106
            if (!array_key_exists($commodityId, $prod) || $prod[$commodityId]->getProduction() == 0) {
107
                continue;
108
            }
109
            $effects[$commodityId]['commodity'] = $value;
110
            $effects[$commodityId]['production'] = $prod[$commodityId];
111
        }
112
113
        $game->setTemplateVar('EFFECTS', $effects);
114
    }
115
116
    private function registerStorage(PlanetFieldHostInterface $host, GameControllerInterface $game): void
117
    {
118
        $commodities = $this->commodityCache->getAll(CommodityTypeEnum::COMMODITY_TYPE_STANDARD);
119
120
        $prod = $this->getProduction($host);
121
        $game->setTemplateVar(
122
            'PRODUCTION_SUM',
123
            $this->colonyLibFactory->createColonyProductionSumReducer()->reduce($prod)
124
        );
125
126
        if (!$host instanceof ColonyInterface) {
127
            return;
128
        }
129
130
        $stor = $host->getStorage();
131
        $storage = [];
132
        foreach ($commodities as $value) {
133
            $commodityId = $value->getId();
134
            if (array_key_exists($commodityId, $prod)) {
135
                $storage[$commodityId]['commodity'] = $value;
136
                $storage[$commodityId]['production'] = $prod[$commodityId];
137
                $storage[$commodityId]['storage'] = $stor->containsKey($commodityId) ? $stor[$commodityId] : false;
138
            } elseif ($stor->containsKey($commodityId)) {
139
                $storage[$commodityId]['commodity'] = $value;
140
                $storage[$commodityId]['storage'] = $stor[$commodityId];
141
                $storage[$commodityId]['production'] = false;
142
            }
143
        }
144
145
        $game->setTemplateVar('STORAGE', $storage);
146
    }
147
148
    private function registerShieldingManager(PlanetFieldHostInterface $host, GameControllerInterface $game): void
149
    {
150
        $game->setTemplateVar('SHIELDING_MANAGER', $this->getShieldingManager($host));
151
    }
152
153
    private function registerShieldBar(PlanetFieldHostInterface $host, GameControllerInterface $game): void
154
    {
155
        $shieldingManager = $this->getShieldingManager($host);
156
157
        if ($shieldingManager->hasShielding()) {
158
            $game->setTemplateVar(
159
                'SHIELD_STATUS_BAR',
160
                $this->buildShieldBar($shieldingManager, $host)
161
            );
162
        }
163
    }
164
165
    private function registerBuildingManagement(PlanetFieldHostInterface $host, GameControllerInterface $game): void
166
    {
167
        $list = $this->planetFieldRepository->getByColonyWithBuilding($host);
168
169
        $game->setTemplateVar('PLANET_FIELD_LIST', $list);
170
        $game->setTemplateVar('USEABLE_COMMODITY_LIST', $this->commodityRepository->getByBuildingsOnColony($host));
171
    }
172
173
    private function getShieldingManager(PlanetFieldHostInterface $host): ColonyShieldingManagerInterface
174
    {
175
        if ($this->shieldingManager === null) {
176
            $this->shieldingManager = $this->colonyLibFactory->createColonyShieldingManager($host);
177
        }
178
179
        return $this->shieldingManager;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->shieldingManager could return the type null which is incompatible with the type-hinted return Stu\Component\Colony\Shi...ieldingManagerInterface. Consider adding an additional type-check to rule them out.
Loading history...
180
    }
181
182
    private function registerEpsBar(PlanetFieldHostInterface $host, GameControllerInterface $game): void
183
    {
184
        $energyProduction = $this->planetFieldRepository->getEnergyProductionByColony($host);
185
186
        $currentEps = $host instanceof ColonyInterface ? $host->getEps() : 0;
187
        $width = 360;
188
        $bars = [];
189
        $epsBar = '';
190
        if ($energyProduction < 0) {
191
            $prod = abs($energyProduction);
192
            if ($currentEps - $prod < 0) {
193
                $bars[StatusBarColorEnum::STATUSBAR_RED] = $currentEps;
194
                $bars[StatusBarColorEnum::STATUSBAR_GREY] = $host->getMaxEps() - $currentEps;
195
            } else {
196
                $bars[StatusBarColorEnum::STATUSBAR_YELLOW] = $currentEps - $prod;
197
                $bars[StatusBarColorEnum::STATUSBAR_RED] = $prod;
198
                $bars[StatusBarColorEnum::STATUSBAR_GREY] = $host->getMaxEps() - $currentEps;
199
            }
200
        }
201
        if ($energyProduction > 0) {
202
            if ($currentEps + $energyProduction > $host->getMaxEps()) {
203
                $bars[StatusBarColorEnum::STATUSBAR_YELLOW] = $currentEps;
204
                if ($currentEps < $host->getMaxEps()) {
205
                    $bars[StatusBarColorEnum::STATUSBAR_GREEN] = $host->getMaxEps() - $currentEps;
206
                }
207
            } else {
208
                $bars[StatusBarColorEnum::STATUSBAR_YELLOW] = $currentEps;
209
                $bars[StatusBarColorEnum::STATUSBAR_GREEN] = $energyProduction;
210
                $bars[StatusBarColorEnum::STATUSBAR_GREY] = $host->getMaxEps() - $currentEps - $energyProduction;
211
            }
212
        }
213
        if ($energyProduction == 0) {
214
            $bars[StatusBarColorEnum::STATUSBAR_YELLOW] = $currentEps;
215
            $bars[StatusBarColorEnum::STATUSBAR_GREY] = $host->getMaxEps() - $currentEps;
216
        }
217
        foreach ($bars as $color => $value) {
218
            if ($host->getMaxEps() < $value) {
219
                $value = $host->getMaxEps();
220
            }
221
            if ($value <= 0) {
222
                continue;
223
            }
224
            $epsBar .= sprintf(
225
                '<img src="/assets/bars/balken.png" style="background-color: #%s;height: 12px; width: %dpx;" title="%s" />',
226
                $color,
227
                round($width / 100 * (100 / $host->getMaxEps() * $value)),
228
                'Energieproduktion'
229
            );
230
        }
231
232
        $game->setTemplateVar(
233
            'EPS_STATUS_BAR',
234
            $epsBar
235
        );
236
    }
237
238
    private function buildShieldBar(
239
        ColonyShieldingManagerInterface $colonyShieldingManager,
240
        PlanetFieldHostInterface $host
241
    ): string {
242
        $shieldBar = '';
243
        $bars = [];
244
        $width = 360;
245
246
        $currentShields = $host instanceof ColonyInterface ? $host->getShields() : 0;
247
        $maxShields = $colonyShieldingManager->getMaxShielding();
248
249
        if ($colonyShieldingManager->isShieldingEnabled()) {
250
            $bars[StatusBarColorEnum::STATUSBAR_SHIELD_ON] = $currentShields;
251
        } else {
252
            $bars[StatusBarColorEnum::STATUSBAR_SHIELD_OFF] = $currentShields;
253
        }
254
        $bars[StatusBarColorEnum::STATUSBAR_GREY] = $maxShields - $currentShields;
255
256
        foreach ($bars as $color => $value) {
257
            if ($maxShields < $value) {
258
                $value = $maxShields;
259
            }
260
            if ($value <= 0) {
261
                continue;
262
            }
263
            $shieldBar .= sprintf(
264
                '<img src="/assets/bars/balken.png" style="background-color: #%s;height: 12px; width: %dpx;" title="%s" />',
265
                $color,
266
                round($width / 100 * (100 / $maxShields * $value)),
267
                'Schildstärke'
268
            );
269
        }
270
271
        return $shieldBar;
272
    }
273
}
274