· 7 years ago · Sep 08, 2018, 05:44 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 vRPclient.spawnGarageVehicle(player,{veh_type,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 local vname = kitems[choice]
154 if vname then
155-- sell vehicle
156 local vehicle = vehicles[vname]
157 if vehicle then
158 local price = math.ceil((vehicle[2]cfg.sell_factor)1)
159
160 MySQL.query("vRP/get_vehicle", {user_id = user_id, vehicle = vname}, function(rows, affected)
161 if #rows > 0 then -- has vehicle
162
163 MySQL.execute("vRP/remove_vehicle", {user_id = user_id, vehicle = vname})
164
165 local usergivenMoney = 0
166 if usergivenMoney = 0 then
167 vRP.giveMoney(user_id,price)
168 local usergivenMoney = 1
169
170 elseif usergivenMoney = 1 then
171 vRPclient.notify("Nice try buddy!")
172 Citizen.Wait(1000)
173 local user.givenMoney = 0
174
175
176 vRPclient.notify(player,{lang.money.received({price})})
177 vRP.closeMenu(player)
178 else
179 vRPclient.notify(player,{lang.common.not_found()})
180 end
181 end)
182 end
183 end
184 end
185
186 -- get player owned vehicles (indexed by vehicle type name in lower case)
187 MySQL.query("vRP/get_vehicles", {user_id = user_id}, function(_pvehicles, affected)
188 local pvehicles = {}
189 for k,v in pairs(_pvehicles) do
190 pvehicles[string.lower(v.vehicle)] = true
191 end
192
193 -- for each existing vehicle in the garage group
194 for k,v in pairs(pvehicles) do
195 local vehicle = vehicles[k]
196 if vehicle then -- not already owned
197 local price = math.ceil((vehicle[2]*cfg.sell_factor)*1)
198 submenu[vehicle[1]] = {choose,lang.garage.buy.info({price,vehicle[3]})}
199 kitems[vehicle[1]] = k
200 end
201 end
202
203 vRP.openMenu(player,submenu)
204 end)
205 end
206 end,lang.garage.sell.description()}
207
208 menu[lang.garage.rent.title()] = {function(player,choice)
209 local user_id = vRP.getUserId(player)
210 if user_id ~= nil then
211 -- init tmpdata for rents
212 local tmpdata = vRP.getUserTmpTable(user_id)
213 if tmpdata.rent_vehicles == nil then
214 tmpdata.rent_vehicles = {}
215 end
216
217 -- build nested menu
218 local kitems = {}
219 local submenu = {name=lang.garage.title({lang.garage.rent.title()}), css={top="75px",header_color="rgba(255,125,0,0.75)"}}
220 submenu.onclose = function()
221 vRP.openMenu(player,menu)
222 end
223
224 local choose = function(player, choice)
225 local vname = kitems[choice]
226 if vname then
227 -- rent vehicle
228 local vehicle = vehicles[vname]
229 if vehicle then
230 local price = math.ceil((vehicle[2]*cfg.rent_factor)*1)
231 if vRP.tryPayment(user_id,price) then
232 -- add vehicle to rent tmp data
233 tmpdata.rent_vehicles[vname] = true
234
235 vRPclient.notify(player,{lang.money.paid({price})})
236 vRP.closeMenu(player)
237 else
238 vRPclient.notify(player,{lang.money.not_enough()})
239 end
240 end
241 end
242 end
243
244 -- get player owned vehicles (indexed by vehicle type name in lower case)
245 MySQL.query("vRP/get_vehicles", {user_id = user_id}, function(_pvehicles, affected)
246 local pvehicles = {}
247 for k,v in pairs(_pvehicles) do
248 pvehicles[string.lower(v.vehicle)] = true
249 end
250
251 -- add rents to blacklist
252 for k,v in pairs(tmpdata.rent_vehicles) do
253 pvehicles[string.lower(k)] = true
254 end
255
256 -- for each existing vehicle in the garage group
257 for k,v in pairs(vehicles) do
258 if k ~= "_config" and pvehicles[string.lower(k)] == nil then -- not already owned
259 local price = math.ceil((v[2]*cfg.rent_factor)*1)
260 submenu[v[1]] = {choose,lang.garage.buy.info({price,v[3]})}
261 kitems[v[1]] = k
262 end
263 end
264
265 vRP.openMenu(player,submenu)
266 end)
267 end
268 end,lang.garage.rent.description()}
269
270 menu[lang.garage.store.title()] = {function(player,choice)
271 vRPclient.despawnGarageVehicle(player,{veh_type,15})
272 end, lang.garage.store.description()}
273end
274
275local function build_client_garages(source)
276 local user_id = vRP.getUserId(source)
277 if user_id ~= nil then
278 for k,v in pairs(garages) do
279 local gtype,x,y,z = table.unpack(v)
280
281 local group = vehicle_groups[gtype]
282 if group then
283 local gcfg = group._config
284
285 -- enter
286 local garage_enter = function(player,area)
287 local user_id = vRP.getUserId(source)
288 if user_id ~= nil and vRP.hasPermissions(user_id,gcfg.permissions or {}) then
289 local menu = garage_menus[gtype]
290 if menu then
291 vRP.openMenu(player,menu)
292 end
293 end
294 end
295
296 -- leave
297 local garage_leave = function(player,area)
298 vRP.closeMenu(player)
299 end
300
301 vRPclient.addBlip(source,{x,y,z,gcfg.blipid,gcfg.blipcolor,lang.garage.title({gtype})})
302 vRPclient.addMarker(source,{x,y,z-1,0.7,0.7,0.5,0,255,125,125,150})
303
304 vRP.setArea(source,"vRP:garage"..k,x,y,z,1,1.5,garage_enter,garage_leave)
305 end
306 end
307 end
308end
309
310AddEventHandler("vRP:playerSpawn",function(user_id,source,first_spawn)
311 if first_spawn then
312 build_client_garages(source)
313 end
314end)
315
316-- VEHICLE MENU
317
318-- define vehicle actions
319-- action => {cb(user_id,player,veh_group,veh_name),desc}
320local veh_actions = {}
321
322-- open trunk
323veh_actions[lang.vehicle.trunk.title()] = {function(user_id,player,vtype,name)
324 local chestname = "u"..user_id.."veh_"..string.lower(name)
325 local max_weight = cfg_inventory.vehicle_chest_weights[string.lower(name)] or cfg_inventory.default_vehicle_chest_weight
326
327 -- open chest
328 vRPclient.vc_openDoor(player, {vtype,5})
329 vRP.openChest(player, chestname, max_weight, function()
330 vRPclient.vc_closeDoor(player, {vtype,5})
331 end)
332end, lang.vehicle.trunk.description()}
333
334-- detach trailer
335veh_actions[lang.vehicle.detach_trailer.title()] = {function(user_id,player,vtype,name)
336 vRPclient.vc_detachTrailer(player, {vtype})
337end, lang.vehicle.detach_trailer.description()}
338
339-- detach towtruck
340veh_actions[lang.vehicle.detach_towtruck.title()] = {function(user_id,player,vtype,name)
341 vRPclient.vc_detachTowTruck(player, {vtype})
342end, lang.vehicle.detach_towtruck.description()}
343
344-- detach cargobob
345veh_actions[lang.vehicle.detach_cargobob.title()] = {function(user_id,player,vtype,name)
346 vRPclient.vc_detachCargobob(player, {vtype})
347end, lang.vehicle.detach_cargobob.description()}
348
349-- lock/unlock
350veh_actions[lang.vehicle.lock.title()] = {function(user_id,player,vtype,name)
351 vRPclient.vc_toggleLock(player, {vtype})
352end, lang.vehicle.lock.description()}
353
354-- engine on/off
355veh_actions[lang.vehicle.engine.title()] = {function(user_id,player,vtype,name)
356 vRPclient.vc_toggleEngine(player, {vtype})
357end, lang.vehicle.engine.description()}
358--sell2
359MySQL.createCommand("vRP/sell_vehicle_player","UPDATE vrp_user_vehicles SET user_id = @user_id, vehicle_plate = @registration WHERE user_id = @oldUser AND vehicle = @vehicle")
360
361-- sell vehicle
362veh_actions[lang.vehicle.sellTP.title()] = {function(playerID,player,vtype,name)
363 if playerID ~= nil then
364 vRPclient.getNearestPlayers(player,{15},function(nplayers)
365 usrList = ""
366 for k,v in pairs(nplayers) do
367 usrList = usrList .. "[" .. vRP.getUserId(k) .. "]" .. GetPlayerName(k) .. " | "
368 end
369 if usrList ~= "" then
370 vRP.prompt(player,"Players Nearby: " .. usrList .. "","",function(player,user_id)
371 user_id = user_id
372 if user_id ~= nil and user_id ~= "" then
373 local target = vRP.getUserSource(tonumber(user_id))
374 if target ~= nil then
375 vRP.prompt(player,"Price $: ","",function(player,amount)
376 if (tonumber(amount)) then
377 MySQL.query("vRP/get_vehicle", {user_id = user_id, vehicle = name}, function(pvehicle, affected)
378 if #pvehicle > 0 then
379 vRPclient.notify(player,{"~r~The player already has this vehicle type."})
380 else
381 local tmpdata = vRP.getUserTmpTable(playerID)
382 if tmpdata.rent_vehicles[name] == true then
383 vRPclient.notify(player,{"~r~You cannot sell a rented vehicle!"})
384 return
385 else
386 vRP.request(target,GetPlayerName(player).." wants to sell: " ..name.. " Price: $"..amount, 10, function(target,ok)
387 if ok then
388 local pID = vRP.getUserId(target)
389 local money = vRP.getMoney(pID)
390 if (tonumber(money) >= tonumber(amount)) then
391 vRPclient.despawnGarageVehicle(player,{vtype,15})
392 vRP.getUserIdentity(pID, function(identity)
393 MySQL.execute("vRP/sell_vehicle_player", {user_id = user_id, registration = "P "..identity.registration, oldUser = playerID, vehicle = name})
394 end)
395 vRP.giveMoney(playerID, amount)
396 vRP.setMoney(pID,money-amount)
397 vRPclient.notify(player,{"~g~You have successfully sold the vehicle to ".. GetPlayerName(target).." for $"..amount.."!"})
398 vRPclient.notify(target,{"~g~"..GetPlayerName(player).." has successfully sold you the car for $"..amount.."!"})
399 else
400 vRPclient.notify(player,{"~r~".. GetPlayerName(target).." doesn't have enough money!"})
401 vRPclient.notify(target,{"~r~You don't have enough money!"})
402 end
403 else
404 vRPclient.notify(player,{"~r~"..GetPlayerName(target).." has refused to buy the car."})
405 vRPclient.notify
406 (target,{"~r~You have refused to buy "..GetPlayerName(player).."'s car."})
407 end
408 end)
409 end
410 vRP.closeMenu(player)
411 end
412 end)
413 else
414 vRPclient.notify(player,{"~r~The price of the car has to be a number."})
415 end
416 end)
417 else
418 vRPclient.notify(player,{"~r~That ID seems invalid."})
419 end
420 else
421 vRPclient.notify(player,{"~r~No player ID selected."})
422 end
423 end)
424 else
425 vRPclient.notify(player,{"~r~No player nearby."})
426 end
427 end)
428 end
429end, lang.vehicle.sellTP.description()}
430
431local function ch_vehicle(player,choice)
432 local user_id = vRP.getUserId(player)
433 if user_id ~= nil then
434 -- check vehicle
435 vRPclient.getNearestOwnedVehicle(player,{7},function(ok,vtype,name)
436 if ok then
437 -- build vehicle menu
438 vRP.buildMenu("vehicle", {user_id = user_id, player = player, vtype = vtype, vname = name}, function(menu)
439 menu.name=lang.vehicle.title()
440 menu.css={top="75px",header_color="rgba(255,125,0,0.75)"}
441
442 for k,v in pairs(veh_actions) do
443 menu[k] = {function(player,choice) v[1](user_id,player,vtype,name) end, v[2]}
444 end
445
446 vRP.openMenu(player,menu)
447 end)
448 else
449 vRPclient.notify(player,{lang.vehicle.no_owned_near()})
450 end
451 end)
452 end
453end
454
455-- ask trunk (open other user car chest)
456local function ch_asktrunk(player,choice)
457 vRPclient.getNearestPlayer(player,{10},function(nplayer)
458 local nuser_id = vRP.getUserId(nplayer)
459 if nuser_id ~= nil then
460 vRPclient.notify(player,{lang.vehicle.asktrunk.asked()})
461 vRP.request(nplayer,lang.vehicle.asktrunk.request(),15,function(nplayer,ok)
462 if ok then -- request accepted, open trunk
463 vRPclient.getNearestOwnedVehicle(nplayer,{7},function(ok,vtype,name)
464 if ok then
465 local chestname = "u"..nuser_id.."veh_"..string.lower(name)
466 local max_weight = cfg_inventory.vehicle_chest_weights[string.lower(name)] or cfg_inventory.default_vehicle_chest_weight
467
468 -- open chest
469 local cb_out = function(idname,amount)
470 vRPclient.notify(nplayer,{lang.inventory.give.given({vRP.getItemName(idname),amount})})
471 end
472
473 local cb_in = function(idname,amount)
474 vRPclient.notify(nplayer,{lang.inventory.give.received({vRP.getItemName(idname),amount})})
475 end
476
477 vRPclient.vc_openDoor(nplayer, {vtype,5})
478 vRP.openChest(player, chestname, max_weight, function()
479 vRPclient.vc_closeDoor(nplayer, {vtype,5})
480 end,cb_in,cb_out)
481 else
482 vRPclient.notify(player,{lang.vehicle.no_owned_near()})
483 vRPclient.notify(nplayer,{lang.vehicle.no_owned_near()})
484 end
485 end)
486 else
487 vRPclient.notify(player,{lang.common.request_refused()})
488 end
489 end)
490 else
491 vRPclient.notify(player,{lang.common.no_player_near()})
492 end
493 end)
494end
495
496-- repair nearest vehicle
497local function ch_repair(player,choice)
498 local user_id = vRP.getUserId(player)
499 if user_id ~= nil then
500 -- anim and repair
501 if vRP.tryGetInventoryItem(user_id,"repairkit",1,true) then
502 vRPclient.playAnim(player,{false,{task="WORLD_HUMAN_WELDING"},false})
503 SetTimeout(15000, function()
504 vRPclient.fixeNearestVehicle(player,{7})
505 vRPclient.stopAnim(player,{false})
506 end)
507 end
508 end
509end
510
511-- replace nearest vehicle
512local function ch_replace(player,choice)
513 vRPclient.replaceNearestVehicle(player,{7})
514end
515
516vRP.registerMenuBuilder("main", function(add, data)
517 local user_id = vRP.getUserId(data.player)
518 if user_id ~= nil then
519 -- add vehicle entry
520 local choices = {}
521 choices[lang.vehicle.title()] = {ch_vehicle}
522
523 -- add ask trunk
524 choices[lang.vehicle.asktrunk.title()] = {ch_asktrunk}
525
526 -- add repair functions
527 if vRP.hasPermission(user_id, "vehicle.repair") then
528 choices[lang.vehicle.repair.title()] = {ch_repair, lang.vehicle.repair.description()}
529 end
530
531 if vRP.hasPermission(user_id, "vehicle.replace") then
532 choices[lang.vehicle.replace.title()] = {ch_replace, lang.vehicle.replace.description()}
533 end
534
535 add(choices)
536 end
537end)