Passed
Pull Request — master (#1657)
by Nico
09:45
created

ColonyRepository::getStartingByFaction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 23
nc 1
nop 1
dl 0
loc 28
ccs 0
cts 19
cp 0
crap 2
rs 9.552
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\Module\Commodity\CommodityTypeEnum;
10
use Stu\Module\PlayerSetting\Lib\UserEnum;
11
use Stu\Orm\Entity\Colony;
12
use Stu\Orm\Entity\ColonyClass;
13
use Stu\Orm\Entity\ColonyInterface;
14
use Stu\Orm\Entity\Map;
15
use Stu\Orm\Entity\MapRegionSettlement;
16
use Stu\Orm\Entity\StarSystemMap;
17
use Stu\Orm\Entity\StarSystemMapInterface;
18
use Stu\Orm\Entity\UserInterface;
19
20
/**
21
 * @extends EntityRepository<Colony>
22
 */
23
final class ColonyRepository extends EntityRepository implements ColonyRepositoryInterface
24
{
25
    public function prototype(): ColonyInterface
26
    {
27
        return new Colony();
28
    }
29
30
    public function save(ColonyInterface $colony): void
31
    {
32
        $em = $this->getEntityManager();
33
34
        $em->persist($colony);
35
    }
36
37
    public function delete(ColonyInterface $colony): void
38
    {
39
        $em = $this->getEntityManager();
40
41
        $em->remove($colony);
42
        $em->flush();
43
    }
44
45
    public function getAmountByUser(UserInterface $user, int $colonyType): int
46
    {
47
        return (int) $this->getEntityManager()
48
            ->createQuery(
49
                sprintf(
50
                    'SELECT count(c.id) from %s c WHERE c.user_id = :userId AND c.colonies_classes_id IN (
51
                        SELECT cc.id FROM %s cc WHERE cc.type = :type
52
                    )',
53
                    Colony::class,
54
                    ColonyClass::class
55
                )
56
            )
57
            ->setParameters([
58
                'userId' => $user,
59
                'type' => $colonyType
60
            ])
61
            ->getSingleScalarResult();
62
    }
63
64
    public function getStartingByFaction(int $factionId): array
65
    {
66
        return $this->getEntityManager()
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->getEntityM...actionId))->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
67
            ->createQuery(
68
                sprintf(
69
                    'SELECT c FROM %s c INDEX BY c.id
70
                     JOIN %s sm
71
                     WITH c.starsystem_map_id = sm.id
72
                     WHERE c.user_id = :userId AND c.colonies_classes_id IN (
73
                        SELECT pt.id FROM %s pt WHERE pt.allow_start = :allowStart
74
                    ) AND sm.systems_id IN (
75
                        SELECT m.systems_id FROM %s m WHERE m.systems_id > 0 AND m.admin_region_id IN (
76
                            SELECT mrs.region_id from %s mrs WHERE mrs.faction_id = :factionId
77
                        )
78
                    )',
79
                    Colony::class,
80
                    StarSystemMap::class,
81
                    ColonyClass::class,
82
                    Map::class,
83
                    MapRegionSettlement::class
84
                )
85
            )
86
            ->setParameters([
87
                'allowStart' => 1,
88
                'userId' => UserEnum::USER_NOONE,
89
                'factionId' => $factionId
90
            ])
91
            ->getResult();
92
    }
93
94
    public function getByPosition(StarSystemMapInterface $sysmap): ?ColonyInterface
95
    {
96
        return $this->findOneBy([
97
            'starsystem_map_id' => $sysmap->getId()
98
        ]);
99
    }
100
101
    public function getForeignColoniesInBroadcastRange(
102
        StarSystemMapInterface $systemMap,
103
        UserInterface $user
104
    ): array {
105
        return $this->getEntityManager()
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->getEntityM...>getSy()))->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
106
            ->createQuery(
107
                sprintf(
108
                    'SELECT c FROM %s c
109
                     JOIN %s sm
110
                     WITH c.starsystem_map_id = sm.id
111
                     WHERE c.user_id NOT IN (:ignoreIds)
112
                     AND sm.systems_id = :systemId
113
                     AND sm.sx BETWEEN (:sx - 1) AND (:sx + 1)
114
                     AND sm.sy BETWEEN (:sy - 1) AND (:sy + 1)',
115
                    Colony::class,
116
                    StarSystemMap::class
117
                )
118
            )
119
            ->setParameters([
120
                'ignoreIds' => [$user->getId(), UserEnum::USER_NOONE],
121
                'systemId' => $systemMap->getSystem()->getId(),
122
                'sx' => $systemMap->getSx(),
123
                'sy' => $systemMap->getSy()
124
            ])
125
            ->getResult();
126
    }
127
128
    public function getByBatchGroup(int $batchGroup, int $batchGroupCount): iterable
