Failed Conditions
Pull Request — master (#371)
by
unknown
02:05
created

QueryManagerMysqlRocketmap   F

Complexity

Total Complexity 115

Size/Duplication

Total Lines 810
Duplicated Lines 40.25 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 326
loc 810
rs 1.79
c 0
b 0
f 0
wmc 115
lcom 1
cbo 1

46 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __destruct() 0 4 1
A testTotalPokemon() 15 17 3
A testTotalGyms() 15 17 3
A testTotalPokestops() 15 17 3
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 getRecentMythic() 16 19 3
A getGymsProtectedByPokemon() 6 8 1
A getPokemonLastSeen() 10 14 1
A getTop50Pokemon() 19 19 2
A getTop50Trainers() 21 22 3
A getPokemonHeatmap() 14 14 2
A getPokemonGraph() 16 18 3
B getPokemonLive() 33 33 11
A getPokemonSliderMinMax() 0 8 1
A getMapsCoords() 0 10 1
A getPokemonCount() 0 10 1
A getRaidCount() 0 10 1
A getTotalPokestops() 0 8 1
A getAllPokestops() 0 13 2
A getTeamGuardians() 0 15 2
A getOwnedAndPoints() 0 11 1
A getAllGyms() 0 14 2
A getGymData() 10 15 1
A getGymDefenders() 0 16 2
B getGymHistories() 13 35 9
A getGymHistoriesPokemon() 0 16 2
B getHistoryForGym() 5 38 7
A getHistoryForGymPokemon() 12 14 2
A getAllRaids() 0 22 2
A getTrainers() 0 22 4
A getTrainerLevelCount() 22 22 5
B getTrainerData() 13 40 9
A getTrainerLevelRating() 0 11 2
A getTrainerActivePokemon() 21 21 2
A getTrainerInactivePokemon() 21 21 2
A getPokemonCountsActive() 0 14 2
A getPokemonCountsLastDay() 0 15 2
A getCaptchaCount() 0 8 1
A getNestData() 21 23 3
A getSpawnpointCount() 8 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 QueryManagerMysqlRocketmap 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 QueryManagerMysqlRocketmap, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
final class QueryManagerMysqlRocketmap extends QueryManagerMysql
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    public function __construct()
6
    {
7
        parent::__construct();
8
    }
9
10
    public function __destruct()
11
    {
12
        parent::__destruct();
13
    }
14
15
    ///////////
16
    // Tester
17
    ///////////
18
19 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...
20
    {
21
        $req = 'SELECT COUNT(*) as total FROM pokemon';
22
        $result = $this->mysqli->query($req);
23
        if (!is_object($result)) {
24
            return 1;
25
        } else {
26
            $data = $result->fetch_object();
27
            $total = $data->total;
28
29
            if (0 == $total) {
30
                return 2;
31
            }
32
        }
33
34
        return 0;
35
    }
36
37 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...
38
    {
39
        $req = 'SELECT COUNT(*) as total FROM gym';
40
        $result = $this->mysqli->query($req);
41
        if (!is_object($result)) {
42
            return 1;
43
        } else {
44
            $data = $result->fetch_object();
45
            $total = $data->total;
46
47
            if (0 == $total) {
48
                return 2;
49
            }
50
        }
51
52
        return 0;
53
    }
54
55 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...
56
    {
57
        $req = 'SELECT COUNT(*) as total FROM pokestop';
58
        $result = $this->mysqli->query($req);
59
        if (!is_object($result)) {
60
            return 1;
61
        } else {
62
            $data = $result->fetch_object();
63
            $total = $data->total;
64
65
            if (0 == $total) {
66
                return 2;
67
            }
68
        }
69
70
        return 0;
71
    }
72
73
    /////////////
74
    // Homepage
75
    /////////////
76
77
    public function getTotalPokemon()
78
    {
79
        $req = 'SELECT COUNT(*) AS total FROM pokemon WHERE disappear_time >= UTC_TIMESTAMP()';
80
        $result = $this->mysqli->query($req);
81
        $data = $result->fetch_object();
82
83
        return $data;
84
    }
85
86
    public function getTotalLures()
87
    {
88
        $req = 'SELECT COUNT(*) AS total FROM pokestop WHERE lure_expiration >= UTC_TIMESTAMP()';
89
        $result = $this->mysqli->query($req);
90
        $data = $result->fetch_object();
91
92
        return $data;
93
    }
94
95
    public function getTotalGyms()
96
    {
97
        $req = 'SELECT COUNT(DISTINCT(gym_id)) AS total FROM gym';
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 raid WHERE start <= UTC_TIMESTAMP AND  end >= UTC_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(DISTINCT(gym_id)) AS total FROM gym WHERE team_id = '".$team_id."'";
116
        $result = $this->mysqli->query($req);
117
        $data = $result->fetch_object();
118
119
        return $data;
120
    }
121
122
    public function getRecentAll()
123
    {
124
        $req = "SELECT DISTINCT pokemon_id, encounter_id, disappear_time, last_modified,
125
				CONVERT_TZ(disappear_time, '+00:00', '".self::$time_offset."') AS disappear_time_real,
126
				latitude, longitude, cp, individual_attack, individual_defense, individual_stamina
127
				FROM pokemon
128
				ORDER BY last_modified DESC
129
				LIMIT 0,12";
130
        $result = $this->mysqli->query($req);
131
        $data = array();
132
        if ($result->num_rows > 0) {
133
            while ($row = $result->fetch_object()) {
134
                $data[] = $row;
135
            }
136
        }
137
138
        return $data;
139
    }
140
141 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...
142
    {
143
        $req = "SELECT DISTINCT pokemon_id, encounter_id, disappear_time, last_modified,
144
				CONVERT_TZ(disappear_time, '+00:00', '".self::$time_offset."') AS disappear_time_real,
145
				latitude, longitude, cp, individual_attack, individual_defense, individual_stamina
146
				FROM pokemon
147
				WHERE pokemon_id IN (".implode(',', $mythic_pokemons).')
148
				ORDER BY last_modified DESC
149
				LIMIT 0,12';
150
        $result = $this->mysqli->query($req);
151
        $data = array();
152
        if ($result->num_rows > 0) {
153
            while ($row = $result->fetch_object()) {
154
                $data[] = $row;
155
            }
156
        }
157
158
        return $data;
159
    }
160
161
    ///////////////////
162
    // Single Pokemon
163
    ///////////////////
164
165 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...
166
    {
167
        $req = "SELECT COUNT(DISTINCT(gym_id)) AS total FROM gym WHERE guard_pokemon_id = '".$pokemon_id."'";
168
        $result = $this->mysqli->query($req);
169
        $data = $result->fetch_object();
170
171
        return $data;
172
    }
173
174 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...
175
    {
176
        $req = "SELECT disappear_time,
177
				CONVERT_TZ(disappear_time, '+00:00', '".self::$time_offset."') AS disappear_time_real,
178
				latitude, longitude
179
				FROM pokemon
180
				WHERE pokemon_id = '".$pokemon_id."'
181
				ORDER BY disappear_time DESC
182
				LIMIT 0,1";
183
        $result = $this->mysqli->query($req);
184
        $data = $result->fetch_object();
185
186
        return $data;
187
    }
188
189 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...
190
    {
191
        $req = "SELECT CONVERT_TZ(disappear_time, '+00:00', '".self::$time_offset."') AS distime,
192
				pokemon_id, disappear_time, latitude, longitude,
193
				cp, individual_attack, individual_defense, individual_stamina,
194
				ROUND(100*(individual_attack+individual_defense+individual_stamina)/45,1) AS IV,
195
				move_1, move_2, form
196
				FROM pokemon
197
				WHERE pokemon_id = '".$pokemon_id."' AND move_1 IS NOT NULL AND move_1 <> '0'
198
				ORDER BY $top_order_by $top_direction, disappear_time DESC
199
				LIMIT 0,50";
200
        $result = $this->mysqli->query($req);
201
        $top = array();
202
        while ($data = $result->fetch_object()) {
203
            $top[] = $data;
204
        }
205
206
        return $top;
207
    }
208
209 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...
210
    {
211
        $trainer_blacklist = '';
212
        if (!empty(self::$config->system->trainer_blacklist)) {
213
            $trainer_blacklist = " AND trainer_name NOT IN ('".implode("','", self::$config->system->trainer_blacklist)."')";
214
        }
215
        $req = "SELECT trainer_name,
216
				ROUND((100*(iv_attack+iv_defense+iv_stamina)/45),1) AS IV,
217
				move_1, move_2, cp,
218
				DATE_FORMAT(last_seen, '%Y-%m-%d') AS lasttime, last_seen
219
				FROM gympokemon
220
				WHERE pokemon_id = '".$pokemon_id."'".$trainer_blacklist."
221
				ORDER BY $best_order_by $best_direction, trainer_name ASC
222
				LIMIT 0,50";
223
        $result = $this->mysqli->query($req);
224
        $toptrainer = array();
225
        while ($data = $result->fetch_object()) {
226
            $toptrainer[] = $data;
227
        }
228
229
        return $toptrainer;
230
    }
231
232 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...
233
    {
234
		$req = "SELECT latitude, longitude
235
				FROM pokemon
236
				WHERE pokemon_id = ".$pokemon_id." AND disappear_time BETWEEN '".$start."' AND '".$end."'
237
				LIMIT 10000";
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,
250
				HOUR(CONVERT_TZ(disappear_time, '+00:00', '".self::$time_offset."')) AS disappear_hour
251
				FROM (SELECT disappear_time FROM pokemon WHERE pokemon_id = '".$pokemon_id."' LIMIT 100000) AS pokemonFiltered
252
				GROUP BY disappear_hour
253
				ORDER BY disappear_hour";
254
        $result = $this->mysqli->query($req);
255
        $array = array_fill(0, 24, 0);
256
        while ($result && $data = $result->fetch_object()) {
257
            $array[$data->disappear_hour] = $data->total;
258
        }
259
        // shift array because AM/PM starts at 1AM not 0:00
260
        $array[] = $array[0];
261
        array_shift($array);
262
263
        return $array;
264
    }
265
266 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...
267
    {
268
        $inmap_pkms_filter = '';
269
        $where = ' WHERE disappear_time >= UTC_TIMESTAMP() AND pokemon_id = '.$pokemon_id;
270
        $reqTestIv = 'SELECT MAX(individual_attack) AS iv FROM pokemon '.$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)*(individual_attack+individual_defense+individual_stamina)) >= ('.$ivMin.') ';
282
        }
