1
|
|
|
<?php |
2
|
|
|
namespace Xetaravel\Utility; |
3
|
|
|
|
4
|
|
|
use Carbon\Carbon; |
5
|
|
|
|
6
|
|
|
class UserUtility |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* The array containing the XP needed for each level. |
10
|
|
|
* |
11
|
|
|
* PostWasSolvedEvent::class => 120, |
12
|
|
|
* ConversationWasCreatedEvent::class => 90, |
13
|
|
|
* PostWasCreatedEvent::class => 75 |
14
|
|
|
* |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected static $levels = [ |
18
|
|
|
0, //0 |
19
|
|
|
400, //1 |
20
|
|
|
800, //2 |
21
|
|
|
1200, //3 |
22
|
|
|
1600, //4 |
23
|
|
|
2000, //5 |
24
|
|
|
2400, //6 |
25
|
|
|
2800, //7 |
26
|
|
|
3200, //8 |
27
|
|
|
3600, //9 |
28
|
|
|
4000, //10 |
29
|
|
|
4400, //11 |
30
|
|
|
4800, //12 |
31
|
|
|
5200, //13 |
32
|
|
|
5600, //14 |
33
|
|
|
6000, //15 |
34
|
|
|
6400, //16 |
35
|
|
|
6800, //17 |
36
|
|
|
7200, //18 |
37
|
|
|
7600, //19 |
38
|
|
|
8000, //20 |
39
|
|
|
8400, //21 |
40
|
|
|
8800, //22 |
41
|
|
|
9200, //23 |
42
|
|
|
9600, //24 |
43
|
|
|
10000, //25 |
44
|
|
|
10400, //26 |
45
|
|
|
10800, //27 |
46
|
|
|
11200, //28 |
47
|
|
|
11600, //29 |
48
|
|
|
12000, //30 |
49
|
|
|
12400, //31 |
50
|
|
|
12800, //32 |
51
|
|
|
13200, //33 |
52
|
|
|
13600, //34 |
53
|
|
|
14000, //35 |
54
|
|
|
14400, //36 |
55
|
|
|
14800, //37 |
56
|
|
|
15200, //38 |
57
|
|
|
15600, //39 |
58
|
|
|
16000, //40 |
59
|
|
|
16400, //41 |
60
|
|
|
16800, //42 |
61
|
|
|
17200, //43 |
62
|
|
|
17600, //44 |
63
|
|
|
18000, //45 |
64
|
|
|
18400, //46 |
65
|
|
|
18800, //47 |
66
|
|
|
19200, //48 |
67
|
|
|
19600, //49 |
68
|
|
|
20000 //50 |
69
|
|
|
]; |
70
|
|
|
|
71
|
|
|
public static function getLevel(int $userXP) |
72
|
|
|
{ |
73
|
|
|
$infos = [ |
74
|
|
|
'previousLevelExperience' => 0, |
75
|
|
|
'previousLevel' => 0, |
76
|
|
|
'currentLevel' => 0, |
77
|
|
|
'currentLevelExperience' => 0, |
78
|
|
|
'currentUserExperience' => 0, |
79
|
|
|
'nextLevel' => 1, |
80
|
|
|
'experienceNeededNextLevel' => 400, |
81
|
|
|
'nextLevelExperience' => 400, |
82
|
|
|
'matchExactXPLevel' => false, |
83
|
|
|
'maxLevel' => false |
84
|
|
|
]; |
85
|
|
|
|
86
|
|
|
if ($userXP == 0) { |
87
|
|
|
return $infos; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
for ($i = 0; $i < count(static::$levels); $i++) { |
|
|
|
|
91
|
|
|
// The XP of the user match the exact XP of the rank and there's another rank after this one. |
92
|
|
|
if ($userXP == static::$levels[$i] && isset(static::$levels[$i + 1])) { |
93
|
|
|
return array_merge($infos, [ |
94
|
|
|
'previousLevelExperience' => static::$levels[$i - 1], |
95
|
|
|
'previousLevel' => $i - 1, |
96
|
|
|
'currentLevel' => $i, |
97
|
|
|
'currentLevelExperience' => static::$levels[$i], |
98
|
|
|
'currentUserExperience' => $userXP, |
99
|
|
|
'nextLevel' => $i + 1, |
100
|
|
|
'experienceNeededNextLevel' => static::$levels[$i + 1] - $userXP, |
101
|
|
|
'nextLevelExperience' => static::$levels[$i + 1], |
102
|
|
|
'matchExactXPLevel' => true |
103
|
|
|
]); |
104
|
|
|
} else { |
105
|
|
|
// If there's another rank after this one and the user XP is higher than the current rank. |
106
|
|
|
if (isset(static::$levels[$i + 1]) && $userXP > static::$levels[$i]) { |
107
|
|
|
// If the user XP is higher than the current rank but lower than the next rank. |
108
|
|
|
if ($userXP > static::$levels[$i] && $userXP < static::$levels[$i + 1]) { |
109
|
|
|
return array_merge($infos, [ |
110
|
|
|
'previousLevelExperience' => static::$levels[$i], |
111
|
|
|
'previousLevel' => $i == 0 ? 0 : $i - 1, |
112
|
|
|
'currentLevel' => $i, |
113
|
|
|
'currentLevelExperience' => static::$levels[$i], |
114
|
|
|
'currentUserExperience' => $userXP, |
115
|
|
|
'nextLevel' => $i + 1, |
116
|
|
|
'experienceNeededNextLevel' => static::$levels[$i + 1] - $userXP, |
117
|
|
|
'nextLevelExperience' => static::$levels[$i + 1] |
118
|
|
|
]); |
119
|
|
|
} |
120
|
|
|
} else { |
121
|
|
|
// The user has reached the max lvl |
122
|
|
|
return array_merge($infos, [ |
123
|
|
|
'previousLevelExperience' => static::$levels[$i], |
124
|
|
|
'previousLevel' => $i == 0 ? 0 : $i - 1, |
125
|
|
|
'currentLevel' => $i, |
126
|
|
|
'currentLevelExperience' => static::$levels[$i], |
127
|
|
|
'currentUserExperience' => $userXP, |
128
|
|
|
'nextLevel' => 0, |
129
|
|
|
'experienceNeededNextLevel' => 0, |
130
|
|
|
'nextLevelExperience' => 0, |
131
|
|
|
'matchExactXPLevel' => (static::$levels[$i] - $userXP) == 0 ? true : false, |
132
|
|
|
'maxLevel' => true |
133
|
|
|
]); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* The prefix of the background images. |
141
|
|
|
* |
142
|
|
|
* @var string |
143
|
|
|
*/ |
144
|
|
|
protected static $prefix = 'images/profile/bg_profile_'; |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* The extension of the background images. |
148
|
|
|
* |
149
|
|
|
* @var string |
150
|
|
|
*/ |
151
|
|
|
protected static $extension = '.jpg'; |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* The days references with the images name. |
155
|
|
|
* |
156
|
|
|
* @var array |
157
|
|
|
*/ |
158
|
|
|
protected static $daysReferences = [ |
159
|
|
|
'1' => '1', |
160
|
|
|
'2' => '2', |
161
|
|
|
'3' => '3', |
162
|
|
|
'4' => '4', |
163
|
|
|
'5' => '5', |
164
|
|
|
'6' => '6', |
165
|
|
|
'7' => '7', |
166
|
|
|
'8' => '8', |
167
|
|
|
'9' => '9', |
168
|
|
|
'10' => '10', |
169
|
|
|
'11' => '11', |
170
|
|
|
'12' => '12', |
171
|
|
|
'13' => '13', |
172
|
|
|
'14' => '14', |
173
|
|
|
'15' => '15', |
174
|
|
|
'16' => '1', |
175
|
|
|
'17' => '2', |
176
|
|
|
'18' => '3', |
177
|
|
|
'19' => '4', |
178
|
|
|
'20' => '5', |
179
|
|
|
'21' => '6', |
180
|
|
|
'22' => '7', |
181
|
|
|
'23' => '8', |
182
|
|
|
'24' => '9', |
183
|
|
|
'25' => '10', |
184
|
|
|
'26' => '11', |
185
|
|
|
'27' => '12', |
186
|
|
|
'28' => '13', |
187
|
|
|
'29' => '14', |
188
|
|
|
'30' => '15', |
189
|
|
|
'31' => '1' |
190
|
|
|
]; |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Get the profile background by the current day. |
194
|
|
|
* |
195
|
|
|
* @return string |
196
|
|
|
*/ |
197
|
|
|
public static function getProfileBackground() |
198
|
|
|
{ |
199
|
|
|
$now = Carbon::now(); |
200
|
|
|
$day = $now->day; |
201
|
|
|
|
202
|
|
|
if (isset(static::$daysReferences[$day])) { |
203
|
|
|
return static::$prefix . static::$daysReferences[$day] . static::$extension; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return static::$prefix . '1' . static::$extension; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: