Passed
Pull Request — master (#1672)
by Nico
25:05
created

MapSectionHelper::getSection()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 17
c 0
b 0
f 0
nc 10
nop 3
dl 0
loc 28
ccs 0
cts 13
cp 0
crap 56
rs 8.8333
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
        int $direction = null
36
    ): void {
37
        //$this->loggerUtil->init('MSH', LoggerEnum::LEVEL_ERROR);
38
39
        $section = $this->getSection($currentSection, $direction, $layer);
40
41
        $xCoordinate = $this->getSectionX($section, $layer);
42
        $yCoordinate = $this->getSectionY($section, $layer);
43
44
        $this->loggerUtil->log(sprintf('section: %d, x: %d, y: %d', $section, $xCoordinate, $yCoordinate));
45
46
        $maxx = $xCoordinate * MapEnum::FIELDS_PER_SECTION;
47
        $minx = $maxx - MapEnum::FIELDS_PER_SECTION + 1;
48
        $maxy = $yCoordinate * MapEnum::FIELDS_PER_SECTION;
49
        $miny = $maxy - MapEnum::FIELDS_PER_SECTION + 1;
50
51
        $this->loggerUtil->log(sprintf('minx: %d, maxx: %d, miny: %d, maxy: %d', $minx, $maxx, $miny, $maxy));
52
53
        $fields = [];
54
        foreach (range($miny, $maxy) as $value) {
55
            $fields[] = $this->starmapUiFactory->createUserYRow($game->getUser(), $layer, $value, $minx, $maxx);
56
        }
57
58
        $game->setTemplateVar('SECTION_ID', $section);
59
        $game->setTemplateVar('HEAD_ROW', range($minx, $maxx));
60
        $game->setTemplateVar('MAP_FIELDS', $fields);
61
        $game->addExecuteJS(sprintf(
62
            'updateSectionAndLayer(%d, %d);',
63
            $section,
64
            $layer->getId()
65
        ), GameEnum::JS_EXECUTION_AJAX_UPDATE);
66
67
        $this->enableNavOptions($xCoordinate, $yCoordinate, $layer, $game);
68
    }
69
70
    private function enableNavOptions(
71
        int $xCoordinate,
72
        int $yCoordinate,
73
        LayerInterface $layer,
74
        GameControllerInterface $game
75
    ): void {
76
        $layerWidth = $layer->getWidth();
77
        $layerHeight = $layer->getHeight();
78
79
        $game->addExecuteJS(sprintf(
80
            'updateNavButtonVisibility(%b, %b, %b, %b);',
81
            $xCoordinate > 1,
82
            $xCoordinate * MapEnum::FIELDS_PER_SECTION < $layerWidth,
83
            $yCoordinate > 1,
84
            $yCoordinate * MapEnum::FIELDS_PER_SECTION < $layerHeight
85
        ), GameEnum::JS_EXECUTION_AJAX_UPDATE);
86
    }
87
88
    private function getSection(
89
        int $currentSection,
90
        ?int $direction,
91
        LayerInterface $layer
92
    ): int {
93
94
        $result = $currentSection;
95
96
        switch ($direction) {
97
            case ShipEnum::DIRECTION_LEFT:
98
                $result -= 1;
99
                break;
100
            case ShipEnum::DIRECTION_RIGHT:
101
                $result += 1;
102
                break;
103
            case ShipEnum::DIRECTION_TOP:
104
                $result -= $layer->getSectorsHorizontal();
105
                break;
106
            case ShipEnum::DIRECTION_BOTTOM:
107
                $result += $layer->getSectorsHorizontal();
108
                break;
109
        }
110
111
        if ($result < 1 || $result > $layer->getSectorCount()) {
112
            throw new RuntimeException('this should not happen');
113
        }
114
115
        return $result;
116
    }
117
118
    private function getSectionX(int $sectionId, LayerInterface $layer): int
119
    {
120
        $this->loggerUtil->log(sprintf('layerSectorsHorizontal: %d', $layer->getSectorsHorizontal()));
121
122
        $result = $sectionId % $layer->getSectorsHorizontal();
123
124
        return $result === 0 ? $layer->getSectorsHorizontal() : $result;
125
    }
126
127
    private function getSectionY(int $sectionId, LayerInterface $layer): int
128
    {
129
        return (int)ceil($sectionId / $layer->getSectorsHorizontal());
130
    }
131
}
132