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

QueryManagerMysqlRocketmap::getPokemonLive()   B

Complexity

Conditions 11
Paths 16

Size

Total Lines 33

Duplication

Lines 33
Ratio 100 %

Importance

Changes 0
Metric Value
cc 11
nc 16
nop 4
dl 33
loc 33
rs 7.3166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Worldopole;
4
5
final class QueryManagerMysqlRocketmap 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 pokemon';
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 gym';
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 pokestop';
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 pokemon WHERE disappear_time >= UTC_TIMESTAMP()';
82
        $result = $this->mysqli->query($req);
83
        $data = $result->fetch_object();
84
85
        return $data;
86
    }
87
88
    public function getTotalLures()
89
    {
90
        $req = 'SELECT COUNT(*) AS total FROM pokestop WHERE lure_expiration >= UTC_TIMESTAMP()';
91
        $result = $this->mysqli->query($req);
92
        $data = $result->fetch_object();
93
94
        return $data;
95
    }
96
97
    public function getTotalGyms()
98
    {
99
        $req = 'SELECT COUNT(DISTINCT(gym_id)) AS total FROM gym';
100
        $result = $this->mysqli->query($req);
101
        $data = $result->fetch_object();
102
103
        return $data;
104
    }
105
106
    public function getTotalRaids()
107
    {
108
        $req = 'SELECT COUNT(*) AS total FROM raid WHERE start <= UTC_TIMESTAMP() AND end >= UTC_TIMESTAMP()';
109
        $result = $this->mysqli->query($req);
110
        $data = $result->fetch_object();
111
112
        return $data;
113
    }
114
115
    public function getTotalGymsForTeam($team_id)
116
    {
117
        $req = "SELECT COUNT(DISTINCT(gym_id)) AS total FROM gym WHERE team_id = '".$team_id."'";
118
        $result = $this->mysqli->query($req);
119
        $data = $result->fetch_object();
120
121
        return $data;
122
    }
123
124
    public function getRecentAll()
