244 lines
8.5 KiB
Lua
244 lines
8.5 KiB
Lua
if Config.Framework ~= "qb" then return end
|
|
local function DetectGarageSystem()
|
|
local garageSystems = {
|
|
{ name = "cd_garage", resource = "cd_garage" },
|
|
{ name = "loaf_garage", resource = "loaf_garage" },
|
|
{ name = "jg-advancedgarages", resource = "jg-advancedgarages" },
|
|
{ name = "qs-advancedgarages", resource = "qs-advancedgarages" },
|
|
{ name = "codem-garage", resource = "codem-garage" },
|
|
{ name = "qb-garages", resource = "qb-garages" },
|
|
{ name = "tgiann-real-parking", resource = "tgiann-real-parking" },
|
|
{ name = "okokGarage", resource = "okokGarage" },
|
|
{ name = "op-garages", resource = "op-garages" },
|
|
}
|
|
|
|
for _, system in ipairs(garageSystems) do
|
|
if GetResourceState(system.resource) == "started" then
|
|
DebugPrint("Garage system detected: " .. system.name)
|
|
return system.name
|
|
end
|
|
end
|
|
|
|
return "default"
|
|
end
|
|
|
|
local function ProcessVehicleData(vehicle, garageSystem)
|
|
if not vehicle then return nil end
|
|
local processedVehicle = {
|
|
plate = vehicle.plate,
|
|
stored = false,
|
|
location = "out",
|
|
garage = nil,
|
|
type = nil,
|
|
model = nil,
|
|
statistics = {
|
|
engine = 100,
|
|
body = 100,
|
|
fuel = 100
|
|
}
|
|
}
|
|
|
|
if vehicle.stored == nil then
|
|
processedVehicle.stored = vehicle.state == 1
|
|
else
|
|
if type(vehicle.stored) == "boolean" then
|
|
processedVehicle.stored = vehicle.stored
|
|
else
|
|
processedVehicle.stored = vehicle.stored == 1
|
|
end
|
|
end
|
|
if garageSystem == "cd_garage" then
|
|
processedVehicle.stored = vehicle.in_garage == 1 or vehicle.in_garage == true
|
|
processedVehicle.garage = vehicle.garage_id
|
|
processedVehicle.type = vehicle.garage_type
|
|
elseif garageSystem == "loaf_garage" then
|
|
processedVehicle.stored = true
|
|
elseif garageSystem == "jg-advancedgarages" then
|
|
processedVehicle.stored = vehicle.in_garage == 1 or vehicle.in_garage == true
|
|
processedVehicle.garage = vehicle.garage_id
|
|
elseif garageSystem == "qs-advancedgarages" then
|
|
processedVehicle.stored = vehicle.garage ~= "out"
|
|
processedVehicle.garage = vehicle.garage
|
|
elseif garageSystem == "codem-garage" then
|
|
processedVehicle.stored = vehicle.stored == 1 or vehicle.stored == true
|
|
processedVehicle.garage = vehicle.parking
|
|
elseif garageSystem == "tgiann-real-parking" then
|
|
processedVehicle.stored = exports["tgiann-realparking"]:IsVehicleParkedAnyGarage(vehicle.plate) or false
|
|
processedVehicle.garage = ''
|
|
elseif garageSystem == "qb-garages" then
|
|
processedVehicle.stored = vehicle.state == 1 or vehicle.state == true
|
|
processedVehicle.garage = vehicle.garage
|
|
elseif garageSystem == "okokGarage" then
|
|
processedVehicle.stored = vehicle.state == 1 or vehicle.state == true
|
|
processedVehicle.garage = vehicle.garage
|
|
elseif garageSystem == "op-garages" then
|
|
processedVehicle.stored = vehicle.state == 0 and vehicle.isTowedOut == 0 or
|
|
vehicle.state == false and vehicle.isTowedOut == false
|
|
processedVehicle.garage = vehicle.garage or 'Garage'
|
|
end
|
|
|
|
processedVehicle.location = processedVehicle.stored and (processedVehicle.garage or "garage") or "out"
|
|
|
|
processedVehicle.statistics = {
|
|
engine = vehicle.engine and math.floor(vehicle.engine / 10 + 0.5) or 100,
|
|
body = vehicle.body and math.floor(vehicle.body / 10 + 0.5) or 100,
|
|
fuel = vehicle.fuel or 100
|
|
}
|
|
|
|
if vehicle.mods then
|
|
local success, vehicleMods = pcall(json.decode, vehicle.mods)
|
|
if success and vehicleMods and vehicleMods.model then
|
|
processedVehicle.model = vehicleMods.model
|
|
else
|
|
DebugPrint("Warning: Failed to decode vehicle data for plate: " .. tostring(vehicle.plate))
|
|
end
|
|
end
|
|
|
|
return processedVehicle
|
|
end
|
|
|
|
Core.Functions.GetPlayerVehicles = function(playerId)
|
|
if not playerId then
|
|
DebugPrint('Error: playerId is required')
|
|
return {}
|
|
end
|
|
|
|
local playerIdentifier = Core.Functions.GetIdentifier(playerId)
|
|
if not playerIdentifier then
|
|
DebugPrint('Error: Could not get player identifier for ID: ' .. tostring(playerId))
|
|
return {}
|
|
end
|
|
|
|
local result = MySQL.query.await("SELECT * FROM player_vehicles WHERE citizenid = ?", { playerIdentifier })
|
|
if not result then
|
|
DebugPrint('Error: Database query failed for identifier: ' .. playerIdentifier)
|
|
return {}
|
|
end
|
|
|
|
local garageSystem = DetectGarageSystem()
|
|
|
|
local processedVehicles = {}
|
|
for i = 1, #result do
|
|
local vehicle = result[i]
|
|
if vehicle then
|
|
local processedVehicle = ProcessVehicleData(vehicle, garageSystem)
|
|
if processedVehicle then
|
|
processedVehicles[#processedVehicles + 1] = processedVehicle
|
|
end
|
|
end
|
|
end
|
|
|
|
DebugPrint('Retrieved ' .. #processedVehicles .. ' vehicles for player: ' .. playerIdentifier)
|
|
return processedVehicles
|
|
end
|
|
|
|
|
|
|
|
Core.Functions.GetVehicleData = function(playerId, plate)
|
|
if not playerId then
|
|
DebugPrint('Error: playerId is required')
|
|
return {}
|
|
end
|
|
|
|
local playerIdentifier = Core.Functions.GetIdentifier(playerId)
|
|
if not playerIdentifier then
|
|
DebugPrint('Error: Could not get player identifier for ID: ' .. tostring(playerId))
|
|
return {}
|
|
end
|
|
|
|
local garageSystem = DetectGarageSystem()
|
|
local storedColumn = "state"
|
|
local storedValue = 1
|
|
local outValue = 0
|
|
|
|
|
|
if garageSystem == "cd_garage" or garageSystem == "jg-advancedgarages" then
|
|
storedColumn = "in_garage"
|
|
storedValue = 1
|
|
outValue = 0
|
|
elseif garageSystem == "codem-garage" then
|
|
storedColumn = "stored"
|
|
storedValue = 1
|
|
outValue = 0
|
|
elseif garageSystem == "qs-advancedgarages" then
|
|
storedColumn = "garage"
|
|
elseif garageSystem == "op-garages" then
|
|
storedColumn = "state"
|
|
storedValue = 0
|
|
outValue = 1
|
|
else
|
|
storedColumn = "state"
|
|
storedValue = 1
|
|
outValue = 0
|
|
end
|
|
|
|
local vehicle
|
|
if garageSystem == "qs-advancedgarages" then
|
|
vehicle = MySQL.single.await(
|
|
'SELECT plate, mods, `hash`, fuel FROM player_vehicles WHERE citizenid = ? AND plate = ? AND `garage` != ?',
|
|
{ playerIdentifier, plate, 'out' }
|
|
)
|
|
else
|
|
vehicle = MySQL.single.await(
|
|
('SELECT plate, mods, `hash`, fuel FROM player_vehicles WHERE citizenid = ? AND plate = ? AND `%s` = ?'):format(
|
|
storedColumn),
|
|
{ playerIdentifier, plate, storedValue }
|
|
)
|
|
end
|
|
|
|
if not vehicle then
|
|
DebugPrint('Error: No vehicle found for plate: ' .. plate .. ' or vehicle is not in garage')
|
|
return
|
|
end
|
|
|
|
local affectedRows
|
|
if garageSystem == "qs-advancedgarages" then
|
|
affectedRows = MySQL.update.await(
|
|
"UPDATE player_vehicles SET `garage` = ? WHERE plate = ?",
|
|
{ 'out', plate }
|
|
)
|
|
else
|
|
affectedRows = MySQL.update.await(
|
|
"UPDATE player_vehicles SET `" .. storedColumn .. "` = ? WHERE plate = ?",
|
|
{ outValue, plate }
|
|
)
|
|
end
|
|
|
|
if affectedRows == 0 then
|
|
DebugPrint('Error: Database update failed for plate: ' .. plate)
|
|
return
|
|
end
|
|
|
|
vehicle.model = tonumber(vehicle.hash)
|
|
return vehicle
|
|
end
|
|
|
|
|
|
local function WaitForEntity(entity)
|
|
local timer = GetGameTimer() + 5000
|
|
while not DoesEntityExist(entity) and timer > GetGameTimer() do
|
|
Wait(0)
|
|
end
|
|
return DoesEntityExist(entity)
|
|
end
|
|
|
|
function Core.Functions.CreateServerVehicle(model, coords, heading)
|
|
heading = heading or 0
|
|
local vehicle = CreateVehicle(model, coords.x, coords.y, coords.z, heading, true, true)
|
|
if not WaitForEntity(vehicle) then
|
|
return
|
|
end
|
|
SetEntityIgnoreRequestControlFilter(vehicle, true)
|
|
return vehicle
|
|
end
|
|
|
|
function Core.Functions.CreateServerPed(model, coords, heading)
|
|
heading = heading or 0
|
|
local ped = CreatePed(4, model, coords.x, coords.y, coords.z, heading, true, true)
|
|
if not WaitForEntity(ped) then
|
|
return
|
|
end
|
|
SetEntityIgnoreRequestControlFilter(ped, true)
|
|
return ped
|
|
end
|