283
        if (null != $testIv->iv && !is_null($ivMax) && ('' != $ivMax)) {
284
            $where .= ' AND ((100/45)*(individual_attack+individual_defense+individual_stamina)) <= ('.$ivMax.') ';
285
        }
286
        $req = "SELECT pokemon_id, encounter_id, latitude, longitude, disappear_time,
287
				CONVERT_TZ(disappear_time, '+00:00', '".self::$time_offset."') AS disappear_time_real,
288
				individual_attack, individual_defense, individual_stamina, move_1, move_2
289
				FROM pokemon ".$where.'
290
				LIMIT 5000';
291
        $result = $this->mysqli->query($req);
292
        $spawns = array();
293
        while ($data = $result->fetch_object()) {
294
            $spawns[] = $data;
295
        }
296
297
        return $spawns;
298
    }
299
300
    public function getPokemonSliderMinMax()
301
    {
302
        $req = 'SELECT MIN(disappear_time) AS min, MAX(disappear_time) AS max FROM pokemon';
303
        $result = $this->mysqli->query($req);
304
        $data = $result->fetch_object();
305
306
        return $data;
307
    }
308
309
    public function getMapsCoords()
310
    {
311
        $req = 'SELECT MAX(latitude) AS max_latitude, MIN(latitude) AS min_latitude,
312
				MAX(longitude) AS max_longitude, MIN(longitude) as min_longitude
313
				FROM spawnpoint';
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 pokestop';
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 latitude, longitude, lure_expiration, UTC_TIMESTAMP() AS now,
358
				CONVERT_TZ(lure_expiration, '+00:00', '".self::$time_offset."') AS lure_expiration_real
359
				FROM pokestop";
360
        $result = $this->mysqli->query($req);
361
        $pokestops = array();
362
        while ($data = $result->fetch_object()) {
363
            $pokestops[] = $data;
364
        }
365
366
        return $pokestops;
367
    }
