Failed Conditions
Pull Request — master (#353)
by Florian
05:07
created

getPokemonLastSeen()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
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...
4
5
	public function __construct() {
6
		parent::__construct();
7
	}
8
9
	public function __destruct() {
10
		parent::__destruct();
11
	}
12
13
	///////////
14
	// Tester
15
	///////////
16
17 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...
18
		$req = "SELECT COUNT(*) as total FROM sightings";
19
		$result = pg_query($this->db, $req);
20
		if ($result === false) {
21
			return 1;
22
		} else {
23
			$data = pg_fetch_object($result);
24
			$total = $data->total;
25
26
			if ($total == 0) {
27
				return 2;
28
			}
29
		}
30
		return 0;
31
	}
32
33 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...
34
		$req = "SELECT COUNT(*) as total FROM forts";
35
		$result = pg_query($this->db, $req);
36
		if ($result === false) {
37
			return 1;
38
		} else {
39
			$data = pg_fetch_object($result);
40
			$total = $data->total;
41
42
			if ($total == 0) {
43
				return 2;
44
			}
45
		}
46
		return 0;
47
	}
48
49 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...
50
		$req = "SELECT COUNT(*) as total FROM pokestops";
51
		$result = pg_query($this->db, $req);
52
		if ($result === false) {
53
			return 1;
54
		} else {
55
			$data = pg_fetch_object($result);
56
			$total = $data->total;
57
58
			if ($total == 0) {
59
				return 2;
60
			}
61
		}
62
		return 0;
63
	}
64
65
66
	/////////////
67
	// Homepage
68
	/////////////
69
70 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...
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 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...
71
		$req = "SELECT COUNT(*) AS total FROM sightings WHERE expire_timestamp >= EXTRACT(EPOCH FROM NOW())";
72
		$result = pg_query($this->db, $req);
73
		$data = pg_fetch_object($result);
74
		return $data;
75
	}
76
77
	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...
78
		$data = (object) array("total" => 0);
79
		return $data;
80
	}
81
82 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...
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 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...
83
		$req = "SELECT COUNT(*) AS total FROM forts";
84
		$result = pg_query($this->db, $req);
85
		$data = pg_fetch_object($result);
86
		return $data;
87
	}
88
89 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...
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 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...
90
		$req = "SELECT COUNT(*) AS total FROM raids WHERE time_battle <= EXTRACT(EPOCH FROM NOW()) AND time_end >= EXTRACT(EPOCH FROM NOW())";
91
		$result = pg_query($this->db, $req);
92
		$data = pg_fetch_object($result);
93
		return $data;
94
	}
95
96
97 View Code Duplication
	function getTotalGymsForTeam($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...
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...
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...
98
		$req = "SELECT COUNT(*) AS total
99
					FROM forts f
100
					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))
101
					WHERE team = '$team_id'";
102
		$result = pg_query($this->db, $req);
103
		$data = pg_fetch_object($result);
104
		return $data;
105
	}
106
107
	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...
108
		$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,
109
              lat AS latitude, lon AS longitude, cp, atk_iv AS individual_attack, def_iv AS individual_defense, sta_iv AS individual_stamina
110
              FROM sightings
111
              ORDER BY updated DESC
112
              LIMIT 12 OFFSET 0";
113
		$result = pg_query($this->db, $req);
114
		$data = array();
115
		if ($result->num_rows > 0) {
116
			while ($row = pg_fetch_object($result)) {
117
				$data[] = $row;
118
			}
119
		}
120
		return $data;
121
	}
122
123
	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...
124
		$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,
125
                lat AS latitude, lon AS longitude, cp, atk_iv AS individual_attack, def_iv AS individual_defense, sta_iv AS individual_stamina
126
                FROM sightings
127
                WHERE pokemon_id IN (".implode(",", $mythic_pokemon).")
128
                ORDER BY updated DESC
129
                LIMIT 12 OFFSET 0";
130
		$result = pg_query($this->db, $req);
131
		$data = array();
132
		if ($result->num_rows > 0) {
133
			while ($row = pg_fetch_object($result)) {
134
				$data[] = $row;
135
			}
136
		}
137
		return $data;
138
	}
139
140
	///////////////////
141
	// Single Pokemon
142
	///////////////////
143
144 View Code Duplication
	function getGymsProtectedByPokemon($pokemon_id) {
0 ignored issues
show
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...
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...
145
		$req = "SELECT COUNT(f.id) AS total
146
					FROM forts f
147
					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))
