Skip to content

Commit

Permalink
update(all): spawn coords now take vec3/4 or arrray of coords
Browse files Browse the repository at this point in the history
  • Loading branch information
mikigoalie committed Nov 6, 2023
1 parent cbeaba6 commit bc515f5
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 55 deletions.
15 changes: 15 additions & 0 deletions server/modules/database.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
local db = {}

-- Add a vehicle to the database.

---@param {Object} data
---@param {string} data.identifier
---@param {string} data.plate
---@param {string} data.properties - Already json.
---@param {string} data.hasFreeCoords - Boolean.
---@param {string} data.vehicleType - A vehicle type for database, ESX specific.
---@param {string} data.vehicleName - A name of the vehicle.

db.addVehicle = function(data)
return MySQL.insert.await('INSERT INTO owned_vehicles (`owner`, `plate`, `vehicle`, `stored`, `type`) VALUES (?, ?, ?, ?, ?)', {data.identifier, data.plate, data.properties, not hasFreeCoords, data.vehicleType})
end

db.select = function(query, params)
return MySQL.query.await(query, params)
end
Expand Down
97 changes: 44 additions & 53 deletions server/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ local plate = require('server.modules.plate')
local functions = require('server.modules.functions')
local framework = require('server.bridge.framework')
local dprint = require('shared.modules.dprint')
local spawnVehicle = require('server.modules.spawnVehicle')
local hooks = require('server.bridge.hooks')

local function initializedThread()
if GetCurrentResourceName() ~= 'lsrp_vehicleshop' then
Expand All @@ -22,77 +24,66 @@ local function initializedThread()
print('^2LSRP Vehicleshop initialized^0')
end MySQL.ready(initializedThread)