368
369
    /////////
370
    // Gyms
371
    /////////
372
373
    public function getTeamGuardians($team_id)
374
    {
375
        $req = "SELECT COUNT(*) AS total, guard_pokemon_id
376
				FROM gym WHERE team_id = '".$team_id."'
377
				GROUP BY guard_pokemon_id
378
				ORDER BY total DESC
379
				LIMIT 0,3";
380
        $result = $this->mysqli->query($req);
381
        $datas = array();
382
        while ($data = $result->fetch_object()) {
383
            $datas[] = $data;
384
        }
385
386
        return $datas;
387
    }
388
389
    public function getOwnedAndPoints($team_id)
390
    {
391
        $req = "SELECT COUNT(DISTINCT(gym_id)) AS total,
392
				ROUND(AVG(total_cp),0) AS average_points
393
				FROM gym
394
				WHERE team_id = '".$team_id."'";
395
        $result = $this->mysqli->query($req);
396
        $data = $result->fetch_object();
397
398
        return $data;
399
    }
400
401
    public function getAllGyms()
402
    {
403
        $req = "SELECT gym_id, team_id, latitude, longitude,
404
				CONVERT_TZ(last_scanned, '+00:00', '".self::$time_offset."') AS last_scanned,
405
				(6 - slots_available) AS level
406
				FROM gym";
407
        $result = $this->mysqli->query($req);
408
        $gyms = array();
409
        while ($data = $result->fetch_object()) {
410
            $gyms[] = $data;
411
        }
412
413
        return $gyms;
414
    }
