Passed
Push — master ( 6d626d...5de675 )
by Janko
09:20
created

MapRepository::getLssBlockadeLocations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 26
nc 1
nop 1
dl 0
loc 29
ccs 16
cts 16
cp 1
crap 1
rs 9.504
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\FieldTypeEffectEnum;
14
use Stu\Lib\Map\VisualPanel\PanelBoundaries;
15
use Stu\Module\PlayerSetting\Lib\UserSettingEnum;
16
use Stu\Module\Starmap\Lib\ExploreableStarMap;
17
use Stu\Orm\Entity\Layer;
18
use Stu\Orm\Entity\Location;
19
use Stu\Orm\Entity\Map;
20
use Stu\Orm\Entity\User;
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(Layer $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(?Layer $layer, int $cx, int $cy): ?Map
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(Map $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, User $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 getLssBlockadeLocations(PanelBoundaries $boundaries): array
323
    {
324 1
        $rsm = new ResultSetMapping();
325 1
        $rsm->addScalarResult('cx', 'x', 'integer');
326 1
        $rsm->addScalarResult('cy', 'y', 'integer');
327 1
        $rsm->addScalarResult('effects', 'effects', 'string');
328
329 1
        return $this->getEntityManager()->createNativeQuery(
330 1
            'WITH bbox AS (
331
                SELECT id, field_id, cx, cy
332
                FROM stu_location
333
                WHERE layer_id = :layerId
334
                AND  cx BETWEEN :xStart AND :xEnd
335
                AND  cy BETWEEN :yStart AND :yEnd
336
                and discr = \'map\'
337
            )
338
            SELECT  l.cx, l.cy, mft.effects
339
            FROM bbox l
340
            JOIN stu_map_ftypes mft ON mft.id = l.field_id
341 1
            JOIN stu_map m ON m.id = l.id',
342 1
            $rsm
343 1
        )->setParameters([
344 1
            'xStart' => $boundaries->getMinX(),
345 1
            'xEnd' => $boundaries->getMaxX(),
346 1
            'yStart' => $boundaries->getMinY(),
347 1
            'yEnd' => $boundaries->getMaxY(),
348 1
            'layerId' => $boundaries->getParentId()
349 1
        ])->getResult();
350
    }
351
352 1
    #[Override]
353
    public function getSpacecraftCountLayerData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
354
    {
355 1
        return $this->getEntityManager()->createNativeQuery(
356 1
            'SELECT l.cx as x, l.cy AS y, mft.effects as effects,
357
                (SELECT count(DISTINCT b.id) FROM stu_spacecraft b
358
                    JOIN stu_location l2
359
                    ON b.location_id = l2.id
360
                    WHERE l2.layer_id = l.layer_id 
361
                    AND l2.cx = l.cx
362
                    AND l2.cy = l.cy
363
                    AND NOT EXISTS (SELECT ss.id
364
                                        FROM stu_spacecraft_system ss
365
                                        WHERE b.id = ss.spacecraft_id
366
                                        AND ss.system_type = :cloakSystemId
367
                                        AND ss.mode > 1)) AS spacecraftcount,
368
                (SELECT count(DISTINCT c.id) FROM stu_spacecraft c
369
                    JOIN stu_location l2
370
                    ON c.location_id = l2.id
371
                    WHERE l2.layer_id = l.layer_id 
372
                    AND l2.cx = l.cx
373
                    AND l2.cy = l.cy
374
                    AND EXISTS (SELECT ss2.id
375
                                        FROM stu_spacecraft_system ss2
376
                                        WHERE c.id = ss2.spacecraft_id
377
                                        AND ss2.system_type = :cloakSystemId
378
                                        AND ss2.mode > 1)) AS cloakcount
379
            FROM stu_map m
380
            JOIN stu_location l
381
            ON m.id = l.id
382
            JOIN stu_map_ftypes mft
383
            ON l.field_id = mft.id
384
            WHERE l.cx BETWEEN :xStart AND :xEnd
385
            AND l.cy BETWEEN :yStart AND :yEnd
386 1
            AND l.layer_id = :layerId',
387 1
            $rsm
388 1
        )->setParameters([
389 1
            'xStart' => $boundaries->getMinX(),
390 1
            'xEnd' => $boundaries->getMaxX(),
391 1
            'yStart' => $boundaries->getMinY(),
392 1
            'yEnd' => $boundaries->getMaxY(),
393 1
            'layerId' => $boundaries->getParentId(),
394 1
            'cloakSystemId' => SpacecraftSystemTypeEnum::CLOAK->value
395 1
        ])->getResult();
396
    }
397
398
399 1
    #[Override]
400
    public function getMapLayerData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
401
    {
402 1
        return $this->getEntityManager()->createNativeQuery(
403 1
            'SELECT l.cx as x, l.cy AS y, ft.type
404
                FROM stu_map m
405
                JOIN stu_location l
406
                ON m.id = l.id
407
                JOIN stu_map_ftypes ft ON ft.id = l.field_id
408
                WHERE l.cx BETWEEN :xStart AND :xEnd AND l.cy BETWEEN :yStart AND :yEnd
409 1
                AND l.layer_id = :layerId',
410 1
            $rsm
411 1
        )->setParameters([
412 1
            'xStart' => $boundaries->getMinX(),
413 1
            'xEnd' => $boundaries->getMaxX(),
414 1
            'yStart' => $boundaries->getMinY(),
415 1
            'yEnd' => $boundaries->getMaxY(),
416 1
            'layerId' => $boundaries->getParentId(),
417 1
        ])->getResult();
418
    }
419
420
    #[Override]
421
    public function getUserSpacecraftCountLayerData(PanelBoundaries $boundaries, int $userId, ResultSetMapping $rsm): array
422
    {
423
        return $this->getEntityManager()->createNativeQuery(
424
            'SELECT l.cx as x, l.cy as y,
425
            (SELECT count(distinct s.id)
426
                FROM stu_spacecraft s
427
                JOIN stu_location spl
428
                ON s.location_id = spl.id
429
                WHERE spl.cx = l.cx
430
                AND spl.cy = l.cy
431
                AND spl.layer_id = l.layer_id
432
                AND s.user_id = :userId) as spacecraftcount
433
            FROM stu_map m
434
            JOIN stu_location l
435
            ON m.id = l.id
436
            WHERE l.cx BETWEEN :xStart AND :xEnd
437
            AND l.cy BETWEEN :yStart AND :yEnd
438
            AND l.layer_id = :layerId',
439
            $rsm
440
        )->setParameters([
441
            'xStart' => $boundaries->getMinX(),
442
            'xEnd' => $boundaries->getMaxX(),
443
            'yStart' => $boundaries->getMinY(),
444
            'yEnd' => $boundaries->getMaxY(),
445
            'layerId' => $boundaries->getParentId(),
446
            'userId' => $userId
447
        ])->getResult();
448
    }
449
450
    #[Override]
451
    public function getAllianceSpacecraftCountLayerData(PanelBoundaries $boundaries, int $allianceId, ResultSetMapping $rsm): array
452
    {
453
        return $this->getEntityManager()->createNativeQuery(
454
            'SELECT l.cx as x, l.cy as y,
455
             (SELECT count(distinct s.id)
456
                    FROM stu_spacecraft s
457
                    JOIN stu_location spl
458
                    ON s.location_id = spl.id
459
                    JOIN stu_user u
460
                    ON s.user_id = u.id
461
                    WHERE spl.cx = l.cx
462
                    AND spl.cy = l.cy
463
                    AND spl.layer_id = l.layer_id
464
                    AND u.allys_id = :allyId) as spacecraftcount
465
            FROM stu_map m
466
            JOIN stu_location l
467
            ON m.id = l.id
468
            WHERE l.cx BETWEEN :xStart AND :xEnd
469
            AND l.cy BETWEEN :yStart AND :yEnd
470
            AND l.layer_id = :layerId',
471
            $rsm
472
        )->setParameters([
473
            'xStart' => $boundaries->getMinX(),
474
            'xEnd' => $boundaries->getMaxX(),
475
            'yStart' => $boundaries->getMinY(),
476
            'yEnd' => $boundaries->getMaxY(),
477
            'layerId' => $boundaries->getParentId(),
478
            'allyId' => $allianceId
479
        ])->getResult();
480
    }
481
482
    #[Override]
483
    public function getSpacecraftCountLayerDataForSpacecraft(PanelBoundaries $boundaries, int $spacecraftId, ResultSetMapping $rsm): array
484
    {
485
        return $this->getEntityManager()->createNativeQuery(
486
            'SELECT l.cx as x, l.cy as y,
487
            (SELECT count(distinct s.id)
488
                FROM stu_spacecraft s
489
                JOIN stu_location spl
490
                ON s.location_id = spl.id
491
                WHERE spl.cx = l.cx
492
                AND spl.cy = l.cy
493
                AND spl.layer_id = l.layer_id
494
                AND s.id = :spacecraftId) as spacecraftcount
495
            FROM stu_map m
496
            JOIN stu_location l
497
            ON m.id = l.id
498
            WHERE l.cx BETWEEN :xStart AND :xEnd
499
            AND l.cy BETWEEN :yStart AND :yEnd
500
            AND l.layer_id = :layerId',
501
            $rsm
502
        )->setParameters([
503
            'xStart' => $boundaries->getMinX(),
504
            'xEnd' => $boundaries->getMaxX(),
505
            'yStart' => $boundaries->getMinY(),
506
            'yEnd' => $boundaries->getMaxY(),
507
            'layerId' => $boundaries->getParentId(),
508
            'spacecraftId' => $spacecraftId
509
        ])->getResult();
510
    }
511
512 3
    #[Override]
513
    public function getExplored(int $userId, int $layerId, int $startX, int $endX, int $cy): array
514
    {
515 3
        $rsm = new ResultSetMapping();
516 3
        $rsm->addEntityResult(ExploreableStarMap::class, 'm');
517 3
        $rsm->addFieldResult('m', 'id', 'id');
518 3
        $rsm->addFieldResult('m', 'cx', 'cx');
519 3
        $rsm->addFieldResult('m', 'cy', 'cy');
520 3
        $rsm->addFieldResult('m', 'field_id', 'field_id');
521 3
        $rsm->addFieldResult('m', 'bordertype_id', 'bordertype_id');
522 3
        $rsm->addFieldResult('m', 'user_id', 'user_id');
523 3
        $rsm->addFieldResult('m', 'mapped', 'mapped');
524 3
        $rsm->addFieldResult('m', 'system_name', 'system_name');
525 3
        $rsm->addFieldResult('m', 'influence_area_id', 'influence_area_id');
526 3
        $rsm->addFieldResult('m', 'region_id', 'region_id');
527 3
        $rsm->addFieldResult('m', 'tradepost_id', 'tradepost_id');
528 3
        $rsm->addFieldResult('m', 'region_description', 'region_description');
529 3
        $rsm->addFieldResult('m', 'layer_id', 'layer_id');
530
531 3
        return $this->getEntityManager()
532 3
            ->createNativeQuery(
533 3
                'SELECT m.id, l.cx, l.cy, l.field_id, m.systems_id, m.bordertype_id, um.user_id,
534
                    dbu.database_id as mapped, m.influence_area_id as influence_area_id, m.admin_region_id as region_id,
535
                    sys.name as system_name, l.layer_id,
536
                    (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,
537
                    (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
538
                FROM stu_map m
539
                JOIN stu_location l
540
                ON m.id = l.id
541
                LEFT JOIN stu_user_map um
542
                    ON um.cx = l.cx AND um.cy = l.cy AND um.user_id = :userId AND um.layer_id = l.layer_id
543
                LEFT JOIN stu_systems sys
544
                    ON m.systems_id = sys.id
545
                LEFT JOIN stu_database_user dbu
546
                    ON dbu.user_id = :userId
547
                    AND sys.database_id = dbu.database_id
548
                WHERE l.cx BETWEEN :startX AND :endX
549
                AND l.cy = :cy
550
                AND l.layer_id = :layerId
551 3
                ORDER BY l.cx ASC',
552 3
                $rsm
553 3
            )
554 3
            ->setParameters([
555 3
                'layerId' => $layerId,
556 3
                'userId' => $userId,
557 3
                'startX' => $startX,
558 3
                'endX' => $endX,
559 3
                'cy' => $cy
560 3
            ])
561 3
            ->getResult();
562
    }
563
564
    #[Override]
565
    public function getWithEmptySystem(Layer $layer): array
566
    {
567
        return $this->getEntityManager()
568
            ->createQuery(
569
                sprintf(
570
                    'SELECT m from %s m
571
                    JOIN %s l
572
                    WITH m.id = l.id
573
                    WHERE m.system_type_id IS NOT NULL
574
                    AND m.systems_id IS NULL
575
                    AND l.layer = :layer',
576
                    Map::class,
577
                    Location::class
578
                )
579
            )
580
            ->setParameters([
581
                'layer' => $layer
582
            ])
583
            ->getResult();
584
    }
585
586
    #[Override]
587
    public function getRandomMapIdsForAstroMeasurement(int $regionId, int $maxPercentage, int $location): array
588
    {
589
        $rsm = new ResultSetMapping();
590
        $rsm->addScalarResult('id', 'id', 'integer');
591
592
        $mapIdResultSet = $this->getEntityManager()
593
            ->createNativeQuery(
594
                "SELECT m.id
595
                FROM stu_map m
596
                JOIN stu_location l ON m.id = l.id
597
                JOIN stu_map_ftypes mf ON l.field_id = mf.id
598
                WHERE m.region_id = :regionId
599
                AND 
600
                    mf.passable = :true
601
                    AND NOT EXISTS (
602
                        SELECT 1
603
                        FROM jsonb_array_elements_text(mf.effects::jsonb) AS elem
604
                        WHERE elem = 'NO_MEASUREPOINT'
605
                    
606
                )
607
                AND m.id != :loc
608
                ORDER BY RANDOM()",
609
                $rsm
610
            )
611
            ->setParameters([
612
                'regionId' => $regionId,
613
                'loc' => $location,
614
                'true' => true
615
            ])
616
            ->getResult();
617
618
        $amount = (int)ceil(count($mapIdResultSet) * $maxPercentage / 100);
619
        $subset = array_slice($mapIdResultSet, 0, $amount);
620
621
        return array_map(fn(array $data) => $data['id'], $subset);
622
    }
623
624
    #[Override]
625
    public function getRandomPassableUnoccupiedWithoutDamage(Layer $layer, bool $isAtBorder = false): Map
626
    {
627
        $rsm = new ResultSetMapping();
628
        $rsm->addScalarResult('id', 'id', 'integer');
629
630
        $borderCriteria = $isAtBorder ?
631
            sprintf(
632
                'AND (l.cx in (1, %d) OR l.cy in (1, %d))',
633
                $layer->getWidth(),
634
                $layer->getHeight()
635
            ) : '';
636
637
        $randomMapId =  (int)$this->getEntityManager()
638
            ->createNativeQuery(
639
                sprintf(
640
                    'SELECT m.id
641
                    FROM stu_map m
642
                    JOIN stu_location l
643
                    ON m.id = l.id
644
                    JOIN stu_map_ftypes mft
645
                    ON l.field_id = mft.id
646
                    WHERE NOT EXISTS (SELECT s.id FROM stu_spacecraft s WHERE s.location_id = m.id)
647
                    AND l.layer_id = :layerId
648
                    AND mft.x_damage = 0
649
                    AND mft.passable = :true
650
                    %s
651
                    ORDER BY RANDOM()
652
                    LIMIT 1',
653
                    $borderCriteria
654
                ),
655
                $rsm
656
            )
657
            ->setParameters([
658
                'layerId' => $layer->getId(),
659
                'true' => true
660
            ])
661
            ->getSingleScalarResult();
662
663
        $map = $this->find($randomMapId);
664
        if ($map === null) {
665
            throw new RuntimeException('this should not happen');
666
        }
667
668
        return $map;
669
    }
670
671
    #[Override]
672
    public function getIgnoringSubspaceLayerData(PanelBoundaries $boundaries, int $ignoreUserId, ResultSetMapping $rsm): array
673
    {
674
        $maxAge = time() - FlightSignatureVisibilityEnum::SIG_VISIBILITY_UNCLOAKED;
675
676
        return $this->getEntityManager()->createNativeQuery(
677
            'SELECT l.cx AS x, l.cy AS y, mft.effects as effects,
678
                (SELECT count(distinct fs1.ship_id) from stu_flight_sig fs1
679
                WHERE fs1.location_id = l.id
680
                AND fs1.user_id != :ignoreUserId
681
                AND (fs1.from_direction = 1 OR fs1.to_direction = 1)
682
                AND fs1.time > :timeThreshold) as d1c,
683
                (SELECT count(distinct fs2.ship_id) from stu_flight_sig fs2
684
                WHERE fs2.location_id = l.id
685
                AND fs2.user_id !=:ignoreUserId
686
                AND (fs2.from_direction = 2 OR fs2.to_direction = 2)
687
                AND fs2.time > :timeThreshold) as d2c,
688
                (SELECT count(distinct fs3.ship_id) from stu_flight_sig fs3
689
                WHERE fs3.location_id = l.id
690
                AND fs3.user_id != :ignoreUserId
691
                AND (fs3.from_direction = 3 OR fs3.to_direction = 3)
692
                AND fs3.time > :timeThreshold) as d3c,
693
                (SELECT count(distinct fs4.ship_id) from stu_flight_sig fs4
694
                WHERE fs4.location_id = l.id
695
                AND fs4.user_id != :ignoreUserId
696
                AND (fs4.from_direction = 4 OR fs4.to_direction = 4)
697
                AND fs4.time > :timeThreshold) as d4c 
698
                FROM stu_location l
699
                JOIN stu_map m
700
                ON l.id = m.id
701
                JOIN stu_map_ftypes mft
702
                ON l.field_id = mft.id
703
                WHERE l.cx BETWEEN :xStart AND :xEnd
704
                AND l.cy BETWEEN :yStart AND :yEnd
705
                AND l.layer_id = :layerId',
706
            $rsm
707
        )->setParameters([
708
            'xStart' => $boundaries->getMinX(),
709
            'xEnd' => $boundaries->getMaxX(),
710
            'yStart' => $boundaries->getMinY(),
711
            'yEnd' => $boundaries->getMaxY(),
712
            'layerId' => $boundaries->getParentId(),
713
            'ignoreUserId' => $ignoreUserId,
714
            'timeThreshold' => $maxAge
715
        ])->getResult();
716
    }
717
718
    #[Override]
719
    public function getSubspaceLayerData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
720
    {
721
        return $this->getEntityManager()->createNativeQuery(
722
            'SELECT l.cx as x, l.cy as y, mft.effects as effects,
723
            (SELECT count(distinct fs1.ship_id) from stu_flight_sig fs1
724
                WHERE fs1.location_id = l.id
725
                AND (fs1.from_direction = 1 OR fs1.to_direction = 1)) as d1c,
726
            (SELECT count(distinct fs2.ship_id) from stu_flight_sig fs2
727
                WHERE fs2.location_id = l.id
728
                AND (fs2.from_direction = 2 OR fs2.to_direction = 2)) as d2c,
729
            (SELECT count(distinct fs3.ship_id) from stu_flight_sig fs3
730
                WHERE fs3.location_id = l.id
731
                AND (fs3.from_direction = 3 OR fs3.to_direction = 3)) as d3c,
732
            (SELECT count(distinct fs4.ship_id) from stu_flight_sig fs4
733
                WHERE fs4.location_id = l.id
734
                AND (fs4.from_direction = 4 OR fs4.to_direction = 4)) as d4c 
735
            FROM stu_location l
736
            JOIN stu_map_ftypes mft
737
            ON l.field_id = mft.id
738
            WHERE l.cx BETWEEN :xStart AND :xEnd
739
            AND l.cy BETWEEN :yStart AND :yEnd
740
            AND l.layer_id = :layerId',
741
            $rsm
742
        )->setParameters([
743
            'xStart' => $boundaries->getMinX(),
744
            'xEnd' => $boundaries->getMaxX(),
745
            'yStart' => $boundaries->getMinY(),
746
            'yEnd' => $boundaries->getMaxY(),
747
            'layerId' => $boundaries->getParentId()
748
        ])->getResult();
749
    }
750
751
    #[Override]
752
    public function getUserSubspaceLayerData(PanelBoundaries $boundaries, int $userId, ResultSetMapping $rsm): array
753
    {
754
        return $this->getEntityManager()->createNativeQuery(
755
            'SELECT l.cx as x, l.cy as y,
756
            (SELECT count(distinct fs1.ship_id) from stu_flight_sig fs1
757
                WHERE fs1.location_id = l.id
758
                AND fs1.user_id = :userId
759
                AND (fs1.from_direction = 1 OR fs1.to_direction = 1)) as d1c,
760
            (SELECT count(distinct fs2.ship_id) from stu_flight_sig fs2
761
                WHERE fs2.location_id = l.id
762
                AND fs2.user_id = :userId
763
                AND (fs2.from_direction = 2 OR fs2.to_direction = 2)) as d2c,
764
            (SELECT count(distinct fs3.ship_id) from stu_flight_sig fs3
765
                WHERE fs3.location_id = l.id
766
                AND fs3.user_id = :userId
767
                AND (fs3.from_direction = 3 OR fs3.to_direction = 3)) as d3c,
768
            (SELECT count(distinct fs4.ship_id) from stu_flight_sig fs4
769
                WHERE fs4.location_id = l.id
770
                AND fs4.user_id = :userId
771
                AND (fs4.from_direction = 4 OR fs4.to_direction = 4)) as d4c 
772
            FROM stu_location l
773
            WHERE l.cx BETWEEN :xStart AND :xEnd
774
            AND l.cy BETWEEN :yStart AND :yEnd
775
            AND l.layer_id = :layerId',
776
            $rsm
777
        )->setParameters([
778
            'xStart' => $boundaries->getMinX(),
779
            'xEnd' => $boundaries->getMaxX(),
780
            'yStart' => $boundaries->getMinY(),
781
            'yEnd' => $boundaries->getMaxY(),
782
            'layerId' => $boundaries->getParentId(),
783
            'userId' => $userId
784
        ])->getResult();
785
    }
786
787
    #[Override]
788
    public function getShipSubspaceLayerData(PanelBoundaries $boundaries, int $shipId, ResultSetMapping $rsm): array
789
    {
790
        return $this->getEntityManager()->createNativeQuery(
791
            'SELECT l.cx as x, l.cy as y,
792
            (SELECT count(distinct fs1.ship_id) from stu_flight_sig fs1
793
                WHERE fs1.location_id = l.id
794
                AND fs1.ship_id = :shipId
795
                AND (fs1.from_direction = 1 OR fs1.to_direction = 1)) as d1c,
796
            (SELECT count(distinct fs2.ship_id) from stu_flight_sig fs2
797
                WHERE fs2.location_id = l.id
798
                AND fs2.ship_id = :shipId
799
                AND (fs2.from_direction = 2 OR fs2.to_direction = 2)) as d2c,
800
            (SELECT count(distinct fs3.ship_id) from stu_flight_sig fs3
801
                WHERE fs3.location_id = l.id
802
                AND fs3.ship_id = :shipId
803
                AND (fs3.from_direction = 3 OR fs3.to_direction = 3)) as d3c,
804
            (SELECT count(distinct fs4.ship_id) from stu_flight_sig fs4
805
                WHERE fs4.location_id = l.id
806
                AND fs4.ship_id = :shipId
807
                AND (fs4.from_direction = 4 OR fs4.to_direction = 4)) as d4c 
808
            FROM stu_location l
809
            WHERE l.cx BETWEEN :xStart AND :xEnd
810
            AND l.cy BETWEEN :yStart AND :yEnd
811
            AND l.layer_id = :layerId',
812
            $rsm
813
        )->setParameters([
814
            'xStart' => $boundaries->getMinX(),
815
            'xEnd' => $boundaries->getMaxX(),
816
            'yStart' => $boundaries->getMinY(),
817
            'yEnd' => $boundaries->getMaxY(),
818
            'layerId' => $boundaries->getParentId(),
819
            'shipId' => $shipId
820
        ])->getResult();
821
    }
822
823
    #[Override]
824
    public function getAllianceSubspaceLayerData(PanelBoundaries $boundaries, int $allianceId, ResultSetMapping $rsm): array
825
    {
826
        return $this->getEntityManager()->createNativeQuery(
827
            'SELECT l.id, l.cx as x, l.cy as y,
828
            (SELECT count(distinct fs1.ship_id) from stu_flight_sig fs1
829
                JOIN stu_user u1 ON fs1.user_id = u1.id
830
                WHERE fs1.location_id = l.id
831
                AND u1.allys_id = :allyId
832
                AND (fs1.from_direction = 1 OR fs1.to_direction = 1)) as d1c,
833
            (SELECT count(distinct fs2.ship_id) from stu_flight_sig fs2
834
                JOIN stu_user u2 ON fs2.user_id = u2.id
835
                WHERE fs2.location_id = l.id
836
                AND u2.allys_id = :allyId
837
                AND (fs2.from_direction = 2 OR fs2.to_direction = 2)) as d2c,
838
            (SELECT count(distinct fs3.ship_id) from stu_flight_sig fs3
839
                JOIN stu_user u3 ON fs3.user_id = u3.id
840
                WHERE fs3.location_id = l.id
841
                AND u3.allys_id = :allyId
842
                AND (fs3.from_direction = 3 OR fs3.to_direction = 3)) as d3c,
843
            (SELECT count(distinct fs4.ship_id) from stu_flight_sig fs4
844
                JOIN stu_user u4 ON fs4.user_id = u4.id
845
                WHERE fs4.location_id = l.id
846
                AND u4.allys_id = :allyId
847
                AND (fs4.from_direction = 4 OR fs4.to_direction = 4)) as d4c 
848
            FROM stu_location l
849
            WHERE l.cx BETWEEN :xStart AND :xEnd
850
            AND l.cy BETWEEN :yStart AND :yEnd
851
            AND l.layer_id = :layerId',
852
            $rsm
853
        )->setParameters([
854
            'xStart' => $boundaries->getMinX(),
855
            'xEnd' => $boundaries->getMaxX(),
856
            'yStart' => $boundaries->getMinY(),
857
            'yEnd' => $boundaries->getMaxY(),
858
            'layerId' => $boundaries->getParentId(),
859
            'allyId' => $allianceId
860
        ])->getResult();
861
    }
862
863 1
    public function getUniqueInfluenceAreaIds(int $layerId): array
864
    {
865 1
        $rsm = new ResultSetMapping();
866 1
        $rsm->addScalarResult('influence_area_id', 'influence_area_id', 'integer');
867
868 1
        $query = $this->getEntityManager()->createNativeQuery(
869 1
            'SELECT DISTINCT m.influence_area_id
870
            FROM stu_map m
871
            JOIN stu_location l ON m.id = l.id
872
            WHERE m.influence_area_id IS NOT NULL
873
            AND l.layer_id = :layerId
874 1
            ORDER BY m.influence_area_id ASC',
875 1
            $rsm
876 1
        )->setParameters([
877 1
            'layerId' => $layerId
878 1
        ]);
879
880 1
        return array_map('intval', array_column($query->getResult(), 'influence_area_id'));
881
    }
882
883
    #[Override]
884
    public function isAdminRegionUserRegion(int $locationId, int $factionId): bool
885
    {
886
        $result = $this->getEntityManager()
887
            ->createQuery(
888
                sprintf(
889
                    'SELECT COUNT(mrs.id) FROM %s m
890
            JOIN %s ssm WITH m.systems_id = ssm.systems_id
891
            JOIN %s mrs WITH m.admin_region_id = mrs.region_id
892
            WHERE ssm.id = :locationId
893
            AND mrs.faction_id = :factionId
894
            AND m.admin_region_id IS NOT NULL',
895
                    Map::class,
896
                    StarSystemMap::class,
897
                    MapRegionSettlement::class
898
                )
899
            )
900
            ->setParameters([
901
                'locationId' => $locationId,
902
                'factionId' => $factionId
903
            ])
904
            ->getSingleScalarResult();
905
906
        return $result > 0;
907
    }
908
}
909