125
    {
126
        $req = "SELECT DISTINCT pokemon_id, encounter_id, disappear_time, last_modified,
127
				CONVERT_TZ(disappear_time, '+00:00', '".self::$time_offset."') AS disappear_time_real,
128
				latitude, longitude, cp, individual_attack, individual_defense, individual_stamina
129
				FROM pokemon
130
				ORDER BY last_modified 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 View Code Duplication
    public function getRecentMythic($mythic_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...
144
    {
145
        $req = "SELECT DISTINCT pokemon_id, encounter_id, disappear_time, last_modified,
146
				CONVERT_TZ(disappear_time, '+00:00', '".self::$time_offset."') AS disappear_time_real,
147
				latitude, longitude, cp, individual_attack, individual_defense, individual_stamina
148
				FROM pokemon
149
				WHERE pokemon_id IN (".implode(',', $mythic_pokemons).')
150
				ORDER BY last_modified DESC
151
				LIMIT 0,12';
152
        $result = $this->mysqli->query($req);
153
        $data = array();
154
        if ($result->num_rows > 0) {
155
            while ($row = $result->fetch_object()) {
156
                $data[] = $row;
157
            }
158
        }
159
160
        return $data;
161
    }
162
163
    ///////////////////
164
    // Single Pokemon
165
    ///////////////////
166
167 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...
168
    {
169
        $req = "SELECT COUNT(DISTINCT(gym_id)) AS total FROM gym WHERE guard_pokemon_id = '".$pokemon_id."'";
170
        $result = $this->mysqli->query($req);
171
        $data = $result->fetch_object();
172
173
        return $data;
174
    }
175
176 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...
177
    {
178
        $req = "SELECT disappear_time,
179
				CONVERT_TZ(disappear_time, '+00:00', '".self::$time_offset."') AS disappear_time_real,
180
				latitude, longitude
181
				FROM pokemon
182
				WHERE pokemon_id = '".$pokemon_id."'
183
				ORDER BY disappear_time DESC
184
				LIMIT 0,1";
185
        $result = $this->mysqli->query($req);
186
        $data = $result->fetch_object();
187
188
        return $data;
189
    }
190
191 View Code Duplication
    public function getTop50Pokemon($pokemon_id, $top_order_by, $top_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...
192
    {
193
        $req = "SELECT CONVERT_TZ(disappear_time, '+00:00', '".self::$time_offset."') AS distime,
194
				pokemon_id, disappear_time, latitude, longitude,
195
				cp, individual_attack, individual_defense, individual_stamina,
196
				ROUND(100*(individual_attack+individual_defense+individual_stamina)/45,1) AS IV,
197
				move_1, move_2, form
198
				FROM pokemon
199
				WHERE pokemon_id = '".$pokemon_id."' AND move_1 IS NOT NULL AND move_1 <> '0'
200
				ORDER BY $top_order_by $top_direction, disappear_time DESC
201
				LIMIT 0,50";
202
        $result = $this->mysqli->query($req);
203
        $top = array();
204
        while ($data = $result->fetch_object()) {
205
            $top[] = $data;
206
        }
207
208
        return $top;
209
    }
210
211 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...
212
    {
213
        $trainer_blacklist = '';
214
        if (!empty(self::$config->system->trainer_blacklist)) {
215
            $trainer_blacklist = " AND trainer_name NOT IN ('".implode("','", self::$config->system->trainer_blacklist)."')";
216
        }
217
        $req = "SELECT trainer_name,
218
				ROUND((100*(iv_attack+iv_defense+iv_stamina)/45),1) AS IV,
219
				move_1, move_2, cp,
220
				DATE_FORMAT(last_seen, '%Y-%m-%d') AS lasttime, last_seen
221
				FROM gympokemon
222
				WHERE pokemon_id = '".$pokemon_id."'".$trainer_blacklist."
223
				ORDER BY $best_order_by $best_direction, trainer_name ASC
224
				LIMIT 0,50";
225
        $result = $this->mysqli->query($req);
226
        $toptrainer = array();
227
        while ($data = $result->fetch_object()) {
228
            $toptrainer[] = $data;
229
        }
230
231
        return $toptrainer;
232
    }
233
234 View Code Duplication
    public function getPokemonHeatmap($pokemon_id, $start, $end)
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...
235
    {
236
		$req = "SELECT latitude, longitude
237
				FROM pokemon
238
				WHERE pokemon_id = ".$pokemon_id." AND disappear_time BETWEEN '".$start."' AND '".$end."'
239
				LIMIT 10000";
240
        $result = $this->mysqli->query($req);
241
        $points = array();
242
        while ($data = $result->fetch_object()) {
243
            $points[] = $data;
244
        }
245
246
        return $points;
247
    }
248
249 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...
250
    {
251
        $req = "SELECT COUNT(*) AS total,
252
				HOUR(CONVERT_TZ(disappear_time, '+00:00', '".self::$time_offset."')) AS disappear_hour
253
				FROM (SELECT disappear_time FROM pokemon WHERE pokemon_id = '".$pokemon_id."' LIMIT 100000) AS pokemonFiltered
254
				GROUP BY disappear_hour
255
				ORDER BY disappear_hour";
256
        $result = $this->mysqli->query($req);
257
        $array = array_fill(0, 24, 0);
258
        while ($result && $data = $result->fetch_object()) {
259
            $array[$data->disappear_hour] = $data->total;
260
        }
261
        // shift array because AM/PM starts at 1AM not 0:00
262
        $array[] = $array[0];
263
        array_shift($array);
264
265
        return $array;
266
    }
267
268 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...
269
    {
270
        $inmap_pkms_filter = '';
271
        $where = ' WHERE disappear_time >= UTC_TIMESTAMP() AND pokemon_id = '.$pokemon_id;
272
        $reqTestIv = 'SELECT MAX(individual_attack) AS iv FROM pokemon '.$where;
273
        $resultTestIv = $this->mysqli->query($reqTestIv);
274
        $testIv = $resultTestIv->fetch_object();
275
        if (!is_null($inmap_pokemons) && ('' != $inmap_pokemons)) {
276
            foreach ($inmap_pokemons as $inmap) {
277
                $inmap_pkms_filter .= "'".$inmap."',";
278
            }
279
            $inmap_pkms_filter = rtrim($inmap_pkms_filter, ',');
280
            $where .= ' AND encounter_id NOT IN ('.$inmap_pkms_filter.') ';
281
        }
282
        if (null != $testIv->iv && !is_null($ivMin) && ('' != $ivMin)) {
283
            $where .= ' AND ((100/45)*(individual_attack+individual_defense+individual_stamina)) >= ('.$ivMin.') ';
284
        }
285
        if (null != $testIv->iv && !is_null($ivMax) && ('' != $ivMax)) {
286
            $where .= ' AND ((100/45)*(individual_attack+individual_defense+individual_stamina)) <= ('.$ivMax.') ';
287
        }
288
        $req = "SELECT pokemon_id, encounter_id, latitude, longitude, disappear_time,
289
				CONVERT_TZ(disappear_time, '+00:00', '".self::$time_offset."') AS disappear_time_real,
290
				individual_attack, individual_defense, individual_stamina, move_1, move_2
291
				FROM pokemon ".$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 MIN(disappear_time) AS min, MAX(disappear_time) AS max FROM pokemon';
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(latitude) AS max_latitude, MIN(latitude) AS min_latitude,
314
				MAX(longitude) AS max_longitude, MIN(longitude) as min_longitude
315
				FROM spawnpoint';
316
        $result = $this->mysqli->query($req);
317
        $data = $result->fetch_object();
318
319
        return $data;
320
    }
321
322
    public function getPokemonCount($pokemon_id)
323
    {
324
        $req = 'SELECT count, last_seen, latitude, longitude
325
				FROM pokemon_stats
326
				WHERE pid = '.$pokemon_id;
327
        $result = $this->mysqli->query($req);
328
        $data = $result->fetch_object();
329
330
        return $data;
331
    }
332
333
    public function getRaidCount($pokemon_id)
334
    {
335
        $req = 'SELECT count, last_seen, latitude, longitude
336
				FROM raid_stats
337
				WHERE pid = '.$pokemon_id;
338
        $result = $this->mysqli->query($req);
339
        $data = $result->fetch_object();
340
341
        return $data;
342
    }
343
344
    ///////////////
345
    // Pokestops
346
    //////////////
347
348
    public function getTotalPokestops()
349
    {
350
        $req = 'SELECT COUNT(*) as total FROM pokestop';
351
        $result = $this->mysqli->query($req);
352
        $data = $result->fetch_object();
353
354
        return $data;
355
    }
356
357
    public function getAllPokestops()
358
    {
359
        $req = "SELECT latitude, longitude, lure_expiration, UTC_TIMESTAMP() AS now,
360
				CONVERT_TZ(lure_expiration, '+00:00', '".self::$time_offset."') AS lure_expiration_real
361
				FROM pokestop";
362
        $result = $this->mysqli->query($req);
363
        $pokestops = array();
364
        while ($data = $result->fetch_object()) {
365
            $pokestops[] = $data;
366
        }
367
368
        return $pokestops;
369
    }
370
371
    /////////
372
    // Gyms
373
    /////////
374
375
    public function getTeamGuardians($team_id)
376
    {
377
        $req = "SELECT COUNT(*) AS total, guard_pokemon_id
378
				FROM gym WHERE team_id = '".$team_id."'
379
				GROUP BY guard_pokemon_id
380
				ORDER BY total DESC
381
				LIMIT 0,3";
382
        $result = $this->mysqli->query($req);
383
        $datas = array();
384
        while ($data = $result->fetch_object()) {
385
            $datas[] = $data;
386
        }
387
388
        return $datas;
389
    }
390
391
    public function getOwnedAndPoints($team_id)
392
    {
393
        $req = "SELECT COUNT(DISTINCT(gym_id)) AS total,
394
				ROUND(AVG(total_cp),0) AS average_points
395
				FROM gym
396
				WHERE team_id = '".$team_id."'";
397
        $result = $this->mysqli->query($req);
398
        $data = $result->fetch_object();
399
400
        return $data;
401
    }
402
403
    public function getAllGyms()
404
    {
405
        $req = "SELECT gym_id, team_id, latitude, longitude,
406
				CONVERT_TZ(last_scanned, '+00:00', '".self::$time_offset."') AS last_scanned,
407
				(6 - slots_available) AS level
408
				FROM gym";
409
        $result = $this->mysqli->query($req);
410
        $gyms = array();
411
        while ($data = $result->fetch_object()) {
412
            $gyms[] = $data;
413
        }
414
415
        return $gyms;
416
    }
417
418 View Code Duplication
    public function getGymData($gym_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...
419
    {
420
        $req = "SELECT gymdetails.name AS name, gymdetails.description AS description, gymdetails.url AS url, gym.team_id AS team,
421
				CONVERT_TZ(gym.last_scanned, '+00:00', '".self::$time_offset."') AS last_scanned,
422
				gym.guard_pokemon_id AS guard_pokemon_id,
423
				gym.total_cp AS total_cp,
424
				(6 - gym.slots_available) AS level
425
				FROM gymdetails
426
				LEFT JOIN gym ON gym.gym_id = gymdetails.gym_id
427
				WHERE gym.gym_id='".$gym_id."'";
428
        $result = $this->mysqli->query($req);
429
        $data = $result->fetch_object();
430
431
        return $data;
432
    }
433
434
    public function getGymDefenders($gym_id)
435
    {
436
        $req = "SELECT DISTINCT gympokemon.pokemon_uid, pokemon_id, iv_attack, iv_defense, iv_stamina, MAX(cp) AS cp, gymmember.gym_id
437
				FROM gympokemon
438
				INNER JOIN gymmember ON gympokemon.pokemon_uid=gymmember.pokemon_uid
439
				GROUP BY gympokemon.pokemon_uid, pokemon_id, iv_attack, iv_defense, iv_stamina, gym_id
440
				HAVING gymmember.gym_id='".$gym_id."'
441
				ORDER BY cp DESC";
442
        $result = $this->mysqli->query($req);
443
        $defenders = array();
444
        while ($data = $result->fetch_object()) {
445
            $defenders[] = $data;
446
        }
447
448
        return $defenders;
449
    }
450
451
    ////////////////
452
    // Gym History
453
    ////////////////
454
455
    public function getGymHistories($gym_name, $team, $page, $ranking)
456
    {
457
        $where = '';
458
        if (isset($gym_name) && '' != $gym_name) {
459
            $where = " WHERE name LIKE '%".$gym_name."%'";
460
        }
461 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...
462
            $where .= ('' == $where ? ' WHERE' : ' AND').' team_id = '.$team;
463
        }
464 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...
465
            case 1:
466
                $order = ' ORDER BY name, last_modified DESC';
467
                break;
468
            case 2:
469
                $order = ' ORDER BY total_cp DESC, last_modified DESC';
470
                break;
471
            default:
472
                $order = ' ORDER BY last_modified DESC, name';
473
        }
474
        $req = "SELECT gymdetails.gym_id, name, team_id, total_cp,
475
				(6 - slots_available) as pokemon_count,
476
				CONVERT_TZ(last_modified, '+00:00', '".self::$time_offset."') as last_modified
477
				FROM gymdetails
478
				LEFT JOIN gym
479
				ON gymdetails.gym_id = gym.gym_id
480
				".$where.$order."
481
				LIMIT ".($page * 10).",10";
482
        $result = $this->mysqli->query($req);
483
        $gym_history = array();
484
        while ($data = $result->fetch_object()) {
485
            $gym_history[] = $data;
486
        }
487
488
        return $gym_history;
489
    }
490
491
    public function getGymHistoriesPokemon($gym_id)
492
    {
493
        $req = "SELECT DISTINCT gymmember.pokemon_uid, pokemon_id, cp, trainer_name
494
				FROM gymmember
495
				LEFT JOIN gympokemon
496
				ON gymmember.pokemon_uid = gympokemon.pokemon_uid
497
				WHERE gymmember.gym_id = '".$gym_id."'
498
				ORDER BY deployment_time";
499
        $result = $this->mysqli->query($req);
500
        $pokemons = array();
501
        while ($data = $result->fetch_object()) {
502
            $pokemons[] = $data;
503
        }
504
505
        return $pokemons;
506
    }
507
508
    public function getHistoryForGym($page, $gym_id)
509
    {
510 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...
511
            $pageSize = 25;
512
        } else {
513
            $pageSize = 10;
514
        }
515
        $req = "SELECT gym_id, team_id, total_cp, pokemon_uids, pokemon_count,
516
				CONVERT_TZ(last_modified, '+00:00', '".self::$time_offset."') as last_modified
517
				FROM gymhistory
518
				WHERE gym_id='".$gym_id."'
519
				ORDER BY last_modified DESC
520
				LIMIT ".($page * $pageSize).','.($pageSize + 1);
521
        $result = $this->mysqli->query($req);
522
        $history = array();
523
        $count = 0;
524
        while ($data = $result->fetch_object()) {
525
            ++$count;
526
            $pkm = array();
527
            if (0 == $data->total_cp) {
528
                $data->pokemon_uids = '';
529
                $data->pokemon_count = 0;
530
            }
531
            if ('' != $data->pokemon_uids) {
532
                $pkm_uids = explode(',', $data->pokemon_uids);
533
                $pkm = $this->getHistoryForGymPokemon($pkm_uids);
534
            }
535
            $data->pokemon = $pkm;
536
            $history[] = $data;
537
        }
538
        if ($count !== ($pageSize + 1)) {
539
            $last_page = true;
540
        } else {
541
            $last_page = false;
542
        }
543
544
        return array('last_page' => $last_page, 'data' => $history);
545
    }
546
547 View Code Duplication
    private function getHistoryForGymPokemon($pkm_uids)
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...
548
    {
549
        $req = "SELECT DISTINCT pokemon_uid, pokemon_id, cp, trainer_name
550
				FROM gympokemon
551
				WHERE pokemon_uid IN ('".implode("','", $pkm_uids)."')
552
				ORDER BY FIND_IN_SET(pokemon_uid, '".implode(',', $pkm_uids)."')";
553
        $result = $this->mysqli->query($req);
554
        $pokemons = array();
555
        while ($data = $result->fetch_object()) {
556
            $pokemons[$data->pokemon_uid] = $data;
557
        }
558
559
        return $pokemons;
560
    }
561
562
    ///////////
563
    // Raids
564
    ///////////
565
566
    public function getAllRaids($page)
567
    {
568
        $req = "SELECT raid.gym_id, raid.level, raid.pokemon_id, raid.cp, raid.move_1, raid.move_2,
569
				CONVERT_TZ(raid.spawn, '+00:00', '".self::$time_offset."') AS spawn,
570
				CONVERT_TZ(raid.start, '+00:00', '".self::$time_offset."') AS start,
571
				CONVERT_TZ(raid.end, '+00:00', '".self::$time_offset."') AS end,
572
				CONVERT_TZ(raid.last_scanned, '+00:00', '".self::$time_offset."') AS last_scanned,
573
				gymdetails.name, gym.latitude, gym.longitude
574
				FROM raid
575
				JOIN gymdetails ON gymdetails.gym_id = raid.gym_id
576
				JOIN gym ON gym.gym_id = raid.gym_id
577
				WHERE raid.end > UTC_TIMESTAMP()
578
				ORDER BY raid.level DESC, raid.start
579
				LIMIT ".($page * 10).",10";
580
        $result = $this->mysqli->query($req);
581
        $raids = array();
582
        while ($data = $result->fetch_object()) {
583
            $raids[] = $data;
584
        }
585
586
        return $raids;
587
    }
588
589
    //////////////
590
    // Trainers
591
    //////////////
592
593
    public function getTrainers($trainer_name, $team, $page, $ranking)
594
    {
595
        $trainers = $this->getTrainerData($trainer_name, $team, $page, $ranking);
596
        foreach ($trainers as $trainer) {
597
            $trainer->rank = $this->getTrainerLevelRating($trainer->level)->rank;
598
            $active_gyms = 0;
599
            $pkmCount = 0;
600
            $trainer->pokemons = array();
601
            $active_pokemon = $this->getTrainerActivePokemon($trainer->name);
602
            foreach ($active_pokemon as $pokemon) {
603
                ++$active_gyms;
604
                $trainer->pokemons[$pkmCount++] = $pokemon;
605
            }
606
            $inactive_pokemon = $this->getTrainerInactivePokemon($trainer->name);
607
            foreach ($inactive_pokemon as $pokemon) {
608
                $trainer->pokemons[$pkmCount++] = $pokemon;
609
            }
610
            $trainer->gyms = ''.$active_gyms;
611
        }
612
613
        return $trainers;
614
    }
615
616 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...
617
    {
618
        $req = "SELECT level, count(level) AS count FROM trainer WHERE team = '".$team_id."'";
619
        if (!empty(self::$config->system->trainer_blacklist)) {
620
            $req .= " AND name NOT IN ('".implode("','", self::$config->system->trainer_blacklist)."')";
621
        }
622
        $req .= ' GROUP BY level';
623
        $result = $this->mysqli->query($req);
624
        $levelData = array();
625
        while ($data = $result->fetch_object()) {
626
            $levelData[$data->level] = $data->count;
627
        }
628
        for ($i = 5; $i <= 40; ++$i) {
629
            if (!isset($levelData[$i])) {
630
                $levelData[$i] = 0;
631
            }
632
        }
633
        // sort array again
634
        ksort($levelData);
635
636
        return $levelData;
637
    }
638
639
    private function getTrainerData($trainer_name, $team, $page, $ranking)
640
    {
641
        $where = '';
642
        if (!empty(self::$config->system->trainer_blacklist)) {
643
            $where .= ('' == $where ? ' HAVING' : ' AND')." name NOT IN ('".implode("','", self::$config->system->trainer_blacklist)."')";
644
        }
645
        if ('' != $trainer_name) {
646
            $where = " HAVING name LIKE '%".$trainer_name."%'";
647
        }
648 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...
649
            $where .= ('' == $where ? ' HAVING' : ' AND').' team = '.$team;
650
        }
651 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...
652
            case 1:
653
                $order = ' ORDER BY active DESC, level DESC';
654
                break;
655
            case 2:
656
                $order = ' ORDER BY maxCp DESC, level DESC';
657
                break;
658
            default:
659
                $order = ' ORDER BY level DESC, active DESC';
660
        }
661
        $order .= ', last_seen DESC, name ';
662
        $limit = ' LIMIT '.($page * 10).',10 ';
663
        $req = "SELECT trainer.*, COUNT(actives_pokemons.trainer_name) AS active, max(actives_pokemons.cp) AS maxCp
664
				FROM trainer
665
				LEFT JOIN (SELECT DISTINCT gympokemon.pokemon_id, gympokemon.pokemon_uid, gympokemon.trainer_name, gympokemon.cp, DATEDIFF(UTC_TIMESTAMP(), gympokemon.last_seen) AS last_scanned
666
				FROM gympokemon
667
				INNER JOIN (SELECT gymmember.pokemon_uid, gymmember.gym_id FROM gymmember GROUP BY gymmember.pokemon_uid, gymmember.gym_id HAVING gymmember.gym_id <> '') AS filtered_gymmember
668
				ON gympokemon.pokemon_uid = filtered_gymmember.pokemon_uid) AS actives_pokemons ON actives_pokemons.trainer_name = trainer.name
669
				GROUP BY trainer.name ".$where.$order.$limit;
670
        $result = $this->mysqli->query($req);
671
        $trainers = array();
672
        while ($data = $result->fetch_object()) {
673
            $data->last_seen = date('Y-m-d', strtotime($data->last_seen));
674
            $trainers[$data->name] = $data;
675
        }
676
677
        return $trainers;
678
    }
