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