Passed
Push — master ( f80fed...0867c9 )
by Nico
11:47
created

MapRepository::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Orm\Repository;
6
7
use Doctrine\ORM\EntityRepository;
8
use Doctrine\ORM\Query\ResultSetMapping;
9
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use 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, mft.effects as effects,
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_map m
256
            JOIN stu_location l
257
            ON m.id = l.id
258
            JOIN stu_map_ftypes mft
259
            ON l.field_id = mft.id
260
            WHERE l.cx BETWEEN :xStart AND :xEnd
261
            AND l.cy BETWEEN :yStart AND :yEnd
262 1
            AND l.layer_id = :layerId',
263 1
            $rsm
264 1
        )->setParameters([
265 1
            'xStart' => $boundaries->getMinX(),
266 1
            'xEnd' => $boundaries->getMaxX(),
267 1
            'yStart' => $boundaries->getMinY(),
268 1
            'yEnd' => $boundaries->getMaxY(),
269 1
            'layerId' => $boundaries->getParentId(),
270 1
            'cloakSystemId' => SpacecraftSystemTypeEnum::CLOAK->value
271 1
        ])->getResult();
272
    }
273
274
275 1
    #[Override]
276
    public function getMapLayerData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
277
    {
278 1
        return $this->getEntityManager()->createNativeQuery(
279 1
            'SELECT l.cx as x, l.cy AS y, ft.type
280
                FROM stu_map m
281
                JOIN stu_location l
282
                ON m.id = l.id
283
                JOIN stu_map_ftypes ft ON ft.id = l.field_id
284
                WHERE l.cx BETWEEN :xStart AND :xEnd AND l.cy BETWEEN :yStart AND :yEnd
285 1
                AND l.layer_id = :layerId',
286 1
            $rsm
287 1
        )->setParameters([
288 1
            'xStart' => $boundaries->getMinX(),
289 1
            'xEnd' => $boundaries->getMaxX(),
290 1
            'yStart' => $boundaries->getMinY(),
291 1
            'yEnd' => $boundaries->getMaxY(),
292 1
            'layerId' => $boundaries->getParentId(),
293 1
        ])->getResult();
294
    }
295
296
    #[Override]
297
    public function getUserSpacecraftCountLayerData(PanelBoundaries $boundaries, int $userId, ResultSetMapping $rsm): array
298
    {
299
        return $this->getEntityManager()->createNativeQuery(
300
            'SELECT l.cx as x, l.cy as y, mft.effects as effects,
301
            (SELECT count(distinct s.id)
302
                FROM stu_spacecraft s
303
                JOIN stu_location spl
304
                ON s.location_id = spl.id
305
                WHERE spl.cx = l.cx
306
                AND spl.cy = l.cy
307
                AND spl.layer_id = l.layer_id
308
                AND s.user_id = :userId) as spacecraftcount
309
            FROM stu_map m
310
            JOIN stu_location l
311
            ON m.id = l.id
312
            JOIN stu_map_ftypes mft
313
            ON l.field_id = mft.id
314
            WHERE l.cx BETWEEN :xStart AND :xEnd
315
            AND l.cy BETWEEN :yStart AND :yEnd
316
            AND l.layer_id = :layerId',
317
            $rsm
318
        )->setParameters([
319
            'xStart' => $boundaries->getMinX(),
320
            'xEnd' => $boundaries->getMaxX(),
321
            'yStart' => $boundaries->getMinY(),
322
            'yEnd' => $boundaries->getMaxY(),
323
            'layerId' => $boundaries->getParentId(),
324
            'userId' => $userId
325
        ])->getResult();
326
    }
327
328
    #[Override]
329
    public function getAllianceSpacecraftCountLayerData(PanelBoundaries $boundaries, int $allianceId, ResultSetMapping $rsm): array
