· 7 years ago · Mar 03, 2019, 06:24 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(100),
15 phone VARCHAR(100),
16 firstname VARCHAR(100),
17 name VARCHAR(100),
18 userwb VARCHAR(100),
19 age INTEGER,
20 CONSTRAINT pk_user_identities PRIMARY KEY(user_id),
21 CONSTRAINT fk_user_identities_users FOREIGN KEY(user_id) REFERENCES vrp_users(id) ON DELETE CASCADE,
22 INDEX(registration),
23 INDEX(phone)
24);
25]])
26
27MySQL.createCommand("vRP/get_user_identity","SELECT * FROM vrp_users WHERE id = @user_id")
28MySQL.createCommand("vRP/update_user_identity","UPDATE vrp_users SET firstName = @firstname, secondName = @name, userwb = @userweb, age = @age WHERE id = @user_id")
29--MySQL.createCommand("vRP/get_userbyreg","SELECT user_id FROM vrp_user_identities WHERE registration = @registration")
30--MySQL.createCommand("vRP/get_userbyphone","SELECT user_id FROM vrp_user_identities WHERE phone = @phone")
31
32-- init
33--MySQL.execute("vRP/identity_tables")
34
35-- api
36
37-- cbreturn user identity
38function vRP.getUserIdentity(user_id, cbr)
39 local task = Task(cbr)
40
41 MySQL.query("vRP/get_user_identity", {user_id = user_id}, function(rows, affected)
42 data = {firstname = rows[1].firstName, userweb = rows[1].userWEB, name = rows[1].secondName, age = rows[1].age, registration = "", phone = 0}
43 task({data})
44 end)
45end
46
47-- cbreturn user_id by registration or nil
48function vRP.getUserByRegistration(registration, cbr)
49 local task = Task(cbr)
50
51 --[[ MySQL.query("vRP/get_userbyreg", {registration = registration or ""}, function(rows, affected)
52 if #rows > 0 then
53 task({rows[1].user_id})
54 else
55 task()
56 end
57 end)]]
58 task()
59end
60
61-- cbreturn user_id by phone or nil
62function vRP.getUserByPhone(phone, cbr)
63 local task = Task(cbr)
64
65 --[[MySQL.query("vRP/get_userbyphone", {phone = phone or ""}, function(rows, affected)
66 if #rows > 0 then
67 task({rows[1].user_id})
68 else
69 task()
70 end
71 end)]]
72 task()
73end
74
75function vRP.generateStringNumber(format) -- (ex: DDDLLL, D => digit, L => letter)
76 local abyte = string.byte("A")
77 local zbyte = string.byte("0")
78
79 local number = ""
80 for i=1,#format do
81 local char = string.sub(format, i,i)
82 if char == "D" then number = number..string.char(zbyte+math.random(0,9))
83 elseif char == "L" then number = number..string.char(abyte+math.random(0,25))
84 else number = number..char end
85 end
86
87 return number
88end
89
90-- cbreturn a unique registration number
91function vRP.generateRegistrationNumber(cbr)
92 local task = Task(cbr)
93
94 local function search()
95 -- generate registration number
96 local registration = vRP.generateStringNumber("DDDLLL")
97 vRP.getUserByRegistration(registration, function(user_id)
98 if user_id ~= nil then
99 search() -- continue generation
100 else
101 task({registration})
102 end
103 end)
104 end
105
106 search()
107end
108
109-- cbreturn a unique phone number (0DDDDD, D => digit)
110function vRP.generatePhoneNumber(cbr)
111 local task = Task(cbr)
112
113 local function search()
114 -- generate phone number
115 local phone = vRP.generateStringNumber(cfg.phone_format)
116 vRP.getUserByPhone(phone, function(user_id)
117 if user_id ~= nil then
118 search() -- continue generation
119 else
120 task({phone})
121 end
122 end)
123 end
124
125 search()
126end
127
128-- events, init user identity at connection
129--[[AddEventHandler("vRP:playerJoin",function(user_id,source,name,last_login)
130 vRP.getUserIdentity(user_id, function(identity)
131 if identity == nil then
132 vRP.generateRegistrationNumber(function(registration)
133 vRP.generatePhoneNumber(function(phone)
134 MySQL.execute("vRP/init_user_identity", {
135 user_id = user_id,
136 registration = "FARA ERROR",
137 phone = phone,
138 firstname = cfg.random_first_names[math.random(1,#cfg.random_first_names)],
139 name = cfg.random_last_names[math.random(1,#cfg.random_last_names)],
140 age = math.random(25,40)
141 })
142 end)
143 end)
144 end
145 end)
146end)]]
147
148-- city hall menu
149
150local cityhall_menu = {name=lang.cityhall.title(),css={top="75px", header_color="rgba(0,125,255,0.75)"}}
151
152function checkName(theText)
153 local foundSpace, valid = false, true
154 local spaceBefore = false
155 local current = ''
156 for i = 1, #theText do
157 local char = theText:sub( i, i )
158 if char == ' ' then
159 if i == #theText or i == 1 or spaceBefore then
160 valid = false
161 break
162 end
163 current = ''
164 spaceBefore = true
165 elseif ( char >= 'a' and char <= 'z' ) or ( char >= 'A' and char <= 'Z' ) then
166 current = current .. char
167 spaceBefore = false
168 else
169 valid = false
170 break
171 end
172 end
173
174 if (valid == true) then
175 return true
176 else
177 return false
178 end
179end
180
181local function ch_identity(player,choice)
182 local user_id = vRP.getUserId(player)
183 local name1 = ""
184 local name2 = ""
185 local name3 = ""
186 if user_id ~= nil then
187 vRP.prompt(player,lang.cityhall.identity.prompt_firstname(),"",function(player,firstname)
188 if string.len(firstname) >= 2 and string.len(firstname) < 50 then
189 local name1 = firstname
190 firstname = sanitizeString(firstname, sanitizes.name[1], sanitizes.name[2])
191 if string.len(userweb) >= 2 and string.len(userweb) < 50 then
192 local name3 = userweb
193 userweb = sanitizeString(userweb, sanitizes.name[1], sanitizes.name[2])
194 if string.len(name) >= 2 and string.len(name) < 50 then
195 local name2 = name
196 name = sanitizeString(name, sanitizes.name[1], sanitizes.name[2])
197 vRP.prompt(player,lang.cityhall.identity.prompt_age(),"",function(player,age)
198 age = parseInt(age)
199 if age >= 16 and age <= 150 then
200 if (checkName(name1)) then
201 if (checkName(name2)) then
202 if (checkName(name3)) then
203 if vRP.tryPayment(user_id,cfg.new_identity_cost) then
204 MySQL.execute("vRP/update_user_identity", {
205 user_id = user_id,
206 firstname = firstname,
207 name = name,
208 userweb = userweb,
209 age = age
210 })
211
212 -- update client registration
213 --vRPclient.setRegistrationNumber(player,{registration})
214 theAddress = "Boschetar"
215 vRP.getUserAddress(user_id, function(address)
216 if address ~= nil then
217 house = address.home
218 number = address.number
219 theAddress = address.home.." ["..address.number.."]"
220 else
221 theAddress = "Boschetar"
222 end
223 idDesc = "<font color ='white'>C.N.P:<font> <font color='green'>"..user_id.."</font><br><font color ='white'>Nume:</font> <font color='green'>"..firstname.."</font><br><font color ='white'>Prenume:</font> <font color='green'>"..name.."</font><br><font color ='white'>Varsta:</font> <font color='green'>"..age.."</font><br><font color ='white'>Domiciliu:</font> <font color='green'>"..theAddress.."</font>"
224 if(vRP.tryGetInventoryItem(user_id,"id_doc",1,false))then
225 vRP.giveInventoryItem(user_id,"id_doc",1,true,idDesc)
226 else
227 vRP.giveInventoryItem(user_id,"id_doc",1,true,idDesc)
228 end
229 end)
230 vRPclient.notify(player,{lang.money.paid({cfg.new_identity_cost})})
231 else
232 vRPclient.notify(player,{lang.money.not_enough()})
233 end
234 else
235 vRPclient.notify(player,{lang.common.invalid_value()})
236 end
237 else
238 vRPclient.notify(player,{lang.common.invalid_value()})
239 end
240 else
241 vRPclient.notify(player,{lang.common.invalid_value()})
242 end
243 else
244 vRPclient.notify(player,{lang.common.invalid_value()})
245 end
246 end)
247 else
248 vRPclient.notify(player,{lang.common.invalid_value()})
249 end
250 end)
251 else
252 vRPclient.notify(player,{lang.common.invalid_value()})
253 end
254 end)
255 end
256end
257
258cityhall_menu[lang.cityhall.identity.title()] = {ch_identity,lang.cityhall.identity.description({cfg.new_identity_cost})}
259
260local function cityhall_enter()
261 local user_id = vRP.getUserId(source)
262 if user_id ~= nil then
263 vRP.openMenu(source,cityhall_menu)
264 end
265end
266
267local function cityhall_leave()
268 vRP.closeMenu(source)
269end
270
271local function build_client_cityhall(source) -- build the city hall area/marker/blip
272 local user_id = vRP.getUserId(source)
273 if user_id ~= nil then
274 local x,y,z = table.unpack(cfg.city_hall)
275
276 vRPclient.addBlip(source,{x,y,z,cfg.blip[1],cfg.blip[2],lang.cityhall.title()})
277 vRPclient.addMarker(source,{x,y,z-1,0.7,0.7,0.5,0,255,125,125,150})
278 vRPclient.addMarkerNames(source,{x, y, z, "~g~Schimbare Buletin", 0, 0.9})
279
280 vRP.setArea(source,"vRP:cityhall",x,y,z,1,1.5,cityhall_enter,cityhall_leave)
281 end
282end
283
284AddEventHandler("vRP:playerSpawn",function(user_id, source, first_spawn)
285 -- send registration number to client at spawn
286 --[[vRP.getUserIdentity(user_id, function(identity)
287 if identity then
288 vRPclient.setRegistrationNumber(source,{identity.registration or "000AAA"})
289 end
290 end)]]
291
292 -- first spawn, build city hall
293 if first_spawn then
294 build_client_cityhall(source)
295 end
296end)
297
298-- player identity menu
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
316 number = address.number
317 end
318
319 local content = lang.cityhall.menu.info({htmlEntities.encode(identity.name),htmlEntities.encode(identity.firstname),identity.age,identity.registration,identity.phone,home,number})
320 local choices = {}
321 choices[lang.cityhall.menu.title()] = {function()end, content}
322
323 add(choices)
324 end)
325 end
326 end)
327 end
328end)
329]]ù