Passed
Push — dev ( 0f4afd...29c427 )
by Janko
25:18 queued 14:48
created

getIgnoringSubspaceLayerData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 42
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 38
c 1
b 1
f 0
nc 1
nop 3
dl 0
loc 42
ccs 0
cts 17
cp 0
crap 2
rs 9.312
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 Stu\Component\Building\BuildingFunctionEnum;
11
use Stu\Component\Ship\AstronomicalMappingEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Ship\AstronomicalMappingEnum 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\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...
13
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
14
use Stu\Lib\Map\VisualPanel\PanelBoundaries;
15
use Stu\Module\PlayerSetting\Lib\UserEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Module\PlayerSetting\Lib\UserEnum 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...
16
use Stu\Orm\Entity\StarSystemInterface;
17
use Stu\Orm\Entity\StarSystemMap;
18
use Stu\Orm\Entity\StarSystemMapInterface;
19
20
/**
21
 * @extends EntityRepository<StarSystemMap>
22
 */
23
final class StarSystemMapRepository extends EntityRepository implements StarSystemMapRepositoryInterface
24
{
25
    #[Override]
26
    public function getBySystemOrdered(int $starSystemId): array
27
    {
28
        return $this->findBy(
29
            ['systems_id' => $starSystemId],
30
            ['sy' => 'asc', 'sx' => 'asc']
31
        );
32
    }
33
34 2
    #[Override]
35
    public function getByCoordinates(
36
        int $starSystemId,
37
        int $sx,
38
        int $sy
39
    ): ?StarSystemMapInterface {
40 2
        return $this->findOneBy([
41 2
            'systems_id' => $starSystemId,
42 2
            'sx' => $sx,
43 2
            'sy' => $sy
44 2
        ]);
45
    }
46
47
    #[Override]
48
    public function getByCoordinateRange(
49
        StarSystemInterface $starSystem,
50
        int $startSx,
51
        int $endSx,
52
        int $startSy,
53
        int $endSy,
54
        bool $sortAscending = true
55
    ): array {
56
        return $this->getEntityManager()
57
            ->createQuery(
58
                sprintf(
59
                    'SELECT m FROM %1$s m
60
                    WHERE m.systems_id = :starSystemId AND
61
                        m.sx BETWEEN :startSx AND :endSx AND
62
                        m.sy BETWEEN :startSy AND :endSy
63
                    ORDER BY m.sy %2$s, m.sx %2$s',
64
                    StarSystemMap::class,
65
                    $sortAscending ? 'ASC' : 'DESC'
66
                )
67
            )
68
            ->setParameters([
69
                'starSystemId' => $starSystem,
70
                'startSx' => $startSx,
71
                'endSx' => $endSx,
72
                'startSy' => $startSy,
73
                'endSy' => $endSy
74
            ])
75
            ->getResult();
76
    }
77
78 3
    #[Override]
79
    public function getMapLayerData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
80
    {
81 3
        return $this->getEntityManager()->createNativeQuery(
82 3
            'SELECT sm.sx as x, sm.sy AS y, ft.type
83
                FROM stu_sys_map sm
84
                JOIN stu_location l
85
                ON sm.id = l.id
86
                JOIN stu_map_ftypes ft ON ft.id = l.field_id
87
                WHERE sm.sx BETWEEN :xStart AND :xEnd AND sm.sy BETWEEN :yStart AND :yEnd
88 3
                AND sm.systems_id = :systemId',
89 3
            $rsm
90 3
        )->setParameters([
91 3
            'xStart' => $boundaries->getMinX(),
92 3
            'xEnd' => $boundaries->getMaxX(),
93 3
            'yStart' => $boundaries->getMinY(),
94 3
            'yEnd' => $boundaries->getMaxY(),
95 3
            'systemId' => $boundaries->getParentId()
96 3
        ])->getResult();
97
    }
98
99 3
    #[Override]
100
    public function getSpacecraftCountLayerData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
101
    {
102 3
        return $this->getEntityManager()->createNativeQuery(
103 3
            'SELECT sm.id, sm.sx as x, sm.sy AS y,
104
                (SELECT count(DISTINCT b.id) FROM stu_spacecraft b
105
                    WHERE sm.id = b.location_id
106
                    AND NOT EXISTS (SELECT ss.id
107
                                        FROM stu_spacecraft_system ss
108
                                        WHERE b.id = ss.spacecraft_id
109
                                        AND ss.system_type = :cloakSystemId
110
                                        AND ss.mode > 1)) AS spacecraftcount,
111
                (SELECT count(DISTINCT c.id) FROM stu_spacecraft c
112
                    WHERE sm.id = c.location_id
113
                    AND EXISTS (SELECT ss2.id
114
                                        FROM stu_spacecraft_system ss2
115
                                        WHERE c.id = ss2.spacecraft_id
116
                                        AND ss2.system_type = :cloakSystemId
117
                                        AND ss2.mode > 1)) AS cloakcount
118
            FROM stu_sys_map sm
119
            WHERE sm.sx BETWEEN :xStart AND :xEnd AND sm.sy BETWEEN :yStart AND :yEnd
120
            AND sm.systems_id = :systemId
121 3
            GROUP BY sm.id, sm.sy, sm.sx',
122 3
            $rsm
123 3
        )->setParameters([
124 3
            'xStart' => $boundaries->getMinX(),
125 3
            'xEnd' => $boundaries->getMaxX(),
126 3
            'yStart' => $boundaries->getMinY(),
127 3
            'yEnd' => $boundaries->getMaxY(),
128 3
            'systemId' => $boundaries->getParentId(),
129 3
            'cloakSystemId' => SpacecraftSystemTypeEnum::CLOAK->value
130 3
        ])->getResult();
131
    }
132
133 3
    #[Override]
134
    public function getColonyShieldData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
135
    {
136 3
        return $this->getEntityManager()->createNativeQuery(
137 3
            'SELECT sm.sx as x, sm.sy AS y,
138
            (SELECT COUNT(*) > 0
139
                FROM stu_colonies col
140
                JOIN stu_colonies_fielddata cfd
141
                ON col.id = cfd.colonies_id
142
                WHERE sm.id = col.starsystem_map_id
143
                AND cfd.aktiv = :active
144
                AND cfd.buildings_id IN (
145
                    SELECT bf.buildings_id
146
                    FROM stu_buildings_functions bf
147
                    WHERE bf.function = :shieldBuilding)) AS shieldstate
148
            FROM stu_sys_map sm
149
            WHERE sm.systems_id = :systemId
150 3
            AND sm.sx BETWEEN :xStart AND :xEnd AND sm.sy BETWEEN :yStart AND :yEnd',
151 3
            $rsm
152 3
        )->setParameters([
153 3
            'xStart' => $boundaries->getMinX(),
154 3
            'xEnd' => $boundaries->getMaxX(),
155 3
            'yStart' => $boundaries->getMinY(),
156 3
            'yEnd' => $boundaries->getMaxY(),
157 3
            'systemId' => $boundaries->getParentId(),
158 3
            'active' => 1,
159 3
            'shieldBuilding' => BuildingFunctionEnum::BUILDING_FUNCTION_SHIELD_GENERATOR
160 3
        ])->getResult();
161
    }
162
163
164
    #[Override]
165
    public function getBorderData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
166
    {
167
        return $this->getEntityManager()->createNativeQuery(
168
            'SELECT sm.sx AS x, sm.sy AS y
169
            FROM stu_sys_map sm
170
            WHERE sm.systems_id = :systemId
171
            AND sm.sx BETWEEN :xStart AND :xEnd
172
            AND sm.sy BETWEEN :yStart AND :yEnd',
173
            $rsm
174
        )->setParameters([
175
            'xStart' => $boundaries->getMinX(),
176
            'xEnd' => $boundaries->getMaxX(),
177
            'yStart' => $boundaries->getMinY(),
178
            'yEnd' => $boundaries->getMaxY(),
179
            'systemId' => $boundaries->getParentId()
180
        ])->getResult();
181
    }
182
183
    #[Override]
184
    public function getIgnoringSubspaceLayerData(PanelBoundaries $boundaries, int $ignoreId, ResultSetMapping $rsm): array
185
    {
186
        $maxAge = time() - FlightSignatureVisibilityEnum::SIG_VISIBILITY_UNCLOAKED;
187
188
        return $this->getEntityManager()->createNativeQuery(
189
            sprintf(
190
                'SELECT sm.sx as x, sm.sy AS y,
191
                (select count(distinct fs1.ship_id) from stu_flight_sig fs1
192
                where fs1.location_id = sm.id
193
                AND fs1.user_id != %1$d
194
                AND (fs1.from_direction = 1 OR fs1.to_direction = 1)
195
                AND fs1.time > %2$d) as d1c,
196
                (select count(distinct fs2.ship_id) from stu_flight_sig fs2
197
                where fs2.location_id = sm.id
198
                AND fs2.user_id != %1$d
199
                AND (fs2.from_direction = 2 OR fs2.to_direction = 2)
200
                AND fs2.time > %2$d) as d2c,
201
                (select count(distinct fs3.ship_id) from stu_flight_sig fs3
202
                where fs3.location_id = sm.id
203
                AND fs3.user_id != %1$d
204
                AND (fs3.from_direction = 3 OR fs3.to_direction = 3)
205
                AND fs3.time > %2$d) as d3c,
206
                (select count(distinct fs4.ship_id) from stu_flight_sig fs4
207
                where fs4.location_id = sm.id
208
                AND fs4.user_id != %1$d
209
                AND (fs4.from_direction = 4 OR fs4.to_direction = 4)
210
                AND fs4.time > %2$d) as d4c 
211
                FROM stu_sys_map sm
212
                WHERE sm.systems_id = :systemId
213
                AND sm.sx BETWEEN :xStart AND :xEnd AND sm.sy BETWEEN :yStart AND :yEnd',
214
                $ignoreId,
215
                $maxAge
216
            ),
217
            $rsm
218
        )->setParameters([
219
            'xStart' => $boundaries->getMinX(),
220
            'xEnd' => $boundaries->getMaxX(),
221
            'yStart' => $boundaries->getMinY(),
222
            'yEnd' => $boundaries->getMaxY(),
223
            'systemId' => $boundaries->getParentId()
224
        ])->getResult();
225
    }
226
227
    #[Override]
228
    public function getRandomSystemMapIdsForAstroMeasurement(int $starSystemId, int $location): array
229
    {
230
        $result = [];
231
232
        $rsm = new ResultSetMapping();
233
        $rsm->addScalarResult('id', 'id', 'integer');
234
235
        $userColonyFields = $this->getEntityManager()
236
            ->createNativeQuery(
237
                'SELECT sm.id as id
238
                FROM stu_sys_map sm
239
                WHERE sm.systems_id = :systemId
240
                AND sm.id != :location
241
                AND EXISTS (SELECT c.id
242
                            FROM stu_colonies c
243
                            WHERE c.starsystem_map_id = sm.id
244
                            AND c.user_id != :noOne)
245
                ORDER BY RANDOM()
246
                LIMIT 2',
247
                $rsm
248
            )
249
            ->setParameters([
250
                'systemId' => $starSystemId,
251
                'location'  => $location,
252
                'noOne' => UserEnum::USER_NOONE
253
            ])
254
            ->getResult();
255
256
        $result = array_merge($result, $userColonyFields);
257
258
        $otherColonyFields = $this->getEntityManager()
259
            ->createNativeQuery(
260
                'SELECT sm.id as id
261
                FROM stu_sys_map sm
262
                JOIN stu_location l
263
                ON sm.id = l.id
264
                JOIN stu_map_ftypes ft
265
                ON l.field_id = ft.id
266
                JOIN stu_colonies_classes cc
267
                ON ft.colonies_classes_id = cc.id
268
                WHERE sm.systems_id = :systemId
269
                AND ft.colonies_classes_id IS NOT NULL
270
                AND cc.type < 3
271
                AND sm.id NOT IN (:ids)
272
                ORDER BY RANDOM()
273
                LIMIT :theLimit',
274
                $rsm
275
            )
276
            ->setParameters([
277
                'systemId' => $starSystemId,
278
                'ids' => $result !== [] ? $result : [0],
279
                'theLimit' => AstronomicalMappingEnum::MEASUREMENT_COUNT - count($result)
280
            ])
281
            ->getResult();
282
283
        $result = array_merge($result, $otherColonyFields);
284
285
        if (count($result) < AstronomicalMappingEnum::MEASUREMENT_COUNT) {
286
            $otherFields = $this->getEntityManager()
287
                ->createNativeQuery(
288
                    'SELECT sm.id as id
289
                    FROM stu_sys_map sm
290
                    JOIN stu_location l
291
                    ON sm.id = l.id
292
                    JOIN stu_map_ftypes ft
293
                    ON l.field_id = ft.id
294
                    WHERE sm.systems_id = :systemId
295
                    AND ft.x_damage_system <= 10
296
                    AND ft.x_damage <= 10
297
                    ORDER BY RANDOM()
298
                    LIMIT :theLimit',
299
                    $rsm
300
                )
301
                ->setParameters([
302
                    'systemId' => $starSystemId,
303
                    'theLimit' => AstronomicalMappingEnum::MEASUREMENT_COUNT - count($result)
304
                ])
305
                ->getResult();
306
307
            $result = array_merge($result, $otherFields);
308
        }
309
310
        return array_map(fn(array $data) => $data['id'], $result);
311
    }
312
313
    #[Override]
314
    public function prototype(): StarSystemMapInterface
315
    {
316
        return new StarSystemMap();
317
    }
318
319
    #[Override]
320
    public function save(StarSystemMapInterface $starSystemMap): void
321
    {
322
        $em = $this->getEntityManager();
323
324
        $em->persist($starSystemMap);
325
    }
326
327
    #[Override]
328
    public function truncateByStarSystem(StarSystemInterface $starSystem): void
329
    {
330
        $this->getEntityManager()->createQuery(
331
            sprintf(
332
                'DELETE FROM %s sm WHERE sm.systems_id = :systemId',
333
                StarSystemMap::class
334
            )
335
        )
336
            ->setParameters(['systemId' => $starSystem->getId()])
337
            ->execute();
338
    }
339
}
340