330
    {
331
        return $this->getEntityManager()->createNativeQuery(
332
            'SELECT l.cx as x, l.cy as y, mft.effects as effects,
333
             (SELECT count(distinct s.id)
334
                    FROM stu_spacecraft s
335
                    JOIN stu_location spl
336
                    ON s.location_id = spl.id
337
                    JOIN stu_user u
338
                    ON s.user_id = u.id
339
                    WHERE spl.cx = l.cx
340
                    AND spl.cy = l.cy
341
                    AND spl.layer_id = l.layer_id
342
                    AND u.allys_id = :allyId) as spacecraftcount
343
            FROM stu_map m
344
            JOIN stu_location l
345
            ON m.id = l.id
346
            JOIN stu_map_ftypes mft
347
            ON l.field_id = mft.id
348
            WHERE l.cx BETWEEN :xStart AND :xEnd
349
            AND l.cy BETWEEN :yStart AND :yEnd
350
            AND l.layer_id = :layerId',
351
            $rsm
352
        )->setParameters([
353
            'xStart' => $boundaries->getMinX(),
354
            'xEnd' => $boundaries->getMaxX(),
355
            'yStart' => $boundaries->getMinY(),
356
            'yEnd' => $boundaries->getMaxY(),
357
            'layerId' => $boundaries->getParentId(),
358
            'allyId' => $allianceId
359
        ])->getResult();
360
    }
361
362
    #[Override]
363
    public function getSpacecraftCountLayerDataForSpacecraft(PanelBoundaries $boundaries, int $spacecraftId, ResultSetMapping $rsm): array
364
    {
365
        return $this->getEntityManager()->createNativeQuery(
366
            'SELECT l.cx as x, l.cy as y, mft.effects as effects,
367
            (SELECT count(distinct s.id)
368
                FROM stu_spacecraft s
369
                JOIN stu_location spl
370
                ON s.location_id = spl.id
371
                WHERE spl.cx = l.cx
372
                AND spl.cy = l.cy
373
                AND spl.layer_id = l.layer_id
374
                AND s.id = :spacecraftId) as spacecraftcount
375
            FROM stu_map m
376
            JOIN stu_location l
377
            ON m.id = l.id
378
            JOIN stu_map_ftypes mft
379
            ON l.field_id = mft.id
380
            WHERE l.cx BETWEEN :xStart AND :xEnd
381
            AND l.cy BETWEEN :yStart AND :yEnd
382
            AND l.layer_id = :layerId',
383
            $rsm
384
        )->setParameters([
385
            'xStart' => $boundaries->getMinX(),
386
            'xEnd' => $boundaries->getMaxX(),
387
            'yStart' => $boundaries->getMinY(),
388
            'yEnd' => $boundaries->getMaxY(),
389
            'layerId' => $boundaries->getParentId(),
390
            'spacecraftId' => $spacecraftId
391
        ])->getResult();
392
    }
393
394 3
    #[Override]
395
    public function getExplored(int $userId, int $layerId, int $startX, int $endX, int $cy): array
