Failed Conditions
Pull Request — master (#353)
by Florian
05:21 queued 02:46
created

getGymHistories()   D

Complexity

Conditions 9
Paths 36

Size

Total Lines 34
Code Lines 23

Duplication

Lines 34
Ratio 100 %

Importance

Changes 0
Metric Value
cc 9
eloc 23
nc 36
nop 4
dl 34
loc 34
rs 4.909
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: floriankostenzer
5
 * Date: 27.01.18
6
 * Time: 02:26
7
 */
8
9
class QueryManagerPostgresqlMonocleAlternate extends QueryManagerPostgresql {
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...
10
11
	public function __construct() {
12
		parent::__construct();
13
	}
14
15
	public function __destruct() {
16
		parent::__destruct();
17
	}
18
19
	///////////
20
	// Tester
21
	///////////
22
23 View Code Duplication
	function testTotalPokemon() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for testTotalPokemon.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
24
		$req = "SELECT COUNT(*) as total FROM sightings";
25
		$result = pg_query($this->db, $req);
26
		if ($result === false) {
27
			return 1;
28
		} else {
29
			$data = pg_fetch_object($result);
30
			$total = $data->total;
31
32
			if ($total == 0) {
33
				return 2;
34
			}
35
		}
36
		return 0;
37
	}
38
39 View Code Duplication
	function testTotalGyms() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for testTotalGyms.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
40
		$req = "SELECT COUNT(*) as total FROM forts";
41
		$result = pg_query($this->db, $req);
42
		if ($result === false) {
43
			return 1;
44
		} else {
45
			$data = pg_fetch_object($result);
46
			$total = $data->total;
47
48
			if ($total == 0) {
49
				return 2;
50
			}
51
		}
52
		return 0;
53
	}
54
55 View Code Duplication
	function testTotalPokestops() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for testTotalPokestops.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
56
		$req = "SELECT COUNT(*) as total FROM pokestops";
57
		$result = pg_query($this->db, $req);
58
		if ($result === false) {
59
			return 1;
60
		} else {
61
			$data = pg_fetch_object($result);
62
			$total = $data->total;
63
64
			if ($total == 0) {
65
				return 2;
66
			}
67
		}
68
		return 0;
69
	}
70
71
72
	/////////////
73
	// Homepage
74
	/////////////
75
76 View Code Duplication
	function getTotalPokemon() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for getTotalPokemon.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
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...
77
		$req = "SELECT COUNT(*) AS total FROM sightings WHERE expire_timestamp >= EXTRACT(EPOCH FROM NOW())";
78
		$result = pg_query($this->db, $req);
79
		$data = pg_fetch_object($result);
80
		return $data;
81
	}
82
83
	function getTotalLures() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for getTotalLures.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
84
		$data = (object) array("total" => 0);
85
		return $data;
86
	}
87
88 View Code Duplication
	function getTotalGyms() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for getTotalGyms.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
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...
89
		$req = "SELECT COUNT(*) AS total FROM forts";
90
		$result = pg_query($this->db, $req);
91
		$data = pg_fetch_object($result);
92
		return $data;
93
	}
94
95 View Code Duplication
	function getTotalRaids() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for getTotalRaids.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
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...
96
		$req = "SELECT COUNT(*) AS total FROM raids WHERE time_battle <= EXTRACT(EPOCH FROM NOW()) AND time_end >= EXTRACT(EPOCH FROM NOW())";
97
		$result = pg_query($this->db, $req);
98
		$data = pg_fetch_object($result);
99
		return $data;
100
	}
101
102
103 View Code Duplication
	function getTotalGymsForTeam($team_id) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for getTotalGymsForTeam.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
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...
104
		$req = "SELECT COUNT(*) AS total
105
					FROM forts f
106
					LEFT JOIN fort_sightings fs ON (fs.fort_id = f.id AND fs.last_modified = (SELECT MAX(last_modified) FROM fort_sightings fs2 WHERE fs2.fort_id=f.id))
107
					WHERE team = '$team_id'";
108
		$result = pg_query($this->db, $req);
109
		$data = pg_fetch_object($result);
110
		return $data;
111
	}
112
113
	function getRecentAll() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for getRecentAll.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
