Failed Conditions
Push — dev ( 776883...d01ef6 )
by Nico
07:09
created

StarSystemMapRepository::truncateByStarSystem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 11
ccs 0
cts 9
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Orm\Repository;
6
7
use Doctrine\ORM\EntityRepository;
8
use Doctrine\ORM\Query\ResultSetMapping;
9
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...
10
use Stu\Component\Building\BuildingFunctionEnum;
11
use Stu\Component\Ship\FlightSignatureVisibilityEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Ship\FlightSignatureVisibilityEnum 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...
12
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
13
use Stu\Lib\Map\VisualPanel\PanelBoundaries;
14
use Stu\Module\PlayerSetting\Lib\UserConstants;
0 ignored issues
show
Bug introduced by
The type Stu\Module\PlayerSetting\Lib\UserConstants 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...
15
use Stu\Orm\Entity\AstronomicalEntry;
0 ignored issues
show
Bug introduced by
The type Stu\Orm\Entity\AstronomicalEntry 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...
16
use Stu\Orm\Entity\StarSystem;
17
use Stu\Orm\Entity\StarSystemMap;
18
19
/**
20
 * @extends EntityRepository<StarSystemMap>
21
 */
22
final class StarSystemMapRepository extends EntityRepository implements StarSystemMapRepositoryInterface
23
{
24
    public function getBySystemOrdered(int $starSystemId): array
25
    {
26
        return $this->findBy(
27
            ['systems_id' => $starSystemId],
28
            ['sy' => 'asc', 'sx' => 'asc']
29
        );
30
    }
31
32 2
    #[Override]
33
    public function getByCoordinates(
34
        int $starSystemId,
35
        int $sx,
36
        int $sy
37
    ): ?StarSystemMap {
38 2
        return $this->findOneBy([
39 2
            'systems_id' => $starSystemId,
40 2
            'sx' => $sx,
41 2
            'sy' => $sy
42 2
        ]);
43
    }
44
45
    #[Override]
46
    public function getByBoundaries(PanelBoundaries $boundaries): array
47
    {
48
        return $this->getByCoordinateRange(
49
            $boundaries->getParentId(),
50
            $boundaries->getMinX(),
51
            $boundaries->getMaxX(),
52
            $boundaries->getMinY(),
53
            $boundaries->getMaxY()
54
        );
55
    }
56
57
    #[Override]
58
    public function getByCoordinateRange(
59
        int $starSystemId,
60
        int $startSx,
61
        int $endSx,
62
        int $startSy,
63
        int $endSy,
64
        bool $sortAscending = true
65
    ): array {
66
        return $this->getEntityManager()
67
            ->createQuery(
68
                sprintf(
69
                    'SELECT m FROM %1$s m
70
                    WHERE m.systems_id = :starSystemId AND
71
                        m.sx BETWEEN :startSx AND :endSx AND
72
                        m.sy BETWEEN :startSy AND :endSy
73
                    ORDER BY m.sy %2$s, m.sx %2$s',
74
                    StarSystemMap::class,
75
                    $sortAscending ? 'ASC' : 'DESC'
76
                )
77
            )
78
            ->setParameters([
79
                'starSystemId' => $starSystemId,
80
                'startSx' => $startSx,
81
                'endSx' => $endSx,
82
                'startSy' => $startSy,
83
                'endSy' => $endSy
84
            ])
85
            ->getResult();
86
    }
87
88 4
    #[Override]
89
    public function getMapLayerData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
90
    {
91 4
        return $this->getEntityManager()->createNativeQuery(
92 4
            'SELECT sm.sx as x, sm.sy AS y, ft.type
93
                FROM stu_sys_map sm
94
                JOIN stu_location l
95
                ON sm.id = l.id
96
                JOIN stu_map_ftypes ft ON ft.id = l.field_id
97
                WHERE sm.sx BETWEEN :xStart AND :xEnd AND sm.sy BETWEEN :yStart AND :yEnd
98 4
                AND sm.systems_id = :systemId',
99 4
            $rsm
100 4
        )->setParameters([
101 4
            'xStart' => $boundaries->getMinX(),
102 4
            'xEnd' => $boundaries->getMaxX(),
103 4
            'yStart' => $boundaries->getMinY(),
104 4
            'yEnd' => $boundaries->getMaxY(),
105 4
            'systemId' => $boundaries->getParentId()
106 4
        ])->getResult();
107
    }
108
109 4
    #[Override]
110
    public function getSpacecraftCountLayerData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
111
    {
112 4
        return $this->getEntityManager()->createNativeQuery(
113 4
            'SELECT sm.id, sm.sx as x, sm.sy AS y,
114
                (SELECT count(DISTINCT b.id) FROM stu_spacecraft b
115
                    WHERE sm.id = b.location_id
116
                    AND NOT EXISTS (SELECT ss.id
117
                                        FROM stu_spacecraft_system ss
118
                                        WHERE b.id = ss.spacecraft_id
119
                                        AND ss.system_type = :cloakSystemId
120
                                        AND ss.mode > 1)) AS spacecraftcount,
121
                (SELECT count(DISTINCT c.id) FROM stu_spacecraft c
122
                    WHERE sm.id = c.location_id
123
                    AND EXISTS (SELECT ss2.id
124
                                        FROM stu_spacecraft_system ss2
125
                                        WHERE c.id = ss2.spacecraft_id
126
                                        AND ss2.system_type = :cloakSystemId
127
                                        AND ss2.mode > 1)) AS cloakcount,
128
                (SELECT mft.effects FROM stu_map_ftypes mft
129
                WHERE l.field_id = mft.id) as effects
130
            FROM stu_sys_map sm
131
            JOIN stu_location l
132
            ON sm.id = l.id
133
            WHERE sm.sx BETWEEN :xStart AND :xEnd AND sm.sy BETWEEN :yStart AND :yEnd
134
            AND sm.systems_id = :systemId
135 4
            GROUP BY sm.id, sm.sy, sm.sx, l.field_id',
136 4
            $rsm
137 4
        )->setParameters([
138 4
            'xStart' => $boundaries->getMinX(),
139 4
            'xEnd' => $boundaries->getMaxX(),
140 4
            'yStart' => $boundaries->getMinY(),
141 4
            'yEnd' => $boundaries->getMaxY(),
142 4
            'systemId' => $boundaries->getParentId(),
143 4
            'cloakSystemId' => SpacecraftSystemTypeEnum::CLOAK->value
144 4
        ])->getResult();
145
    }
146
147 4
    #[Override]
148
    public function getColonyShieldData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
149
    {
150 4
        return $this->getEntityManager()->createNativeQuery(
151 4
            'SELECT sm.sx as x, sm.sy AS y,
152
            (SELECT COUNT(*) > 0
153
                FROM stu_colony col
154
                JOIN stu_colonies_fielddata cfd
155
                ON col.id = cfd.colonies_id
156
                WHERE sm.id = col.starsystem_map_id
157
                AND cfd.aktiv = :active
158
                AND cfd.buildings_id IN (
159
                    SELECT bf.buildings_id
160
                    FROM stu_buildings_functions bf
161
                    WHERE bf.function = :shieldBuilding)) AS shieldstate
162
            FROM stu_sys_map sm
163
            WHERE sm.systems_id = :systemId
164 4
            AND sm.sx BETWEEN :xStart AND :xEnd AND sm.sy BETWEEN :yStart AND :yEnd',
165 4
            $rsm
166 4
        )->setParameters([
167 4
            'xStart' => $boundaries->getMinX(),
168 4
            'xEnd' => $boundaries->getMaxX(),
169 4
            'yStart' => $boundaries->getMinY(),
170 4
            'yEnd' => $boundaries->getMaxY(),
171 4
            'systemId' => $boundaries->getParentId(),
172 4
            'active' => 1,
173 4
            'shieldBuilding' => BuildingFunctionEnum::SHIELD_GENERATOR
174 4
        ])->getResult();
175
    }
176
177
    #[Override]
178
    public function getNormalBorderData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
179
    {
180
        return $this->getEntityManager()->createNativeQuery(
181
            'SELECT sm.sx AS x, sm.sy AS y
182
            FROM stu_sys_map sm
183
            WHERE sm.systems_id = :systemId
184
            AND sm.sx BETWEEN :xStart AND :xEnd
185
            AND sm.sy BETWEEN :yStart AND :yEnd',
186
            $rsm
187
        )->setParameters([
188
            'xStart' => $boundaries->getMinX(),
189
            'xEnd' => $boundaries->getMaxX(),
190
            'yStart' => $boundaries->getMinY(),
191
            'yEnd' => $boundaries->getMaxY(),
192
            'systemId' => $boundaries->getParentId()
193
        ])->getResult();
194
    }
195
196
    #[Override]
197
    public function getRegionBorderData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
198
    {
199
        return $this->getEntityManager()->createNativeQuery(
200
            'SELECT sm.sx AS x, sm.sy AS y
201
            FROM stu_sys_map sm
202
            WHERE sm.systems_id = :systemId
203
            AND sm.sx BETWEEN :xStart AND :xEnd
204
            AND sm.sy BETWEEN :yStart AND :yEnd',
205
            $rsm
206
        )->setParameters([
207
            'xStart' => $boundaries->getMinX(),
208
            'xEnd' => $boundaries->getMaxX(),
209
            'yStart' => $boundaries->getMinY(),
210
            'yEnd' => $boundaries->getMaxY(),
211
            'systemId' => $boundaries->getParentId()
212
        ])->getResult();
213
    }
214
215
    // TODO: Show impassable only for cartographed systems. 
216
    // Currently, it does not display any impassable areas.
217
    #[Override]
218
    public function getImpassableBorderData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
219
    {
220
        return $this->getEntityManager()->createNativeQuery(
221
            'SELECT sm.sx AS x, sm.sy AS y, TRUE AS impassable,
222
                    (SELECT mft.complementary_color FROM stu_map_ftypes mft JOIN stu_location l on l.id = sm.id where mft.id = l.field_id) AS complementary_color
223
            FROM stu_sys_map sm
224
            WHERE sm.systems_id = :systemId
225
            AND sm.sx BETWEEN :xStart AND :xEnd
226
            AND sm.sy BETWEEN :yStart AND :yEnd',
227
            $rsm
228
        )->setParameters([
229
            'xStart' => $boundaries->getMinX(),
230
            'xEnd' => $boundaries->getMaxX(),
231
            'yStart' => $boundaries->getMinY(),
232
            'yEnd' => $boundaries->getMaxY(),
233
            'systemId' => $boundaries->getParentId()
234
        ])->getResult();
235
    }
236
237
    #[Override]
238
    public function getCartographingData(PanelBoundaries $boundaries, ResultSetMapping $rsm, array $locations): array
239
    {
240
        return $this->getEntityManager()->createNativeQuery(
241
            'SELECT DISTINCT 
242
                sm.sx AS x, 
243
                sm.sy AS y, 
244
                CASE 
245
                    WHEN sm.id IN (:fieldIds) THEN TRUE ELSE FALSE
246
                END AS cartographing,
247
                    (SELECT mft.complementary_color FROM stu_map_ftypes mft JOIN stu_location l on l.id = sm.id where mft.id = l.field_id) AS complementary_color
248
            FROM stu_sys_map sm
249
            WHERE sm.sx BETWEEN :xStart AND :xEnd
250
            AND sm.sy BETWEEN :yStart AND :yEnd
251
            AND sm.systems_id = :systemId
252
            ORDER BY cartographing DESC',
253
            $rsm
254
        )->setParameters([
255
            'xStart' => $boundaries->getMinX(),
256
            'xEnd' => $boundaries->getMaxX(),
257
            'yStart' => $boundaries->getMinY(),
258
            'yEnd' => $boundaries->getMaxY(),
259
            'systemId' => $boundaries->getParentId(),
260
            'fieldIds' => $locations
261
        ])->getResult();
262
    }
263
264
265
    #[Override]
266
    public function getAnomalyData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
267
    {
268
        return $this->getEntityManager()->createNativeQuery(
269
            'SELECT sm.sx AS x, sm.sy AS y,
270
                (SELECT array_to_string(array(SELECT a.anomaly_type_id FROM stu_anomaly a WHERE a.location_id = sm.id), \',\')) as anomalytypes
271
            FROM stu_sys_map sm
272
            WHERE sm.systems_id = :systemId
273
            AND sm.sx BETWEEN :xStart AND :xEnd
274
            AND sm.sy BETWEEN :yStart AND :yEnd',
275
            $rsm
276
        )->setParameters([
277
            'xStart' => $boundaries->getMinX(),
278
            'xEnd' => $boundaries->getMaxX(),
279
            'yStart' => $boundaries->getMinY(),
280
            'yEnd' => $boundaries->getMaxY(),
281
            'systemId' => $boundaries->getParentId()
282
        ])->getResult();
283
    }
284
285 4
    #[Override]
286
    public function getLssBlockadeLocations(PanelBoundaries $boundaries): array
287
    {
288 4
        $rsm = new ResultSetMapping();
289 4
        $rsm->addScalarResult('sx', 'x', 'integer');
290 4
        $rsm->addScalarResult('sy', 'y', 'integer');
291 4
        $rsm->addScalarResult('effects', 'effects', 'string');
292
293 4
        return $this->getEntityManager()->createNativeQuery(
294 4
            'WITH bbox AS (
295
                SELECT id, sx, sy
296
                FROM stu_sys_map
297
                WHERE systems_id = :systemId
298
                AND sx BETWEEN :xStart AND :xEnd
299
                AND sy BETWEEN :yStart AND :yEnd
300
            )
301
            SELECT sm.sx, sm.sy, mft.effects
302
            FROM bbox sm
303
            JOIN stu_location l ON sm.id = l.id
304 4
            JOIN stu_map_ftypes mft ON mft.id   = l.field_id',
305 4
            $rsm
306 4
        )->setParameters([
307 4
            'xStart' => $boundaries->getMinX(),
308 4
            'xEnd' => $boundaries->getMaxX(),
309 4
            'yStart' => $boundaries->getMinY(),
310 4
            'yEnd' => $boundaries->getMaxY(),
311 4
            'systemId' => $boundaries->getParentId()
312 4
        ])->getResult();
313
    }
314
315
    #[Override]
316
    public function getIgnoringSubspaceLayerData(PanelBoundaries $boundaries, int $ignoreUserId, ResultSetMapping $rsm): array
317
    {
318
        $maxAge = time() - FlightSignatureVisibilityEnum::SIG_VISIBILITY_UNCLOAKED;
319
320
        return $this->getEntityManager()->createNativeQuery(
321
            'SELECT sm.sx as x, sm.sy AS y, mft.effects as effects,
322
                (select count(distinct fs1.ship_id) from stu_flight_sig fs1
323
                where fs1.location_id = sm.id
324
                AND fs1.user_id != :ignoreUserId
325
                AND (fs1.from_direction = 1 OR fs1.to_direction = 1)
326
                AND fs1.time > :timeThreshold) as d1c,
327
                (select count(distinct fs2.ship_id) from stu_flight_sig fs2
328
                where fs2.location_id = sm.id
329
                AND fs2.user_id != :ignoreUserId
330
                AND (fs2.from_direction = 2 OR fs2.to_direction = 2)
331
                AND fs2.time > :timeThreshold) as d2c,
332
                (select count(distinct fs3.ship_id) from stu_flight_sig fs3
333
                where fs3.location_id = sm.id
334
                AND fs3.user_id != :ignoreUserId
335
                AND (fs3.from_direction = 3 OR fs3.to_direction = 3)
336
                AND fs3.time > :timeThreshold) as d3c,
337
                (select count(distinct fs4.ship_id) from stu_flight_sig fs4
338
                where fs4.location_id = sm.id
339
                AND fs4.user_id != :ignoreUserId
340
                AND (fs4.from_direction = 4 OR fs4.to_direction = 4)
341
                AND fs4.time > :timeThreshold) as d4c 
342
                FROM stu_sys_map sm
343
                JOIN stu_location l
344
                ON sm.id = l.id
345
                JOIN stu_map_ftypes mft
346
                ON l.field_id = mft.id
347
                WHERE sm.systems_id = :systemId
348
                AND sm.sx BETWEEN :xStart AND :xEnd AND sm.sy BETWEEN :yStart AND :yEnd',
349
            $rsm
350
        )->setParameters([
351
            'xStart' => $boundaries->getMinX(),
352
            'xEnd' => $boundaries->getMaxX(),
353
            'yStart' => $boundaries->getMinY(),
354
            'yEnd' => $boundaries->getMaxY(),
355
            'systemId' => $boundaries->getParentId(),
356
            'ignoreUserId' => $ignoreUserId,
357
            'timeThreshold' => $maxAge
358
        ])->getResult();
359
    }
360
    #[Override]
361
    public function getShipSubspaceLayerData(PanelBoundaries $boundaries, int $shipId, ResultSetMapping $rsm, ?int $rumpId = null): array
362
    {
363
        $maxAge = time() - FlightSignatureVisibilityEnum::SIG_VISIBILITY_UNCLOAKED;
364
365
        $rumpId_condition = $rumpId ? 'AND fs1.rump_id = :rumpId' : '';
366
        $rumpId_condition2 = $rumpId ? 'AND fs2.rump_id = :rumpId' : '';
367
        $rumpId_condition3 = $rumpId ? 'AND fs3.rump_id = :rumpId' : '';
368
        $rumpId_condition4 = $rumpId ? 'AND fs4.rump_id = :rumpId' : '';
369
370
        $query = $this->getEntityManager()->createNativeQuery(
371
            sprintf(
372
                'SELECT sm.sx as x, sm.sy as y, mft.effects as effects,
373
            (SELECT count(distinct fs1.ship_id) from stu_flight_sig fs1
374
                WHERE fs1.location_id = l.id
375
                AND fs1.ship_id = :shipId %s
376
                AND (fs1.from_direction = 1 OR fs1.to_direction = 1)
377
                AND fs1.time > :timeThreshold AND fs1.is_cloaked = :false) as d1c,
378
            (SELECT count(distinct fs2.ship_id) from stu_flight_sig fs2
379
                WHERE fs2.location_id = l.id
380
                AND fs2.ship_id = :shipId %s
381
                AND (fs2.from_direction = 2 OR fs2.to_direction = 2)
382
                AND fs2.time > :timeThreshold AND fs2.is_cloaked = :false) as d2c,
383
            (SELECT count(distinct fs3.ship_id) from stu_flight_sig fs3
384
                WHERE fs3.location_id = l.id
385
                AND fs3.ship_id = :shipId %s
386
                AND (fs3.from_direction = 3 OR fs3.to_direction = 3)
387
                AND fs3.time > :timeThreshold AND fs3.is_cloaked = :false) as d3c,
388
            (SELECT count(distinct fs4.ship_id) from stu_flight_sig fs4
389
                WHERE fs4.location_id = l.id
390
                AND fs4.ship_id = :shipId %s
391
                AND (fs4.from_direction = 4 OR fs4.to_direction = 4)
392
                AND fs4.time > :timeThreshold AND fs4.is_cloaked = :false) as d4c 
393
            FROM stu_sys_map sm
394
                JOIN stu_location l
395
                ON sm.id = l.id
396
                JOIN stu_map_ftypes mft
397
                ON l.field_id = mft.id
398
                WHERE sm.systems_id = :systemId
399
                AND sm.sx BETWEEN :xStart AND :xEnd AND sm.sy BETWEEN :yStart AND :yEnd',
400
                $rumpId_condition,
401
                $rumpId_condition2,
402
                $rumpId_condition3,
403
                $rumpId_condition4
404
            ),
405
            $rsm
406
        );
407
408
        $parameters = [
409
            'xStart' => $boundaries->getMinX(),
410
            'xEnd' => $boundaries->getMaxX(),
411
            'yStart' => $boundaries->getMinY(),
412
            'yEnd' => $boundaries->getMaxY(),
413
            'systemId' => $boundaries->getParentId(),
414
            'shipId' => $shipId,
415
            'timeThreshold' => $maxAge,
416
            'false' => false
417
        ];
418
419
        if ($rumpId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $rumpId of type integer|null is loosely compared to true; 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...
420
            $parameters['rumpId'] = $rumpId;
421
        }
422
423
        return $query->setParameters($parameters)->getResult();
424
    }
425
426
    #[Override]
427
    public function getRandomSystemMapIdsForAstroMeasurement(int $starSystemId, int $location): array
428
    {
429
        $result = [];
430
431
        $rsm = new ResultSetMapping();
432
        $rsm->addScalarResult('id', 'id', 'integer');
433
434
        $userColonyFields = $this->getEntityManager()
435
            ->createNativeQuery(
436
                'SELECT sm.id as id
437
                FROM stu_sys_map sm
438
                WHERE sm.systems_id = :systemId
439
                AND sm.id != :location
440
                AND EXISTS (SELECT c.id
441
                            FROM stu_colony c
442
                            WHERE c.starsystem_map_id = sm.id
443
                            AND c.user_id != :noOne)
444
                ORDER BY RANDOM()
445
                LIMIT 2',
446
                $rsm
447
            )
448
            ->setParameters([
449
                'systemId' => $starSystemId,
450
                'location'  => $location,
451
                'noOne' => UserConstants::USER_NOONE
452
            ])
453
            ->getResult();
454
455
        $result = array_merge($result, $userColonyFields);
456
457
        $otherColonyFields = $this->getEntityManager()
458
            ->createNativeQuery(
459
                'SELECT sm.id as id
460
                FROM stu_sys_map sm
461
                JOIN stu_location l
462
                ON sm.id = l.id
463
                JOIN stu_map_ftypes ft
464
                ON l.field_id = ft.id
465
                JOIN stu_colonies_classes cc
466
                ON ft.colonies_classes_id = cc.id
467
                WHERE sm.systems_id = :systemId
468
                AND ft.colonies_classes_id IS NOT NULL
469
                AND cc.type < 3
470
                AND sm.id NOT IN (:ids)
471
                ORDER BY RANDOM()
472
                LIMIT :theLimit',
473
                $rsm
474
            )
475
            ->setParameters([
476
                'systemId' => $starSystemId,
477
                'ids' => $result !== [] ? $result : [0],
478
                'theLimit' => AstronomicalEntry::MEASUREMENT_COUNT - count($result)
479
            ])
480
            ->getResult();
481
482
        $result = array_merge($result, $otherColonyFields);
483
484
        if (count($result) < AstronomicalEntry::MEASUREMENT_COUNT) {
485
            $otherFields = $this->getEntityManager()
486
                ->createNativeQuery(
487
                    'SELECT sm.id as id
488
                    FROM stu_sys_map sm
489
                    JOIN stu_location l
490
                    ON sm.id = l.id
491
                    JOIN stu_map_ftypes ft
492
                    ON l.field_id = ft.id
493
                    WHERE sm.systems_id = :systemId
494
                    AND ft.x_damage_system <= 10
495
                    AND ft.x_damage <= 10
496
                    ORDER BY RANDOM()
497
                    LIMIT :theLimit',
498
                    $rsm
499
                )
500
                ->setParameters([
501
                    'systemId' => $starSystemId,
502
                    'theLimit' => AstronomicalEntry::MEASUREMENT_COUNT - count($result)
503
                ])
504
                ->getResult();
505
506
            $result = array_merge($result, $otherFields);
507
        }
508
509
        return array_map(fn(array $data) => $data['id'], $result);
510
    }
511
512
    #[Override]
513
    public function prototype(): StarSystemMap
514
    {
515
        return new StarSystemMap();
516
    }
517
518
    #[Override]
519
    public function save(StarSystemMap $starSystemMap): void
520
    {
521
        $em = $this->getEntityManager();
522
523
        $em->persist($starSystemMap);
524
    }
525
526
    #[Override]
527
    public function truncateByStarSystem(StarSystem $starSystem): void
528
    {
529
        $this->getEntityManager()->createQuery(
530
            sprintf(
531
                'DELETE FROM %s sm WHERE sm.systems_id = :systemId',
532
                StarSystemMap::class
533
            )
534
        )
535
            ->setParameters(['systemId' => $starSystem->getId()])
536
            ->execute();
537
    }
538
}
539