Failed Conditions
Pull Request — master (#363)
by Florian
08:33 queued 05:40
created

getGymData()   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
 * 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." ORDER BY expire_timestamp DESC 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."' ORDER BY disappear_time 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
					ORDER BY disappear_time DESC
263
					LIMIT 5000";
264
		$result = pg_query($this->db, $req);
265
		$spawns = array();
266
		while ($data = pg_fetch_object($result)) {
267
			$spawns[] = $data;
268
		}
269
		return $spawns;
270
	}
271
272 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...
273
		$req = "SELECT TO_TIMESTAMP(MIN(expire_timestamp)) AS min, TO_TIMESTAMP(MAX(expire_timestamp)) AS max FROM sightings";
274
		$result = pg_query($this->db, $req);
275
		$data = pg_fetch_object($result);
276
		return $data;
277
	}
278
279 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...
280
		$req = "SELECT MAX(lat) AS max_latitude, MIN(lat) AS min_latitude, MAX(lon) AS max_longitude, MIN(lon) as min_longitude FROM spawnpoints";
281
		$result = pg_query($this->db, $req);
282
		$data = pg_fetch_object($result);
283
		return $data;
284
	}
285
286
287
	///////////////
288
	// Pokestops
289
	//////////////
290
291
292 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...
293
		$req = "SELECT COUNT(*) as total FROM pokestops";
294
		$result = pg_query($this->db, $req);
295
		$data = pg_fetch_object($result);
296
		return $data;
297
	}
298
299
	public function getAllPokestops() {
300
		$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";
301
		$result = pg_query($this->db, $req);
302
		$pokestops = array();
303
		while ($data = pg_fetch_object($result)) {
304
			$pokestops[] = $data;
305
		}
306
		return $pokestops;
307
	}
308
309
310
	/////////
311
	// Gyms
312
	/////////
313
314
	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...
315
		$req = "SELECT COUNT(*) AS total, guard_pokemon_id 
316
					FROM forts f
317
					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))
318
					WHERE team = '".$team_id."' GROUP BY guard_pokemon_id ORDER BY total DESC LIMIT 3 OFFSET 0";
319
		$result = pg_query($this->db, $req);
320
321
		$datas = array();
322
		while ($data = pg_fetch_object($result)) {
323
			$datas[] = $data;
324
		}
325
326
		return $datas;
327
	}
328
329 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...
330
		$req = "SELECT COUNT(f.id) AS total, ROUND(AVG(fs.total_cp))AS average_points
331
        			FROM forts f
332
					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))
333
        			WHERE fs.team = '" . $team_id . "'";
334
		$result = pg_query($this->db, $req);
335
		$data = pg_fetch_object($result);
336
		return $data;
337
	}
338
339
	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...
340
		$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 
341
					FROM forts f
342
					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))";
343
		$result = pg_query($this->db, $req);
344
		$gyms = array();
345
		while ($data = pg_fetch_object($result)) {
346
			$gyms[] = $data;
347
		}
348
		return $gyms;
349
	}
350
351 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...
352
		$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	
353
			FROM forts f
354
			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))
355
			WHERE f.id ='".$gym_id."'
356
			GROUP BY f.name, f.url, fs.team, fs.updated, fs.guard_pokemon_id, fs.slots_available, gd.cp";
357
		$result = pg_query($this->db, $req);
358
		$data = pg_fetch_object($result);
359
		return $data;
360
	}
361
362
	public function getGymDefenders($gym_id) {
363
		$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
364
			FROM gym_defenders 
365
			WHERE fort_id='".$gym_id."'
366
			ORDER BY deployment_time";
367
		$result = pg_query($this->db, $req);
368
		$defenders = array();
369
		while ($data = pg_fetch_object($result)) {
370
			$defenders[] = $data;
371
		}
372
		return $defenders;
373
	}
374
375
376
377
	////////////////
378
	// Gym History
379
	////////////////
380
381 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...
382
	{
383
		$where = "";
384
		if (isset($gym_name) && $gym_name != '') {
385
			$where = " WHERE name LIKE '%".$gym_name."%'";
386
		}
387
		if (isset($team) && $team != '') {
388
			$where .= ($where === "" ? " WHERE" : " AND")." fs.team = ".$team;
389
		}
390
		switch ($ranking) {
391
			case 1:
392
				$order = " ORDER BY name, last_modified DESC";
393
				break;
394
			case 2:
395
				$order = " ORDER BY total_cp DESC, last_modified DESC";
396
				break;
397
			default:
398
				$order = " ORDER BY last_modified DESC, name";
399
		}
400
401
		$limit = " LIMIT 10 OFFSET ".($page * 10);
402
403
		$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 
404
			FROM forts f
405
			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))
