Failed Conditions
Pull Request — master (#363)
by Florian
10:07
created

functions.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
########################################################################
4
// Human Time Ago
5
// @param $timestamp	=> timestamp (mandatory)
6
// @param $locales	=> locales (mandatory)
7
//
8
// Return time ago at human format (eg: 2 hours ago)
9
########################################################################
10
11
function time_ago($timestamp, $locales)
12
{
13
14
	// Set up our variables.
15
	$minute_in_seconds = 60;
16
	$hour_in_seconds   = $minute_in_seconds * 60;
17
	$day_in_seconds	   = $hour_in_seconds * 24;
18
	$week_in_seconds   = $day_in_seconds * 7;
19
	$month_in_seconds  = $day_in_seconds * 30;
20
	$year_in_seconds   = $day_in_seconds * 365;
21
22
	// current time
23
	$now = time();
24
25
	// Calculate the time difference between the current time reference point and the timestamp we're comparing.
26
	// The difference is defined negative, when in the future.
27
	$time_difference = $now - $timestamp;
28
29
	// Calculate the time ago using the smallest applicable unit.
30
	if ($time_difference < $hour_in_seconds) {
31
		$difference_value = abs(round($time_difference / $minute_in_seconds));
32
		$difference_label = 'MINUTE';
33
	} elseif ($time_difference < $day_in_seconds) {
34
		$difference_value = abs(round($time_difference / $hour_in_seconds));
35
		$difference_label = 'HOUR';
36
	} elseif ($time_difference < $week_in_seconds) {
37
		$difference_value = abs(round($time_difference / $day_in_seconds));
38
		$difference_label = 'DAY';
39
	} elseif ($time_difference < $month_in_seconds) {
40
		$difference_value = abs(round($time_difference / $week_in_seconds));
41
		$difference_label = 'WEEK';
42
	} elseif ($time_difference < $year_in_seconds) {
43
		$difference_value = abs(round($time_difference / $month_in_seconds));
44
		$difference_label = 'MONTH';
45
	} else {
46
		$difference_value = abs(round($time_difference / $year_in_seconds));
47
		$difference_label = 'YEAR';
48
	}
49
50
	// plural
51
	if ($difference_value != 1) {
52
		$difference_label = $difference_label.'S';
53
	}
54
55
	if ($time_difference <= 0) {
56
		// Present
57
		return sprintf($locales->TIME_LEFT, $difference_value.' '.$locales->$difference_label);
58
	} else {
59
		// Past
60
		return sprintf($locales->TIME_AGO, $difference_value.' '.$locales->$difference_label);
61
	}
62
}
63
64
65
########################################################################
66
// Percent calculator
67
// @param $val		=> int (mandatory)
68
// @param $val_total	=> int (mandatory)
69
//
70
// Return pourcent from total
71
########################################################################
72
73
function percent($val, $val_total)
74
{
75
	$count1 = $val_total / $val;
76
	$count2 = $count1 * 100;
77
78
	$count = number_format($count2, 0);
79
80
	return $count;
81
}
82
83
84
########################################################################
85
// File version (unix timestamp)
86
// @param $url		=> string (mandatory)
87
//
88
// Return $url with last_modified unix timestamp before suffix
89
########################################################################
90
91
function auto_ver($url)
92
{
93
	if (is_file(SYS_PATH.'/'.$url)) {
94
		$path = pathinfo($url);
95
		$ver = '.'.filemtime(SYS_PATH.'/'.$url).'.';
96
		echo $path['dirname'].'/'.preg_replace('/\.(css|js|json)$/', $ver."$1", $path['basename']);
97
	} else {
98
		echo $url;
99
	}
100
}
101
102
103
########################################################################
104
// File age in secs
105
// @param $filepath     => string (mandatory)
106
//
107
// Return file age of file in secs, PHP_INT_MAX if file doesn't exist
108
########################################################################
109
110
function file_update_ago($filepath)
111
{
112
	if (is_file($filepath)) {
113
		$filemtime = filemtime($filepath);
114
		$now = time();
115
		$diff = $now - $filemtime;
116
		return $diff;
117
	}
118
	// file doesn't exist yet!
119
	return PHP_INT_MAX;
120
}
121
122
123
########################################################################
124
// Only keep data after $timestamp in $array (compared to 'timestamp' key)
125
// @param $array     => array (mandatory)
126
// @param $timestamp => int (mandatory)
127
//
128
// Return trimmed array
129
########################################################################
130
131
function trim_stats_json($array, $timestamp)
132
{
133
	foreach ($array as $key => $value) {
134
		if ($value['timestamp'] < $timestamp) {
135
			unset($array[$key]);
136
		}
137
	}
138
	return $array;
139
}
140
141
142
########################################################################
143
// gym level from prestige value
144
// @param $prestige => int (mandatory)
145
//
146
// Return gym level
147
########################################################################
148
149
function gym_level($prestige)
150
{
151
	if ($prestige == 0) {
152
		$gym_level = 0;
153
	} elseif ($prestige < 2000) {
154
		$gym_level = 1;
155
	} elseif ($prestige < 4000) {
156
		$gym_level = 2;
157
	} elseif ($prestige < 8000) {
158
		$gym_level = 3;
159
	} elseif ($prestige < 12000) {
160
		$gym_level = 4;
161
	} elseif ($prestige < 16000) {
162
		$gym_level = 5;
163
	} elseif ($prestige < 20000) {
164
		$gym_level = 6;
165
	} elseif ($prestige < 30000) {
166
		$gym_level = 7;
167
	} elseif ($prestige < 40000) {
168
		$gym_level = 8;
169
	} elseif ($prestige < 50000) {
170
		$gym_level = 9;
171
	} else {
172
		$gym_level = 10;
173
	}
174
175
	return $gym_level;
176
}
177
178
########################################################################
179
// depth of array
180
// @param $arr     => array (mandatory)
181
//
182
// Retruns max depth of array
183
########################################################################
184
function get_depth($arr) {
185
	$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr));
