Failed Conditions
Push — master ( 328b73...9cdd6e )
by Markus
04:54
created

getHistoryForGym()   B

Complexity

Conditions 8
Paths 20

Size

Total Lines 39

Duplication

Lines 20
Ratio 51.28 %

Importance

Changes 0
Metric Value
cc 8
nc 20
nop 2
dl 20
loc 39
rs 8.0515
c 0
b 0
f 0
1
<?php
2
3
namespace Worldopole;
4
5
class QueryManagerMysqlMonocleAlternate extends QueryManagerMysql
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
22
    {
23
        $req = 'SELECT COUNT(*) as total FROM sightings';
24
        $result = $this->mysqli->query($req);
25
        if (!is_object($result)) {
26
            return 1;
27
        } else {
28
            $data = $result->fetch_object();
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
40
    {
41
        $req = 'SELECT COUNT(*) as total FROM forts';
42
        $result = $this->mysqli->query($req);
43
        if (!is_object($result)) {
44
            return 1;
45
        } else {
46
            $data = $result->fetch_object();
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
58
    {
59
        $req = 'SELECT COUNT(*) as total FROM pokestops';
60
        $result = $this->mysqli->query($req);
61
        if (!is_object($result)) {
62
            return 1;
63
        } else {
64
            $data = $result->fetch_object();
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
    public function getTotalPokemon()
80
    {
81
        $req = 'SELECT COUNT(*) AS total FROM sightings WHERE expire_timestamp >= UNIX_TIMESTAMP()';
82
        $result = $this->mysqli->query($req);
83
        $data = $result->fetch_object();
84
85
        return $data;
86
    }
87
88
    public function getTotalLures()
89
    {
90
        $data = (object) array('total' => 0);
91
92
        return $data;
93
    }
94
95
    public function getTotalGyms()
96
    {
97
        $req = 'SELECT COUNT(*) AS total FROM forts';
98
        $result = $this->mysqli->query($req);
99
        $data = $result->fetch_object();
100
101
        return $data;
102
    }
103
104
    public function getTotalRaids()
105
    {
106
        $req = 'SELECT COUNT(*) AS total FROM raids WHERE time_battle <= UNIX_TIMESTAMP() AND time_end >= UNIX_TIMESTAMP()';
107
        $result = $this->mysqli->query($req);
108
        $data = $result->fetch_object();
109
110
        return $data;
111
    }
112
113
    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 = $this->mysqli->query($req);
120
        $data = $result->fetch_object();
121
122
        return $data;
123
    }
124
125
    public function getRecentAll()
126
    {
127
        $req = 'SELECT DISTINCT pokemon_id, encounter_id, FROM_UNIXTIME(expire_timestamp) AS disappear_time, FROM_UNIXTIME(updated) AS last_modified, FROM_UNIXTIME(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 0,12';
132
        $result = $this->mysqli->query($req);
133
        $data = array();
134
        if ($result->num_rows > 0) {
135
            while ($row = $result->fetch_object()) {
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, FROM_UNIXTIME(expire_timestamp) AS disappear_time, FROM_UNIXTIME(updated) AS last_modified, FROM_UNIXTIME(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 0,12';
151
        $result = $this->mysqli->query($req);
152
        $data = array();
153
        if ($result->num_rows > 0) {
154
            while ($row = $result->fetch_object()) {
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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 = $this->mysqli->query($req);
173
        $data = $result->fetch_object();
174
175
        return $data;
176
    }
177
178 View Code Duplication
    public function getPokemonLastSeen($pokemon_id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
179
    {
180
        $req = "SELECT FROM_UNIXTIME(expire_timestamp) AS expire_timestamp, FROM_UNIXTIME(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 0,1";
185
        $result = $this->mysqli->query($req);
186
        $data = $result->fetch_object();
187
188
        return $data;
189
    }
190
191
    public function getTop50Pokemon($pokemon_id, $top_order_by, $top_direction)
192
    {
193
        $req = "SELECT FROM_UNIXTIME(expire_timestamp) AS distime, pokemon_id as pokemon_id, FROM_UNIXTIME(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 0,50";
200
201
        $result = $this->mysqli->query($req);
202
        $top = array();
203
        while ($data = $result->fetch_object()) {
204
            $top[] = $data;
205
        }
206
207
        return $top;
208
    }
209
210 View Code Duplication
    public function getTop50Trainers($pokemon_id, $best_order_by, $best_direction)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
211
    {
212
        $trainer_blacklist = '';
213
        if (!empty(self::$config->system->trainer_blacklist)) {
214
            $trainer_blacklist = " AND owner_name NOT IN ('".implode("','", self::$config->system->trainer_blacklist)."')";
215
        }
216
217
        $req = "SELECT owner_name as trainer_name, ROUND((100*((atk_iv)+(def_iv)+(sta_iv))/45),1) AS IV, move_1, move_2, cp as cp,
218
                FROM_UNIXTIME(last_modified) AS lasttime, last_modified as last_seen
219
                FROM gym_defenders
220
				WHERE pokemon_id = '".$pokemon_id."'".$trainer_blacklist."
221
				ORDER BY $best_order_by $best_direction, owner_name ASC
222
				LIMIT 0,50";
223
224
        $result = $this->mysqli->query($req);
225
        $toptrainer = array();
226
        while ($data = $result->fetch_object()) {
227
            $toptrainer[] = $data;
228
        }
229
230
        return $toptrainer;
231
    }
232
233
    public function getPokemonHeatmap($pokemon_id, $start, $end)
234
    {
235
        $where = ' WHERE pokemon_id = '.$pokemon_id.' '
236
            ."AND FROM_UNIXTIME(expire_timestamp) BETWEEN '".$start."' AND '".$end."'";
237
        $req = 'SELECT lat AS latitude, lon AS longitude FROM sightings'.$where.' LIMIT 100000';
238
        $result = $this->mysqli->query($req);
239
        $points = array();
240
        while ($data = $result->fetch_object()) {
241
            $points[] = $data;
242
        }
243
244
        return $points;
245
    }
246
247 View Code Duplication
    public function getPokemonGraph($pokemon_id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
248
    {
249
        $req = "SELECT COUNT(*) AS total, HOUR(disappear_time) AS disappear_hour
250
					FROM (SELECT FROM_UNIXTIME(expire_timestamp) as disappear_time FROM sightings WHERE pokemon_id = '".$pokemon_id."' LIMIT 100000) AS pokemonFiltered
251
				GROUP BY disappear_hour
252
				ORDER BY disappear_hour";
253
        $result = $this->mysqli->query($req);
254
        $array = array_fill(0, 24, 0);
255
        while ($result && $data = $result->fetch_object()) {
256
            $array[$data->disappear_hour] = $data->total;
257
        }
258
        // shift array because AM/PM starts at 1AM not 0:00
259
        $array[] = $array[0];
260
        array_shift($array);
261
262
        return $array;
263
    }
264
265 View Code Duplication
    public function getPokemonLive($pokemon_id, $ivMin, $ivMax, $inmap_pokemons)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
266
    {
267
        $inmap_pkms_filter = '';
268
        $where = ' WHERE expire_timestamp >= UNIX_TIMESTAMP() AND pokemon_id = '.$pokemon_id;
269
270
        $reqTestIv = 'SELECT MAX(atk_iv) AS iv FROM sightings '.$where;
271
        $resultTestIv = $this->mysqli->query($reqTestIv);
272
        $testIv = $resultTestIv->fetch_object();
273
        if (!is_null($inmap_pokemons) && ('' != $inmap_pokemons)) {
274
            foreach ($inmap_pokemons as $inmap) {
275
                $inmap_pkms_filter .= "'".$inmap."',";
276
            }
277
            $inmap_pkms_filter = rtrim($inmap_pkms_filter, ',');
278
            $where .= ' AND encounter_id NOT IN ('.$inmap_pkms_filter.') ';
279
        }
280
        if (null != $testIv->iv && !is_null($ivMin) && ('' != $ivMin)) {
281
            $where .= ' AND ((100/45)*(atk_iv + def_iv + sta_iv)) >= ('.$ivMin.') ';
282
        }
283
        if (null != $testIv->iv && !is_null($ivMax) && ('' != $ivMax)) {
284
            $where .= ' AND ((100/45)*(atk_iv + def_iv + sta_iv)) <= ('.$ivMax.') ';
285
        }
286
        $req = 'SELECT pokemon_id, lat AS latitude, lon AS longitude,
287
    					FROM_UNIXTIME(expire_timestamp) AS disappear_time,
288
    					FROM_UNIXTIME(expire_timestamp) AS disappear_time_real,
289
    					atk_iv AS individual_attack, def_iv AS individual_defense, sta_iv AS individual_stamina,
290
   						move_1, move_2
291
					FROM sightings '.$where.'
292
					LIMIT 5000';
293
        $result = $this->mysqli->query($req);
294
        $spawns = array();
295
        while ($data = $result->fetch_object()) {
296
            $spawns[] = $data;
297
        }
298
299
        return $spawns;
300
    }
301
302
    public function getPokemonSliderMinMax()
303
    {
304
        $req = 'SELECT FROM_UNIXTIME(MIN(expire_timestamp)) AS min, FROM_UNIXTIME(MAX(expire_timestamp)) AS max FROM sightings';
305
        $result = $this->mysqli->query($req);
306
        $data = $result->fetch_object();
307
308
        return $data;
309
    }
310
311
    public function getMapsCoords()
312
    {
313
        $req = 'SELECT MAX(lat) AS max_latitude, MIN(lat) AS min_latitude, MAX(lon) AS max_longitude, MIN(lon) as min_longitude FROM spawnpoints';
314
        $result = $this->mysqli->query($req);
315
        $data = $result->fetch_object();
316
317
        return $data;
318
    }
319
320
    public function getPokemonCount($pokemon_id)
321
    {
322
        $req = 'SELECT count, last_seen, latitude, longitude
323
					FROM pokemon_stats
324
					WHERE pid = '.$pokemon_id;
325
        $result = $this->mysqli->query($req);
326
        $data = $result->fetch_object();
327
328
        return $data;
329
    }
330
331
    public function getRaidCount($pokemon_id)
332
    {
333
        $req = 'SELECT count, last_seen, latitude, longitude
334
					FROM raid_stats
335
					WHERE pid = '.$pokemon_id;
336
        $result = $this->mysqli->query($req);
337
        $data = $result->fetch_object();
338
339
        return $data;
340
    }
341
342
    ///////////////
343
    // Pokestops
344
    //////////////
345
346
    public function getTotalPokestops()
347
    {
348
        $req = 'SELECT COUNT(*) as total FROM pokestops';
349
        $result = $this->mysqli->query($req);
350
        $data = $result->fetch_object();
351
352
        return $data;
353
    }
354
355
    public function getAllPokestops()
356
    {
357
        $req = 'SELECT lat as latitude, lon as longitude, null AS lure_expiration, UNIX_TIMESTAMP() AS now, null AS lure_expiration_real FROM pokestops';
358
        $result = $this->mysqli->query($req);
359
        $pokestops = array();
360
        while ($data = $result->fetch_object()) {
361
            $pokestops[] = $data;
362
        }
363
364
        return $pokestops;
365
    }
366
367
    /////////
368
    // Gyms
369
    /////////
370
371
    public function getTeamGuardians($team_id)
372
    {
373
        $req = "SELECT COUNT(*) AS total, guard_pokemon_id
374
					FROM forts f
375
					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))
376
					WHERE team = '".$team_id."' GROUP BY guard_pokemon_id ORDER BY total DESC LIMIT 0,3";
377
        $result = $this->mysqli->query($req);
378
379
        $datas = array();
380
        while ($data = $result->fetch_object()) {
381
            $datas[] = $data;
382
        }
383
384
        return $datas;
385
    }
386
387
    public function getOwnedAndPoints($team_id)
388
    {
389
        $req = "SELECT COUNT(f.id) AS total, ROUND(AVG(fs.total_cp)) AS average_points
390
        			FROM forts f
391
					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))
392
        			WHERE fs.team = '".$team_id."'";
393
        $result = $this->mysqli->query($req);
394
        $data = $result->fetch_object();
395
396
        return $data;
397
    }
398
399
    public function getAllGyms()
400
    {
401
        $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
402
					FROM forts f
403
					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));';
404
        $result = $this->mysqli->query($req);
405
        $gyms = array();
406
        while ($data = $result->fetch_object()) {
407
            $gyms[] = $data;
408
        }
409
410
        return $gyms;
411
    }
412
413
    public function getGymData($gym_id)
414
    {
415
        $req = "SELECT f.name AS name, null AS description, f.url AS url, fs.team AS team, FROM_UNIXTIME(fs.updated) AS last_scanned, fs.guard_pokemon_id AS guard_pokemon_id, (6 - fs.slots_available) AS level, fs.total_cp
416
			FROM forts f
417
			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))
418
			WHERE f.id ='".$gym_id."'";
419
        $result = $this->mysqli->query($req);
420
        $data = $result->fetch_object();
421
422
        return $data;
423
    }
424
425
    public function getGymDefenders($gym_id)
426
    {
427
        $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
428
			FROM gym_defenders
429
			WHERE fort_id='".$gym_id."'
430
			ORDER BY deployment_time";
431
        $result = $this->mysqli->query($req);
432
        $defenders = array();
433
        while ($data = $result->fetch_object()) {
434
            $defenders[] = $data;
435
        }
436
437
        return $defenders;
438
    }
439
440
    ///////////
441
    // Raids
442
    ///////////
443
444 View Code Duplication
    public function getAllRaids($page)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
445
    {
446
        $limit = ' LIMIT '.($page * 10).',10';
447
        $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, FROM_UNIXTIME(r.time_spawn) AS spawn, FROM_UNIXTIME(r.time_battle) AS start, FROM_UNIXTIME(r.time_end) AS end, FROM_UNIXTIME(fs.updated) AS last_scanned, f.name, f.lat AS latitude, f.lon as longitude
448
					FROM forts f
449
					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))
450
				 	LEFT JOIN raids r ON (r.fort_id = f.id AND r.time_end >= UNIX_TIMESTAMP())
451
					WHERE r.time_end > UNIX_TIMESTAMP()
452
					ORDER BY r.level DESC, r.time_battle'.$limit;
453
        $result = $this->mysqli->query($req);
454
        $raids = array();
455
        while ($data = $result->fetch_object()) {
456
            $raids[] = $data;
457
        }
458
459
        return $raids;
460
    }
461
462
    ////////////////
463
    // Gym History
464
    ////////////////
465
466
    public function getGymHistories($gym_name, $team, $page, $ranking)
467
    {
468
        $where = '';
469
        if (isset($gym_name) && '' != $gym_name) {
470
            $where = " WHERE name LIKE '%".$gym_name."%'";
471
        }
472 View Code Duplication
        if (isset($team) && '' != $team) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
473
            $where .= ('' === $where ? ' WHERE' : ' AND').' fs.team = '.$team;
474
        }
475 View Code Duplication
        switch ($ranking) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
476
            case 1:
477
                $order = ' ORDER BY name, last_modified DESC';
478
                break;
479
            case 2:
480
                $order = ' ORDER BY total_cp DESC, last_modified DESC';
481
                break;
482
            default:
483
                $order = ' ORDER BY last_modified DESC, name';
484
        }
485
486
        $limit = ' LIMIT '.($page * 10).',10';
487
488
        $req = 'SELECT f.id as gym_id, fs.total_cp, f.name, fs.team as team_id, (6 - slots_available) as pokemon_count, FROM_UNIXTIME(last_modified) AS last_modified
489
			FROM forts f
490
			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))
