Failed Conditions
Pull Request — master (#353)
by Florian
02:54 queued 17s
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
	public function __destruct() {
10
		parent::__destruct();
11
	}
12
13
	///////////
14
	// TesterF
15
	///////////
16
17 View Code Duplication
	function testTotalPokemon() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

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

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

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

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

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

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

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

Loading history...
18
		$req = "SELECT COUNT(*) as total FROM sightings";
19
		$result = $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 forts";
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 pokestops";
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 sightings WHERE expire_timestamp >= UNIX_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
		$data = (object) array("total" => 0);
79
		return $data;
80
	}
81
82
	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...
83
		$req = "SELECT COUNT(*) AS total FROM forts";
84
		$result = $this->mysqli->query($req);
85
		$data = $result->fetch_object();
86
		return $data;
87
	}
88
89
	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...
90
		$req = "SELECT COUNT(*) AS total FROM raids WHERE time_battle <= UNIX_TIMESTAMP() AND time_end >= UNIX_TIMESTAMP()";
91
		$result = $this->mysqli->query($req);
92
		$data = $result->fetch_object();
93
		return $data;
94
	}
95
96
97
	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...
98
		$req = "SELECT COUNT(*) AS total FROM fort_sightings WHERE team = '$team_id'";
99
		$result = $this->mysqli->query($req);
100
		$data = $result->fetch_object();
101
		return $data;
102
	}
103
104
	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...
105
		$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,
106
              lat AS latitude, lon AS longitude, cp, atk_iv AS individual_attack, def_iv AS individual_defense, sta_iv AS individual_stamina
107
              FROM sightings
108
              ORDER BY last_modified DESC
109
              LIMIT 0,12";
110
		$result = $this->mysqli->query($req);
111
		$data = array();
112
		if ($result->num_rows > 0) {
113
			while ($row = $result->fetch_object()) {
114
				$data[] = $row;
115
			}
116
		}
117
		return $data;
118
	}
119
120
	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...
121
		$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,
122
                lat AS latitude, lon AS longitude, cp, atk_iv AS individual_attack, def_iv AS individual_defense, sta_iv AS individual_stamina
123
                FROM sightings
124
                WHERE pokemon_id+0 IN (".implode(",", $mythic_pokemon).")
125
                ORDER BY last_modified DESC
126
                LIMIT 0,12";
127
		$result = $this->mysqli->query($req);
128
		$data = array();
129
		if ($result->num_rows > 0) {
130
			while ($row = $result->fetch_object()) {
131
				$data[] = $row;
132
			}
133
		}
134
		return $data;
135
	}
136
137
	///////////////////
138
	// Single Pokemon
139
	///////////////////
140
141 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...
142
		$req = "SELECT COUNT(DISTINCT(fort_id)) AS total FROM fort_sightings WHERE guard_pokemon_id = '".$pokemon_id."'";
143
		$result = $this->mysqli->query($req);
144
		$data = $result->fetch_object();
145
		return $data;
146
	}
147
148 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...
149
		$req = "SELECT FROM_UNIXTIME(expire_timestamp) AS expire_timestamp, FROM_UNIXTIME(expire_timestamp) AS disappear_time_real, lat AS latitude, lon AS longitude
150
                FROM sightings
151
                WHERE pokemon_id = '".$pokemon_id."'
152
                ORDER BY expire_timestamp DESC
153
                LIMIT 0,1";
154
		$result = $this->mysqli->query($req);
155
		$data = $result->fetch_object();
156
		return $data;
157
	}
158
159
	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...
160
		$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,
161
                cp, atk_iv+0 as individual_attack, def_iv+0 as individual_defense, sta_iv+0 as individual_stamina,
162
                ROUND(100*(atk_iv+def_iv+sta_iv)/45,1) AS IV, move_1+0 as move_1, move_2, form
163
                FROM sightings
164
	            WHERE pokemon_id+0 = '" . $pokemon_id . "' AND move_1+0 IS NOT NULL AND move_1+0 <> '0'