679
680
    private function getTrainerLevelRating($level)
681
    {
682
        $req = 'SELECT COUNT(1) AS rank FROM trainer WHERE level = '.$level;
683
        if (!empty(self::$config->system->trainer_blacklist)) {
684
            $req .= " AND name NOT IN ('".implode("','", self::$config->system->trainer_blacklist)."')";
685
        }
686
        $result = $this->mysqli->query($req);
687
        $data = $result->fetch_object();
688
689
        return $data;
690
    }
691
692 View Code Duplication
    private function getTrainerActivePokemon($trainer_name)
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...
693
    {
694
        $req = "SELECT DISTINCT gympokemon.pokemon_id, gympokemon.pokemon_uid, gympokemon.cp,
695
				DATEDIFF(UTC_TIMESTAMP(), gympokemon.last_seen) AS last_scanned,
696
				gympokemon.trainer_name, gympokemon.iv_defense, gympokemon.iv_stamina, gympokemon.iv_attack,
697
				filtered_gymmember.gym_id,
698
				CONVERT_TZ(filtered_gymmember.deployment_time, '+00:00', '".self::$time_offset."') as deployment_time,
699
				'1' AS active
700
				FROM gympokemon INNER JOIN
701
				(SELECT gymmember.pokemon_uid, gymmember.gym_id, gymmember.deployment_time FROM gymmember GROUP BY gymmember.pokemon_uid, gymmember.deployment_time, gymmember.gym_id HAVING gymmember.gym_id <> '') AS filtered_gymmember
702
				ON gympokemon.pokemon_uid = filtered_gymmember.pokemon_uid
703
				WHERE gympokemon.trainer_name='".$trainer_name."'
704
				ORDER BY gympokemon.cp DESC";
705
        $result = $this->mysqli->query($req);
706
        $pokemons = array();
707
        while ($data = $result->fetch_object()) {
708
            $pokemons[] = $data;
709
        }
710
711
        return $pokemons;
712
    }
