|
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
|
|
|
): int { |
|
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
|
|
|
return $section; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
private function enablePreviewRows( |
|
90
|
|
|
int $xCoordinate, |
|
91
|
|
|
int $yCoordinate, |
|
92
|
|
|
int $minx, |
|
93
|
|
|
int $maxx, |
|
94
|
|
|
int $miny, |
|
95
|
|
|
int $maxy, |
|
96
|
|
|
LayerInterface $layer, |
|
97
|
|
|
GameControllerInterface $game |
|
98
|
|
|
): void { |
|
99
|
|
|
if ($yCoordinate - 1 >= 1) { |
|
100
|
|
|
$game->setTemplateVar( |
|
101
|
|
|
'TOP_PREVIEW_ROW', |
|
102
|
|
|
$this->starmapUiFactory->createYRow($layer, $yCoordinate * MapEnum::FIELDS_PER_SECTION - MapEnum::FIELDS_PER_SECTION, $minx, $maxx, 0)->getFields() |
|
103
|
|
|
); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if ($yCoordinate * MapEnum::FIELDS_PER_SECTION + 1 <= $layer->getHeight()) { |
|
107
|
|
|
$game->setTemplateVar( |
|
108
|
|
|
'BOTTOM_PREVIEW_ROW', |
|
109
|
|
|
$this->starmapUiFactory->createYRow($layer, $yCoordinate * MapEnum::FIELDS_PER_SECTION + 1, $minx, $maxx, 0)->getFields() |
|
110
|
|
|
); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
if ($xCoordinate - 1 >= 1) { |
|
114
|
|
|
$row = []; |
|
115
|
|
|
for ($i = $miny; $i <= $maxy; $i++) { |
|
116
|
|
|
$row[] = $this->starmapUiFactory->createYRow($layer, $i, $minx - 1, $minx - 1, 0); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$game->setTemplateVar( |
|
120
|
|
|
'LEFT_PREVIEW_ROW', |
|
121
|
|
|
$row |
|
122
|
|
|
); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
if ($xCoordinate * MapEnum::FIELDS_PER_SECTION + 1 <= $layer->getWidth()) { |
|
126
|
|
|
$row = []; |
|
127
|
|
|
for ($i = $miny; $i <= $maxy; $i++) { |
|
128
|
|
|
$row[] = $this->starmapUiFactory->createYRow($layer, $i, $maxx + 1, $maxx + 1, 0); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$game->setTemplateVar( |
|
132
|
|
|
'RIGHT_PREVIEW_ROW', |
|
133
|
|
|
$row |
|
134
|
|
|
); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
private function enableNavOptions( |
|
139
|
|
|
int $xCoordinate, |
|
140
|
|
|
int $yCoordinate, |
|
141
|
|
|
LayerInterface $layer, |
|
142
|
|
|
GameControllerInterface $game |
|
143
|
|
|
): void { |
|
144
|
|
|
$layerWidth = $layer->getWidth(); |
|
145
|
|
|
$layerHeight = $layer->getHeight(); |
|
146
|
|
|
|
|
147
|
|
|
$game->addExecuteJS(sprintf( |
|
148
|
|
|
'updateNavButtons(%b, %b, %b, %b);', |
|
149
|
|
|
$xCoordinate > 1, |
|
150
|
|
|
$xCoordinate * MapEnum::FIELDS_PER_SECTION < $layerWidth, |
|
151
|
|
|
$yCoordinate > 1, |
|
152
|
|
|
$yCoordinate * MapEnum::FIELDS_PER_SECTION < $layerHeight |
|
153
|
|
|
), GameEnum::JS_EXECUTION_AJAX_UPDATE); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
private function getSection( |
|
157
|
|
|
int $currentSection, |
|
158
|
|
|
?int $direction, |
|
159
|
|
|
LayerInterface $layer |
|
160
|
|
|
): int { |
|
161
|
|
|
|
|
162
|
|
|
$result = $currentSection; |
|
163
|
|
|
|
|
164
|
|
|
switch ($direction) { |
|
165
|
|
|
case ShipEnum::DIRECTION_LEFT: |
|
166
|
|
|
$result -= 1; |
|
167
|
|
|
break; |
|
168
|
|
|
case ShipEnum::DIRECTION_RIGHT: |
|
169
|
|
|
$result += 1; |
|
170
|
|
|
break; |
|
171
|
|
|
case ShipEnum::DIRECTION_TOP: |
|
172
|
|
|
$result -= $layer->getSectorsHorizontal(); |
|
173
|
|
|
break; |
|
174
|
|
|
case ShipEnum::DIRECTION_BOTTOM: |
|
175
|
|
|
$result += $layer->getSectorsHorizontal(); |
|
176
|
|
|
break; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
if ($result < 1 || $result > $layer->getSectorCount()) { |
|
180
|
|
|
throw new RuntimeException('this should not happen'); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
return $result; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
private function getSectionX(int $sectionId, LayerInterface $layer): int |
|
187
|
|
|
{ |
|
188
|
|
|
$this->loggerUtil->log(sprintf('layerSectorsHorizontal: %d', $layer->getSectorsHorizontal())); |
|
189
|
|
|
|
|
190
|
|
|
$result = $sectionId % $layer->getSectorsHorizontal(); |
|
191
|
|
|
|
|
192
|
|
|
return $result === 0 ? $layer->getSectorsHorizontal() : $result; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
private function getSectionY(int $sectionId, LayerInterface $layer): int |
|
196
|
|
|
{ |
|
197
|
|
|
return (int)ceil($sectionId / $layer->getSectorsHorizontal()); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|