Failed Conditions
Pull Request — master (#353)
by Florian
04:36 queued 02:02
created

getNestData()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 4
nop 0
dl 0
loc 20
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 FROM fort_sightings WHERE team = '$team_id'";
105
		$result = pg_query($this->db, $req);
106
		$data = pg_fetch_object($result);
107
		return $data;
108
	}
109
110
	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...
111
		$req = "SELECT DISTINCT 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,
112
              lat AS latitude, lon AS longitude, cp, atk_iv AS individual_attack, def_iv AS individual_defense, sta_iv AS individual_stamina
113
              FROM sightings
114
              ORDER BY last_modified DESC
115
              LIMIT 12 OFFSET 0";
116
		$result = pg_query($this->db, $req);
117
		$data = array();
118
		if ($result->num_rows > 0) {
119
			while ($row = pg_fetch_object($result)) {
120
				$data[] = $row;
121
			}
122
		}
123
		return $data;
124
	}
125
126
	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...
127
		$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,
128
                lat AS latitude, lon AS longitude, cp, atk_iv AS individual_attack, def_iv AS individual_defense, sta_iv AS individual_stamina
129
                FROM sightings
130
                WHERE pokemon_id IN (".implode(",", $mythic_pokemon).")
131
                ORDER BY updated DESC
132
                LIMIT 12 OFFSET 0";
133
		$result = pg_query($this->db, $req);
134
		$data = array();
135
		if ($result->num_rows > 0) {
136
			while ($row = pg_fetch_object($result)) {
137
				$data[] = $row;
138
			}
139
		}
140
		return $data;
141
	}
142
143
	///////////////////
144
	// Single Pokemon
145
	///////////////////
146
147 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...
148
		$req = "SELECT COUNT(DISTINCT(fort_id)) AS total FROM fort_sightings 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." ORDER BY expire_timestamp DESC 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."' ORDER BY disappear_time 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
					ORDER BY disappear_time DESC
257
					LIMIT 5000";
258
		$result = pg_query($this->db, $req);
259
		$spawns = array();
260
		while ($data = pg_fetch_object($result)) {
261
			$spawns[] = $data;
262
		}
263
		return $spawns;
264
	}
265
266 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...
267
		$req = "SELECT TO_TIMESTAMP(MIN(expire_timestamp)) AS min, TO_TIMESTAMP(MAX(expire_timestamp)) AS max FROM sightings";
268
		$result = pg_query($this->db, $req);
269
		$data = pg_fetch_object($result);
270
		return $data;
271
	}
272
273 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...
274
		$req = "SELECT MAX(lat) AS max_latitude, MIN(lat) AS min_latitude, MAX(lon) AS max_longitude, MIN(lon) as min_longitude FROM spawnpoints";
275
		$result = pg_query($this->db, $req);
276
		$data = pg_fetch_object($result);
277
		return $data;
278
	}
279
280
281
	///////////////
282
	// Pokestops
283
	//////////////
284
285
286 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...
287
		$req = "SELECT COUNT(*) as total FROM pokestops";
288
		$result = pg_query($this->db, $req);
289
		$data = pg_fetch_object($result);
290
		return $data;
291
	}
292
293
	public function getAllPokestops() {
294
		$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";
295
		$result = pg_query($this->db, $req);
296
		$pokestops = array();
297
		while ($data = pg_fetch_object($result)) {
298
			$pokestops[] = $data;
299
		}
300
		return $pokestops;
301
	}
302
303
304
	/////////
305
	// Gyms
306
	/////////
307
308
	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...
309
		$req = "SELECT COUNT(*) AS total, guard_pokemon_id FROM fort_sightings WHERE team = '".$team_id."' GROUP BY guard_pokemon_id ORDER BY total DESC LIMIT 3 OFFSET 0";
310
		$result = pg_query($this->db, $req);
311
312
		$datas = array();