491
			'.$where.$order.$limit;
492
        $result = $this->mysqli->query($req);
493
        $gym_history = array();
494
        while ($data = $result->fetch_object()) {
495
            $gym_history[] = $data;
496
        }
497
498
        return $gym_history;
499
    }
500
501
    public function getGymHistoriesPokemon($gym_id)
502
    {
503
        $req = "SELECT external_id AS pokemon_uid, pokemon_id, cp_now as cp, owner_name AS trainer_name
504
					FROM gym_defenders
505
					WHERE fort_id = '".$gym_id."'
506
					ORDER BY deployment_time";
507
        $result = $this->mysqli->query($req);
508
        $pokemons = array();
509
        while ($data = $result->fetch_object()) {
510
            $pokemons[] = $data;
511
        }
512
513
        return $pokemons;
514
    }
515
516
    public function getHistoryForGym($page, $gym_id)
517
    {
518 View Code Duplication
        if (isset(self::$config->system->gymhistory_hide_cp_changes) && true === self::$config->system->gymhistory_hide_cp_changes) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
519
            $pageSize = 25;
520
        } else {
521
            $pageSize = 10;
522
        }
523
        $req = "SELECT f.id as gym_id, fs.team as team_id, total_cp, FROM_UNIXTIME(fs.last_modified) as last_modified, last_modified as last_modified_real