186
	$depth = 0;
187
	foreach ($it as $v) {
188
		$it->getDepth() > $depth && $depth = $it->getDepth();
189
	}
190
	return $depth;
191
}
192
193
########################################################################
194
// tree for at depth
195
// @param $trees     => array (mandatory)
196
// @param $depth => int (mandatory)
197
// @param $max_pokemon => int (mandatory)
198
// @param $currentDepth => int (optional)
199
//
200
// Return all pokemon with data at a certain tree depth
201
########################################################################
202
function get_tree_at_depth($trees, $depth, $max_pokemon, $currentDepth = 0) {
203
	if ($depth == $currentDepth) { // Found depth
204
		return tree_remove_bellow($trees, $max_pokemon);
205
	} else { // Go deeper
206
		$arr = array();
207
		foreach ($trees as $temp) { // Go into all trees
208
			$tree = $temp->evolutions;
209
			$results = tree_remove_bellow(get_tree_at_depth($tree, $depth, $max_pokemon, $currentDepth + 1), $max_pokemon);
210
			$arr = tree_check_array($results, $arr, $depth - $currentDepth == 1);
211
		}
212
		return $arr;
213
	}
214
}
215
216
########################################################################
217
// used in get_tree_at_depth
218
########################################################################
219
function tree_check_array($array_check, $array_add, $correct_arrow) {
220
	$count = count($array_check);
221
	$i = 0;
222
	if (!is_null($array_check)) { // check if exists
223
		foreach ($array_check as $res) { // Check if above, equal or bellow center
224
			if ($count != 1 && $correct_arrow) { // only add arrow once
225
				$num = $i / ($count - 1);
226
				if ($num < 0.5) {
227
					$res->array_sufix = "_up";
228
				} elseif ($num > 0.5) {
229
					$res->array_sufix = "_down";
230
				} else {
231
					$res->array_sufix = "";
232
				}
233
			} else if (!isset($res->array_sufix)) {
234
				$res->array_sufix = "";
235
			}
236
			$array_add[] = $res;
237
			$i++;
238
		}
239
	}
240
	return $array_add;
241
}
242
243
########################################################################
244
// used in get_tree_at_depth
245
########################################################################
246
function tree_remove_bellow($tree, $max_pokemon)
247
{
248
	if (is_null($tree)) {
249
		return null;
250
	}
251
	$arr = array();
252
	foreach ($tree as $item) { // Check if above, equal or bellow center
253
		if ($item->id <= $max_pokemon) {
254
			$arr[] = $item;
255
		}
256
	}
257
	return $arr;
258
}
259
260
########################################################################
261
// generation
262
########################################################################
263
function generation($id)
264
{
265
	switch ($id) {
266
		case ($id >= 1 && $id <= 151):
267
			return [1, "Kanto"];
268
		case ($id >= 152 && $id <= 251):
269
			return [2, "Johto"];
270
		case ($id >= 252 && $id <= 386):
271
			return [3, "Hoenn"];
272
		case ($id >= 387 && $id <= 493):
273
			return [4, "Sinnoh"];
274
		case ($id >= 494 && $id <= 649):
275
			return [5, "Teselia"];
276
		case ($id >= 650 && $id <= 721):
277
			return [6, "Kalos"];
278
		case ($id >= 722 && $id <= 802):
279
			return [7, "Alola"];
280
	}
281
}
282
283
########################################################################
284
// check if point is inside porygon
285
########################################################################
286
function pointIsInsidePolygon($lat, $lng, $geos, $bounds) {
287
	if ($lat >= $bounds["minlat"] && $lat <= $bounds["maxlat"] && $lng >= $bounds["minlon"] && $lng <= $bounds["maxlon"]) {
0 ignored issues
show
Blank line found at start of control structure
Loading history...
288
289
		$intersections = 0;
290
		$geos_count = count($geos);
291
292
		for ($i = 1; $i < $geos_count; $i++) {
293
			$geo1 = $geos[$i - 1];
294
			$geo2 = $geos[$i];
295
			if ($geo1['lng'] == $lng && $geo1['lat'] == $lat) { // On one of the coords
296
				return true;
297
			}
298
			if ($geo1['lng'] == $geo2['lng'] and $geo1['lng'] == $lng and $lat > min($geo1['lat'], $geo2['lat']) and $lat < max($geo1['lat'], $geo2['lat'])) { // Check if point is on an horizontal polygon boundary
299
				return true;
300
			}
301
			if ($lng > min($geo1['lng'], $geo2['lng']) and $lng <= max($geo1['lng'], $geo2['lng']) and $lat <= max($geo1['lat'], $geo2['lat']) and $geo1['lng'] != $geo2['lng']) {
302
				$xinters = ($lng - $geo1['lng']) * ($geo2['lat'] - $geo1['lat']) / ($geo2['lng'] - $geo1['lng']) + $geo1['lat'];
303
				if ($xinters == $lat) { // Check if point is on the polygon boundary (other than horizontal)
304
					return true;
305
				}
306
				if ($geo1['lat'] == $geo2['lat'] || $lat <= $xinters) {
307
					$intersections++;
308
				}
309
			}
310
		}
311
		// If the number of edges we passed through is odd, then it's in the polygon.
312
		if ($intersections % 2 != 0) {
0 ignored issues
show
The if-else statement can be simplified to return $intersections % 2 != 0;.
Loading history...
313
			return true;
314
		} else {
315
			return false;
316
		}
317
	} else {
318
		return false; // outside bounds
319
	}
320
}
321
322
########################################################################
323
// check if $boundsIn is inside (or equal to) $boundsOut
324
########################################################################
325
function polyIsInsidePolygon($geoIn, $boundsIn, $geoOut, $boundsOut) {
326
	if ($boundsIn["minlat"] >= $boundsOut["minlat"] && $boundsIn["maxlat"] <= $boundsOut["maxlat"] && $boundsIn["minlon"] >= $boundsOut["minlon"] && $boundsIn["maxlon"] <= $boundsOut["maxlon"]) {
327
		$insideCount = 0;
328
		foreach ($geoIn as $coord) {
329
			if (pointIsInsidePolygon($coord["lat"], $coord["lng"], $geoOut, $boundsOut)) {
330
				$insideCount++;
331
			}
332
		}
333
		return $insideCount / count($geoIn) >= 0.95;
334
	} else {
335
		return false; // bounds outside
336
	}
337
}
338
339
########################################################################
340
// compine outer ways into porygon
341
########################################################################
342
function combineOuter($outers) {
343
	$polygons = array();
344
	$index = 0;
345
	$count = 0;
346
	$maxCount = count($outers);
347
	while (count($outers) != 0 && $count <= $maxCount) {
348
		$count++;
349
		foreach ($outers as $key => $outer) {
350
			if (!isset($polygons[$index])) {
351
				$polygons[$index] = $outer;
352
				unset($outers[$key]);
353
			} else {
354
				$firstEle = $outer[0];
355
				$lastEle = $outer[count($outer) - 1];
356
				$firstElePoly = $polygons[$index][0];
357
				$lastElePoly = $polygons[$index][count($polygons[$index]) - 1];
358
				if ($firstEle == $lastElePoly) {
359
					$polygons[$index] = array_merge($polygons[$index], $outer);
360
					unset($outers[$key]);
361
				} else if ($lastEle == $lastElePoly) {
362
					$polygons[$index] = array_merge($polygons[$index], array_reverse($outer));
363
					unset($outers[$key]);
364
				} else if ($firstEle == $firstElePoly) {
365
					$polygons[$index] = array_merge(array_reverse($outer), $polygons[$index]);
366
					unset($outers[$key]);
367
				} else if ($lastEle == $firstElePoly) {
368
					$polygons[$index] = array_merge($outer, $polygons[$index]);
369
					unset($outers[$key]);
370
				} 
371
			}
372
			
373
			$firstElePoly = $polygons[$index][0];
374
			$lastElePoly = $polygons[$index][count($polygons[$index]) - 1];
375
			if ($firstElePoly == $lastElePoly){
376
				$index++;
377
			}
378
		}
379
	}
380
	return $polygons;
381
}
382