114
		$req = "SELECT pokemon_id, encounter_id, TO_TIMESTAMP(expire_timestamp) AS disappear_time, TO_TIMESTAMP(updated) AS last_modified, TO_TIMESTAMP(expire_timestamp) AS disappear_time_real,
115
              lat AS latitude, lon AS longitude, cp, atk_iv AS individual_attack, def_iv AS individual_defense, sta_iv AS individual_stamina
116
              FROM sightings
117
              ORDER BY updated DESC
118
              LIMIT 12 OFFSET 0";
119
		$result = pg_query($this->db, $req);
120
		$data = array();
121
		if ($result->num_rows > 0) {
122
			while ($row = pg_fetch_object($result)) {
123
				$data[] = $row;
124
			}
125
		}
126
		return $data;
127
	}
128
129
	function getRecentMythic($mythic_pokemon) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for getRecentMythic.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
130
		$req = "SELECT pokemon_id, encounter_id, TO_TIMESTAMP(expire_timestamp) AS disappear_time, TO_TIMESTAMP(updated) AS last_modified, TO_TIMESTAMP(expire_timestamp) AS disappear_time_real,
131
                lat AS latitude, lon AS longitude, cp, atk_iv AS individual_attack, def_iv AS individual_defense, sta_iv AS individual_stamina
132
                FROM sightings
133
                WHERE pokemon_id IN (".implode(",", $mythic_pokemon).")
134
                ORDER BY updated DESC
135
                LIMIT 12 OFFSET 0";
136
		$result = pg_query($this->db, $req);
137
		$data = array();
138
		if ($result->num_rows > 0) {
139
			while ($row = pg_fetch_object($result)) {
140
				$data[] = $row;
141
			}
142
		}
143
		return $data;
144
	}
145
146
	///////////////////
147
	// Single Pokemon
148
	///////////////////
149
150 View Code Duplication
	function getGymsProtectedByPokemon($pokemon_id) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for getGymsProtectedByPokemon.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
151
		$req = "SELECT COUNT(f.id) AS total
152
					FROM forts f
153
					LEFT JOIN fort_sightings fs ON (fs.fort_id = f.id AND fs.last_modified = (SELECT MAX(last_modified) FROM fort_sightings fs2 WHERE fs2.fort_id=f.id))
154
					WHERE guard_pokemon_id = '".$pokemon_id."'";
155
		$result = pg_query($this->db, $req);
156
		$data = pg_fetch_object($result);
157
		return $data;
158
	}
159
160 View Code Duplication
	function getPokemonLastSeen($pokemon_id) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for getPokemonLastSeen.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
161
		$req = "SELECT TO_TIMESTAMP(expire_timestamp) AS expire_timestamp, TO_TIMESTAMP(expire_timestamp) AS disappear_time_real, lat AS latitude, lon AS longitude
162
                FROM sightings
163
                WHERE pokemon_id = '".$pokemon_id."'
164
                ORDER BY expire_timestamp DESC
165
                LIMIT 1 OFFSET 0";
166
		$result = pg_query($this->db, $req);
167
		$data = pg_fetch_object($result);
168
		return $data;
169
	}
170
171 View Code Duplication
	function getTop50Pokemon($pokemon_id, $top_order_by, $top_direction) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for getTop50Pokemon.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
172
		$req = "SELECT expire_timestamp, TO_TIMESTAMP(expire_timestamp) AS distime, pokemon_id as pokemon_id, TO_TIMESTAMP(expire_timestamp) as disappear_time, lat as latitude, lon as longitude,
173
                cp, atk_iv as individual_attack, def_iv as individual_defense, sta_iv as individual_stamina,
174
                ROUND(100*(atk_iv+def_iv+sta_iv)/45,1) AS \"IV\", move_1 as move_1, move_2, form
175
                FROM sightings
176
	            WHERE pokemon_id = '" . $pokemon_id . "' AND move_1 IS NOT NULL AND move_1 <> '0'
177
	            ORDER BY $top_order_by $top_direction, expire_timestamp DESC
178
	            LIMIT 50 OFFSET 0";
179
		$result = pg_query($this->db, $req);
180
		$top = array();
181
		while ($data = pg_fetch_object($result)) {
182
			$top[] = $data;
183
		}
184
		return $top;
185
	}
186
187
	function getTop50Trainers($pokemon_id, $best_order_by, $best_direction) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for getTop50Trainers.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
188
		$trainer_blacklist = "";
