diff --git a/server/main.lua b/server/main.lua index 731c724..f8d6ccc 100644 --- a/server/main.lua +++ b/server/main.lua @@ -21,10 +21,6 @@ local State = { ---@field depotprice number ---@field drivingdistance number ---@field status string ----@field balance number ----@field paymentamount number ----@field paymentsleft number ----@field financetime number ---@class CreateEntityQuery ---@field citizenId string The citizen id of the owner @@ -35,8 +31,9 @@ local State = { --- Creates a Vehicle DB Entity ---@param query CreateEntityQuery +---@return vehicleId integer local function createEntity(query) - MySQL.insert('INSERT INTO player_vehicles (license, citizenid, vehicle, hash, mods, plate, state) VALUES ((SELECT license FROM players WHERE citizenid = ?),?,?,?,?,?,?)', { + return MySQL.insert.await('INSERT INTO player_vehicles (license, citizenid, vehicle, hash, mods, plate, state) VALUES ((SELECT license FROM players WHERE citizenid = ?),?,?,?,?,?,?)', { query.citizenId, query.citizenId, query.model, @@ -133,6 +130,14 @@ end exports('DeleteEntityByPlate', deleteEntityByPlate) +--- Deletes a DB Vehicle Entity through searching for the vehicle id +---@param id integer +local function deleteEntityById(id) + MySQL.query('DELETE FROM player_vehicles WHERE id = ?', {id}) +end + +exports('DeleteEntityById', deleteEntityById) + --- Returns if the given plate exists ---@param plate string ---@return boolean diff --git a/vehicles.sql b/vehicles.sql index d5f7c00..779b301 100644 --- a/vehicles.sql +++ b/vehicles.sql @@ -5,7 +5,7 @@ CREATE TABLE IF NOT EXISTS `player_vehicles` ( `vehicle` varchar(50) DEFAULT NULL, `hash` varchar(50) DEFAULT NULL, `mods` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL, - `plate` varchar(50) NOT NULL, + `plate` varchar(15) NOT NULL, `fakeplate` varchar(50) DEFAULT NULL, `garage` varchar(50) DEFAULT NULL, `fuel` int(11) DEFAULT 100, @@ -15,12 +15,8 @@ CREATE TABLE IF NOT EXISTS `player_vehicles` ( `depotprice` int(11) NOT NULL DEFAULT 0, `drivingdistance` int(50) DEFAULT NULL, `status` text DEFAULT NULL, - `balance` int(11) NOT NULL DEFAULT 0, - `paymentamount` int(11) NOT NULL DEFAULT 0, - `paymentsleft` int(11) NOT NULL DEFAULT 0, - `financetime` int(11) NOT NULL DEFAULT 0, PRIMARY KEY (`id`), - KEY `plate` (`plate`), - KEY `citizenid` (`citizenid`), - KEY `license` (`license`) -) ENGINE=InnoDB AUTO_INCREMENT=1; + UNIQUE KEY `plate` (`plate`), + FOREIGN KEY (`citizenid`) REFERENCES `players` (`citizenid`) ON DELETE CASCADE ON UPDATE CASCADE, + FOREIGN KEY (`license`) REFERENCES `players` (`license`) ON DELETE CASCADE ON UPDATE CASCADE +) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;