lib.callback.register('lsrp_vehicleShop:server:payment', function(source, useBank, _shopIndex, _selected, _secondary)
if not _shopIndex or not _selected or not _secondary then
return false
end
local function addVehicleToPlayer(source, vehicle, shopIndex, selectIndex, paymentMethod)

local clientData = lib.callback.await('lsrp_vehicleshop:server:proceed', source, shopIndex)
if not clientData.vehicleProperties or clientData.vehicleProperties.model ~= vehicle.VEHICLE_MODEL then return false end

local vehicleData = Config.VEHICLE_LIST[Config.vehicleShops[_shopIndex].VEHICLE_LIST][_selected].values[_secondary]
local VEHICLE_PRICE =vehicleData.VEHICLE_PRICE
if plate.plateTaken(clientData.vehicleProperties.plate) then
clientData.vehicleProperties.plate = plate.getPlate()
end

if not tonumber(VEHICLE_PRICE) or VEHICLE_PRICE < 1000 then return false end
local playerIdentifier = framework.getPlayerIdentifier(source)
if not playerIdentifier then return false end

if Config.vehicleShops[_shopIndex].license then
local gotLicense = framework.checkLicense(source, Config.vehicleShops[_shopIndex].license)
if not gotLicense then
return 'license'
end
end
local success = db.addVehicle({
identifier = playerIdentifier,
plate = clientData.vehicleProperties.plate,
properties = json.encode(clientData.vehicleProperties),
hasFreeCoords = clientData.vehicleSpawnCoords and true,
vehicleType = Config.VEHICLE_LIST[Config.vehicleShops[shopIndex].VEHICLE_LIST][selectIndex].dbData,
vehicleName = vehicle.label
})

if not success then return false end

if not useBank then
return framework.paymentCASH(source, VEHICLE_PRICE)
if type(clientData.vehicleSpawnCoords) == "vector3" or type(clientData.vehicleSpawnCoords) == "vector4" then
spawnVehicle(vehicle.VEHICLE_MODEL, clientData.vehicleSpawnCoords, clientData.vehicleProperties, playerIdentifier)
end

local bankMoney = framework.getBankBalance(source)
if bankMoney < VEHICLE_PRICE then
hooks.onVehiclePurchase(vehicle.label, clientData.vehicleProperties.plate)
dprint(('Purchase made! Player %s has purchased %s for %s'):format(source, vehicle.VEHICLE_MODEL, vehicle.VEHICLE_PRICE))
functions.log({['Vehicle model'] = vehicle.label, ['Price'] = vehicle.VEHICLE_PRICE, ['Plate'] = clientData.vehicleProperties.plate, ['Buyer'] = GetPlayerName(source), ['Player identifier'] = playerIdentifier, ['Payment type'] = paymentMethod, ['Spawned around shop'] = clientData.vehicleSpawnCoords and true})

return true, clientData.vehicleSpawnCoords and true, clientData.vehicleProperties.plate
end

lib.callback.register('lsrp_vehicleShop:server:payment', function(source, paymentMethod, shopIndex, selectIndex, scrollIndex)
if not shopIndex or not selectIndex or not scrollIndex then
dprint(('Player with ID: %s has called a callback without args. Possible cheating'):format(source))
return false
end

return framework.paymentBANK(source, vehicleData)
end)



local vehicleData = Config.VEHICLE_LIST[Config.vehicleShops[shopIndex].VEHICLE_LIST][selectIndex].values[scrollIndex]
local VEHICLE_PRICE = vehicleData.VEHICLE_PRICE



lib.callback.register('lsrp_vehicleShop:server:addVehicle', function(source, vehProperties, vehicleSpot, _shopIndex, _selected, _secondary, useBank)
local player = framework.getPlayer(source)
if not player then return false end

local _vehProps = vehProperties
local data = Config.VEHICLE_LIST[Config.vehicleShops[_shopIndex].VEHICLE_LIST][_selected].values[_secondary]

if _vehProps.model ~= data.VEHICLE_MODEL then
return false
if type(VEHICLE_PRICE) ~= 'number' or VEHICLE_PRICE < 1000 then
dprint(('An error has occured while getting vehicle price for vehicle %s'):format(vehicleData?.VEHICLE_MODEL))
return false
end

if plate.plateTaken(_vehProps.plate) then
_vehProps.plate = plate.getPlate()
if Config.vehicleShops[shopIndex].license then
if not framework.checkLicense(source, Config.vehicleShops[shopIndex].license) then
return false
end
end

local playerIdentifier = framework.getPlayerIdentifier(source)
if not playerIdentifier then return false end

local success = MySQL.insert.await('INSERT INTO owned_vehicles (`owner`, `plate`, `vehicle`, `stored`, `type`, `name`) VALUES (?, ?, ?, ?, ?, ?)', {playerIdentifier, _vehProps.plate, json.encode(_vehProps), vehicleSpot ~= 0, Config.VEHICLE_LIST[Config.vehicleShops[_shopIndex].VEHICLE_LIST][_selected].dbData, data.label})
if vehicleSpot == 0 then
ESX.OneSync.SpawnVehicle(data.VEHICLE_MODEL, Config.vehicleShops[_shopIndex].PURCHASED_VEHICLE_SPAWNS.xyz, Config.vehicleShops[_shopIndex].PURCHASED_VEHICLE_SPAWNS.w, _vehProps, function(NetworkId)
Wait(100)
local Vehicle = NetworkGetEntityFromNetworkId(NetworkId)
if DoesEntityExist(Vehicle) then
SetVehicleDoorsLocked(Vehicle, 1)
local _vehicleState = Entity(Vehicle).state
_vehicleState:set('owner', playerIdentifier:sub(1, 10), true)
end
end)
if not framework.payment(source, paymentMethod, VEHICLE_PRICE) then
return false
end

functions.log({['Vehicle model'] = data.label, ['Price'] = data.VEHICLE_PRICE, ['Plate'] = _vehProps.plate, ['Buyer'] = GetPlayerName(source), ['Player identifier'] = playerIdentifier, ['Payment type'] = useBank and locale('bank') or locale('cash')})

return success, _vehProps.plate, vehicleSpot ~= 0, Vehicle


return addVehicleToPlayer(source, vehicleData, shopIndex, selectIndex, paymentMethod)
end)


Expand Down
2 changes: 1 addition & 1 deletion shared/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Config.vehicleShops = {
MENU_ICON = 'fa-solid fa-car',
SHOP_COORDS = vec3(-32.7748, -1095.4304, 27.2744),
PREVIEW_COORDS = vec4(-47.6072, -1092.1250, 26.7543, 90.0),
PURCHASED_VEHICLE_SPAWNS = vec4(-23.6204, -1094.3016, 27.0452, 339.1980),
PURCHASED_VEHICLE_SPAWNS = {vec4(-10.0278, -1095.0568, 27.0321, 178.7414), vec4(-14.3705, -1108.2189, 26.9248, 158.5954)},
VEHICLE_LIST = 'vehicles',
BLIP_DATA = {color = 5, sprite = 810, scale = 0.8},
NPC_DATA = {model = joaat('IG_Avon'), position = vec4(-30.7224, -1096.5004, 26.2744, 68.4467)},
Expand Down
2 changes: 1 addition & 1 deletion shared/vehiclelist.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Config.VEHICLE_LIST = {
{label = 'Zion', VEHICLE_MODEL = joaat('zion'), VEHICLE_PRICE = 82000},
{label = 'Zion Cabrio', VEHICLE_MODEL = joaat('zion2'), VEHICLE_PRICE = 82000},
{label = 'Sentinel XS', VEHICLE_MODEL = joaat('sentinel'), VEHICLE_PRICE = 77000},
{label = 'Sentinel ', VEHICLE_MODEL = joaat('sentinel2'), VEHICLE_PRICE = 80000},
{label = 'Sentinel', VEHICLE_MODEL = joaat('sentinel2'), VEHICLE_PRICE = 80000},
{label = 'Windsor', VEHICLE_MODEL = joaat('windsor'), VEHICLE_PRICE = 95000},
{label = 'Windsor Drop', VEHICLE_MODEL = joaat('windsor2'), VEHICLE_PRICE = 95000},
{label = 'Felon', VEHICLE_MODEL = joaat('felon'), VEHICLE_PRICE = 83000},
Expand Down

0 comments on commit bc515f5

Please sign in to comment.