524
					FROM fort_sightings fs
525
					LEFT JOIN forts f ON f.id = fs.fort_id
526
					WHERE f.id = '".$gym_id."'
527
					ORDER BY fs.last_modified DESC
528
					LIMIT ".($page * $pageSize).','.($pageSize + 1);
529
        $result = $this->mysqli->query($req);
530
        $history = array();
531
        $count = 0;
532 View Code Duplication
        while ($data = $result->fetch_object()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
533
            ++$count;
534
            if (0 == $data->total_cp) {
535
                $data->pokemon = array();
536
                $data->pokemon_count = 0;
537
                $data->pokemon_uids = '';
538
            } else {
539
                $data->pokemon = $this->getHistoryForGymPokemon($gym_id, $data->last_modified_real);
540
                $data->pokemon_count = count($data->pokemon);
541
                $data->pokemon_uids = implode(',', array_keys($data->pokemon));
542
            }
543
            if (0 === $data->total_cp || 0 !== $data->pokemon_count) {
544
                $history[] = $data;
545
            }
546
        }
547
        if ($count !== ($pageSize + 1)) {
548
            $last_page = true;
549
        } else {
550
            $last_page = false;
551
        }
552
553
        return array('last_page' => $last_page, 'data' => $history);
554
    }
555
556 View Code Duplication
    private function getHistoryForGymPokemon($gym_id, $last_modified)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
