· 5 years ago · Mar 09, 2020, 07:40 PM
1
2 1
3 2
4 3
5 4
6 5
7 6
8 7
9 8
10 9
11 10
12 11
13 12
14 13
15 14
16 15
17 16
18 17
19 18
20 19
21 20
22 21
23 22
24 23
25 24
26 25
27 26
28 27
29 28
30 29
31 30
32 31
33 32
34 33
35 34
36 35
37 36
38 37
39 38
40 39
41 40
42 41
43 42
44 43
45 44
46 45
47 46
48 47
49 48
50 49
51 50
52 51
53 52
54 53
55 54
56 55
57 56
58 57
59 58
60 59
61 60
62 61
63 62
64 63
65 64
66 65
67 66
68 67
69 68
70 69
71 70
72 71
73 72
74 73
75 74
76 75
77 76
78 77
79 78
80 79
81 80
82 81
83 82
84 83
85 84
86 85
87 86
88 87
89 88
90 89
91 90
92 91
93 92
94 93
95 94
96 95
97 96
98 97
99 98
100 99
101100
102101
103102
104103
105104
106105
107106
108107
109108
110109
111110
112111
113112
114113
115114
116115
117116
118117
119118
120119
121120
122121
123122
124123
125124
126125
127126
128127
129128
130129
131130
132131
133132
134133
135134
136135
137136
138137
139138
140139
141140
142141
143142
144143
145144
146145
147146
148147
149148
150149
151150
152151
153152
154153
155154
156155
157156
158157
159158
160159
161160
162161
163162
164163
165164
166165
167166
168167
169168
170169
171170
172171
173172
174173
175174
176175
177176
178177
179178
180179
181180
182181
183182
184183
185184
186185
187186
188187
189188
190189
191190
192191
193192
194193
195194
196195
197196
198197
199198
200199
201200
202201
203202
204203
205204
206205
207206
208207
209208
210209
211210
212211
213212
214213
215214
216215
217216
218217
219218
220219
221220
222221
223222
224223
225224
226225
227226
228227
229228
230229
231230
232231
233232
234233
235234
236235
237236
238237
239238
240239
241240
242241
243242
244243
245244
246245
247246
248247
249248
250249
251250
252251
253252
254253
255254
256255
257256
258257
259258
260259
261260
262261
263262
264263
265264
266265
267266
268267
269268
270269
271270
272271
273272
274273
275274
276275
277276
278277
279278
280279
281280
282281
283282
284283
285local htmlEntities = module("lib/htmlEntities")
286
287local cfg = module("cfg/identity")
288local lang = vRP.lang
289
290local sanitizes = module("cfg/sanitizes")
291
292-- this module describe the identity system
293
294-- init sql
295MySQL.createCommand("vRP/identity_tables", [[
296CREATE TABLE IF NOT EXISTS vrp_user_identities(
297 user_id INTEGER,
298 registration VARCHAR(20),
299 phone VARCHAR(20),
300 firstname VARCHAR(50),
301 name VARCHAR(50),
302 age INTEGER,
303 CONSTRAINT pk_user_identities PRIMARY KEY(user_id),
304 CONSTRAINT fk_user_identities_users FOREIGN KEY(user_id) REFERENCES vrp_users(id) ON DELETE CASCADE,
305 INDEX(registration),
306 INDEX(phone)
307);
308]])
309
310MySQL.createCommand("vRP/get_user_identity","SELECT * FROM vrp_user_identities WHERE user_id = @user_id")
311MySQL.createCommand("vRP/init_user_identity","INSERT IGNORE INTO vrp_user_identities(user_id,registration,phone,firstname,name,age) VALUES(@user_id,@registration,@phone,@firstname,@name,@age)")
312MySQL.createCommand("vRP/update_user_identity","UPDATE vrp_user_identities SET firstname = @firstname, name = @name, age = @age, registration = @registration, phone = @phone WHERE user_id = @user_id")
313MySQL.createCommand("vRP/get_userbyreg","SELECT user_id FROM vrp_user_identities WHERE registration = @registration")
314MySQL.createCommand("vRP/get_userbyphone","SELECT user_id FROM vrp_user_identities WHERE phone = @phone")
315MySQL.createCommand("vRP/dmv_search_identity", "SELECT * FROM vrp_users WHERE id = @id")
316
317-- init
318MySQL.execute("vRP/identity_tables")
319
320-- api
321
322-- cbreturn user identity
323function vRP.getUserIdentity(user_id, cbr)
324 local task = Task(cbr)
325
326 MySQL.query("vRP/get_user_identity", {user_id = user_id}, function(rows, affected)
327 task({rows[1]})
328 end)
329end
330
331function vRP.getDriversLicense(user_id, cbr)
332 local task = Task(cbr)
333
334 MySQL.query("vRP/dmv_search_identity", {id = user_id}, function(rows, affected)
335 if #rows > 0 then
336 task({rows[1]})
337 else
338 task()
339 end
340 end)
341end
342
343-- cbreturn user_id by registration or nil
344function vRP.getUserByRegistration(registration, cbr)
345 local task = Task(cbr)
346
347 MySQL.query("vRP/get_userbyreg", {registration = registration or ""}, function(rows, affected)
348 if #rows > 0 then
349 task({rows[1].user_id})
350 else
351 task()
352 end
353 end)
354end
355
356-- cbreturn user_id by phone or nil
357function vRP.getUserByPhone(phone, cbr)
358 local task = Task(cbr)
359
360 MySQL.query("vRP/get_userbyphone", {phone = phone or ""}, function(rows, affected)
361 if #rows > 0 then
362 task({rows[1].user_id})
363 else
364 task()
365 end
366 end)
367end
368
369function vRP.generateStringNumber(format) -- (ex: DDDLLL, D => digit, L => letter)
370 local abyte = string.byte("A")
371 local zbyte = string.byte("0")
372
373 local number = ""
374 for i=1,#format do
375 local char = string.sub(format, i,i)
376 if char == "D" then number = number..string.char(zbyte+math.random(0,9))
377 elseif char == "L" then number = number..string.char(abyte+math.random(0,25))
378 else number = number..char end
379 end
380
381 return number
382end
383
384-- cbreturn a unique registration number
385function vRP.generateRegistrationNumber(cbr)
386 local task = Task(cbr)
387
388 local function search()
389 -- generate registration number
390 local registration = vRP.generateStringNumber("DDDLLL")
391 vRP.getUserByRegistration(registration, function(user_id)
392 if user_id ~= nil then
393 search() -- continue generation
394 else
395 task({registration})
396 end
397 end)
398 end
399
400 search()
401end
402
403-- cbreturn a unique phone number (0DDDDD, D => digit)
404function vRP.generatePhoneNumber(cbr)
405 local task = Task(cbr)
406
407 local function search()
408 -- generate phone number
409 local phone = vRP.generateStringNumber(cfg.phone_format)
410 vRP.getUserByPhone(phone, function(user_id)
411 if user_id ~= nil then
412 search() -- continue generation
413 else
414 task({phone})
415 end
416 end)
417 end
418
419 search()
420end
421
422-- events, init user identity at connection
423AddEventHandler("vRP:playerJoin",function(user_id,source,name,last_login)
424 vRP.getUserIdentity(user_id, function(identity)
425 if identity == nil then
426 vRP.generateRegistrationNumber(function(registration)
427 vRP.generatePhoneNumber(function(phone)
428 MySQL.execute("vRP/init_user_identity", {
429 user_id = user_id,
430 registration = registration,
431 phone = phone,
432 firstname = cfg.random_first_names[math.random(1,#cfg.random_first_names)],
433 name = cfg.random_last_names[math.random(1,#cfg.random_last_names)],
434 age = math.random(25,40)
435 })
436 end)
437 end)
438 end
439 end)
440end)
441
442-- city hall menu
443
444local cityhall_menu = {name=lang.cityhall.title(),css={top="75px", header_color="rgba(0,125,255,0.75)"}}
445
446local function ch_identity(player,choice)
447 local user_id = vRP.getUserId(player)
448 if user_id ~= nil then
449 vRP.prompt(player,lang.cityhall.identity.prompt_firstname(),"",function(player,firstname)
450 if string.len(firstname) >= 2 and string.len(firstname) < 50 then
451 firstname = sanitizeString(firstname, sanitizes.name[1], sanitizes.name[2])
452 vRP.prompt(player,lang.cityhall.identity.prompt_name(),"",function(player,name)
453 if string.len(name) >= 2 and string.len(name) < 50 then
454 name = sanitizeString(name, sanitizes.name[1], sanitizes.name[2])
455 vRP.prompt(player,lang.cityhall.identity.prompt_age(),"",function(player,age)
456 age = parseInt(age)
457 if age >= 16 and age <= 75 then
458 if vRP.tryFullPayment(user_id,cfg.new_identity_cost) then
459 vRP.generateRegistrationNumber(function(registration)
460 vRP.generatePhoneNumber(function(phone)
461
462 MySQL.execute("vRP/update_user_identity", {
463 user_id = user_id,
464 firstname = firstname,
465 name = name,
466 age = age,
467 registration = registration,
468 phone = phone
469 })
470
471 TriggerEvent('scoreboard:update', user_id)
472
473 -- update client registration
474 vRPclient.setRegistrationNumber(player,{registration})
475 vRPclient.notify(player,{lang.money.paid({cfg.new_identity_cost})})
476 end)
477 end)
478 else
479 vRPclient.notify(player,{lang.money.not_enough()})
480 end
481 else
482 vRPclient.notify(player,{lang.common.invalid_value()})
483 end
484 end)
485 else
486 vRPclient.notify(player,{lang.common.invalid_value()})
487 end
488 end)
489 else
490 vRPclient.notify(player,{lang.common.invalid_value()})
491 end
492 end)
493 end
494end
495
496cityhall_menu[lang.cityhall.identity.title()] = {ch_identity,lang.cityhall.identity.description({cfg.new_identity_cost})}
497
498local function cityhall_enter()
499 local user_id = vRP.getUserId(source)
500 if user_id ~= nil then
501 vRP.openMenu(source,cityhall_menu)
502 end
503end
504
505local function cityhall_leave()
506 vRP.closeMenu(source)
507end
508
509local function build_client_cityhall(source) -- build the city hall area/marker/blip
510 local user_id = vRP.getUserId(source)
511 if user_id ~= nil then
512 local x,y,z = table.unpack(cfg.city_hall)
513
514 vRPclient.addBlip(source,{x,y,z,cfg.blip[1],cfg.blip[2],lang.cityhall.title()})
515 vRPclient.addMarker(source,{x,y,z-1,0.7,0.7,0.5,0,255,125,125,150})
516
517 vRP.setArea(source,"vRP:cityhall",x,y,z,1,1.5,cityhall_enter,cityhall_leave)
518 end
519end
520
521AddEventHandler("vRP:playerSpawn",function(user_id, source, first_spawn)
522 -- send registration number to client at spawn
523 vRP.getUserIdentity(user_id, function(identity)
524 if identity then
525 vRPclient.setRegistrationNumber(source,{identity.registration or "000AAA"})
526 end
527 end)
528
529 -- first spawn, build city hall
530 if first_spawn then
531 build_client_cityhall(source)
532 end
533end)
534
535-- player identity menu
536
537-- add identity to main menu
538vRP.registerMenuBuilder("main", function(add, data)
539 local player = data.player
540
541 local user_id = vRP.getUserId(player)
542 if user_id ~= nil then
543 vRP.getUserIdentity(user_id, function(identity)
544 vRP.getDriversLicense(user_id, function(driverlic)
545
546 if identity then
547 -- generate identity content
548 -- get address
549 vRP.getUserAddress(user_id, function(address)
550 local home = ""
551 local number = ""
552 if address then
553 home = address.home
554 number = address.number
555 end
556
557 local content = lang.cityhall.menu.info({htmlEntities.encode(identity.firstname),htmlEntities.encode(identity.name),identity.age,identity.registration,identity.phone,home,number,vRP.getMoney(user_id),vRP.getBankMoney(user_id),driverlic.DmvTest})
558 local choices = {}
559 choices[lang.cityhall.menu.title()] = {function()end, content}
560
561 add(choices)
562 end)
563 end
564 end)
565 end)
566 end
567end)