148
					WHERE guard_pokemon_id = '".$pokemon_id."'";
149
		$result = pg_query($this->db, $req);
150
		$data = pg_fetch_object($result);
151
		return $data;
152
	}
153
154 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...
155
		$req = "SELECT TO_TIMESTAMP(expire_timestamp) AS expire_timestamp, TO_TIMESTAMP(expire_timestamp) AS disappear_time_real, lat AS latitude, lon AS longitude
156
                FROM sightings
157
                WHERE pokemon_id = '".$pokemon_id."'
158
                ORDER BY expire_timestamp DESC
159
                LIMIT 1 OFFSET 0";
160
		$result = pg_query($this->db, $req);
161
		$data = pg_fetch_object($result);
162
		return $data;
163
	}
164
165 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...
166
		$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,
167
                cp, atk_iv as individual_attack, def_iv as individual_defense, sta_iv as individual_stamina,
168
                ROUND(100*(atk_iv+def_iv+sta_iv)/45,1) AS \"IV\", move_1 as move_1, move_2, form
169
                FROM sightings
170
	            WHERE pokemon_id = '" . $pokemon_id . "' AND move_1 IS NOT NULL AND move_1 <> '0'
171
	            ORDER BY $top_order_by $top_direction, expire_timestamp DESC
172
	            LIMIT 50 OFFSET 0";
173
		$result = pg_query($this->db, $req);
174
		$top = array();
175
		while ($data = pg_fetch_object($result)) {
176
			$top[] = $data;
177
		}
178
		return $top;
179
	}
180
181
	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...
182
		$trainer_blacklist = "";
183
		if (!empty(self::$config->system->trainer_blacklist)) {
184
			$trainer_blacklist = " AND owner_name NOT IN ('" . implode("','", self::$config->system->trainer_blacklist) . "')";
185
		}
186
187
		$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,
188
                TO_TIMESTAMP(last_modified) AS lasttime, last_modified as last_seen
189
                FROM gym_defenders
190
				WHERE pokemon_id = '" . $pokemon_id . "'" . $trainer_blacklist . "
191
				ORDER BY $best_order_by $best_direction, owner_name ASC
192
				LIMIT 50 OFFSET 0";
193
194
		$result = pg_query($this->db, $req);
195
		$toptrainer = array();
196
		while ($data = pg_fetch_object($result)) {
197
			$toptrainer[] = $data;
198
		}
199
		return $toptrainer;
200
	}
201
202
	public function getPokemonHeatmap($pokemon_id, $start, $end) {
203
		$where = " WHERE pokemon_id = ".$pokemon_id." "
204
			. "AND TO_TIMESTAMP(expire_timestamp) BETWEEN '".$start."' AND '".$end."'";
205
		$req 		= "SELECT lat AS latitude, lon AS longitude FROM sightings".$where." LIMIT 100000";
206
		$result = pg_query($this->db, $req);
207
		$points = array();
208
		while ($data = pg_fetch_object($result)) {
209
			$points[] = $data;
210
		}
211
		return $points;
212
	}
213
214
	public function getPokemonGraph($pokemon_id) {
215
		$req = "SELECT COUNT(*) AS total, EXTRACT(HOUR FROM disappear_time) AS disappear_hour
216
					FROM (SELECT TO_TIMESTAMP(expire_timestamp) as disappear_time FROM sightings WHERE pokemon_id = '".$pokemon_id."' LIMIT 100000) AS pokemonFiltered
217
				GROUP BY disappear_hour
218
				ORDER BY disappear_hour";
219
		$result = pg_query($this->db, $req);
220
		$array = array_fill(0, 24, 0);
221
		while ($result && $data = pg_fetch_object($result)) {
222
			$array[$data->disappear_hour] = $data->total;
223
		}
224
		// shift array because AM/PM starts at 1AM not 0:00
225
		$array[] = $array[0];
226
		array_shift($array);
227
		return $array;
228
	}