557
    {
558
        $req = "SELECT ghd.defender_id, gd.pokemon_id, ghd.cp, gd.owner_name as trainer_name
559
					FROM gym_history_defenders ghd
560
					JOIN gym_defenders gd ON ghd.defender_id = gd.external_id
561
					WHERE ghd.fort_id = '".$gym_id."' AND date = '".$last_modified."'
562
					ORDER BY gd.deployment_time";
563
        $result = $this->mysqli->query($req);
564
        $pokemons = array();
565
        while ($data = $result->fetch_object()) {
566
            $pokemons[$data->defender_id] = $data;
567
        }
568
569
        return $pokemons;
570
    }
571
572
    //////////////
573
    // Trainers
574
    //////////////
575
576
    public function getTrainers($trainer_name, $team, $page, $rankingNumber)
577
    {
578
        $ranking = $this->getTrainerLevelRanking();
579
        $where = '';
580
        if (!empty(self::$config->system->trainer_blacklist)) {
581
            $where .= " AND gd.owner_name NOT IN ('".implode("','", self::$config->system->trainer_blacklist)."')";
582
        }
583
        if ('' != $trainer_name) {
584
            $where = " AND gd.owner_name LIKE '%".$trainer_name."%'";
585
        }
586 View Code Duplication
        if (0 != $team) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
587
            $where .= ('' == $where ? ' HAVING' : ' AND').' team = '.$team;
588
        }