406
			".$where.$order.$limit;
407
408
		$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...
409
		$gym_history = array();
410
		while ($data = $result->fetch_object()) {
411
			$gym_history[] = $data;
412
		}
413
		return $gym_history;
414
	}
415
416
	public function getGymHistoriesPokemon($gym_id)
417
	{
418
		$req = "SELECT external_id AS pokemon_uid, pokemon_id, cp_now as cp, owner_name AS trainer_name
419
					FROM gym_defenders
420
					WHERE fort_id = '". $gym_id ."'
421
					ORDER BY deployment_time";
422
		$result = $this->mysqli->query($req);
423
		$pokemons = array();
424
		while ($data = $result->fetch_object()) {
425
			$pokemons[] = $data;
426
		}
427
		return $pokemons;
428
	}
429
430 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...
431
	{
432
		if (isset(self::$config->system->gymhistory_hide_cp_changes) && self::$config->system->gymhistory_hide_cp_changes === true) {
433
			$pageSize = 25;
434
		} else {
435
			$pageSize = 10;
436
		}
437
		$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
438
					FROM fort_sightings fs
439
					LEFT JOIN forts f ON f.id = fs.fort_id
440
					WHERE f.id = '". $gym_id ."'
441
					ORDER BY fs.last_modified DESC
442
					LIMIT ".($pageSize+1)." OFFSET ".($page * $pageSize);
443
		$result = $this->mysqli->query($req);
444
		$history = array();
445
		$count = 0;
446
		while ($data = $result->fetch_object()) {
447
			$count++;
448
			if ($data->total_cp == 0) {
449
				$data->pokemon = array();
450
				$data->pokemon_count = 0;
451
				$data->pokemon_uids = "";
452
			} else {
453
				$data->pokemon = $this->getHistoryForGymPokemon($gym_id, $data->last_modified_real);
454
				$data->pokemon_count = count($data->pokemon);
455
				$data->pokemon_uids = implode(",", array_keys($data->pokemon));
456
			}
457
			if ($data->total_cp === 0 || $data->pokemon_count !== 0) {
458
				$history[] = $data;
459
			}
460
		}
461
		if ($count !== ($pageSize + 1)) {
462
			$last_page = true;
463
		} else {
464
			$last_page = false;
465
		}
466
		return array("last_page" => $last_page, "data" => $history);
467
	}
468
469 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...
470
	{
471
		$req = "SELECT ghd.defender_id, gd.pokemon_id, ghd.cp, gd.owner_name as trainer_name
472
					FROM gym_history_defenders ghd
473
					JOIN gym_defenders gd ON ghd.defender_id = gd.external_id
474
					WHERE ghd.fort_id = '". $gym_id ."' AND date = '".$last_modified."'
475
					ORDER BY gd.deployment_time";
476
		$result = $this->mysqli->query($req);
477
		$pokemons = array();
478
		while ($data = $result->fetch_object()) {
479
			$pokemons[$data->defender_id] = $data;
480
		}
481
		return $pokemons;
482
	}
483
484
	///////////
485
	// Raids
486
	///////////
487
488 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...
489
		$limit = " LIMIT 10 OFFSET ". ($page * 10);
490
		$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 
491
					FROM forts f
492
					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))
493
				 	LEFT JOIN raids r ON (r.fort_id = f.id AND r.time_end >= UNIX_TIMESTAMP()) 
494
					WHERE r.time_end > EXTRACT(EPOCH FROM NOW()) 
495
					ORDER BY r.level DESC, r.time_battle" . $limit;
496
		$result = pg_query($this->db, $req);
497
		$raids = array();
498
		while ($data = pg_fetch_object($result)) {
499
			$raids[] = $data;
500
		}
501
		return $raids;
502
	}
503
504
505
	//////////////
506
	// Trainers
507
	//////////////
508
509 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...
510
		$ranking = $this->getTrainerLevelRanking();
511
		$where = "";
512
		if (!empty(self::$config->system->trainer_blacklist)) {
513
			$where .= " AND gd.owner_name NOT IN ('".implode("','", self::$config->system->trainer_blacklist)."')";
514
		}
