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