589 View Code Duplication
        switch ($rankingNumber) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
590
            case 1:
591
                $order = ' ORDER BY active DESC, level DESC';
592
                break;
593
            case 2:
594
                $order = ' ORDER BY maxCp DESC, level DESC';
595
                break;
596
            default:
597
                $order = ' ORDER BY level DESC, active DESC';
598
        }
599
        $order .= ', last_seen DESC, name ';
600
        $limit = ' LIMIT '.($page * 10).',10 ';
601
        $req = 'SELECT gd.owner_name AS name, MAX(owner_level) AS level, MAX(cp) AS maxCp, MAX(active) AS active, MAX(team) AS team, FROM_UNIXTIME(MAX(last_modified)) as last_seen
602
				  	FROM gym_defenders gd
603
				  	LEFT JOIN (
604
				  		SELECT owner_name, COUNT(*) as active
605
				  		FROM gym_defenders gd2
606
						WHERE fort_id IS NOT NULL
607
				  		GROUP BY owner_name
608
				  	) active ON active.owner_name = gd.owner_name
609
				  	WHERE gd.owner_level IS NOT NULL '.$where.'
610
				  	GROUP BY gd.owner_name'.$order.$limit;
611
        $result = $this->mysqli->query($req);
612
        $trainers = array();
613 View Code Duplication
        while ($data = $result->fetch_object()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
614
            $data->last_seen = date('Y-m-d', strtotime($data->last_seen));
615
            if (is_null($data->active)) {
616
                $data->active = 0;
617
            }
618
            $trainers[$data->name] = $data;
619
620
            $pokemon = array_merge($this->getActivePokemon($data->name), $this->getInactivePokemon($data->name));
621
622
            $trainers[$data->name]->gyms = $data->active;
623
            $trainers[$data->name]->pokemons = $pokemon;
624
            $trainers[$data->name]->rank = $ranking[$data->level];
625
        }