313
		while ($data = pg_fetch_object($result)) {
314
			$datas[] = $data;
315
		}
316
317
		return $datas;
318
	}
319
320 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...
321
		$req = "SELECT COUNT(DISTINCT(fs.fort_id)) AS total, ROUND((SUM(gd.cp)) / COUNT(DISTINCT(fs.fort_id)),0) AS average_points
322
        			FROM fort_sightings fs
323
        			JOIN gym_defenders gd ON fs.fort_id = gd.fort_id
324
        			WHERE fs.team = '" . $team_id . "'";
325
		$result = pg_query($this->db, $req);
326
		$data = pg_fetch_object($result);
327
		return $data;
328
	}
329
330
	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...
331
		$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 FROM forts f LEFT JOIN fort_sightings fs ON f.id = fs.fort_id;";
332
		$result = pg_query($this->db, $req);
333
		$gyms = array();
334
		while ($data = pg_fetch_object($result)) {
335
			$gyms[] = $data;
336
		}
337
		return $gyms;
338
	}
339
340 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...
341
		$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, SUM(gd.cp) as total_cp	
342
			FROM fort_sightings fs
343
			LEFT JOIN forts f ON f.id = fs.fort_id
344
			LEFT JOIN gym_defenders gd ON f.id = gd.fort_id
345
			WHERE f.id ='".$gym_id."'
346
			GROUP BY f.name, f.url, fs.team, fs.updated, fs.guard_pokemon_id, fs.slots_available, gd.cp";
347
		$result = pg_query($this->db, $req);
348
		$data = pg_fetch_object($result);
349
		return $data;
350
	}
351
352
	public function getGymDefenders($gym_id) {
353
		$req = "SELECT DISTINCT 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
354
			FROM gym_defenders 
355
			WHERE fort_id='".$gym_id."'
356
			ORDER BY cp DESC";
357
		$result = pg_query($this->db, $req);
358
		$defenders = array();
359
		while ($data = pg_fetch_object($result)) {
360
			$defenders[] = $data;
361
		}
362
		return $defenders;
363
	}
364
365
366
367
	////////////////
368
	// Gym History
369
	////////////////
370
371
	public function getGymHistories($gym_name, $team, $page, $ranking)
372
	{
373
		// TODO: Implement
374
		return array();
375
	}
376
377
	public function getGymHistoriesPokemon($gym_id)
378
	{
379
		// TODO: Implement
380
		return array();
381
	}
382
383
	public function getHistoryForGym($page, $gym_id)
384
	{
385
		// TODO: Implement
386
		return array();
387
	}
388
389
	public function getHistoryForGymPokemon($pkm_uids)
390
	{
391
		// TODO: Implement
392
		return array();
393
	}
394
395
	///////////
396
	// Raids
397
	///////////
398
399 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...
400
		$limit = " LIMIT 10 OFFSET ". ($page * 10);
401
		$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 
402
					FROM raids r 
403
					JOIN forts f ON f.id = r.fort_id 
404
					LEFT JOIN fort_sightings fs ON fs.fort_id = r.fort_id 
405
					WHERE r.time_end > EXTRACT(EPOCH FROM NOW()) 
406
					ORDER BY r.level DESC, r.time_battle" . $limit;
407
		$result = pg_query($this->db, $req);
408
		$raids = array();
409
		while ($data = pg_fetch_object($result)) {
410
			$raids[] = $data;
411
		}
412
		return $raids;
413
	}
414
415
416
	//////////////
417
	// Trainers
418
	//////////////
419
420
	public function getTrainers($trainer_name, $team, $page, $ranking) {
421
		return array(); // Waiting for Monocle to store level
422
	}
423
424 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...
425
		$levelData = array();
426
		for ($i = 5; $i <= 40; $i++) {
427
			if (!isset($levelData[$i])) {
428
				$levelData[$i] = 0;
429
			}
430
		}
