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