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

QueryManagerMysqlRocketmap::getOwnedAndPoints()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
final class QueryManagerMysqlRocketmap extends QueryManagerMysql {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

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

namespace YourVendor;

class YourClass { }

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

Loading history...
4
5
	public function __construct() {
6
		parent::__construct();
7
	}
8
9
	public function __destruct() {
10
		parent::__destruct();
11
	}
12
13
	///////////
14
	// Tester
15
	///////////
16
	
17 View Code Duplication
	function testTotalPokemon() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

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

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

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

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

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

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

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

Loading history...
18
		$req = "SELECT COUNT(*) as total FROM pokemon";
19
		$result = $this->mysqli->query($req);
20
		if (!is_object($result)) {
21
			return 1;
22
		} else {
23
			$data = $result->fetch_object();
24
			$total = $data->total;
25
	
26
			if ($total == 0) {
27
				return 2;
28
			}
29
		}
30
		return 0;
31
	}
32
	
33 View Code Duplication
	function testTotalGyms() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

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

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

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

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

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

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

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

Loading history...
34
		$req = "SELECT COUNT(*) as total FROM gym";
35
		$result = $this->mysqli->query($req);
36
		if (!is_object($result)) {
37
			return 1;
38
		} else {
39
			$data = $result->fetch_object();
40
			$total = $data->total;
41
	
42
			if ($total == 0) {
43
				return 2;
44
			}
45
		}
46
		return 0;
47
	}
48
	
49 View Code Duplication
	function testTotalPokestops() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

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

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

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

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

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

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

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

Loading history...
50
		$req = "SELECT COUNT(*) as total FROM pokestop";
51
		$result = $this->mysqli->query($req);
52
		if (!is_object($result)) {
53
			return 1;
54
		} else {
55
			$data = $result->fetch_object();
56
			$total = $data->total;
57
	
58
			if ($total == 0) {
59
				return 2;
60
			}
61
		}
62
		return 0;
63
	}
64
	
65
	
66
	/////////////
67
	// Homepage
68
	/////////////
69
	
70
	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...
71
		$req = "SELECT COUNT(*) AS total FROM pokemon WHERE disappear_time >= UTC_TIMESTAMP()";
72
		$result = $this->mysqli->query($req);
73
		$data = $result->fetch_object();
74
		return $data;
75
	}
76
	
77
	function getTotalLures() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

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

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

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

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

Loading history...
78
		$req = "SELECT COUNT(*) AS total FROM pokestop WHERE lure_expiration >= UTC_TIMESTAMP()";
79
		$result = $this->mysqli->query($req);
80
		$data = $result->fetch_object();
81
		return $data;
82
	}
83
	
84
	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...
85
		$req = "SELECT COUNT(DISTINCT(gym_id)) AS total FROM gym";
86
		$result = $this->mysqli->query($req);
87
		$data = $result->fetch_object();
88
		return $data;
89
	}
90
	
91
	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...
92
		$req = "SELECT COUNT(*) AS total FROM raid WHERE start <= UTC_TIMESTAMP AND  end >= UTC_TIMESTAMP()";
93
		$result = $this->mysqli->query($req);
94
		$data = $result->fetch_object();
95
		return $data;
96
	}
97
	
98
	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...
99
		$req = "SELECT COUNT(DISTINCT(gym_id)) AS total FROM gym WHERE team_id = '".$team_id."'";
100
		$result = $this->mysqli->query($req);
101
		$data = $result->fetch_object();
102
		return $data;
103
	}
104
	
105
	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...
106
		$req = "SELECT DISTINCT pokemon_id, encounter_id, disappear_time, last_modified, (CONVERT_TZ(disappear_time, '+00:00', '".self::$time_offset."')) AS disappear_time_real,
107
					latitude, longitude, cp, individual_attack, individual_defense, individual_stamina
108
					FROM pokemon
109
					ORDER BY last_modified DESC
110
					LIMIT 0,12";
111
		$result = $this->mysqli->query($req);
112
		$data = array();
113
		if ($result->num_rows > 0) {
114
			while ($row = $result->fetch_object()) {
115
				$data[] = $row;
116
			}
117
		}
118
		return $data;
119
	}
120
	