431
		return $levelData; // Waiting for Monocle to store level
432
	}
433
434
435
	/////////
436
	// Cron
437
	/////////
438
439
	public function getPokemonCountsActive() {
440
		$req = "SELECT pokemon_id, COUNT(*) as total FROM sightings WHERE expire_timestamp >= EXTRACT(EPOCH FROM NOW()) GROUP BY pokemon_id";
441
		$result = pg_query($this->db, $req);
442
		$counts = array();
443
		while ($data = pg_fetch_object($result)) {
444
			$counts[$data->pokemon_id] = $data->total;
445
		}
446
		return $counts;
447
	}
448
449
	public function getPokemonCountsLastDay() {
450
		$req = "SELECT pokemon_id, COUNT(*) AS spawns_last_day
451
					FROM sightings
452
					WHERE expire_timestamp >= (SELECT MAX(expire_timestamp) - 86400 FROM sightings)
453
					GROUP BY pokemon_id
454
				  	ORDER BY pokemon_id ASC";
455
		$result = pg_query($this->db, $req);
456
		$counts = array();
457
		while ($data = pg_fetch_object($result)) {
458
			$counts[$data->pokemon_id] = $data->spawns_last_day;
459
		}
460
		return $counts;
461
	}
462
463 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...
464
		$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."'";
465
		$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
466
					FROM sightings p
467
					LEFT JOIN spawnpoints s ON p.spawn_id = s.spawn_id
468
				  	JOIN (SELECT COUNT(*) AS count
469
						FROM FROM sightings p
470
						LEFT JOIN spawnpoints s ON p.spawn_id = s.spawn_id
471
                    	" . $where. "
472
                    ) count ON 1 = 1
473
					" . $where . "
474
					ORDER BY last_timestamp DESC
475
					LIMIT 1 OFFSET 0";
476
		$result = pg_query($this->db, $req);
477
		$data = pg_fetch_object($result);
478
		return $data;
479
	}
480
481 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...
482
		$where = "WHERE pokemon_id = '".$pokemon_id."' AND time_battle > '".$last_update."'";
483
		$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
484
					FROM raids r
485
					JOIN forts g ON r.fort_id = g.id
486
					JOIN (SELECT COUNT(*) AS count
487
						FROM raids
488
                    	" . $where."
489
                    ) count ON 1 = 1 
490
	                " . $where."
491
	                ORDER BY time_battle DESC
492
					LIMIT 1 OFFSET 0";
493
		$result = pg_query($this->db, $req);
494
		$data = pg_fetch_object($result);
495
		return $data;
496
	}
497
498 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...
499
		$req = " SELECT COUNT(*) as total FROM accounts WHERE captchaed IS NOT NULL AND reason IS NULL";
500
		$result = pg_query($this->db, $req);
501
		$data = pg_fetch_object($result);
502
		return $data;
503
	}
504
505
	public function getNestData() {
506
		$pokemon_exclude_sql = "";
507
		if (!empty(self::$config->system->nest_exclude_pokemon)) {
508
			$pokemon_exclude_sql = "AND p.pokemon_id NOT IN (" . implode(",", self::$config->system->nest_exclude_pokemon) . ")";
509
		}
510
		$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
511
			          FROM sightings p
512
			          INNER JOIN spawnpoints s ON (p.spawn_id = s.spawn_id)
513
			          WHERE p.expire_timestamp > EXTRACT(EPOCH FROM NOW()) - 86400
514
			          " . $pokemon_exclude_sql . "
515
			          GROUP BY p.spawn_id, p.pokemon_id
516
			          HAVING COUNT(p.pokemon_id) >= 6
517
			          ORDER BY p.pokemon_id";
518
		$result = pg_query($this->db, $req);
519
		$nests = array();
520
		while ($data = pg_fetch_object($result)) {
521
			$nests[] = $data;
522
		}
523
		return $nests;
524
	}
525
526
}
527