415
416 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...
417
    {
418
        $req = "SELECT gymdetails.name AS name, gymdetails.description AS description, gymdetails.url AS url, gym.team_id AS team,
419
				CONVERT_TZ(gym.last_scanned, '+00:00', '".self::$time_offset."') AS last_scanned,
420
				gym.guard_pokemon_id AS guard_pokemon_id,
421
				gym.total_cp AS total_cp,
422
				(6 - gym.slots_available) AS level
423
				FROM gymdetails
424
				LEFT JOIN gym ON gym.gym_id = gymdetails.gym_id
425
				WHERE gym.gym_id='".$gym_id."'";
426
        $result = $this->mysqli->query($req);
427
        $data = $result->fetch_object();
428
429
        return $data;
430
    }
431
432
    public function getGymDefenders($gym_id)
433
    {
434
        $req = "SELECT DISTINCT gympokemon.pokemon_uid, pokemon_id, iv_attack, iv_defense, iv_stamina, MAX(cp) AS cp, gymmember.gym_id
435
				FROM gympokemon
436
				INNER JOIN gymmember ON gympokemon.pokemon_uid=gymmember.pokemon_uid
437
				GROUP BY gympokemon.pokemon_uid, pokemon_id, iv_attack, iv_defense, iv_stamina, gym_id
438
				HAVING gymmember.gym_id='".$gym_id."'
439
				ORDER BY cp DESC";
440
        $result = $this->mysqli->query($req);
441
        $defenders = array();
442
        while ($data = $result->fetch_object()) {
443
            $defenders[] = $data;
444
        }
445
446
        return $defenders;
447
    }