626
627
        return $trainers;
628
    }
629
630 View Code Duplication
    public function getTrainerLevelRanking()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
631
    {
632
        $exclue = '';
633
        if (!empty(self::$config->system->trainer_blacklist)) {
634
            $exclue .= " AND owner_name NOT IN ('".implode("','", self::$config->system->trainer_blacklist)."')";
635
        }
636
        $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';
637
        $result = $this->mysqli->query($req);
638
        $levelData = array();
639
        while ($data = $result->fetch_object()) {
640
            $levelData[$data->level] = $data->count;
641
        }
642
        for ($i = 5; $i <= 40; ++$i) {
643
            if (!isset($levelData[$i])) {
644
                $levelData[$i] = 0;
645
            }
646
        }
647
        // sort array again
648
        ksort($levelData);
649
650
        return $levelData;
651
    }
652
653
    public function getActivePokemon($trainer_name)
654
    {
655
        $req = "SELECT pokemon_id, cp, atk_iv AS iv_attack, sta_iv AS iv_stamina, def_iv AS iv_defense, FROM_UNIXTIME(deployment_time) AS deployment_time, '1' AS active, fort_id as gym_id, FLOOR((UNIX_TIMESTAMP() - created) / 86400) AS last_scanned
656
						FROM gym_defenders
657
						WHERE owner_name = '".$trainer_name."' AND fort_id IS NOT NULL
658
						ORDER BY deployment_time";
659
        $result = $this->mysqli->query($req);
660
        $pokemon = array();
661
        while ($data = $result->fetch_object()) {
662
            $pokemon[] = $data;
663
        }
664
665
        return $pokemon;
666
    }
667
668
    public function getInactivePokemon($trainer_name)
669
    {
670
        $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
671
					FROM gym_defenders
672
					WHERE owner_name = '".$trainer_name."' AND fort_id IS NULL
673
					ORDER BY last_scanned";
674
        $result = $this->mysqli->query($req);
675
        $pokemon = array();
676
        while ($data = $result->fetch_object()) {
677
            $pokemon[] = $data;
678
        }
679
680
        return $pokemon;
681
    }