165
	            GROUP BY encounter_id+0
166
	            ORDER BY $top_order_by $top_direction, expire_timestamp+0 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 owner_name NOT IN ('" . implode("','", self::$config->system->trainer_blacklist) . "')";
181
		}
182
183
		$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,
184
                FROM_UNIXTIME(last_modified+0) AS lasttime, last_modified+0 as last_seen
185
                FROM gym_defenders
186
				WHERE pokemon_id+0 = '" . $pokemon_id . "'" . $trainer_blacklist . "
187
				GROUP BY external_id
188
				ORDER BY $best_order_by $best_direction, owner_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 FROM_UNIXTIME(expire_timestamp) BETWEEN '".$start."' AND '".$end."'";
202
		$req 		= "SELECT lat AS latitude, lon AS longitude FROM sightings".$where." ORDER BY expire_timestamp DESC LIMIT 100000";
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, HOUR(disappear_time) AS disappear_hour
213
					FROM (SELECT FROM_UNIXTIME(expire_timestamp) as disappear_time FROM sightings WHERE pokemon_id = '".$pokemon_id."' ORDER BY disappear_time LIMIT 100000) AS pokemonFiltered
214
				GROUP BY disappear_hour
215
				ORDER BY disappear_hour";
216
		$result = $this->mysqli->query($req);
217
		$array = array_fill(0, 24, 0);
218
		while ($result && $data = $result->fetch_object()) {
219
			$array[$data->disappear_hour] = $data->total;
220
		}
221
		// shift array because AM/PM starts at 1AM not 0:00
222
		$array[] = $array[0];
223
		array_shift($array);
224
		return $array;
225
	}
226
227 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...
228
		$inmap_pkms_filter = "";
229
		$where = " WHERE expire_timestamp >= UNIX_TIMESTAMP() AND pokemon_id = " . $pokemon_id;
230
231
		$reqTestIv = "SELECT MAX(atk_iv) AS iv FROM sightings " . $where;
232
		$resultTestIv = $this->mysqli->query($reqTestIv);
233
		$testIv = $resultTestIv->fetch_object();
234
		if (!is_null($inmap_pokemons) && ($inmap_pokemons != "")) {
235
			foreach ($_POST['inmap_pokemons'] as $inmap) {
236
				$inmap_pkms_filter .= "'".$inmap."',";
237
			}
238
			$inmap_pkms_filter = rtrim($inmap_pkms_filter, ",");
239
			$where .= " AND encounter_id NOT IN (" . $inmap_pkms_filter . ") ";
240
		}
241
		if ($testIv->iv != null && !is_null($ivMin) && ($ivMin != "")) {
242
			$where .= " AND ((100/45)*(atk_iv + def_iv + sta_iv)) >= (" . $ivMin . ") ";
243
		}
244
		if ($testIv->iv != null && !is_null($ivMax) && ($ivMax != "")) {
245
			$where .= " AND ((100/45)*(atk_iv + def_iv + sta_iv)) <= (" . $ivMax . ") ";
246
		}
247
		$req = "SELECT pokemon_id, lat AS latitude, lon AS longitude,
248
    					FROM_UNIXTIME(expire_timestamp) AS disappear_time,
249
    					FROM_UNIXTIME(expire_timestamp) AS disappear_time_real,
250
    					atk_iv AS individual_attack, def_iv AS individual_defense, sta_iv AS individual_stamina,
251
   						move_1, move_2
252
					FROM sightings " . $where . "
253
					ORDER BY disappear_time DESC
254
					LIMIT 5000";
255
		$result = $this->mysqli->query($req);
256
		$spawns = array();
257
		while ($data = $result->fetch_object()) {
258
			$spawns[] = $data;
259
		}
260
		return $spawns;
261
	}
262
263
	public function getPokemonSliederMinMax() {
264
		$req = "SELECT FROM_UNIXTIME(MIN(expire_timestamp)) AS min, FROM_UNIXTIME(MAX(expire_timestamp)) AS max FROM sightings";
265
		$result = $this->mysqli->query($req);
266
		$data = $result->fetch_object();
267
		return $data;
268
	}