229
230
	public function getPokemonLive($pokemon_id, $ivMin, $ivMax, $inmap_pokemons) {
231
		$inmap_pkms_filter = "";
232
		$where = " WHERE expire_timestamp >= EXTRACT(EPOCH FROM NOW()) AND pokemon_id = " . $pokemon_id;
233
234
		$reqTestIv = "SELECT MAX(atk_iv) AS iv FROM sightings " . $where;
235
		$resultTestIv = pg_query($this->db, $reqTestIv);
236
		$testIv = pg_fetch_object($resultTestIv);
237
		if (!is_null($inmap_pokemons) && ($inmap_pokemons != "")) {
238
			foreach ($inmap_pokemons as $inmap) {
239
				$inmap_pkms_filter .= "'".$inmap."',";
240
			}
241
			$inmap_pkms_filter = rtrim($inmap_pkms_filter, ",");
242
			$where .= " AND encounter_id NOT IN (" . $inmap_pkms_filter . ") ";
243
		}
244
		if ($testIv->iv != null && !is_null($ivMin) && ($ivMin != "")) {
245
			$where .= " AND ((100/45)*(atk_iv + def_iv + sta_iv)) >= (" . $ivMin . ") ";
246
		}
247
		if ($testIv->iv != null && !is_null($ivMax) && ($ivMax != "")) {
248
			$where .= " AND ((100/45)*(atk_iv + def_iv + sta_iv)) <= (" . $ivMax . ") ";
249
		}
250
		$req = "SELECT pokemon_id, lat AS latitude, lon AS longitude,
251
    					TO_TIMESTAMP(expire_timestamp) AS disappear_time,
252
    					TO_TIMESTAMP(expire_timestamp) AS disappear_time_real,
253
    					atk_iv AS individual_attack, def_iv AS individual_defense, sta_iv AS individual_stamina,
254
   						move_1, move_2
255
					FROM sightings " . $where . "
256
					LIMIT 5000";
257
		$result = pg_query($this->db, $req);
258
		$spawns = array();
259
		while ($data = pg_fetch_object($result)) {
260
			$spawns[] = $data;
261
		}
262
		return $spawns;
263
	}
264
265 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...
266
		$req = "SELECT TO_TIMESTAMP(MIN(expire_timestamp)) AS min, TO_TIMESTAMP(MAX(expire_timestamp)) AS max FROM sightings";
267
		$result = pg_query($this->db, $req);
268
		$data = pg_fetch_object($result);
269
		return $data;
270
	}
271
272 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...
273
		$req = "SELECT MAX(lat) AS max_latitude, MIN(lat) AS min_latitude, MAX(lon) AS max_longitude, MIN(lon) as min_longitude FROM spawnpoints";
274
		$result = pg_query($this->db, $req);
275
		$data = pg_fetch_object($result);
276
		return $data;
277
	}
278
279 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...
280
		$req = "SELECT count, last_seen, latitude, longitude
281
					FROM pokemon_stats
282
					WHERE pid = ".$pokemon_id;
283
		$result = pg_query($this->db, $req);
284
		$data = pg_fetch_object($result);
285
		return $data;
286
	}
287
288 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...
289
		$req = "SELECT count, last_seen, latitude, longitude
290
					FROM raid_stats
291
					WHERE pid = ".$pokemon_id;
292
		$result = pg_query($this->db, $req);
293
		$data = pg_fetch_object($result);
294
		return $data;
295
	}
296
297
298
	///////////////
299
	// Pokestops
300
	//////////////
301
302
303 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...
304
		$req = "SELECT COUNT(*) as total FROM pokestops";
305
		$result = pg_query($this->db, $req);
306
		$data = pg_fetch_object($result);
307
		return $data;
308
	}
309
310
	public function getAllPokestops() {
311
		$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";
312
		$result = pg_query($this->db, $req);
313
		$pokestops = array();
314
		while ($data = pg_fetch_object($result)) {
315
			$pokestops[] = $data;
316
		}
317
		return $pokestops;
318
	}
319
320
321
	/////////
322
	// Gyms
323
	/////////
324
325
	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...
326
		$req = "SELECT COUNT(*) AS total, guard_pokemon_id
327
					FROM forts f
328
					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))
329
					WHERE team = '".$team_id."' GROUP BY guard_pokemon_id ORDER BY total DESC LIMIT 3 OFFSET 0";
330
		$result = pg_query($this->db, $req);
331
332
		$datas = array();
333
		while ($data = pg_fetch_object($result)) {
334
			$datas[] = $data;
335
		}
336
337
		return $datas;
338
	}
339
340 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...
341
		$req = "SELECT COUNT(f.id) AS total, ROUND(AVG(fs.total_cp))AS average_points
342
        			FROM forts f
343
					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))
344
        			WHERE fs.team = '" . $team_id . "'";
345
		$result = pg_query($this->db, $req);