121
	function getRecentMythic($mythic_pokemons) {
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...
122
		$req = "SELECT DISTINCT pokemon_id, encounter_id, disappear_time, last_modified, (CONVERT_TZ(disappear_time, '+00:00', '".self::$time_offset."')) AS disappear_time_real,
123
					latitude, longitude, cp, individual_attack, individual_defense, individual_stamina
124
					FROM pokemon
125
					WHERE pokemon_id IN (".implode(",", $mythic_pokemons).")
126
					ORDER BY last_modified DESC
127
					LIMIT 0,12";
128
		$result = $this->mysqli->query($req);
129
		$data = array();
130
		if ($result->num_rows > 0) {
131
			while ($row = $result->fetch_object()) {
132
				$data[] = $row;
133
			}
134
		}
135
		return $data;
136
	}
137
	
138
	///////////////////
139
	// Single Pokemon
140
	///////////////////
141
	
142 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...
143
		$req = "SELECT COUNT(DISTINCT(gym_id)) AS total FROM gym WHERE guard_pokemon_id = '".$pokemon_id."'";
144
		$result = $this->mysqli->query($req);
145
		$data = $result->fetch_object();
146
		return $data;
147
	}
148
	
149 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...
150
		$req = "SELECT disappear_time, (CONVERT_TZ(disappear_time, '+00:00', '".self::$time_offset."')) AS disappear_time_real, latitude, longitude
151
							FROM pokemon
152
							WHERE pokemon_id = '".$pokemon_id."'
153
							ORDER BY disappear_time DESC
154
							LIMIT 0,1";
155
		$result = $this->mysqli->query($req);
156
		$data = $result->fetch_object();
157
		return $data;
158
	}
159
160 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...
161
		$req = "SELECT (CONVERT_TZ(disappear_time, '+00:00', '".self::$time_offset."')) AS distime, pokemon_id, disappear_time, latitude, longitude,
162
							cp, individual_attack, individual_defense, individual_stamina,
163
							ROUND(100*(individual_attack+individual_defense+individual_stamina)/45,1) AS IV, move_1, move_2, form
164
							FROM pokemon
165
							WHERE pokemon_id = '".$pokemon_id."' AND move_1 IS NOT NULL AND move_1 <> '0'
166
							ORDER BY $top_order_by $top_direction, disappear_time DESC
167
							LIMIT 0,50";
168
169
		$result = $this->mysqli->query($req);
170
		$top = array();
171
		while ($data = $result->fetch_object()) {
172
			$top[] = $data;
173
		}
174
		return $top;
175
	}
176
177 View Code Duplication
	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...
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 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...
178
		$trainer_blacklist = "";
179
		if (!empty(self::$config->system->trainer_blacklist)) {
180
			$trainer_blacklist = " AND trainer_name NOT IN ('".implode("','", self::$config->system->trainer_blacklist)."')";
181
		}
182
183
		$req = "SELECT trainer_name, ROUND(SUM(100*(iv_attack+iv_defense+iv_stamina)/45),1) AS IV, move_1, move_2, cp,
184
						DATE_FORMAT(last_seen, '%Y-%m-%d') AS lasttime, last_seen
185
						FROM gympokemon
186
						WHERE pokemon_id = '".$pokemon_id."'".$trainer_blacklist."
187
						GROUP BY pokemon_uid
188
						ORDER BY $best_order_by $best_direction, trainer_name ASC
189
						LIMIT 0,50";
190
191
		$result = $this->mysqli->query($req);
192
		$toptrainer = array();
193
		while ($data = $result->fetch_object()) {
194
			$toptrainer[] = $data;
195
		}
196
		return $toptrainer;
197
	}
198
199 View Code Duplication
	public function getPokemonHeatmap($pokemon_id, $start, $end) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
200
		$where = " WHERE pokemon_id = ".$pokemon_id." "
201
			. "AND disappear_time BETWEEN '".$start."' AND '".$end."'";
202
		$req 		= "SELECT latitude, longitude FROM pokemon".$where." ORDER BY disappear_time DESC LIMIT 10000";
203
		$result = $this->mysqli->query($req);
204
		$points = array();
205
		while ($data = $result->fetch_object()) {
206
			$points[] = $data;
207
		}