189
		if (!empty(self::$config->system->trainer_blacklist)) {
190
			$trainer_blacklist = " AND owner_name NOT IN ('" . implode("','", self::$config->system->trainer_blacklist) . "')";
191
		}
192
193
		$req = "SELECT owner_name as trainer_name, ROUND((100.0*((atk_iv)+(def_iv)+(sta_iv))/45),1) AS \"IV\", move_1, move_2, cp as cp,
194
                TO_TIMESTAMP(last_modified) AS lasttime, last_modified as last_seen
195
                FROM gym_defenders
196
				WHERE pokemon_id = '" . $pokemon_id . "'" . $trainer_blacklist . "
197
				ORDER BY $best_order_by $best_direction, owner_name ASC
198
				LIMIT 50 OFFSET 0";
199
200
		$result = pg_query($this->db, $req);
201
		$toptrainer = array();
202
		while ($data = pg_fetch_object($result)) {
203
			$toptrainer[] = $data;
204
		}
205
		return $toptrainer;
206
	}
207
208
	public function getPokemonHeatmap($pokemon_id, $start, $end) {
209
		$where = " WHERE pokemon_id = ".$pokemon_id." "
210
			. "AND TO_TIMESTAMP(expire_timestamp) BETWEEN '".$start."' AND '".$end."'";
211
		$req 		= "SELECT lat AS latitude, lon AS longitude FROM sightings".$where." LIMIT 100000";
212
		$result = pg_query($this->db, $req);
213
		$points = array();
214
		while ($data = pg_fetch_object($result)) {
215
			$points[] = $data;
216
		}
217
		return $points;
218
	}
219
220
	public function getPokemonGraph($pokemon_id) {
221
		$req = "SELECT COUNT(*) AS total, EXTRACT(HOUR FROM disappear_time) AS disappear_hour
222
					FROM (SELECT TO_TIMESTAMP(expire_timestamp) as disappear_time FROM sightings WHERE pokemon_id = '".$pokemon_id."' LIMIT 100000) AS pokemonFiltered
223
				GROUP BY disappear_hour
224
				ORDER BY disappear_hour";
225
		$result = pg_query($this->db, $req);
226
		$array = array_fill(0, 24, 0);
227
		while ($result && $data = pg_fetch_object($result)) {
228
			$array[$data->disappear_hour] = $data->total;
229
		}
230
		// shift array because AM/PM starts at 1AM not 0:00
231
		$array[] = $array[0];
232
		array_shift($array);
233
		return $array;
234
	}
235
236
	public function getPokemonLive($pokemon_id, $ivMin, $ivMax, $inmap_pokemons) {
237
		$inmap_pkms_filter = "";
238
		$where = " WHERE expire_timestamp >= EXTRACT(EPOCH FROM NOW()) AND pokemon_id = " . $pokemon_id;
239
240
		$reqTestIv = "SELECT MAX(atk_iv) AS iv FROM sightings " . $where;
241
		$resultTestIv = pg_query($this->db, $reqTestIv);
242
		$testIv = pg_fetch_object($resultTestIv);
243
		if (!is_null($inmap_pokemons) && ($inmap_pokemons != "")) {
244
			foreach ($inmap_pokemons as $inmap) {
245
				$inmap_pkms_filter .= "'".$inmap."',";
246
			}
247
			$inmap_pkms_filter = rtrim($inmap_pkms_filter, ",");
248
			$where .= " AND encounter_id NOT IN (" . $inmap_pkms_filter . ") ";
249
		}
250
		if ($testIv->iv != null && !is_null($ivMin) && ($ivMin != "")) {
251
			$where .= " AND ((100/45)*(atk_iv + def_iv + sta_iv)) >= (" . $ivMin . ") ";
252
		}
253
		if ($testIv->iv != null && !is_null($ivMax) && ($ivMax != "")) {
254
			$where .= " AND ((100/45)*(atk_iv + def_iv + sta_iv)) <= (" . $ivMax . ") ";
255
		}
256
		$req = "SELECT pokemon_id, lat AS latitude, lon AS longitude,
257
    					TO_TIMESTAMP(expire_timestamp) AS disappear_time,
258
    					TO_TIMESTAMP(expire_timestamp) AS disappear_time_real,
259
    					atk_iv AS individual_attack, def_iv AS individual_defense, sta_iv AS individual_stamina,
260
   						move_1, move_2
261
					FROM sightings " . $where . "
262
					LIMIT 5000";
263
		$result = pg_query($this->db, $req);
264
		$spawns = array();
265
		while ($data = pg_fetch_object($result)) {
266
			$spawns[] = $data;
267
		}
268
		return $spawns;
269
	}
