Passed
Push — dev ( f637f2...79177f )
by Nico
09:16
created

VisualNavPanel   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 200
Duplicated Lines 0 %

Test Coverage

Coverage 8.74%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 100
c 1
b 0
f 0
dl 0
loc 200
ccs 9
cts 103
cp 0.0874
rs 9.92
wmc 31

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 1
A getOuterSystemResult() 0 24 2
B getHeadRow() 0 42 11
A isUserMapActive() 0 7 2
A showOuterMap() 0 5 3
A getPanelViewportPercentage() 0 3 2
B loadLSS() 0 54 9
A getInnerSystemResult() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Lib\Ui;
6
7
use RuntimeException;
8
use Stu\Component\Ship\ShipRumpEnum;
9
use Stu\Lib\Map\VisualPanel\AbstractVisualPanel;
10
use Stu\Lib\Map\VisualPanel\VisualPanelEntryData;
11
use Stu\Lib\Map\VisualPanel\VisualPanelRow;
12
use Stu\Lib\Map\VisualPanel\VisualPanelRowIndex;
13
use Stu\Module\Logging\LoggerUtilInterface;
14
use Stu\Orm\Entity\ShipInterface;
15
use Stu\Orm\Entity\UserInterface;
16
use Stu\Orm\Repository\ShipRepositoryInterface;
17
use Stu\Orm\Repository\UserMapRepositoryInterface;
18
19
class VisualNavPanel extends AbstractVisualPanel
20
{
21
    private ShipInterface $currentShip;
22
23
    private UserInterface $user;
24
25
    private bool $isTachyonSystemActive;
26
27
    private bool $tachyonFresh;
28
29
    private UserMapRepositoryInterface $userMapRepository;
30
31
    private ShipRepositoryInterface $shipRepository;
32
33
    private ShipUiFactoryInterface $shipUiFactory;
34
35 1
    public function __construct(
36
        ShipUiFactoryInterface $shipUiFactory,
37
        UserMapRepositoryInterface $userMapRepository,
38
        ShipRepositoryInterface $shipRepository,
39
        ShipInterface $currentShip,
40
        UserInterface $user,
41
        LoggerUtilInterface $loggerUtil,
42
        bool $isTachyonSystemActive,
43
        bool $tachyonFresh
44
    ) {
45 1
        parent::__construct($loggerUtil);
46
47 1
        $this->userMapRepository = $userMapRepository;
48 1
        $this->shipRepository = $shipRepository;
49 1
        $this->currentShip = $currentShip;
50 1
        $this->user = $user;
51 1
        $this->isTachyonSystemActive = $isTachyonSystemActive;
52 1
        $this->tachyonFresh = $tachyonFresh;
53 1
        $this->shipUiFactory = $shipUiFactory;
54
    }
55
56
    /**
57
     * @return array<VisualPanelEntryData>
58
     */
59
    private function getOuterSystemResult(): array
60
    {
61
        $cx = $this->currentShip->getCX();
62
        $cy = $this->currentShip->getCY();
63
        $range = $this->currentShip->getSensorRange();
64
65
        $layerId = $this->currentShip->getLayerId();
66
        if ($this->isUserMapActive($layerId)) {
67
            $this->userMapRepository->insertMapFieldsForUser(
68
                $this->user->getId(),
69
                $layerId,
70
                $cx,
71
                $cy,
72
                $range
73
            );
74
        }
75
76
        return $this->shipRepository->getSensorResultOuterSystem(
77
            $cx,
78
            $cy,
79
            $layerId,
80
            $range,
81
            $this->currentShip->getSubspaceState(),
82
            $this->user->getId()
83
        );
84
    }
85
86
    private function isUserMapActive(int $layerId): bool
87
    {
88
        if (!$this->user->hasColony()) {
89
            return false;
90
        }
91
92
        return !$this->user->hasExplored($layerId);
93
    }
94
95
    /**
96
     * @return array<VisualPanelEntryData>
97
     */
98
    private function getInnerSystemResult(): iterable
99
    {
100
        return $this->shipRepository->getSensorResultInnerSystem(
101
            $this->currentShip,
102
            $this->user->getId()
103
        );
104
    }
105
106
    protected function loadLSS(): array
107
    {
108
        if ($this->loggerUtil->doLog()) {
109
            $startTime = microtime(true);
0 ignored issues
show
Unused Code introduced by
The assignment to $startTime is dead and can be removed.
Loading history...
110
        }
111
        if ($this->showOuterMap()) {
112
            $result = $this->getOuterSystemResult();
113
        } else {
114
            $result = $this->getInnerSystemResult();
115
        }
116
        if ($this->loggerUtil->doLog()) {
117
            $endTime = microtime(true);
0 ignored issues
show
Unused Code introduced by
The assignment to $endTime is dead and can be removed.
Loading history...
118
        }
119
120
        $currentShip = $this->currentShip;
121
122
        if ($this->loggerUtil->doLog()) {
123
            $startTime = microtime(true);
124
        }
125
126
        $rows = [];
127
128
        foreach ($result as $data) {
129
            $y = $data->getPosY();
130
131
            if ($y < 1) {
132
                continue;
133
            }
134
135
            //create new row if y changed
136
            if (!array_key_exists($y, $rows)) {
137
                $navPanelRow = new VisualPanelRow();
138
                $rowIndex = new VisualPanelRowIndex($y, 'th');
139
                $navPanelRow->addEntry($rowIndex);
140
141
                $rows[$y] = $navPanelRow;
142
            }
143
144
            $navPanelRow = $rows[$y];
145
            $entry = $this->shipUiFactory->createVisualNavPanelEntry(
146
                $data,
147
                $currentShip->getLayer(),
148
                $currentShip,
149
                $this->isTachyonSystemActive,
150
                $this->tachyonFresh
151
            );
152
            $navPanelRow->addEntry($entry);
153
        }
154
        if ($this->loggerUtil->doLog()) {
155
            $endTime = microtime(true);
156
            //$this->loggerUtil->log(sprintf("\tloadLSS-loop, seconds: %F", $endTime - $startTime));
157
        }
158
159
        return $rows;
160
    }
161
162
    /**
163
     * @return array<array{value: int}>
164
     */
165
    public function getHeadRow(): array
166
    {
167
        if ($this->headRow === null) {
168
            $cx = $this->showOuterMap() ? $this->currentShip->getCx() : $this->currentShip->getPosX();
169
            $range = $this->currentShip->getSensorRange();
170
171
            $min = $cx - $range;
172
            $max = $cx + $range;
173
174
            $row = [];
175
176
            while ($min <= $max) {
177
                if ($min < 1) {
178
                    $min++;
179
                    continue;
180
                }
181
                if ($this->showOuterMap()) {
182
                    if ($this->currentShip->getLayer() === null) {
183
                        throw new RuntimeException('layer should not be null if outside of system');
184
                    }
185
186
                    if ($min > $this->currentShip->getLayer()->getWidth()) {
187
                        break;
188
                    }
189
                }
190
                if (!$this->showOuterMap()) {
191
                    if ($this->currentShip->getSystem() === null) {
192
                        throw new RuntimeException('system should not be null if inside of system');
193
                    }
194
195
                    if ($min > $this->currentShip->getSystem()->getMaxX()) {
196
                        break;
197
                    }
198
                }
199
                $row[]['value'] = $min;
200
                $min++;
201
            }
202
203
            $this->headRow = $row;
204
        }
205
206
        return $this->headRow;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->headRow could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
207
    }
208
209
    protected function getPanelViewportPercentage(): int
210
    {
211
        return $this->currentShip->isBase() ? 50 : 33;
212
    }
213
214
    private function showOuterMap(): bool
215
    {
216
        return $this->currentShip->getSystem() === null
217
            || $this->currentShip->getRump()->getRoleId() === ShipRumpEnum::SHIP_ROLE_SENSOR
218
            || $this->currentShip->getRump()->getRoleId() === ShipRumpEnum::SHIP_ROLE_BASE;
219
    }
220
}
221