|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Stu\Component\Spacecraft\System; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
8
|
|
|
use Doctrine\Common\Collections\Collection; |
|
9
|
|
|
use Override; |
|
|
|
|
|
|
10
|
|
|
use Stu\Component\Spacecraft\System\Exception\ActivationConditionsNotMetException; |
|
11
|
|
|
use Stu\Component\Spacecraft\System\Exception\AlreadyActiveException; |
|
12
|
|
|
use Stu\Component\Spacecraft\System\Exception\AlreadyOffException; |
|
13
|
|
|
use Stu\Component\Spacecraft\System\Exception\DeactivationConditionsNotMetException; |
|
14
|
|
|
use Stu\Component\Spacecraft\System\Exception\InsufficientCrewException; |
|
15
|
|
|
use Stu\Component\Spacecraft\System\Exception\InsufficientEnergyException; |
|
16
|
|
|
use Stu\Component\Spacecraft\System\Exception\InvalidSystemException; |
|
17
|
|
|
use Stu\Component\Spacecraft\System\Exception\SpacecraftSystemException; |
|
18
|
|
|
use Stu\Component\Spacecraft\System\Exception\SystemCooldownException; |
|
19
|
|
|
use Stu\Component\Spacecraft\System\Exception\SystemDamagedException; |
|
20
|
|
|
use Stu\Component\Spacecraft\System\Exception\SystemNotActivatableException; |
|
21
|
|
|
use Stu\Component\Spacecraft\System\Exception\SystemNotDeactivatableException; |
|
22
|
|
|
use Stu\Component\Spacecraft\System\Exception\SystemNotFoundException; |
|
23
|
|
|
use Stu\Module\Control\StuTime; |
|
24
|
|
|
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface; |
|
25
|
|
|
use Stu\Orm\Entity\SpacecraftSystem; |
|
26
|
|
|
use Stu\Orm\Entity\Spacecraft; |
|
27
|
|
|
|
|
28
|
|
|
final class SpacecraftSystemManager implements SpacecraftSystemManagerInterface |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @param array<SpacecraftSystemTypeInterface> $systemList |
|
32
|
|
|
*/ |
|
33
|
20 |
|
public function __construct(private array $systemList, private StuTime $stuTime) {} |
|
34
|
|
|
|
|
35
|
26 |
|
#[Override] |
|
36
|
|
|
public function activate( |
|
37
|
|
|
SpacecraftWrapperInterface $wrapper, |
|
38
|
|
|
SpacecraftSystemTypeEnum $type, |
|
39
|
|
|
bool $force = false, |
|
40
|
|
|
bool $isDryRun = false |
|
41
|
|
|
): void { |
|
42
|
26 |
|
$time = $this->stuTime->time(); |
|
43
|
26 |
|
$system = $this->lookupSystem($type); |
|
44
|
|
|
|
|
45
|
26 |
|
if (!$force) { |
|
46
|
26 |
|
$this->checkActivationConditions($wrapper, $system, $type, $time); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
12 |
|
if ($isDryRun) { |
|
50
|
5 |
|
return; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
11 |
|
$epsSystem = $wrapper->getEpsSystemData(); |
|
54
|
11 |
|
if ($epsSystem !== null) { |
|
55
|
11 |
|
$epsSystem->lowerEps($system->getEnergyUsageForActivation())->update(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
11 |
|
$this->setCooldown($wrapper, $type, $time, $system); |
|
59
|
|
|
|
|
60
|
11 |
|
$system->activate($wrapper, $this); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
11 |
|
private function setCooldown( |
|
64
|
|
|
SpacecraftWrapperInterface $wrapper, |
|
65
|
|
|
SpacecraftSystemTypeEnum $type, |
|
66
|
|
|
int $time, |
|
67
|
|
|
SpacecraftSystemTypeInterface $system |
|
68
|
|
|
): void { |
|
69
|
11 |
|
$shipSystem = $wrapper->get()->getSystems()[$type->value] ?? null; |
|
70
|
11 |
|
if ($shipSystem !== null && $system->getCooldownSeconds() !== null) { |
|
71
|
1 |
|
$shipSystem->setCooldown($time + $system->getCooldownSeconds()); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
18 |
|
#[Override] |
|
76
|
|
|
public function deactivate(SpacecraftWrapperInterface $wrapper, SpacecraftSystemTypeEnum $type, bool $force = false): void |
|
77
|
|
|
{ |
|
78
|
18 |
|
$system = $this->lookupSystem($type); |
|
79
|
|
|
|
|
80
|
17 |
|
if (!$force) { |
|
81
|
12 |
|
$this->checkDeactivationConditions($wrapper, $system, $type); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
9 |
|
$system->deactivate($wrapper); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
3 |
|
#[Override] |
|
88
|
|
|
public function deactivateAll(SpacecraftWrapperInterface $wrapper): void |
|
89
|
|
|
{ |
|
90
|
3 |
|
foreach ($wrapper->get()->getSystems() as $shipSystem) { |
|
91
|
|
|
try { |
|
92
|
3 |
|
$this->deactivate($wrapper, $shipSystem->getSystemType(), true); |
|
93
|
1 |
|
} catch (SpacecraftSystemException) { |
|
94
|
1 |
|
continue; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
4 |
|
#[Override] |
|
100
|
|
|
public function getEnergyUsageForActivation(SpacecraftSystemTypeEnum $type): int |
|
101
|
|
|
{ |
|
102
|
4 |
|
$system = $this->lookupSystem($type); |
|
103
|
|
|
|
|
104
|
4 |
|
return $system->getEnergyUsageForActivation(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
16 |
|
#[Override] |
|
108
|
|
|
public function getEnergyConsumption(SpacecraftSystemTypeEnum $type): int |
|
109
|
|
|
{ |
|
110
|
16 |
|
$system = $this->lookupSystem($type); |
|
111
|
|
|
|
|
112
|
16 |
|
return $system->getEnergyConsumption(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
49 |
|
#[Override] |
|
116
|
|
|
public function lookupSystem(SpacecraftSystemTypeEnum $type): SpacecraftSystemTypeInterface |
|
117
|
|
|
{ |
|
118
|
49 |
|
$system = $this->systemList[$type->value] ?? null; |
|
119
|
49 |
|
if ($system === null) { |
|
120
|
1 |
|
throw new InvalidSystemException(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
48 |
|
return $system; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
26 |
|
private function checkActivationConditions( |
|
127
|
|
|
SpacecraftWrapperInterface $wrapper, |
|
128
|
|
|
SpacecraftSystemTypeInterface $system, |
|
129
|
|
|
SpacecraftSystemTypeEnum $type, |
|
130
|
|
|
int $time |
|
131
|
|
|
): void { |
|
132
|
26 |
|
$ship = $wrapper->get(); |
|
133
|
26 |
|
$shipSystem = $ship->getSystems()[$type->value] ?? null; |
|
134
|
26 |
|
if ($shipSystem === null) { |
|
135
|
7 |
|
throw new SystemNotFoundException(); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
23 |
|
if ($shipSystem->getStatus() === 0) { |
|
139
|
1 |
|
throw new SystemDamagedException(); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
22 |
|
$mode = $shipSystem->getMode(); |
|
143
|
22 |
|
if ($mode === SpacecraftSystemModeEnum::MODE_ALWAYS_OFF) { |
|
144
|
1 |
|
throw new SystemNotActivatableException(); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
21 |
|
if ($mode->isActivated()) { |
|
148
|
6 |
|
throw new AlreadyActiveException(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
17 |
|
if (!$ship->hasEnoughCrew() && !$system->canBeActivatedWithInsufficientCrew()) { |
|
152
|
2 |
|
throw new InsufficientCrewException(); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
15 |
|
$epsSystem = $wrapper->getEpsSystemData(); |
|
156
|
15 |
|
if ($epsSystem === null || $epsSystem->getEps() < $system->getEnergyUsageForActivation()) { |
|
157
|
1 |
|
throw new InsufficientEnergyException($system->getEnergyUsageForActivation()); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
14 |
|
$cooldown = $shipSystem->getCooldown(); |
|
161
|
14 |
|
if ($cooldown !== null && $cooldown > $time) { |
|
162
|
1 |
|
throw new SystemCooldownException($cooldown - $time); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
13 |
|
$reason = ''; |
|
166
|
13 |
|
if (!$system->checkActivationConditions($wrapper, $reason)) { |
|
167
|
1 |
|
throw new ActivationConditionsNotMetException($reason); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
12 |
|
private function checkDeactivationConditions( |
|
172
|
|
|
SpacecraftWrapperInterface $wrapper, |
|
173
|
|
|
SpacecraftSystemTypeInterface $system, |
|
174
|
|
|
SpacecraftSystemTypeEnum $type |
|
175
|
|
|
): void { |
|
176
|
12 |
|
$ship = $wrapper->get(); |
|
177
|
12 |
|
$shipSystem = $ship->getSystems()[$type->value] ?? null; |
|
178
|
12 |
|
if ($shipSystem === null) { |
|
179
|
2 |
|
throw new SystemNotFoundException(); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
11 |
|
$mode = $shipSystem->getMode(); |
|
183
|
11 |
|
if ($mode === SpacecraftSystemModeEnum::MODE_ALWAYS_ON) { |
|
184
|
1 |
|
throw new SystemNotDeactivatableException(); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
if ( |
|
188
|
10 |
|
$mode === SpacecraftSystemModeEnum::MODE_OFF |
|
189
|
10 |
|
|| $mode === SpacecraftSystemModeEnum::MODE_ALWAYS_OFF |
|
190
|
|
|
) { |
|
191
|
6 |
|
throw new AlreadyOffException(); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
5 |
|
$reason = ''; |
|
195
|
5 |
|
if (!$system->checkDeactivationConditions($wrapper, $reason)) { |
|
196
|
1 |
|
throw new DeactivationConditionsNotMetException($reason); |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
1 |
|
#[Override] |
|
201
|
|
|
public function handleDestroyedSystem(SpacecraftWrapperInterface $wrapper, SpacecraftSystemTypeEnum $type): void |
|
202
|
|
|
{ |
|
203
|
1 |
|
$system = $this->lookupSystem($type); |
|
204
|
|
|
|
|
205
|
1 |
|
$system->handleDestruction($wrapper); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
1 |
|
#[Override] |
|
209
|
|
|
public function handleDamagedSystem(SpacecraftWrapperInterface $wrapper, SpacecraftSystemTypeEnum $type): void |
|
210
|
|
|
{ |
|
211
|
1 |
|
$system = $this->lookupSystem($type); |
|
212
|
|
|
|
|
213
|
1 |
|
$system->handleDamage($wrapper); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
17 |
|
#[Override] |
|
217
|
|
|
public function getActiveSystems(Spacecraft $ship, bool $sort = false): Collection |
|
218
|
|
|
{ |
|
219
|
17 |
|
$activeSystems = []; |
|
220
|
17 |
|
$prioArray = []; |
|
221
|
17 |
|
foreach ($ship->getSystems() as $system) { |
|
222
|
17 |
|
if ($system->getMode()->isActivated()) { |
|
223
|
17 |
|
$activeSystems[] = $system; |
|
224
|
17 |
|
if ($sort) { |
|
225
|
|
|
$prioArray[$system->getSystemType()->value] = $system->getSystemType()->getPriority(); |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
17 |
|
if ($sort) { |
|
231
|
|
|
usort( |
|
232
|
|
|
$activeSystems, |
|
233
|
|
|
fn(SpacecraftSystem $a, SpacecraftSystem $b): int => $prioArray[$a->getSystemType()->value] <=> $prioArray[$b->getSystemType()->value] |
|
234
|
|
|
); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
17 |
|
return new ArrayCollection($activeSystems); |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths