Passed
Push — dev ( 7c9db0...a5a4e7 )
by Janko
05:30
created

ShipRepository::getAllDockedShips()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 10
ccs 0
cts 8
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 Stu\Component\Game\TimeConstants;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Game\TimeConstants 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\Spacecraft\SpacecraftRumpCategoryEnum;
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\UserConstants;
0 ignored issues
show
Bug introduced by
The type Stu\Module\PlayerSetting\Lib\UserConstants 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\PlayerSetting\Lib\UserStateEnum;
16
use Stu\Module\Ship\Lib\TFleetShipItem;
17
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
18
use Stu\Orm\Entity\Location;
19
use Stu\Orm\Entity\Map;
20
use Stu\Orm\Entity\PirateWrath;
21
use Stu\Orm\Entity\Ship;
22
use Stu\Orm\Entity\CrewAssignment;
23
use Stu\Orm\Entity\SpacecraftRump;
24
use Stu\Orm\Entity\Spacecraft;
25
use Stu\Orm\Entity\SpacecraftCondition;
26
use Stu\Orm\Entity\StarSystemMap;
27
use Stu\Orm\Entity\Station;
28
use Stu\Orm\Entity\Storage;
29
use Stu\Orm\Entity\User;
30
use Stu\Orm\Entity\UserRegistration;
31
32
/**
33
 * @extends EntityRepository<Ship>
34
 */