448
449
    ////////////////
450
    // Gym History
451
    ////////////////
452
453
    public function getGymHistories($gym_name, $team, $page, $ranking)
454
    {
455
        $where = '';
456
        if (isset($gym_name) && '' != $gym_name) {
457
            $where = " WHERE name LIKE '%".$gym_name."%'";
458
        }
459 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...
460
            $where .= ('' == $where ? ' WHERE' : ' AND').' team_id = '.$team;
461
        }
462 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...
463
            case 1:
464
                $order = ' ORDER BY name, last_modified DESC';
465
                break;
466
            case 2:
467
                $order = ' ORDER BY total_cp DESC, last_modified DESC';
468
                break;
469
            default:
470
                $order = ' ORDER BY last_modified DESC, name';
471
        }
472
        $req = "SELECT gymdetails.gym_id, name, team_id, total_cp,
473
				(6 - slots_available) as pokemon_count,
474
				CONVERT_TZ(last_modified, '+00:00', '".self::$time_offset."') as last_modified
475
				FROM gymdetails
476
				LEFT JOIN gym
477
				ON gymdetails.gym_id = gym.gym_id
478
				".$where.$order."
479
				LIMIT ".($page * 10).",10";
480
        $result = $this->mysqli->query($req);
481
        $gym_history = array();
482
        while ($data = $result->fetch_object()) {
483
            $gym_history[] = $data;
484
        }
485
486
        return $gym_history;
487
    }
488
489
    public function getGymHistoriesPokemon($gym_id)
490
    {
491
        $req = "SELECT DISTINCT gymmember.pokemon_uid, pokemon_id, cp, trainer_name
492
				FROM gymmember
493
				LEFT JOIN gympokemon
494
				ON gymmember.pokemon_uid = gympokemon.pokemon_uid
495
				WHERE gymmember.gym_id = '".$gym_id."'
496
				ORDER BY deployment_time";
497
        $result = $this->mysqli->query($req);
498
        $pokemons = array();
499
        while ($data = $result->fetch_object()) {
500
            $pokemons[] = $data;
501
        }
502
503
        return $pokemons;
504
    }
505
506
    public function getHistoryForGym($page, $gym_id)
507
    {
508 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...
509
            $pageSize = 25;
510
        } else {
511
            $pageSize = 10;
512
        }
513
        $req = "SELECT gym_id, team_id, total_cp, pokemon_uids, pokemon_count,
514
				CONVERT_TZ(last_modified, '+00:00', '".self::$time_offset."') as last_modified
515
				FROM gymhistory
516
				WHERE gym_id='".$gym_id."'
517
				ORDER BY last_modified DESC
518
				LIMIT ".($page * $pageSize).','.($pageSize + 1);
519
        $result = $this->mysqli->query($req);
520
        $history = array();
521
        $count = 0;
522
        while ($data = $result->fetch_object()) {
523
            ++$count;
524
            $pkm = array();
525
            if (0 == $data->total_cp) {
526
                $data->pokemon_uids = '';
527
                $data->pokemon_count = 0;
528
            }
529
            if ('' != $data->pokemon_uids) {
530
                $pkm_uids = explode(',', $data->pokemon_uids);
531
                $pkm = $this->getHistoryForGymPokemon($pkm_uids);
532
            }
533
            $data->pokemon = $pkm;
534
            $history[] = $data;
535
        }
536
        if ($count !== ($pageSize + 1)) {
537
            $last_page = true;
538
        } else {
539
            $last_page = false;
540
        }
541
542
        return array('last_page' => $last_page, 'data' => $history);
543
    }
544
545 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...
546
    {
547
        $req = "SELECT DISTINCT pokemon_uid, pokemon_id, cp, trainer_name
548
				FROM gympokemon
549
				WHERE pokemon_uid IN ('".implode("','", $pkm_uids)."')
550
				ORDER BY FIND_IN_SET(pokemon_uid, '".implode(',', $pkm_uids)."')";
551
        $result = $this->mysqli->query($req);
552
        $pokemons = array();
553
        while ($data = $result->fetch_object()) {
554
            $pokemons[$data->pokemon_uid] = $data;
555
        }
556
557
        return $pokemons;
558
    }
