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