Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): full vehicle persistence #23

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOT NULL DEFAULT NULL ? 😅

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad 😅

PRIMARY KEY (`id`),
UNIQUE KEY `plate` (`plate`),
FOREIGN KEY (`citizenid`) REFERENCES `players` (`citizenid`) ON DELETE CASCADE ON UPDATE CASCADE,
Expand Down