|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Stu\Module\Game\Lib\View\Provider; |
|
6
|
|
|
|
|
7
|
|
|
use request; |
|
8
|
|
|
use Stu\Component\Map\MapEnum; |
|
9
|
|
|
use Stu\Exception\SanityCheckException; |
|
10
|
|
|
use Stu\Module\Control\GameControllerInterface; |
|
11
|
|
|
use Stu\Module\Game\Lib\View\Provider\ViewComponentProviderInterface; |
|
12
|
|
|
use Stu\Orm\Repository\LayerRepositoryInterface; |
|
13
|
|
|
|
|
14
|
|
|
final class MapProvider implements ViewComponentProviderInterface |
|
15
|
|
|
{ |
|
16
|
|
|
private LayerRepositoryInterface $layerRepository; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(LayerRepositoryInterface $layerRepository) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->layerRepository = $layerRepository; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function setTemplateVariables(GameControllerInterface $game): void |
|
24
|
|
|
{ |
|
25
|
|
|
//only layers, that are known by user |
|
26
|
|
|
$layers = $this->layerRepository->getKnownByUser($game->getUser()->getId()); |
|
27
|
|
|
|
|
28
|
|
|
if ($layers === []) { |
|
29
|
|
|
return; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
$layerId = request::getInt('layerid'); |
|
33
|
|
|
if ($layerId === 0) { |
|
34
|
|
|
$layer = current($layers); |
|
35
|
|
|
} else { |
|
36
|
|
|
if (!array_key_exists($layerId, $layers)) { |
|
37
|
|
|
throw new SanityCheckException('user tried to access unknown layer'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$layer = $layers[$layerId]; |
|
41
|
|
|
} |
|
42
|
|
|
$game->setTemplateVar('LAYERID', $layer->getId()); |
|
43
|
|
|
|
|
44
|
|
|
//HEADROW |
|
45
|
|
|
$xHeadRow = []; |
|
46
|
|
|
for ($j = 1; $j <= (int)ceil($layer->getWidth() / MapEnum::FIELDS_PER_SECTION); $j++) { |
|
47
|
|
|
$xHeadRow[] = $j; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
//SECTIONS |
|
51
|
|
|
$sections = []; |
|
52
|
|
|
$k = 1; |
|
53
|
|
|
for ($i = 1; $i <= (int)ceil($layer->getHeight() / MapEnum::FIELDS_PER_SECTION); $i++) { |
|
54
|
|
|
for ($j = 1; $j <= (int)ceil($layer->getWidth() / MapEnum::FIELDS_PER_SECTION); $j++) { |
|
55
|
|
|
$sections[$i][$j] = $k; |
|
56
|
|
|
$k++; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$game->setTemplateVar('LAYERS', $layers); |
|
61
|
|
|
$game->setTemplateVar('X_HEAD_ROW', $xHeadRow); |
|
62
|
|
|
$game->setTemplateVar('SECTIONS', $sections); |
|
63
|
|
|
$game->setTemplateVar('FIELDS_PER_SECTION', MapEnum::FIELDS_PER_SECTION); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|