1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Burthorpe\Runescape; |
4
|
|
|
|
5
|
|
|
class Common |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Checks if the given string is a valid display name |
9
|
|
|
* |
10
|
|
|
* @param string $rsn |
11
|
|
|
* @return bool |
12
|
|
|
*/ |
13
|
7 |
|
public function validateDisplayName($rsn) |
14
|
|
|
{ |
15
|
7 |
|
return (bool) preg_match('/^[a-z0-9\-_ ]{1,12}$/i', $rsn); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Expands a short-hand number to its full value |
20
|
|
|
* |
21
|
|
|
* @param string $number |
22
|
|
|
* @return float |
23
|
|
|
*/ |
24
|
1 |
|
public function expandNumber($number) |
25
|
|
|
{ |
26
|
1 |
|
switch (strtoupper(substr($number, -1))) { |
27
|
1 |
|
case 'B': |
28
|
1 |
|
$multiplier = 1000000000; |
29
|
1 |
|
break; |
30
|
1 |
|
case 'M': |
31
|
1 |
|
$multiplier = 1000000; |
32
|
1 |
|
break; |
33
|
1 |
|
case 'K': |
34
|
1 |
|
$multiplier = 1000; |
35
|
1 |
|
break; |
36
|
1 |
|
default: |
37
|
1 |
|
$multiplier = 1; |
38
|
1 |
|
} |
39
|
|
|
|
40
|
1 |
|
return (int) intval($number) * $multiplier; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Compact a number into short-hand |
45
|
|
|
* |
46
|
|
|
* @param int $number |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
1 |
|
public function shortenNumber($number) |
50
|
|
|
{ |
51
|
1 |
|
$abbr = [9 => 'B', 6 => 'M', 3 => 'K']; |
52
|
|
|
|
53
|
1 |
|
foreach ($abbr as $exponent => $suffix) { |
54
|
1 |
|
if ($number >= pow(10, $exponent)) { |
55
|
1 |
|
return intval($number / pow(10, $exponent)) . $suffix; |
56
|
|
|
} |
57
|
1 |
|
} |
58
|
|
|
|
59
|
1 |
|
return $number; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Calculate a level with the give amount of experience |
64
|
|
|
* |
65
|
|
|
* @param int $xp |
66
|
|
|
* @return int |
67
|
|
|
*/ |
68
|
1 |
|
public function xpTolevel($xp) |
69
|
|
|
{ |
70
|
1 |
|
$modifier = 0; |
71
|
|
|
|
72
|
1 |
|
for ($i = 1; $i <= 126; $i++) { |
73
|
1 |
|
$modifier += floor($i + 300 * pow(2, ($i / 7))); |
74
|
1 |
|
$level = floor($modifier / 4); |
75
|
|
|
|
76
|
1 |
|
if ($xp < $level) { |
77
|
1 |
|
return $i; |
78
|
|
|
} |
79
|
1 |
|
} |
80
|
|
|
|
81
|
|
|
// Return the maximum possible level |
82
|
1 |
|
return 126; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Calculates the minimum experience needed for the given level |
87
|
|
|
* |
88
|
|
|
* @param int $level |
89
|
|
|
* @return int |
90
|
|
|
*/ |
91
|
1 |
|
public function levelToXp($level) |
92
|
|
|
{ |
93
|
1 |
|
$xp = 0; |
94
|
|
|
|
95
|
1 |
|
for ($i = 1; $i < $level; $i++) { |
96
|
1 |
|
$xp += floor($i + 300 * pow(2, ($i / 7))); |
97
|
1 |
|
} |
98
|
|
|
|
99
|
1 |
|
$xp = floor($xp / 4); |
100
|
|
|
|
101
|
|
|
// Check if our value is above 200m, if so return 200m, otherwise our value |
102
|
1 |
|
return ($xp > 200000000 ? 200000000 : $xp); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|