· 6 years ago · Jul 17, 2019, 03:08 PM
1local htmlEntities = module("lib/htmlEntities")
2
3local cfg = module("cfg/identity")
4local lang = vRP.lang
5
6local sanitizes = module("cfg/sanitizes")
7
8-- this module describe the identity system
9
10-- init sql
11MySQL.createCommand("vRP/identity_tables", [[
12CREATE TABLE IF NOT EXISTS vrp_user_identities(
13 user_id INTEGER,
14 registration VARCHAR(20),
15 phone VARCHAR(20),
16 firstname VARCHAR(50),
17 name VARCHAR(50),
18 age INTEGER,
19 CONSTRAINT pk_user_identities PRIMARY KEY(user_id),
20 CONSTRAINT fk_user_identities_users FOREIGN KEY(user_id) REFERENCES vrp_users(id) ON DELETE CASCADE,
21 INDEX(registration),
22 INDEX(phone)
23);
24]])
25
26MySQL.createCommand("vRP/get_user_identity","SELECT * FROM vrp_user_identities WHERE user_id = @user_id")
27MySQL.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)")
28MySQL.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")
29MySQL.createCommand("vRP/get_userbyreg","SELECT user_id FROM vrp_user_identities WHERE registration = @registration")
30MySQL.createCommand("vRP/get_userbyphone","SELECT user_id FROM vrp_user_identities WHERE phone = @phone")
31MySQL.createCommand("vRP/status_driverlicense", "SELECT * FROM vrp_users WHERE id = @user_id")
32MySQL.createCommand("vRP/status_gunlicense", "SELECT * FROM vrp_users WHERE id = @user_id")
33MySQL.createCommand("vRP/gcphone_update","INSERT INTO users(identifier,phone_number) VALUES(@identifier,@phone_number)")
34MySQL.createCommand("vRP/gcphone_get", "SELECT * FROM users WHERE identifier = @user_id")
35
36-- init
37MySQL.execute("vRP/identity_tables")
38
39-- api
40
41
42-- wallet amount
43function vRP.getWalletAmount(user_id)
44 local tmp = vRP.getUserTmpTable(user_id)
45 if tmp then
46 return tmp.wallet or 0
47 else
48 return 0
49 end
50end
51
52-- bank amount
53function vRP.getBankAmount(user_id)
54 local tmp = vRP.getUserTmpTable(user_id)
55 if tmp then
56 return tmp.bank or 0
57 else
58 return 0
59 end
60end
61
62-- cbreturn user identity
63function vRP.getUserIdentity(user_id, cbr)
64 local task = Task(cbr)
65
66 MySQL.query("vRP/get_user_identity", {user_id = user_id}, function(rows, affected)
67 task({rows[1]})
68 end)
69end
70
71-- cbreturn user_id by registration or nil
72function vRP.getUserByRegistration(registration, cbr)
73 local task = Task(cbr)
74
75 MySQL.query("vRP/get_userbyreg", {registration = registration or ""}, function(rows, affected)
76 if #rows > 0 then
77 task({rows[1].user_id})
78 else
79 task()
80 end
81 end)
82end
83
84-- cbreturn user_id by phone or nil
85function vRP.getUserByPhone(phone, cbr)
86 local task = Task(cbr)
87
88 MySQL.query("vRP/get_userbyphone", {phone = phone or ""}, function(rows, affected)
89 if #rows > 0 then
90 task({rows[1].user_id})
91 else
92 task()
93 end
94 end)
95end
96
97function vRP.generateStringNumber(format) -- (ex: DDDLLL, D => digit, L => letter)
98 local abyte = string.byte("A")
99 local zbyte = string.byte("0")
100
101 local number = ""
102 for i=1,#format do
103 local char = string.sub(format, i,i)
104 if char == "O" then number = number..os.date("%d%m%y")
105 elseif char == "D" then number = number..string.char(zbyte+math.random(0,9))
106 elseif char == "L" then number = number..string.char(abyte+math.random(0,25))
107 else number = number..char end
108 end
109
110 return number
111end
112
113-- cbreturn a unique registration number
114function vRP.generateRegistrationNumber(cbr)
115 local task = Task(cbr)
116
117 local function search()
118 -- generate registration number
119 local registration = vRP.generateStringNumber("DDDDDD") --CPR
120 vRP.getUserByRegistration(registration, function(user_id)
121 if user_id ~= nil then
122 search() -- continue generation
123 else
124 task({registration})
125 end
126 end)
127 end
128
129 search()
130end
131
132-- cbreturn a unique phone number (0DDDDD, D => digit)
133function vRP.generatePhoneNumber(cbr)
134 local task = Task(cbr)
135
136 local function search()
137 -- generate phone number
138 local phone = vRP.generateStringNumber(cfg.phone_format)
139 vRP.getUserByPhone(phone, function(user_id)
140 if user_id ~= nil then
141 search() -- continue generation
142 else
143 task({phone})
144 end
145 end)
146 end
147
148 search()
149end
150
151-- events, init user identity at connection
152AddEventHandler("vRP:playerJoin",function(user_id,source,name,last_login)
153 vRP.getUserIdentity(user_id, function(identity)
154 if identity == nil then
155 vRP.generateRegistrationNumber(function(registration)
156 vRP.generatePhoneNumber(function(phone)
157 MySQL.execute("vRP/init_user_identity", {
158 user_id = user_id,
159 registration = registration,
160 phone = phone,
161 firstname = cfg.random_first_names[math.random(1,#cfg.random_first_names)],
162 name = cfg.random_last_names[math.random(1,#cfg.random_last_names)],
163 age = math.random(25,40)
164 })
165 end)
166 end)
167 end
168 end)
169end)
170
171AddEventHandler("vRP:playerJoin",function(user_id,source,name,last_login)
172 if user_id ~= nil then
173 MySQL.query("vRP/gcphone_get", {user_id = user_id}, function(rows,affected)
174 if #rows <= 0 then
175 vRP.getUserIdentity(user_id, function(identity)
176 if identity ~= nil then
177 if identity.phone ~= nil then
178 MySQL.execute("vRP/gcphone_update", {
179 identifier = user_id,
180 phone_number = identity.phone
181 })
182 else
183 vRP.generatePhoneNumber(function(rannum)
184 MySQL.execute("vRP/gcphone_update", {
185 identifier = user_id,
186 phone_number = rannum
187 })
188 end)
189 end
190 else
191 vRP.generatePhoneNumber(function(rannum)
192 MySQL.execute("vRP/gcphone_update", {
193 identifier = user_id,
194 phone_number = rannum
195 })
196 end)
197 end
198 end)
199 end
200 end)
201 end
202end)
203
204-- city hall menu
205
206local cityhall_menu = {name=lang.cityhall.title(),css={top="75px", header_color="rgba(25,25,25,1.0)"}}
207
208local function ch_identity(player,choice)
209 local user_id = vRP.getUserId(player)
210 if user_id ~= nil then
211 vRP.prompt(player,lang.cityhall.identity.prompt_firstname(),"",function(player,firstname)
212 if string.len(firstname) >= 2 and string.len(firstname) < 15 then
213 firstname = sanitizeString(firstname, sanitizes.name[1], sanitizes.name[2])
214 vRP.prompt(player,lang.cityhall.identity.prompt_name(),"",function(player,name)
215 if string.len(name) >= 2 and string.len(name) < 15 then
216 name = sanitizeString(name, sanitizes.name[1], sanitizes.name[2])
217 vRP.prompt(player,lang.cityhall.identity.prompt_age(),"",function(player,age)
218 age = parseInt(age)
219 if age >= 16 and age <= 150 then
220 if vRP.tryFullPayment(user_id,cfg.new_identity_cost) then
221 vRP.generateRegistrationNumber(function(registration)
222 vRP.generatePhoneNumber(function(phone)
223
224 MySQL.execute("vRP/update_user_identity", {
225 user_id = user_id,
226 firstname = firstname,
227 name = name,
228 age = age,
229 registration = registration,
230 phone = phone
231 })
232
233 -- update client registration
234 vRPclient.setRegistrationNumber(player,{registration})
235 TriggerClientEvent("pNotify:SendNotification", player,{text = {lang.money.paid({cfg.new_identity_cost})}, type = "success", queue = "global", timeout = 4000, layout = "centerLeft",animation = {open = "gta_effects_fade_in", close = "gta_effects_fade_out"}})
236 end)
237 end)
238 else
239 TriggerClientEvent("pNotify:SendNotification", player,{text = {lang.money.not_enough()}, type = "error", queue = "global", timeout = 4000, layout = "centerLeft",animation = {open = "gta_effects_fade_in", close = "gta_effects_fade_out"}})
240 end
241 else
242 TriggerClientEvent("pNotify:SendNotification", player,{text = {lang.common.invalid_value()}, type = "error", queue = "global", timeout = 4000, layout = "centerLeft",animation = {open = "gta_effects_fade_in", close = "gta_effects_fade_out"}})
243 end
244 end)
245 else
246 TriggerClientEvent("pNotify:SendNotification", player,{text = {lang.common.invalid_value()}, type = "error", queue = "global", timeout = 4000, layout = "centerLeft",animation = {open = "gta_effects_fade_in", close = "gta_effects_fade_out"}})
247 end
248 end)
249 else
250 TriggerClientEvent("pNotify:SendNotification", player,{text = {lang.common.invalid_value()}, type = "error", queue = "global", timeout = 4000, layout = "centerLeft",animation = {open = "gta_effects_fade_in", close = "gta_effects_fade_out"}})
251 end
252 end)
253 end
254end
255
256cityhall_menu[lang.cityhall.identity.title()] = {ch_identity,lang.cityhall.identity.description({cfg.new_identity_cost})}
257
258local function cityhall_enter()
259 local user_id = vRP.getUserId(source)
260 if user_id ~= nil then
261 vRP.openMenu(source,cityhall_menu)
262 end
263end
264
265local function cityhall_leave()
266 vRP.closeMenu(source)
267end
268
269local function build_client_cityhall(source) -- build the city hall area/marker/blip
270 local user_id = vRP.getUserId(source)
271 if user_id ~= nil then
272 local x,y,z = table.unpack(cfg.city_hall)
273
274 --vRPclient.addBlip(source,{x,y,z,cfg.blip[1],cfg.blip[2],lang.cityhall.title()})
275 vRPclient.addMarker(source,{x,y,z-1,0.7,0.7,0.5,0,255,125,125,150})
276
277 vRP.setArea(source,"vRP:cityhall",x,y,z,1,1.5,cityhall_enter,cityhall_leave)
278 end
279end
280
281AddEventHandler("vRP:playerSpawn",function(user_id, source, first_spawn)
282 -- send registration number to client at spawn
283 vRP.getUserIdentity(user_id, function(identity)
284 if identity then
285 vRPclient.setRegistrationNumber(source,{identity.registration or "000AAA"})
286 end
287 end)
288
289 -- first spawn, build city hall
290 if first_spawn then
291 build_client_cityhall(source)
292 end
293end)
294
295-- player identity menu
296
297MySQL.createCommand("vRP/get_pin", "SELECT bankPin FROM vrp_users WHERE id = @user_id")
298MySQL.createCommand("vRP/gcphone_get_nr", "SELECT * FROM users WHERE identifier = @id")
299
300-- add identity to main menu
301vRP.registerMenuBuilder("main", function(add, data)
302 local player = data.player
303
304 local user_id = vRP.getUserId(player)
305 if user_id ~= nil then
306 vRP.getUserIdentity(user_id, function(identity)
307
308 if identity then
309 -- generate identity content
310 -- get address
311 vRP.getUserAddress(user_id, function(address)
312 local home = ""
313 local number = ""
314 if address then
315 home = address.home.. " nr. "
316 number = address.number
317 else
318 home = "Hjemløs"
319 number = ""
320 end
321
322 -- get driver license status
323 local driverlicense = ""
324 MySQL.query("vRP/status_driverlicense", {user_id = user_id}, function(rows, affected)
325 if #rows > 0 then
326 driverlicense = rows[1].DmvTest
327 else
328 driverlicense = 1
329 end
330
331 if driverlicense == 3 then
332 driverlicense = "Ja"
333 elseif driverlicense == 2 then
334 driverlicense = "Frataget"
335 else
336 driverlicense = "Nej"
337 end
338 end
339
340 -- get gun license status
341 local gunlicense = ""
342 MySQL.query("vRP/status_gunlicense", {user_id = user_id}, function(rows, affected)
343 if #rows > 0 then
344 gunlicense = rows[1].GunLicenseA
345 else
346 gunlicense = 1
347 end
348
349 if gunlicense == 3 then
350 gunlicense = "Ja"
351 elseif gunlicense == 2 then
352 gunlicense = "Frataget"
353 else
354 gunlicense = "Nej"
355 end
356 end
357--------------------------------------------------------------------
358
359 MySQL.query("vRP/get_pin", {user_id = user_id}, function(rows,affected)
360
361 if rows[1].bankPin ~= nil and rows[1].bankPin ~= "0" and rows[1].bankPin ~= 0 then
362 ipin = rows[1].bankPin
363 else
364 ipin = "Opret en konto i banken"
365 end
366
367 function format_thousand(v)
368 local s = string.format("%d", math.floor(v))
369 local pos = string.len(s) % 3
370 if pos == 0 then pos = 3 end
371 return string.sub(s, 1, pos)
372 .. string.gsub(string.sub(s, pos+1), "(...)", ".%1")
373 end
374
375 MySQL.query("vRP/gcphone_get_nr", {id = user_id}, function(grows, gaffected)
376 if #rows > 0 then
377 phonenr = grows[1].phone_number
378 end
379
380 -- get wallet amount
381 local walletamount = format_thousand(math.floor(vRP.getWalletAmount(user_id)))
382
383 -- get bank amount
384 local bankamount = format_thousand(math.floor(vRP.getBankAmount(user_id)))
385
386 local content = lang.cityhall.menu.info({htmlEntities.encode(identity.name),htmlEntities.encode(identity.firstname),identity.age,identity.registration,phonenr,home,number,driverlicense,gunlicense,walletamount,bankamount,user_id,ipin})
387 local choices = {}
388 choices[lang.cityhall.menu.title()] = {function()end, content}
389
390 add(choices)
391 end)
392 end)
393 end)
394 end)
395 end
396 end)
397 end
398end)