1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Stu\Module\Database\View\DatabaseEntry; |
6
|
|
|
|
7
|
|
|
use Stu\Component\Database\DatabaseEntryTypeEnum; |
8
|
|
|
use Stu\Component\Ship\Crew\ShipCrewCalculatorInterface; |
9
|
|
|
use Stu\Exception\AccessViolation; |
10
|
|
|
use Stu\Lib\Map\VisualPanel\SystemCellData; |
11
|
|
|
use Stu\Module\Control\GameControllerInterface; |
12
|
|
|
use Stu\Module\Control\ViewControllerInterface; |
13
|
|
|
use Stu\Module\Database\View\Category\Category; |
14
|
|
|
use Stu\Orm\Entity\ColonyScanInterface; |
15
|
|
|
use Stu\Orm\Entity\DatabaseEntryInterface; |
16
|
|
|
use Stu\Orm\Entity\StarSystemMapInterface; |
17
|
|
|
use Stu\Orm\Entity\UserInterface; |
18
|
|
|
use Stu\Orm\Repository\ColonyScanRepositoryInterface; |
19
|
|
|
use Stu\Orm\Repository\DatabaseCategoryRepositoryInterface; |
20
|
|
|
use Stu\Orm\Repository\DatabaseEntryRepositoryInterface; |
21
|
|
|
use Stu\Orm\Repository\DatabaseUserRepositoryInterface; |
22
|
|
|
use Stu\Orm\Repository\MapRegionRepositoryInterface; |
23
|
|
|
use Stu\Orm\Repository\ShipRepositoryInterface; |
24
|
|
|
use Stu\Orm\Repository\ShipRumpRepositoryInterface; |
25
|
|
|
use Stu\Orm\Repository\StarSystemRepositoryInterface; |
26
|
|
|
use Stu\PlanetGenerator\PlanetGeneratorInterface; |
27
|
|
|
|
28
|
|
|
final class DatabaseEntry implements ViewControllerInterface |
29
|
|
|
{ |
30
|
|
|
public const VIEW_IDENTIFIER = 'SHOW_ENTRY'; |
31
|
|
|
|
32
|
|
|
private ColonyScanRepositoryInterface $colonyScanRepository; |
33
|
|
|
|
34
|
|
|
private DatabaseEntryRequestInterface $databaseEntryRequest; |
35
|
|
|
|
36
|
|
|
private DatabaseCategoryRepositoryInterface $databaseCategoryRepository; |
37
|
|
|
|
38
|
|
|
private DatabaseEntryRepositoryInterface $databaseEntryRepository; |
39
|
|
|
|
40
|
|
|
private DatabaseUserRepositoryInterface $databaseUserRepository; |
41
|
|
|
|
42
|
|
|
private MapRegionRepositoryInterface $mapRegionRepository; |
43
|
|
|
|
44
|
|
|
private StarSystemRepositoryInterface $starSystemRepository; |
45
|
|
|
|
46
|
|
|
private ShipRumpRepositoryInterface $shipRumpRepository; |
47
|
|
|
|
48
|
|
|
private ShipRepositoryInterface $shipRepository; |
49
|
|
|
|
50
|
|
|
private ShipCrewCalculatorInterface $shipCrewCalculator; |
51
|
|
|
|
52
|
|
|
private PlanetGeneratorInterface $planetGenerator; |
53
|
|
|
|
54
|
|
|
public function __construct( |
55
|
|
|
ColonyScanRepositoryInterface $colonyScanRepository, |
56
|
|
|
DatabaseEntryRequestInterface $databaseEntryRequest, |
57
|
|
|
DatabaseCategoryRepositoryInterface $databaseCategoryRepository, |
58
|
|
|
DatabaseEntryRepositoryInterface $databaseEntryRepository, |
59
|
|
|
DatabaseUserRepositoryInterface $databaseUserRepository, |
60
|
|
|
MapRegionRepositoryInterface $mapRegionRepository, |
61
|
|
|
StarSystemRepositoryInterface $starSystemRepository, |
62
|
|
|
ShipRumpRepositoryInterface $shipRumpRepository, |
63
|
|
|
ShipCrewCalculatorInterface $shipCrewCalculator, |
64
|
|
|
ShipRepositoryInterface $shipRepository, |
65
|
|
|
PlanetGeneratorInterface $planetGenerator |
66
|
|
|
) { |
67
|
|
|
$this->colonyScanRepository = $colonyScanRepository; |
68
|
|
|
$this->databaseEntryRequest = $databaseEntryRequest; |
69
|
|
|
$this->databaseCategoryRepository = $databaseCategoryRepository; |
70
|
|
|
$this->databaseEntryRepository = $databaseEntryRepository; |
71
|
|
|
$this->databaseUserRepository = $databaseUserRepository; |
72
|
|
|
$this->mapRegionRepository = $mapRegionRepository; |
73
|
|
|
$this->starSystemRepository = $starSystemRepository; |
74
|
|
|
$this->shipRumpRepository = $shipRumpRepository; |
75
|
|
|
$this->shipRepository = $shipRepository; |
76
|
|
|
$this->shipCrewCalculator = $shipCrewCalculator; |
77
|
|
|
$this->planetGenerator = $planetGenerator; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function handle(GameControllerInterface $game): void |
81
|
|
|
{ |
82
|
|
|
$userId = $game->getUser()->getId(); |
83
|
|
|
$entryId = $this->databaseEntryRequest->getEntryId(); |
84
|
|
|
$categoryId = $this->databaseEntryRequest->getCategoryId(); |
85
|
|
|
|
86
|
|
|
if (!$this->databaseUserRepository->exists($userId, $entryId)) { |
87
|
|
|
throw new AccessViolation(sprintf(_('userId %d tried to open databaseEntryId %d, but has not discovered it yet!'), $userId, $entryId)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @var DatabaseEntryInterface $entry |
92
|
|
|
*/ |
93
|
|
|
$entry = $this->databaseEntryRepository->find($entryId); |
94
|
|
|
$category = $this->databaseCategoryRepository->find($categoryId); |
95
|
|
|
|
96
|
|
|
$entry_name = $entry->getDescription(); |
97
|
|
|
|
98
|
|
|
$game->appendNavigationPart( |
99
|
|
|
'database.php', |
100
|
|
|
_('Datenbank') |
101
|
|
|
); |
102
|
|
|
$game->appendNavigationPart( |
103
|
|
|
sprintf( |
104
|
|
|
'database.php?%s=1&cat=%d', |
105
|
|
|
Category::VIEW_IDENTIFIER, |
106
|
|
|
$categoryId, |
107
|
|
|
), |
108
|
|
|
$category->getDescription() |
109
|
|
|
); |
110
|
|
|
$game->appendNavigationPart( |
111
|
|
|
sprintf( |
112
|
|
|
'database.php?%s=1&cat=%d&ent=%d', |
113
|
|
|
static::VIEW_IDENTIFIER, |
114
|
|
|
$categoryId, |
115
|
|
|
$entryId, |
116
|
|
|
), |
117
|
|
|
sprintf( |
118
|
|
|
_('Eintrag: %s'), |
119
|
|
|
$entry_name |
120
|
|
|
) |
121
|
|
|
); |
122
|
|
|
$game->setPageTitle(sprintf(_('/ Datenbankeintrag: %s'), $entry_name)); |
123
|
|
|
$game->setTemplateFile('html/databaseentry.xhtml'); |
124
|
|
|
|
125
|
|
|
$this->addSpecialVars($game, $entry); |
126
|
|
|
$game->setTemplateVar('ENTRY', $entry); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
protected function addSpecialVars(GameControllerInterface $game, DatabaseEntryInterface $entry): void |
130
|
|
|
{ |
131
|
|
|
$entry_object_id = $entry->getObjectId(); |
132
|
|
|
|
133
|
|
|
switch ($entry->getTypeObject()->getId()) { |
134
|
|
|
case DatabaseEntryTypeEnum::DATABASE_TYPE_PLANET: |
135
|
|
|
$game->setTemplateVar('FIELD_ARRAY', $this->getPlanetFieldArray($entry_object_id)); |
136
|
|
|
$game->setTemplateVar('SURFACE_TILE_STYLE', $this->getSurfaceTileStyle($entry_object_id)); |
137
|
|
|
break; |
138
|
|
|
case DatabaseEntryTypeEnum::DATABASE_TYPE_POI: |
139
|
|
|
$game->setTemplateVar('POI', $this->shipRepository->find($entry_object_id)); |
140
|
|
|
break; |
141
|
|
|
case DatabaseEntryTypeEnum::DATABASE_TYPE_MAP: |
142
|
|
|
$game->setTemplateVar('REGION', $this->mapRegionRepository->find($entry_object_id)); |
143
|
|
|
break; |
144
|
|
|
case DatabaseEntryTypeEnum::DATABASE_TYPE_RUMP: |
145
|
|
|
$rump = $this->shipRumpRepository->find($entry_object_id); |
146
|
|
|
if ($rump === null) { |
147
|
|
|
return; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$game->setTemplateVar('RUMP', $rump); |
151
|
|
|
$game->setTemplateVar( |
152
|
|
|
'MAX_CREW_COUNT', |
153
|
|
|
$this->shipCrewCalculator->getMaxCrewCountByRump($rump) |
154
|
|
|
); |
155
|
|
|
break; |
156
|
|
|
case DatabaseEntryTypeEnum::DATABASE_TYPE_STARSYSTEM: |
157
|
|
|
$starSystem = $this->starSystemRepository->find($entry_object_id); |
158
|
|
|
$fields = []; |
159
|
|
|
$userHasColonyInSystem = $this->hasUserColonyInSystem($game->getUser(), $entry_object_id); |
160
|
|
|
foreach ($starSystem->getFields() as $obj) { |
161
|
|
|
$fields['fields'][$obj->getSY()][] = [ |
162
|
|
|
'systemCellData' => $this->createSystemCellData($obj), |
163
|
|
|
'colony' => $obj->getColony(), |
164
|
|
|
'showPm' => $userHasColonyInSystem && $this->showPmHref($obj, $game->getUser()) |
165
|
|
|
]; |
166
|
|
|
} |
167
|
|
|
$fields['xaxis'] = range(1, $starSystem->getMaxX()); |
168
|
|
|
$game->setTemplateVar('SYSTEM', $starSystem); |
169
|
|
|
$game->setTemplateVar('FIELDS', $fields); |
170
|
|
|
$game->setTemplateVar('COLONYSCANLIST', $this->getColonyScanList($game->getUser(), $entry_object_id)); |
171
|
|
|
break; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** @return array<int> */ |
176
|
|
|
private function getPlanetFieldArray(int $colonyClassId): array |
177
|
|
|
{ |
178
|
|
|
$planetConfig = $this->planetGenerator->generateColony( |
179
|
|
|
$colonyClassId, |
180
|
|
|
random_int(0, 3) |
181
|
|
|
); |
182
|
|
|
|
183
|
|
|
return $planetConfig->getFieldArray(); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function getSurfaceTileStyle(int $colonyClassId): string |
187
|
|
|
{ |
188
|
|
|
$width = $this->planetGenerator->loadColonyClassConfig($colonyClassId)['sizew']; |
189
|
|
|
$gridArray = []; |
190
|
|
|
for ($i = 0; $i < $width; $i++) { |
191
|
|
|
$gridArray[] = '43px'; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return sprintf('display: grid; grid-template-columns: %s;', implode(' ', $gridArray)); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
private function createSystemCellData(StarSystemMapInterface $systemMap): SystemCellData |
198
|
|
|
{ |
199
|
|
|
return new SystemCellData( |
200
|
|
|
$systemMap->getSx(), |
201
|
|
|
$systemMap->getSy(), |
202
|
|
|
$systemMap->getFieldId(), |
203
|
|
|
false, |
204
|
|
|
null, |
205
|
|
|
null |
206
|
|
|
); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
private function hasUserColonyInSystem(UserInterface $user, int $systemId): bool |
210
|
|
|
{ |
211
|
|
|
foreach ($user->getColonies() as $colony) { |
212
|
|
|
if ($colony->getStarsystemMap()->getSystem()->getId() === $systemId) { |
213
|
|
|
return true; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return false; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
private function showPmHref(StarSystemMapInterface $map, UserInterface $user): bool |
221
|
|
|
{ |
222
|
|
|
return $map->getColony() !== null |
223
|
|
|
&& !$map->getColony()->isFree() |
224
|
|
|
&& $map->getColony()->getUser() !== $user; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @return array<ColonyScanInterface> |
229
|
|
|
*/ |
230
|
|
|
public function getColonyScanList(UserInterface $user, int $systemId): iterable |
231
|
|
|
{ |
232
|
|
|
$scanlist = []; |
233
|
|
|
|
234
|
|
|
foreach ($this->colonyScanRepository->getEntryByUserAndSystem($user->getId(), $systemId) as $element) { |
235
|
|
|
$i = $element->getColony()->getId(); |
236
|
|
|
$scanlist[$i] = $element; |
237
|
|
|
} |
238
|
|
|
return $scanlist; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|