682
683 View Code Duplication
    public function getTrainerLevelCount($team_id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
684
    {
685
        $exclue = '';
686
        if (!empty(self::$config->system->trainer_blacklist)) {
687
            $exclue .= " AND owner_name NOT IN ('".implode("','", self::$config->system->trainer_blacklist)."')";
688
        }
689
        $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';
690
        $result = $this->mysqli->query($req);
691
        $levelData = array();
692
        while ($data = $result->fetch_object()) {
693
            $levelData[$data->level] = $data->count;
694
        }
695
        for ($i = 5; $i <= 40; ++$i) {
696
            if (!isset($levelData[$i])) {
697
                $levelData[$i] = 0;
698
            }
699
        }
700
        // sort array again
701
        ksort($levelData);
702
703
        return $levelData;
704
    }
705
706
    /////////
707
    // Cron
708
    /////////
709
710
    public function getPokemonCountsActive()
711
    {
712
        $req = 'SELECT pokemon_id, COUNT(*) as total FROM sightings WHERE expire_timestamp >= UNIX_TIMESTAMP() GROUP BY pokemon_id';
713
        $result = $this->mysqli->query($req);
714
        $counts = array();
715
        while ($data = $result->fetch_object()) {
716
            $counts[$data->pokemon_id] = $data->total;
717
        }
718
719
        return $counts;
720
    }
721
722
    public function getPokemonCountsLastDay()
723
    {
724
        $req = 'SELECT pokemon_id, COUNT(*) AS spawns_last_day
725
					FROM sightings
726
					WHERE expire_timestamp >= (SELECT MAX(expire_timestamp) - 86400 FROM sightings)
727
					GROUP BY pokemon_id
728
				  	ORDER BY pokemon_id ASC';
729
        $result = $this->mysqli->query($req);
730
        $counts = array();
731
        while ($data = $result->fetch_object()) {
732
            $counts[$data->pokemon_id] = $data->spawns_last_day;
733
        }
734
735
        return $counts;
736
    }
737
738
    public function getCaptchaCount()
739
    {
740
        $req = ' SELECT COUNT(*) as total FROM accounts WHERE captchaed IS NOT NULL AND reason IS NULL';
741
        $result = $this->mysqli->query($req);
742
        $data = $result->fetch_object();
743
744
        return $data;
745
    }
746
747 View Code Duplication
    public function getNestData($time, $minLatitude, $maxLatitude, $minLongitude, $maxLongitude)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
748
    {
749
        $pokemon_exclude_sql = '';
750
        if (!empty(self::$config->system->nest_exclude_pokemon)) {
751
            $pokemon_exclude_sql = 'AND p.pokemon_id NOT IN ('.implode(',', self::$config->system->nest_exclude_pokemon).')';
752
        }
753
        $req = 'SELECT 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
754
			          FROM sightings p
755
			          INNER JOIN spawnpoints s ON (p.spawn_id = s.spawn_id)
756
			          WHERE p.expire_timestamp > UNIX_TIMESTAMP() - '.($time * 3600).'
757
			            AND p.lat >= '.$minLatitude.' AND p.lat < '.$maxLatitude.' AND p.lon >= '.$minLongitude.' AND p.lon < '.$maxLongitude.'
758
			          '.$pokemon_exclude_sql.'
759
			          GROUP BY p.spawn_id, p.pokemon_id
760
			          HAVING COUNT(p.pokemon_id) >= '.($time / 4).'
761
			          ORDER BY p.pokemon_id';
762
        $result = $this->mysqli->query($req);
763
        $nests = array();
764
        while ($data = $result->fetch_object()) {
765
            $nests[] = $data;
766
        }
767
768
        return $nests;
769
    }
770
771 View Code Duplication
    public function getSpawnpointCount($minLatitude, $maxLatitude, $minLongitude, $maxLongitude)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
772
    {
773
        $req = 'SELECT COUNT(*) as total 
774
					FROM spawnpoints 
775
 					WHERE lat >= '.$minLatitude.' AND lat < '.$maxLatitude.' AND lon >= '.$minLongitude.' AND lon < '.$maxLongitude;
776
        $result = $this->mysqli->query($req);
777
        $data = $result->fetch_object();
778
779
        return $data;
780
    }
781
}
782