346
		$data = pg_fetch_object($result);
347
		return $data;
348
	}
349
350
	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...
351
		$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
352
					FROM forts f
353
					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))";
354
		$result = pg_query($this->db, $req);
355
		$gyms = array();
356
		while ($data = pg_fetch_object($result)) {
357
			$gyms[] = $data;
358
		}
359
		return $gyms;
360
	}
361
362 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...
363
		$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
364
			FROM forts f
365
			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))
366
			WHERE f.id ='".$gym_id."'
367
			GROUP BY f.name, f.url, fs.team, fs.updated, fs.guard_pokemon_id, fs.slots_available, gd.cp";
368
		$result = pg_query($this->db, $req);
369
		$data = pg_fetch_object($result);
370
		return $data;
371
	}
372
373
	public function getGymDefenders($gym_id) {
374
		$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
375
			FROM gym_defenders
376
			WHERE fort_id='".$gym_id."'
377
			ORDER BY deployment_time";
378
		$result = pg_query($this->db, $req);
379
		$defenders = array();
380
		while ($data = pg_fetch_object($result)) {
381
			$defenders[] = $data;
382
		}
383
		return $defenders;
384
	}
385
386
387
388
	////////////////
389
	// Gym History
390
	////////////////
391
392
	public function getGymHistories($gym_name, $team, $page, $ranking)
393
	{
394
		$where = "";
395
		if (isset($gym_name) && $gym_name != '') {
396
			$where = " WHERE name LIKE '%".$gym_name."%'";
397
		}
398
		if (isset($team) && $team != '') {
399
			$where .= ($where === "" ? " WHERE" : " AND")." fs.team = ".$team;
400
		}
401
		switch ($ranking) {
402
			case 1:
403
				$order = " ORDER BY name, last_modified DESC";
404
				break;
405
			case 2:
406
				$order = " ORDER BY total_cp DESC, last_modified DESC";
407
				break;
408
			default:
409
				$order = " ORDER BY last_modified DESC, name";
410
		}
411
412
		$limit = " LIMIT 10 OFFSET ".($page * 10);
413
414
		$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
415
			FROM forts f
416
			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))
417
			".$where.$order.$limit;
418
419
		$result = pg_query($this->db, $req);
420
		$gym_history = array();
421
		while ($data = pg_fetch_object($result)) {
422
			$gym_history[] = $data;
423
		}
424
		return $gym_history;
425
	}
426
427
	public function getGymHistoriesPokemon($gym_id)
428
	{
429
		$req = "SELECT external_id AS pokemon_uid, pokemon_id, cp_now as cp, owner_name AS trainer_name
430
					FROM gym_defenders
431
					WHERE fort_id = '". $gym_id ."'
432
					ORDER BY deployment_time";
433
		$result = pg_query($this->db, $req);
434
		$pokemons = array();
435
		while ($data = pg_fetch_object($result)) {
436
			$pokemons[] = $data;
437
		}
438
		return $pokemons;
439
	}
440
441
	public function getHistoryForGym($page, $gym_id)
442
	{
443 View Code Duplication
		if (isset(self::$config->system->gymhistory_hide_cp_changes) && self::$config->system->gymhistory_hide_cp_changes === true) {
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...
444
			$pageSize = 25;
445
		} else {
446
			$pageSize = 10;
447
		}
448
		$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
449
					FROM fort_sightings fs
450
					LEFT JOIN forts f ON f.id = fs.fort_id
451
					WHERE f.id = '". $gym_id ."'
452
					ORDER BY fs.last_modified DESC
453
					LIMIT ".($pageSize+1)." OFFSET ".($page * $pageSize);
454
		$result = pg_query($this->db, $req);
455
		$history = array();
456
		$count = 0;
457 View Code Duplication
		while ($data = pg_fetch_object($result)) {
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...
458
			$count++;
459
			if ($data->total_cp == 0) {
460
				$data->pokemon = array();
461
				$data->pokemon_count = 0;
462
				$data->pokemon_uids = "";
463
			} else {
464
				$data->pokemon = $this->getHistoryForGymPokemon($gym_id, $data->last_modified_real);
465
				$data->pokemon_count = count($data->pokemon);
466
				$data->pokemon_uids = implode(",", array_keys($data->pokemon));
467
			}
468
			if ($data->total_cp === 0 || $data->pokemon_count !== 0) {
469
				$history[] = $data;
470
			}
471
		}
472
		if ($count !== ($pageSize + 1)) {
473
			$last_page = true;
474
		} else {
475
			$last_page = false;
476
		}
477
		return array("last_page" => $last_page, "data" => $history);
478
	}
