Failed Conditions
Pull Request — master (#353)
by Florian
02:52
created

QueryManagerMonocleHydro::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
class QueryManagerMonocleHydro 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
	///////////
10
	// TesterF
11
	///////////
12
13 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...
14
		$req = "SELECT COUNT(*) as total FROM sightings";
15
		$result = $this->mysqli->query($req);
16
		if (!is_object($result)) {
17
			return 1;
18
		} else {
19
			$data = $result->fetch_object();
20
			$total = $data->total;
21
22
			if ($total == 0) {
23
				return 2;
24
			}
25
		}
26
		return 0;
27
	}
28
29 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...
30
		$req = "SELECT COUNT(*) as total FROM forts";
31
		$result = $this->mysqli->query($req);
32
		if (!is_object($result)) {
33
			return 1;
34
		} else {
35
			$data = $result->fetch_object();
36
			$total = $data->total;
37
38
			if ($total == 0) {
39
				return 2;
40
			}
41
		}
42
		return 0;
43
	}
44
45 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...
46
		$req = "SELECT COUNT(*) as total FROM pokestops";
47
		$result = $this->mysqli->query($req);
48
		if (!is_object($result)) {
49
			return 1;
50
		} else {
51
			$data = $result->fetch_object();
52
			$total = $data->total;
53
54
			if ($total == 0) {
55
				return 2;
56
			}
57
		}
58
		return 0;
59
	}
60
	
61
	
62
	/////////////
63
	// Homepage
64
	/////////////
65
66
	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...
67
		$req = "SELECT COUNT(*) AS total FROM sightings WHERE expire_timestamp >= UNIX_TIMESTAMP()";
68
		$result = $this->mysqli->query($req);
69
		$data = $result->fetch_object();
70
		return $data;
71
	}
72
73
	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...
74
		$data = (object) array("total" => 0);
75
		return $data;
76
	}
77
78
	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...
79
		$req = "SELECT COUNT(*) AS total FROM forts";
80
		$result = $this->mysqli->query($req);
81
		$data = $result->fetch_object();
82
		return $data;
83
	}
84
85
	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...
86
		$req = "SELECT COUNT(*) AS total FROM raids WHERE time_battle <= UNIX_TIMESTAMP() AND time_end >= UNIX_TIMESTAMP()";
87
		$result = $this->mysqli->query($req);
88
		$data = $result->fetch_object();
89
		return $data;
90
	}
91
92
93
	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...
94
		$req = "SELECT COUNT(*) AS total FROM fort_sightings WHERE team = '$team_id'";
95
		$result = $this->mysqli->query($req);
96
		$data = $result->fetch_object();
97
		return $data;
98
	}
99
100
	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...
101
		$req = "SELECT DISTINCT pokemon_id, encounter_id, FROM_UNIXTIME(expire_timestamp) AS disappear_time, FROM_UNIXTIME(updated+0) AS last_modified, FROM_UNIXTIME(expire_timestamp) AS disappear_time_real,
102
              lat AS latitude, lon AS longitude, cp, atk_iv AS individual_attack, def_iv AS individual_defense, sta_iv AS individual_stamina
103
              FROM sightings
104
              ORDER BY last_modified DESC
105
              LIMIT 0,12";
106
		$result = $this->mysqli->query($req);
107
		$data = array();
108
		if ($result->num_rows > 0) {
109
			while ($row = $result->fetch_object()) {
110
				$data[] = $row;
111
			}
112
		}
113
		return $data;
114
	}
115
116
	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...
117
		$req = "SELECT DISTINCT pokemon_id as pokemon_id, CONCAT('A', encounter_id) as encounter_id, FROM_UNIXTIME(expire_timestamp) AS disappear_time, FROM_UNIXTIME(updated+0) AS last_modified, FROM_UNIXTIME(expire_timestamp) AS disappear_time_real,
118
                lat AS latitude, lon AS longitude, cp, atk_iv AS individual_attack, def_iv AS individual_defense, sta_iv AS individual_stamina
119
                FROM sightings
120
                WHERE pokemon_id+0 IN (".implode(",", $mythic_pokemon).")