559
560
    ///////////
561
    // Raids
562
    ///////////
563
564
    public function getAllRaids($page)
565
    {
566
        $req = "SELECT raid.gym_id, raid.level, raid.pokemon_id, raid.cp, raid.move_1, raid.move_2,
567
				CONVERT_TZ(raid.spawn, '+00:00', '".self::$time_offset."') AS spawn,
568
				CONVERT_TZ(raid.start, '+00:00', '".self::$time_offset."') AS start,
569
				CONVERT_TZ(raid.end, '+00:00', '".self::$time_offset."') AS end,
570
				CONVERT_TZ(raid.last_scanned, '+00:00', '".self::$time_offset."') AS last_scanned,
571
				gymdetails.name, gym.latitude, gym.longitude
572
				FROM raid
573
				JOIN gymdetails ON gymdetails.gym_id = raid.gym_id
574
				JOIN gym ON gym.gym_id = raid.gym_id
575
				WHERE raid.end > UTC_TIMESTAMP()
576
				ORDER BY raid.level DESC, raid.start
577
				LIMIT ".($page * 10).",10";
578
        $result = $this->mysqli->query($req);
579
        $raids = array();
580
        while ($data = $result->fetch_object()) {
581
            $raids[] = $data;
582
        }
583
584
        return $raids;
585
    }
586
587
    //////////////
588
    // Trainers
589
    //////////////
590
591
    public function getTrainers($trainer_name, $team, $page, $ranking)
592
    {
593
        $trainers = $this->getTrainerData($trainer_name, $team, $page, $ranking);
594
        foreach ($trainers as $trainer) {
595
            $trainer->rank = $this->getTrainerLevelRating($trainer->level)->rank;
596
            $active_gyms = 0;
597
            $pkmCount = 0;
598
            $trainer->pokemons = array();
599
            $active_pokemon = $this->getTrainerActivePokemon($trainer->name);
600
            foreach ($active_pokemon as $pokemon) {
601
                ++$active_gyms;
602
                $trainer->pokemons[$pkmCount++] = $pokemon;
603
            }
604
            $inactive_pokemon = $this->getTrainerInactivePokemon($trainer->name);
605
            foreach ($inactive_pokemon as $pokemon) {
606
                $trainer->pokemons[$pkmCount++] = $pokemon;
607
            }
608
            $trainer->gyms = ''.$active_gyms;
609
        }
610
611
        return $trainers;
612
    }
613
614 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...
615
    {
616
        $req = "SELECT level, count(level) AS count FROM trainer WHERE team = '".$team_id."'";
617
        if (!empty(self::$config->system->trainer_blacklist)) {
618
            $req .= " AND name NOT IN ('".implode("','", self::$config->system->trainer_blacklist)."')";
619
        }
620
        $req .= ' GROUP BY level';
621
        $result = $this->mysqli->query($req);
622
        $levelData = array();
623
        while ($data = $result->fetch_object()) {
624
            $levelData[$data->level] = $data->count;
625
        }
626
        for ($i = 5; $i <= 40; ++$i) {
627
            if (!isset($levelData[$i])) {
628
                $levelData[$i] = 0;
629
            }
630
        }
631
        // sort array again
632
        ksort($levelData);
633
634
        return $levelData;
635
    }
636
637
    private function getTrainerData($trainer_name, $team, $page, $ranking)
638
    {
639
        $where = '';
640
        if (!empty(self::$config->system->trainer_blacklist)) {
641
            $where .= ('' == $where ? ' HAVING' : ' AND')." name NOT IN ('".implode("','", self::$config->system->trainer_blacklist)."')";
642
        }
643
        if ('' != $trainer_name) {
644
            $where = " HAVING name LIKE '%".$trainer_name."%'";
645
        }
646 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...
647
            $where .= ('' == $where ? ' HAVING' : ' AND').' team = '.$team;
648
        }
