Passed
Push — master ( 37217b...efdda9 )
by Nico
19:16 queued 09:28
created

VisualNavPanelEntry::getSystemId()   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
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
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 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;
16
17
    /** @var null|int */
18
    public $currentShipPosY;
19
20
    /** @var null|int */
21
    public $currentShipSysId;
22
23
    public ?int $row = null;
24
25
    /**
26
     * @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...
27
     *     posx: int,
28
     *     posy: int,
29
     *     sysid: null|int,
30
     *     shipcount: int,
31
     *     cloakcount: int,
32
     *     allycolor: string,
33
     *     usercolor: string,
34
     *     factioncolor: string,
35
     *     shieldstate: null|bool,
36
     *     type: int,
37
     *    layer: int,
38
     *     d1c?: int,
39
     *     d2c?: int,
40
     *     d3c?: int,
41
     *     d4c?: int
42
     * }
43
     */
44
    private array $data;
45
46
    private bool $isTachyonSystemActive;
47
48
    private bool $tachyonFresh;
49
50
    private ?ShipInterface $ship;
51
52
    private int $tachyonRange;
53
54
    private ?StarSystemInterface $system;
55
56
    private string $cssClass = 'lss';
57
58
    /**
59
     * @param array{
60
     *     posx: int,
61
     *     posy: int,
62
     *     sysid: ?int,
63
     *     shipcount: int,
64
     *     cloakcount: int,
65
     *     allycolor: string,
66
     *     usercolor: string,
67
     *     factioncolor: string,
68
     *     shieldstate: null|bool,
69
     *     type: int,
70
     *     layer: int,
71
     *     d1c?: int,
72
     *     d2c?: int,
73
     *     d3c?: int,
74
     *     d4c?: int
75
     * } $entry
76
     */
77 1
    public function __construct(
78
        array &$entry,
79
        bool $isTachyonSystemActive = false,
80
        bool $tachyonFresh = false,
81
        ShipInterface $ship = null,
82
        StarSystemInterface $system = null
83
    ) {
84 1
        $this->data = $entry;
85 1
        $this->isTachyonSystemActive = $isTachyonSystemActive;
86 1
        $this->tachyonFresh = $tachyonFresh;
87 1
        $this->ship = $ship;
88 1
        $this->system = $system;
89 1
        $this->tachyonRange = $ship !== null ? ($ship->isBase() ? 7 : 3) : 0;
90
    }
91
92
    public function getPosX(): int
93
    {
94
        return $this->data['posx'];
95
    }
96
97
    public function getPosY(): int
98
    {
99
        return $this->data['posy'];
100
    }
101
102
    public function getSystemId(): int
103
    {
104
        return $this->data['sysid'] ?? 0;
105
    }
106
107
    public function getMapfieldType(): int
108
    {
109
        return $this->data['type'];
110
    }
111
112
    public function getFieldGraphicID(): int
113
    {
114
        $fieldId = $this->getMapfieldType();
115
116
117
        if ($fieldId === 1) {
118
            return 0;
119
        } else {
120
121
            return $fieldId;
122
        }
123
    }
124
125
    public function getSystemBackgroundId(): string
126
    {
127
128
        $x = (string)$this->getPosX();
129
        $y = (string)$this->getPosY();
130
131
        $x = str_pad($x, 2, '0', STR_PAD_LEFT);
132
        $y = str_pad($y, 2, '0', STR_PAD_LEFT);
133
134
        $backgroundId = $y . $x;
135
136
        return $backgroundId;
137
    }
138
139
140
    public function getLayer(): ?int
141
    {
142
        return $this->data['layer'];
143
    }
144
145
    public function getShipCount(): int
146
    {
147
        return $this->data['shipcount'];
148
    }
149
150
    public function hasCloakedShips(): bool
151
    {
152
        return $this->data['cloakcount'] > 0;
153
    }
154
155
    public function getShieldState(): bool
156
    {
157
        return $this->data['shieldstate'] ?? false;
158
    }
159
160
    public function hasShips(): bool
161
    {
162
        return $this->data['shipcount'] > 0;
163
    }
164
165
    public function getSubspaceCode(): ?string
166
    {
167
        $code = sprintf('%d%d%d%d', $this->getCode('d1c'), $this->getCode('d2c'), $this->getCode('d3c'), $this->getCode('d4c'));
168
        return $code == '0000' ? null : $code;
169
    }
170
171
    private function getCode(string $column): int