121
                ORDER BY last_modified DESC
122
                LIMIT 0,12";
123
		$result = $this->mysqli->query($req);
124
		$data = array();
125
		if ($result->num_rows > 0) {
126
			while ($row = $result->fetch_object()) {
127
				$data[] = $row;
128
			}
129
		}
130
		return $data;
131
	}
132
133
	///////////////////
134
	// Single Pokemon
135
	///////////////////
136
137 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...
138
		$req = "SELECT COUNT(DISTINCT(fort_id)) AS total FROM fort_sightings WHERE guard_pokemon_id = '".$pokemon_id."'";
139
		$result = $this->mysqli->query($req);
140
		$data = $result->fetch_object();
141
		return $data;
142
	}
143
144 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...
145
		$req = "SELECT FROM_UNIXTIME(expire_timestamp) AS expire_timestamp, FROM_UNIXTIME(expire_timestamp) AS disappear_time_real, lat AS latitude, lon AS longitude
146
                FROM sightings
147
                WHERE pokemon_id = '".$pokemon_id."'
148
                ORDER BY expire_timestamp DESC
149
                LIMIT 0,1";
150
		$result = $this->mysqli->query($req);
151
		$data = $result->fetch_object();
152
		return $data;
153
	}
154
155
	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...
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...
156
		$req = "SELECT FROM_UNIXTIME(expire_timestamp+0) AS distime, pokemon_id+0 as pokemon_id, FROM_UNIXTIME(expire_timestamp+0) as disappear_time, lat as latitude, lon as longitude,
157
                cp, atk_iv+0 as individual_attack, def_iv+0 as individual_defense, sta_iv+0 as individual_stamina,
158
                ROUND(100*(atk_iv+def_iv+sta_iv)/45,1) AS IV, move_1+0 as move_1, move_2, form
159
                FROM sightings
160
	            WHERE pokemon_id+0 = '" . $pokemon_id . "' AND move_1+0 IS NOT NULL AND move_1+0 <> '0'
161
	            GROUP BY encounter_id+0
162
	            ORDER BY $top_order_by $top_direction, expire_timestamp+0 DESC
163
	            LIMIT 0,50";
164
165
		$result = $this->mysqli->query($req);
166
		$top = array();
167
		while ($data = $result->fetch_object()) {
168
			$top[] = $data;
169
		}
170
		return $top;
171
	}
172
173 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...
174
		$trainer_blacklist = "";
175
		if (!empty(self::$config->system->trainer_blacklist)) {
176
			$trainer_blacklist = " AND owner_name NOT IN ('" . implode("','", self::$config->system->trainer_blacklist) . "')";
177
		}
178
179
		$req = "SELECT owner_name as trainer_name, ROUND(SUM(100*((atk_iv+0)+(def_iv+0)+(sta_iv+0))/45),1) AS IV, move_1, move_2, cp+0 as cp,
180
                FROM_UNIXTIME(last_modified+0) AS lasttime, last_modified+0 as last_seen
181
                FROM gym_defenders
182
				WHERE pokemon_id+0 = '" . $pokemon_id . "'" . $trainer_blacklist . "
183
				GROUP BY external_id
184
				ORDER BY $best_order_by $best_direction, owner_name+'' ASC
185
				LIMIT 0,50";
186
187
		$result = $this->mysqli->query($req);
188
		$toptrainer = array();
189
		while ($data = $result->fetch_object()) {
190
			$toptrainer[] = $data;
191
		}
192
		return $toptrainer;
193
	}
194
195 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...
196
		$where = " WHERE pokemon_id = ".$pokemon_id." "
197
			. "AND FROM_UNIXTIME(expire_timestamp) BETWEEN '".$start."' AND '".$end."'";
198
		$req 		= "SELECT lat AS latitude, lon AS longitude FROM sightings".$where." ORDER BY expire_timestamp DESC LIMIT 100000";
199
		$result = $this->mysqli->query($req);
200
		$points = array();
201
		while ($data = $result->fetch_object()) {
202
			$points[] = $data;
203
		}