649 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...
650
            case 1:
651
                $order = ' ORDER BY active DESC, level DESC';
652
                break;
653
            case 2:
654
                $order = ' ORDER BY maxCp DESC, level DESC';
655
                break;
656
            default:
657
                $order = ' ORDER BY level DESC, active DESC';
658
        }
659
        $order .= ', last_seen DESC, name ';
660
        $limit = ' LIMIT '.($page * 10).',10 ';
661
        $req = "SELECT trainer.*, COUNT(actives_pokemons.trainer_name) AS active, max(actives_pokemons.cp) AS maxCp
662
				FROM trainer
663
				LEFT JOIN (SELECT DISTINCT gympokemon.pokemon_id, gympokemon.pokemon_uid, gympokemon.trainer_name, gympokemon.cp, DATEDIFF(UTC_TIMESTAMP(), gympokemon.last_seen) AS last_scanned
664
				FROM gympokemon
665
				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
666
				ON gympokemon.pokemon_uid = filtered_gymmember.pokemon_uid) AS actives_pokemons ON actives_pokemons.trainer_name = trainer.name
667
				GROUP BY trainer.name ".$where.$order.$limit;
668
        $result = $this->mysqli->query($req);
669
        $trainers = array();
670
        while ($data = $result->fetch_object()) {
671
            $data->last_seen = date('Y-m-d', strtotime($data->last_seen));
672
            $trainers[$data->name] = $data;
673
        }
674
675
        return $trainers;
676
    }
677
678
    private function getTrainerLevelRating($level)
679
    {
680
        $req = 'SELECT COUNT(1) AS rank FROM trainer WHERE level = '.$level;
681
        if (!empty(self::$config->system->trainer_blacklist)) {
682
            $req .= " AND name NOT IN ('".implode("','", self::$config->system->trainer_blacklist)."')";
683
        }
684
        $result = $this->mysqli->query($req);
685
        $data = $result->fetch_object();
686
687
        return $data;
688
    }
