· 6 years ago · May 11, 2019, 05:12 PM
1
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 TriggerEvent('ply_garages:CheckForSpawnBasicVeh', user_id, 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
97
98 menu[lang.garage.sell.title()] = {function(player,choice)
99 local user_id = vRP.getUserId(player)
100 if user_id ~= nil then
101
102 -- build nested menu
103 local kitems = {}
104 local submenu = {name=lang.garage.title({lang.garage.sell.title()}), css={top="75px",header_color="rgba(255,125,0,0.75)"}}
105 submenu.onclose = function()
106 vRP.openMenu(player,menu)
107 end
108
109 local choose = function(player, choice)
110 vRP.request(player,"Esti sigur ca doresti sa vinzi acest vehicul?",30,function(player,ok)
111 if ok then
112 local vname = kitems[choice]
113 if vname then
114 -- sell vehicle
115 local vehicle = vehicles[vname]
116 if vehicle then
117 local price = math.ceil((vehicle[2]*cfg.sell_factor)*1)
118
119 MySQL.query("vRP/get_vehicle", {user_id = user_id, vehicle = vname}, function(rows, affected)
120 if #rows > 0 then -- has vehicle
121 vRP.giveMoney(user_id,price)
122 MySQL.execute("vRP/remove_vehicle", {user_id = user_id, vehicle = vname})
123 vRPclient.notify(player,{lang.money.received({price})})
124 vRP.closeMenu(player)
125 else
126 vRPclient.notify(player,{lang.common.not_found()})
127 end
128 end)
129 end
130 end
131 end
132 end)
133 end
134
135 -- get player owned vehicles (indexed by vehicle type name in lower case)
136 MySQL.query("vRP/get_vehicles", {user_id = user_id}, function(_pvehicles, affected)
137 local pvehicles = {}
138 for k,v in pairs(_pvehicles) do
139 pvehicles[string.lower(v.vehicle)] = true
140 end
141
142 -- for each existing vehicle in the garage group
143 for k,v in pairs(pvehicles) do
144 local vehicle = vehicles[k]
145 if vehicle then -- not already owned
146 local price = math.ceil((vehicle[2]*cfg.sell_factor)*1)
147 submenu[vehicle[1]] = {choose,lang.garage.buy.info({price,vehicle[3]})}
148 kitems[vehicle[1]] = k
149 end
150 end
151
152 vRP.openMenu(player,submenu)
153 end)
154 end
155 end,lang.garage.sell.description()}
156
157
158
159 menu[lang.garage.store.title()] = {function(player,choice)
160 vRPclient.despawnGarageVehicle(player,{veh_type,15})
161 end, lang.garage.store.description()}
162end
163
164local function build_client_garages(source)
165 local user_id = vRP.getUserId(source)
166 if user_id ~= nil then
167 for k,v in pairs(garages) do
168 local gtype,x,y,z = table.unpack(v)
169
170 local group = vehicle_groups[gtype]
171 if group then
172 local gcfg = group._config
173
174 -- enter
175 local garage_enter = function(player,area)
176 local user_id = vRP.getUserId(source)
177 if user_id ~= nil and vRP.hasPermissions(user_id,gcfg.permissions or {}) then
178 local menu = garage_menus[gtype]
179 if menu then
180 vRP.openMenu(player,menu)
181 end
182 end
183 end
184
185 -- leave
186 local garage_leave = function(player,area)
187 vRP.closeMenu(player)
188 end
189
190 vRPclient.addBlip(source,{x,y,z,gcfg.blipid,gcfg.blipcolor,lang.garage.title({gtype})})
191 vRPclient.addMarker(source,{x,y,z-1,0.7,0.7,0.5,0,255,125,125,150})
192
193 vRP.setArea(source,"vRP:garage"..k,x,y,z,1,1.5,garage_enter,garage_leave)
194 end
195 end
196 end
197end
198
199AddEventHandler("vRP:playerSpawn",function(user_id,source,first_spawn)
200 if first_spawn then
201 build_client_garages(source)
202 end
203end)
204
205-- VEHICLE MENU
206
207-- define vehicle actions
208-- action => {cb(user_id,player,veh_group,veh_name),desc}
209local veh_actions = {}
210
211-- open trunk
212veh_actions[lang.vehicle.trunk.title()] = {function(user_id,player,vtype,name)
213 local chestname = "u"..user_id.."veh_"..string.lower(name)
214 local max_weight = cfg_inventory.vehicle_chest_weights[string.lower(name)] or cfg_inventory.default_vehicle_chest_weight
215
216 -- open chest
217 vRPclient.vc_openDoor(player, {vtype,5})
218 vRP.openChest(player, chestname, max_weight, function()
219 vRPclient.vc_closeDoor(player, {vtype,5})
220 end)
221end, lang.vehicle.trunk.description()}
222
223-- detach trailer
224veh_actions[lang.vehicle.detach_trailer.title()] = {function(user_id,player,vtype,name)
225 vRPclient.vc_detachTrailer(player, {vtype})
226end, lang.vehicle.detach_trailer.description()}
227
228-- detach towtruck
229veh_actions[lang.vehicle.detach_towtruck.title()] = {function(user_id,player,vtype,name)
230 vRPclient.vc_detachTowTruck(player, {vtype})
231end, lang.vehicle.detach_towtruck.description()}
232
233-- detach cargobob
234veh_actions[lang.vehicle.detach_cargobob.title()] = {function(user_id,player,vtype,name)
235 vRPclient.vc_detachCargobob(player, {vtype})
236end, lang.vehicle.detach_cargobob.description()}
237
238-- lock/unlock
239veh_actions[lang.vehicle.lock.title()] = {function(user_id,player,vtype,name)
240 vRPclient.vc_toggleLock(player, {vtype})
241end, lang.vehicle.lock.description()}
242
243-- engine on/off
244veh_actions[lang.vehicle.engine.title()] = {function(user_id,player,vtype,name)
245 vRPclient.vc_toggleEngine(player, {vtype})
246end, lang.vehicle.engine.description()}
247
248MySQL.createCommand("vRP/sell_vehicle_player","UPDATE vrp_user_vehicles SET user_id = @user_id, vehicle_plate = @registration WHERE user_id = @oldUser AND vehicle = @vehicle")
249
250-- sell vehicle
251veh_actions[lang.vehicle.sellTP.title()] = {function(playerID,player,vtype,name)
252 if playerID ~= nil then
253 vRPclient.getNearestPlayers(player,{15},function(nplayers)
254 usrList = ""
255 for k,v in pairs(nplayers) do
256 usrList = usrList .. "[" .. vRP.getUserId(k) .. "]" .. GetPlayerName(k) .. " | "
257 end
258 if usrList ~= "" then
259 vRP.prompt(player,"Players Nearby: " .. usrList .. "","",function(player,user_id)
260 user_id = user_id
261 if user_id ~= nil and user_id ~= "" then
262 local target = vRP.getUserSource(tonumber(user_id))
263 if target ~= nil then
264 vRP.prompt(player,"Price $: ","",function(player,amount)
265 if tonumber(amount) and tonumber(amount) > 0 and tonumber(amount) < limit then
266 MySQL.query("vRP/get_vehicle", {user_id = user_id, vehicle = name}, function(pvehicle, affected)
267 if #pvehicle > 0 then
268 vRPclient.notify(player,{"~r~The player already has this vehicle type."})
269 else
270 local tmpdata = vRP.getUserTmpTable(playerID)
271 if tmpdata.rent_vehicles[name] == true then
272 vRPclient.notify(player,{"~r~Nu poti vinde o masina luata de pe OLX!"})
273 return
274 else
275 vRP.request(target,GetPlayerName(player).." wants to sell: " ..name.. " Price: $"..amount, 10, function(target,ok)
276 if ok then
277 local pID = vRP.getUserId(target)
278 local money = vRP.getMoney(pID)
279 if (tonumber(money) >= tonumber(amount)) then
280 vRPclient.despawnGarageVehicle(player,{vtype,15})
281 vRP.getUserIdentity(pID, function(identity)
282 MySQL.execute("vRP/sell_vehicle_player", {user_id = user_id, registration = "P "..identity.registration, oldUser = playerID, vehicle = name})
283 end)
284 vRP.giveMoney(playerID, amount)
285 vRP.setMoney(pID,money-amount)
286 vRPclient.notify(player,{"~g~You have successfully sold the vehicle to ".. GetPlayerName(target).." for $"..amount.."!"})
287 vRPclient.notify(target,{"~g~"..GetPlayerName(player).." has successfully sold you the car for $"..amount.."!"})
288 else
289 vRPclient.notify(player,{"~r~".. GetPlayerName(target).." doesn't have enough money!"})
290 vRPclient.notify(target,{"~r~You don't have enough money!"})
291 end
292 else
293 vRPclient.notify(player,{"~r~"..GetPlayerName(target).." has refused to buy the car."})
294 vRPclient.notify(target,{"~r~You have refused to buy "..GetPlayerName(player).."'s car."})
295 end
296 end)
297 end
298 vRP.closeMenu(player)
299 end
300 end)
301 else
302 vRPclient.notify(player,{"~r~The price of the car has to be a number."})
303 end
304 end)
305 else
306 vRPclient.notify(player,{"~r~That ID seems invalid."})
307 end
308 else
309 vRPclient.notify(player,{"~r~No player ID selected."})
310 end
311 end)
312 else
313 vRPclient.notify(player,{"~r~No player nearby."})
314 end
315 end)
316 end
317end, lang.vehicle.sellTP.description()}
318
319local function ch_vehicle(player,choice)
320 local user_id = vRP.getUserId(player)
321 if user_id ~= nil then
322 -- check vehicle
323 vRPclient.getNearestOwnedVehicle(player,{7},function(ok,vtype,name)
324 if ok then
325 -- build vehicle menu
326 vRP.buildMenu("vehicle", {user_id = user_id, player = player, vtype = vtype, vname = name}, function(menu)
327 menu.name=lang.vehicle.title()
328 menu.css={top="75px",header_color="rgba(255,125,0,0.75)"}
329
330 for k,v in pairs(veh_actions) do
331 menu[k] = {function(player,choice) v[1](user_id,player,vtype,name) end, v[2]}
332 end
333
334 vRP.openMenu(player,menu)
335 end)
336 else
337 vRPclient.notify(player,{lang.vehicle.no_owned_near()})
338 end
339 end)
340 end
341end
342
343-- ask trunk (open other user car chest)
344local function ch_asktrunk(player,choice)
345 vRPclient.getNearestPlayer(player,{10},function(nplayer)
346 local nuser_id = vRP.getUserId(nplayer)
347 if nuser_id ~= nil then
348 vRPclient.notify(player,{lang.vehicle.asktrunk.asked()})
349 vRP.request(nplayer,lang.vehicle.asktrunk.request(),15,function(nplayer,ok)
350 if ok then -- request accepted, open trunk
351 vRPclient.getNearestOwnedVehicle(nplayer,{7},function(ok,vtype,name)
352 if ok then
353 local chestname = "u"..nuser_id.."veh_"..string.lower(name)
354 local max_weight = cfg_inventory.vehicle_chest_weights[string.lower(name)] or cfg_inventory.default_vehicle_chest_weight
355
356 -- open chest
357 local cb_out = function(idname,amount)
358 vRPclient.notify(nplayer,{lang.inventory.give.given({vRP.getItemName(idname),amount})})
359 end
360
361 local cb_in = function(idname,amount)
362 vRPclient.notify(nplayer,{lang.inventory.give.received({vRP.getItemName(idname),amount})})
363 end
364
365 vRPclient.vc_openDoor(nplayer, {vtype,5})
366 vRP.openChest(player, chestname, max_weight, function()
367 vRPclient.vc_closeDoor(nplayer, {vtype,5})
368 end,cb_in,cb_out)
369 else
370 vRPclient.notify(player,{lang.vehicle.no_owned_near()})
371 vRPclient.notify(nplayer,{lang.vehicle.no_owned_near()})
372 end
373 end)
374 else
375 vRPclient.notify(player,{lang.common.request_refused()})
376 end
377 end)
378 else
379 vRPclient.notify(player,{lang.common.no_player_near()})
380 end
381 end)
382end
383
384-- repair nearest vehicle
385local function ch_repair(player,choice)
386 local user_id = vRP.getUserId(player)
387 if user_id ~= nil then
388 -- anim and repair
389 if vRP.tryGetInventoryItem(user_id,"repairkit",1,true) then
390 vRPclient.playAnim(player,{false,{task="WORLD_HUMAN_WELDING"},false})
391 SetTimeout(15000, function()
392 vRPclient.fixeNearestVehicle(player,{7})
393 vRPclient.stopAnim(player,{false})
394 end)
395 end
396 end
397end
398
399-- replace nearest vehicle
400local function ch_replace(player,choice)
401 vRPclient.replaceNearestVehicle(player,{7})
402end
403
404vRP.registerMenuBuilder("main", function(add, data)
405 local user_id = vRP.getUserId(data.player)
406 if user_id ~= nil then
407 -- add vehicle entry
408 local choices = {}
409 choices[lang.vehicle.title()] = {ch_vehicle}
410
411 -- add ask trunk
412 choices[lang.vehicle.asktrunk.title()] = {ch_asktrunk}
413
414 -- add repair functions
415 if vRP.hasPermission(user_id, "vehicle.repair") then
416 choices[lang.vehicle.repair.title()] = {ch_repair, lang.vehicle.repair.description()}
417 end
418
419 if vRP.hasPermission(user_id, "vehicle.replace") then
420 choices[lang.vehicle.replace.title()] = {ch_replace, lang.vehicle.replace.description()}
421 end
422
423 add(choices)
424 end
425end)