270
271 View Code Duplication
	public function getPokemonSliderMinMax() {
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...
272
		$req = "SELECT TO_TIMESTAMP(MIN(expire_timestamp)) AS min, TO_TIMESTAMP(MAX(expire_timestamp)) AS max FROM sightings";
273
		$result = pg_query($this->db, $req);
274
		$data = pg_fetch_object($result);
275
		return $data;
276
	}
277
278 View Code Duplication
	public function getMapsCoords() {
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...
279
		$req = "SELECT MAX(lat) AS max_latitude, MIN(lat) AS min_latitude, MAX(lon) AS max_longitude, MIN(lon) as min_longitude FROM spawnpoints";
280
		$result = pg_query($this->db, $req);
281
		$data = pg_fetch_object($result);
282
		return $data;
283
	}
284
285 View Code Duplication
	public function getPokemonCount($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...
286
		$req = "SELECT count, last_seen, latitude, longitude
287
					FROM pokemon_stats
288
					WHERE pid = ".$pokemon_id;
289
		$result = pg_query($this->db, $req);
290
		$data = pg_fetch_object($result);
291
		return $data;
292
	}
293
294 View Code Duplication
	public function getRaidCount($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...
295
		$req = "SELECT count, last_seen, latitude, longitude
296
					FROM raid_stats
297
					WHERE pid = ".$pokemon_id;
298
		$result = pg_query($this->db, $req);
299
		$data = pg_fetch_object($result);
300
		return $data;
301
	}
302
303
304
	///////////////
305
	// Pokestops
306
	//////////////
307
308
309 View Code Duplication
	function getTotalPokestops() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for getTotalPokestops.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
310
		$req = "SELECT COUNT(*) as total FROM pokestops";
311
		$result = pg_query($this->db, $req);
312
		$data = pg_fetch_object($result);
313
		return $data;
314
	}
315
316
	public function getAllPokestops() {
317
		$req = "SELECT lat as latitude, lon as longitude, null AS lure_expiration, EXTRACT(EPOCH FROM NOW()) AS now, null AS lure_expiration_real FROM pokestops";
318
		$result = pg_query($this->db, $req);
319
		$pokestops = array();
320
		while ($data = pg_fetch_object($result)) {
321
			$pokestops[] = $data;
322
		}
323
		return $pokestops;
324
	}
325
326
327
	/////////
328
	// Gyms
329
	/////////
330
331
	function getTeamGuardians($team_id) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for getTeamGuardians.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
332
		$req = "SELECT COUNT(*) AS total, guard_pokemon_id
333
					FROM forts f
334
					LEFT JOIN fort_sightings fs ON (fs.fort_id = f.id AND fs.last_modified = (SELECT MAX(last_modified) FROM fort_sightings fs2 WHERE fs2.fort_id=f.id))
335
					WHERE team = '".$team_id."' GROUP BY guard_pokemon_id ORDER BY total DESC LIMIT 3 OFFSET 0";
336
		$result = pg_query($this->db, $req);
337
338
		$datas = array();
339
		while ($data = pg_fetch_object($result)) {
340
			$datas[] = $data;
341
		}
342
343
		return $datas;
344
	}
345
346 View Code Duplication
	function getOwnedAndPoints($team_id) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for getOwnedAndPoints.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
347
		$req = "SELECT COUNT(f.id) AS total, ROUND(AVG(fs.total_cp))AS average_points
348
        			FROM forts f
349
					LEFT JOIN fort_sightings fs ON (fs.fort_id = f.id AND fs.last_modified = (SELECT MAX(last_modified) FROM fort_sightings fs2 WHERE fs2.fort_id=f.id))
350
        			WHERE fs.team = '" . $team_id . "'";
351
		$result = pg_query($this->db, $req);
352
		$data = pg_fetch_object($result);
353
		return $data;
354
	}
355
356
	function getAllGyms() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for getAllGyms.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
357
		$req = "SELECT f.id as gym_id, team as team_id, f.lat as latitude, f.lon as longitude, updated as last_scanned, (6 - fs.slots_available) AS level