479
480 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...
481
	{
482
		$req = "SELECT ghd.defender_id, gd.pokemon_id, ghd.cp, gd.owner_name as trainer_name
483
					FROM gym_history_defenders ghd
484
					JOIN gym_defenders gd ON ghd.defender_id = gd.external_id
485
					WHERE ghd.fort_id = '". $gym_id ."' AND date = '".$last_modified."'
486
					ORDER BY gd.deployment_time";
487
		$result = pg_query($this->db, $req);
488
		$pokemons = array();
489
		while ($data = pg_fetch_object($result)) {
490
			$pokemons[$data->defender_id] = $data;
491
		}
492
		return $pokemons;
493
	}
494
495
	///////////
496
	// Raids
497
	///////////
498
499 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...
500
		$limit = " LIMIT 10 OFFSET ". ($page * 10);
501
		$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
502
					FROM forts f
503
					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))
504
				 	LEFT JOIN raids r ON (r.fort_id = f.id AND r.time_end >= UNIX_TIMESTAMP())
505
					WHERE r.time_end > EXTRACT(EPOCH FROM NOW())
506
					ORDER BY r.level DESC, r.time_battle" . $limit;
507
		$result = pg_query($this->db, $req);
508
		$raids = array();
509
		while ($data = pg_fetch_object($result)) {
510
			$raids[] = $data;
511
		}
512
		return $raids;
513
	}
514
515
516
	//////////////
517
	// Trainers
518
	//////////////
519
520
	public function getTrainers($trainer_name, $team, $page, $rankingNumber) {
521
		$ranking = $this->getTrainerLevelRanking();
522
		$where = "";
523
		if (!empty(self::$config->system->trainer_blacklist)) {
524
			$where .= " AND gd.owner_name NOT IN ('".implode("','", self::$config->system->trainer_blacklist)."')";
525
		}
526
		if ($trainer_name != "") {
527
			$where = " AND gd.owner_name LIKE '%".$trainer_name."%'";
528
		}
529 View Code Duplication
		if ($team != 0) {
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...
530
			$where .= ($where == "" ? " HAVING" : " AND")." team = ".$team;
531
		}
532 View Code Duplication
		switch ($rankingNumber) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
533
			case 1:
534
				$order = " ORDER BY active DESC, level DESC";
535
				break;
536
			case 2:
537
				$order = " ORDER BY maxCp DESC, level DESC";
538
				break;
539
			default:
540
				$order = " ORDER BY level DESC, active DESC";
541
		}
542
		$order .= ", last_seen DESC, name ";
543
		$limit = " LIMIT 10 OFFSET ".($page * 10);
544
		$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
545
				  	FROM gym_defenders gd
546
				  	LEFT JOIN (
547
				  		SELECT owner_name, COUNT(*) as active
548
				  		FROM gym_defenders gd2
549
						WHERE fort_id IS NOT NULL
550
				  		GROUP BY owner_name
551
				  	) active ON active.owner_name = gd.owner_name
552
				  	WHERE level IS NOT NULL " . $where . "
553
				  	GROUP BY gd.owner_name" . $order  . $limit;
554
		$result = pg_query($this->db, $req);
555
		$trainers = array();
556 View Code Duplication
		while ($data = pg_fetch_object($result)) {
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...
557
			$data->last_seen = date("Y-m-d", strtotime($data->last_seen));
558
			if (is_null($data->active)) {
559
				$data->active = 0;
560
			}
561
			$trainers[$data->name] = $data;
562
563
			$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...
564
565
			$trainers[$data->name]->gyms = $data->active;
566
			$trainers[$data->name]->pokemons = $pokemon;
567
			$trainers[$data->name]->rank = $ranking[$data->level];
568
		}
569
		return $trainers;
570
	}
571
572 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...
573
		$exclue = "";
574
		if (!empty(self::$config->system->trainer_blacklist)) {
575
			$exclue .= " AND owner_name NOT IN ('".implode("','", self::$config->system->trainer_blacklist)."')";
576
		}
577
		$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";
578
		$result = pg_query($this->db, $req);
579
		$levelData = array();
580
		while ($data = pg_fetch_object($result)) {
581
			$levelData[$data->level] = $data->count;
582
		}