208
		return $points;
209
	}
210
211 View Code Duplication
	public function getPokemonGraph($pokemon_id) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
212
		$req = "SELECT COUNT(*) AS total,
213
					HOUR(CONVERT_TZ(disappear_time, '+00:00', '".self::$time_offset."')) AS disappear_hour
214
					FROM (SELECT disappear_time FROM pokemon WHERE pokemon_id = '".$pokemon_id."' ORDER BY disappear_time LIMIT 100000) AS pokemonFiltered
215
					GROUP BY disappear_hour
216
					ORDER BY disappear_hour";
217
		$result = $this->mysqli->query($req);
218
		$array = array_fill(0, 24, 0);
219
		while ($result && $data = $result->fetch_object()) {
220
			$array[$data->disappear_hour] = $data->total;
221
		}
222
		// shift array because AM/PM starts at 1AM not 0:00
223
		$array[] = $array[0];
224
		array_shift($array);
225
		return $array;
226
	}
227
228 View Code Duplication
	public function getPokemonLive($pokemon_id, $ivMin, $ivMax, $inmap_pokemons) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
Coding Style introduced by
getPokemonLive uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
229
		$inmap_pkms_filter = "";
230
		$where = " WHERE disappear_time >= UTC_TIMESTAMP() AND pokemon_id = ".$pokemon_id;
231
232
		$reqTestIv = "SELECT MAX(individual_attack) AS iv FROM pokemon ".$where;
233
		$resultTestIv = $this->mysqli->query($reqTestIv);
234
		$testIv = $resultTestIv->fetch_object();
235
		if (!is_null($inmap_pokemons) && ($inmap_pokemons != "")) {
236
			foreach ($_POST['inmap_pokemons'] as $inmap) {
237
				$inmap_pkms_filter .= "'".$inmap."',";
238
			}
239
			$inmap_pkms_filter = rtrim($inmap_pkms_filter, ",");
240
			$where .= " AND encounter_id NOT IN (".$inmap_pkms_filter.") ";
241
		}
242
		if ($testIv->iv != null && !is_null($ivMin) && ($ivMin != "")) {
243
			$where .= " AND ((100/45)*(individual_attack+individual_defense+individual_stamina)) >= (".$ivMin.") ";
244
		}
245
		if ($testIv->iv != null && !is_null($ivMax) && ($ivMax != "")) {
246
			$where .= " AND ((100/45)*(individual_attack+individual_defense+individual_stamina)) <= (".$ivMax.") ";
247
		}
248
		$req = "SELECT pokemon_id, encounter_id, latitude, longitude, disappear_time,
249
						(CONVERT_TZ(disappear_time, '+00:00', '".self::$time_offset."')) AS disappear_time_real,
250
						individual_attack, individual_defense, individual_stamina, move_1, move_2
251
						FROM pokemon ".$where."
252
						ORDER BY disappear_time DESC
253
						LIMIT 5000";
254
		$result = $this->mysqli->query($req);
255
		$spawns = array();
256
		while ($data = $result->fetch_object()) {
257
			$spawns[] = $data;
258
		}
259
		return $spawns;
260
	}
261
262
	public function getPokemonSliederMinMax() {
263
		$req = "SELECT MIN(disappear_time) AS min, MAX(disappear_time) AS max FROM pokemon";
264
		$result = $this->mysqli->query($req);
265
		$data = $result->fetch_object();
266
		return $data;
267
	}
268
269
	public function getMapsCoords() {
270
		$req = "SELECT MAX(latitude) AS max_latitude, MIN(latitude) AS min_latitude, MAX(longitude) AS max_longitude, MIN(longitude) as min_longitude FROM spawnpoint";
271
		$result = $this->mysqli->query($req);
272
		$data = $result->fetch_object();
273
		return $data;
274
	}
275
276
277
	///////////////
278
	// Pokestops
279
	//////////////
280
281
	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...
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...
282
		$req = "SELECT COUNT(*) as total FROM pokestop";
283
		$result = $this->mysqli->query($req);
284
		$data = $result->fetch_object();
285
		return $data;
286
	}