358
					FROM forts f
359
					LEFT JOIN fort_sightings fs ON (fs.fort_id = f.id AND fs.last_modified = (SELECT MAX(last_modified) FROM fort_sightings fs2 WHERE fs2.fort_id=f.id))";
360
		$result = pg_query($this->db, $req);
361
		$gyms = array();
362
		while ($data = pg_fetch_object($result)) {
363
			$gyms[] = $data;
364
		}
365
		return $gyms;
366
	}
367
368 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...
369
		$req = "SELECT f.name AS name, null AS description, f.url AS url, fs.team AS team, TO_TIMESTAMP(fs.updated) AS last_scanned, fs.guard_pokemon_id AS guard_pokemon_id, (6 - fs.slots_available) AS level, fs.total_cp
370
			FROM forts f
371
			LEFT JOIN fort_sightings fs ON (fs.fort_id = f.id AND fs.last_modified = (SELECT MAX(last_modified) FROM fort_sightings fs2 WHERE fs2.fort_id=f.id))
372
			WHERE f.id ='".$gym_id."'
373
			GROUP BY f.name, f.url, fs.team, fs.updated, fs.guard_pokemon_id, fs.slots_available, gd.cp";
374
		$result = pg_query($this->db, $req);
375
		$data = pg_fetch_object($result);
376
		return $data;
377
	}
378
379
	public function getGymDefenders($gym_id) {
380
		$req = "SELECT external_id as pokemon_uid, pokemon_id, atk_iv as iv_attack, def_iv as iv_defense, sta_iv as iv_stamina, cp, fort_id as gym_id
381
			FROM gym_defenders
382
			WHERE fort_id='".$gym_id."'
383
			ORDER BY deployment_time";
384
		$result = pg_query($this->db, $req);
385
		$defenders = array();
386
		while ($data = pg_fetch_object($result)) {
387
			$defenders[] = $data;
388
		}
389
		return $defenders;
390
	}
391
392
393
394
	////////////////
395
	// Gym History
396
	////////////////
397
398 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...
399
	{
400
		$where = "";
401
		if (isset($gym_name) && $gym_name != '') {
402
			$where = " WHERE name LIKE '%".$gym_name."%'";
403
		}
404
		if (isset($team) && $team != '') {
405
			$where .= ($where === "" ? " WHERE" : " AND")." fs.team = ".$team;
406
		}
407
		switch ($ranking) {
408
			case 1:
409
				$order = " ORDER BY name, last_modified DESC";
410
				break;
411
			case 2:
412
				$order = " ORDER BY total_cp DESC, last_modified DESC";
413
				break;
414
			default:
415
				$order = " ORDER BY last_modified DESC, name";
416
		}
417
418
		$limit = " LIMIT 10 OFFSET ".($page * 10);
419
420
		$req = "SELECT f.id as gym_id, fs.total_cp, f.name, fs.team as team_id, (6 - slots_available) as pokemon_count, TO_TIMESTAMP(last_modified) AS last_modified
421
			FROM forts f
422
			LEFT JOIN fort_sightings fs ON (fs.fort_id = f.id AND fs.last_modified = (SELECT MAX(last_modified) FROM fort_sightings fs2 WHERE fs2.fort_id=f.id))
423
			".$where.$order.$limit;
424
425
		$result = $this->mysqli->query($req);
0 ignored issues
show
Bug introduced by
The property mysqli does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
426
		$gym_history = array();
427
		while ($data = $result->fetch_object()) {
428
			$gym_history[] = $data;
429
		}
430
		return $gym_history;
431
	}
432
433
	public function getGymHistoriesPokemon($gym_id)
434
	{
435
		$req = "SELECT external_id AS pokemon_uid, pokemon_id, cp_now as cp, owner_name AS trainer_name
436
					FROM gym_defenders
437
					WHERE fort_id = '". $gym_id ."'
438
					ORDER BY deployment_time";
439
		$result = $this->mysqli->query($req);
440
		$pokemons = array();
441
		while ($data = $result->fetch_object()) {
442
			$pokemons[] = $data;
443
		}
444
		return $pokemons;
445
	}
