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\Ship\FlightSignatureVisibilityEnum; |
10
|
|
|
use Stu\Module\PlayerSetting\Lib\UserEnum; |
11
|
|
|
use Stu\Orm\Entity\ColonyInterface; |
12
|
|
|
use Stu\Orm\Entity\FlightSignature; |
13
|
|
|
use Stu\Orm\Entity\FlightSignatureInterface; |
14
|
|
|
use Stu\Orm\Entity\StarSystemMap; |
15
|
|
|
use Stu\Orm\Entity\UserInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @extends EntityRepository<FlightSignature> |
19
|
|
|
*/ |
20
|
|
|
final class FlightSignatureRepository extends EntityRepository implements FlightSignatureRepositoryInterface |
21
|
|
|
{ |
22
|
1 |
|
public function prototype(): FlightSignatureInterface |
23
|
|
|
{ |
24
|
1 |
|
return new FlightSignature(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function saveAll(array $array): void |
28
|
|
|
{ |
29
|
|
|
$em = $this->getEntityManager(); |
30
|
|
|
|
31
|
|
|
foreach ($array as $obj) { |
32
|
|
|
$em->persist($obj); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
1 |
|
public function save(FlightSignatureInterface $item): void |
37
|
|
|
{ |
38
|
1 |
|
$em = $this->getEntityManager(); |
39
|
1 |
|
$em->persist($item); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getVisibleSignatureCount(ColonyInterface $colony): int |
43
|
|
|
{ |
44
|
|
|
return (int) $this->getEntityManager() |
45
|
|
|
->createQuery( |
46
|
|
|
sprintf( |
47
|
|
|
'SELECT count(DISTINCT CONCAT(fs.ship_id, fs.ship_name)) as count |
48
|
|
|
FROM %s fs |
49
|
|
|
JOIN %s ssm |
50
|
|
|
WITH fs.starsystem_map_id = ssm.id |
51
|
|
|
WHERE (fs.is_cloaked = false AND fs.time > :maxAgeUncloaked |
52
|
|
|
OR fs.is_cloaked = true AND fs.time > :maxAgeCloaked) |
53
|
|
|
AND ssm.sx = :sx |
54
|
|
|
AND ssm.sy = :sy |
55
|
|
|
AND ssm.systems_id = :systemsId |
56
|
|
|
AND fs.user_id != :ignoreId', |
57
|
|
|
FlightSignature::class, |
58
|
|
|
StarSystemMap::class |
59
|
|
|
) |
60
|
|
|
) |
61
|
|
|
->setParameters([ |
62
|
|
|
'maxAgeUncloaked' => time() - FlightSignatureVisibilityEnum::SIG_VISIBILITY_UNCLOAKED, |
63
|
|
|
'maxAgeCloaked' => time() - FlightSignatureVisibilityEnum::SIG_VISIBILITY_CLOAKED, |
64
|
|
|
'sx' => $colony->getSx(), |
65
|
|
|
'sy' => $colony->getSy(), |
66
|
|
|
'systemsId' => $colony->getSystem()->getId(), |
67
|
|
|
'ignoreId' => $colony->getUserId() |
68
|
|
|
]) |
69
|
|
|
->getSingleScalarResult(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
|
74
|
|
|
|
75
|
|
|
public function getVisibleSignatures(int $fieldId, bool $isSystem, int $ignoreId): array |
76
|
|
|
{ |
77
|
|
|
return $this->getEntityManager() |
|
|
|
|
78
|
|
|
->createQuery( |
79
|
|
|
sprintf( |
80
|
|
|
'SELECT fs FROM %s fs |
81
|
|
|
WHERE fs.time > :maxAge |
82
|
|
|
AND fs.%s = :fieldId |
83
|
|
|
AND fs.user_id != :ignoreId |
84
|
|
|
ORDER BY fs.time desc', |
85
|
|
|
FlightSignature::class, |
86
|
|
|
$isSystem ? "starsystem_map_id" : "map_id" |
87
|
|
|
) |
88
|
|
|
) |
89
|
|
|
->setParameters([ |
90
|
|
|
'maxAge' => time() - FlightSignatureVisibilityEnum::SIG_VISIBILITY_UNCLOAKED, |
91
|
|
|
'fieldId' => $fieldId, |
92
|
|
|
'ignoreId' => $ignoreId |
93
|
|
|
]) |
94
|
|
|
->getResult(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function createSignatureRangeResultMapping(): ResultSetMapping |
98
|
|
|
{ |
99
|
|
|
$rsm = new ResultSetMapping(); |
100
|
|
|
$rsm->addScalarResult('minx', 'minx', 'integer'); |
101
|
|
|
$rsm->addScalarResult('maxx', 'maxx', 'integer'); |
102
|
|
|
$rsm->addScalarResult('miny', 'miny', 'integer'); |
103
|
|
|
$rsm->addScalarResult('maxy', 'maxy', 'integer'); |
104
|
|
|
|
105
|
|
|
return $rsm; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function getSignatureRange(): array |
109
|
|
|
{ |
110
|
|
|
return $this->getEntityManager() |
111
|
|
|
->createNativeQuery( |
112
|
|
|
'SELECT COALESCE(min(m.cx),0) as minx, COALESCE(max(m.cx),0) as maxx, |
113
|
|
|
COALESCE(min(m.cy),0) as miny, COALESCE(max(m.cy),0) as maxy |
114
|
|
|
FROM stu_flight_sig fs |
115
|
|
|
JOIN stu_map m ON m.id = fs.map_id', |
116
|
|
|
$this->createSignatureRangeResultMapping() |
117
|
|
|
) |
118
|
|
|
->getResult(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function getSignatureRangeForUser(int $userId): array |
122
|
|
|
{ |
123
|
|
|
return $this->getEntityManager() |
124
|
|
|
->createNativeQuery( |
125
|
|
|
'SELECT COALESCE(min(m.cx),0) as minx, COALESCE(max(m.cx),0) as maxx, COALESCE(min(m.cy),0) as miny, COALESCE(max(m.cy),0) as maxy |
126
|
|
|
FROM stu_flight_sig fs |
127
|
|
|
JOIN stu_map m ON m.id = fs.map_id |
128
|
|
|
WHERE fs.user_id = :userId', |
129
|
|
|
$this->createSignatureRangeResultMapping() |
130
|
|
|
) |
131
|
|
|
->setParameter('userId', $userId) |
132
|
|
|
->getResult(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function getSignatureRangeForAlly(int $allyId): array |
136
|
|
|
{ |
137
|
|
|
return $this->getEntityManager() |
138
|
|
|
->createNativeQuery( |
139
|
|
|
'SELECT COALESCE(min(m.cx),0) as minx, COALESCE(max(m.cx),0) as maxx, COALESCE(min(m.cy),0) as miny, COALESCE(max(m.cy),0) as maxy |
140
|
|
|
FROM stu_flight_sig fs |
141
|
|
|
JOIN stu_map m ON m.id = fs.map_id |
142
|
|
|
JOIN stu_user u ON fs.user_id = u.id |
143
|
|
|
WHERE u.allys_id = :allyId', |
144
|
|
|
$this->createSignatureRangeResultMapping() |
145
|
|
|
) |
146
|
|
|
->setParameter('allyId', $allyId) |
147
|
|
|
->getResult(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function deleteOldSignatures(int $threshold): void |
151
|
|
|
{ |
152
|
|
|
$q = $this->getEntityManager() |
153
|
|
|
->createQuery( |
154
|
|
|
sprintf( |
155
|
|
|
'DELETE FROM %s fs WHERE fs.time < :maxAge', |
156
|
|
|
FlightSignature::class |
157
|
|
|
) |
158
|
|
|
); |
159
|
|
|
$q->setParameter('maxAge', time() - $threshold); |
160
|
|
|
$q->execute(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function getFlightsTop10(): array |
164
|
|
|
{ |
165
|
|
|
$rsm = new ResultSetMapping(); |
166
|
|
|
$rsm->addScalarResult('user_id', 'user_id', 'integer'); |
167
|
|
|
$rsm->addScalarResult('sc', 'sc', 'integer'); |
168
|
|
|
$rsm->addScalarResult('race', 'race', 'integer'); |
169
|
|
|
$rsm->addScalarResult('shipc', 'shipc', 'integer'); |
170
|
|
|
|
171
|
|
|
return $this |
172
|
|
|
->getEntityManager() |
173
|
|
|
->createNativeQuery( |
174
|
|
|
'SELECT fs.user_id, count(*) as sc, |
175
|
|
|
(SELECT race |
176
|
|
|
FROM stu_user u |
177
|
|
|
WHERE fs.user_id = u.id), |
178
|
|
|
count(distinct ship_id) as shipc |
179
|
|
|
FROM stu_flight_sig fs |
180
|
|
|
WHERE fs.to_direction != 0 |
181
|
|
|
AND fs.user_id > :firstUserId |
182
|
|
|
AND fs.time > :maxAge |
183
|
|
|
GROUP BY fs.user_id |
184
|
|
|
ORDER BY 2 DESC |
185
|
|
|
LIMIT 10', |
186
|
|
|
$rsm |
187
|
|
|
) |
188
|
|
|
->setParameters([ |
189
|
|
|
'maxAge' => time() - FlightSignatureVisibilityEnum::SIG_VISIBILITY_UNCLOAKED, |
190
|
|
|
'firstUserId' => UserEnum::USER_FIRST_ID |
191
|
|
|
]) |
192
|
|
|
->getResult(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function getSignaturesForUser(UserInterface $user): int |
196
|
|
|
{ |
197
|
|
|
return (int)$this |
198
|
|
|
->getEntityManager() |
199
|
|
|
->createQuery( |
200
|
|
|
sprintf( |
201
|
|
|
'SELECT count(fs.id) |
202
|
|
|
FROM %s fs |
203
|
|
|
WHERE fs.to_direction != 0 |
204
|
|
|
AND fs.user_id = :userId |
205
|
|
|
AND fs.time > :maxAge', |
206
|
|
|
FlightSignature::class |
207
|
|
|
) |
208
|
|
|
) |
209
|
|
|
->setParameters([ |
210
|
|
|
'maxAge' => time() - FlightSignatureVisibilityEnum::SIG_VISIBILITY_UNCLOAKED, |
211
|
|
|
'userId' => $user->getId() |
212
|
|
|
]) |
213
|
|
|
->getSingleScalarResult(); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function truncateAllSignatures(): void |
217
|
|
|
{ |
218
|
|
|
$this->getEntityManager()->createQuery( |
219
|
|
|
sprintf( |
220
|
|
|
'DELETE FROM %s fs', |
221
|
|
|
FlightSignature::class |
222
|
|
|
) |
223
|
|
|
)->execute(); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|