Passed
Push — dev ( 9416fb...0448bb )
by Janko
12:17 queued 03:07
created

SpacecraftRepository::getByLocation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 28
nc 1
nop 1
dl 0
loc 33
ccs 0
cts 18
cp 0
crap 2
rs 9.472
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 Stu\Component\Anomaly\Type\AnomalyTypeEnum;
11
use Stu\Component\Spacecraft\SpacecraftStateEnum;
12
use Stu\Component\Spacecraft\System\SpacecraftSystemModeEnum;
13
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
14
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...
15
use Stu\Module\Spacecraft\Lib\ShipRumpSpecialAbilityEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Module\Spacecraft\Li...pRumpSpecialAbilityEnum 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\Anomaly;
17
use Stu\Orm\Entity\SpacecraftBuildplan;
18
use Stu\Orm\Entity\CrewAssignment;
19
use Stu\Orm\Entity\ShipRumpSpecial;
20
use Stu\Orm\Entity\Spacecraft;
21
use Stu\Orm\Entity\SpacecraftInterface;
22
use Stu\Orm\Entity\SpacecraftSystem;
23
use Stu\Orm\Entity\User;
24
use Stu\Orm\Entity\UserInterface;
25
26
/**
27
 * @extends EntityRepository<Spacecraft>
28
 */