515
		if ($trainer_name != "") {
516
			$where = " AND gd.owner_name LIKE '%".$trainer_name."%'";
517
		}
518
		if ($team != 0) {
519
			$where .= ($where == "" ? " HAVING" : " AND")." team = ".$team;
520
		}
521
		switch ($rankingNumber) {
522
			case 1:
523
				$order = " ORDER BY active DESC, level DESC";
524
				break;
525
			case 2:
526
				$order = " ORDER BY maxCp DESC, level DESC";
527
				break;
528
			default:
529
				$order = " ORDER BY level DESC, active DESC";
530
		}
531
		$order .= ", last_seen DESC, name ";
532
		$limit = " LIMIT 10 OFFSET ".($page * 10);
533
		$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
534
				  	FROM gym_defenders gd
535
				  	LEFT JOIN (
536
				  		SELECT owner_name, COUNT(*) as active
537
				  		FROM gym_defenders gd2
538
						WHERE fort_id IS NOT NULL
539
				  		GROUP BY owner_name
540
				  	) active ON active.owner_name = gd.owner_name
541
				  	WHERE level IS NOT NULL " . $where . "
542
				  	GROUP BY gd.owner_name" . $order  . $limit;
543
		$result = $this->mysqli->query($req);
544
		$trainers = array();
545
		while ($data = $result->fetch_object()) {
546
			$data->last_seen = date("Y-m-d", strtotime($data->last_seen));
547
			if (is_null($data->active)) {
548
				$data->active = 0;
549
			}
550
			$trainers[$data->name] = $data;
551
552
			$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...
553
554
			$trainers[$data->name]->gyms = $data->active;
555
			$trainers[$data->name]->pokemons = $pokemon;
556
			$trainers[$data->name]->rank = $ranking[$data->level];
557
		}
558
		return $trainers;
559
	}
560
561 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...
562
		$exclue = "";
563
		if (!empty(self::$config->system->trainer_blacklist)) {
564
			$exclue .= " AND owner_name NOT IN ('".implode("','", self::$config->system->trainer_blacklist)."')";
565
		}
566
		$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";
567
		$result = $this->mysqli->query($req);
568
		$levelData = array();
569
		while ($data = $result->fetch_object()) {
570
			$levelData[$data->level] = $data->count;
571
		}
572
		for ($i = 5; $i <= 40; $i++) {
573
			if (!isset($levelData[$i])) {
574
				$levelData[$i] = 0;
575
			}
576
		}
577
		# sort array again
578
		ksort($levelData);
579
		return $levelData;
580
	}
581
582
	public function getActivePokemon($trainer_name) {
583
		$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
584
						FROM gym_defenders 
585
						WHERE owner_name = '".$trainer_name."' AND fort_id IS NOT NULL
586
						ORDER BY deployment_time";
587
		$result = $this->mysqli->query($req);
588
		$pokemon = array();
589
		while ($data = $result->fetch_object()) {
590
			$pokemon[] = $data;
591
		}
592
		return $pokemon;
593
	}
594
595
	public function getInactivePokemon($trainer_name) {
596
		$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
597
					FROM gym_defenders 
598
					WHERE owner_name = '".$trainer_name."' AND fort_id IS NULL
599
					ORDER BY last_scanned";
600
		$result = $this->mysqli->query($req);
601
		$pokemon = array();
602
		while ($data = $result->fetch_object()) {
603
			$pokemon[] = $data;
604
		}
605
		return $pokemon;
606
	}
607
608 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...
609
		$exclue = "";
610
		if (!empty(self::$config->system->trainer_blacklist)) {
611
			$exclue .= " AND owner_name NOT IN ('".implode("','", self::$config->system->trainer_blacklist)."')";
612
		}
613
		$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";
614
		$result = $this->mysqli->query($req);
615
		$levelData = array();
616
		while ($data = $result->fetch_object()) {
617
			$levelData[$data->level] = $data->count;
618
		}
619
		for ($i = 5; $i <= 40; $i++) {
620
			if (!isset($levelData[$i])) {
621
				$levelData[$i] = 0;
622
			}
623
		}
624
		# sort array again
625
		ksort($levelData);
626
		return $levelData;
627
	}
628
629
630
	/////////
631
	// Cron
632
	/////////
