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
|
|
|
|
27
|
|
|
final class DatabaseEntry implements ViewControllerInterface |
28
|
|
|
{ |
29
|
|
|
public const VIEW_IDENTIFIER = 'SHOW_ENTRY'; |
30
|
|
|
|
31
|
|
|
private ColonyScanRepositoryInterface $colonyScanRepository; |
32
|
|
|
|
33
|
|
|
private DatabaseEntryRequestInterface $databaseEntryRequest; |
34
|
|
|
|
35
|
|
|
private DatabaseCategoryRepositoryInterface $databaseCategoryRepository; |
36
|
|
|
|
37
|
|
|
private DatabaseEntryRepositoryInterface $databaseEntryRepository; |
38
|
|
|
|
39
|
|
|
private DatabaseUserRepositoryInterface $databaseUserRepository; |
40
|
|
|
|
41
|
|
|
private MapRegionRepositoryInterface $mapRegionRepository; |
42
|
|
|
|
43
|
|
|
private StarSystemRepositoryInterface $starSystemRepository; |
44
|
|
|
|
45
|
|
|
private ShipRumpRepositoryInterface $shipRumpRepository; |
46
|
|
|
|
47
|
|
|
private ShipRepositoryInterface $shipRepository; |
48
|
|
|
|
49
|
|
|
private ShipCrewCalculatorInterface $shipCrewCalculator; |
50
|
|
|
|
51
|
|
|
public function __construct( |
52
|
|
|
ColonyScanRepositoryInterface $colonyScanRepository, |
53
|
|
|
DatabaseEntryRequestInterface $databaseEntryRequest, |
54
|
|
|
DatabaseCategoryRepositoryInterface $databaseCategoryRepository, |
55
|
|
|
DatabaseEntryRepositoryInterface $databaseEntryRepository, |
56
|
|
|
DatabaseUserRepositoryInterface $databaseUserRepository, |
57
|
|
|
MapRegionRepositoryInterface $mapRegionRepository, |
58
|
|
|
StarSystemRepositoryInterface $starSystemRepository, |
59
|
|
|
ShipRumpRepositoryInterface $shipRumpRepository, |
60
|
|
|
ShipCrewCalculatorInterface $shipCrewCalculator, |
61
|
|
|
ShipRepositoryInterface $shipRepository |
62
|
|
|
) { |
63
|
|
|
$this->colonyScanRepository = $colonyScanRepository; |
64
|
|
|
$this->databaseEntryRequest = $databaseEntryRequest; |
65
|
|
|
$this->databaseCategoryRepository = $databaseCategoryRepository; |
66
|
|
|
$this->databaseEntryRepository = $databaseEntryRepository; |
67
|
|
|
$this->databaseUserRepository = $databaseUserRepository; |
68
|
|
|
$this->mapRegionRepository = $mapRegionRepository; |
69
|
|
|
$this->starSystemRepository = $starSystemRepository; |
70
|
|
|
$this->shipRumpRepository = $shipRumpRepository; |
71
|
|
|
$this->shipRepository = $shipRepository; |
72
|
|
|
$this->shipCrewCalculator = $shipCrewCalculator; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function handle(GameControllerInterface $game): void |
76
|
|
|
{ |
77
|
|
|
$userId = $game->getUser()->getId(); |
78
|
|
|
$entryId = $this->databaseEntryRequest->getEntryId(); |
79
|
|
|
$categoryId = $this->databaseEntryRequest->getCategoryId(); |
80
|
|
|
|
81
|
|
|
if (!$this->databaseUserRepository->exists($userId, $entryId)) { |
82
|
|
|
throw new AccessViolation(sprintf(_('userId %d tried to open databaseEntryId %d, but has not discovered it yet!'), $userId, $entryId)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @var DatabaseEntryInterface $entry |
87
|
|
|
*/ |
88
|
|
|
$entry = $this->databaseEntryRepository->find($entryId); |
89
|
|
|
$category = $this->databaseCategoryRepository->find($categoryId); |
90
|
|
|
|
91
|
|
|
$entry_name = $entry->getDescription(); |
92
|
|
|
|
93
|
|
|
$game->appendNavigationPart( |
94
|
|
|
'database.php', |
95
|
|
|
_('Datenbank') |
96
|
|
|
); |
97
|
|
|
$game->appendNavigationPart( |
98
|
|
|
sprintf( |
99
|
|
|
'database.php?%s=1&cat=%d', |
100
|
|
|
Category::VIEW_IDENTIFIER, |
101
|
|
|
$categoryId, |
102
|
|
|
), |
103
|
|
|
$category->getDescription() |
104
|
|
|
); |
105
|
|
|
$game->appendNavigationPart( |
106
|
|
|
sprintf( |
107
|
|
|
'database.php?%s=1&cat=%d&ent=%d', |
108
|
|
|
static::VIEW_IDENTIFIER, |
109
|
|
|
$categoryId, |
110
|
|
|
$entryId, |
111
|
|
|
), |
112
|
|
|
sprintf( |
113
|
|
|
_('Eintrag: %s'), |
114
|
|
|
$entry_name |
115
|
|
|
) |
116
|
|
|
); |
117
|
|
|
$game->setPageTitle(sprintf(_('/ Datenbankeintrag: %s'), $entry_name)); |
118
|
|
|
$game->setTemplateFile('html/databaseentry.xhtml'); |
119
|
|
|
|
120
|
|
|
$this->addSpecialVars($game, $entry); |
121
|
|
|
$game->setTemplateVar('ENTRY', $entry); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
protected function addSpecialVars(GameControllerInterface $game, DatabaseEntryInterface $entry): void |
125
|
|
|
{ |
126
|
|
|
$entry_object_id = $entry->getObjectId(); |
127
|
|
|
|
128
|
|
|
switch ($entry->getTypeObject()->getId()) { |
129
|
|
|
case DatabaseEntryTypeEnum::DATABASE_TYPE_POI: |
130
|
|
|
$game->setTemplateVar('POI', $this->shipRepository->find($entry_object_id)); |
131
|
|
|
break; |
132
|
|
|
case DatabaseEntryTypeEnum::DATABASE_TYPE_MAP: |
133
|
|
|
$game->setTemplateVar('REGION', $this->mapRegionRepository->find($entry_object_id)); |
134
|
|
|
break; |
135
|
|
|
case DatabaseEntryTypeEnum::DATABASE_TYPE_RUMP: |
136
|
|
|
$rump = $this->shipRumpRepository->find($entry_object_id); |
137
|
|
|
if ($rump === null) { |
138
|
|
|
return; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$game->setTemplateVar('RUMP', $rump); |
142
|
|
|
$game->setTemplateVar( |
143
|
|
|
'MAX_CREW_COUNT', |
144
|
|
|
$this->shipCrewCalculator->getMaxCrewCountByRump($rump) |
145
|
|
|
); |
146
|
|
|
break; |
147
|
|
|
case DatabaseEntryTypeEnum::DATABASE_TYPE_STARSYSTEM: |
148
|
|
|
$starSystem = $this->starSystemRepository->find($entry_object_id); |
149
|
|
|
$fields = []; |
150
|
|
|
$userHasColonyInSystem = $this->hasUserColonyInSystem($game->getUser(), $entry_object_id); |
151
|
|
|
foreach ($starSystem->getFields() as $obj) { |
152
|
|
|
$fields['fields'][$obj->getSY()][] = [ |
153
|
|
|
'systemCellData' => $this->createSystemCellData($obj), |
154
|
|
|
'colony' => $obj->getColony(), |
155
|
|
|
'showPm' => $userHasColonyInSystem && $this->showPmHref($obj, $game->getUser()) |
156
|
|
|
]; |
157
|
|
|
} |
158
|
|
|
$fields['xaxis'] = range(1, $starSystem->getMaxX()); |
159
|
|
|
$game->setTemplateVar('SYSTEM', $starSystem); |
160
|
|
|
$game->setTemplateVar('FIELDS', $fields); |
161
|
|
|
$game->setTemplateVar('COLONYSCANLIST', $this->getColonyScanList($game->getUser(), $entry_object_id)); |
162
|
|
|
break; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
private function createSystemCellData(StarSystemMapInterface $systemMap): SystemCellData |
167
|
|
|
{ |
168
|
|
|
return new SystemCellData( |
169
|
|
|
$systemMap->getSx(), |
170
|
|
|
$systemMap->getSy(), |
171
|
|
|
$systemMap->getFieldId(), |
172
|
|
|
false, |
173
|
|
|
null, |
174
|
|
|
null |
175
|
|
|
); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
private function hasUserColonyInSystem(UserInterface $user, int $systemId): bool |
179
|
|
|
{ |
180
|
|
|
foreach ($user->getColonies() as $colony) { |
181
|
|
|
if ($colony->getStarsystemMap()->getSystem()->getId() === $systemId) { |
182
|
|
|
return true; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return false; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
private function showPmHref(StarSystemMapInterface $map, UserInterface $user): bool |
190
|
|
|
{ |
191
|
|
|
return $map->getColony() !== null |
192
|
|
|
&& !$map->getColony()->isFree() |
193
|
|
|
&& $map->getColony()->getUser() !== $user; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @return array<ColonyScanInterface> |
198
|
|
|
*/ |
199
|
|
|
public function getColonyScanList(UserInterface $user, int $systemId): iterable |
200
|
|
|
{ |
201
|
|
|
$scanlist = []; |
202
|
|
|
|
203
|
|
|
foreach ($this->colonyScanRepository->getEntryByUserAndSystem($user->getId(), $systemId) as $element) { |
204
|
|
|
$i = $element->getColony()->getId(); |
205
|
|
|
$scanlist[$i] = $element; |
206
|
|
|
} |
207
|
|
|
return $scanlist; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|