287
288
	public function getAllPokestops() {
289
		$req = "SELECT latitude, longitude, lure_expiration, UTC_TIMESTAMP() AS now, (CONVERT_TZ(lure_expiration, '+00:00', '".self::$time_offset."')) AS lure_expiration_real FROM pokestop";
290
		$result = $this->mysqli->query($req);
291
		$pokestops = array();
292
		while ($data = $result->fetch_object()) {
293
			$pokestops[] = $data;
294
		}
295
		return $pokestops;
296
	}
297
298
299
	/////////
300
	// Gyms
301
	/////////
302
303
	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...
304
		$req = "SELECT COUNT(*) AS total, guard_pokemon_id FROM gym WHERE team_id = '".$team_id ."' GROUP BY guard_pokemon_id ORDER BY total DESC LIMIT 0,3";
305
		$result = $this->mysqli->query($req);
306
		$datas = array();
307
		while ($data = $result->fetch_object()) {
308
			$datas[] = $data;
309
		}
310
		return $datas;
311
	}
312
313
	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...
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...
314
		$req 	= "SELECT COUNT(DISTINCT(gym_id)) AS total, ROUND(AVG(total_cp),0) AS average_points FROM gym WHERE team_id = '".$team_id."'";
315
		$result = $this->mysqli->query($req);
316
		$data = $result->fetch_object();
317
		return $data;
318
	}
319
320
	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...
321
		$req = "SELECT gym_id, team_id, latitude, longitude, (CONVERT_TZ(last_scanned, '+00:00', '".self::$time_offset."')) AS last_scanned, (6 - slots_available) AS level FROM gym";
322
		$result = $this->mysqli->query($req);
323
		$gyms = array();
324
		while ($data = $result->fetch_object()) {
325
			$gyms[] = $data;
326
		}
327
		return $gyms;
328
	}
329
330 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...
331
		$req = "SELECT gymdetails.name AS name, gymdetails.description AS description, gymdetails.url AS url, gym.team_id AS team,
332
					(CONVERT_TZ(gym.last_scanned, '+00:00', '".self::$time_offset."')) AS last_scanned, gym.guard_pokemon_id AS guard_pokemon_id, gym.total_cp AS total_cp, (6 - gym.slots_available) AS level
333
					FROM gymdetails
334
					LEFT JOIN gym ON gym.gym_id = gymdetails.gym_id
335
					WHERE gym.gym_id='".$gym_id."'";
336
		$result = $this->mysqli->query($req);
337
		$data = $result->fetch_object();
338
		return $data;
339
	}
340
341
	public function getGymDefenders($gym_id) {
342
		$req = "SELECT DISTINCT gympokemon.pokemon_uid, pokemon_id, iv_attack, iv_defense, iv_stamina, MAX(cp) AS cp, gymmember.gym_id
343
					FROM gympokemon INNER JOIN gymmember ON gympokemon.pokemon_uid=gymmember.pokemon_uid
344
					GROUP BY gympokemon.pokemon_uid, pokemon_id, iv_attack, iv_defense, iv_stamina, gym_id
345
					HAVING gymmember.gym_id='".$gym_id."'
346
					ORDER BY cp DESC";
347
		$result = $this->mysqli->query($req);
348
		$defenders = array();
349
		while ($data = $result->fetch_object()) {
350
			$defenders[] = $data;
351
		}
352
		return $defenders;
353
	}
354
355
356
	///////////
357
	// Raids
358
	///////////
359
360
	public function getAllRaids($page) {
361
		$limit = " LIMIT ".($page * 10).",10";
362
		$req = "SELECT raid.gym_id, raid.level, raid.pokemon_id, raid.cp, raid.move_1, raid.move_2, CONVERT_TZ(raid.spawn, '+00:00', '".self::$time_offset."') AS spawn, CONVERT_TZ(raid.start, '+00:00', '".self::$time_offset."') AS start, CONVERT_TZ(raid.end, '+00:00', '".self::$time_offset."') AS end, CONVERT_TZ(raid.last_scanned, '+00:00', '".self::$time_offset."') AS last_scanned, gymdetails.name, gym.latitude, gym.longitude FROM raid
363
					JOIN gymdetails ON gymdetails.gym_id = raid.gym_id
364
					JOIN gym ON gym.gym_id = raid.gym_id
365
					WHERE raid.end > UTC_TIMESTAMP()
366
					ORDER BY raid.level DESC, raid.start".$limit;
367
		$result = $this->mysqli->query($req);
368
		$raids = array();
369
		while ($data = $result->fetch_object()) {
370
			$raids[] = $data;
371
		}
372
		return $raids;
373
	}