29
final class SpacecraftRepository extends EntityRepository implements SpacecraftRepositoryInterface
30
{
31
    #[Override]
32
    public function save(SpacecraftInterface $spacecraft): void
33
    {
34
        $em = $this->getEntityManager();
35
36
        $em->persist($spacecraft);
37
    }
38
39
    #[Override]
40
    public function delete(SpacecraftInterface $spacecraft): void
41
    {
42
        $em = $this->getEntityManager();
43
44
        $em->remove($spacecraft);
45
    }
46
47
    #[Override]
48
    public function getAmountByUserAndSpecialAbility(
49
        int $userId,
50
        int $specialAbilityId
51
    ): int {
52
        return (int) $this->getEntityManager()->createQuery(
53
            sprintf(
54
                'SELECT COUNT(s)
55
                FROM %s s
56
                JOIN %s bp
57
                WITH s.plan_id = bp.id
58
                WHERE s.user_id = :userId AND s.rump_id IN (
59
                    SELECT rs.rump_id FROM %s rs WHERE rs.special = :specialAbilityId
60
                )
61
                %s',
62
                Spacecraft::class,
63
                SpacecraftBuildplan::class,
64
                ShipRumpSpecial::class,
65
                $specialAbilityId === ShipRumpSpecialAbilityEnum::COLONIZE ? 'AND bp.crew = 0' : ''
66
            )
67
        )->setParameters([
68
            'userId' => $userId,
69
            'specialAbilityId' => $specialAbilityId,
70
        ])->getSingleScalarResult();
71
    }
72
73
    #[Override]
74
    public function getAmountByUserAndRump(int $userId, int $rumpId): int
75
    {
76
        return $this->count([
77
            'user_id' => $userId,
78
            'rump_id' => $rumpId,
79
        ]);
80
    }
81
82
    #[Override]
83
    public function getByUser(UserInterface $user): array
84
    {
85
        return $this->findBy([
86
            'user_id' => $user,
87
        ]);
88
    }
89
90
    #[Override]
91
    public function getSuitableForShieldRegeneration(int $regenerationThreshold): array
92
    {
93
        return $this->getEntityManager()->createQuery(
94
            sprintf(
95
                'SELECT s FROM %s s
96
                JOIN %s ss
97
                WITH s.id = ss.spacecraft_id
98
                JOIN %s bp
99
                WITH s.plan_id = bp.id
100
                WHERE ss.system_type = :shieldType
101
                AND ss.mode < :modeOn
102
                AND s.schilde<s.max_schilde
103
                AND s.shield_regeneration_timer <= :regenerationThreshold
104
                AND (SELECT count(sc.id) FROM %s sc WHERE s.id = sc.spacecraft_id) >= bp.crew
105
                AND NOT EXISTS (SELECT a FROM %s a
106
                                WHERE a.location_id = s.location_id
107
                                AND a.anomaly_type_id = :anomalyType
108
                                AND a.remaining_ticks > 0)',
109
                Spacecraft::class,
110
                SpacecraftSystem::class,
111
                SpacecraftBuildplan::class,
112
                CrewAssignment::class,
113
                Anomaly::class
114
            )
115
        )->setParameters([
116
            'shieldType' => SpacecraftSystemTypeEnum::SHIELDS->value,
117
            'modeOn' => SpacecraftSystemModeEnum::MODE_ON->value,
118
            'regenerationThreshold' => $regenerationThreshold,
119
            'anomalyType' => AnomalyTypeEnum::SUBSPACE_ELLIPSE
120
        ])->getResult();
121
    }
122
123
    #[Override]
124
    public function getPlayerSpacecraftsForTick(): iterable
125
    {
126
        return $this->getEntityManager()->createQuery(
127
            sprintf(
128
                'SELECT s
129
                FROM %s s
130
                JOIN %s p
131
                WITH s.plan_id = p.id
132
                JOIN %s u
133
                WITH s.user_id = u.id
134
                WHERE s.user_id > :firstUserId
135
                AND (   ((SELECT count(sc.id)
136
                        FROM %s sc
137
                        WHERE sc.spacecraft_id = s.id) > 0)
138
                    OR
139
                        (s.state IN (:scrapping, :underConstruction))
140
                    OR
141
                        (p.crew = 0))
142
                AND (u.vac_active = :false OR u.vac_request_date > :vacationThreshold)',
143
                Spacecraft::class,
144
                SpacecraftBuildplan::class,
145
                User::class,
146
                CrewAssignment::class
147
            )
148
        )->setParameters([
149
            'underConstruction' => SpacecraftStateEnum::SHIP_STATE_UNDER_CONSTRUCTION,
150
            'scrapping' => SpacecraftStateEnum::SHIP_STATE_UNDER_SCRAPPING,
151
            'vacationThreshold' => time() - UserEnum::VACATION_DELAY_IN_SECONDS,
152
            'firstUserId' => UserEnum::USER_FIRST_ID,
153
            'false' => false
154
        ])->toIterable();
155
    }
156
157
    #[Override]
158
    public function getNpcSpacecraftsForTick(): array
159
    {
160
        return $this->getEntityManager()->createQuery(
161
            sprintf(
162
                'SELECT s FROM %s s WHERE s.user_id BETWEEN 2 AND (:firstUserId - 1)',
163
                Spacecraft::class
164
            )
165
        )->setParameter('firstUserId', UserEnum::USER_FIRST_ID)->getResult();
166
    }
167
168
    #[Override]
169
    public function isCloakedSpacecraftAtLocation(
170
        SpacecraftInterface $spacecraft
171
    ): bool {
172
173
        $result = $this->getEntityManager()->createQuery(
174
            sprintf(
175
                'SELECT COUNT(s.id) FROM %s s
176
                    WHERE s.location = :location
177
                    AND EXISTS (SELECT ss.id
178
                            FROM %s ss
179
                            WHERE s = ss.spacecraft
180
                            AND ss.system_type = %d
181
                            AND ss.mode > 1)
182
                    AND s.user != :ignoreUser',
183
                Spacecraft::class,
184
                SpacecraftSystem::class,
185
                SpacecraftSystemTypeEnum::CLOAK->value
186
            )
187
        )->setParameters([
188
            'location' => $spacecraft->getLocation(),
189
            'ignoreUser' => $spacecraft->getUser()
190
        ])->getSingleScalarResult();
191
192
        return $result > 0;
193
    }
194
195
    #[Override]
196
    public function getRandomSpacecraftIdWithCrewByUser(int $userId): ?int
197
    {
198
        $rsm = new ResultSetMapping();
199
        $rsm->addScalarResult('id', 'id', 'integer');
200
201
        $result = $this->getEntityManager()
202
            ->createNativeQuery(
203
                'SELECT s.id as id FROM stu_spacecraft s
204
                WHERE s.user_id = :userId
205
                AND EXISTS (SELECT sc.id
206
                            FROM stu_crew_assign sc
207
                            WHERE s.id = sc.spacecraft_id)
208
                ORDER BY RANDOM()
209
                LIMIT 1',
210
                $rsm
211
            )
212
            ->setParameters([
213
                'userId' => $userId
214
            ])
215
            ->getOneOrNullResult();
216
217
        return $result != null ? $result['id'] : null;
218
    }
219
220
    #[Override]
221
    public function getAllTractoringSpacecrafts(): array
222
    {
223
        return $this->getEntityManager()->createQuery(
224
            sprintf(
225
                'SELECT s FROM %s s
226
                WHERE s.tractored_ship_id IS NOT NULL',
227
                Spacecraft::class
228
            )
229
        )->getResult();
230
    }
231
232
    #[Override]
233
    public function truncateAllSpacecrafts(): void
234
    {
235
        $this->getEntityManager()->createQuery(
236
            sprintf(
237
                'DELETE FROM %s s',
238
                Spacecraft::class
239
            )
240
        )->execute();
241
    }
242
}
243