Failed Conditions
Pull Request — master (#374)
by Michael
01:54
created

QueryManagerMysqlMAD   F

Complexity

Total Complexity 118

Size/Duplication

Total Lines 832
Duplicated Lines 72 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 599
loc 832
rs 1.768
c 0
b 0
f 0
wmc 118
lcom 1
cbo 1

49 Methods

Rating   Name   Duplication   Size   Complexity  
A testTotalPokemon() 17 17 3
A testTotalGyms() 17 17 3
A testTotalPokestops() 17 17 3
A getRecentMythic() 19 19 3
A getGymsProtectedByPokemon() 8 8 1
A getPokemonLastSeen() 14 14 1
A getTop50Pokemon() 19 19 2
A __construct() 0 4 1
A __destruct() 0 4 1
A getTotalPokemon() 0 8 1
A getTotalLures() 0 8 1
A getTotalGyms() 0 8 1
A getTotalRaids() 0 8 1
A getTotalGymsForTeam() 0 8 1
A getRecentAll() 0 18 3
A getTop50Trainers() 0 4 1
A getPokemonHeatmap() 14 14 2
A getPokemonGraph() 18 18 3
B getPokemonLive() 33 33 11
A getPokemonSliderMinMax() 0 8 1
A getMapsCoords() 0 10 1
A getPokemonCount() 0 10 1
A getPokemonCountAll() 13 13 2
A getRaidCount() 0 10 1
A getRaidCountAll() 13 13 2
A getTotalPokestops() 0 8 1
A getAllPokestops() 13 13 2
A getTeamGuardians() 15 15 2
A getOwnedAndPoints() 0 11 1
A getAllGyms() 14 14 2
A getGymData() 15 15 1
A getGymDefenders() 16 16 2
B getGymHistories() 35 35 9
A getGymHistoriesPokemon() 16 16 2
B getHistoryForGym() 38 38 7
A getHistoryForGymPokemon() 14 14 2
A getAllRaids() 22 22 2
A getTrainers() 22 22 4
A getTrainerLevelCount() 22 22 5
B getTrainerData() 40 40 9
A getTrainerLevelRating() 11 11 2
A getTrainerActivePokemon() 21 21 2
A getTrainerInactivePokemon() 21 21 2
A getPokemonCountsActive() 14 14 2
A getTotalPokemonIV() 0 10 1
A getPokemonCountsLastDay() 15 15 2
A getCaptchaCount() 0 8 1
A getNestData() 23 23 3
A getSpawnpointCount() 10 10 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like QueryManagerMysqlMAD often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use QueryManagerMysqlMAD, and based on these observations, apply Extract Interface, too.

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