374
375
376
	//////////////
377
	// Trainers
378
	//////////////
379
380
	public function getTrainers($trainer_name, $team, $page, $ranking) {
381
		$trainers = $this->getTrainerData($trainer_name, $team, $page, $ranking);
382
		foreach ($trainers as $trainer) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
383
384
			$trainer->rank = $this->getTrainerLevelRating($trainer->level)->rank;
385
386
			$active_gyms = 0;
387
			$pkmCount = 0;
388
389
			$trainer->pokemons = array();
390
			$active_pokemon = $this->getTrainerActivePokemon($trainer->name);
391
			foreach ($active_pokemon as $pokemon) {
392
				$active_gyms++;
393
				$trainer->pokemons[$pkmCount++] = $pokemon;
394
			}
395
396
			$inactive_pokemon = $this->getTrainerInactivePokemon($trainer->name);
397
			foreach ($inactive_pokemon as $pokemon) {
398
				$trainer->pokemons[$pkmCount++] = $pokemon;
399
			}
400
			$trainer->gyms = "".$active_gyms;
401
		}
402
		return $trainers;
403
	}
404
405
	public function getTrainerLevelCount($team_id) {
406
		$req = "SELECT level, count(level) AS count FROM trainer WHERE team = '".$team_id."'";
407
		if (!empty(self::$config->system->trainer_blacklist)) {
408
			$req .= " AND name NOT IN ('".implode("','", self::$config->system->trainer_blacklist)."')";
409
		}
410
		$req .= " GROUP BY level";
411
		$result = $this->mysqli->query($req);
412
		$levelData = array();
413
		while ($data = $result->fetch_object()) {
414
			$levelData[$data['level']] = $data['count'];
415
		}
416
		for ($i = 5; $i <= 40; $i++) {
417
			if (!isset($levelData[$i])) {
418
				$levelData[$i] = 0;
419
			}
420
		}
421
		# sort array again
422
		ksort($levelData);
423
		return $levelData;
424
	}
425
426
	private function getTrainerData($trainer_name, $team, $page, $ranking) {
427
		$where = "";
428
429
		if (!empty(self::$config->system->trainer_blacklist)) {
430
			$where .= ($where == "" ? " HAVING" : " AND")." name NOT IN ('".implode("','", self::$config->system->trainer_blacklist)."')";
431
		}
432
		if ($trainer_name != "") {
433
			$where = " HAVING name LIKE '%".$trainer_name."%'";
434
		}
435
		if ($team != 0) {
436
			$where .= ($where == "" ? " HAVING" : " AND")." team = ".$team;
437
		}
438
		switch ($ranking) {
439
			case 1:
440
				$order = " ORDER BY active DESC, level DESC";
441
				break;
442
			case 2:
443
				$order = " ORDER BY maxCp DESC, level DESC";
444
				break;
445
			default:
446
				$order = " ORDER BY level DESC, active DESC";
447
		}
448
		$order .= ", last_seen DESC, name ";
449
		$limit = " LIMIT ".($page * 10).",10 ";
450
		$req = "SELECT trainer.*, COUNT(actives_pokemons.trainer_name) AS active, max(actives_pokemons.cp) AS maxCp
451
				FROM trainer
452
				LEFT JOIN (SELECT DISTINCT gympokemon.pokemon_id, gympokemon.pokemon_uid, gympokemon.trainer_name, gympokemon.cp, DATEDIFF(UTC_TIMESTAMP(), gympokemon.last_seen) AS last_scanned
453
				FROM gympokemon
454
				INNER JOIN (SELECT gymmember.pokemon_uid, gymmember.gym_id FROM gymmember GROUP BY gymmember.pokemon_uid, gymmember.gym_id HAVING gymmember.gym_id <> '') AS filtered_gymmember
455
				ON gympokemon.pokemon_uid = filtered_gymmember.pokemon_uid) AS actives_pokemons ON actives_pokemons.trainer_name = trainer.name
456
				GROUP BY trainer.name " . $where . $order . $limit;
457
458
		$result = $this->mysqli->query($req);
459
		$trainers = array();
460
		while ($data = $result->fetch_object()) {
461
			$data->last_seen = date("Y-m-d", strtotime($data->last_seen));
462
			$trainers[$data->name] = $data;
463
		}
464
		return $trainers;
465
	}
