Failed Conditions
Pull Request — master (#363)
by Florian
08:53 queued 13s
created

functions.php (1 issue)

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) {
287
	
288
	$intersections = 0;
289
	$geos_count = count($geos);
290
291
	for ($i=1; $i < $geos_count; $i++) {
292
		$geo1 = $geos[$i-1];
293
		$geo2 = $geos[$i];
294
		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
295
			return "boundary";
296
		}
297
		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']) {
298
			$xinters = ($lng - $geo1['lng']) * ($geo2['lat'] - $geo1['lat']) / ($geo2['lng'] - $geo1['lng']) + $geo1['lat'];
299
			if ($xinters == $lat) { // Check if point is on the polygon boundary (other than horizontal)
300
				return "boundary";
301
			}
302
			if ($geo1['lat'] == $geo2['lat'] || $lat <= $xinters) {
303
				$intersections++;
304
			}
305
		}
306
	}
307
	// If the number of edges we passed through is odd, then it's in the polygon. 
308
	if ($intersections % 2 != 0) {
0 ignored issues
show
The if-else statement can be simplified to return $intersections % 2 != 0;.
Loading history...
309
		return true;
310
	} else {
311
		return false;
312
	}
313
}
314
315
########################################################################
316
// compine outer ways into porygon
317
########################################################################
318
function combineOuter($outers) {
319
	$polygon = array();
320
	foreach ($outers as $key => $outer) {
321
		if ($key == 0) {
322
			$polygon = $outer;
323
		} else {
324
			$firstEle = $outer[0];
325
			$lastEle = $outer[count($outer) - 1];
326
			$lastElePoly = $polygon[count($polygon) - 1];
327
			if ($firstEle == $lastElePoly) {
328
				$polygon = array_merge($polygon, $outer);
329
				break;
330
			} else if ($lastEle == $lastElePoly) {
331
				$polygon = array_merge($polygon, array_reverse($outer));
332
				break;
333
			}
334
		}
335
	}
336
	return $polygon;
337
}
338