Passed
Push — dev ( 45bf8d...5b8f46 )
by Janko
10:07
created

MapRepository::getAnomalyData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
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 20
ccs 0
cts 12
cp 0
crap 2
rs 9.6666
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
21
/**
22
 * @extends EntityRepository<Map>
23
 */
24
final class MapRepository extends EntityRepository implements MapRepositoryInterface
25
{
26
    #[Override]
27
    public function getAmountByLayer(LayerInterface $layer): int
28
    {
29
        return $this->count([
30
            'layer_id' => $layer->getId()
31
        ]);
32
    }
33
34
    #[Override]
35
    public function getAllOrdered(int $layerId): array
36
    {
37
        return $this->getEntityManager()
38
            ->createQuery(
39
                sprintf(
40
                    'SELECT m FROM %s m
41
                    JOIN %s l
42
                    WITH m.id = l.id
43
                    WHERE l.layer_id = :layerId
44
                    ORDER BY l.cy, l.cx',
45
                    Map::class,
46
                    Location::class
47
                )
48
            )
49
            ->setParameters([
50
                'layerId' => $layerId
51
            ])
52
            ->getResult();
53
    }
54
55
    #[Override]
56
    public function getAllWithSystem(int $layerId): array
57
    {
58
        return $this->getEntityManager()
59
            ->createQuery(
60
                sprintf(
61
                    'SELECT m FROM %s m INDEX BY m.id
62
                    JOIN %s l
63
                    WITH m.id = l.id
64
                    WHERE l.layer_id = :layerId
65
                    AND m.systems_id IS NOT null',
66
                    Map::class,
67
                    Location::class
68
                )
69
            )
70
            ->setParameters([
71
                'layerId' => $layerId
72
            ])
73
            ->getResult();
74
    }
75
76
    #[Override]
77
    public function getAllWithoutSystem(int $layerId): array
78
    {
79
        return $this->getEntityManager()
80
            ->createQuery(
81
                sprintf(
82
                    'SELECT m FROM %s m INDEX BY m.id
83
                    JOIN %s l
84
                    WITH m.id = l.id
85
                    WHERE l.layer_id = :layerId
86
                    AND m.systems_id IS null',
87
                    Map::class,
88
                    Location::class
89
                )
90
            )
91
            ->setParameters([
92
                'layerId' => $layerId
93
            ])
94
            ->getResult();
95
    }
96
97 1
    #[Override]
98
    public function getByCoordinates(?LayerInterface $layer, int $cx, int $cy): ?MapInterface
99
    {
100 1
        if ($layer === null) {
101
            return null;
102
        }
103
104 1
        return $this->findOneBy([
105 1
            'layer_id' => $layer->getId(),
106 1
            'cx' => $cx,
107 1
            'cy' => $cy
108 1
        ]);
109
    }
110
111
    #[Override]
112
    public function getByBoundaries(PanelBoundaries $boundaries): array
113
    {
114
        return $this->getByCoordinateRange(
115
            $boundaries->getParentId(),
116
            $boundaries->getMinX(),
117
            $boundaries->getMaxX(),
118
            $boundaries->getMinY(),
119
            $boundaries->getMaxY()
120
        );
121
    }
122
123
    #[Override]
124
    public function getByCoordinateRange(
125
        int $layerId,
126
        int $startCx,
127
        int $endCx,
128
        int $startCy,
129
        int $endCy,
130
        bool $sortAscending = true
131
    ): array {
132
        return $this->getEntityManager()
133
            ->createQuery(
134
                sprintf(
135
                    'SELECT m FROM %s m
136
                    JOIN %s l
137
                    WITH m.id = l.id
138
                    WHERE l.cx BETWEEN :startCx AND :endCx
139
                    AND l.cy BETWEEN :startCy AND :endCy
140
                    AND l.layer_id = :layerId
141
                    ORDER BY l.cy %3$s, l.cx %3$s',
142
                    Map::class,
143
                    Location::class,
144
                    $sortAscending ? 'ASC' : 'DESC'
145
                )
146
            )
147
            ->setParameters([
148
                'layerId' => $layerId,
149
                'startCx' => $startCx,
150
                'endCx' => $endCx,
151
                'startCy' => $startCy,
152
                'endCy' => $endCy
153
            ])
154
            ->getResult();
155
    }
156
157
    #[Override]
158
    public function save(MapInterface $map): void
159
    {
160
        $em = $this->getEntityManager();
161
162
        $em->persist($map);
163
    }
164
165 1
    #[Override]
166
    public function getBorderData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
167
    {
168 1
        return $this->getEntityManager()->createNativeQuery(
169 1
            'SELECT l.cx AS x, l.cy AS y,
170
                (SELECT al.rgb_code FROM stu_alliances al
171
                    JOIN stu_user u ON al.id = u.allys_id
172
                    JOIN stu_spacecraft sc ON u.id = sc.user_id
173
                    JOIN stu_station s ON sc.id = s.id
174
                    JOIN stu_map ma ON ma.influence_area_id = s.influence_area_id
175
                    WHERE ma.id = m.id AND ma.bordertype_id IS NULL AND ma.admin_region_id IS NULL)
176
                            AS allycolor,
177
                (SELECT COALESCE(us.value, \'\') FROM stu_user u
178
                    LEFT JOIN stu_user_setting us ON u.id = us.user_id
179
                    JOIN stu_spacecraft sc ON u.id = sc.user_id
180
                    JOIN stu_station s ON sc.id = s.id
181
                    JOIN stu_map mu ON mu.influence_area_id = s.influence_area_id
182
                    WHERE us.setting = :rgbCodeSetting
183
                    AND mu.id = m.id AND mu.bordertype_id IS NULL AND mu.admin_region_id IS NULL)
184
                        as usercolor,
185
                (SELECT mbt.color FROM stu_map_bordertypes mbt
186
                    JOIN stu_map mb ON mb.bordertype_id = mbt.id
187
                    WHERE mb.id = m.id AND mb.bordertype_id IS NOT NULL)
188
                        AS factioncolor
189
            FROM stu_map m
190
            JOIN stu_location l
191
            ON m.id = l.id
192
            WHERE l.cx BETWEEN :xStart AND :xEnd
193
            AND l.cy BETWEEN :yStart AND :yEnd
194 1
            AND l.layer_id = :layerId',
195 1
            $rsm
196 1
        )->setParameters([
197 1
            'xStart' => $boundaries->getMinX(),
198 1
            'xEnd' => $boundaries->getMaxX(),
199 1
            'yStart' => $boundaries->getMinY(),
200 1
            'yEnd' => $boundaries->getMaxY(),
201 1
            'layerId' => $boundaries->getParentId(),
202 1
            'rgbCodeSetting' => UserSettingEnum::RGB_CODE->value
203 1
        ])->getResult();
204
    }
205
206
    #[Override]
207
    public function getAnomalyData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
208
    {
209
        return $this->getEntityManager()->createNativeQuery(
210
            'SELECT l.cx AS x, l.cy AS y,
211
                (SELECT array_to_string(array(SELECT a.anomaly_type_id FROM stu_anomaly a WHERE a.location_id = m.id), \',\')) as anomalytypes
212
            FROM stu_map m
213
            JOIN stu_location l
214
            ON m.id = l.id
215
            WHERE l.cx BETWEEN :xStart AND :xEnd
216
            AND l.cy BETWEEN :yStart AND :yEnd
217
            AND l.layer_id = :layerId',
218
            $rsm
219
        )->setParameters([
220
            'xStart' => $boundaries->getMinX(),
221
            'xEnd' => $boundaries->getMaxX(),
222
            'yStart' => $boundaries->getMinY(),
223
            'yEnd' => $boundaries->getMaxY(),
224
            'layerId' => $boundaries->getParentId(),
225
        ])->getResult();
226
    }
227
228 1
    #[Override]
229
    public function getSpacecraftCountLayerData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
230
    {
231 1
        return $this->getEntityManager()->createNativeQuery(
232 1
            'SELECT l.cx as x, l.cy AS y,
233
                (SELECT count(DISTINCT b.id) FROM stu_spacecraft b
234
                    JOIN stu_location l2
235
                    ON b.location_id = l2.id
236
                    WHERE l2.layer_id = l.layer_id 
237
                    AND l2.cx = l.cx
238
                    AND l2.cy = l.cy
239
                    AND NOT EXISTS (SELECT ss.id
240
                                        FROM stu_spacecraft_system ss
241
                                        WHERE b.id = ss.spacecraft_id
242
                                        AND ss.system_type = :cloakSystemId
243
                                        AND ss.mode > 1)) AS spacecraftcount,
244
                (SELECT count(DISTINCT c.id) FROM stu_spacecraft c
245
                    JOIN stu_location l2
246
                    ON c.location_id = l2.id
247
                    WHERE l2.layer_id = l.layer_id 
248
                    AND l2.cx = l.cx
249
                    AND l2.cy = l.cy
250
                    AND EXISTS (SELECT ss2.id
251
                                        FROM stu_spacecraft_system ss2
252
                                        WHERE c.id = ss2.spacecraft_id
253
                                        AND ss2.system_type = :cloakSystemId
254
                                        AND ss2.mode > 1)) AS cloakcount
255
            FROM stu_location l
256
            WHERE l.cx BETWEEN :xStart AND :xEnd AND l.cy BETWEEN :yStart AND :yEnd
257
            AND l.layer_id = :layerId
258 1
            GROUP BY layer_id, x, y',
259 1
            $rsm
260 1
        )->setParameters([
261 1
            'xStart' => $boundaries->getMinX(),
262 1
            'xEnd' => $boundaries->getMaxX(),
263 1
            'yStart' => $boundaries->getMinY(),
264 1
            'yEnd' => $boundaries->getMaxY(),
265 1
            'layerId' => $boundaries->getParentId(),
266 1
            'cloakSystemId' => SpacecraftSystemTypeEnum::CLOAK->value
267 1
        ])->getResult();
268
    }
269
270
271 1
    #[Override]
272
    public function getMapLayerData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
273
    {
274 1
        return $this->getEntityManager()->createNativeQuery(
275 1
            'SELECT l.cx as x, l.cy AS y, ft.type
276
                FROM stu_map m
277
                JOIN stu_location l
278
                ON m.id = l.id
279
                JOIN stu_map_ftypes ft ON ft.id = l.field_id
280
                WHERE l.cx BETWEEN :xStart AND :xEnd AND l.cy BETWEEN :yStart AND :yEnd
281 1
                AND l.layer_id = :layerId',
282 1
            $rsm
283 1
        )->setParameters([
284 1
            'xStart' => $boundaries->getMinX(),
285 1
            'xEnd' => $boundaries->getMaxX(),
286 1
            'yStart' => $boundaries->getMinY(),
287 1
            'yEnd' => $boundaries->getMaxY(),
288 1
            'layerId' => $boundaries->getParentId(),
289 1
        ])->getResult();
290
    }
291
292 3
    #[Override]
293
    public function getExplored(int $userId, int $layerId, int $startX, int $endX, int $cy): array
294
    {
295 3
        $rsm = new ResultSetMapping();
296 3
        $rsm->addEntityResult(ExploreableStarMap::class, 'm');
297 3
        $rsm->addFieldResult('m', 'id', 'id');
298 3
        $rsm->addFieldResult('m', 'cx', 'cx');
299 3
        $rsm->addFieldResult('m', 'cy', 'cy');
300 3
        $rsm->addFieldResult('m', 'field_id', 'field_id');
301 3
        $rsm->addFieldResult('m', 'bordertype_id', 'bordertype_id');
302 3
        $rsm->addFieldResult('m', 'user_id', 'user_id');
303 3
        $rsm->addFieldResult('m', 'mapped', 'mapped');
304 3
        $rsm->addFieldResult('m', 'system_name', 'system_name');
305 3
        $rsm->addFieldResult('m', 'influence_area_id', 'influence_area_id');
306 3
        $rsm->addFieldResult('m', 'region_id', 'region_id');
307 3
        $rsm->addFieldResult('m', 'tradepost_id', 'tradepost_id');
308 3
        $rsm->addFieldResult('m', 'region_description', 'region_description');
309 3
        $rsm->addFieldResult('m', 'layer_id', 'layer_id');
310
311 3
        return $this->getEntityManager()
312 3
            ->createNativeQuery(
313 3
                'SELECT m.id, l.cx, l.cy, l.field_id, m.systems_id, m.bordertype_id, um.user_id,
314
                    dbu.database_id as mapped, m.influence_area_id as influence_area_id, m.admin_region_id as region_id,
315
                    sys.name as system_name, l.layer_id,
316
                    (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,
317
                    (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
318
                FROM stu_map m
319
                JOIN stu_location l
320
                ON m.id = l.id
321
                LEFT JOIN stu_user_map um
322
                    ON um.cy = l.cy AND um.cx = l.cx AND um.user_id = :userId AND um.layer_id = l.layer_id
323
                LEFT JOIN stu_systems sys
324
                    ON m.systems_id = sys.id
325
                LEFT JOIN stu_database_user dbu
326
                    ON dbu.user_id = :userId
327
                    AND sys.database_id = dbu.database_id
328
                WHERE l.cx BETWEEN :startX AND :endX
329
                AND l.cy = :cy
330
                AND l.layer_id = :layerId
331 3
                ORDER BY l.cx ASC',
332 3
                $rsm
333 3
            )
334 3
            ->setParameters([
335 3
                'layerId' => $layerId,
336 3
                'userId' => $userId,
337 3
                'startX' => $startX,
338 3
                'endX' => $endX,
339 3
                'cy' => $cy
340 3
            ])
341 3
            ->getResult();
342
    }
343
344
    #[Override]
345
    public function getWithEmptySystem(LayerInterface $layer): array
346
    {
347
        return $this->getEntityManager()
348
            ->createQuery(
349
                sprintf(
350
                    'SELECT m from %s m
351
                    JOIN %s l
352
                    WITH m.id = l.id
353
                    WHERE m.system_type_id IS NOT NULL
354
                    AND m.systems_id IS NULL
355
                    AND l.layer = :layer',
356
                    Map::class,
357
                    Location::class
358
                )
359
            )
360
            ->setParameters([
361
                'layer' => $layer
362
            ])
363
            ->getResult();
364
    }
365
366
    #[Override]
367
    public function getRandomMapIdsForAstroMeasurement(int $regionId, int $maxPercentage, int $location): array
368
    {
369
        $rsm = new ResultSetMapping();
370
        $rsm->addScalarResult('id', 'id', 'integer');
371
372
        $mapIdResultSet = $this->getEntityManager()
373
            ->createNativeQuery(
374
                'SELECT m.id FROM stu_map m
375
                JOIN stu_location l
376
                ON m.id = l.id
377
                JOIN stu_map_ftypes mf
378
                ON l.field_id = mf.id
379
                WHERE m.region_id = :regionId
380
                AND mf.passable = :true
381
                AND m.id != :loc
382
                ORDER BY RANDOM()',
383
                $rsm
384
            )
385
            ->setParameters([
386
                'regionId' => $regionId,
387
                'loc' => $location,
388
                'true' => true
389
            ])
390
            ->getResult();
391
392
        $amount = (int)ceil(count($mapIdResultSet) * $maxPercentage / 100);
393
        $subset = array_slice($mapIdResultSet, 0, $amount);
394
395
        return array_map(fn(array $data) => $data['id'], $subset);
396
    }
397
398
    #[Override]
399
    public function getRandomPassableUnoccupiedWithoutDamage(LayerInterface $layer, bool $isAtBorder = false): MapInterface
400
    {
401
        $rsm = new ResultSetMapping();
402
        $rsm->addScalarResult('id', 'id', 'integer');
403
404
        $borderCriteria = $isAtBorder ?
405
            sprintf(
406
                'AND (l.cx in (1, %d) OR l.cy in (1, %d))',
407
                $layer->getWidth(),
408
                $layer->getHeight()
409
            ) : '';
410
411
        $randomMapId =  (int)$this->getEntityManager()
412
            ->createNativeQuery(
413
                sprintf(
414
                    'SELECT m.id
415
                    FROM stu_map m
416
                    JOIN stu_location l
417
                    ON m.id = l.id
418
                    JOIN stu_map_ftypes mft
419
                    ON l.field_id = mft.id
420
                    WHERE NOT EXISTS (SELECT s.id FROM stu_spacecraft s WHERE s.location_id = m.id)
421
                    AND l.layer_id = :layerId
422
                    AND mft.x_damage = 0
423
                    AND mft.passable = :true
424
                    %s
425
                    ORDER BY RANDOM()
426
                    LIMIT 1',
427
                    $borderCriteria
428
                ),
429
                $rsm
430
            )
431
            ->setParameters([
432
                'layerId' => $layer->getId(),
433
                'true' => true
434
            ])
435
            ->getSingleScalarResult();
436
437
        $map = $this->find($randomMapId);
438
        if ($map === null) {
439
            throw new RuntimeException('this should not happen');
440
        }
441
442
        return $map;
443
    }
444
445
    #[Override]
446
    public function getIgnoringSubspaceLayerData(PanelBoundaries $boundaries, int $ignoreId, ResultSetMapping $rsm): array
447
    {
448
        $maxAge = time() - FlightSignatureVisibilityEnum::SIG_VISIBILITY_UNCLOAKED;
449
450
        return $this->getEntityManager()->createNativeQuery(
451
            sprintf(
452
                'SELECT l.cx AS x, l.cy AS y,
453
                (SELECT count(distinct fs1.ship_id) from stu_flight_sig fs1
454
                WHERE fs1.location_id = l.id
455
                AND fs1.user_id != %1$d
456
                AND (fs1.from_direction = 1 OR fs1.to_direction = 1)
457
                AND fs1.time > %2$d) as d1c,
458
                (SELECT count(distinct fs2.ship_id) from stu_flight_sig fs2
459
                WHERE fs2.location_id = l.id
460
                AND fs2.user_id != %1$d
461
                AND (fs2.from_direction = 2 OR fs2.to_direction = 2)
462
                AND fs2.time > %2$d) as d2c,
463
                (SELECT count(distinct fs3.ship_id) from stu_flight_sig fs3
464
                WHERE fs3.location_id = l.id
465
                AND fs3.user_id != %1$d
466
                AND (fs3.from_direction = 3 OR fs3.to_direction = 3)
467
                AND fs3.time > %2$d) as d3c,
468
                (SELECT count(distinct fs4.ship_id) from stu_flight_sig fs4
469
                WHERE fs4.location_id = l.id
470
                AND fs4.user_id != %1$d
471
                AND (fs4.from_direction = 4 OR fs4.to_direction = 4)
472
                AND fs4.time > %2$d) as d4c 
473
                FROM stu_location l
474
                WHERE l.cx BETWEEN :xStart AND :xEnd
475
                AND l.cy BETWEEN :yStart AND :yEnd
476
                AND l.layer_id = :layerId',
477
                $ignoreId,
478
                $maxAge
479
            ),
480
            $rsm
481
        )->setParameters([
482
            'xStart' => $boundaries->getMinX(),
483
            'xEnd' => $boundaries->getMaxX(),
484
            'yStart' => $boundaries->getMinY(),
485
            'yEnd' => $boundaries->getMaxY(),
486
            'layerId' => $boundaries->getParentId(),
487
        ])->getResult();
488
    }
489
490
    #[Override]
491
    public function getSubspaceLayerData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
492
    {
493
        return $this->getEntityManager()->createNativeQuery(
494
            'SELECT l.cx as x, l.cy as y,
495
            (SELECT count(distinct fs1.ship_id) from stu_flight_sig fs1
496
                WHERE fs1.location_id = l.id
497
                AND (fs1.from_direction = 1 OR fs1.to_direction = 1)) as d1c,
498
            (SELECT count(distinct fs2.ship_id) from stu_flight_sig fs2
499
                WHERE fs2.location_id = l.id
500
                AND (fs2.from_direction = 2 OR fs2.to_direction = 2)) as d2c,
501
            (SELECT count(distinct fs3.ship_id) from stu_flight_sig fs3
502
                WHERE fs3.location_id = l.id
503
                AND (fs3.from_direction = 3 OR fs3.to_direction = 3)) as d3c,
504
            (SELECT count(distinct fs4.ship_id) from stu_flight_sig fs4
505
                WHERE fs4.location_id = l.id
506
                AND (fs4.from_direction = 4 OR fs4.to_direction = 4)) as d4c 
507
            FROM stu_location l
508
            WHERE l.cx BETWEEN :xStart AND :xEnd
509
            AND l.cy BETWEEN :yStart AND :yEnd
510
            AND l.layer_id = :layerId',
511
            $rsm
512
        )->setParameters([
513
            'xStart' => $boundaries->getMinX(),
514
            'xEnd' => $boundaries->getMaxX(),
515
            'yStart' => $boundaries->getMinY(),
516
            'yEnd' => $boundaries->getMaxY(),
517
            'layerId' => $boundaries->getParentId()
518
        ])->getResult();
519
    }
520
521
    #[Override]
522
    public function getUserSubspaceLayerData(PanelBoundaries $boundaries, int $userId, ResultSetMapping $rsm): array
523
    {
524
        return $this->getEntityManager()->createNativeQuery(
525
            'SELECT l.cx as x, l.cy as y,
526
            (SELECT count(distinct fs1.ship_id) from stu_flight_sig fs1
527
                WHERE fs1.location_id = l.id
528
                AND fs1.user_id = :userId
529
                AND (fs1.from_direction = 1 OR fs1.to_direction = 1)) as d1c,
530
            (SELECT count(distinct fs2.ship_id) from stu_flight_sig fs2
531
                WHERE fs2.location_id = l.id
532
                AND fs2.user_id = :userId
533
                AND (fs2.from_direction = 2 OR fs2.to_direction = 2)) as d2c,
534
            (SELECT count(distinct fs3.ship_id) from stu_flight_sig fs3
535
                WHERE fs3.location_id = l.id
536
                AND fs3.user_id = :userId
537
                AND (fs3.from_direction = 3 OR fs3.to_direction = 3)) as d3c,
538
            (SELECT count(distinct fs4.ship_id) from stu_flight_sig fs4
539
                WHERE fs4.location_id = l.id
540
                AND fs4.user_id = :userId
541
                AND (fs4.from_direction = 4 OR fs4.to_direction = 4)) as d4c 
542
            FROM stu_location l
543
            WHERE l.cx BETWEEN :xStart AND :xEnd
544
            AND l.cy BETWEEN :yStart AND :yEnd
545
            AND l.layer_id = :layerId',
546
            $rsm
547
        )->setParameters([
548
            'xStart' => $boundaries->getMinX(),
549
            'xEnd' => $boundaries->getMaxX(),
550
            'yStart' => $boundaries->getMinY(),
551
            'yEnd' => $boundaries->getMaxY(),
552
            'layerId' => $boundaries->getParentId(),
553
            'userId' => $userId
554
        ])->getResult();
555
    }
556
557
    #[Override]
558
    public function getShipSubspaceLayerData(PanelBoundaries $boundaries, int $shipId, ResultSetMapping $rsm): array
559
    {
560
        return $this->getEntityManager()->createNativeQuery(
561
            'SELECT l.cx as x, l.cy as y,
562
            (SELECT count(distinct fs1.ship_id) from stu_flight_sig fs1
563
                WHERE fs1.location_id = l.id
564
                AND fs1.ship_id = :shipId
565
                AND (fs1.from_direction = 1 OR fs1.to_direction = 1)) as d1c,
566
            (SELECT count(distinct fs2.ship_id) from stu_flight_sig fs2
567
                WHERE fs2.location_id = l.id
568
                AND fs2.ship_id = :shipId
569
                AND (fs2.from_direction = 2 OR fs2.to_direction = 2)) as d2c,
570
            (SELECT count(distinct fs3.ship_id) from stu_flight_sig fs3
571
                WHERE fs3.location_id = l.id
572
                AND fs3.ship_id = :shipId
573
                AND (fs3.from_direction = 3 OR fs3.to_direction = 3)) as d3c,
574
            (SELECT count(distinct fs4.ship_id) from stu_flight_sig fs4
575
                WHERE fs4.location_id = l.id
576
                AND fs4.ship_id = :shipId
577
                AND (fs4.from_direction = 4 OR fs4.to_direction = 4)) as d4c 
578
            FROM stu_location l
579
            WHERE l.cx BETWEEN :xStart AND :xEnd
580
            AND l.cy BETWEEN :yStart AND :yEnd
581
            AND l.layer_id = :layerId',
582
            $rsm
583
        )->setParameters([
584
            'xStart' => $boundaries->getMinX(),
585
            'xEnd' => $boundaries->getMaxX(),
586
            'yStart' => $boundaries->getMinY(),
587
            'yEnd' => $boundaries->getMaxY(),
588
            'layerId' => $boundaries->getParentId(),
589
            'shipId' => $shipId
590
        ])->getResult();
591
    }
592
593
    #[Override]
594
    public function getAllianceSubspaceLayerData(PanelBoundaries $boundaries, int $allianceId, ResultSetMapping $rsm): array
595
    {
596
        return $this->getEntityManager()->createNativeQuery(
597
            'SELECT l.id, l.cx as x, l.cy as y,
598
            (SELECT count(distinct fs1.ship_id) from stu_flight_sig fs1
599
                JOIN stu_user u1 ON fs1.user_id = u1.id
600
                WHERE fs1.location_id = l.id
601
                AND u1.allys_id = :allyId
602
                AND (fs1.from_direction = 1 OR fs1.to_direction = 1)) as d1c,
603
            (SELECT count(distinct fs2.ship_id) from stu_flight_sig fs2
604
                JOIN stu_user u2 ON fs2.user_id = u2.id
605
                WHERE fs2.location_id = l.id
606
                AND u2.allys_id = :allyId
607
                AND (fs2.from_direction = 2 OR fs2.to_direction = 2)) as d2c,
608
            (SELECT count(distinct fs3.ship_id) from stu_flight_sig fs3
609
                JOIN stu_user u3 ON fs3.user_id = u3.id
610
                WHERE fs3.location_id = l.id
611
                AND u3.allys_id = :allyId
612
                AND (fs3.from_direction = 3 OR fs3.to_direction = 3)) as d3c,
613
            (SELECT count(distinct fs4.ship_id) from stu_flight_sig fs4
614
                JOIN stu_user u4 ON fs4.user_id = u4.id
615
                WHERE fs4.location_id = l.id
616
                AND u4.allys_id = :allyId
617
                AND (fs4.from_direction = 4 OR fs4.to_direction = 4)) as d4c 
618
            FROM stu_location l
619
            WHERE l.cx BETWEEN :xStart AND :xEnd
620
            AND l.cy BETWEEN :yStart AND :yEnd
621
            AND l.layer_id = :layerId',
622
            $rsm
623
        )->setParameters([
624
            'xStart' => $boundaries->getMinX(),
625
            'xEnd' => $boundaries->getMaxX(),
626
            'yStart' => $boundaries->getMinY(),
627
            'yEnd' => $boundaries->getMaxY(),
628
            'layerId' => $boundaries->getParentId(),
629
            'allyId' => $allianceId
630
        ])->getResult();
631
    }
632
633 1
    public function getUniqueInfluenceAreaIds(): array
634
    {
635 1
        $rsm = new ResultSetMapping();
636 1
        $rsm->addScalarResult('influence_area_id', 'influence_area_id', 'integer');
637
638 1
        $query = $this->getEntityManager()->createNativeQuery(
639 1
            'SELECT DISTINCT influence_area_id
640
             FROM stu_map
641
             WHERE influence_area_id IS NOT NULL
642 1
             ORDER BY influence_area_id ASC',
643 1
            $rsm
644 1
        );
645
646 1
        return array_map('intval', array_column($query->getResult(), 'influence_area_id'));
647
    }
648
}
649