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