713
714 View Code Duplication
    private function getTrainerInactivePokemon($trainer_name)
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...
715
    {
716
        $req = "SELECT DISTINCT gympokemon.pokemon_id, gympokemon.pokemon_uid, gympokemon.cp,
717
				DATEDIFF(UTC_TIMESTAMP(), gympokemon.last_seen) AS last_scanned,
718
				gympokemon.trainer_name, gympokemon.iv_defense, gympokemon.iv_stamina, gympokemon.iv_attack,
719
				null AS gym_id,
720
				CONVERT_TZ(filtered_gymmember.deployment_time, '+00:00', '".self::$time_offset."') as deployment_time,
721
				'0' AS active
722
				FROM gympokemon LEFT JOIN
723
				(SELECT * FROM gymmember HAVING gymmember.gym_id <> '') AS filtered_gymmember
724
				ON gympokemon.pokemon_uid = filtered_gymmember.pokemon_uid
725
				WHERE filtered_gymmember.pokemon_uid IS NULL AND gympokemon.trainer_name='".$trainer_name."'
726
				ORDER BY gympokemon.cp DESC";
727
        $result = $this->mysqli->query($req);
728
        $pokemons = array();
729
        while ($data = $result->fetch_object()) {
730
            $pokemons[] = $data;
731
        }
732
733
        return $pokemons;
734
    }
