1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Stu\Module\Starmap\Lib; |
6
|
|
|
|
7
|
|
|
use RuntimeException; |
8
|
|
|
use Stu\Component\Game\GameEnum; |
9
|
|
|
use Stu\Component\Map\MapEnum; |
10
|
|
|
use Stu\Component\Ship\ShipEnum; |
11
|
|
|
use Stu\Module\Control\GameControllerInterface; |
12
|
|
|
use Stu\Module\Logging\LoggerEnum; |
13
|
|
|
use Stu\Module\Logging\LoggerUtilFactoryInterface; |
14
|
|
|
use Stu\Module\Logging\LoggerUtilInterface; |
15
|
|
|
use Stu\Orm\Entity\LayerInterface; |
16
|
|
|
|
17
|
|
|
final class MapSectionHelper |
18
|
|
|
{ |
19
|
|
|
private StarmapUiFactoryInterface $starmapUiFactory; |
20
|
|
|
|
21
|
|
|
private LoggerUtilInterface $loggerUtil; |
22
|
|
|
|
23
|
1 |
|
public function __construct( |
24
|
|
|
StarmapUiFactoryInterface $starmapUiFactory, |
25
|
|
|
LoggerUtilFactoryInterface $loggerUtilFactory |
26
|
|
|
) { |
27
|
1 |
|
$this->starmapUiFactory = $starmapUiFactory; |
28
|
1 |
|
$this->loggerUtil = $loggerUtilFactory->getLoggerUtil(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function setTemplateVars( |
32
|
|
|
GameControllerInterface $game, |
33
|
|
|
LayerInterface $layer, |
34
|
|
|
int $currentSection, |
35
|
|
|
bool $isMapEditor = false, |
36
|
|
|
int $direction = null |
37
|
|
|
): void { |
38
|
|
|
//$this->loggerUtil->init('MSH', LoggerEnum::LEVEL_ERROR); |
39
|
|
|
|
40
|
|
|
$section = $this->getSection($currentSection, $direction, $layer); |
41
|
|
|
|
42
|
|
|
$xCoordinate = $this->getSectionX($section, $layer); |
43
|
|
|
$yCoordinate = $this->getSectionY($section, $layer); |
44
|
|
|
|
45
|
|
|
$this->loggerUtil->log(sprintf('section: %d, x: %d, y: %d', $section, $xCoordinate, $yCoordinate)); |
46
|
|
|
|
47
|
|
|
$maxx = $xCoordinate * MapEnum::FIELDS_PER_SECTION; |
48
|
|
|
$minx = $maxx - MapEnum::FIELDS_PER_SECTION + 1; |
49
|
|
|
$maxy = $yCoordinate * MapEnum::FIELDS_PER_SECTION; |
50
|
|
|
$miny = $maxy - MapEnum::FIELDS_PER_SECTION + 1; |
51
|
|
|
|
52
|
|
|
$this->loggerUtil->log(sprintf('minx: %d, maxx: %d, miny: %d, maxy: %d', $minx, $maxx, $miny, $maxy)); |
53
|
|
|
|
54
|
|
|
$fields = []; |
55
|
|
|
foreach (range($miny, $maxy) as $value) { |
56
|
|
|
if ($isMapEditor) { |
57
|
|
|
$fields[] = $this->starmapUiFactory->createYRow($layer, $value, $minx, $maxx, 0); |
58
|
|
|
} else { |
59
|
|
|
$fields[] = $this->starmapUiFactory->createUserYRow($game->getUser(), $layer, $value, $minx, $maxx); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$game->setTemplateVar('HEAD_ROW', range($minx, $maxx)); |
64
|
|
|
$game->setTemplateVar('MAP_FIELDS', $fields); |
65
|
|
|
$game->addExecuteJS(sprintf( |
66
|
|
|
'updateSectionAndLayer(%d, %d);', |
67
|
|
|
$section, |
68
|
|
|
$layer->getId() |
69
|
|
|
), GameEnum::JS_EXECUTION_AJAX_UPDATE); |
70
|
|
|
|
71
|
|
|
$this->enableNavOptions($xCoordinate, $yCoordinate, $layer, $game); |
72
|
|
|
|
73
|
|
|
if ($isMapEditor) { |
74
|
|
|
$this->enablePreviewRows( |
75
|
|
|
$xCoordinate, |
76
|
|
|
$yCoordinate, |
77
|
|
|
$minx, |
78
|
|
|
$maxx, |
79
|
|
|
$miny, |
80
|
|
|
$maxy, |
81
|
|
|
$layer, |
82
|
|
|
$game |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function enablePreviewRows( |
88
|
|
|
int $xCoordinate, |
89
|
|
|
int $yCoordinate, |
90
|
|
|
int $minx, |
91
|
|
|
int $maxx, |
92
|
|
|
int $miny, |
93
|
|
|
int $maxy, |
94
|
|
|
LayerInterface $layer, |
95
|
|
|
GameControllerInterface $game |
96
|
|
|
): void { |
97
|
|
|
if ($yCoordinate - 1 >= 1) { |
98
|
|
|
$game->setTemplateVar( |
99
|
|
|
'TOP_PREVIEW_ROW', |
100
|
|
|
$this->starmapUiFactory->createYRow($layer, $yCoordinate * MapEnum::FIELDS_PER_SECTION - MapEnum::FIELDS_PER_SECTION, $minx, $maxx, 0)->getFields() |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if ($yCoordinate * MapEnum::FIELDS_PER_SECTION + 1 <= $layer->getHeight()) { |
105
|
|
|
$game->setTemplateVar( |
106
|
|
|
'BOTTOM_PREVIEW_ROW', |
107
|
|
|
$this->starmapUiFactory->createYRow($layer, $yCoordinate * MapEnum::FIELDS_PER_SECTION + 1, $minx, $maxx, 0)->getFields() |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if ($xCoordinate - 1 >= 1) { |
112
|
|
|
$row = []; |
113
|
|
|
for ($i = $miny; $i <= $maxy; $i++) { |
114
|
|
|
$row[] = $this->starmapUiFactory->createYRow($layer, $i, $minx - 1, $minx - 1, 0); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$game->setTemplateVar( |
118
|
|
|
'LEFT_PREVIEW_ROW', |
119
|
|
|
$row |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if ($xCoordinate * MapEnum::FIELDS_PER_SECTION + 1 <= $layer->getWidth()) { |
124
|
|
|
$row = []; |
125
|
|
|
for ($i = $miny; $i <= $maxy; $i++) { |
126
|
|
|
$row[] = $this->starmapUiFactory->createYRow($layer, $i, $maxx + 1, $maxx + 1, 0); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$game->setTemplateVar( |
130
|
|
|
'RIGHT_PREVIEW_ROW', |
131
|
|
|
$row |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
private function enableNavOptions( |
137
|
|
|
int $xCoordinate, |
138
|
|
|
int $yCoordinate, |
139
|
|
|
LayerInterface $layer, |
140
|
|
|
GameControllerInterface $game |
141
|
|
|
): void { |
142
|
|
|
$layerWidth = $layer->getWidth(); |
143
|
|
|
$layerHeight = $layer->getHeight(); |
144
|
|
|
|
145
|
|
|
$game->addExecuteJS(sprintf( |
146
|
|
|
'updateNavButtonVisibility(%b, %b, %b, %b);', |
147
|
|
|
$xCoordinate > 1, |
148
|
|
|
$xCoordinate * MapEnum::FIELDS_PER_SECTION < $layerWidth, |
149
|
|
|
$yCoordinate > 1, |
150
|
|
|
$yCoordinate * MapEnum::FIELDS_PER_SECTION < $layerHeight |
151
|
|
|
), GameEnum::JS_EXECUTION_AJAX_UPDATE); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
private function getSection( |
155
|
|
|
int $currentSection, |
156
|
|
|
?int $direction, |
157
|
|
|
LayerInterface $layer |
158
|
|
|
): int { |
159
|
|
|
|
160
|
|
|
$result = $currentSection; |
161
|
|
|
|
162
|
|
|
switch ($direction) { |
163
|
|
|
case ShipEnum::DIRECTION_LEFT: |
164
|
|
|
$result -= 1; |
165
|
|
|
break; |
166
|
|
|
case ShipEnum::DIRECTION_RIGHT: |
167
|
|
|
$result += 1; |
168
|
|
|
break; |
169
|
|
|
case ShipEnum::DIRECTION_TOP: |
170
|
|
|
$result -= $layer->getSectorsHorizontal(); |
171
|
|
|
break; |
172
|
|
|
case ShipEnum::DIRECTION_BOTTOM: |
173
|
|
|
$result += $layer->getSectorsHorizontal(); |
174
|
|
|
break; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
if ($result < 1 || $result > $layer->getSectorCount()) { |
178
|
|
|
throw new RuntimeException('this should not happen'); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $result; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
private function getSectionX(int $sectionId, LayerInterface $layer): int |
185
|
|
|
{ |
186
|
|
|
$this->loggerUtil->log(sprintf('layerSectorsHorizontal: %d', $layer->getSectorsHorizontal())); |
187
|
|
|
|
188
|
|
|
$result = $sectionId % $layer->getSectorsHorizontal(); |
189
|
|
|
|
190
|
|
|
return $result === 0 ? $layer->getSectorsHorizontal() : $result; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
private function getSectionY(int $sectionId, LayerInterface $layer): int |
194
|
|
|
{ |
195
|
|
|
return (int)ceil($sectionId / $layer->getSectorsHorizontal()); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|