633
634
	public function getPokemonCountsActive() {
635
		$req = "SELECT pokemon_id, COUNT(*) as total FROM sightings WHERE expire_timestamp >= EXTRACT(EPOCH FROM NOW()) GROUP BY pokemon_id";
636
		$result = pg_query($this->db, $req);
637
		$counts = array();
638
		while ($data = pg_fetch_object($result)) {
639
			$counts[$data->pokemon_id] = $data->total;
640
		}
641
		return $counts;
642
	}
643
644
	public function getPokemonCountsLastDay() {
645
		$req = "SELECT pokemon_id, COUNT(*) AS spawns_last_day
646
					FROM sightings
647
					WHERE expire_timestamp >= (SELECT MAX(expire_timestamp) - 86400 FROM sightings)
648
					GROUP BY pokemon_id
649
				  	ORDER BY pokemon_id ASC";
650
		$result = pg_query($this->db, $req);
651
		$counts = array();
652
		while ($data = pg_fetch_object($result)) {
653
			$counts[$data->pokemon_id] = $data->spawns_last_day;
654
		}
655
		return $counts;
656
	}
657
658 View Code Duplication
	public function getPokemonSinceLastUpdate($pokemon_id, $last_update) {
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...
659
		$where = "WHERE p.pokemon_id = '".$pokemon_id."' AND p.expire_timestamp - (coalesce(CASE WHEN duration = 0 THEN NULL ELSE duration END ,30)*60) > '".$last_update."'";
660
		$req = "SELECT count, p.expire_timestamp - (coalesce(CASE WHEN duration = 0 THEN NULL ELSE duration END ,30)*60) AS last_timestamp, (TO_TIMESTAMP(expire_timestamp)) AS disappear_time_real, lat as latitude, lon as longitude
661
					FROM sightings p
662
					LEFT JOIN spawnpoints s ON p.spawn_id = s.spawn_id
663
				  	JOIN (SELECT COUNT(*) AS count
664
						FROM FROM sightings p
665
						LEFT JOIN spawnpoints s ON p.spawn_id = s.spawn_id
666
                    	" . $where. "
667
                    ) count ON 1 = 1
668
					" . $where . "
669
					ORDER BY last_timestamp DESC
670
					LIMIT 1 OFFSET 0";
671
		$result = pg_query($this->db, $req);
672
		$data = pg_fetch_object($result);
673
		return $data;
674
	}
675
676 View Code Duplication
	public function getRaidsSinceLastUpdate($pokemon_id, $last_update) {
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
		$where = "WHERE pokemon_id = '".$pokemon_id."' AND time_battle > '".$last_update."'";
678
		$req = "SELECT time_battle AS start_timestamp, time_end as end, (TO_TIMESTAMP(time_end)) AS end_time_real, lat as latitude, lon as longitude, count
679
					FROM raids r
680
					JOIN forts g ON r.fort_id = g.id
681
					JOIN (SELECT COUNT(*) AS count
682
						FROM raids
683
                    	" . $where."
684
                    ) count ON 1 = 1 
685
	                " . $where."
686
	                ORDER BY time_battle DESC
687
					LIMIT 1 OFFSET 0";
688
		$result = pg_query($this->db, $req);
689
		$data = pg_fetch_object($result);
690
		return $data;
691
	}
692
693 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...
694
		$req = " SELECT COUNT(*) as total FROM accounts WHERE captchaed IS NOT NULL AND reason IS NULL";
695
		$result = pg_query($this->db, $req);
696
		$data = pg_fetch_object($result);
697
		return $data;
698
	}
699
700
	public function getNestData($time) {
701
		$pokemon_exclude_sql = "";
702
		if (!empty(self::$config->system->nest_exclude_pokemon)) {
703
			$pokemon_exclude_sql = "AND p.pokemon_id NOT IN (" . implode(",", self::$config->system->nest_exclude_pokemon) . ")";
704
		}
705
		$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
706
			          FROM sightings p
707
			          INNER JOIN spawnpoints s ON (p.spawn_id = s.spawn_id)
708
			          WHERE p.expire_timestamp > EXTRACT(EPOCH FROM NOW()) - ".($time * 3600)."
709
			          " . $pokemon_exclude_sql . "
710
			          GROUP BY p.spawn_id, p.pokemon_id
711
			          HAVING COUNT(p.pokemon_id) >= ".($time / 4)."
712
			          ORDER BY p.pokemon_id";
713
		$result = pg_query($this->db, $req);
714
		$nests = array();
715
		while ($data = pg_fetch_object($result)) {
716
			$nests[] = $data;
717
		}
718
		return $nests;
719
	}
720
721
}
722