129
    {
130
        return $this->getEntityManager()
131
            ->createQuery(
132
                sprintf(
133
                    'SELECT c FROM %s c
134
                    WHERE MOD(c.user_id, :groupCount) + 1 = :groupId
135
                    AND c.user_id != :userId',
136
                    Colony::class
137
                )
138
            )
139
            ->setParameters([
140
                'groupId' => $batchGroup,
141
                'groupCount' => $batchGroupCount,
142
                'userId' => UserEnum::USER_NOONE
143
            ])
144
            ->getResult();
145
    }
146
147
    public function getColonized(): iterable
148
    {
149
        return $this->getEntityManager()
150
            ->createQuery(
151
                sprintf(
152
                    'SELECT c FROM %s c WHERE c.user_id != :userId',
153
                    Colony::class
154
                )
155
            )
156
            ->setParameters([
157
                'userId' => UserEnum::USER_NOONE,
158
            ])
159
            ->getResult();
160
    }
161
162
    public function getColonyListForRenderFragment(UserInterface $user): array
163
    {
164
        $rsm = new ResultSetMapping();
165
        $rsm->addScalarResult('colonyid', 'colonyid', 'integer');
166
        $rsm->addScalarResult('classid', 'classid', 'integer');
167
        $rsm->addScalarResult('type', 'type', 'integer');
168
        $rsm->addScalarResult('nameandsector', 'nameandsector', 'string');
169
170
        return $this->getEntityManager()
171
            ->createNativeQuery(
172
                'SELECT c.id AS colonyid, cc.id AS classid, cc.type AS type,
173
                            concat(c.name, \' \', sm.sx, \'|\', sm.sy, \' (\', s.name, \'-\',
174
                                    CASE WHEN s.is_wormhole
175
                                        THEN \'Wurmloch\'
176
                                        ELSE \'System\'
177
                                    END, \')\') as nameandsector
178
                        FROM stu_colonies c
179
                        JOIN stu_colonies_classes cc
180
                            ON c.colonies_classes_id = cc.id
181
                        JOIN stu_sys_map sm
182
                            ON c.starsystem_map_id = sm.id
183
                        JOIN stu_systems s
184
                            ON sm.systems_id = s.id
185
                        WHERE c.user_id = :userId
186
                        ORDER BY cc.id ASC, c.id ASC',
187
                $rsm
188
            )
189
            ->setParameter('userId', $user->getId())
190
            ->getResult();
191
    }
192
193
    public function getColoniesNetWorth(): array
194
    {
195
        $rsm = new ResultSetMapping();
196
        $rsm->addScalarResult('user_id', 'user_id', 'integer');
197
        $rsm->addScalarResult('commodity_id', 'commodity_id', 'integer');
198
        $rsm->addScalarResult('sum', 'sum', 'integer');
199
200
        return $this->getEntityManager()
201
            ->createNativeQuery(
202
                'SELECT u.id as user_id, bc.commodity_id AS commodity_id, SUM(bc.count) AS sum
203
                FROM stu_user u
204
                JOIN stu_colonies c
205
                ON u.id = c.user_id 
206
                JOIN stu_colonies_fielddata cf
207
                ON cf.colonies_id = c.id
208
                JOIN stu_buildings_cost bc 
209
                ON cf.buildings_id = bc.buildings_id 
210
                WHERE u.id >= :firstUserId
211
                AND cf.buildings_id IS NOT NULL
212
                AND cf.aktiv = 1
213
                GROUP BY u.id, bc.commodity_id',
214
                $rsm
215
            )
216
            ->setParameters(['firstUserId' => UserEnum::USER_FIRST_ID])
217
            ->getResult();
218
    }
219
220
    public function getSatisfiedWorkerTop10(): array
221
    {
222
        $rsm = new ResultSetMapping();
223
        $rsm->addScalarResult('user_id', 'user_id', 'integer');
224
        $rsm->addScalarResult('satisfied', 'satisfied', 'integer');
225
226
        return $this->getEntityManager()
227
            ->createNativeQuery(
228
                'SELECT c.user_id,
229
                    LEAST(
230
                        COALESCE(SUM(c.bev_work), 0),
231
                        (SELECT COALESCE(SUM(bc.count), 0)
232
                        FROM stu_colonies c2
233
                        JOIN stu_colonies_fielddata cf
234
                        ON cf.colonies_id = c2.id
235
                        JOIN stu_buildings b
236
                        ON cf.buildings_id = b.id
237
                        JOIN stu_buildings_commodity bc
238
                        ON b.id = bc.buildings_id
239
                        WHERE c2.user_id = c.user_id
240
                        AND bc.commodity_id = :lifeStandard
241
                        AND cf.aktiv = 1)
242
                    ) AS satisfied
243
                FROM stu_colonies c
244
                WHERE c.user_id >= :firstUserId
245
                GROUP BY c.user_id
246
                ORDER BY satisfied DESC
247
                LIMIT 10',
248
                $rsm
249
            )
250
            ->setParameters([
251
                'firstUserId' => UserEnum::USER_FIRST_ID,
252
                'lifeStandard' => CommodityTypeEnum::COMMODITY_EFFECT_LIFE_STANDARD
253
            ])
254
            ->getResult();
255
    }
256
}
257