/
LVPPlayer.php
311 lines (282 loc) · 7.87 KB
/
LVPPlayer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
/**
* LVPEchoHandler module for Nuwani v2
*
* @author Dik Grapendaal <dik@sa-mp.nl>
*/
class LVPPlayer implements ArrayAccess, Serializable {
/**
* The ingame ID of this player.
*
* @var integer
*/
private $m_nId;
/**
* The ID of this player in the database. This is referred to as the
* profile ID in the LVP gamemode, so I'll use that as well. If 0, it
* means this player is unregistered.
*
* @var integer
*/
private $m_nProfileId;
/**
* The ingame nickname of this player.
*
* @var string
*/
private $m_sNickname;
/**
* The official level of this player.
*
* @var integer
*/
private $m_nLevel;
/**
* The temporary level of this player ingame. This is 0 if it's no
* different from the official level.
*
* @var integer
*/
private $m_nTempLevel;
/**
* The current IP address of the player.
*
* @var string
*/
private $m_sIp;
/**
* The timestamp of the moment this player joined the server.
*
* @var integer
*/
private $m_nJoinTime;
/**
* The timestamp of the moment the player logged in. This stays 0 when
* he doesn't log in.
*
* @var integer
*/
private $m_nLogInTime;
/**
* This array contains all the keys supported by the ArrayAccess
* methods. This array is also used to serialize all information
* available in this class, so that we load it up again at a later time.
*
* @var array
*/
private static $s_aInfoKeys;
/**
* This class represents a single player on the LVP server. It keeps
* track of random stats as well as some less random information, like
* the ID and nickname of the player.
*
* @param integer $nId The ID of this new player.
*/
public function __construct($nId) {
$this->setInfoKeys();
// Set all variables to their defaults.
foreach (self::$s_aInfoKeys as $sKey) {
unset($this[$sKey]);
}
$this['ID'] = $nId;
}
/**
* This method will set the all important information keys array, where
* the serialize functionality works with, as well as the ArrayAccess
* functionality.
*/
private function setInfoKeys() {
if (!empty(self::$s_aInfoKeys)) {
return;
}
self::$s_aInfoKeys = array(
'ID', 'ProfileID', 'Nickname', 'Level', 'TempLevel',
'IP', 'JoinTime', 'LogInTime',
);
}
/**
* This method will serialize all the information in this object into a
* single array. Updating this method is not necessary when new
* properties are added to this object in the future. Only the InfoKeys
* array should be updated with all the properties.
*
* @return string
*/
public function serialize() {
$aInformation = array();
foreach (self::$s_aInfoKeys as $sKey) {
$aInformation[$sKey] = $this[$sKey];
}
return serialize($aInformation);
}
/**
* This method will unserialize the date that was serialized earlier in
* the serialize () method, and set the properties to the values as
* specified in the serialized data.
*
* @param string $sSerialized The serialized data.
*/
public function unserialize($sSerialized) {
$this->setInfoKeys();
$aInformation = unserialize($sSerialized);
foreach ($aInformation as $sKey => $mValue) {
$this[$sKey] = $mValue;
}
}
/**
* LVP's central database contains a shed load of information. Using
* this method, we'll fetch whatever we may need from there and store it
* in some variable until it's needed. At the moment this method only
* fetches the crew status of a user.
*
* @param LVPDatabase $database Reference to the database connection.
* @param integer $profileId The unique ID of the user in the database.
* @return boolean
*/
public function fetchInformation($database, $profileId) {
$this->m_nProfileId = $profileId;
$pResult = $database->query(
'SELECT
u.level, u.is_developer
FROM
lvp_mainserver.users u
WHERE
u.user_id = ' . (int) $profileId);
if ($pResult !== false && $pResult->num_rows != 0) {
$aInformation = $pResult->fetch_assoc();
$this['Level'] = LVPCrewHandler::translateGamemodeLevel($aInformation['level'], $aInformation['is_developer']);
$pResult->free();
}
}
/**
* The ArrayAccess interface provides us with the ability to use an
* instance of this class as an array. The operations executed with that
* syntax, are redirected to these methods. I use this for the LVPPlayer
* class because then I can group all the getters, setters and unsetters
* into four methods.
*
* This method gets called when an isset () call has been performed on
* an array index of this class.
*
* @param mixed $mKey The key used when getting something from this class using array syntax.
* @return boolean
*/
public function offsetExists($mKey) {
return in_array($mKey, self::$s_aInfoKeys);
}
/**
* The ArrayAccess interface provides us with the ability to use an
* instance of this class as an array. The operations executed with that
* syntax, are redirected to these methods. I use this for the LVPPlayer
* class because then I can group all the getters, setters and unsetters
* into four methods.
*
* Invokation of this method will occur when one wants to get a value
* from this class using the array syntax.
*
* @param mixed $mKey The key used when getting something from this class using array syntax.
* @return mixed
*/
public function offsetGet($mKey) {
switch ($mKey) {
case 'ID':
return $this->m_nId;
case 'ProfileID':
return $this->m_nProfileId;
case 'Nickname':
return $this->m_sNickname;
case 'Level':
return $this->m_nLevel;
case 'TempLevel':
return $this->m_nTempLevel;
case 'IP':
return $this->m_sIp;
case 'JoinTime':
return $this->m_nJoinTime;
case 'LogInTime':
return $this->m_nLogInTime;
}
return null;
}
/**
* The ArrayAccess interface provides us with the ability to use an
* instance of this class as an array. The operations executed with that
* syntax, are redirected to these methods. I use this for the LVPPlayer
* class because then I can group all the getters, setters and unsetters
* into four methods.
*
* This method will be invoked when an index on this class has been
* set using the array syntax.
*
* @param mixed $mKey The key used when getting something from this class using array syntax.
* @param mixed $mKey The new value for the specified key.
*/
public function offsetSet($mKey, $mValue) {
switch ($mKey) {
case 'ID':
$this->m_nId = (int) $mValue;
break;
case 'ProfileID':
$this->m_nProfileId = (int) $mValue;
break;
case 'Nickname':
$this->m_sNickname = (string) $mValue;
break;
case 'Level':
$this->m_nLevel = (int) $mValue;
break;
case 'TempLevel':
$this->m_nTempLevel = (int) $mValue;
break;
case 'IP':
$this->m_sIp = (string) $mValue;
break;
case 'JoinTime':
$this->m_nJoinTime = (int) $mValue;
break;
case 'LogInTime':
$this->m_nLogInTime = (int) $mValue;
break;
}
}
/**
* The ArrayAccess interface provides us with the ability to use an
* instance of this class as an array. The operations executed with that
* syntax, are redirected to these methods. I use this for the LVPPlayer
* class because then I can group all the getters, setters and unsetters
* into four methods.
*
* This method gets called when an unset () call has been performed on
* an array index of this class.
*
* @param mixed $mKey The key used when getting something from this class using array syntax.
*/
public function offsetUnset($mKey) {
switch ($mKey) {
case 'ID':
$this->m_nId = 0;
break;
case 'ProfileID':
$this->m_nProfileId = 0;
break;
case 'Nickname':
$this->m_sNickname = '';
break;
case 'Level':
$this->m_nLevel = 0;
break;
case 'TempLevel':
$this->m_nTempLevel = 0;
break;
case 'IP':
$this->m_sIp = '';
break;
case 'JoinTime':
$this->m_nJoinTime = 0;
break;
case 'LogInTime':
$this->m_nLogInTime = 0;
break;
}
}
}