1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Worldopole; |
4
|
|
|
|
5
|
|
|
class QueryManagerPostgresqlMonocleAlternate extends QueryManagerPostgresql |
6
|
|
|
{ |
7
|
|
|
public function __construct() |
8
|
|
|
{ |
9
|
|
|
parent::__construct(); |
10
|
|
|
} |
11
|
|
|
|
12
|
|
|
public function __destruct() |
13
|
|
|
{ |
14
|
|
|
parent::__destruct(); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/////////// |
18
|
|
|
// Tester |
19
|
|
|
/////////// |
20
|
|
|
|
21
|
|
View Code Duplication |
public function testTotalPokemon() |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
$req = 'SELECT COUNT(*) as total FROM sightings'; |
24
|
|
|
$result = pg_query($this->db, $req); |
25
|
|
|
if (false === $result) { |
26
|
|
|
return 1; |
27
|
|
|
} else { |
28
|
|
|
$data = pg_fetch_object($result); |
29
|
|
|
$total = $data->total; |
30
|
|
|
|
31
|
|
|
if (0 == $total) { |
32
|
|
|
return 2; |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return 0; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
View Code Duplication |
public function testTotalGyms() |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
$req = 'SELECT COUNT(*) as total FROM forts'; |
42
|
|
|
$result = pg_query($this->db, $req); |
43
|
|
|
if (false === $result) { |
44
|
|
|
return 1; |
45
|
|
|
} else { |
46
|
|
|
$data = pg_fetch_object($result); |
47
|
|
|
$total = $data->total; |
48
|
|
|
|
49
|
|
|
if (0 == $total) { |
50
|
|
|
return 2; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return 0; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
View Code Duplication |
public function testTotalPokestops() |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$req = 'SELECT COUNT(*) as total FROM pokestops'; |
60
|
|
|
$result = pg_query($this->db, $req); |
61
|
|
|
if (false === $result) { |
62
|
|
|
return 1; |
63
|
|
|
} else { |
64
|
|
|
$data = pg_fetch_object($result); |
65
|
|
|
$total = $data->total; |
66
|
|
|
|
67
|
|
|
if (0 == $total) { |
68
|
|
|
return 2; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return 0; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
///////////// |
76
|
|
|
// Homepage |
77
|
|
|
///////////// |
78
|
|
|
|
79
|
|
View Code Duplication |
public function getTotalPokemon() |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$req = 'SELECT COUNT(*) AS total FROM sightings WHERE expire_timestamp >= EXTRACT(EPOCH FROM NOW())'; |
82
|
|
|
$result = pg_query($this->db, $req); |
83
|
|
|
$data = pg_fetch_object($result); |
84
|
|
|
|
85
|
|
|
return $data; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getTotalLures() |
89
|
|
|
{ |
90
|
|
|
$data = (object) array('total' => 0); |
91
|
|
|
|
92
|
|
|
return $data; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
View Code Duplication |
public function getTotalGyms() |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$req = 'SELECT COUNT(*) AS total FROM forts'; |
98
|
|
|
$result = pg_query($this->db, $req); |
99
|
|
|
$data = pg_fetch_object($result); |
100
|
|
|
|
101
|
|
|
return $data; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
View Code Duplication |
public function getTotalRaids() |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$req = 'SELECT COUNT(*) AS total FROM raids WHERE time_battle <= EXTRACT(EPOCH FROM NOW()) AND time_end >= EXTRACT(EPOCH FROM NOW())'; |
107
|
|
|
$result = pg_query($this->db, $req); |
108
|
|
|
$data = pg_fetch_object($result); |
109
|
|
|
|
110
|
|
|
return $data; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
View Code Duplication |
public function getTotalGymsForTeam($team_id) |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
$req = "SELECT COUNT(*) AS total |
116
|
|
|
FROM forts f |
117
|
|
|
LEFT JOIN fort_sightings fs ON (fs.fort_id = f.id AND fs.last_modified = (SELECT MAX(last_modified) FROM fort_sightings fs2 WHERE fs2.fort_id=f.id)) |
118
|
|
|
WHERE team = '$team_id'"; |
119
|
|
|
$result = pg_query($this->db, $req); |
120
|
|
|
$data = pg_fetch_object($result); |
121
|
|
|
|
122
|
|
|
return $data; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function getRecentAll() |
126
|
|
|
{ |
127
|
|
|
$req = 'SELECT pokemon_id, encounter_id, TO_TIMESTAMP(expire_timestamp) AS disappear_time, TO_TIMESTAMP(updated) AS last_modified, TO_TIMESTAMP(expire_timestamp) AS disappear_time_real, |
128
|
|
|
lat AS latitude, lon AS longitude, cp, atk_iv AS individual_attack, def_iv AS individual_defense, sta_iv AS individual_stamina |
129
|
|
|
FROM sightings |
130
|
|
|
ORDER BY updated DESC |
131
|
|
|
LIMIT 12 OFFSET 0'; |
132
|
|
|
$result = pg_query($this->db, $req); |
133
|
|
|
$data = array(); |
134
|
|
|
if ($result->num_rows > 0) { |
135
|
|
|
while ($row = pg_fetch_object($result)) { |
136
|
|
|
$data[] = $row; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $data; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function getRecentMythic($mythic_pokemon) |
144
|
|
|
{ |
145
|
|
|
$req = 'SELECT pokemon_id, encounter_id, TO_TIMESTAMP(expire_timestamp) AS disappear_time, TO_TIMESTAMP(updated) AS last_modified, TO_TIMESTAMP(expire_timestamp) AS disappear_time_real, |
146
|
|
|
lat AS latitude, lon AS longitude, cp, atk_iv AS individual_attack, def_iv AS individual_defense, sta_iv AS individual_stamina |
147
|
|
|
FROM sightings |
148
|
|
|
WHERE pokemon_id IN ('.implode(',', $mythic_pokemon).') |
149
|
|
|
ORDER BY updated DESC |
150
|
|
|
LIMIT 12 OFFSET 0'; |
151
|
|
|
$result = pg_query($this->db, $req); |
152
|
|
|
$data = array(); |
153
|
|
|
if ($result->num_rows > 0) { |
154
|
|
|
while ($row = pg_fetch_object($result)) { |
155
|
|
|
$data[] = $row; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $data; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/////////////////// |
163
|
|
|
// Single Pokemon |
164
|
|
|
/////////////////// |
165
|
|
|
|
166
|
|
View Code Duplication |
public function getGymsProtectedByPokemon($pokemon_id) |
|
|
|
|
167
|
|
|
{ |
168
|
|
|
$req = "SELECT COUNT(f.id) AS total |
169
|
|
|
FROM forts f |
170
|
|
|
LEFT JOIN fort_sightings fs ON (fs.fort_id = f.id AND fs.last_modified = (SELECT MAX(last_modified) FROM fort_sightings fs2 WHERE fs2.fort_id=f.id)) |
171
|
|
|
WHERE guard_pokemon_id = '".$pokemon_id."'"; |
172
|
|
|
$result = pg_query($this->db, $req); |
173
|
|
|
$data = pg_fetch_object($result); |
174
|
|
|
|
175
|
|
|
return $data; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
View Code Duplication |
public function getPokemonLastSeen($pokemon_id) |
|
|
|
|
179
|
|
|
{ |
180
|
|
|
$req = "SELECT TO_TIMESTAMP(expire_timestamp) AS expire_timestamp, TO_TIMESTAMP(expire_timestamp) AS disappear_time_real, lat AS latitude, lon AS longitude |
181
|
|
|
FROM sightings |
182
|
|
|
WHERE pokemon_id = '".$pokemon_id."' |
183
|
|
|
ORDER BY expire_timestamp DESC |
184
|
|
|
LIMIT 1 OFFSET 0"; |
185
|
|
|
$result = pg_query($this->db, $req); |
186
|
|
|
$data = pg_fetch_object($result); |
187
|
|
|
|
188
|
|
|
return $data; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
View Code Duplication |
public function getTop50Pokemon($pokemon_id, $top_order_by, $top_direction) |
|
|
|
|
192
|
|
|
{ |
193
|
|
|
$req = "SELECT expire_timestamp, TO_TIMESTAMP(expire_timestamp) AS distime, pokemon_id as pokemon_id, TO_TIMESTAMP(expire_timestamp) as disappear_time, lat as latitude, lon as longitude, |
194
|
|
|
cp, atk_iv as individual_attack, def_iv as individual_defense, sta_iv as individual_stamina, |
195
|
|
|
ROUND(100*(atk_iv+def_iv+sta_iv)/45,1) AS \"IV\", move_1 as move_1, move_2, form |
196
|
|
|
FROM sightings |
197
|
|
|
WHERE pokemon_id = '".$pokemon_id."' AND move_1 IS NOT NULL AND move_1 <> '0' |
198
|
|
|
ORDER BY $top_order_by $top_direction, expire_timestamp DESC |
199
|
|
|
LIMIT 50 OFFSET 0"; |
200
|
|
|
$result = pg_query($this->db, $req); |
201
|
|
|
$top = array(); |
202
|
|
|
while ($data = pg_fetch_object($result)) { |
203
|
|
|
$top[] = $data; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return $top; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function getTop50Trainers($pokemon_id, $best_order_by, $best_direction) |
210
|
|
|
{ |
211
|
|
|
$trainer_blacklist = ''; |
212
|
|
|
if (!empty(self::$config->system->trainer_blacklist)) { |
213
|
|
|
$trainer_blacklist = " AND owner_name NOT IN ('".implode("','", self::$config->system->trainer_blacklist)."')"; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
$req = "SELECT owner_name as trainer_name, ROUND((100.0*((atk_iv)+(def_iv)+(sta_iv))/45),1) AS \"IV\", move_1, move_2, cp as cp, |
217
|
|
|
TO_TIMESTAMP(last_modified) AS lasttime, last_modified as last_seen |
218
|
|
|
FROM gym_defenders |
219
|
|
|
WHERE pokemon_id = '".$pokemon_id."'".$trainer_blacklist." |
220
|
|
|
ORDER BY $best_order_by $best_direction, owner_name ASC |
221
|
|
|
LIMIT 50 OFFSET 0"; |
222
|
|
|
|
223
|
|
|
$result = pg_query($this->db, $req); |
224
|
|
|
$toptrainer = array(); |
225
|
|
|
while ($data = pg_fetch_object($result)) { |
226
|
|
|
$toptrainer[] = $data; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return $toptrainer; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function getPokemonHeatmap($pokemon_id, $start, $end) |
233
|
|
|
{ |
234
|
|
|
$where = ' WHERE pokemon_id = '.$pokemon_id.' ' |
235
|
|
|
."AND TO_TIMESTAMP(expire_timestamp) BETWEEN '".$start."' AND '".$end."'"; |
236
|
|
|
$req = 'SELECT lat AS latitude, lon AS longitude FROM sightings'.$where.' LIMIT 100000'; |
237
|
|
|
$result = pg_query($this->db, $req); |
238
|
|
|
$points = array(); |
239
|
|
|
while ($data = pg_fetch_object($result)) { |
240
|
|
|
$points[] = $data; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
return $points; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
public function getPokemonGraph($pokemon_id) |
247
|
|
|
{ |
248
|
|
|
$req = "SELECT COUNT(*) AS total, EXTRACT(HOUR FROM disappear_time) AS disappear_hour |
249
|
|
|
FROM (SELECT TO_TIMESTAMP(expire_timestamp) as disappear_time FROM sightings WHERE pokemon_id = '".$pokemon_id."' LIMIT 100000) AS pokemonFiltered |
250
|
|
|
GROUP BY disappear_hour |
251
|
|
|
ORDER BY disappear_hour"; |
252
|
|
|
$result = pg_query($this->db, $req); |
253
|
|
|
$array = array_fill(0, 24, 0); |
254
|
|
|
while ($result && $data = pg_fetch_object($result)) { |
255
|
|
|
$array[$data->disappear_hour] = $data->total; |
256
|
|
|
} |
257
|
|
|
// shift array because AM/PM starts at 1AM not 0:00 |
258
|
|
|
$array[] = $array[0]; |
259
|
|
|
array_shift($array); |
260
|
|
|
|
261
|
|
|
return $array; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
public function getPokemonLive($pokemon_id, $ivMin, $ivMax, $inmap_pokemons) |
265
|
|
|
{ |
266
|
|
|
$inmap_pkms_filter = ''; |
267
|
|
|
$where = ' WHERE expire_timestamp >= EXTRACT(EPOCH FROM NOW()) AND pokemon_id = '.$pokemon_id; |
268
|
|
|
|
269
|
|
|
$reqTestIv = 'SELECT MAX(atk_iv) AS iv FROM sightings '.$where; |
270
|
|
|
$resultTestIv = pg_query($this->db, $reqTestIv); |
271
|
|
|
$testIv = pg_fetch_object($resultTestIv); |
272
|
|
|
if (!is_null($inmap_pokemons) && ('' != $inmap_pokemons)) { |
273
|
|
|
foreach ($inmap_pokemons as $inmap) { |
274
|
|
|
$inmap_pkms_filter .= "'".$inmap."',"; |
275
|
|
|
} |
276
|
|
|
$inmap_pkms_filter = rtrim($inmap_pkms_filter, ','); |
277
|
|
|
$where .= ' AND encounter_id NOT IN ('.$inmap_pkms_filter.') '; |
278
|
|
|
} |
279
|
|
|
if (null != $testIv->iv && !is_null($ivMin) && ('' != $ivMin)) { |
280
|
|
|
$where .= ' AND ((100/45)*(atk_iv + def_iv + sta_iv)) >= ('.$ivMin.') '; |
281
|
|
|
} |
282
|
|
|
if (null != $testIv->iv && !is_null($ivMax) && ('' != $ivMax)) { |
283
|
|
|
$where .= ' AND ((100/45)*(atk_iv + def_iv + sta_iv)) <= ('.$ivMax.') '; |
284
|
|
|
} |
285
|
|
|
$req = 'SELECT pokemon_id, lat AS latitude, lon AS longitude, |
286
|
|
|
TO_TIMESTAMP(expire_timestamp) AS disappear_time, |
287
|
|
|
TO_TIMESTAMP(expire_timestamp) AS disappear_time_real, |
288
|
|
|
atk_iv AS individual_attack, def_iv AS individual_defense, sta_iv AS individual_stamina, |
289
|
|
|
move_1, move_2 |
290
|
|
|
FROM sightings '.$where.' |
291
|
|
|
LIMIT 5000'; |
292
|
|
|
$result = pg_query($this->db, $req); |
293
|
|
|
$spawns = array(); |
294
|
|
|
while ($data = pg_fetch_object($result)) { |
295
|
|
|
$spawns[] = $data; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
return $spawns; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
View Code Duplication |
public function getPokemonSliderMinMax() |
|
|
|
|
302
|
|
|
{ |
303
|
|
|
$req = 'SELECT TO_TIMESTAMP(MIN(expire_timestamp)) AS min, TO_TIMESTAMP(MAX(expire_timestamp)) AS max FROM sightings'; |
304
|
|
|
$result = pg_query($this->db, $req); |
305
|
|
|
$data = pg_fetch_object($result); |
306
|
|
|
|
307
|
|
|
return $data; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
View Code Duplication |
public function getMapsCoords() |
|
|
|
|
311
|
|
|
{ |
312
|
|
|
$req = 'SELECT MAX(lat) AS max_latitude, MIN(lat) AS min_latitude, MAX(lon) AS max_longitude, MIN(lon) as min_longitude FROM spawnpoints'; |
313
|
|
|
$result = pg_query($this->db, $req); |
314
|
|
|
$data = pg_fetch_object($result); |
315
|
|
|
|
316
|
|
|
return $data; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
View Code Duplication |
public function getPokemonCount($pokemon_id) |
|
|
|
|
320
|
|
|
{ |
321
|
|
|
$req = 'SELECT count, last_seen, latitude, longitude |
322
|
|
|
FROM pokemon_stats |
323
|
|
|
WHERE pid = '.$pokemon_id; |
324
|
|
|
$result = pg_query($this->db, $req); |
325
|
|
|
$data = pg_fetch_object($result); |
326
|
|
|
|
327
|
|
|
return $data; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
View Code Duplication |
public function getPokemonCountAll() |
|
|
|
|
331
|
|
|
{ |
332
|
|
|
$req = 'SELECT pid as pokemon_id, count, last_seen, latitude, longitude |
333
|
|
|
FROM pokemon_stats |
334
|
|
|
GROUP BY pid'; |
335
|
|
|
$result = $this->mysqli->query($req); |
|
|
|
|
336
|
|
|
$array = array(); |
337
|
|
|
while ($data = pg_fetch_object($result)) { |
338
|
|
|
$array[] = $data; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
return $array; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
View Code Duplication |
public function getRaidCount($pokemon_id) |
|
|
|
|
345
|
|
|
{ |
346
|
|
|
$req = 'SELECT count, last_seen, latitude, longitude |
347
|
|
|
FROM raid_stats |
348
|
|
|
WHERE pid = '.$pokemon_id; |
349
|
|
|
$result = pg_query($this->db, $req); |
350
|
|
|
$data = pg_fetch_object($result); |
351
|
|
|
|
352
|
|
|
return $data; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
View Code Duplication |
public function getRaidCountAll() |
|
|
|
|
356
|
|
|
{ |
357
|
|
|
$req = 'SELECT pid as pokemon_id, count, last_seen, latitude, longitude |
358
|
|
|
FROM raid_stats |
359
|
|
|
GROUP BY pid'; |
360
|
|
|
$result = $this->mysqli->query($req); |
361
|
|
|
$array = array(); |
362
|
|
|
while ($data = pg_fetch_object($result)) { |
363
|
|
|
$array[] = $data; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
return $array; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/////////////// |
370
|
|
|
// Pokestops |
371
|
|
|
////////////// |
372
|
|
|
|
373
|
|
View Code Duplication |
public function getTotalPokestops() |
|
|
|
|
374
|
|
|
{ |
375
|
|
|
$req = 'SELECT COUNT(*) as total FROM pokestops'; |
376
|
|
|
$result = pg_query($this->db, $req); |
377
|
|
|
$data = pg_fetch_object($result); |
378
|
|
|
|
379
|
|
|
return $data; |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
public function getAllPokestops() |
383
|
|
|
{ |
384
|
|
|
$req = 'SELECT lat as latitude, lon as longitude, null AS lure_expiration, EXTRACT(EPOCH FROM NOW()) AS now, null AS lure_expiration_real FROM pokestops'; |
385
|
|
|
$result = pg_query($this->db, $req); |
386
|
|
|
$pokestops = array(); |
387
|
|
|
while ($data = pg_fetch_object($result)) { |
388
|
|
|
$pokestops[] = $data; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
return $pokestops; |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
///////// |
395
|
|
|
// Gyms |
396
|
|
|
///////// |
397
|
|
|
|
398
|
|
|
public function getTeamGuardians($team_id) |
399
|
|
|
{ |
400
|
|
|
$req = "SELECT COUNT(*) AS total, guard_pokemon_id |
401
|
|
|
FROM forts f |
402
|
|
|
LEFT JOIN fort_sightings fs ON (fs.fort_id = f.id AND fs.last_modified = (SELECT MAX(last_modified) FROM fort_sightings fs2 WHERE fs2.fort_id=f.id)) |
403
|
|
|
WHERE team = '".$team_id."' GROUP BY guard_pokemon_id ORDER BY total DESC LIMIT 3 OFFSET 0"; |
404
|
|
|
$result = pg_query($this->db, $req); |
405
|
|
|
|
406
|
|
|
$datas = array(); |
407
|
|
|
while ($data = pg_fetch_object($result)) { |
408
|
|
|
$datas[] = $data; |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
return $datas; |
412
|
|
|
} |
413
|
|
|
|
414
|
|
View Code Duplication |
public function getOwnedAndPoints($team_id) |
|
|
|
|
415
|
|
|
{ |
416
|
|
|
$req = "SELECT COUNT(f.id) AS total, ROUND(AVG(fs.total_cp))AS average_points |
417
|
|
|
FROM forts f |
418
|
|
|
LEFT JOIN fort_sightings fs ON (fs.fort_id = f.id AND fs.last_modified = (SELECT MAX(last_modified) FROM fort_sightings fs2 WHERE fs2.fort_id=f.id)) |
419
|
|
|
WHERE fs.team = '".$team_id."'"; |
420
|
|
|
$result = pg_query($this->db, $req); |
421
|
|
|
$data = pg_fetch_object($result); |
422
|
|
|
|
423
|
|
|
return $data; |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
public function getAllGyms() |
427
|
|
|
{ |
428
|
|
|
$req = 'SELECT f.id as gym_id, team as team_id, f.lat as latitude, f.lon as longitude, updated as last_scanned, (6 - fs.slots_available) AS level |
429
|
|
|
FROM forts f |
430
|
|
|
LEFT JOIN fort_sightings fs ON (fs.fort_id = f.id AND fs.last_modified = (SELECT MAX(last_modified) FROM fort_sightings fs2 WHERE fs2.fort_id=f.id))'; |
431
|
|
|
$result = pg_query($this->db, $req); |
432
|
|
|
$gyms = array(); |
433
|
|
|
while ($data = pg_fetch_object($result)) { |
434
|
|
|
$gyms[] = $data; |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
return $gyms; |
438
|
|
|
} |
439
|
|
|
|
440
|
|
View Code Duplication |
public function getGymData($gym_id) |
|
|
|
|
441
|
|
|
{ |
442
|
|
|
$req = "SELECT f.name AS name, null AS description, f.url AS url, fs.team AS team, TO_TIMESTAMP(fs.updated) AS last_scanned, fs.guard_pokemon_id AS guard_pokemon_id, (6 - fs.slots_available) AS level, fs.total_cp |
443
|
|
|
FROM forts f |
444
|
|
|
LEFT JOIN fort_sightings fs ON (fs.fort_id = f.id AND fs.last_modified = (SELECT MAX(last_modified) FROM fort_sightings fs2 WHERE fs2.fort_id=f.id)) |
445
|
|
|
WHERE f.id ='".$gym_id."' |
446
|
|
|
GROUP BY f.name, f.url, fs.team, fs.updated, fs.guard_pokemon_id, fs.slots_available, gd.cp"; |
447
|
|
|
$result = pg_query($this->db, $req); |
448
|
|
|
$data = pg_fetch_object($result); |
449
|
|
|
|
450
|
|
|
return $data; |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
public function getGymDefenders($gym_id) |
454
|
|
|
{ |
455
|
|
|
$req = "SELECT external_id as pokemon_uid, pokemon_id, atk_iv as iv_attack, def_iv as iv_defense, sta_iv as iv_stamina, cp, fort_id as gym_id |
456
|
|
|
FROM gym_defenders |
457
|
|
|
WHERE fort_id='".$gym_id."' |
458
|
|
|
ORDER BY deployment_time"; |
459
|
|
|
$result = pg_query($this->db, $req); |
460
|
|
|
$defenders = array(); |
461
|
|
|
while ($data = pg_fetch_object($result)) { |
462
|
|
|
$defenders[] = $data; |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
return $defenders; |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
//////////////// |
469
|
|
|
// Gym History |
470
|
|
|
//////////////// |
471
|
|
|
|
472
|
|
|
public function getGymHistories($gym_name, $team, $page, $ranking) |
473
|
|
|
{ |
474
|
|
|
$where = ''; |
475
|
|
|
if (isset($gym_name) && '' != $gym_name) { |
476
|
|
|
$where = " WHERE name LIKE '%".$gym_name."%'"; |
477
|
|
|
} |
478
|
|
View Code Duplication |
if (isset($team) && '' != $team) { |
|
|
|
|
479
|
|
|
$where .= ('' === $where ? ' WHERE' : ' AND').' fs.team = '.$team; |
480
|
|
|
} |
481
|
|
View Code Duplication |
switch ($ranking) { |
|
|
|
|
482
|
|
|
case 1: |
483
|
|
|
$order = ' ORDER BY name, last_modified DESC'; |
484
|
|
|
break; |
485
|
|
|
case 2: |
486
|
|
|
$order = ' ORDER BY total_cp DESC, last_modified DESC'; |
487
|
|
|
break; |
488
|
|
|
default: |
489
|
|
|
$order = ' ORDER BY last_modified DESC, name'; |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
$limit = ' LIMIT 10 OFFSET '.($page * 10); |
493
|
|
|
|
494
|
|
|
$req = 'SELECT f.id as gym_id, fs.total_cp, f.name, fs.team as team_id, (6 - slots_available) as pokemon_count, TO_TIMESTAMP(last_modified) AS last_modified |
495
|
|
|
FROM forts f |
496
|
|
|
LEFT JOIN fort_sightings fs ON (fs.fort_id = f.id AND fs.last_modified = (SELECT MAX(last_modified) FROM fort_sightings fs2 WHERE fs2.fort_id=f.id)) |
497
|
|
|
'.$where.$order.$limit; |
498
|
|
|
|
499
|
|
|
$result = pg_query($this->db, $req); |
500
|
|
|
$gym_history = array(); |
501
|
|
|
while ($data = pg_fetch_object($result)) { |
502
|
|
|
$gym_history[] = $data; |
503
|
|
|
} |
504
|
|
|
|
505
|
|
|
return $gym_history; |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
public function getGymHistoriesPokemon($gym_id) |
509
|
|
|
{ |
510
|
|
|
$req = "SELECT external_id AS pokemon_uid, pokemon_id, cp_now as cp, owner_name AS trainer_name |
511
|
|
|
FROM gym_defenders |
512
|
|
|
WHERE fort_id = '".$gym_id."' |
513
|
|
|
ORDER BY deployment_time"; |
514
|
|
|
$result = pg_query($this->db, $req); |
515
|
|
|
$pokemons = array(); |
516
|
|
|
while ($data = pg_fetch_object($result)) { |
517
|
|
|
$pokemons[] = $data; |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
return $pokemons; |
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
public function getHistoryForGym($page, $gym_id) |
524
|
|
|
{ |
525
|
|
View Code Duplication |
if (isset(self::$config->system->gymhistory_hide_cp_changes) && true === self::$config->system->gymhistory_hide_cp_changes) { |
|
|
|
|
526
|
|
|
$pageSize = 25; |
527
|
|
|
} else { |
528
|
|
|
$pageSize = 10; |
529
|
|
|
} |
530
|
|
|
$req = "SELECT f.id as gym_id, fs.team as team_id, total_cp, TO_TIMESTAMP(fs.last_modified) as last_modified, last_modified as last_modified_real |
531
|
|
|
FROM fort_sightings fs |
532
|
|
|
LEFT JOIN forts f ON f.id = fs.fort_id |
533
|
|
|
WHERE f.id = '".$gym_id."' |
534
|
|
|
ORDER BY fs.last_modified DESC |
535
|
|
|
LIMIT ".($pageSize + 1).' OFFSET '.($page * $pageSize); |
536
|
|
|
$result = pg_query($this->db, $req); |
537
|
|
|
$history = array(); |
538
|
|
|
$count = 0; |
539
|
|
View Code Duplication |
while ($data = pg_fetch_object($result)) { |
|
|
|
|
540
|
|
|
++$count; |
541
|
|
|
if (0 == $data->total_cp) { |
542
|
|
|
$data->pokemon = array(); |
543
|
|
|
$data->pokemon_count = 0; |
544
|
|
|
$data->pokemon_uids = ''; |
545
|
|
|
} else { |
546
|
|
|
$data->pokemon = $this->getHistoryForGymPokemon($gym_id, $data->last_modified_real); |
547
|
|
|
$data->pokemon_count = count($data->pokemon); |
548
|
|
|
$data->pokemon_uids = implode(',', array_keys($data->pokemon)); |
549
|
|
|
} |
550
|
|
|
if (0 === $data->total_cp || 0 !== $data->pokemon_count) { |
551
|
|
|
$history[] = $data; |
552
|
|
|
} |
553
|
|
|
} |
554
|
|
|
if ($count !== ($pageSize + 1)) { |
555
|
|
|
$last_page = true; |
556
|
|
|
} else { |
557
|
|
|
$last_page = false; |
558
|
|
|
} |
559
|
|
|
|
560
|
|
|
return array('last_page' => $last_page, 'data' => $history); |
561
|
|
|
} |
562
|
|
|
|
563
|
|
View Code Duplication |
private function getHistoryForGymPokemon($gym_id, $last_modified) |
|
|
|
|
564
|
|
|
{ |
565
|
|
|
$req = "SELECT ghd.defender_id, gd.pokemon_id, ghd.cp, gd.owner_name as trainer_name |
566
|
|
|
FROM gym_history_defenders ghd |
567
|
|
|
JOIN gym_defenders gd ON ghd.defender_id = gd.external_id |
568
|
|
|
WHERE ghd.fort_id = '".$gym_id."' AND date = '".$last_modified."' |
569
|
|
|
ORDER BY gd.deployment_time"; |
570
|
|
|
$result = pg_query($this->db, $req); |
571
|
|
|
$pokemons = array(); |
572
|
|
|
while ($data = pg_fetch_object($result)) { |
573
|
|
|
$pokemons[$data->defender_id] = $data; |
574
|
|
|
} |
575
|
|
|
|
576
|
|
|
return $pokemons; |
577
|
|
|
} |
578
|
|
|
|
579
|
|
|
/////////// |
580
|
|
|
// Raids |
581
|
|
|
/////////// |
582
|
|
|
|
583
|
|
View Code Duplication |
public function getAllRaids($page) |
|
|
|
|
584
|
|
|
{ |
585
|
|
|
$limit = ' LIMIT 10 OFFSET '.($page * 10); |
586
|
|
|
$req = 'SELECT r.fort_id AS gym_id, r.level AS level, r.pokemon_id AS pokemon_id, r.cp AS cp, r.move_1 AS move_1, r.move_2 AS move_2, TO_TIMESTAMP(r.time_spawn) AS spawn, TO_TIMESTAMP(r.time_battle) AS start, TO_TIMESTAMP(r.time_end) AS end, TO_TIMESTAMP(fs.updated) AS last_scanned, f.name, f.lat AS latitude, f.lon as longitude |
587
|
|
|
FROM forts f |
588
|
|
|
LEFT JOIN fort_sightings fs ON (fs.fort_id = f.id AND fs.last_modified = (SELECT MAX(last_modified) FROM fort_sightings fs2 WHERE fs2.fort_id=f.id)) |
589
|
|
|
LEFT JOIN raids r ON (r.fort_id = f.id AND r.time_end >= UNIX_TIMESTAMP()) |
590
|
|
|
WHERE r.time_end > EXTRACT(EPOCH FROM NOW()) |
591
|
|
|
ORDER BY r.level DESC, r.time_battle'.$limit; |
592
|
|
|
$result = pg_query($this->db, $req); |
593
|
|
|
$raids = array(); |
594
|
|
|
while ($data = pg_fetch_object($result)) { |
595
|
|
|
$raids[] = $data; |
596
|
|
|
} |
597
|
|
|
|
598
|
|
|
return $raids; |
599
|
|
|
} |
600
|
|
|
|
601
|
|
|
////////////// |
602
|
|
|
// Trainers |
603
|
|
|
////////////// |
604
|
|
|
|
605
|
|
|
public function getTrainers($trainer_name, $team, $page, $rankingNumber) |
606
|
|
|
{ |
607
|
|
|
$ranking = $this->getTrainerLevelRanking(); |
608
|
|
|
$where = ''; |
609
|
|
|
if (!empty(self::$config->system->trainer_blacklist)) { |
610
|
|
|
$where .= " AND gd.owner_name NOT IN ('".implode("','", self::$config->system->trainer_blacklist)."')"; |
611
|
|
|
} |
612
|
|
|
if ('' != $trainer_name) { |
613
|
|
|
$where = " AND gd.owner_name LIKE '%".$trainer_name."%'"; |
614
|
|
|
} |
615
|
|
View Code Duplication |
if (0 != $team) { |
|
|
|
|
616
|
|
|
$where .= ('' == $where ? ' HAVING' : ' AND').' team = '.$team; |
617
|
|
|
} |
618
|
|
View Code Duplication |
switch ($rankingNumber) { |
|
|
|
|
619
|
|
|
case 1: |
620
|
|
|
$order = ' ORDER BY active DESC, level DESC'; |
621
|
|
|
break; |
622
|
|
|
case 2: |
623
|
|
|
$order = ' ORDER BY maxCp DESC, level DESC'; |
624
|
|
|
break; |
625
|
|
|
default: |
626
|
|
|
$order = ' ORDER BY level DESC, active DESC'; |
627
|
|
|
} |
628
|
|
|
$order .= ', last_seen DESC, name '; |
629
|
|
|
$limit = ' LIMIT 10 OFFSET '.($page * 10); |
630
|
|
|
$req = 'SELECT gd.owner_name AS name, MAX(owner_level) AS level, MAX(cp) AS maxCp, MAX(active) AS active, MAX(team) AS team, TO_TIMESTAMP(MAX(last_modified)) as last_seen |
631
|
|
|
FROM gym_defenders gd |
632
|
|
|
LEFT JOIN ( |
633
|
|
|
SELECT owner_name, COUNT(*) as active |
634
|
|
|
FROM gym_defenders gd2 |
635
|
|
|
WHERE fort_id IS NOT NULL |
636
|
|
|
GROUP BY owner_name |
637
|
|
|
) active ON active.owner_name = gd.owner_name |
638
|
|
|
WHERE level IS NOT NULL '.$where.' |
639
|
|
|
GROUP BY gd.owner_name'.$order.$limit; |
640
|
|
|
$result = pg_query($this->db, $req); |
641
|
|
|
$trainers = array(); |
642
|
|
View Code Duplication |
while ($data = pg_fetch_object($result)) { |
|
|
|
|
643
|
|
|
$data->last_seen = date('Y-m-d', strtotime($data->last_seen)); |
644
|
|
|
if (is_null($data->active)) { |
645
|
|
|
$data->active = 0; |
646
|
|
|
} |
647
|
|
|
$trainers[$data->name] = $data; |
648
|
|
|
|
649
|
|
|
$pokemon = array_merge($this->getActivePokemon($data->name), $this->getInactivePokemon($data->name)); |
650
|
|
|
|
651
|
|
|
$trainers[$data->name]->gyms = $data->active; |
652
|
|
|
$trainers[$data->name]->pokemons = $pokemon; |
653
|
|
|
$trainers[$data->name]->rank = $ranking[$data->level]; |
654
|
|
|
} |
655
|
|
|
|
656
|
|
|
return $trainers; |
657
|
|
|
} |
658
|
|
|
|
659
|
|
View Code Duplication |
public function getTrainerLevelRanking() |
|
|
|
|
660
|
|
|
{ |
661
|
|
|
$exclue = ''; |
662
|
|
|
if (!empty(self::$config->system->trainer_blacklist)) { |
663
|
|
|
$exclue .= " AND owner_name NOT IN ('".implode("','", self::$config->system->trainer_blacklist)."')"; |
664
|
|
|
} |
665
|
|
|
$req = 'SELECT COUNT(*) AS count, level FROM (SELECT MAX(owner_level) as level FROM gym_defenders WHERE owner_level IS NOT NULL '.$exclue.' GROUP BY owner_level, owner_name) x GROUP BY level'; |
666
|
|
|
$result = pg_query($this->db, $req); |
667
|
|
|
$levelData = array(); |
668
|
|
|
while ($data = pg_fetch_object($result)) { |
669
|
|
|
$levelData[$data->level] = $data->count; |
670
|
|
|
} |
671
|
|
|
for ($i = 5; $i <= 40; ++$i) { |
672
|
|
|
if (!isset($levelData[$i])) { |
673
|
|
|
$levelData[$i] = 0; |
674
|
|
|
} |
675
|
|
|
} |
676
|
|
|
// sort array again |
677
|
|
|
ksort($levelData); |
678
|
|
|
|
679
|
|
|
return $levelData; |
680
|
|
|
} |
681
|
|
|
|
682
|
|
|
public function getActivePokemon($trainer_name) |
683
|
|
|
{ |
684
|
|
|
$req = "SELECT pokemon_id, cp, atk_iv AS iv_attack, sta_iv AS iv_stamina, def_iv AS iv_defense, TO_TIMESTAMP(deployment_time) AS deployment_time, '1' AS active, fort_id as gym_id, FLOOR((UNIX_TIMESTAMP() - created) / 86400) AS last_scanned |
685
|
|
|
FROM gym_defenders |
686
|
|
|
WHERE owner_name = '".$trainer_name."' AND fort_id IS NOT NULL |
687
|
|
|
ORDER BY deployment_time"; |
688
|
|
|
$result = pg_query($this->db, $req); |
689
|
|
|
$pokemon = array(); |
690
|
|
|
while ($data = pg_fetch_object($result)) { |
691
|
|
|
$pokemon[] = $data; |
692
|
|
|
} |
693
|
|
|
|
694
|
|
|
return $pokemon; |
695
|
|
|
} |
696
|
|
|
|
697
|
|
|
public function getInactivePokemon($trainer_name) |
698
|
|
|
{ |
699
|
|
|
$req = "SELECT pokemon_id, cp, atk_iv AS iv_attack, sta_iv AS iv_stamina, def_iv AS iv_defense, NULL AS deployment_time, '0' AS active, fort_id as gym_id, FLOOR((UNIX_TIMESTAMP() - created) / 86400) AS last_scanned |
700
|
|
|
FROM gym_defenders |
701
|
|
|
WHERE owner_name = '".$trainer_name."' AND fort_id IS NULL |
702
|
|
|
ORDER BY last_scanned"; |
703
|
|
|
$result = pg_query($this->db, $req); |
704
|
|
|
$pokemon = array(); |
705
|
|
|
while ($data = pg_fetch_object($result)) { |
706
|
|
|
$pokemon[] = $data; |
707
|
|
|
} |
708
|
|
|
|
709
|
|
|
return $pokemon; |
710
|
|
|
} |
711
|
|
|
|
712
|
|
View Code Duplication |
public function getTrainerLevelCount($team_id) |
|
|
|
|
713
|
|
|
{ |
714
|
|
|
$exclue = ''; |
715
|
|
|
if (!empty(self::$config->system->trainer_blacklist)) { |
716
|
|
|
$exclue .= " AND owner_name NOT IN ('".implode("','", self::$config->system->trainer_blacklist)."')"; |
717
|
|
|
} |
718
|
|
|
$req = "SELECT COUNT(*) AS count, level FROM (SELECT MAX(owner_level) as level FROM gym_defenders WHERE owner_level IS NOT NULL AND team = '".$team_id."' ".$exclue.' GROUP BY owner_level, owner_name) x GROUP BY level'; |
719
|
|
|
$result = pg_query($this->db, $req); |
720
|
|
|
$levelData = array(); |
721
|
|
|
while ($data = pg_fetch_object($result)) { |
722
|
|
|
$levelData[$data->level] = $data->count; |
723
|
|
|
} |
724
|
|
|
for ($i = 5; $i <= 40; ++$i) { |
725
|
|
|
if (!isset($levelData[$i])) { |
726
|
|
|
$levelData[$i] = 0; |
727
|
|
|
} |
728
|
|
|
} |
729
|
|
|
// sort array again |
730
|
|
|
ksort($levelData); |
731
|
|
|
|
732
|
|
|
return $levelData; |
733
|
|
|
} |
734
|
|
|
|
735
|
|
|
///////// |
736
|
|
|
// Cron |
737
|
|
|
///////// |
738
|
|
|
|
739
|
|
|
public function getPokemonCountsActive() |
740
|
|
|
{ |
741
|
|
|
$req = 'SELECT pokemon_id, COUNT(*) as total FROM sightings WHERE expire_timestamp >= EXTRACT(EPOCH FROM NOW()) GROUP BY pokemon_id'; |
742
|
|
|
$result = pg_query($this->db, $req); |
743
|
|
|
$counts = array(); |
744
|
|
|
while ($data = pg_fetch_object($result)) { |
745
|
|
|
$counts[$data->pokemon_id] = $data->total; |
746
|
|
|
} |
747
|
|
|
|
748
|
|
|
return $counts; |
749
|
|
|
} |
750
|
|
|
|
751
|
|
View Code Duplication |
public function getTotalPokemonIV() |
|
|
|
|
752
|
|
|
{ |
753
|
|
|
$req = 'SELECT COUNT(*) as total FROM sightings WHERE expire_timestamp >= EXTRACT(EPOCH FROM NOW()) AND cp IS NOT NULL'; |
754
|
|
|
$result = pg_query($this->db, $req); |
755
|
|
|
$data = pg_fetch_object($result); |
756
|
|
|
|
757
|
|
|
return $data; |
758
|
|
|
} |
759
|
|
|
|
760
|
|
|
public function getPokemonCountsLastDay() |
761
|
|
|
{ |
762
|
|
|
$req = 'SELECT pokemon_id, COUNT(*) AS spawns_last_day |
763
|
|
|
FROM sightings |
764
|
|
|
WHERE expire_timestamp >= (SELECT MAX(expire_timestamp) - 86400 FROM sightings) |
765
|
|
|
GROUP BY pokemon_id |
766
|
|
|
ORDER BY pokemon_id ASC'; |
767
|
|
|
$result = pg_query($this->db, $req); |
768
|
|
|
$counts = array(); |
769
|
|
|
while ($data = pg_fetch_object($result)) { |
770
|
|
|
$counts[$data->pokemon_id] = $data->spawns_last_day; |
771
|
|
|
} |
772
|
|
|
|
773
|
|
|
return $counts; |
774
|
|
|
} |
775
|
|
|
|
776
|
|
|
public function getCaptchaCount() |
777
|
|
|
{ |
778
|
|
|
$req = ' SELECT COUNT(*) as total FROM accounts WHERE captchaed IS NOT NULL AND reason IS NULL'; |
779
|
|
|
$result = pg_query($this->db, $req); |
780
|
|
|
$data = pg_fetch_object($result); |
781
|
|
|
|
782
|
|
|
return $data; |
783
|
|
|
} |
784
|
|
|
|
785
|
|
|
public function getNestData($time, $minLatitude, $maxLatitude, $minLongitude, $maxLongitude) |
786
|
|
|
{ |
787
|
|
|
$pokemon_exclude_sql = ''; |
788
|
|
|
if (!empty(self::$config->system->nest_exclude_pokemon)) { |
789
|
|
|
$pokemon_exclude_sql = 'AND p.pokemon_id NOT IN ('.implode(',', self::$config->system->nest_exclude_pokemon).')'; |
790
|
|
|
} |
791
|
|
|
$req = 'SELECT p.spawn_id, p.pokemon_id, MAX(p.lat) AS latitude, MAX(p.lon) AS longitude, count(p.pokemon_id) AS total_pokemon, MAX(s.updated) as latest_seen, coalesce(CASE WHEN MAX(duration) = 0 THEN NULL ELSE MAX(duration) END ,30)*60 as duration |
792
|
|
|
FROM sightings p |
793
|
|
|
INNER JOIN spawnpoints s ON (p.spawn_id = s.spawn_id) |
794
|
|
|
WHERE p.expire_timestamp > EXTRACT(EPOCH FROM NOW()) - '.($time * 3600).' |
795
|
|
|
AND p.lat >= '.$minLatitude.' AND p.lat < '.$maxLatitude.' AND p.lon >= '.$minLongitude.' AND p.lon < '.$maxLongitude.' |
796
|
|
|
'.$pokemon_exclude_sql.' |
797
|
|
|
GROUP BY p.spawn_id, p.pokemon_id |
798
|
|
|
HAVING COUNT(p.pokemon_id) >= '.($time / 4).' |
799
|
|
|
ORDER BY p.pokemon_id'; |
800
|
|
|
$result = pg_query($this->db, $req); |
801
|
|
|
$nests = array(); |
802
|
|
|
while ($data = pg_fetch_object($result)) { |
803
|
|
|
$nests[] = $data; |
804
|
|
|
} |
805
|
|
|
|
806
|
|
|
return $nests; |
807
|
|
|
} |
808
|
|
|
|
809
|
|
|
public function getSpawnpointCount($minLatitude, $maxLatitude, $minLongitude, $maxLongitude) |
810
|
|
|
{ |
811
|
|
|
$req = 'SELECT COUNT(*) as total |
812
|
|
|
FROM spawnpoints |
813
|
|
|
WHERE lat >= '.$minLatitude.' AND lat < '.$maxLatitude.' AND lon >= '.$minLongitude.' AND lon < '.$maxLongitude; |
814
|
|
|
$result = pg_query($this->db, $req); |
815
|
|
|
$data = pg_fetch_object($result); |
816
|
|
|
|
817
|
|
|
return $data; |
818
|
|
|
} |
819
|
|
|
} |
820
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.