735
736
    /////////
737
    // Cron
738
    /////////
739
740
    public function getPokemonCountsActive()
741
    {
742
        $req = 'SELECT pokemon_id, COUNT(*) as total
743
				FROM pokemon
744
				WHERE disappear_time >= UTC_TIMESTAMP()
745
				GROUP BY pokemon_id';
746
        $result = $this->mysqli->query($req);
747
        $counts = array();
748
        while ($data = $result->fetch_object()) {
749
            $counts[$data->pokemon_id] = $data->total;
750
        }
751
752
        return $counts;
753
    }
754
755
    public function getPokemonCountsLastDay()
756
    {
757
        $req = 'SELECT pokemon_id, COUNT(*) AS spawns_last_day
758
				FROM pokemon
759
				WHERE disappear_time >= (SELECT MAX(disappear_time) FROM pokemon) - INTERVAL 1 DAY
760
				GROUP BY pokemon_id
761
				ORDER BY pokemon_id ASC';
762
        $result = $this->mysqli->query($req);
763
        $counts = array();
764
        while ($data = $result->fetch_object()) {
765
            $counts[$data->pokemon_id] = $data->spawns_last_day;
766
        }
767
768
        return $counts;
769
    }
770
771
    public function getCaptchaCount()
772
    {
773
        $req = 'SELECT SUM(accounts_captcha) AS total FROM mainworker';
774
        $result = $this->mysqli->query($req);
775
        $data = $result->fetch_object();
776
777
        return $data;
778
    }
