Passed
Push — dev ( 9894c7...511ebd )
by Nico
27:50 queued 11:44
created

MapRepository::isAdminRegionUserRegion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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