1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Stu\Module\Colony\Lib; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
8
|
|
|
use Stu\Component\Building\BuildingEnum; |
9
|
|
|
use Stu\Component\Colony\ColonyPopulationCalculatorInterface; |
10
|
|
|
use Stu\Lib\Colony\PlanetFieldHostInterface; |
11
|
|
|
use Stu\Lib\ColonyProduction\ColonyProduction; |
12
|
|
|
use Stu\Module\Building\BuildingFunctionTypeEnum; |
13
|
|
|
use Stu\Module\Logging\LoggerUtilInterface; |
14
|
|
|
use Stu\Orm\Entity\ColonyInterface; |
15
|
|
|
use Stu\Orm\Entity\PlanetFieldInterface; |
16
|
|
|
use Stu\Orm\Repository\BuildingRepositoryInterface; |
17
|
|
|
use Stu\Orm\Repository\ColonyRepositoryInterface; |
18
|
|
|
use Stu\Orm\Repository\PlanetFieldRepositoryInterface; |
19
|
|
|
use Stu\Orm\Repository\ResearchedRepositoryInterface; |
20
|
|
|
use Stu\PlanetGenerator\Exception\PlanetGeneratorException; |
21
|
|
|
use Stu\PlanetGenerator\PlanetGeneratorInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Provides access to several colony surface related methods |
25
|
|
|
*/ |
26
|
|
|
final class ColonySurface implements ColonySurfaceInterface |
27
|
|
|
{ |
28
|
|
|
private PlanetFieldRepositoryInterface $planetFieldRepository; |
29
|
|
|
|
30
|
|
|
private BuildingRepositoryInterface $buildingRepository; |
31
|
|
|
|
32
|
|
|
private ColonyRepositoryInterface $colonyRepository; |
33
|
|
|
|
34
|
|
|
private ResearchedRepositoryInterface $researchedRepository; |
35
|
|
|
|
36
|
|
|
private PlanetGeneratorInterface $planetGenerator; |
37
|
|
|
|
38
|
|
|
private EntityManagerInterface $entityManager; |
39
|
|
|
|
40
|
|
|
private PlanetFieldHostInterface $host; |
41
|
|
|
|
42
|
|
|
private ?int $buildingId; |
43
|
|
|
|
44
|
|
|
private bool $showUnderground; |
45
|
|
|
|
46
|
|
|
private PlanetFieldTypeRetrieverInterface $planetFieldTypeRetriever; |
47
|
|
|
|
48
|
|
|
private ColonyLibFactoryInterface $colonyLibFactory; |
49
|
|
|
|
50
|
|
|
private ?ColonyPopulationCalculatorInterface $colonyPopulationCalculator = null; |
51
|
|
|
|
52
|
|
|
/** @var array<ColonyProduction>|null */ |
53
|
|
|
private ?array $production = null; |
54
|
|
|
|
55
|
|
|
public function __construct( |
56
|
|
|
ColonyLibFactoryInterface $colonyLibFactory, |
57
|
|
|
PlanetFieldRepositoryInterface $planetFieldRepository, |
58
|
|
|
BuildingRepositoryInterface $buildingRepository, |
59
|
|
|
ColonyRepositoryInterface $colonyRepository, |
60
|
|
|
ResearchedRepositoryInterface $researchedRepository, |
61
|
|
|
PlanetGeneratorInterface $planetGenerator, |
62
|
|
|
EntityManagerInterface $entityManager, |
63
|
|
|
PlanetFieldTypeRetrieverInterface $planetFieldTypeRetriever, |
64
|
|
|
PlanetFieldHostInterface $host, |
65
|
|
|
?int $buildingId, |
66
|
|
|
bool $showUnderground, |
67
|
|
|
LoggerUtilInterface $loggerUtil |
|
|
|
|
68
|
|
|
) { |
69
|
|
|
$this->colonyLibFactory = $colonyLibFactory; |
70
|
|
|
$this->host = $host; |
71
|
|
|
$this->planetFieldRepository = $planetFieldRepository; |
72
|
|
|
$this->buildingRepository = $buildingRepository; |
73
|
|
|
$this->buildingId = $buildingId; |
74
|
|
|
$this->colonyRepository = $colonyRepository; |
75
|
|
|
$this->researchedRepository = $researchedRepository; |
76
|
|
|
$this->planetGenerator = $planetGenerator; |
77
|
|
|
$this->entityManager = $entityManager; |
78
|
|
|
$this->showUnderground = $showUnderground; |
79
|
|
|
$this->planetFieldTypeRetriever = $planetFieldTypeRetriever; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getSurface(): array |
83
|
|
|
{ |
84
|
|
|
try { |
85
|
|
|
$this->updateSurface(); |
86
|
|
|
} catch (PlanetGeneratorException $e) { |
87
|
|
|
return $this->host->getPlanetFields()->toArray(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$fields = $this->host->getPlanetFields()->toArray(); |
91
|
|
|
|
92
|
|
|
if (!$this->showUnderground) { |
93
|
|
|
$fields = array_filter( |
94
|
|
|
$fields, |
95
|
|
|
fn (PlanetFieldInterface $field): bool => !$this->planetFieldTypeRetriever->isUndergroundField($field) |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ($this->buildingId !== null) { |
100
|
|
|
$building = $this->buildingRepository->find($this->buildingId); |
101
|
|
|
$user = $this->host->getUser(); |
102
|
|
|
|
103
|
|
|
$researchedArray = $this->researchedRepository->getFinishedListByUser($user->getId()); |
104
|
|
|
|
105
|
|
|
array_walk( |
106
|
|
|
$fields, |
107
|
|
|
function (PlanetFieldInterface $field) use ($building, $researchedArray): void { |
108
|
|
|
if ( |
109
|
|
|
$field->getTerraformingId() === null && |
110
|
|
|
$building->getBuildableFields()->containsKey($field->getFieldType()) |
111
|
|
|
) { |
112
|
|
|
//PlanetFieldTypeBuildingInterface |
113
|
|
|
$fieldBuilding = $building->getBuildableFields()->get($field->getFieldType()); |
114
|
|
|
|
115
|
|
|
$researchId = $fieldBuilding->getResearchId(); |
116
|
|
|
if ($researchId == null || $this->isResearched($researchId, $researchedArray)) { |
117
|
|
|
$field->setBuildMode(true); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $fields; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
private function isResearched(int $researchId, array $researched): bool |
128
|
|
|
{ |
129
|
|
|
foreach ($researched as $research) { |
130
|
|
|
if ($research->getResearchId() == $researchId) { |
131
|
|
|
return true; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function getSurfaceTileStyle(): string |
139
|
|
|
{ |
140
|
|
|
$width = $this->planetGenerator->loadColonyClassConfig($this->host->getColonyClass()->getId())['sizew']; |
141
|
|
|
$gridArray = []; |
142
|
|
|
for ($i = 0; $i < $width; $i++) { |
143
|
|
|
$gridArray[] = '43px'; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return sprintf('display: grid; grid-template-columns: %s;', implode(' ', $gridArray)); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function updateSurface(): void |
150
|
|
|
{ |
151
|
|
|
$host = $this->host; |
152
|
|
|
if (!$host instanceof ColonyInterface) { |
153
|
|
|
return; |
154
|
|
|
} |
155
|
|
|
if (!$host->isFree()) { |
156
|
|
|
return; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$mask = $host->getMask(); |
160
|
|
|
|
161
|
|
|
if ($mask === null) { |
162
|
|
|
$planetConfig = $this->planetGenerator->generateColony( |
163
|
|
|
$host->getColonyClassId(), |
164
|
|
|
$host->getSystem()->getBonusFieldAmount() |
165
|
|
|
); |
166
|
|
|
|
167
|
|
|
$mask = base64_encode(serialize($planetConfig->getFieldArray())); |
168
|
|
|
|
169
|
|
|
$host->setMask($mask); |
170
|
|
|
$host->setSurfaceWidth($planetConfig->getSurfaceWidth()); |
171
|
|
|
|
172
|
|
|
$this->colonyRepository->save($host); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$fields = $host->getPlanetFields()->toArray(); |
176
|
|
|
|
177
|
|
|
$surface = unserialize(base64_decode($mask)); |
178
|
|
|
foreach ($surface as $fieldId => $type) { |
179
|
|
|
if (!array_key_exists($fieldId, $fields)) { |
180
|
|
|
$newField = $this->planetFieldRepository->prototype(); |
181
|
|
|
$fields[$fieldId] = $newField; |
182
|
|
|
$fields[$fieldId]->setColony($host); |
183
|
|
|
$fields[$fieldId]->setFieldId($fieldId); |
184
|
|
|
$host->getPlanetFields()->set($fieldId, $newField); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
$fields[$fieldId]->setBuilding(null); |
188
|
|
|
$fields[$fieldId]->setIntegrity(0); |
189
|
|
|
$fields[$fieldId]->setFieldType((int) $type); |
190
|
|
|
$fields[$fieldId]->setActive(0); |
191
|
|
|
|
192
|
|
|
$this->planetFieldRepository->save($fields[$fieldId]); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$this->entityManager->flush(); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function hasShipyard(): bool |
199
|
|
|
{ |
200
|
|
|
return $this->planetFieldRepository->getCountByColonyAndBuildingFunctionAndState( |
201
|
|
|
$this->host, |
202
|
|
|
BuildingFunctionTypeEnum::getShipyardOptions(), |
203
|
|
|
[0, 1] |
204
|
|
|
) > 0; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function hasAirfield(): bool |
208
|
|
|
{ |
209
|
|
|
return $this->planetFieldRepository->getCountByColonyAndBuildingFunctionAndState( |
210
|
|
|
$this->host, |
211
|
|
|
[BuildingEnum::BUILDING_FUNCTION_AIRFIELD], |
212
|
|
|
[0, 1] |
213
|
|
|
) > 0; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function getPopulation(): ColonyPopulationCalculatorInterface |
217
|
|
|
{ |
218
|
|
|
if ($this->colonyPopulationCalculator === null) { |
219
|
|
|
$this->colonyPopulationCalculator = $this->colonyLibFactory->createColonyPopulationCalculator( |
220
|
|
|
$this->host, |
221
|
|
|
$this->getProduction() |
222
|
|
|
); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return $this->colonyPopulationCalculator; |
|
|
|
|
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @return array<ColonyProduction> |
230
|
|
|
*/ |
231
|
|
|
private function getProduction(): array |
232
|
|
|
{ |
233
|
|
|
if ($this->production === null) { |
234
|
|
|
$this->production = $this->colonyLibFactory->createColonyCommodityProduction($this->host)->getProduction(); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return $this->production; |
|
|
|
|
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.