396
    {
397 3
        $rsm = new ResultSetMapping();
398 3
        $rsm->addEntityResult(ExploreableStarMap::class, 'm');
399 3
        $rsm->addFieldResult('m', 'id', 'id');
400 3
        $rsm->addFieldResult('m', 'cx', 'cx');
401 3
        $rsm->addFieldResult('m', 'cy', 'cy');
402 3
        $rsm->addFieldResult('m', 'field_id', 'field_id');
403 3
        $rsm->addFieldResult('m', 'bordertype_id', 'bordertype_id');
404 3
        $rsm->addFieldResult('m', 'user_id', 'user_id');
405 3
        $rsm->addFieldResult('m', 'mapped', 'mapped');
406 3
        $rsm->addFieldResult('m', 'system_name', 'system_name');
407 3
        $rsm->addFieldResult('m', 'influence_area_id', 'influence_area_id');
408 3
        $rsm->addFieldResult('m', 'region_id', 'region_id');
409 3
        $rsm->addFieldResult('m', 'tradepost_id', 'tradepost_id');
410 3
        $rsm->addFieldResult('m', 'region_description', 'region_description');
411 3
        $rsm->addFieldResult('m', 'layer_id', 'layer_id');
412
413 3
        return $this->getEntityManager()
414 3
            ->createNativeQuery(
415 3
                'SELECT m.id, l.cx, l.cy, l.field_id, m.systems_id, m.bordertype_id, um.user_id,
416
                    dbu.database_id as mapped, m.influence_area_id as influence_area_id, m.admin_region_id as region_id,
417
                    sys.name as system_name, l.layer_id,
418
                    (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,
419
                    (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
420
                FROM stu_map m
421
                JOIN stu_location l
422
                ON m.id = l.id
423
                LEFT JOIN stu_user_map um
424
                    ON um.cy = l.cy AND um.cx = l.cx AND um.user_id = :userId AND um.layer_id = l.layer_id
425
                LEFT JOIN stu_systems sys
426
                    ON m.systems_id = sys.id
427
                LEFT JOIN stu_database_user dbu
428
                    ON dbu.user_id = :userId
429
                    AND sys.database_id = dbu.database_id
430
                WHERE l.cx BETWEEN :startX AND :endX
431
                AND l.cy = :cy
432
                AND l.layer_id = :layerId
433 3
                ORDER BY l.cx ASC',
434 3
                $rsm
435 3
            )
436 3
            ->setParameters([
437 3
                'layerId' => $layerId,
438 3
                'userId' => $userId,
439 3
                'startX' => $startX,
440 3
                'endX' => $endX,
441 3
                'cy' => $cy
442 3
            ])
443 3
            ->getResult();
444
    }
445
446
    #[Override]
447
    public function getWithEmptySystem(LayerInterface $layer): array
448
    {
449
        return $this->getEntityManager()
450
            ->createQuery(
451
                sprintf(
452
                    'SELECT m from %s m
453
                    JOIN %s l
454
                    WITH m.id = l.id
455
                    WHERE m.system_type_id IS NOT NULL
456
                    AND m.systems_id IS NULL
457
                    AND l.layer = :layer',
458
                    Map::class,
459
                    Location::class
460
                )
461
            )
462
            ->setParameters([
463
                'layer' => $layer
464
            ])
465
            ->getResult();
466
    }
467
468
    #[Override]
469
    public function getRandomMapIdsForAstroMeasurement(int $regionId, int $maxPercentage, int $location): array
470
    {
471
        $rsm = new ResultSetMapping();
472
        $rsm->addScalarResult('id', 'id', 'integer');
473
474
        $mapIdResultSet = $this->getEntityManager()
475
            ->createNativeQuery(
476
                'SELECT m.id FROM stu_map m
477
                JOIN stu_location l
478
                ON m.id = l.id
479
                JOIN stu_map_ftypes mf
480
                ON l.field_id = mf.id
481
                WHERE m.region_id = :regionId
482
                AND mf.passable = :true
483
                AND m.id != :loc
484
                ORDER BY RANDOM()',
485
                $rsm
486
            )
487
            ->setParameters([
488
                'regionId' => $regionId,
489
                'loc' => $location,
490
                'true' => true
491
            ])
492
            ->getResult();
493
494
        $amount = (int)ceil(count($mapIdResultSet) * $maxPercentage / 100);
495
        $subset = array_slice($mapIdResultSet, 0, $amount);
496
497
        return array_map(fn(array $data) => $data['id'], $subset);
498
    }
499
500
    #[Override]
501
    public function getRandomPassableUnoccupiedWithoutDamage(LayerInterface $layer, bool $isAtBorder = false): MapInterface
502
    {
503
        $rsm = new ResultSetMapping();
504
        $rsm->addScalarResult('id', 'id', 'integer');
505
506
        $borderCriteria = $isAtBorder ?
507
            sprintf(
508
                'AND (l.cx in (1, %d) OR l.cy in (1, %d))',
509
                $layer->getWidth(),
510
                $layer->getHeight()
511
            ) : '';
512
513
        $randomMapId =  (int)$this->getEntityManager()
514
            ->createNativeQuery(
515
                sprintf(
516
                    'SELECT m.id
517
                    FROM stu_map m
518
                    JOIN stu_location l
519
                    ON m.id = l.id
520
                    JOIN stu_map_ftypes mft
521
                    ON l.field_id = mft.id
522
                    WHERE NOT EXISTS (SELECT s.id FROM stu_spacecraft s WHERE s.location_id = m.id)
523
                    AND l.layer_id = :layerId
524
                    AND mft.x_damage = 0
525
                    AND mft.passable = :true
526
                    %s
527
                    ORDER BY RANDOM()
528
                    LIMIT 1',
529
                    $borderCriteria
530
                ),
531
                $rsm
532
            )
533
            ->setParameters([
534
                'layerId' => $layer->getId(),
535
                'true' => true
536
            ])
537
            ->getSingleScalarResult();
538
539
        $map = $this->find($randomMapId);
540
        if ($map === null) {
541
            throw new RuntimeException('this should not happen');
542
        }
543
544
        return $map;
545
    }
546
547
    #[Override]
548
    public function getIgnoringSubspaceLayerData(PanelBoundaries $boundaries, int $ignoreUserId, ResultSetMapping $rsm): array
549
    {
550
        $maxAge = time() - FlightSignatureVisibilityEnum::SIG_VISIBILITY_UNCLOAKED;
551
552
        return $this->getEntityManager()->createNativeQuery(
553
            'SELECT l.cx AS x, l.cy AS y, mft.effects as effects,
554
                (SELECT count(distinct fs1.ship_id) from stu_flight_sig fs1
555
                WHERE fs1.location_id = l.id
556
                AND fs1.user_id != :ignoreUserId
557
                AND (fs1.from_direction = 1 OR fs1.to_direction = 1)
558
                AND fs1.time > :timeThreshold) as d1c,
559
                (SELECT count(distinct fs2.ship_id) from stu_flight_sig fs2
560
                WHERE fs2.location_id = l.id
561
                AND fs2.user_id !=:ignoreUserId
562
                AND (fs2.from_direction = 2 OR fs2.to_direction = 2)
563
                AND fs2.time > :timeThreshold) as d2c,
564
                (SELECT count(distinct fs3.ship_id) from stu_flight_sig fs3
565
                WHERE fs3.location_id = l.id
566
                AND fs3.user_id != :ignoreUserId
567
                AND (fs3.from_direction = 3 OR fs3.to_direction = 3)
568
                AND fs3.time > :timeThreshold) as d3c,
569
                (SELECT count(distinct fs4.ship_id) from stu_flight_sig fs4
570
                WHERE fs4.location_id = l.id
571
                AND fs4.user_id != :ignoreUserId
572
                AND (fs4.from_direction = 4 OR fs4.to_direction = 4)
573
                AND fs4.time > :timeThreshold) as d4c 
574
                FROM stu_location l
575
                JOIN stu_map m
576
                ON l.id = m.id
577
                JOIN stu_map_ftypes mft
578
                ON l.field_id = mft.id
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
            'ignoreUserId' => $ignoreUserId,
590
            'timeThreshold' => $maxAge
591
        ])->getResult();
592
    }
593
594
    #[Override]
595
    public function getSubspaceLayerData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
596
    {
597
        return $this->getEntityManager()->createNativeQuery(
598
            'SELECT l.cx as x, l.cy as y, mft.effects as effects,
599
            (SELECT count(distinct fs1.ship_id) from stu_flight_sig fs1
600
                WHERE fs1.location_id = l.id
601
                AND (fs1.from_direction = 1 OR fs1.to_direction = 1)) as d1c,
602
            (SELECT count(distinct fs2.ship_id) from stu_flight_sig fs2
603
                WHERE fs2.location_id = l.id
604
                AND (fs2.from_direction = 2 OR fs2.to_direction = 2)) as d2c,
605
            (SELECT count(distinct fs3.ship_id) from stu_flight_sig fs3
606
                WHERE fs3.location_id = l.id
607
                AND (fs3.from_direction = 3 OR fs3.to_direction = 3)) as d3c,
608
            (SELECT count(distinct fs4.ship_id) from stu_flight_sig fs4
609
                WHERE fs4.location_id = l.id
610
                AND (fs4.from_direction = 4 OR fs4.to_direction = 4)) as d4c 
611
            FROM stu_location l
612
            JOIN stu_map_ftypes mft
613
            ON l.field_id = mft.id
614
            WHERE l.cx BETWEEN :xStart AND :xEnd
615
            AND l.cy BETWEEN :yStart AND :yEnd
616
            AND l.layer_id = :layerId',
617
            $rsm
618
        )->setParameters([
619
            'xStart' => $boundaries->getMinX(),
620
            'xEnd' => $boundaries->getMaxX(),
621
            'yStart' => $boundaries->getMinY(),
622
            'yEnd' => $boundaries->getMaxY(),
623
            'layerId' => $boundaries->getParentId()
624
        ])->getResult();
625
    }
626
627
    #[Override]
628
    public function getUserSubspaceLayerData(PanelBoundaries $boundaries, int $userId, ResultSetMapping $rsm): array
629
    {
630
        return $this->getEntityManager()->createNativeQuery(
631
            'SELECT l.cx as x, l.cy as y, mft.effects as effects,
632
            (SELECT count(distinct fs1.ship_id) from stu_flight_sig fs1
633
                WHERE fs1.location_id = l.id
634
                AND fs1.user_id = :userId
635
                AND (fs1.from_direction = 1 OR fs1.to_direction = 1)) as d1c,
636
            (SELECT count(distinct fs2.ship_id) from stu_flight_sig fs2
637
                WHERE fs2.location_id = l.id
638
                AND fs2.user_id = :userId
639
                AND (fs2.from_direction = 2 OR fs2.to_direction = 2)) as d2c,
640
            (SELECT count(distinct fs3.ship_id) from stu_flight_sig fs3
641
                WHERE fs3.location_id = l.id
642
                AND fs3.user_id = :userId
643
                AND (fs3.from_direction = 3 OR fs3.to_direction = 3)) as d3c,
644
            (SELECT count(distinct fs4.ship_id) from stu_flight_sig fs4
645
                WHERE fs4.location_id = l.id
646
                AND fs4.user_id = :userId
647
                AND (fs4.from_direction = 4 OR fs4.to_direction = 4)) as d4c 
648
            FROM stu_location l
649
            JOIN stu_map_ftypes mft
650
            ON l.field_id = mft.id
651
            WHERE l.cx BETWEEN :xStart AND :xEnd
652
            AND l.cy BETWEEN :yStart AND :yEnd
653
            AND l.layer_id = :layerId',
654
            $rsm
655
        )->setParameters([
656
            'xStart' => $boundaries->getMinX(),
657
            'xEnd' => $boundaries->getMaxX(),
658
            'yStart' => $boundaries->getMinY(),
659
            'yEnd' => $boundaries->getMaxY(),
660
            'layerId' => $boundaries->getParentId(),
661
            'userId' => $userId
662
        ])->getResult();
663
    }
664
665
    #[Override]
666
    public function getShipSubspaceLayerData(PanelBoundaries $boundaries, int $shipId, ResultSetMapping $rsm): array
667
    {
668
        return $this->getEntityManager()->createNativeQuery(
669
            'SELECT l.cx as x, l.cy as y, mft.effects as effects,
670
            (SELECT count(distinct fs1.ship_id) from stu_flight_sig fs1
671
                WHERE fs1.location_id = l.id
672
                AND fs1.ship_id = :shipId
673
                AND (fs1.from_direction = 1 OR fs1.to_direction = 1)) as d1c,
674
            (SELECT count(distinct fs2.ship_id) from stu_flight_sig fs2
675
                WHERE fs2.location_id = l.id
676
                AND fs2.ship_id = :shipId
677
                AND (fs2.from_direction = 2 OR fs2.to_direction = 2)) as d2c,
678
            (SELECT count(distinct fs3.ship_id) from stu_flight_sig fs3
679
                WHERE fs3.location_id = l.id
680
                AND fs3.ship_id = :shipId
681
                AND (fs3.from_direction = 3 OR fs3.to_direction = 3)) as d3c,
682
            (SELECT count(distinct fs4.ship_id) from stu_flight_sig fs4
683
                WHERE fs4.location_id = l.id
684
                AND fs4.ship_id = :shipId
685
                AND (fs4.from_direction = 4 OR fs4.to_direction = 4)) as d4c 
686
            FROM stu_location l
687
            JOIN stu_map_ftypes mft
688
            ON l.field_id = mft.id
689
            WHERE l.cx BETWEEN :xStart AND :xEnd
690
            AND l.cy BETWEEN :yStart AND :yEnd
691
            AND l.layer_id = :layerId',
692
            $rsm
693
        )->setParameters([
694
            'xStart' => $boundaries->getMinX(),
695
            'xEnd' => $boundaries->getMaxX(),
696
            'yStart' => $boundaries->getMinY(),
697
            'yEnd' => $boundaries->getMaxY(),
698
            'layerId' => $boundaries->getParentId(),
699
            'shipId' => $shipId
700
        ])->getResult();
701
    }
702
703
    #[Override]
704
    public function getAllianceSubspaceLayerData(PanelBoundaries $boundaries, int $allianceId, ResultSetMapping $rsm): array
705
    {
706
        return $this->getEntityManager()->createNativeQuery(
707
            'SELECT l.id, l.cx as x, l.cy as y, mft.effects as effects,
708
            (SELECT count(distinct fs1.ship_id) from stu_flight_sig fs1
709
                JOIN stu_user u1 ON fs1.user_id = u1.id
710
                WHERE fs1.location_id = l.id
711
                AND u1.allys_id = :allyId
712
                AND (fs1.from_direction = 1 OR fs1.to_direction = 1)) as d1c,
713
            (SELECT count(distinct fs2.ship_id) from stu_flight_sig fs2
714
                JOIN stu_user u2 ON fs2.user_id = u2.id
715
                WHERE fs2.location_id = l.id
716
                AND u2.allys_id = :allyId
717
                AND (fs2.from_direction = 2 OR fs2.to_direction = 2)) as d2c,
718
            (SELECT count(distinct fs3.ship_id) from stu_flight_sig fs3
719
                JOIN stu_user u3 ON fs3.user_id = u3.id
720
                WHERE fs3.location_id = l.id
721
                AND u3.allys_id = :allyId
722
                AND (fs3.from_direction = 3 OR fs3.to_direction = 3)) as d3c,
723
            (SELECT count(distinct fs4.ship_id) from stu_flight_sig fs4
724
                JOIN stu_user u4 ON fs4.user_id = u4.id
725
                WHERE fs4.location_id = l.id
726
                AND u4.allys_id = :allyId
727
                AND (fs4.from_direction = 4 OR fs4.to_direction = 4)) as d4c 
728
            FROM stu_location l
729
            JOIN stu_map_ftypes mft
730
            ON l.field_id = mft.id
731
            WHERE l.cx BETWEEN :xStart AND :xEnd
732
            AND l.cy BETWEEN :yStart AND :yEnd
733
            AND l.layer_id = :layerId',
734
            $rsm
735
        )->setParameters([
736
            'xStart' => $boundaries->getMinX(),
737
            'xEnd' => $boundaries->getMaxX(),
738
            'yStart' => $boundaries->getMinY(),
739
            'yEnd' => $boundaries->getMaxY(),
740
            'layerId' => $boundaries->getParentId(),
741
            'allyId' => $allianceId
742
        ])->getResult();
743
    }
744
745 1
    public function getUniqueInfluenceAreaIds(): array
746
    {
747 1
        $rsm = new ResultSetMapping();
748 1
        $rsm->addScalarResult('influence_area_id', 'influence_area_id', 'integer');
749
750 1
        $query = $this->getEntityManager()->createNativeQuery(
751 1
            'SELECT DISTINCT influence_area_id
752
             FROM stu_map
753
             WHERE influence_area_id IS NOT NULL
754 1
             ORDER BY influence_area_id ASC',
755 1
            $rsm
756 1
        );
757
758 1
        return array_map('intval', array_column($query->getResult(), 'influence_area_id'));
759
    }
760
}
761