Passed
Push — dev ( 709bf3...12fc43 )
by Nico
10:02
created

VisualNavPanelEntry::getLayer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Lib\Ui;
6
7
use Stu\Component\Ship\ShipLSSModeEnum;
8
use Stu\Component\Ship\ShipRumpEnum;
9
use Stu\Orm\Entity\ShipInterface;
10
use Stu\Orm\Entity\StarSystemInterface;
11
12
class VisualNavPanelEntry
13
{
14
    /** @var null|int */
15
    public $currentShipPosX = null;
16
17
    /** @var null|int */
18
    public $currentShipPosY = null;
19
20
    /** @var null|int */
21
    public $currentShipSysId = null;
22
23
    /** @var int */
24
    public $row;
25
26
    /**
27
     * @var array{
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{ at position 2 could not be parsed: the token is null at position 2.
Loading history...
28
     *     posx: int,
29
     *     posy: int,
30
     *     sysid: null|int,
31
     *     shipcount: int,
32
     *     cloakcount: int,
33
     *     allycolor: string,
34
     *     usercolor: string,
35
     *     factioncolor: string,
36
     *     shieldstate: null|bool,
37
     *     type: int,
38
     *    layer: int,
39
     *     d1c?: int,
40
     *     d2c?: int,
41
     *     d3c?: int,
42
     *     d4c?: int
43
     * }
44
     */
45
    private array $data;
46
47
    private bool $isTachyonSystemActive;
48
49
    private bool $tachyonFresh;
50
51
    private ?ShipInterface $ship;
52
53
    private int $tachyonRange;
54
55
    private ?StarSystemInterface $system;
56
57
    private string $cssClass = 'lss';
58
59
    /**
60
     * @param array{
61
     *     posx: int,
62
     *     posy: int,
63
     *     sysid: ?int,
64
     *     shipcount: int,
65
     *     cloakcount: int,
66
     *     allycolor: string,
67
     *     usercolor: string,
68
     *     factioncolor: string,
69
     *     shieldstate: null|bool,
70
     *     type: int,
71
     *     layer: int,
72
     *     d1c?: int,
73
     *     d2c?: int,
74
     *     d3c?: int,
75
     *     d4c?: int
76
     * } $entry
77
     */
78 1
    public function __construct(
79
        array &$entry,
80
        bool $isTachyonSystemActive = false,
81
        bool $tachyonFresh = false,
82
        ShipInterface $ship = null,
83
        StarSystemInterface $system = null
84
    ) {
85 1
        $this->data = $entry;
86 1
        $this->isTachyonSystemActive = $isTachyonSystemActive;
87 1
        $this->tachyonFresh = $tachyonFresh;
88 1
        $this->ship = $ship;
89 1
        $this->system = $system;
90 1
        $this->tachyonRange = $ship !== null ? ($ship->isBase() ? 7 : 3) : 0;
91
    }
92
93
    public function getPosX(): int
94
    {
95
        return $this->data['posx'];
96
    }
97
98
    public function getPosY(): int
99
    {
100
        return $this->data['posy'];
101
    }
102
103
    public function getSystemId(): int
104
    {
105
        return $this->data['sysid'] ?? 0;
106
    }
107
108
    public function getMapfieldType(): int
109
    {
110
        return $this->data['type'];
111
    }
112
113
    public function getLayer(): int
114
    {
115
        return $this->data['layer'];
116
    }
117
118
    public function getShipCount(): int
119
    {
120
        return $this->data['shipcount'];
121
    }
122
123
    public function hasCloakedShips(): bool
124
    {
125
        return $this->data['cloakcount'] > 0;
126
    }
127
128
    public function getShieldState(): bool
129
    {
130
        return $this->data['shieldstate'] ?? false;
131
    }
132
133
    public function hasShips(): bool
134
    {
135
        return $this->data['shipcount'] > 0;
136
    }
137
138
    public function getSubspaceCode(): ?string
139
    {
140
        $code = sprintf('%d%d%d%d', $this->getCode('d1c'), $this->getCode('d2c'), $this->getCode('d3c'), $this->getCode('d4c'));
141
        return $code == '0000' ? null : $code;
142
    }
143
144
    private function getCode(string $column): int
145
    {
146
        $shipCount = $this->data[$column] ?? 0;
147
148
        if ($shipCount == 0) {
149
            return 0;
150
        }
151
        if ($shipCount == 1) {
152
            return 1;
153
        }
154
        if ($shipCount < 6) {
155
            return 2;
156
        }
157
        if ($shipCount < 11) {
158
            return 3;
159
        }
160
        if ($shipCount < 21) {
161
            return 4;
162
        }
163
164
        return 5;
165
    }
166
167
    public function getDisplayCount(): string
168
    {
169
        if ($this->hasShips()) {
170
            return strval($this->getShipCount());
171
        }
172
        if ($this->hasCloakedShips()) {
173
            if ($this->tachyonFresh) {
174
                return "?";
175
            }
176
177
            if (
178
                $this->isTachyonSystemActive
179
                && abs($this->getPosX() - $this->currentShipPosX) < $this->tachyonRange
180
                && abs($this->getPosY() - $this->currentShipPosY) < $this->tachyonRange
181
            ) {
182
                return "?";
183
            }
184
        }
185
        return "";
186
    }
187
188
    public function getCacheValue(): string
189
    {
190
        return $this->getPosX() . "_" . $this->getPosY() . "_" . $this->getMapfieldType() . "_" . $this->getLayer() . "_" . $this->getDisplayCount() . "_" . $this->isClickAble() . "_" . $this->getBorder();
191
    }
192
193
    public function isCurrentShipPosition(): bool
194
    {
195
        if (
196
            $this->getSystemId() == $this->currentShipSysId
197
            && $this->getPosX() == $this->currentShipPosX
198
            && $this->getPosY() == $this->currentShipPosY
199
        ) {
200
            return true;
201
        }
202
        return false;
203
    }
204
205
    public function getBorder(): string
206
    {
207
        // current position gets grey border
208
        if (!$this->getRow() && $this->isCurrentShipPosition()) {
209
            return '#9b9b9b';
210
        }
211
212
        // hierarchy based border style
213
        if (
214
            $this->ship !== null &&
215
            $this->ship->getLSSmode() == ShipLSSModeEnum::LSS_BORDER
216
        ) {
217
            $factionColor = $this->data['factioncolor'] ?? null;
218
            if (!empty($factionColor)) {
219
                return $factionColor;
220
            }
221
222
            $allyColor = $this->data['allycolor'] ?? null;
223
            if (!empty($allyColor)) {
224
                return $allyColor;
225
            }
226
227
            $userColor = $this->data['usercolor'] ?? null;
228
            if (!empty($userColor)) {
229
                return $userColor;
230
            }
231
        }
232
233
        // default border style
234
        return '#2d2d2d';
235
    }
236
237
    public function setCSSClass(string $class): VisualNavPanelEntry
238
    {
239
        $this->cssClass = $class;
240
241
        return $this;
242
    }
243
244
    public function getCSSClass(): string
245
    {
246
        if (!$this->getRow() && $this->isCurrentShipPosition()) {
247
            return 'lss_current';
248
        }
249
        return $this->cssClass;
250
    }
251
252
    public function isClickAble(): bool
253
    {
254
        if ($this->ship === null) {
255
            return false;
256
        }
257
        if ($this->ship->getRump()->getRoleId() === ShipRumpEnum::SHIP_ROLE_SENSOR || $this->ship->getRump()->getRoleId() === ShipRumpEnum::SHIP_ROLE_BASE) {
258
            return true;
259
        }
260
        if (!$this->ship->canMove()) {
261
            return false;
262
        }
263
        if (!$this->isCurrentShipPosition() && ($this->getPosX() == $this->currentShipPosX || $this->getPosY() == $this->currentShipPosY)) {
264
            return true;
265
        }
266
267
        return false;
268
    }
269
270
    public function getOnClick(): string
271
    {
272
        if ($this->ship === null) {
273
            return '';
274
        }
275
276
        if (
277
            $this->ship->getRump()->getRoleId() === ShipRumpEnum::SHIP_ROLE_SENSOR
278
            || $this->ship->getRump()->getRoleId() === ShipRumpEnum::SHIP_ROLE_BASE
279
        ) {
280
            return sprintf(
281
                'showSectorScanWindow(this, %d, %d, %d, %s);',
282
                $this->getPosX(),
283
                $this->getPosY(),
284
                $this->system ? $this->system->getId() : 0,
285
                $this->system ? 'false' : 'true'
286
            );
287
        }
288
        return sprintf('moveToPosition(%d,%d);', $this->getPosX(), $this->getPosY());
289
    }
290
291
    public function getRow(): ?int
292
    {
293
        return $this->row;
294
    }
295
}
296