779
780 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...
781
    {
782
        $pokemon_exclude_sql = '';
783
        if (!empty(self::$config->system->nest_exclude_pokemon)) {
784
            $pokemon_exclude_sql = 'AND pokemon_id NOT IN ('.implode(',', self::$config->system->nest_exclude_pokemon).')';
785
        }
786
        $req = 'SELECT spawnpoint_id, pokemon_id, MAX(latitude) AS latitude, MAX(longitude) AS longitude, count(pokemon_id) AS total_pokemon,
787
				MAX(UNIX_TIMESTAMP(disappear_time)) as latest_seen
788
				FROM pokemon
789
				WHERE disappear_time > (UTC_TIMESTAMP() - INTERVAL '.$time.' HOUR)
790
				AND latitude >= '.$minLatitude.' AND latitude < '.$maxLatitude.' AND longitude >= '.$minLongitude.' AND longitude < '.$maxLongitude.'
791
				'.$pokemon_exclude_sql.' 
792
				GROUP BY spawnpoint_id, pokemon_id 
793
				HAVING COUNT(pokemon_id) >= '.($time / 4).'
794
				ORDER BY pokemon_id';
795
        $result = $this->mysqli->query($req);
796
        $nests = array();
797
        while ($data = $result->fetch_object()) {
798
            $nests[] = $data;
799
        }
800
801
        return $nests;
802
    }
803
804 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...
805
    {
806
        $req = 'SELECT COUNT(*) as total 
807
				FROM spawnpoint
808
				WHERE latitude >= '.$minLatitude.' AND latitude < '.$maxLatitude.' AND longitude >= '.$minLongitude.' AND longitude < '.$maxLongitude;
809
        $result = $this->mysqli->query($req);
810
        $data = $result->fetch_object();
811
812
        return $data;
813
    }
814
}
815