689
690 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...
691
    {
692
        $req = "SELECT DISTINCT gympokemon.pokemon_id, gympokemon.pokemon_uid, gympokemon.cp,
693
				DATEDIFF(UTC_TIMESTAMP(), gympokemon.last_seen) AS last_scanned,
694
				gympokemon.trainer_name, gympokemon.iv_defense, gympokemon.iv_stamina, gympokemon.iv_attack,
695
				filtered_gymmember.gym_id,
696
				CONVERT_TZ(filtered_gymmember.deployment_time, '+00:00', '".self::$time_offset."') as deployment_time,
697
				'1' AS active
698
				FROM gympokemon INNER JOIN
699
				(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
700
				ON gympokemon.pokemon_uid = filtered_gymmember.pokemon_uid
701
				WHERE gympokemon.trainer_name='".$trainer_name."'
702
				ORDER BY gympokemon.cp DESC";
703
        $result = $this->mysqli->query($req);
704
        $pokemons = array();
705
        while ($data = $result->fetch_object()) {
706
            $pokemons[] = $data;
707
        }
708
709
        return $pokemons;
710
    }
711
712 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...
713
    {
714
        $req = "SELECT DISTINCT gympokemon.pokemon_id, gympokemon.pokemon_uid, gympokemon.cp,
715
				DATEDIFF(UTC_TIMESTAMP(), gympokemon.last_seen) AS last_scanned,
716
				gympokemon.trainer_name, gympokemon.iv_defense, gympokemon.iv_stamina, gympokemon.iv_attack,
717
				null AS gym_id,
718
				CONVERT_TZ(filtered_gymmember.deployment_time, '+00:00', '".self::$time_offset."') as deployment_time,
719
				'0' AS active
720
				FROM gympokemon LEFT JOIN
721
				(SELECT * FROM gymmember HAVING gymmember.gym_id <> '') AS filtered_gymmember
722
				ON gympokemon.pokemon_uid = filtered_gymmember.pokemon_uid
723
				WHERE filtered_gymmember.pokemon_uid IS NULL AND gympokemon.trainer_name='".$trainer_name."'
724
				ORDER BY gympokemon.cp DESC";
725
        $result = $this->mysqli->query($req);
726
        $pokemons = array();
727
        while ($data = $result->fetch_object()) {
728
            $pokemons[] = $data;
729
        }
730
731
        return $pokemons;
732
    }
733
734
    /////////
735
    // Cron
736
    /////////
737
738
    public function getPokemonCountsActive()
739
    {
740
        $req = 'SELECT pokemon_id, COUNT(*) as total
741
				FROM pokemon
742
				WHERE disappear_time >= UTC_TIMESTAMP()
743
				GROUP BY pokemon_id';
744
        $result = $this->mysqli->query($req);
745
        $counts = array();
746
        while ($data = $result->fetch_object()) {
747
            $counts[$data->pokemon_id] = $data->total;
748
        }
749
750
        return $counts;
751
    }
752
753
    public function getPokemonCountsLastDay()
754
    {
755
        $req = 'SELECT pokemon_id, COUNT(*) AS spawns_last_day
756
				FROM pokemon
757
				WHERE disappear_time >= (SELECT MAX(disappear_time) FROM pokemon) - INTERVAL 1 DAY
758
				GROUP BY pokemon_id
759
				ORDER BY pokemon_id ASC';
760
        $result = $this->mysqli->query($req);
761
        $counts = array();
762
        while ($data = $result->fetch_object()) {
763
            $counts[$data->pokemon_id] = $data->spawns_last_day;
764
        }
765
766
        return $counts;
767
    }
768
769
    public function getCaptchaCount()
770
    {
771
        $req = 'SELECT SUM(accounts_captcha) AS total FROM mainworker';
772
        $result = $this->mysqli->query($req);
773
        $data = $result->fetch_object();
774
775
        return $data;
776
    }
777
778 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...
779
    {
780
        $pokemon_exclude_sql = '';
781
        if (!empty(self::$config->system->nest_exclude_pokemon)) {
782
            $pokemon_exclude_sql = 'AND pokemon_id NOT IN ('.implode(',', self::$config->system->nest_exclude_pokemon).')';
783
        }
784
        $req = 'SELECT spawnpoint_id, pokemon_id, MAX(latitude) AS latitude, MAX(longitude) AS longitude, count(pokemon_id) AS total_pokemon,
785
				MAX(UNIX_TIMESTAMP(disappear_time)) as latest_seen
786
				FROM pokemon
787
				WHERE disappear_time > (UTC_TIMESTAMP() - INTERVAL '.$time.' HOUR)
788
				AND latitude >= '.$minLatitude.' AND latitude < '.$maxLatitude.' AND longitude >= '.$minLongitude.' AND longitude < '.$maxLongitude.'
789
				'.$pokemon_exclude_sql.' 
790
				GROUP BY spawnpoint_id, pokemon_id 
791
				HAVING COUNT(pokemon_id) >= '.($time / 4).'
792
				ORDER BY pokemon_id';
793
        $result = $this->mysqli->query($req);
794
        $nests = array();
795
        while ($data = $result->fetch_object()) {
796
            $nests[] = $data;
797
        }
798
799
        return $nests;
800
    }
801
802 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...
803
    {
804
        $req = 'SELECT COUNT(*) as total 
805
				FROM spawnpoint
806
				WHERE latitude >= '.$minLatitude.' AND latitude < '.$maxLatitude.' AND longitude >= '.$minLongitude.' AND longitude < '.$maxLongitude;
807
        $result = $this->mysqli->query($req);
808
        $data = $result->fetch_object();
809
810
        return $data;
811
    }
812
}
813