Passed
Pull Request — master (#1846)
by Nico
32:16
created

VisualNavPanel::getParentMapLocation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Lib\Ui;
6
7
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use RuntimeException;
9
use Stu\Component\Ship\ShipRumpEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Ship\ShipRumpEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Stu\Lib\Map\VisualPanel\AbstractVisualPanel;
11
use Stu\Lib\Map\VisualPanel\Layer\DataProvider\Shipcount\ShipcountLayerTypeEnum;
12
use Stu\Lib\Map\VisualPanel\Layer\DataProvider\Subspace\SubspaceLayerTypeEnum;
13
use Stu\Lib\Map\VisualPanel\Layer\PanelLayerCreationInterface;
14
use Stu\Lib\Map\VisualPanel\PanelBoundaries;
15
use Stu\Lib\Map\VisualPanel\VisualNavPanelEntry;
16
use Stu\Module\Logging\LoggerUtilInterface;
17
use Stu\Orm\Entity\LayerInterface;
18
use Stu\Orm\Entity\LocationInterface;
19
use Stu\Orm\Entity\MapInterface;
20
use Stu\Orm\Entity\ShipInterface;
21
use Stu\Orm\Entity\StarSystemMapInterface;
22
use Stu\Orm\Entity\UserInterface;
23
use Stu\Orm\Repository\UserMapRepositoryInterface;
24
25
class VisualNavPanel extends AbstractVisualPanel
26
{
27
    private MapInterface|StarSystemMapInterface|null $panelCenter = null;
28
29
    private ?bool $isOnShipLevel = null;
30
31 1
    public function __construct(
32
        PanelLayerCreationInterface $panelLayerCreation,
33
        private UserMapRepositoryInterface $userMapRepository,
34
        private ShipInterface $currentShip,
35
        private UserInterface $user,
36
        LoggerUtilInterface $loggerUtil,
37
        private bool $tachyonFresh
38
    ) {
39 1
        parent::__construct($panelLayerCreation, $loggerUtil);
40
    }
41
42
    #[Override]
43
    protected function createBoundaries(): PanelBoundaries
44
    {
45
        return PanelBoundaries::fromLocation($this->getPanelCenter(), $this->currentShip->getSensorRange());
46
    }
47
48
    #[Override]
49
    protected function loadLayers(): void
50
    {
51
52
        $panelLayerCreation = $this->panelLayerCreation
53
            ->addShipCountLayer($this->tachyonFresh, $this->currentShip, ShipcountLayerTypeEnum::ALL, 0)
54
            ->addBorderLayer($this->currentShip, $this->isOnShipLevel());
55
56
        $map = $this->getPanelCenter();
57
58
        if ($map instanceof MapInterface) {
59
            $layer = $map->getLayer();
60
            if ($layer === null) {
61
                throw new RuntimeException('this should not happen');
62
            }
63
            $panelLayerCreation->addMapLayer($layer);
64
            $this->createUserMapEntries($layer);
65
        } else {
66
            $panelLayerCreation
67
                ->addSystemLayer()
68
                ->addColonyShieldLayer();
69
        }
70
71
        if ($this->currentShip->getSubspaceState()) {
72
            $panelLayerCreation->addSubspaceLayer($this->user->getId(), SubspaceLayerTypeEnum::IGNORE_USER);
73
        }
74
75
        $this->layers = $panelLayerCreation->build($this);
76
    }
77
78
    #[Override]
79
    protected function getEntryCallable(): callable
80
    {
81
        return fn (int $x, int $y): VisualNavPanelEntry => new VisualNavPanelEntry(
82
            $x,
83
            $y,
84
            $this->isOnShipLevel(),
85
            $this->layers,
86
            $this->currentShip
87
        );
88
    }
89
90
    #[Override]
91
    protected function getPanelViewportPercentage(): int
92
    {
93
        return $this->currentShip->isBase() ? 50 : 33;
94
    }
95
96
    private function isOnShipLevel(): bool
97
    {
98
        if ($this->isOnShipLevel === null) {
99
            $this->isOnShipLevel = $this->currentShip->getLocation() === $this->getPanelCenter();
100
        }
101
102
        return $this->isOnShipLevel;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->isOnShipLevel could return the type null which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
103
    }
104
105
    private function getPanelCenter(): MapInterface|StarSystemMapInterface
106
    {
107
        if ($this->panelCenter === null) {
108
            $this->panelCenter = $this->determinePanelCenter();
109
        }
110
111
        return $this->panelCenter;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->panelCenter could return the type null which is incompatible with the type-hinted return Stu\Orm\Entity\MapInterf...\StarSystemMapInterface. Consider adding an additional type-check to rule them out.
Loading history...
112
    }
113
114
    private function determinePanelCenter(): MapInterface|StarSystemMapInterface
115
    {
116
        $location = $this->currentShip->getLocation();
117
        if ($location instanceof MapInterface) {
118
            return $location;
119
        }
120
121
        if (
122
            $this->currentShip->getRump()->getRoleId() === ShipRumpEnum::SHIP_ROLE_SENSOR
123
            || $this->currentShip->getRump()->getRoleId() === ShipRumpEnum::SHIP_ROLE_BASE
124
        ) {
125
            $parentMapLocation = $this->getParentMapLocation($location);
126
127
            return $parentMapLocation ?? $location;
128
        }
129
130
        return $location;
131
    }
132
133
    private function getParentMapLocation(LocationInterface $location): ?MapInterface
134
    {
135
        if ($location instanceof StarSystemMapInterface) {
136
            return $location->getSystem()->getMap();
137
        }
138
139
        return null;
140
    }
141
142
    private function createUserMapEntries(LayerInterface $layer): void
143
    {
144
        $map = $this->currentShip->getMap();
145
        if ($map === null) {
146
            return;
147
        }
148
149
        $cx = $map->getX();
150
        $cy = $map->getY();
151
        $range = $this->currentShip->getSensorRange();
152
153
        if ($this->isUserMapActive($layer->getId())) {
154
            $this->userMapRepository->insertMapFieldsForUser(
155
                $this->user->getId(),
156
                $layer->getId(),
157
                $cx,
158
                $cy,
159
                $range
160
            );
161
        }
162
    }
163
164
    private function isUserMapActive(int $layerId): bool
165
    {
166
        if (!$this->user->hasColony()) {
167
            return false;
168
        }
169
170
        return !$this->user->hasExplored($layerId);
171
    }
172
}
173