172
    {
173
        $shipCount = $this->data[$column] ?? 0;
174
175
        if ($shipCount == 0) {
176
            return 0;
177
        }
178
        if ($shipCount == 1) {
179
            return 1;
180
        }
181
        if ($shipCount < 6) {
182
            return 2;
183
        }
184
        if ($shipCount < 11) {
185
            return 3;
186
        }
187
        if ($shipCount < 21) {
188
            return 4;
189
        }
190
191
        return 5;
192
    }
193
194
    public function getDisplayCount(): string
195
    {
196
        if ($this->hasShips()) {
197
            return (string) $this->getShipCount();
198
        }
199
        if ($this->hasCloakedShips()) {
200
            if ($this->tachyonFresh) {
201
                return "?";
202
            }
203
204
            if (
205
                $this->isTachyonSystemActive
206
                && abs($this->getPosX() - $this->currentShipPosX) < $this->tachyonRange
207
                && abs($this->getPosY() - $this->currentShipPosY) < $this->tachyonRange
208
            ) {
209
                return "?";
210
            }
211
        }
212
        return "";
213
    }
214
215
    public function getCacheValue(): string
216
    {
217
        return $this->getPosX() . "_" . $this->getPosY() . "_" . $this->getMapfieldType() . "_" . $this->getLayer() . "_" . $this->getDisplayCount() . "_" . $this->isClickAble() . "_" . $this->getBorder();
218
    }
219
220
    public function isCurrentShipPosition(): bool
221
    {
222
        return $this->getSystemId() == $this->currentShipSysId
223
            && $this->getPosX() == $this->currentShipPosX
224
            && $this->getPosY() == $this->currentShipPosY;
225
    }
226
227
    public function getBorder(): string
228
    {
229
        // current position gets grey border
230
        if (!$this->getRow() && $this->isCurrentShipPosition()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->getRow() of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
231
            return '#9b9b9b';
232
        }
233
234
        // hierarchy based border style
235
        if (
236
            $this->ship !== null &&
237
            $this->ship->getLSSmode() == ShipLSSModeEnum::LSS_BORDER
238
        ) {
239
            $factionColor = $this->data['factioncolor'] ?? null;
240
            if (!empty($factionColor)) {
241
                return $factionColor;
242
            }
243
244
            $allyColor = $this->data['allycolor'] ?? null;
245
            if (!empty($allyColor)) {
246
                return $allyColor;
247
            }
248
249
            $userColor = $this->data['usercolor'] ?? null;
250
            if (!empty($userColor)) {
251
                return $userColor;
252
            }
253
        }
254
255
        // default border style
256
        return '#2d2d2d';
257
    }
258
259
    public function setCSSClass(string $class): VisualNavPanelEntry
260
    {
261
        $this->cssClass = $class;
262
263
        return $this;
264
    }
265
266
    public function getCSSClass(): string
267
    {
268
        if (!$this->getRow() && $this->isCurrentShipPosition()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->getRow() of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
269
            return 'lss_current';
270
        }
271
        return $this->cssClass;
272
    }
273
274
    public function isClickAble(): bool
275
    {
276
        if ($this->ship === null) {
277
            return false;
278
        }
279
        if ($this->ship->getRump()->getRoleId() === ShipRumpEnum::SHIP_ROLE_SENSOR || $this->ship->getRump()->getRoleId() === ShipRumpEnum::SHIP_ROLE_BASE) {
280
            return true;
281
        }
282
        if (!$this->ship->canMove()) {
283
            return false;
284
        }
285
        return !$this->isCurrentShipPosition() && ($this->getPosX() == $this->currentShipPosX || $this->getPosY() == $this->currentShipPosY);
286
    }
287
288
    public function getOnClick(): string
289
    {
290
        if ($this->ship === null) {
291
            return '';
292
        }
293
294
        if (
295
            $this->ship->getRump()->getRoleId() === ShipRumpEnum::SHIP_ROLE_SENSOR
296
            || $this->ship->getRump()->getRoleId() === ShipRumpEnum::SHIP_ROLE_BASE
297
        ) {
298
            return sprintf(
299
                'showSectorScanWindow(this, %d, %d, %d, %s);',
300
                $this->getPosX(),
301
                $this->getPosY(),
302
                $this->system !== null ? $this->system->getId() : 0,
303
                $this->system !== null ? 'false' : 'true'
304
            );
305
        }
306
        return sprintf('moveToPosition(%d,%d);', $this->getPosX(), $this->getPosY());
307
    }
308
309
    public function getRow(): ?int
310
    {
311
        return $this->row;
312
    }
313
}
314