466
467
	private function getTrainerLevelRating($level) {
468
		$req = "SELECT COUNT(1) AS rank FROM trainer WHERE level = ".$level;
469
		if (!empty(self::$config->system->trainer_blacklist)) {
470
			$req .= " AND name NOT IN ('".implode("','", self::$config->system->trainer_blacklist)."')";
471
		}
472
		$result = $this->mysqli->query($req);
473
		$data = $result->fetch_object();
474
		return $data;
475
	}
476
477 View Code Duplication
	private function getTrainerActivePokemon($trainer_name){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
478
		$req = "(SELECT DISTINCT gympokemon.pokemon_id, gympokemon.pokemon_uid, gympokemon.cp, DATEDIFF(UTC_TIMESTAMP(), gympokemon.last_seen) AS last_scanned, gympokemon.trainer_name, gympokemon.iv_defense, gympokemon.iv_stamina, gympokemon.iv_attack, filtered_gymmember.gym_id, CONVERT_TZ(filtered_gymmember.deployment_time, '+00:00', '".self::$time_offset."') as deployment_time, '1' AS active
479
					FROM gympokemon INNER JOIN
480
					(SELECT gymmember.pokemon_uid, gymmember.gym_id, gymmember.deployment_time FROM gymmember GROUP BY gymmember.pokemon_uid, gymmember.deployment_time, gymmember.gym_id HAVING gymmember.gym_id <> '') AS filtered_gymmember
481
					ON gympokemon.pokemon_uid = filtered_gymmember.pokemon_uid
482
					WHERE gympokemon.trainer_name='".$trainer_name."'
483
					ORDER BY gympokemon.cp DESC)";
484
		$result = $this->mysqli->query($req);
485
		$pokemons = array();
486
		while ($data = $result->fetch_object()) {
487
			$pokemons[] = $data;
488
		}
489
		return $pokemons;
490
	}
491
492 View Code Duplication
	private function getTrainerInactivePokemon($trainer_name){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
493
		$req = "(SELECT DISTINCT gympokemon.pokemon_id, gympokemon.pokemon_uid, gympokemon.cp, DATEDIFF(UTC_TIMESTAMP(), gympokemon.last_seen) AS last_scanned, gympokemon.trainer_name, gympokemon.iv_defense, gympokemon.iv_stamina, gympokemon.iv_attack, null AS gym_id, CONVERT_TZ(filtered_gymmember.deployment_time, '+00:00', '".self::$time_offset."') as deployment_time, '0' AS active
494
					FROM gympokemon LEFT JOIN
495
					(SELECT * FROM gymmember HAVING gymmember.gym_id <> '') AS filtered_gymmember
496
					ON gympokemon.pokemon_uid = filtered_gymmember.pokemon_uid
497
					WHERE filtered_gymmember.pokemon_uid IS NULL AND gympokemon.trainer_name='".$trainer_name."'
498
					ORDER BY gympokemon.cp DESC)";
499
		$result = $this->mysqli->query($req);
500
		$pokemons = array();
501
		while ($data = $result->fetch_object()) {
502
			$pokemons[] = $data;
503
		}
504
		return $pokemons;
505
	}
506
507
508
	/////////
509
	// Cron
510
	/////////
511
512
	public function getPokemonCountsActive() {
513
		$req = "SELECT pokemon_id, COUNT(*) as total FROM pokemon WHERE disappear_time >= UTC_TIMESTAMP() GROUP BY pokemon_id";
514
		$result = $this->mysqli->query($req);
515
		$counts = array();
516
		while ($data = $result->fetch_object()) {
517
			$counts[$data->pokemon_id] = $data->total;
518
		}
519
		return $counts;
520
	}