269
270
	public function getMapsCoords() {
271
		$req = "SELECT MAX(lat) AS max_latitude, MIN(lat) AS min_latitude, MAX(lon) AS max_longitude, MIN(lon) as min_longitude FROM spawnpoints";
272
		$result = $this->mysqli->query($req);
273
		$data = $result->fetch_object();
274
		return $data;
275
	}
276
277
278
	///////////////
279
	// Pokestops
280
	//////////////
281
282
283
	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...
284
		$req = "SELECT COUNT(*) as total FROM pokestops";
285
		$result = $this->mysqli->query($req);
286
		$data = $result->fetch_object();
287
		return $data;
288
	}
289
290
	public function getAllPokestops() {
291
		$req = "SELECT lat as latitude, lon as longitude, null AS lure_expiration, UNIX_TIMESTAMP() AS now, null AS lure_expiration_real FROM pokestops";
292
		$result = $this->mysqli->query($req);
293
		$pokestops = array();
294
		while ($data = $result->fetch_object()) {
295
			$pokestops[] = $data;
296
		}
297
		return $pokestops;
298
	}
299
300
301
	/////////
302
	// Gyms
303
	/////////
304
305
	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...
306
		$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";
307
		$result = $this->mysqli->query($req);
308
309
		$datas = array();
310
		while ($data = $result->fetch_object()) {
311
			$datas[] = $data;
312
		}
313
314
		return $datas;
315
	}
316
317
	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...
318
		$req = "SELECT COUNT(DISTINCT(fs.fort_id)) AS total, ROUND((SUM(gd.cp)) / COUNT(DISTINCT(fs.fort_id)),0) AS average_points
319
        			FROM fort_sightings fs
320
        			JOIN gym_defenders gd ON fs.fort_id = gd.fort_id
321
        			WHERE fs.team = '" . $team_id . "'";
322
		$result = $this->mysqli->query($req);
323
		$data = $result->fetch_object();
324
		return $data;
325
	}
326
327
	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...
328
		$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;";
329
		$result = $this->mysqli->query($req);
330
		$gyms = array();
331
		while ($data = $result->fetch_object()) {
332
			$gyms[] = $data;
333
		}
334
		return $gyms;
335
	}
336
337
	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...
338
		$gym_id = $this->mysqli->real_escape_string($_GET['gym_id']);
339
		$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	
340
			FROM fort_sightings fs
341
			LEFT JOIN forts f ON f.id = fs.fort_id
342
			JOIN gym_defenders gd ON f.id = gd.fort_id
343
			WHERE f.id ='".$gym_id."'";
344
		$result = $this->mysqli->query($req);
345
		$data = $result->fetch_object();
346
		return $data;
347
	}
348
349
	public function getGymDefenders($gym_id) {
350
		$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
351
			FROM gym_defenders 
352
			WHERE fort_id='".$gym_id."'
353
			ORDER BY cp DESC";
354
		$result = $this->mysqli->query($req);
355
		$defenders = array();
356
		while ($data = $result->fetch_object()) {
357
			$defenders[] = $data;
358
		}
359
		return $defenders;
360
	}
361
362
363
	///////////
364
	// Raids
365
	///////////
366
367
	public function getAllRaids($page) {
368
		$limit = " LIMIT ".($page * 10).",10";
369
		$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 
370
					FROM raids r 
371
					JOIN forts f ON f.id = r.fort_id 
372
					JOIN fort_sightings fs ON fs.fort_id = r.fort_id 
373
					WHERE r.time_end > UNIX_TIMESTAMP() 
374
					ORDER BY r.level DESC, r.time_battle" . $limit;
375
		$result = $this->mysqli->query($req);
376
		$raids = array();
377
		while ($data = $result->fetch_object()) {
378
			$raids[] = $data;
379
		}
380
		return $raids;
381
	}
