Skip to content

Commit

Permalink
Update: Added Test Drive, Categories
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverGoLt committed Sep 16, 2022
1 parent e0d7664 commit decd92d
Show file tree
Hide file tree
Showing 12 changed files with 461 additions and 95 deletions.
1 change: 1 addition & 0 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ end)
AddEventHandler('onResourceStart', function(resource)
if resource == GetCurrentResourceName() then
Utils.createBlips()
print('[INFO] Resource started')
_Loaded = true
end
end)
Expand Down
56 changes: 56 additions & 0 deletions client/testDrive.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
local testDriving = false
local testVehicle
local testShop

RegisterNUICallback('testDrive', function(data, cb)
ESX.TriggerServerCallback('h-vshop:testDrive', function(success)
print(success)
if success then
testDriving = true
local shop = Utils.getShop()
testShop = shop

if shop then
local sPos = shop.delivery.pos
local sHeading = shop.delivery.heading
ESX.Game.SpawnLocalVehicle(data.vehicle, sPos, heading, function(vehicle)
testVehicle = vehicle
print('Test vehicle spawned: '..vehicle)
end)
Wait(100)
SetPedIntoVehicle(PlayerPedId(), testVehicle, -1)
StartTestTimer()
end
cb(true)
else
cb(false)
end
end, data.vehicle)
end)

RegisterNUICallback('stopTestDrive', function(data, cb)
if testDriving then
testDriving = false
SendNUIMessage({
type = 'stopTimer',
})
ESX.Game.DeleteVehicle(testVehicle)
SetEntityCoords(PlayerPedId(), testShop.pos)
ESX.TriggerServerCallback('h-vshop:removeBucket', function(success)
if not success then
print('Failed to remove bucket')
end
end)
cb(true)
else
cb(false)
end
end)

local testTime = Config.TestTime
StartTestTimer = function ()
SendNUIMessage({
type = 'startTimer',
time = testTime
})
end
49 changes: 42 additions & 7 deletions client/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Utils.openShop = function ()
type = 'open',
shop = shop,
cars = cars,
translation = Config.Translation
translation = Config.Translation,
})
SetNuiFocus(true, true)
end
Expand All @@ -77,6 +77,22 @@ Utils.deleteVehicle = function()
end
end

Utils.loadModel = function(model)
RequestModel(model)

while not HasModelLoaded(model) do
Wait(1)
end

print('Loading Completed')

SendNUIMessage({
type = 'loadingDone'
})

return true
end

Utils.spawnShowcase = function(vehicle)
local shop = Utils.getShop()
local pos = shop.preview
Expand All @@ -88,6 +104,12 @@ Utils.spawnShowcase = function(vehicle)
if not cars then return end

if pos then

local loaded = Utils.loadModel(cars[1].model)
while not loaded do
Wait(1)
end