583
		for ($i = 5; $i <= 40; $i++) {
584
			if (!isset($levelData[$i])) {
585
				$levelData[$i] = 0;
586
			}
587
		}
588
		# sort array again
589
		ksort($levelData);
590
		return $levelData;
591
	}
592
593
	public function getActivePokemon($trainer_name) {
594
		$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
595
						FROM gym_defenders
596
						WHERE owner_name = '".$trainer_name."' AND fort_id IS NOT NULL
597
						ORDER BY deployment_time";
598
		$result = pg_query($this->db, $req);
599
		$pokemon = array();
600
		while ($data = pg_fetch_object($result)) {
601
			$pokemon[] = $data;
602
		}
603
		return $pokemon;
604
	}
605
606
	public function getInactivePokemon($trainer_name) {
607
		$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
608
					FROM gym_defenders
609
					WHERE owner_name = '".$trainer_name."' AND fort_id IS NULL
610
					ORDER BY last_scanned";
611
		$result = pg_query($this->db, $req);
612
		$pokemon = array();
613
		while ($data = pg_fetch_object($result)) {
614
			$pokemon[] = $data;
615
		}
616
		return $pokemon;
617
	}
618
619 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...
620
		$exclue = "";
621
		if (!empty(self::$config->system->trainer_blacklist)) {
622
			$exclue .= " AND owner_name NOT IN ('".implode("','", self::$config->system->trainer_blacklist)."')";
623
		}
624
		$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";
625
		$result = pg_query($this->db, $req);
626
		$levelData = array();
627
		while ($data = pg_fetch_object($result)) {
628
			$levelData[$data->level] = $data->count;
629
		}
630
		for ($i = 5; $i <= 40; $i++) {
631
			if (!isset($levelData[$i])) {
632
				$levelData[$i] = 0;
633
			}
634
		}
635
		# sort array again
636
		ksort($levelData);
637
		return $levelData;
638
	}
639
640
641
	/////////
642
	// Cron
643
	/////////
644
645
	public function getPokemonCountsActive() {
646
		$req = "SELECT pokemon_id, COUNT(*) as total FROM sightings WHERE expire_timestamp >= EXTRACT(EPOCH FROM NOW()) GROUP BY pokemon_id";
647
		$result = pg_query($this->db, $req);
648
		$counts = array();
649
		while ($data = pg_fetch_object($result)) {
650
			$counts[$data->pokemon_id] = $data->total;
651
		}
652
		return $counts;
653
	}
654
655
	public function getPokemonCountsLastDay() {
656
		$req = "SELECT pokemon_id, COUNT(*) AS spawns_last_day
657
					FROM sightings
658
					WHERE expire_timestamp >= (SELECT MAX(expire_timestamp) - 86400 FROM sightings)
659
					GROUP BY pokemon_id
660
				  	ORDER BY pokemon_id ASC";
661
		$result = pg_query($this->db, $req);
662
		$counts = array();
663
		while ($data = pg_fetch_object($result)) {
664
			$counts[$data->pokemon_id] = $data->spawns_last_day;
665
		}
666
		return $counts;
667
	}
668
669
670 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...
671
		$req = " SELECT COUNT(*) as total FROM accounts WHERE captchaed IS NOT NULL AND reason IS NULL";
672
		$result = pg_query($this->db, $req);
673
		$data = pg_fetch_object($result);
674
		return $data;
675
	}
676
677
	public function getNestData() {
678
		$pokemon_exclude_sql = "";
679
		if (!empty(self::$config->system->nest_exclude_pokemon)) {
680
			$pokemon_exclude_sql = "AND p.pokemon_id NOT IN (" . implode(",", self::$config->system->nest_exclude_pokemon) . ")";
681
		}
682
		$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
683
			          FROM sightings p
684
			          INNER JOIN spawnpoints s ON (p.spawn_id = s.spawn_id)
685
			          WHERE p.expire_timestamp > EXTRACT(EPOCH FROM NOW()) - 86400
686
			          " . $pokemon_exclude_sql . "
687
			          GROUP BY p.spawn_id, p.pokemon_id
688
			          HAVING COUNT(p.pokemon_id) >= 6
689
			          ORDER BY p.pokemon_id";
690
		$result = pg_query($this->db, $req);
691
		$nests = array();
692
		while ($data = pg_fetch_object($result)) {
693
			$nests[] = $data;
694
		}
695
		return $nests;
696
	}
697
}
698