382
383
384
	//////////////
385
	// Trainers
386
	//////////////
387
388
	public function getTrainers($trainer_name, $team, $page, $ranking) {
389
		return array(); // Waiting for Monocle to store level
390
	}
391
392
	public function getTrainerLevelCount($team_id) {
393
		$levelData = array();
394 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...
395
			if (!isset($levelData[$i])) {
396
				$levelData[$i] = 0;
397
			}
398
		}
399
		return $levelData; // Waiting for Monocle to store level
400
	}
401
402
403
	/////////
404
	// Cron
405
	/////////
406
407
	public function getPokemonCountsActive() {
408
		$req = "SELECT pokemon_id, COUNT(*) as total FROM sightings WHERE expire_timestamp >= UNIX_TIMESTAMP() GROUP BY pokemon_id";
409
		$result = $this->mysqli->query($req);
410
		$counts = array();
411
		while ($data = $result->fetch_object()) {
412
			$counts[$data->pokemon_id] = $data->total;
413
		}
414
		return $counts;
415
	}
416
417
	public function getPoekmonCountsLastDay() {
418
		$req = "SELECT pokemon_id, COUNT(*) AS spawns_last_day
419
					FROM sightings
420
					WHERE FROM_UNIXTIME(expire_timestamp) >= (SELECT FROM_UNIXTIME(MAX(expire_timestamp)) FROM sightings) - INTERVAL 1 DAY
421
					GROUP BY pokemon_id
422
				  	ORDER BY pokemon_id ASC";
423
		$result = $this->mysqli->query($req);
424
		$counts = array();
425
		while ($data = $result->fetch_object()) {
426
			$counts[$data->pokemon_id] = $data->spawns_last_day;
427
		}
428
		return $counts;
429
	}
430
431 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...
432
		$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
433
					FROM sightings
434
					WHERE pokemon_id = '".$pokemon_id."' && expire_timestamp > '".$last_update."'";
435
		$result = $this->mysqli->query($req);
436
		$data = $result->fetch_object();
437
		return $data;
438
	}
439
440 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...
441
		$where = "WHERE pokemon_id = '".$pokemon_id."' && time_battle > '".$last_update."'";
442
		$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
443
					FROM raids r
444
					JOIN forts g
445
					JOIN (SELECT COUNT(*) AS count
446
					FROM raids
447
                    " . $where."
448
                ) x 
449
				ON r.fort_id = g.id
450
                " . $where."
451
                ORDER BY time_battle DESC
452
				LIMIT 0 , 1";
453
		$result = $this->mysqli->query($req);
454
		$data = $result->fetch_object();
455
		return $data;
456
	}
457
458
	public function getCaptchaCount() {
459
		$req = " SELECT COUNT(*) as total FROM accounts WHERE captchaed IS NOT NULL AND reason IS NULL";
460
		$result = $this->mysqli->query($req);
461
		$data = $result->fetch_object();
462
		return $data;
463
	}
464
465 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...
466
		$pokemon_exclude_sql = "";
467
		if (!empty(self::$config->system->nest_exclude_pokemon)) {
468
			$pokemon_exclude_sql = "AND p.pokemon_id NOT IN (" . implode(",", self::$config->system->nest_exclude_pokemon) . ")";
469
		}
470
		$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
471
			          FROM sightings p
472
			          INNER JOIN spawnpoints s ON (p.spawn_id = s.spawn_id)
473
			          WHERE p.expire_timestamp > UNIX_TIMESTAMP() - 86400
474
			          " . $pokemon_exclude_sql . "
475
			          GROUP BY p.spawn_id, p.pokemon_id
476
			          HAVING COUNT(p.pokemon_id) >= 6
477
			          ORDER BY p.pokemon_id";
478
		$result = $this->mysqli->query($req);
479
		$nests = array();
480
		while ($data = $result->fetch_object()) {
481
			$nests[] = $data;
482
		}
483
		return $nests;
484
	}
485
486
}