204
		return $points;
205
	}
206
207 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...
208
		$req = "SELECT COUNT(*) AS total, HOUR(disappear_time) AS disappear_hour
209
					FROM (SELECT FROM_UNIXTIME(expire_timestamp) as disappear_time FROM sightings WHERE pokemon_id = '".$pokemon_id."' ORDER BY disappear_time LIMIT 100000) AS pokemonFiltered
210
				GROUP BY disappear_hour
211
				ORDER BY disappear_hour";
212
		$result = $this->mysqli->query($req);
213
		$array = array_fill(0, 24, 0);
214
		while ($result && $data = $result->fetch_object()) {
215
			$array[$data->disappear_hour] = $data->total;
216
		}
217
		// shift array because AM/PM starts at 1AM not 0:00
218
		$array[] = $array[0];
219
		array_shift($array);
220
		return $array;
221
	}
222
223 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...
224
		$inmap_pkms_filter = "";
225
		$where = " WHERE expire_timestamp >= UNIX_TIMESTAMP() AND pokemon_id = " . $pokemon_id;
226
227
		$reqTestIv = "SELECT MAX(atk_iv) AS iv FROM sightings " . $where;
228
		$resultTestIv = $this->mysqli->query($reqTestIv);
229
		$testIv = $resultTestIv->fetch_object();
230
		if (!is_null($inmap_pokemons) && ($inmap_pokemons != "")) {
231
			foreach ($_POST['inmap_pokemons'] as $inmap) {
232
				$inmap_pkms_filter .= "'".$inmap."',";
233
			}
234
			$inmap_pkms_filter = rtrim($inmap_pkms_filter, ",");
235
			$where .= " AND encounter_id NOT IN (" . $inmap_pkms_filter . ") ";
236
		}
237
		if ($testIv->iv != null && !is_null($ivMin) && ($ivMin != "")) {
238
			$where .= " AND ((100/45)*(atk_iv + def_iv + sta_iv)) >= (" . $ivMin . ") ";
239
		}
240
		if ($testIv->iv != null && !is_null($ivMax) && ($ivMax != "")) {
241
			$where .= " AND ((100/45)*(atk_iv + def_iv + sta_iv)) <= (" . $ivMax . ") ";
242
		}
243
		$req = "SELECT pokemon_id, lat AS latitude, lon AS longitude,
244
    					FROM_UNIXTIME(expire_timestamp) AS disappear_time,
245
    					FROM_UNIXTIME(expire_timestamp) AS disappear_time_real,
246
    					atk_iv AS individual_attack, def_iv AS individual_defense, sta_iv AS individual_stamina,
247
   						move_1, move_2
248
					FROM sightings " . $where . "
249
					ORDER BY disappear_time DESC
250
					LIMIT 5000";
251
		$result = $this->mysqli->query($req);
252
		$spawns = array();
253
		while ($data = $result->fetch_object()) {
254
			$spawns[] = $data;
255
		}
256
		return $spawns;
257
	}
258
259
	public function getPokemonSliederMinMax() {
260
		$req = "SELECT FROM_UNIXTIME(MIN(expire_timestamp)) AS min, FROM_UNIXTIME(MAX(expire_timestamp)) AS max FROM sightings";
261
		$result = $this->mysqli->query($req);
262
		$data = $result->fetch_object();
263
		return $data;
264
	}
265
266
	public function getMapsCoords() {
267
		$req = "SELECT MAX(lat) AS max_latitude, MIN(lat) AS min_latitude, MAX(lon) AS max_longitude, MIN(lon) as min_longitude FROM spawnpoints";
268
		$result = $this->mysqli->query($req);
269
		$data = $result->fetch_object();
270
		return $data;
271
	}
272
273
274
	///////////////
275
	// Pokestops
276
	//////////////
277
278
279
	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...
280
		$req = "SELECT COUNT(*) as total FROM pokestops";
281
		$result = $this->mysqli->query($req);
282
		$data = $result->fetch_object();
283
		return $data;
284
	}
