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