35
final class ShipRepository extends EntityRepository implements ShipRepositoryInterface
36
{
37 1
    #[\Override]
38
    public function save(Ship $post): void
39
    {
40 1
        $em = $this->getEntityManager();
41
42 1
        $em->persist($post);
43
    }
44
45
    #[\Override]
46
    public function delete(Ship $post): void
47
    {
48
        $em = $this->getEntityManager();
49
50
        $em->remove($post);
51
    }
52
53 2
    #[\Override]
54
    public function getByUserAndFleet(int $userId, ?int $fleetId): array
55
    {
56 2
        return $this->findBy(
57 2
            [
58 2
                'user_id' => $userId,
59 2
                'fleet_id' => $fleetId
60 2
            ],
61 2
            ['id' => 'asc']
62 2
        );
63
    }
64
65 1
    #[\Override]
66
    public function getByLocationAndUser(Location $location, User $user): array
67
    {
68 1
        return $this->findBy([
69 1
            'user' => $user,
70 1
            'location' => $location,
71 1
        ], [
72 1
            'fleet_id' => 'desc',
73 1
            'is_fleet_leader' => 'desc',
74 1
            'id' => 'desc'
75 1
        ]);
76
    }
77
78 1
    #[\Override]
79
    public function getPossibleFleetMembers(Ship $fleetLeader): array
80
    {
81 1
        return $this->getEntityManager()->createQuery(
82 1
            sprintf(
83 1
                'SELECT s FROM %s s
84
                JOIN %s sp
85
                WITH s.id = sp.id
86
                JOIN %s sc
87
                WITH sp = sc.spacecraft
88
                WHERE sp.location = :location
89
                AND s.fleet_id IS NULL
90
                AND sp.user = :user
91
                AND sc.state != :state
92 1
                ORDER BY sp.rump_id ASC, sp.name ASC',
93 1
                Ship::class,
94 1
                Spacecraft::class,
95 1
                SpacecraftCondition::class
96 1
            )
97 1
        )->setParameters([
98 1
            'location' => $fleetLeader->getLocation(),
99 1
            'state' => SpacecraftStateEnum::RETROFIT,
100 1
            'user' => $fleetLeader->getUser()
101 1
        ])->getResult();
102
    }
103
104
    #[\Override]
105
    public function getWithTradeLicensePayment(
106
        int $userId,
107
        Station $tradePostStation,
108
        int $commodityId,
109
        int $amount
110
    ): array {
111
        return $this->getEntityManager()->createQuery(
112
            sprintf(
113
                'SELECT s FROM %s s
114
                WHERE s.user_id = :userId
115
                AND s.dockedTo = :tradePostStation
116
                AND s.id IN (
117
                    SELECT st.spacecraft_id
118
                    FROM %s st
119
                    WHERE st.commodity_id = :commodityId
120
                    AND st.count >= :amount
121
                )',
122
                Ship::class,
123
                Storage::class
124
            )
125
        )->setParameters([
126
            'userId' => $userId,
127
            'tradePostStation' => $tradePostStation,
128
            'commodityId' => $commodityId,
129
            'amount' => $amount,
130
        ])->getResult();
131
    }
132
133 1
    #[\Override]
134
    public function getEscapePods(): array
135
    {
136 1
        return $this->getEntityManager()->createQuery(
137 1
            sprintf(
138 1
                'SELECT s FROM %s s
139
                LEFT JOIN %s sr
140
                WITH s.rump_id = sr.id
141 1
                WHERE sr.category_id = :categoryId',
142 1
                Ship::class,
143 1
                SpacecraftRump::class
144 1
            )
145 1
        )->setParameters([
146 1
            'categoryId' => SpacecraftRumpCategoryEnum::ESCAPE_PODS->value
147 1
        ])->getResult();
148
    }
149
150
    #[\Override]
151
    public function getEscapePodsByCrewOwner(User $user): array
152
    {
153
        return $this->getEntityManager()->createQuery(
154
            sprintf(
155
                'SELECT s FROM %s s
156
                LEFT JOIN %s sr
157
                WITH s.rump_id = sr.id
158
                LEFT JOIN %s sc
159
                WITH sc.spacecraft = s
160
                WHERE sr.category_id = :categoryId
161
                AND sc.user = :user',
162
                Ship::class,
163
                SpacecraftRump::class,
164
                CrewAssignment::class
165
            )
166
        )->setParameters([
167
            'categoryId' => SpacecraftRumpCategoryEnum::ESCAPE_PODS->value,
168
            'user' => $user
169
        ])->getResult();
170
    }
171
172 1
    #[\Override]
173
    public function getFleetShipsScannerResults(
174
        Spacecraft $spacecraft,
175
        bool $showCloaked = false,
176
        Map|StarSystemMap|null $field = null
177
    ): array {
178
179 1
        $rsm = new ResultSetMapping();
180 1
        $rsm->addEntityResult(TFleetShipItem::class, 's');
181 1
        $rsm->addFieldResult('s', 'fleetname', 'fleet_name');
182 1
        $rsm->addFieldResult('s', 'isdefending', 'is_defending');
183 1
        $rsm->addFieldResult('s', 'isblocking', 'is_blocking');
184 1
        TFleetShipItem::addTSpacecraftItemFields($rsm);
185
186 1
        $location = $field ?? $spacecraft->getLocation();
187
188 1
        $query = $this->getEntityManager()->createNativeQuery(
189 1
            sprintf(
190 1
                'SELECT f.id as fleetid, f.name as fleetname, f.defended_colony_id is not null as isdefending,
191
                    f.blocked_colony_id is not null as isblocking, s.id as shipid, sp.rump_id as rumpid,
192
                    ss.mode as warpstate, twd.mode as tractorwarpstate, COALESCE(ss2.mode,0) as cloakstate, ss3.mode as shieldstate,
193
                    COALESCE(ss4.status,0) as uplinkstate, sp.type as spacecrafttype, sp.name as shipname,
194
                    sc.hull as hull, sp.max_huelle as maxhull, sc.shield as shield, sp.holding_web_id as webid, tw.finished_time as webfinishtime,
195
                    u.id as userid, u.username, r.category_id as rumpcategoryid, r.name as rumpname, r.role_id as rumproleid,
196
                    (SELECT count(*) > 0 FROM stu_ship_log sl WHERE sl.spacecraft_id = s.id AND sl.is_private = :false) as haslogbook,
197
                    (SELECT count(*) > 0 FROM stu_crew_assign ca WHERE ca.spacecraft_id = s.id) as hascrew
198
                FROM stu_ship s
199
                JOIN stu_spacecraft sp
200
                ON s.id = sp.id
201
                JOIN stu_spacecraft_condition sc
202
                ON sp.id = sc.spacecraft_id
203
                LEFT JOIN stu_spacecraft_system ss
204
                ON s.id = ss.spacecraft_id
205
                AND ss.system_type = :warpdriveType
206
                LEFT JOIN stu_spacecraft tractor
207
                ON tractor.tractored_ship_id = s.id
208
                LEFT JOIN stu_spacecraft_system twd
209
                ON tractor.id = twd.spacecraft_id
210
                AND twd.system_type = :warpdriveType
211
                LEFT JOIN stu_spacecraft_system ss2
212
                ON s.id = ss2.spacecraft_id
213
                AND ss2.system_type = :cloakType
214
                LEFT JOIN stu_spacecraft_system ss3
215
                ON s.id = ss3.spacecraft_id
216
                AND ss3.system_type = :shieldType
217
                LEFT JOIN stu_spacecraft_system ss4
218
                ON s.id = ss4.spacecraft_id
219
                AND ss4.system_type = :uplinkType
220
                JOIN stu_rump r
221
                ON sp.rump_id = r.id
222
                JOIN stu_fleets f
223
                ON s.fleet_id = f.id
224
                LEFT OUTER JOIN stu_tholian_web tw
225
                ON sp.holding_web_id = tw.id
226
                JOIN stu_user u
227
                ON sp.user_id = u.id
228
                WHERE sp.location_id = :locationId
229
                AND s.id != :ignoreId
230
                %s
231 1
                ORDER BY f.sort DESC, f.id DESC, (CASE WHEN s.is_fleet_leader = :true THEN 0 ELSE 1 END), r.category_id ASC, r.role_id ASC, r.id ASC, sp.name ASC',
232 1
                $showCloaked ? '' : sprintf(' AND (sp.user_id = %d OR COALESCE(ss2.mode,0) < %d) ', $spacecraft->getUser()->getId(), SpacecraftSystemModeEnum::MODE_ON->value)
233 1
            ),
234 1
            $rsm
235 1
        )->setParameters([
236 1
            'locationId' => $location->getId(),
0 ignored issues
show
Bug introduced by
The method getId() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

236
            'locationId' => $location->/** @scrutinizer ignore-call */ getId(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
237 1
            'ignoreId' => $spacecraft->getId(),
238 1
            'cloakType' => SpacecraftSystemTypeEnum::CLOAK->value,
239 1
            'warpdriveType' => SpacecraftSystemTypeEnum::WARPDRIVE->value,
240 1
            'shieldType' => SpacecraftSystemTypeEnum::SHIELDS->value,
241 1
            'uplinkType' => SpacecraftSystemTypeEnum::UPLINK->value,
242 1
            'false' => false,
243 1
            'true' => true
244 1
        ]);
245
246 1
        return $query->getResult();
247
    }
248
249
    #[\Override]
250
    public function getPirateTargets(SpacecraftWrapperInterface $wrapper): array
251
    {
252
        $layer = $wrapper->get()->getLayer();
253
        if ($layer === null) {
254
            return [];
255
        }
256
257
        $location = $wrapper->get()->getLocation();
258
        $range = $wrapper->getLssSystemData()?->getSensorRange() ?? 0;
259
260
        return $this->getEntityManager()->createQuery(
261
            sprintf(
262
                'SELECT s FROM %s s
263
                JOIN %s l
264
                WITH s.location = l.id
265
                JOIN %s u
266
                WITH s.user = u
267
                JOIN %s ur
268
                WITH ur.user = u
269
                LEFT JOIN %s w
270
                WITH u = w.user
271
                WHERE l.layer = :layer
272
                AND l.cx BETWEEN :minX AND :maxX
273
                AND l.cy BETWEEN :minY AND :maxY
274
                AND (s.fleet_id IS NULL OR s.is_fleet_leader = :true)
275
                AND u.id >= :firstUserId
276
                AND u.state >= :stateActive
277
                AND ur.creation < :eightWeeksEarlier
278
                AND (u.vac_active = :false OR u.vac_request_date > :vacationThreshold)
279
                AND COALESCE(w.protection_timeout, 0) < :currentTime',
280
                Ship::class,
281
                Location::class,
282
                User::class,
283
                UserRegistration::class,
284
                PirateWrath::class
285
            )
286
        )
287
            ->setParameters([
288
                'minX' => $location->getCx() - $range,
289
                'maxX' => $location->getCx() + $range,
290
                'minY' => $location->getCy() - $range,
291
                'maxY' => $location->getCy() + $range,
292
                'layer' => $layer,
293
                'firstUserId' => UserConstants::USER_FIRST_ID,
294
                'stateActive' => UserStateEnum::ACTIVE->value,
295
                'eightWeeksEarlier' => time() - TimeConstants::EIGHT_WEEKS_IN_SECONDS,
296
                'vacationThreshold' => time() - UserConstants::VACATION_DELAY_IN_SECONDS,
297
                'currentTime' => time(),
298
                'true' => true,
299
                'false' => false
300
            ])
301
            ->getResult();
302
    }
303
304
    #[\Override]
305
    public function getPirateFriends(SpacecraftWrapperInterface $wrapper): array
306
    {
307
        $layer = $wrapper->get()->getLayer();
308
        if ($layer === null) {
309
            return [];
310
        }
311
312
        $location = $wrapper->get()->getLocation();
313
        $range = $wrapper->getLssSystemData()?->getSensorRange() ?? 0;
314
315
        return $this->getEntityManager()->createQuery(
316
            sprintf(
317
                'SELECT s FROM %s s
318
                JOIN %s l
319
                WITH s.location_id = l.id
320
                WHERE l.layer = :layer
321
                AND l.cx BETWEEN :minX AND :maxX
322
                AND l.cy BETWEEN :minY AND :maxY
323
                AND s.id != :shipId
324
                AND s.user_id = :kazonUserId',
325
                Ship::class,
326
                Location::class
327
            )
328
        )
329
            ->setParameters([
330
                'minX' => $location->getCx() - $range,
331
                'maxX' => $location->getCx() + $range,
332
                'minY' => $location->getCy() - $range,
333
                'maxY' => $location->getCy() + $range,
334
                'layer' => $layer,
335
                'shipId' => $wrapper->get()->getId(),
336
                'kazonUserId' => UserConstants::USER_NPC_KAZON
337
            ])
338
            ->getResult();
339
    }
340
341 2
    #[\Override]
342
    public function getByUserAndRump(User $user, SpacecraftRump $rump): array
343
    {
344 2
        return $this->findBy([
345 2
            'user_id' => $user->getId(),
346 2
            'rump_id' => $rump->getId()
347 2
        ], [
348 2
            'location_id' => 'asc',
349 2
            'fleet_id' => 'asc',
350 2
            'is_fleet_leader' => 'desc'
351 2
        ]);
352
    }
353
}
354