285
286
	public function getAllPokestops() {
287
		$req = "SELECT lat as latitude, lon as longitude, null AS lure_expiration, UNIX_TIMESTAMP() AS now, null AS lure_expiration_real FROM pokestops";
288
		$result = $this->mysqli->query($req);
289
		$pokestops = array();
290
		while ($data = $result->fetch_object()) {
291
			$pokestops[] = $data;
292
		}
293
		return $pokestops;
294
	}
295
296
297
	/////////
298
	// Gyms
299
	/////////
300
301
	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...
302
		$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 0,3";
303
		$result = $this->mysqli->query($req);
304
305
		$datas = array();
306
		while ($data = $result->fetch_object()) {
307
			$datas[] = $data;
308
		}
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(fs.fort_id)) AS total, ROUND((SUM(gd.cp)) / COUNT(DISTINCT(fs.fort_id)),0) AS average_points
315
        			FROM fort_sightings fs
316
        			JOIN gym_defenders gd ON fs.fort_id = gd.fort_id
317
        			WHERE fs.team = '" . $team_id . "'";
318
		$result = $this->mysqli->query($req);
319
		$data = $result->fetch_object();
320
		return $data;
321
	}
322
323
	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...
324
		$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;";
325
		$result = $this->mysqli->query($req);
326
		$gyms = array();
327
		while ($data = $result->fetch_object()) {
328
			$gyms[] = $data;
329
		}
330
		return $gyms;
331
	}
332
333
	public function getGymData($gym_id) {
0 ignored issues
show
Coding Style introduced by
getGymData uses the super-global variable $_GET 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...
334
		$gym_id = $this->mysqli->real_escape_string($_GET['gym_id']);
335
		$req = "SELECT f.name AS name, null AS description, f.url AS url, fs.team AS team, FROM_UNIXTIME(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	
336
			FROM fort_sightings fs
337
			LEFT JOIN forts f ON f.id = fs.fort_id
338
			JOIN gym_defenders gd ON f.id = gd.fort_id
339
			WHERE f.id ='".$gym_id."'";
340
		$result = $this->mysqli->query($req);
341
		$data = $result->fetch_object();
342
		return $data;
343
	}
344
345
	public function getGymDefenders($gym_id) {
346
		$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
347
			FROM gym_defenders 
348
			WHERE fort_id='".$gym_id."'
349
			ORDER BY cp DESC";
350
		$result = $this->mysqli->query($req);
351
		$defenders = array();
352
		while ($data = $result->fetch_object()) {
353
			$defenders[] = $data;
354
		}
355
		return $defenders;
356
	}
357
358
359
	///////////
360
	// Raids
361
	///////////
362
363
	public function getAllRaids($page) {
364
		$limit = " LIMIT ".($page * 10).",10";
365
		$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, FROM_UNIXTIME(r.time_spawn) AS spawn, FROM_UNIXTIME(r.time_battle) AS start, FROM_UNIXTIME(r.time_end) AS end, FROM_UNIXTIME(fs.updated) AS last_scanned, f.name, f.lat AS latitude, f.lon as longitude 
366
					FROM raids r 
367
					JOIN forts f ON f.id = r.fort_id 
368
					JOIN fort_sightings fs ON fs.fort_id = r.fort_id 
369
					WHERE r.time_end > UNIX_TIMESTAMP() 
370
					ORDER BY r.level DESC, r.time_battle" . $limit;
371
		$result = $this->mysqli->query($req);
372
		$raids = array();
373
		while ($data = $result->fetch_object()) {
374
			$raids[] = $data;
375
		}
376
		return $raids;
377
	}
378
379
380
	//////////////
381
	// Trainers
382
	//////////////
383
384
	public function getTrainers($trainer_name, $team, $page, $ranking) {
385
		return array(); // Waiting for Monocle to store level
386
	}
387
388
	public function getTrainerLevelCount($team_id) {
389
		$levelData = array();
390 View Code Duplication
		for ($i = 5; $i <= 40; $i++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
391
			if (!isset($levelData[$i])) {
392
				$levelData[$i] = 0;
393
			}
394
		}
395
		return $levelData; // Waiting for Monocle to store level
396
	}
