· 7 years ago · Jan 22, 2019, 03:16 PM
1-- a basic garage implementation
2
3-- vehicle db
4MySQL.createCommand("vRP/vehicles_table", [[
5CREATE TABLE IF NOT EXISTS vrp_user_vehicles(
6 user_id INTEGER,
7 vehicle VARCHAR(100),
8 CONSTRAINT pk_user_vehicles PRIMARY KEY(user_id,vehicle),
9 CONSTRAINT fk_user_vehicles_users FOREIGN KEY(user_id) REFERENCES vrp_users(id) ON DELETE CASCADE
10);
11]])
12
13MySQL.createCommand("vRP/add_vehicle","INSERT IGNORE INTO vrp_user_vehicles(user_id,vehicle) VALUES(@user_id,@vehicle)")
14MySQL.createCommand("vRP/remove_vehicle","DELETE FROM vrp_user_vehicles WHERE user_id = @user_id AND vehicle = @vehicle")
15MySQL.createCommand("vRP/get_vehicles","SELECT vehicle FROM vrp_user_vehicles WHERE user_id = @user_id")
16MySQL.createCommand("vRP/get_vehicle","SELECT vehicle FROM vrp_user_vehicles WHERE user_id = @user_id AND vehicle = @vehicle")
17
18-- init
19MySQL.execute("vRP/vehicles_table")
20
21-- load config
22
23local cfg = module("cfg/garages")
24local cfg_inventory = module("cfg/inventory")
25local vehicle_groups = cfg.garage_types
26local lang = vRP.lang
27
28local garages = cfg.garages
29
30-- garage menus
31
32local garage_menus = {}
33
34for group,vehicles in pairs(vehicle_groups) do
35 local veh_type = vehicles._config.vtype or "default"
36
37 local menu = {
38 name=lang.garage.title({group}),
39 css={top = "75px", header_color="rgba(255,125,0,0.75)"}
40 }
41 garage_menus[group] = menu
42
43 menu[lang.garage.owned.title()] = {function(player,choice)
44 local user_id = vRP.getUserId(player)
45 if user_id ~= nil then
46 -- init tmpdata for rents
47 local tmpdata = vRP.getUserTmpTable(user_id)
48 if tmpdata.rent_vehicles == nil then
49 tmpdata.rent_vehicles = {}
50 end
51
52
53 -- build nested menu
54 local kitems = {}
55 local submenu = {name=lang.garage.title({lang.garage.owned.title()}), css={top="75px",header_color="rgba(255,125,0,0.75)"}}
56 submenu.onclose = function()
57 vRP.openMenu(player,menu)
58 end
59
60 local choose = function(player, choice)
61 local vname = kitems[choice]
62 if vname then
63 -- spawn vehicle
64 local vehicle = vehicles[vname]
65 if vehicle then
66 vRP.closeMenu(player)
67 TriggerEvent('ply_garages:CheckForSpawnBasicVeh', user_id, vname)
68 end
69 end
70 end
71
72 -- get player owned vehicles
73 MySQL.query("vRP/get_vehicles", {user_id = user_id}, function(pvehicles, affected)
74 -- add rents to whitelist
75 for k,v in pairs(tmpdata.rent_vehicles) do
76 if v then -- check true, prevent future neolua issues
77 table.insert(pvehicles,{vehicle = k})
78 end
79 end
80
81 for k,v in pairs(pvehicles) do
82 local vehicle = vehicles[v.vehicle]
83 if vehicle then
84 submenu[vehicle[1]] = {choose,vehicle[3]}
85 kitems[vehicle[1]] = v.vehicle
86 end
87 end
88
89 vRP.openMenu(player,submenu)
90 end)
91 end
92 end,lang.garage.owned.description()}
93
94 menu[lang.garage.buy.title()] = {function(player,choice)
95 local user_id = vRP.getUserId(player)
96 if user_id ~= nil then
97
98 -- build nested menu
99 local kitems = {}
100 local submenu = {name=lang.garage.title({lang.garage.buy.title()}), css={top="75px",header_color="rgba(255,125,0,0.75)"}}
101 submenu.onclose = function()
102 vRP.openMenu(player,menu)
103 end
104
105 local choose = function(player, choice)
106 local vname = kitems[choice]
107 if vname then
108 -- buy vehicle
109 local vehicle = vehicles[vname]
110 if vehicle and vRP.tryPayment(user_id,vehicle[2]) then
111 MySQL.execute("vRP/add_vehicle", {user_id = user_id, vehicle = vname})
112
113 vRPclient.notify(player,{lang.money.paid({vehicle[2]})})
114 vRP.closeMenu(player)
115 else
116 vRPclient.notify(player,{lang.money.not_enough()})
117 end
118 end
119 end
120
121 -- get player owned vehicles (indexed by vehicle type name in lower case)
122 MySQL.query("vRP/get_vehicles", {user_id = user_id}, function(_pvehicles, affected)
123 local pvehicles = {}
124 for k,v in pairs(_pvehicles) do
125 pvehicles[string.lower(v.vehicle)] = true
126 end
127
128 -- for each existing vehicle in the garage group
129 for k,v in pairs(vehicles) do
130 if k ~= "_config" and pvehicles[string.lower(k)] == nil then -- not already owned
131 submenu[v[1]] = {choose,lang.garage.buy.info({v[2],v[3]})}
132 kitems[v[1]] = k
133 end
134 end
135
136 vRP.openMenu(player,submenu)
137 end)
138 end
139 end,lang.garage.buy.description()}
140
141 menu[lang.garage.sell.title()] = {function(player,choice)
142 local user_id = vRP.getUserId(player)
143 if user_id ~= nil then
144
145 -- build nested menu
146 local kitems = {}
147 local submenu = {name=lang.garage.title({lang.garage.sell.title()}), css={top="75px",header_color="rgba(255,125,0,0.75)"}}
148 submenu.onclose = function()
149 vRP.openMenu(player,menu)
150 end
151
152 local choose = function(player, choice)
153 vRP.request(player,"Are you sure that you want to sell this vehicle?",30,function(player,ok)
154 if ok then
155 local vname = kitems[choice]
156 if vname then
157 -- sell vehicle
158 local vehicle = vehicles[vname]
159 if vehicle then
160 local price = math.ceil((vehicle[2]*cfg.sell_factor)*1)
161
162 MySQL.query("vRP/get_vehicle", {user_id = user_id, vehicle = vname}, function(rows, affected)
163 if #rows > 0 then -- has vehicle
164 vRP.giveMoney(user_id,price)
165 MySQL.execute("vRP/remove_vehicle", {user_id = user_id, vehicle = vname})
166 vRPclient.notify(player,{lang.money.received({price})})
167 vRP.closeMenu(player)
168 else
169 vRPclient.notify(player,{lang.common.not_found()})
170 end
171 end)
172 end
173 end
174 end
175 end)
176 end
177
178 -- get player owned vehicles (indexed by vehicle type name in lower case)
179 MySQL.query("vRP/get_vehicles", {user_id = user_id}, function(_pvehicles, affected)
180 local pvehicles = {}
181 for k,v in pairs(_pvehicles) do
182 pvehicles[string.lower(v.vehicle)] = true
183 end
184
185 -- for each existing vehicle in the garage group
186 for k,v in pairs(pvehicles) do
187 local vehicle = vehicles[k]
188 if vehicle then -- not already owned
189 local price = math.ceil((vehicle[2]*cfg.sell_factor)*1)
190 submenu[vehicle[1]] = {choose,lang.garage.buy.info({price,vehicle[3]})}
191 kitems[vehicle[1]] = k
192 end
193 end
194
195 vRP.openMenu(player,submenu)
196 end)
197 end
198 end,lang.garage.sell.description()}
199
200 menu[lang.garage.rent.title()] = {function(player,choice)
201 local user_id = vRP.getUserId(player)
202 if user_id ~= nil then
203 -- init tmpdata for rents
204 local tmpdata = vRP.getUserTmpTable(user_id)
205 if tmpdata.rent_vehicles == nil then
206 tmpdata.rent_vehicles = {}
207 end
208
209 -- build nested menu
210 local kitems = {}
211 local submenu = {name=lang.garage.title({lang.garage.rent.title()}), css={top="75px",header_color="rgba(255,125,0,0.75)"}}
212 submenu.onclose = function()
213 vRP.openMenu(player,menu)
214 end
215
216 local choose = function(player, choice)
217 local vname = kitems[choice]
218 if vname then
219 -- rent vehicle
220 local vehicle = vehicles[vname]
221 if vehicle then
222 local price = math.ceil((vehicle[2]*cfg.rent_factor)*1)
223 if vRP.tryPayment(user_id,price) then
224 -- add vehicle to rent tmp data
225 tmpdata.rent_vehicles[vname] = true
226
227 vRPclient.notify(player,{lang.money.paid({price})})
228 vRP.closeMenu(player)
229 else
230 vRPclient.notify(player,{lang.money.not_enough()})
231 end
232 end
233 end
234 end
235
236 -- get player owned vehicles (indexed by vehicle type name in lower case)
237 MySQL.query("vRP/get_vehicles", {user_id = user_id}, function(_pvehicles, affected)
238 local pvehicles = {}
239 for k,v in pairs(_pvehicles) do
240 pvehicles[string.lower(v.vehicle)] = true
241 end
242
243 -- add rents to blacklist
244 for k,v in pairs(tmpdata.rent_vehicles) do
245 pvehicles[string.lower(k)] = true
246 end
247
248 -- for each existing vehicle in the garage group
249 for k,v in pairs(vehicles) do
250 if k ~= "_config" and pvehicles[string.lower(k)] == nil then -- not already owned
251 local price = math.ceil((v[2]*cfg.rent_factor)*1)
252 submenu[v[1]] = {choose,lang.garage.buy.info({price,v[3]})}
253 kitems[v[1]] = k
254 end
255 end
256
257 vRP.openMenu(player,submenu)
258 end)
259 end
260 end,lang.garage.rent.description()}
261
262 menu[lang.garage.store.title()] = {function(player,choice)
263 vRPclient.despawnGarageVehicle(player,{veh_type,15})
264 end, lang.garage.store.description()}
265end
266
267local function build_client_garages(source)
268 local user_id = vRP.getUserId(source)
269 if user_id ~= nil then
270 for k,v in pairs(garages) do
271 local gtype,x,y,z = table.unpack(v)
272
273 local group = vehicle_groups[gtype]
274 if group then
275 local gcfg = group._config
276
277 -- enter
278 local garage_enter = function(player,area)
279 local user_id = vRP.getUserId(source)
280 if user_id ~= nil and vRP.hasPermissions(user_id,gcfg.permissions or {}) then
281 local menu = garage_menus[gtype]
282 if menu then
283 vRP.openMenu(player,menu)
284 end
285 end
286 end
287
288 -- leave
289 local garage_leave = function(player,area)
290 vRP.closeMenu(player)
291 end
292
293 vRPclient.addBlip(source,{x,y,z,gcfg.blipid,gcfg.blipcolor,lang.garage.title({gtype})})
294 vRPclient.addMarker(source,{x,y,z-1,0.7,0.7,0.5,0,255,125,125,150})
295
296 vRP.setArea(source,"vRP:garage"..k,x,y,z,1,1.5,garage_enter,garage_leave)
297 end
298 end
299 end
300end
301
302AddEventHandler("vRP:playerSpawn",function(user_id,source,first_spawn)
303 if first_spawn then
304 build_client_garages(source)
305 end
306end)
307
308-- VEHICLE MENU
309
310-- define vehicle actions
311-- action => {cb(user_id,player,veh_group,veh_name),desc}
312local veh_actions = {}
313
314-- open trunk
315veh_actions[lang.vehicle.trunk.title()] = {function(user_id,player,vtype,name)
316 local chestname = "u"..user_id.."veh_"..string.lower(name)
317 local max_weight = cfg_inventory.vehicle_chest_weights[string.lower(name)] or cfg_inventory.default_vehicle_chest_weight
318
319 -- open chest
320 vRPclient.vc_openDoor(player, {vtype,5})
321 vRP.openChest(player, chestname, max_weight, function()
322 vRPclient.vc_closeDoor(player, {vtype,5})
323 end)
324end, lang.vehicle.trunk.description()}
325
326-- detach trailer
327veh_actions[lang.vehicle.detach_trailer.title()] = {function(user_id,player,vtype,name)
328 vRPclient.vc_detachTrailer(player, {vtype})
329end, lang.vehicle.detach_trailer.description()}
330
331-- detach towtruck
332veh_actions[lang.vehicle.detach_towtruck.title()] = {function(user_id,player,vtype,name)
333 vRPclient.vc_detachTowTruck(player, {vtype})
334end, lang.vehicle.detach_towtruck.description()}
335
336-- detach cargobob
337veh_actions[lang.vehicle.detach_cargobob.title()] = {function(user_id,player,vtype,name)
338 vRPclient.vc_detachCargobob(player, {vtype})
339end, lang.vehicle.detach_cargobob.description()}
340
341-- lock/unlock
342veh_actions[lang.vehicle.lock.title()] = {function(user_id,player,vtype,name)
343 vRPclient.vc_toggleLock(player, {vtype})
344end, lang.vehicle.lock.description()}
345
346-- engine on/off
347veh_actions[lang.vehicle.engine.title()] = {function(user_id,player,vtype,name)
348 vRPclient.vc_toggleEngine(player, {vtype})
349end, lang.vehicle.engine.description()}
350--sell2
351MySQL.createCommand("vRP/sell_vehicle_player","UPDATE vrp_user_vehicles SET user_id = @user_id, vehicle_plate = @registration WHERE user_id = @oldUser AND vehicle = @vehicle")
352
353-- sell vehicle
354veh_actions[lang.vehicle.sellTP.title()] = {function(playerID,player,vtype,name)
355 if playerID ~= nil then
356 vRPclient.getNearestPlayers(player,{15},function(nplayers)
357 usrList = ""
358 for k,v in pairs(nplayers) do
359 usrList = usrList .. "[" .. vRP.getUserId(k) .. "]" .. GetPlayerName(k) .. " | "
360 end
361 if usrList ~= "" then
362 vRP.prompt(player,"Players Nearby: " .. usrList .. "","",function(player,user_id)
363 user_id = user_id
364 if user_id ~= nil and user_id ~= "" then
365 local target = vRP.getUserSource(tonumber(user_id))
366 if target ~= nil then
367 vRP.prompt(player,"Price $: ","",function(player,amount)
368 if (tonumber(amount)) then
369 MySQL.query("vRP/get_vehicle", {user_id = user_id, vehicle = name}, function(pvehicle, affected)
370 if #pvehicle > 0 then
371 vRPclient.notify(player,{"~r~The player already has this vehicle type."})
372 else
373 local tmpdata = vRP.getUserTmpTable(playerID)
374 if tmpdata.rent_vehicles[name] == true then
375 vRPclient.notify(player,{"~r~You cannot sell a rented vehicle!"})
376 return
377 else
378 vRP.request(target,GetPlayerName(player).." wants to sell: " ..name.. " Price: $"..amount, 10, function(target,ok)
379 if ok then
380 local pID = vRP.getUserId(target)
381 local money = vRP.getMoney(pID)
382 if (tonumber(money) >= tonumber(amount)) then
383 vRPclient.despawnGarageVehicle(player,{vtype,15})
384 vRP.getUserIdentity(pID, function(identity)
385 MySQL.execute("vRP/sell_vehicle_player", {user_id = user_id, registration = "P "..identity.registration, oldUser = playerID, vehicle = name})
386 end)
387 vRP.giveMoney(playerID, amount)
388 vRP.setMoney(pID,money-amount)
389 vRPclient.notify(player,{"~g~You have successfully sold the vehicle to ".. GetPlayerName(target).." for $"..amount.."!"})
390 vRPclient.notify(target,{"~g~"..GetPlayerName(player).." has successfully sold you the car for $"..amount.."!"})
391 else
392 vRPclient.notify(player,{"~r~".. GetPlayerName(target).." doesn't have enough money!"})
393 vRPclient.notify(target,{"~r~You don't have enough money!"})
394 end
395 else
396 vRPclient.notify(player,{"~r~"..GetPlayerName(target).." has refused to buy the car."})
397 vRPclient.notify(target,{"~r~You have refused to buy "..GetPlayerName(player).."'s car."})
398 end
399 end)
400 end
401 vRP.closeMenu(player)
402 end
403 end)
404 else
405 vRPclient.notify(player,{"~r~The price of the car has to be a number."})
406 end
407 end)
408 else
409 vRPclient.notify(player,{"~r~That ID seems invalid."})
410 end
411 else
412 vRPclient.notify(player,{"~r~No player ID selected."})
413 end
414 end)
415 else
416 vRPclient.notify(player,{"~r~No player nearby."})
417 end
418 end)
419 end
420end, lang.vehicle.sellTP.description()}
421
422local function ch_vehicle(player,choice)
423 local user_id = vRP.getUserId(player)
424 if user_id ~= nil then
425 -- check vehicle
426 vRPclient.getNearestOwnedVehicle(player,{7},function(ok,vtype,name)
427 if ok then
428 -- build vehicle menu
429 vRP.buildMenu("vehicle", {user_id = user_id, player = player, vtype = vtype, vname = name}, function(menu)
430 menu.name=lang.vehicle.title()
431 menu.css={top="75px",header_color="rgba(255,125,0,0.75)"}
432
433 for k,v in pairs(veh_actions) do
434 menu[k] = {function(player,choice) v[1](user_id,player,vtype,name) end, v[2]}
435 end
436
437 vRP.openMenu(player,menu)
438 end)
439 else
440 vRPclient.notify(player,{lang.vehicle.no_owned_near()})
441 end
442 end)
443 end
444end
445
446-- ask trunk (open other user car chest)
447local function ch_asktrunk(player,choice)
448 vRPclient.getNearestPlayer(player,{10},function(nplayer)
449 local nuser_id = vRP.getUserId(nplayer)
450 if nuser_id ~= nil then
451 vRPclient.notify(player,{lang.vehicle.asktrunk.asked()})
452 vRP.request(nplayer,lang.vehicle.asktrunk.request(),15,function(nplayer,ok)
453 if ok then -- request accepted, open trunk
454 vRPclient.getNearestOwnedVehicle(nplayer,{7},function(ok,vtype,name)
455 if ok then
456 local chestname = "u"..nuser_id.."veh_"..string.lower(name)
457 local max_weight = cfg_inventory.vehicle_chest_weights[string.lower(name)] or cfg_inventory.default_vehicle_chest_weight
458
459 -- open chest
460 local cb_out = function(idname,amount)
461 vRPclient.notify(nplayer,{lang.inventory.give.given({vRP.getItemName(idname),amount})})
462 end
463
464 local cb_in = function(idname,amount)
465 vRPclient.notify(nplayer,{lang.inventory.give.received({vRP.getItemName(idname),amount})})
466 end
467
468 vRPclient.vc_openDoor(nplayer, {vtype,5})
469 vRP.openChest(player, chestname, max_weight, function()
470 vRPclient.vc_closeDoor(nplayer, {vtype,5})
471 end,cb_in,cb_out)
472 else
473 vRPclient.notify(player,{lang.vehicle.no_owned_near()})
474 vRPclient.notify(nplayer,{lang.vehicle.no_owned_near()})
475 end
476 end)
477 else
478 vRPclient.notify(player,{lang.common.request_refused()})
479 end
480 end)
481 else
482 vRPclient.notify(player,{lang.common.no_player_near()})
483 end
484 end)
485end
486
487-- repair nearest vehicle
488local function ch_repair(player,choice)
489 local user_id = vRP.getUserId(player)
490 if user_id ~= nil then
491 -- anim and repair
492 if vRP.tryGetInventoryItem(user_id,"repairkit",1,true) then
493 vRPclient.playAnim(player,{false,{task="WORLD_HUMAN_WELDING"},false})
494 SetTimeout(15000, function()
495 vRPclient.fixeNearestVehicle(player,{7})
496 vRPclient.stopAnim(player,{false})
497 end)
498 end
499 end
500end
501
502-- replace nearest vehicle
503local function ch_replace(player,choice)
504 vRPclient.replaceNearestVehicle(player,{7})
505end
506
507vRP.registerMenuBuilder("main", function(add, data)
508 local user_id = vRP.getUserId(data.player)
509 if user_id ~= nil then
510 -- add vehicle entry
511 local choices = {}
512 choices[lang.vehicle.title()] = {ch_vehicle}
513
514 -- add ask trunk
515 choices[lang.vehicle.asktrunk.title()] = {ch_asktrunk}
516
517 -- add repair functions
518 if vRP.hasPermission(user_id, "vehicle.repair") then
519 choices[lang.vehicle.repair.title()] = {ch_repair, lang.vehicle.repair.description()}
520 end
521
522 if vRP.hasPermission(user_id, "vehicle.replace") then
523 choices[lang.vehicle.replace.title()] = {ch_replace, lang.vehicle.replace.description()}
524 end
525
526 add(choices)
527 end
528end)