446
447 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...
448
	{
449
		if (isset(self::$config->system->gymhistory_hide_cp_changes) && self::$config->system->gymhistory_hide_cp_changes === true) {
450
			$pageSize = 25;
451
		} else {
452
			$pageSize = 10;
453
		}
454
		$req = "SELECT f.id as gym_id, fs.team as team_id, total_cp, TO_TIMESTAMP(fs.last_modified) as last_modified, last_modified as last_modified_real
455
					FROM fort_sightings fs
456
					LEFT JOIN forts f ON f.id = fs.fort_id
457
					WHERE f.id = '". $gym_id ."'
458
					ORDER BY fs.last_modified DESC
459
					LIMIT ".($pageSize+1)." OFFSET ".($page * $pageSize);
460
		$result = $this->mysqli->query($req);
461
		$history = array();
462
		$count = 0;
463
		while ($data = $result->fetch_object()) {
464
			$count++;
465
			if ($data->total_cp == 0) {
466
				$data->pokemon = array();
467
				$data->pokemon_count = 0;
468
				$data->pokemon_uids = "";
469
			} else {
470
				$data->pokemon = $this->getHistoryForGymPokemon($gym_id, $data->last_modified_real);
471
				$data->pokemon_count = count($data->pokemon);
472
				$data->pokemon_uids = implode(",", array_keys($data->pokemon));
473
			}
474
			if ($data->total_cp === 0 || $data->pokemon_count !== 0) {
475
				$history[] = $data;
476
			}
477
		}
478
		if ($count !== ($pageSize + 1)) {
479
			$last_page = true;
480
		} else {
481
			$last_page = false;
482
		}
483
		return array("last_page" => $last_page, "data" => $history);
484
	}
485
486 View Code Duplication
	private function getHistoryForGymPokemon($gym_id, $last_modified)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
487
	{
488
		$req = "SELECT ghd.defender_id, gd.pokemon_id, ghd.cp, gd.owner_name as trainer_name
489
					FROM gym_history_defenders ghd
490
					JOIN gym_defenders gd ON ghd.defender_id = gd.external_id
491
					WHERE ghd.fort_id = '". $gym_id ."' AND date = '".$last_modified."'
492
					ORDER BY gd.deployment_time";
493
		$result = $this->mysqli->query($req);
494
		$pokemons = array();
495
		while ($data = $result->fetch_object()) {
496
			$pokemons[$data->defender_id] = $data;
497
		}
498
		return $pokemons;
499
	}
500
501
	///////////
502
	// Raids
503
	///////////
504
505 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...
506
		$limit = " LIMIT 10 OFFSET ". ($page * 10);
507
		$req = "SELECT r.fort_id AS gym_id, r.level AS level, r.pokemon_id AS pokemon_id, r.cp AS cp, r.move_1 AS move_1, r.move_2 AS move_2, TO_TIMESTAMP(r.time_spawn) AS spawn, TO_TIMESTAMP(r.time_battle) AS start, TO_TIMESTAMP(r.time_end) AS end, TO_TIMESTAMP(fs.updated) AS last_scanned, f.name, f.lat AS latitude, f.lon as longitude
508
					FROM forts f
509
					LEFT JOIN fort_sightings fs ON (fs.fort_id = f.id AND fs.last_modified = (SELECT MAX(last_modified) FROM fort_sightings fs2 WHERE fs2.fort_id=f.id))
510
				 	LEFT JOIN raids r ON (r.fort_id = f.id AND r.time_end >= UNIX_TIMESTAMP())
511
					WHERE r.time_end > EXTRACT(EPOCH FROM NOW())
512
					ORDER BY r.level DESC, r.time_battle" . $limit;
513
		$result = pg_query($this->db, $req);
514
		$raids = array();
515
		while ($data = pg_fetch_object($result)) {
516
			$raids[] = $data;
517
		}
518
		return $raids;
519
	}
520
521
522
	//////////////
523
	// Trainers
524
	//////////////