397
398
399
	/////////
400
	// Cron
401
	/////////
402
403
	public function getPokemonCountsActive() {
404
		$req = "SELECT pokemon_id, COUNT(*) as total FROM sightings WHERE expire_timestamp >= UNIX_TIMESTAMP() GROUP BY pokemon_id";
405
		$result = $this->mysqli->query($req);
406
		$counts = array();
407
		while ($data = $result->fetch_object()) {
408
			$counts[$data->pokemon_id] = $data->total;
409
		}
410
		return $counts;
411
	}
412
413
	public function getPoekmonCountsLastDay() {
414
		$req = "SELECT pokemon_id, COUNT(*) AS spawns_last_day
415
					FROM sightings
416
					WHERE FROM_UNIXTIME(expire_timestamp) >= (SELECT FROM_UNIXTIME(MAX(expire_timestamp)) FROM sightings) - INTERVAL 1 DAY
417
					GROUP BY pokemon_id
418
				  	ORDER BY pokemon_id ASC";
419
		$result = $this->mysqli->query($req);
420
		$counts = array();
421
		while ($data = $result->fetch_object()) {
422
			$counts[$data->pokemon_id] = $data->spawns_last_day;
423
		}
424
		return $counts;
425
	}
426
427 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...
428
		$req = "SELECT COUNT(*) AS count, MAX(expire_timestamp) AS last_timestamp, (FROM_UNIXTIME(MAX(expire_timestamp))) AS disappear_time_real, lat as latitude, lon as longitude
429
					FROM sightings
430
					WHERE pokemon_id = '".$pokemon_id."' && expire_timestamp > '".$last_update."'";
431
		$result = $this->mysqli->query($req);
432
		$data = $result->fetch_object();
433
		return $data;
434
	}
435
436 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...
437
		$where = "WHERE pokemon_id = '".$pokemon_id."' && time_battle > '".$last_update."'";
438
		$req = "SELECT time_battle AS start_timestamp, time_end as end, (FROM_UNIXTIME(time_end)) AS end_time_real, lat as latitude, lon as longitude, count
439
					FROM raids r
440
					JOIN forts g
441
					JOIN (SELECT COUNT(*) AS count
442
					FROM raids
443
                    " . $where."
444
                ) x 
445
				ON r.fort_id = g.id
446
                " . $where."
447
                ORDER BY time_battle DESC
448
				LIMIT 0 , 1";
449
		$result = $this->mysqli->query($req);
450
		$data = $result->fetch_object();
451
		return $data;
452
	}
453
454
	public function getCaptchaCount() {
455
		$req = " SELECT COUNT(*) as total FROM accounts WHERE captchaed IS NOT NULL AND reason IS NULL";
456
		$result = $this->mysqli->query($req);
457
		$data = $result->fetch_object();
458
		return $data;
459
	}
460
461 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...
462
		$pokemon_exclude_sql = "";
463
		if (!empty(self::$config->system->nest_exclude_pokemon)) {
464
			$pokemon_exclude_sql = "AND p.pokemon_id NOT IN (" . implode(",", self::$config->system->nest_exclude_pokemon) . ")";
465
		}
466
		$req = "SELECT p.pokemon_id, p.lat AS latitude, p.lon AS longitude, count(p.pokemon_id) AS total_pokemon, s.updated, coalesce(duration,30)*60 as duration
467
			          FROM sightings p
468
			          INNER JOIN spawnpoints s ON (p.spawn_id = s.spawn_id)
469
			          WHERE p.expire_timestamp > UNIX_TIMESTAMP() - 86400
470
			          " . $pokemon_exclude_sql . "
471
			          GROUP BY p.spawn_id, p.pokemon_id
472
			          HAVING COUNT(p.pokemon_id) >= 6
473
			          ORDER BY p.pokemon_id";
474
		$result = $this->mysqli->query($req);
475
		$nests = array();
476
		while ($data = $result->fetch_object()) {
477
			$nests[] = $data;
478
		}
479
		return $nests;
480
	}
481
482
}