Passed
Push — dev ( 1ba1d5...ed0d06 )
by Nico
10:18
created

MapRepository::getRegionBorderData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 37
nc 1
nop 2
dl 0
loc 39
ccs 0
cts 13
cp 0
crap 2
rs 9.328
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 RuntimeException;
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\UserSettingEnum;
15
use Stu\Module\Starmap\Lib\ExploreableStarMap;
16
use Stu\Orm\Entity\LayerInterface;
17
use Stu\Orm\Entity\Location;
18
use Stu\Orm\Entity\Map;
19
use Stu\Orm\Entity\MapInterface;
20
use Stu\Orm\Entity\UserInterface;
21
22
/**
23
 * @extends EntityRepository<Map>
24
 */
25
final class MapRepository extends EntityRepository implements MapRepositoryInterface
26
{
27
    #[Override]
28
    public function getAmountByLayer(LayerInterface $layer): int
29
    {
30
        return $this->count([
31
            'layer_id' => $layer->getId()
32
        ]);
33
    }
34
35
    #[Override]
36
    public function getAllOrdered(int $layerId): array
37
    {
38
        return $this->getEntityManager()
39
            ->createQuery(
40
                sprintf(
41
                    'SELECT m FROM %s m
42
                    JOIN %s l
43
                    WITH m.id = l.id
44
                    WHERE l.layer_id = :layerId
45
                    ORDER BY l.cy, l.cx',
46
                    Map::class,
47
                    Location::class
48
                )
49
            )
50
            ->setParameters([
51
                'layerId' => $layerId
52
            ])
53
            ->getResult();
54
    }
55
56
    #[Override]
57
    public function getAllWithSystem(int $layerId): array
58
    {
59
        return $this->getEntityManager()
60
            ->createQuery(
61
                sprintf(
62
                    'SELECT m FROM %s m INDEX BY m.id
63
                    JOIN %s l
64
                    WITH m.id = l.id
65
                    WHERE l.layer_id = :layerId
66
                    AND m.systems_id IS NOT null',
67
                    Map::class,
68
                    Location::class
69
                )
70
            )
71
            ->setParameters([
72
                'layerId' => $layerId
73
            ])
74
            ->getResult();
75
    }
76
77
    #[Override]
78
    public function getAllWithoutSystem(int $layerId): array
79
    {
80
        return $this->getEntityManager()
81
            ->createQuery(
82
                sprintf(
83
                    'SELECT m FROM %s m INDEX BY m.id
84
                    JOIN %s l
85
                    WITH m.id = l.id
86
                    WHERE l.layer_id = :layerId
87
                    AND m.systems_id IS null',
88
                    Map::class,
89
                    Location::class
90
                )
91
            )
92
            ->setParameters([
93
                'layerId' => $layerId
94
            ])
95
            ->getResult();
96
    }
97
98 1
    #[Override]
99
    public function getByCoordinates(?LayerInterface $layer, int $cx, int $cy): ?MapInterface
100
    {
101 1
        if ($layer === null) {
102
            return null;
103
        }
104
105 1
        return $this->findOneBy([
106 1
            'layer_id' => $layer->getId(),
107 1
            'cx' => $cx,
108 1
            'cy' => $cy
109 1
        ]);
110
    }
111
112
    #[Override]
113
    public function getByBoundaries(PanelBoundaries $boundaries): array
114
    {
115
        return $this->getByCoordinateRange(
116
            $boundaries->getParentId(),
117
            $boundaries->getMinX(),
118
            $boundaries->getMaxX(),
119
            $boundaries->getMinY(),
120
            $boundaries->getMaxY()
121
        );
122
    }
123
124
    #[Override]
125
    public function getByCoordinateRange(
126
        int $layerId,
127
        int $startCx,
128
        int $endCx,
129
        int $startCy,
130
        int $endCy,
131
        bool $sortAscending = true
132
    ): array {
133
        return $this->getEntityManager()
134
            ->createQuery(
135
                sprintf(
136
                    'SELECT m FROM %s m
137
                    JOIN %s l
138
                    WITH m.id = l.id
139
                    WHERE l.cx BETWEEN :startCx AND :endCx
140
                    AND l.cy BETWEEN :startCy AND :endCy
141
                    AND l.layer_id = :layerId
142
                    ORDER BY l.cy %3$s, l.cx %3$s',
143
                    Map::class,
144
                    Location::class,
145
                    $sortAscending ? 'ASC' : 'DESC'
146
                )
147
            )
148
            ->setParameters([
149
                'layerId' => $layerId,
150
                'startCx' => $startCx,
151
                'endCx' => $endCx,
152
                'startCy' => $startCy,
153
                'endCy' => $endCy
154
            ])
155
            ->getResult();
156
    }
157
158
    #[Override]
159
    public function save(MapInterface $map): void
160
    {
161
        $em = $this->getEntityManager();
162
163
        $em->persist($map);
164
    }
165
166 1
    #[Override]
167
    public function getNormalBorderData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
168
    {
169 1
        return $this->getEntityManager()->createNativeQuery(
170 1
            'SELECT l.cx AS x, l.cy AS y
171
            FROM stu_map m
172
            JOIN stu_location l
173
            ON m.id = l.id
174
            WHERE l.cx BETWEEN :xStart AND :xEnd
175
            AND l.cy BETWEEN :yStart AND :yEnd
176 1
            AND l.layer_id = :layerId',
177 1
            $rsm
178 1
        )->setParameters([
179 1
            'xStart' => $boundaries->getMinX(),
180 1
            'xEnd' => $boundaries->getMaxX(),
181 1
            'yStart' => $boundaries->getMinY(),
182 1
            'yEnd' => $boundaries->getMaxY(),
183 1
            'layerId' => $boundaries->getParentId()
184 1
        ])->getResult();
185
    }
186
187
    #[Override]
188
    public function getCartographingData(PanelBoundaries $boundaries, ResultSetMapping $rsm, string $locations): array
189
    {
190
191
        return $this->getEntityManager()->createNativeQuery(
192
            'SELECT DISTINCT 
193
                l.cx AS x, 
194
                l.cy AS y, 
195
                CASE 
196
                    WHEN POSITION(l.id::TEXT IN :fieldIds) > 0 THEN TRUE ELSE FALSE
197
                END AS cartographing
198
            FROM stu_location l
199
            WHERE l.cx BETWEEN :xStart AND :xEnd
200
              AND l.cy BETWEEN :yStart AND :yEnd
201
              AND l.layer_id = :layerId
202
            ORDER BY cartographing DESC',
203
            $rsm
204
        )->setParameters([
205
            'xStart' => $boundaries->getMinX(),
206
            'xEnd' => $boundaries->getMaxX(),
207
            'yStart' => $boundaries->getMinY(),
208
            'yEnd' => $boundaries->getMaxY(),
209
            'layerId' => $boundaries->getParentId(),
210
            'fieldIds' => $locations
211
        ])->getResult();
212
    }
213
214
215
    #[Override]
216
    public function getRegionBorderData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
217
    {
218
        return $this->getEntityManager()->createNativeQuery(
219
            'SELECT l.cx AS x, l.cy AS y,
220
                (SELECT al.rgb_code FROM stu_alliances al
221
                    JOIN stu_user u ON al.id = u.allys_id
222
                    JOIN stu_spacecraft sc ON u.id = sc.user_id
223
                    JOIN stu_station s ON sc.id = s.id
224
                    JOIN stu_map ma ON ma.influence_area_id = s.influence_area_id
225
                    WHERE ma.id = m.id AND ma.bordertype_id IS NULL AND ma.admin_region_id IS NULL)
226
                            AS allycolor,
227
                (SELECT COALESCE(us.value, \'\') FROM stu_user u
228
                    LEFT JOIN stu_user_setting us ON u.id = us.user_id
229
                    JOIN stu_spacecraft sc ON u.id = sc.user_id
230
                    JOIN stu_station s ON sc.id = s.id
231
                    JOIN stu_map mu ON mu.influence_area_id = s.influence_area_id
232
                    WHERE us.setting = :rgbCodeSetting
233
                    AND mu.id = m.id AND mu.bordertype_id IS NULL AND mu.admin_region_id IS NULL)
234
                        as usercolor,
235
                (SELECT mbt.color FROM stu_map_bordertypes mbt
236
                    JOIN stu_map mb ON mb.bordertype_id = mbt.id
237
                    WHERE mb.id = m.id AND mb.bordertype_id IS NOT NULL)
238
                        AS factioncolor
239
            FROM stu_map m
240
            JOIN stu_location l
241
            ON m.id = l.id
242
            WHERE l.cx BETWEEN :xStart AND :xEnd
243
            AND l.cy BETWEEN :yStart AND :yEnd
244
            AND l.layer_id = :layerId',
245
            $rsm
246
        )->setParameters([
247
            'xStart' => $boundaries->getMinX(),
248
            'xEnd' => $boundaries->getMaxX(),
249
            'yStart' => $boundaries->getMinY(),
250
            'yEnd' => $boundaries->getMaxY(),
251
            'layerId' => $boundaries->getParentId(),
252
            'rgbCodeSetting' => UserSettingEnum::RGB_CODE->value
253
        ])->getResult();
254
    }
255
256
    #[Override]
257
    public function getImpassableBorderData(PanelBoundaries $boundaries, Userinterface $user, ResultSetMapping $rsm): array
258
    {
259
        return $this->getEntityManager()->createNativeQuery(
260
            'SELECT DISTINCT 
261
                l.cx AS x, 
262
                l.cy AS y,
263
                CASE 
264
                    WHEN mf.passable = FALSE 
265
                         AND EXISTS (
266
                            SELECT 1 
267
                            FROM stu_database_user du
268
                            JOIN stu_database_entrys de ON du.database_id = de.id
269
                            WHERE du.user_id = :userId
270
                            AND de.id = r.database_id
271
                         ) 
272
                    THEN FALSE
273
                    ELSE TRUE
274
                END AS impassable
275
            FROM stu_location l
276
            LEFT JOIN stu_map_ftypes mf ON l.field_id = mf.id
277
            LEFT JOIN stu_map m ON l.id = m.id
278
            LEFT JOIN stu_map_regions r ON m.region_id = r.id
279
            WHERE l.cx BETWEEN :xStart AND :xEnd
280
              AND l.cy BETWEEN :yStart AND :yEnd
281
              AND l.layer_id = :layerId',
282
            $rsm
283
        )->setParameters([
284
            'xStart' => $boundaries->getMinX(),
285
            'xEnd' => $boundaries->getMaxX(),
286
            'yStart' => $boundaries->getMinY(),
287
            'yEnd' => $boundaries->getMaxY(),
288
            'layerId' => $boundaries->getParentId(),
289
            'userId' => $user->getId()
290
        ])->getResult();
291
    }
292
293
294
    #[Override]
295
    public function getAnomalyData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
296
    {
297
        return $this->getEntityManager()->createNativeQuery(
298
            'SELECT l.cx AS x, l.cy AS y,
299
                (SELECT array_to_string(array(SELECT a.anomaly_type_id FROM stu_anomaly a WHERE a.location_id = m.id), \',\')) as anomalytypes
300
            FROM stu_map m
301
            JOIN stu_location l
302
            ON m.id = l.id
303
            WHERE l.cx BETWEEN :xStart AND :xEnd
304
            AND l.cy BETWEEN :yStart AND :yEnd
305
            AND l.layer_id = :layerId',
306
            $rsm
307
        )->setParameters([
308
            'xStart' => $boundaries->getMinX(),
309
            'xEnd' => $boundaries->getMaxX(),
310
            'yStart' => $boundaries->getMinY(),
311
            'yEnd' => $boundaries->getMaxY(),
312
            'layerId' => $boundaries->getParentId(),
313
        ])->getResult();
314
    }
315
316 1
    #[Override]
317
    public function getSpacecraftCountLayerData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
318
    {
319 1
        return $this->getEntityManager()->createNativeQuery(
320 1
            'SELECT l.cx as x, l.cy AS y, mft.effects as effects,
321
                (SELECT count(DISTINCT b.id) FROM stu_spacecraft b
322
                    JOIN stu_location l2
323
                    ON b.location_id = l2.id
324
                    WHERE l2.layer_id = l.layer_id 
325
                    AND l2.cx = l.cx
326
                    AND l2.cy = l.cy
327
                    AND NOT EXISTS (SELECT ss.id
328
                                        FROM stu_spacecraft_system ss
329
                                        WHERE b.id = ss.spacecraft_id
330
                                        AND ss.system_type = :cloakSystemId
331
                                        AND ss.mode > 1)) AS spacecraftcount,
332
                (SELECT count(DISTINCT c.id) FROM stu_spacecraft c
333
                    JOIN stu_location l2
334
                    ON c.location_id = l2.id
335
                    WHERE l2.layer_id = l.layer_id 
336
                    AND l2.cx = l.cx
337
                    AND l2.cy = l.cy
338
                    AND EXISTS (SELECT ss2.id
339
                                        FROM stu_spacecraft_system ss2
340
                                        WHERE c.id = ss2.spacecraft_id
341
                                        AND ss2.system_type = :cloakSystemId
342
                                        AND ss2.mode > 1)) AS cloakcount
343
            FROM stu_map m
344
            JOIN stu_location l
345
            ON m.id = l.id
346
            JOIN stu_map_ftypes mft
347
            ON l.field_id = mft.id
348
            WHERE l.cx BETWEEN :xStart AND :xEnd
349
            AND l.cy BETWEEN :yStart AND :yEnd
350 1
            AND l.layer_id = :layerId',
351 1
            $rsm
352 1
        )->setParameters([
353 1
            'xStart' => $boundaries->getMinX(),
354 1
            'xEnd' => $boundaries->getMaxX(),
355 1
            'yStart' => $boundaries->getMinY(),
356 1
            'yEnd' => $boundaries->getMaxY(),
357 1
            'layerId' => $boundaries->getParentId(),
358 1
            'cloakSystemId' => SpacecraftSystemTypeEnum::CLOAK->value
359 1
        ])->getResult();
360
    }
361
362
363 1
    #[Override]
364
    public function getMapLayerData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
365
    {
366 1
        return $this->getEntityManager()->createNativeQuery(
367 1
            'SELECT l.cx as x, l.cy AS y, ft.type
368
                FROM stu_map m
369
                JOIN stu_location l
370
                ON m.id = l.id
371
                JOIN stu_map_ftypes ft ON ft.id = l.field_id
372
                WHERE l.cx BETWEEN :xStart AND :xEnd AND l.cy BETWEEN :yStart AND :yEnd
373 1
                AND l.layer_id = :layerId',
374 1
            $rsm
375 1
        )->setParameters([
376 1
            'xStart' => $boundaries->getMinX(),
377 1
            'xEnd' => $boundaries->getMaxX(),
378 1
            'yStart' => $boundaries->getMinY(),
379 1
            'yEnd' => $boundaries->getMaxY(),
380 1
            'layerId' => $boundaries->getParentId(),
381 1
        ])->getResult();
382
    }
383
384
    #[Override]
385
    public function getUserSpacecraftCountLayerData(PanelBoundaries $boundaries, int $userId, ResultSetMapping $rsm): array
386
    {
387
        return $this->getEntityManager()->createNativeQuery(
388
            'SELECT l.cx as x, l.cy as y,
389
            (SELECT count(distinct s.id)
390
                FROM stu_spacecraft s
391
                JOIN stu_location spl
392
                ON s.location_id = spl.id
393
                WHERE spl.cx = l.cx
394
                AND spl.cy = l.cy
395
                AND spl.layer_id = l.layer_id
396
                AND s.user_id = :userId) as spacecraftcount
397
            FROM stu_map m
398
            JOIN stu_location l
399
            ON m.id = l.id
400
            WHERE l.cx BETWEEN :xStart AND :xEnd
401
            AND l.cy BETWEEN :yStart AND :yEnd
402
            AND l.layer_id = :layerId',
403
            $rsm
404
        )->setParameters([
405
            'xStart' => $boundaries->getMinX(),
406
            'xEnd' => $boundaries->getMaxX(),
407
            'yStart' => $boundaries->getMinY(),
408
            'yEnd' => $boundaries->getMaxY(),
409
            'layerId' => $boundaries->getParentId(),
410
            'userId' => $userId
411
        ])->getResult();
412
    }
413
414
    #[Override]
415
    public function getAllianceSpacecraftCountLayerData(PanelBoundaries $boundaries, int $allianceId, ResultSetMapping $rsm): array
416
    {
417
        return $this->getEntityManager()->createNativeQuery(
418
            'SELECT l.cx as x, l.cy as y,
419
             (SELECT count(distinct s.id)
420
                    FROM stu_spacecraft s
421
                    JOIN stu_location spl
422
                    ON s.location_id = spl.id
423
                    JOIN stu_user u
424
                    ON s.user_id = u.id
425
                    WHERE spl.cx = l.cx
426
                    AND spl.cy = l.cy
427
                    AND spl.layer_id = l.layer_id
428
                    AND u.allys_id = :allyId) as spacecraftcount
429
            FROM stu_map m
430
            JOIN stu_location l
431
            ON m.id = l.id
432
            WHERE l.cx BETWEEN :xStart AND :xEnd
433
            AND l.cy BETWEEN :yStart AND :yEnd
434
            AND l.layer_id = :layerId',
435
            $rsm
436
        )->setParameters([
437
            'xStart' => $boundaries->getMinX(),
438
            'xEnd' => $boundaries->getMaxX(),
439
            'yStart' => $boundaries->getMinY(),
440
            'yEnd' => $boundaries->getMaxY(),
441
            'layerId' => $boundaries->getParentId(),
442
            'allyId' => $allianceId
443
        ])->getResult();
444
    }
445
446
    #[Override]
447
    public function getSpacecraftCountLayerDataForSpacecraft(PanelBoundaries $boundaries, int $spacecraftId, ResultSetMapping $rsm): array
448
    {
449
        return $this->getEntityManager()->createNativeQuery(
450
            'SELECT l.cx as x, l.cy as y,
451
            (SELECT count(distinct s.id)
452
                FROM stu_spacecraft s
453
                JOIN stu_location spl
454
                ON s.location_id = spl.id
455
                WHERE spl.cx = l.cx
456
                AND spl.cy = l.cy
457
                AND spl.layer_id = l.layer_id
458
                AND s.id = :spacecraftId) as spacecraftcount
459
            FROM stu_map m
460
            JOIN stu_location l
461
            ON m.id = l.id
462
            WHERE l.cx BETWEEN :xStart AND :xEnd
463
            AND l.cy BETWEEN :yStart AND :yEnd
464
            AND l.layer_id = :layerId',
465
            $rsm
466
        )->setParameters([
467
            'xStart' => $boundaries->getMinX(),
468
            'xEnd' => $boundaries->getMaxX(),
469
            'yStart' => $boundaries->getMinY(),
470
            'yEnd' => $boundaries->getMaxY(),
471
            'layerId' => $boundaries->getParentId(),
472
            'spacecraftId' => $spacecraftId
473
        ])->getResult();
474
    }
475
476 3
    #[Override]
477
    public function getExplored(int $userId, int $layerId, int $startX, int $endX, int $cy): array
478
    {
479 3
        $rsm = new ResultSetMapping();
480 3
        $rsm->addEntityResult(ExploreableStarMap::class, 'm');
481 3
        $rsm->addFieldResult('m', 'id', 'id');
482 3
        $rsm->addFieldResult('m', 'cx', 'cx');
483 3
        $rsm->addFieldResult('m', 'cy', 'cy');
484 3
        $rsm->addFieldResult('m', 'field_id', 'field_id');
485 3
        $rsm->addFieldResult('m', 'bordertype_id', 'bordertype_id');
486 3
        $rsm->addFieldResult('m', 'user_id', 'user_id');
487 3
        $rsm->addFieldResult('m', 'mapped', 'mapped');
488 3
        $rsm->addFieldResult('m', 'system_name', 'system_name');
489 3
        $rsm->addFieldResult('m', 'influence_area_id', 'influence_area_id');
490 3
        $rsm->addFieldResult('m', 'region_id', 'region_id');
491 3
        $rsm->addFieldResult('m', 'tradepost_id', 'tradepost_id');
492 3
        $rsm->addFieldResult('m', 'region_description', 'region_description');
493 3
        $rsm->addFieldResult('m', 'layer_id', 'layer_id');
494
495 3
        return $this->getEntityManager()
496 3
            ->createNativeQuery(
497 3
                'SELECT m.id, l.cx, l.cy, l.field_id, m.systems_id, m.bordertype_id, um.user_id,
498
                    dbu.database_id as mapped, m.influence_area_id as influence_area_id, m.admin_region_id as region_id,
499
                    sys.name as system_name, l.layer_id,
500
                    (SELECT tp.id FROM stu_spacecraft s JOIN stu_trade_posts tp ON s.id = tp.station_id WHERE s.location_id = m.id) as tradepost_id,
501
                    (SELECT mr.description FROM stu_map_regions mr JOIN stu_database_user dbu on dbu.user_id = :userId and mr.database_id = dbu.database_id WHERE m.region_id = mr.id) as region_description
502
                FROM stu_map m
503
                JOIN stu_location l
504
                ON m.id = l.id
505
                LEFT JOIN stu_user_map um
506
                    ON um.cy = l.cy AND um.cx = l.cx AND um.user_id = :userId AND um.layer_id = l.layer_id
507
                LEFT JOIN stu_systems sys
508
                    ON m.systems_id = sys.id
509
                LEFT JOIN stu_database_user dbu
510
                    ON dbu.user_id = :userId
511
                    AND sys.database_id = dbu.database_id
512
                WHERE l.cx BETWEEN :startX AND :endX
513
                AND l.cy = :cy
514
                AND l.layer_id = :layerId
515 3
                ORDER BY l.cx ASC',
516 3
                $rsm
517 3
            )
518 3
            ->setParameters([
519 3
                'layerId' => $layerId,
520 3
                'userId' => $userId,
521 3
                'startX' => $startX,
522 3
                'endX' => $endX,
523 3
                'cy' => $cy
524 3
            ])
525 3
            ->getResult();
526
    }
527
528
    #[Override]
529
    public function getWithEmptySystem(LayerInterface $layer): array
530
    {
531
        return $this->getEntityManager()
532
            ->createQuery(
533
                sprintf(
534
                    'SELECT m from %s m
535
                    JOIN %s l
536
                    WITH m.id = l.id
537
                    WHERE m.system_type_id IS NOT NULL
538
                    AND m.systems_id IS NULL
539
                    AND l.layer = :layer',
540
                    Map::class,
541
                    Location::class
542
                )
543
            )
544
            ->setParameters([
545
                'layer' => $layer
546
            ])
547
            ->getResult();
548
    }
549
550
    #[Override]
551
    public function getRandomMapIdsForAstroMeasurement(int $regionId, int $maxPercentage, int $location): array
552
    {
553
        $rsm = new ResultSetMapping();
554
        $rsm->addScalarResult('id', 'id', 'integer');
555
556
        $mapIdResultSet = $this->getEntityManager()
557
            ->createNativeQuery(
558
                'SELECT m.id FROM stu_map m
559
                JOIN stu_location l
560
                ON m.id = l.id
561
                JOIN stu_map_ftypes mf
562
                ON l.field_id = mf.id
563
                WHERE m.region_id = :regionId
564
                AND mf.passable = :true
565
                AND m.id != :loc
566
                ORDER BY RANDOM()',
567
                $rsm
568
            )
569
            ->setParameters([
570
                'regionId' => $regionId,
571
                'loc' => $location,
572
                'true' => true
573
            ])
574
            ->getResult();
575
576
        $amount = (int)ceil(count($mapIdResultSet) * $maxPercentage / 100);
577
        $subset = array_slice($mapIdResultSet, 0, $amount);
578
579
        return array_map(fn(array $data) => $data['id'], $subset);
580
    }
581
582
    #[Override]
583
    public function getRandomPassableUnoccupiedWithoutDamage(LayerInterface $layer, bool $isAtBorder = false): MapInterface
584
    {
585
        $rsm = new ResultSetMapping();
586
        $rsm->addScalarResult('id', 'id', 'integer');
587
588
        $borderCriteria = $isAtBorder ?
589
            sprintf(
590
                'AND (l.cx in (1, %d) OR l.cy in (1, %d))',
591
                $layer->getWidth(),
592
                $layer->getHeight()
593
            ) : '';
594
595
        $randomMapId =  (int)$this->getEntityManager()
596
            ->createNativeQuery(
597
                sprintf(
598
                    'SELECT m.id
599
                    FROM stu_map m
600
                    JOIN stu_location l
601
                    ON m.id = l.id
602
                    JOIN stu_map_ftypes mft
603
                    ON l.field_id = mft.id
604
                    WHERE NOT EXISTS (SELECT s.id FROM stu_spacecraft s WHERE s.location_id = m.id)
605
                    AND l.layer_id = :layerId
606
                    AND mft.x_damage = 0
607
                    AND mft.passable = :true
608
                    %s
609
                    ORDER BY RANDOM()
610
                    LIMIT 1',
611
                    $borderCriteria
612
                ),
613
                $rsm
614
            )
615
            ->setParameters([
616
                'layerId' => $layer->getId(),
617
                'true' => true
618
            ])
619
            ->getSingleScalarResult();
620
621
        $map = $this->find($randomMapId);
622
        if ($map === null) {
623
            throw new RuntimeException('this should not happen');
624
        }
625
626
        return $map;
627
    }
628
629
    #[Override]
630
    public function getIgnoringSubspaceLayerData(PanelBoundaries $boundaries, int $ignoreUserId, ResultSetMapping $rsm): array
631
    {
632
        $maxAge = time() - FlightSignatureVisibilityEnum::SIG_VISIBILITY_UNCLOAKED;
633
634
        return $this->getEntityManager()->createNativeQuery(
635
            'SELECT l.cx AS x, l.cy AS y, mft.effects as effects,
636
                (SELECT count(distinct fs1.ship_id) from stu_flight_sig fs1
637
                WHERE fs1.location_id = l.id
638
                AND fs1.user_id != :ignoreUserId
639
                AND (fs1.from_direction = 1 OR fs1.to_direction = 1)
640
                AND fs1.time > :timeThreshold) as d1c,
641
                (SELECT count(distinct fs2.ship_id) from stu_flight_sig fs2
642
                WHERE fs2.location_id = l.id
643
                AND fs2.user_id !=:ignoreUserId
644
                AND (fs2.from_direction = 2 OR fs2.to_direction = 2)
645
                AND fs2.time > :timeThreshold) as d2c,
646
                (SELECT count(distinct fs3.ship_id) from stu_flight_sig fs3
647
                WHERE fs3.location_id = l.id
648
                AND fs3.user_id != :ignoreUserId
649
                AND (fs3.from_direction = 3 OR fs3.to_direction = 3)
650
                AND fs3.time > :timeThreshold) as d3c,
651
                (SELECT count(distinct fs4.ship_id) from stu_flight_sig fs4
652
                WHERE fs4.location_id = l.id
653
                AND fs4.user_id != :ignoreUserId
654
                AND (fs4.from_direction = 4 OR fs4.to_direction = 4)
655
                AND fs4.time > :timeThreshold) as d4c 
656
                FROM stu_location l
657
                JOIN stu_map m
658
                ON l.id = m.id
659
                JOIN stu_map_ftypes mft
660
                ON l.field_id = mft.id
661
                WHERE l.cx BETWEEN :xStart AND :xEnd
662
                AND l.cy BETWEEN :yStart AND :yEnd
663
                AND l.layer_id = :layerId',
664
            $rsm
665
        )->setParameters([
666
            'xStart' => $boundaries->getMinX(),
667
            'xEnd' => $boundaries->getMaxX(),
668
            'yStart' => $boundaries->getMinY(),
669
            'yEnd' => $boundaries->getMaxY(),
670
            'layerId' => $boundaries->getParentId(),
671
            'ignoreUserId' => $ignoreUserId,
672
            'timeThreshold' => $maxAge
673
        ])->getResult();
674
    }
675
676
    #[Override]
677
    public function getSubspaceLayerData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
678
    {
679
        return $this->getEntityManager()->createNativeQuery(
680
            'SELECT l.cx as x, l.cy as y, mft.effects as effects,
681
            (SELECT count(distinct fs1.ship_id) from stu_flight_sig fs1
682
                WHERE fs1.location_id = l.id
683
                AND (fs1.from_direction = 1 OR fs1.to_direction = 1)) as d1c,
684
            (SELECT count(distinct fs2.ship_id) from stu_flight_sig fs2
685
                WHERE fs2.location_id = l.id
686
                AND (fs2.from_direction = 2 OR fs2.to_direction = 2)) as d2c,
687
            (SELECT count(distinct fs3.ship_id) from stu_flight_sig fs3
688
                WHERE fs3.location_id = l.id
689
                AND (fs3.from_direction = 3 OR fs3.to_direction = 3)) as d3c,
690
            (SELECT count(distinct fs4.ship_id) from stu_flight_sig fs4
691
                WHERE fs4.location_id = l.id
692
                AND (fs4.from_direction = 4 OR fs4.to_direction = 4)) as d4c 
693
            FROM stu_location l
694
            JOIN stu_map_ftypes mft
695
            ON l.field_id = mft.id
696
            WHERE l.cx BETWEEN :xStart AND :xEnd
697
            AND l.cy BETWEEN :yStart AND :yEnd
698
            AND l.layer_id = :layerId',
699
            $rsm
700
        )->setParameters([
701
            'xStart' => $boundaries->getMinX(),
702
            'xEnd' => $boundaries->getMaxX(),
703
            'yStart' => $boundaries->getMinY(),
704
            'yEnd' => $boundaries->getMaxY(),
705
            'layerId' => $boundaries->getParentId()
706
        ])->getResult();
707
    }
708
709
    #[Override]
710
    public function getUserSubspaceLayerData(PanelBoundaries $boundaries, int $userId, ResultSetMapping $rsm): array
711
    {
712
        return $this->getEntityManager()->createNativeQuery(
713
            'SELECT l.cx as x, l.cy as y,
714
            (SELECT count(distinct fs1.ship_id) from stu_flight_sig fs1
715
                WHERE fs1.location_id = l.id
716
                AND fs1.user_id = :userId
717
                AND (fs1.from_direction = 1 OR fs1.to_direction = 1)) as d1c,
718
            (SELECT count(distinct fs2.ship_id) from stu_flight_sig fs2
719
                WHERE fs2.location_id = l.id
720
                AND fs2.user_id = :userId
721
                AND (fs2.from_direction = 2 OR fs2.to_direction = 2)) as d2c,
722
            (SELECT count(distinct fs3.ship_id) from stu_flight_sig fs3
723
                WHERE fs3.location_id = l.id
724
                AND fs3.user_id = :userId
725
                AND (fs3.from_direction = 3 OR fs3.to_direction = 3)) as d3c,
726
            (SELECT count(distinct fs4.ship_id) from stu_flight_sig fs4
727
                WHERE fs4.location_id = l.id
728
                AND fs4.user_id = :userId
729
                AND (fs4.from_direction = 4 OR fs4.to_direction = 4)) as d4c 
730
            FROM stu_location l
731
            WHERE l.cx BETWEEN :xStart AND :xEnd
732
            AND l.cy BETWEEN :yStart AND :yEnd
733
            AND l.layer_id = :layerId',
734
            $rsm
735
        )->setParameters([
736
            'xStart' => $boundaries->getMinX(),
737
            'xEnd' => $boundaries->getMaxX(),
738
            'yStart' => $boundaries->getMinY(),
739
            'yEnd' => $boundaries->getMaxY(),
740
            'layerId' => $boundaries->getParentId(),
741
            'userId' => $userId
742
        ])->getResult();
743
    }
744
745
    #[Override]
746
    public function getShipSubspaceLayerData(PanelBoundaries $boundaries, int $shipId, ResultSetMapping $rsm): array
747
    {
748
        return $this->getEntityManager()->createNativeQuery(
749
            'SELECT l.cx as x, l.cy as y,
750
            (SELECT count(distinct fs1.ship_id) from stu_flight_sig fs1
751
                WHERE fs1.location_id = l.id
752
                AND fs1.ship_id = :shipId
753
                AND (fs1.from_direction = 1 OR fs1.to_direction = 1)) as d1c,
754
            (SELECT count(distinct fs2.ship_id) from stu_flight_sig fs2
755
                WHERE fs2.location_id = l.id
756
                AND fs2.ship_id = :shipId
757
                AND (fs2.from_direction = 2 OR fs2.to_direction = 2)) as d2c,
758
            (SELECT count(distinct fs3.ship_id) from stu_flight_sig fs3
759
                WHERE fs3.location_id = l.id
760
                AND fs3.ship_id = :shipId
761
                AND (fs3.from_direction = 3 OR fs3.to_direction = 3)) as d3c,
762
            (SELECT count(distinct fs4.ship_id) from stu_flight_sig fs4
763
                WHERE fs4.location_id = l.id
764
                AND fs4.ship_id = :shipId
765
                AND (fs4.from_direction = 4 OR fs4.to_direction = 4)) as d4c 
766
            FROM stu_location l
767
            WHERE l.cx BETWEEN :xStart AND :xEnd
768
            AND l.cy BETWEEN :yStart AND :yEnd
769
            AND l.layer_id = :layerId',
770
            $rsm
771
        )->setParameters([
772
            'xStart' => $boundaries->getMinX(),
773
            'xEnd' => $boundaries->getMaxX(),
774
            'yStart' => $boundaries->getMinY(),
775
            'yEnd' => $boundaries->getMaxY(),
776
            'layerId' => $boundaries->getParentId(),
777
            'shipId' => $shipId
778
        ])->getResult();
779
    }
780
781
    #[Override]
782
    public function getAllianceSubspaceLayerData(PanelBoundaries $boundaries, int $allianceId, ResultSetMapping $rsm): array
783
    {
784
        return $this->getEntityManager()->createNativeQuery(
785
            'SELECT l.id, l.cx as x, l.cy as y,
786
            (SELECT count(distinct fs1.ship_id) from stu_flight_sig fs1
787
                JOIN stu_user u1 ON fs1.user_id = u1.id
788
                WHERE fs1.location_id = l.id
789
                AND u1.allys_id = :allyId
790
                AND (fs1.from_direction = 1 OR fs1.to_direction = 1)) as d1c,
791
            (SELECT count(distinct fs2.ship_id) from stu_flight_sig fs2
792
                JOIN stu_user u2 ON fs2.user_id = u2.id
793
                WHERE fs2.location_id = l.id
794
                AND u2.allys_id = :allyId
795
                AND (fs2.from_direction = 2 OR fs2.to_direction = 2)) as d2c,
796
            (SELECT count(distinct fs3.ship_id) from stu_flight_sig fs3
797
                JOIN stu_user u3 ON fs3.user_id = u3.id
798
                WHERE fs3.location_id = l.id
799
                AND u3.allys_id = :allyId
800
                AND (fs3.from_direction = 3 OR fs3.to_direction = 3)) as d3c,
801
            (SELECT count(distinct fs4.ship_id) from stu_flight_sig fs4
802
                JOIN stu_user u4 ON fs4.user_id = u4.id
803
                WHERE fs4.location_id = l.id
804
                AND u4.allys_id = :allyId
805
                AND (fs4.from_direction = 4 OR fs4.to_direction = 4)) as d4c 
806
            FROM stu_location l
807
            WHERE l.cx BETWEEN :xStart AND :xEnd
808
            AND l.cy BETWEEN :yStart AND :yEnd
809
            AND l.layer_id = :layerId',
810
            $rsm
811
        )->setParameters([
812
            'xStart' => $boundaries->getMinX(),
813
            'xEnd' => $boundaries->getMaxX(),
814
            'yStart' => $boundaries->getMinY(),
815
            'yEnd' => $boundaries->getMaxY(),
816
            'layerId' => $boundaries->getParentId(),
817
            'allyId' => $allianceId
818
        ])->getResult();
819
    }
820
821 1
    public function getUniqueInfluenceAreaIds(): array
822
    {
823 1
        $rsm = new ResultSetMapping();
824 1
        $rsm->addScalarResult('influence_area_id', 'influence_area_id', 'integer');
825
826 1
        $query = $this->getEntityManager()->createNativeQuery(
827 1
            'SELECT DISTINCT influence_area_id
828
             FROM stu_map
829
             WHERE influence_area_id IS NOT NULL
830 1
             ORDER BY influence_area_id ASC',
831 1
            $rsm
832 1
        );
833
834 1
        return array_map('intval', array_column($query->getResult(), 'influence_area_id'));
835
    }
836
}