if not currentVeh and not vehicle then
ESX.Game.SpawnLocalVehicle(cars[1].model, vector3(pos.x, pos.y, pos.z), heading, function(vehicle)
currentVeh = vehicle
Expand Down Expand Up @@ -123,6 +145,7 @@ end

Utils.spawnVehicle = function(model, plate)
local shop = Utils.getShop()

if shop then
local sPos = shop.delivery.pos
local sHeading = shop.delivery.heading
Expand All @@ -148,12 +171,24 @@ Utils.spawnVehicle = function(model, plate)
end

Utils.getShopVehicles = function (category)
local vehicles = {}
for k,v in pairs(Config.Vehicles) do
if v.category == category then
vehicles[#vehicles+1] = v
if type(category) == "table" then
local vehicles = {}
for i = 1, #category do
local name = category[i]
for _,v in pairs(Config.Vehicles) do
if v.category == name then
vehicles[#vehicles+1] = v
end
end
end
return vehicles or nil
else
local vehicles = {}
for k,v in pairs(Config.Vehicles) do
if v.category == category then
vehicles[#vehicles+1] = v
end
end
return vehicles
end

return vehicles
end
3 changes: 3 additions & 0 deletions fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ fx_version 'adamant'

game 'gta5'

lua54 'yes'

server_scripts {
'@oxmysql/lib/MySQL.lua',
'server/*.lua',
Expand All @@ -17,6 +19,7 @@ shared_scripts {
}

ui_page 'web/dist/index.html'
--ui_page 'http://localhost:3000/'

files {
'web/dist/**'
Expand Down
66 changes: 58 additions & 8 deletions server/callbacks.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
local shop = {}

local inUseBuckets = {}

ESX.RegisterServerCallback('esx_vehicleshop:isPlateTaken', function(source, cb, plate)
MySQL.scalar('SELECT plate FROM owned_vehicles WHERE plate = ?', {plate},
function(result)
Expand Down Expand Up @@ -33,6 +35,30 @@ ESX.RegisterServerCallback('h-vshop:setOwned', function(source, cb, props)
end
end)

ESX.RegisterServerCallback('h-vshop:testDrive', function (source, cb, model)
local bucketSet = shop.testDrive(source, model)

if bucketSet then
cb(true)
else
cb(false)
end
end)

ESX.RegisterServerCallback('h-vshop:removeBucket', function (source, cb, bucket)
if source ~= nil and source ~= -1 then
local bucket = GetPlayerRoutingBucket(source)

if bucket ~= 0 then
SetPlayerRoutingBucket(source, 0)
inUseBuckets[bucket] = nil
cb(true)
else
cb(false)
end
end
end)

function shop.setOwned(source, props)
local xPlayer = ESX.GetPlayerFromId(source)
if xPlayer then
Expand Down Expand Up @@ -62,19 +88,30 @@ function shop.purchaseCar(source,model, plate)
TriggerClientEvent('esx:showNotification', source, 'Sorry but you need: $' .. vPrice .. ' to purchase a ' .. vehicle.name)
return false
end
elseif Config.Payment == 'money' then
local money = xPlayer.getMoney()
if money >= vPrice then
xPlayer.removeMoney(vPrice)
TriggerClientEvent('esx:showNotification', source, 'You have purchased a ' .. vehicle.name .. ' for $' .. vPrice)
end
else
print('Issues finding the vehicle from the config.lua file!')
end
end

function shop.testDrive(source, model)
local car = findCarByModel(model)
if source ~= -1 or source ~= nil and car then
local bucket = GetPlayerRoutingBucket(source)

if bucket == 0 then
local r = math.random(400, 900)
if not inUseBuckets[r] then
inUseBuckets[r] = true
SetPlayerRoutingBucket(source, r)
Player(source).state.bucket = r
return true
else
TriggerClientEvent('esx:showNotification', source, 'Sorry but you need: $' .. vPrice .. ' to purchase a ' .. vehicle.name)
return false
end
else
return false
end
else
print('Issues finding the vehicle from the config.lua file!')
end
end

Expand All @@ -88,3 +125,16 @@ function findCarByModel(model)

return false
end

local Debug = false

if Debug then
RegisterCommand('getPlayerBucket', function(source)
local bucket = GetPlayerRoutingBucket(source)
print('Bucket: ' .. bucket)
end)

RegisterCommand('resetPlayerBucket', function (source)
SetPlayerRoutingBucket(source, 0)
end)
end
3 changes: 2 additions & 1 deletion shared/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Config.PlateLetters = 3
Config.PlateNumbers = 3
Config.PlateUseSpace = true
Config.Payment = 'bank'
Config.TestTime = 30 -- Sekundes Nahui!

-- This table is used to configure different shops!
Config.Shops = {
Expand All @@ -13,7 +14,7 @@ Config.Shops = {
description = 'At our dealership you can find only premium quality vehicles that are brand new and not used!',
pos = vec3(-33.942856, -1102.008789, 26.415405), -- Vec3(x, y, z)
preview = vec3(-43.279121, -1099.938477, 26.415405), previewHeading = 68.031494, -- Vec3(x, y, z)
category = 'sports', -- Vehicle Category
category = {'sports', 'suv', 'bmw', 'audi'}, -- Vehicle Category you can have a table of categories or a string example: 'sports' or {'sports', 'offroad'} don't remove X from the categories list!
blip = { -- Blip Settings
color = 3,
sprite = 355,
Expand Down
6 changes: 3 additions & 3 deletions web/dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" href="./favicon.ico" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite App</title>
<script type="module" crossorigin src="./assets/index.36a13a7b.js"></script>
<link rel="stylesheet" href="./assets/index.45dbf8be.css">
<script type="module" crossorigin src="/assets/index.09663dc6.js"></script>
<link rel="stylesheet" href="/assets/index.f264da9f.css">
</head>
<body>
<div id="app"></div>
Expand Down
Loading

0 comments on commit decd92d

Please sign in to comment.