521
522
	public function getPoekmonCountsLastDay() {
523
		$req = "SELECT pokemon_id, COUNT(*) AS spawns_last_day
524
					FROM pokemon
525
					WHERE disappear_time >= (SELECT MAX(disappear_time) FROM pokemon) - INTERVAL 1 DAY
526
					GROUP BY pokemon_id
527
				  	ORDER BY pokemon_id ASC";
528
		$result = $this->mysqli->query($req);
529
		$counts = array();
530
		while ($data = $result->fetch_object()) {
531
			$counts[$data->pokemon_id] = $data->spawns_last_day;
532
		}
533
		return $counts;
534
	}
535
536 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...
537
		$where = "WHERE p.pokemon_id = '".$pokemon_id."' AND (UNIX_TIMESTAMP(p.disappear_time) - (LENGTH(s.kind) - LENGTH( REPLACE ( kind, \"s\", \"\") )) * 900) > '".$last_update."'";
538
		$req = "SELECT count, (UNIX_TIMESTAMP(p.disappear_time) - (LENGTH(s.kind) - LENGTH( REPLACE ( kind, \"s\", \"\") )) * 900) as last_timestamp, (CONVERT_TZ(p.disappear_time, '+00:00', '".self::$time_offset."')) AS disappear_time_real, p.latitude, p.longitude 
539
				FROM pokemon p
540
				JOIN spawnpoint s ON p.spawnpoint_id = s.id
541
				JOIN (SELECT count(*) as count
542
                    FROM pokemon p
543
                    JOIN spawnpoint s ON p.spawnpoint_id = s.id
544
                    " . $where."
545
                ) x
546
				" . $where . "
547
				ORDER BY last_timestamp DESC
548
                LIMIT 0,1";
549
		$result = $this->mysqli->query($req);
550
		$data = $result->fetch_object();
551
		return $data;
552
	}
553
554 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...
555
		$where = "WHERE pokemon_id = '".$pokemon_id."' AND UNIX_TIMESTAMP(start) > '".$last_update."'";
556
		$req = "SELECT UNIX_TIMESTAMP(start) as start_timestamp, end, (CONVERT_TZ(end, '+00:00', '".self::$time_offset."')) AS end_time_real, latitude, longitude, count
557
                FROM raid r
558
                JOIN gym g
559
                JOIN (SELECT count(*) as count
560
                    FROM raid
561
                    " . $where."
562
                ) x
563
                ON r.gym_id = g.gym_id
564
                " . $where."
565
                ORDER BY start DESC
566
                LIMIT 0,1";
567
		$result = $this->mysqli->query($req);
568
		$data = $result->fetch_object();
569
		return $data;
570
	}
571
572
	public function getCaptchaCount() {
573
		$req = "SELECT SUM(accounts_captcha) AS total FROM mainworker";
574
		$result = $this->mysqli->query($req);
575
		$data = $result->fetch_object();
576
		return $data;
577
	}
578
579 View Code Duplication
	public function getNestData() {
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...
580
		$pokemon_exclude_sql = "";
581
		if (!empty(self::$config->system->nest_exclude_pokemon)) {
582
			$pokemon_exclude_sql = "AND p.pokemon_id NOT IN (".implode(",", self::$config->system->nest_exclude_pokemon).")";
583
		}
584
		$req = "SELECT p.pokemon_id, max(p.latitude) AS latitude, max(p.longitude) AS longitude, count(p.pokemon_id) AS total_pokemon, s.latest_seen, (LENGTH(s.kind) - LENGTH( REPLACE ( kind, \"s\", \"\") )) * 900 AS duration
585
			          FROM pokemon p 
586
			          INNER JOIN spawnpoint s ON (p.spawnpoint_id = s.id) 
587
			          WHERE p.disappear_time > UTC_TIMESTAMP() - INTERVAL 24 HOUR 
588
			          " . $pokemon_exclude_sql . " 
589
			          GROUP BY p.spawnpoint_id, p.pokemon_id 
590
			          HAVING COUNT(p.pokemon_id) >= 6 
591
			          ORDER BY p.pokemon_id";
592
		$result = $this->mysqli->query($req);
593
		$nests = array();
594
		while ($data = $result->fetch_object()) {
595
			$nests[] = $data;
596
		}
597
		return $nests;
598
	}
599
600
}