Skip to content

Commit

Permalink
feat: full vehicle persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
CodexisPhantom committed Nov 13, 2024
1 parent 2f34cdb commit 7c1de9a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
12 changes: 10 additions & 2 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ exports('DoesPlayerVehiclePlateExist', doesEntityPlateExist)
---@field state State
---@field depotPrice integer
---@field props table ox_lib properties table
---@field coords table vector4

---@class PlayerVehiclesFilters
---@field citizenid? string
Expand Down Expand Up @@ -82,7 +83,7 @@ end
---@param filters? PlayerVehiclesInternalFilters
---@return PlayerVehicle[]
local function getPlayerVehiclesInternal(filters)
local query = 'SELECT id, citizenid, vehicle, mods, garage, state, depotprice FROM player_vehicles'
local query = 'SELECT id, citizenid, vehicle, mods, garage, state, depotprice, coords FROM player_vehicles'
local whereClause, placeholders = buildWhereClause(filters)
lib.print.debug(query .. whereClause)
local results = MySQL.query.await(query .. whereClause, placeholders)
Expand All @@ -95,7 +96,8 @@ local function getPlayerVehiclesInternal(filters)
garage = data.garage,
state = data.state,
depotPrice = data.depotprice,
props = json.decode(data.mods)
props = json.decode(data.mods),
coords = json.decode(data.coords)
}
end
return ownedVehicles
Expand Down Expand Up @@ -217,6 +219,7 @@ exports('GetVehicleIdByPlate', getVehicleIdByPlate)
---@field state? State
---@field depotPrice? integer
---@field props? table ox_lib properties table
---@field coords? table vector4

---@param vehicleId integer
---@param options SaveVehicleOptions
Expand Down Expand Up @@ -265,6 +268,11 @@ local function buildSaveVehicleQuery(vehicleId, options)
end
end

if options.coords then
crumbs[#crumbs+1] = 'coords = ?'
placeholders[#placeholders+1] = json.encode(options.coords)
end

placeholders[#placeholders+1] = vehicleId

return string.format('UPDATE player_vehicles SET %s WHERE id = ?', table.concat(crumbs, ',')), placeholders
Expand Down
1 change: 1 addition & 0 deletions vehicles.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ CREATE TABLE IF NOT EXISTS `player_vehicles` (
`depotprice` int(11) NOT NULL DEFAULT 0,
`drivingdistance` int(50) DEFAULT NULL,
`status` text DEFAULT NULL,
`coords` text NOT NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `plate` (`plate`),
FOREIGN KEY (`citizenid`) REFERENCES `players` (`citizenid`) ON DELETE CASCADE ON UPDATE CASCADE,
Expand Down

0 comments on commit 7c1de9a

Please sign in to comment.