525
526 View Code Duplication
	public function getTrainers($trainer_name, $team, $page, $rankingNumber) {
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...
527
		$ranking = $this->getTrainerLevelRanking();
528
		$where = "";
529
		if (!empty(self::$config->system->trainer_blacklist)) {
530
			$where .= " AND gd.owner_name NOT IN ('".implode("','", self::$config->system->trainer_blacklist)."')";
531
		}
532
		if ($trainer_name != "") {
533
			$where = " AND gd.owner_name LIKE '%".$trainer_name."%'";
534
		}
535
		if ($team != 0) {
536
			$where .= ($where == "" ? " HAVING" : " AND")." team = ".$team;
537
		}
538
		switch ($rankingNumber) {
539
			case 1:
540
				$order = " ORDER BY active DESC, level DESC";
541
				break;
542
			case 2:
543
				$order = " ORDER BY maxCp DESC, level DESC";
544
				break;
545
			default:
546
				$order = " ORDER BY level DESC, active DESC";
547
		}
548
		$order .= ", last_seen DESC, name ";
549
		$limit = " LIMIT 10 OFFSET ".($page * 10);
550
		$req = "SELECT gd.owner_name AS name, MAX(owner_level) AS level, MAX(cp) AS maxCp, MAX(active) AS active, MAX(team) AS team, TO_TIMESTAMP(MAX(last_modified)) as last_seen
551
				  	FROM gym_defenders gd
552
				  	LEFT JOIN (
553
				  		SELECT owner_name, COUNT(*) as active
554
				  		FROM gym_defenders gd2
555
						WHERE fort_id IS NOT NULL
556
				  		GROUP BY owner_name
557
				  	) active ON active.owner_name = gd.owner_name
558
				  	WHERE level IS NOT NULL " . $where . "
559
				  	GROUP BY gd.owner_name" . $order  . $limit;
560
		$result = $this->mysqli->query($req);
561
		$trainers = array();
562
		while ($data = $result->fetch_object()) {
563
			$data->last_seen = date("Y-m-d", strtotime($data->last_seen));
564
			if (is_null($data->active)) {
565
				$data->active = 0;
566
			}
567
			$trainers[$data->name] = $data;
568
569
			$pokemon = array_merge($this->getActivePokemon($data->name),  $this->getInactivePokemon($data->name));
0 ignored issues
show
Coding Style introduced by
Expected 1 space instead of 2 after comma in function call.
Loading history...
570
571
			$trainers[$data->name]->gyms = $data->active;
572
			$trainers[$data->name]->pokemons = $pokemon;
573
			$trainers[$data->name]->rank = $ranking[$data->level];
574
		}
575
		return $trainers;
576
	}
577
578 View Code Duplication
	public function getTrainerLevelRanking() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
579
		$exclue = "";
580
		if (!empty(self::$config->system->trainer_blacklist)) {
581
			$exclue .= " AND owner_name NOT IN ('".implode("','", self::$config->system->trainer_blacklist)."')";
582
		}
583
		$req = "SELECT COUNT(*) AS count, level FROM (SELECT MAX(owner_level) as level FROM gym_defenders WHERE owner_level IS NOT NULL ".$exclue." GROUP BY owner_level, owner_name) x GROUP BY level";
584
		$result = $this->mysqli->query($req);
585
		$levelData = array();
586
		while ($data = $result->fetch_object()) {
587
			$levelData[$data->level] = $data->count;
588
		}
589
		for ($i = 5; $i <= 40; $i++) {
590
			if (!isset($levelData[$i])) {
591
				$levelData[$i] = 0;
592
			}
593
		}
594
		# sort array again
595
		ksort($levelData);
596
		return $levelData;
597
	}
598
599
	public function getActivePokemon($trainer_name) {
600
		$req = "SELECT pokemon_id, cp, atk_iv AS iv_attack, sta_iv AS iv_stamina, def_iv AS iv_defense, TO_TIMESTAMP(deployment_time) AS deployment_time, '1' AS active, fort_id as gym_id, FLOOR((UNIX_TIMESTAMP() - created) / 86400) AS last_scanned
601
						FROM gym_defenders
602
						WHERE owner_name = '".$trainer_name."' AND fort_id IS NOT NULL
603
						ORDER BY deployment_time";
604
		$result = $this->mysqli->query($req);
605
		$pokemon = array();
606
		while ($data = $result->fetch_object()) {
607
			$pokemon[] = $data;
608
		}
609
		return $pokemon;
610
	}
611
612
	public function getInactivePokemon($trainer_name) {
613
		$req = "SELECT pokemon_id, cp, atk_iv AS iv_attack, sta_iv AS iv_stamina, def_iv AS iv_defense, NULL AS deployment_time, '0' AS active, fort_id as gym_id, FLOOR((UNIX_TIMESTAMP() - created) / 86400) AS last_scanned
614
					FROM gym_defenders
615
					WHERE owner_name = '".$trainer_name."' AND fort_id IS NULL
616
					ORDER BY last_scanned";
617
		$result = $this->mysqli->query($req);
618
		$pokemon = array();
619
		while ($data = $result->fetch_object()) {
620
			$pokemon[] = $data;
621
		}
622
		return $pokemon;
623
	}
624
625 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...
626
		$exclue = "";
627
		if (!empty(self::$config->system->trainer_blacklist)) {
628
			$exclue .= " AND owner_name NOT IN ('".implode("','", self::$config->system->trainer_blacklist)."')";
629
		}
630
		$req = "SELECT COUNT(*) AS count, level FROM (SELECT MAX(owner_level) as level FROM gym_defenders WHERE owner_level IS NOT NULL AND team = '".$team_id."' ".$exclue." GROUP BY owner_level, owner_name) x GROUP BY level";
631
		$result = $this->mysqli->query($req);
632
		$levelData = array();
633
		while ($data = $result->fetch_object()) {
634
			$levelData[$data->level] = $data->count;
635
		}
636
		for ($i = 5; $i <= 40; $i++) {
637
			if (!isset($levelData[$i])) {
638
				$levelData[$i] = 0;
639
			}
640
		}
641
		# sort array again
642
		ksort($levelData);
643
		return $levelData;
644
	}
645
646
647
	/////////
648
	// Cron
649
	/////////
650
651
	public function getPokemonCountsActive() {
652
		$req = "SELECT pokemon_id, COUNT(*) as total FROM sightings WHERE expire_timestamp >= EXTRACT(EPOCH FROM NOW()) GROUP BY pokemon_id";
653
		$result = pg_query($this->db, $req);
654
		$counts = array();
655
		while ($data = pg_fetch_object($result)) {
656
			$counts[$data->pokemon_id] = $data->total;
657
		}
658
		return $counts;
659
	}
660
661
	public function getPokemonCountsLastDay() {
662
		$req = "SELECT pokemon_id, COUNT(*) AS spawns_last_day
663
					FROM sightings
664
					WHERE expire_timestamp >= (SELECT MAX(expire_timestamp) - 86400 FROM sightings)
665
					GROUP BY pokemon_id
666
				  	ORDER BY pokemon_id ASC";
667
		$result = pg_query($this->db, $req);
668
		$counts = array();
669
		while ($data = pg_fetch_object($result)) {
670
			$counts[$data->pokemon_id] = $data->spawns_last_day;
671
		}
672
		return $counts;
673
	}
674
675
676 View Code Duplication
	public function getCaptchaCount() {
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...
677
		$req = " SELECT COUNT(*) as total FROM accounts WHERE captchaed IS NOT NULL AND reason IS NULL";
678
		$result = pg_query($this->db, $req);
679
		$data = pg_fetch_object($result);
680
		return $data;
681
	}
682
683
	public function getNestData() {
684
		$pokemon_exclude_sql = "";
685
		if (!empty(self::$config->system->nest_exclude_pokemon)) {
686
			$pokemon_exclude_sql = "AND p.pokemon_id NOT IN (" . implode(",", self::$config->system->nest_exclude_pokemon) . ")";
687
		}
688
		$req = "SELECT p.spawn_id, p.pokemon_id, MAX(p.lat) AS latitude, MAX(p.lon) AS longitude, count(p.pokemon_id) AS total_pokemon, MAX(s.updated) as latest_seen, coalesce(CASE WHEN MAX(duration) = 0 THEN NULL ELSE MAX(duration) END ,30)*60 as duration
689
			          FROM sightings p
690
			          INNER JOIN spawnpoints s ON (p.spawn_id = s.spawn_id)
691
			          WHERE p.expire_timestamp > EXTRACT(EPOCH FROM NOW()) - 86400
692
			          " . $pokemon_exclude_sql . "
693
			          GROUP BY p.spawn_id, p.pokemon_id
694
			          HAVING COUNT(p.pokemon_id) >= 6
695
			          ORDER BY p.pokemon_id";
696
		$result = pg_query($this->db, $req);
697
		$nests = array();
698
		while ($data = pg_fetch_object($result)) {
699
			$nests[] = $data;
700
		}
701
		return $nests;
702
	}
703
704
}
705