Skip to content

Commit

Permalink
Merge branch 'Qbox-project:main' into debug
Browse files Browse the repository at this point in the history
  • Loading branch information
artur-michalak authored Jun 10, 2024
2 parents ff4b708 + b89be3a commit 31cc649
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 192 deletions.
33 changes: 0 additions & 33 deletions client/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,6 @@ local isCloseToCoords = functions.isCloseToCoords
local alertSend = false
local public = {}

---Checks if the current player has a key for the specified vehicle.
---@param vehicle number The entity number of the vehicle to check for a key.
---@return boolean? `true` if the player has a key for the vehicle, nil otherwise.
function HasKey(vehicle)
if not vehicle or type(vehicle) ~= 'number' then return end
local ent = Entity(vehicle)
if not ent or not ent.state.keys then return end
return ent.state.keys[QBX.PlayerData.citizenid]
end

---Attempt to Give a key to a target player for the specified vehicle.
---@param targetPlayerId number The ID of the target player who will receive the key.
---@param vehicle number The entity number of the vehicle for which the key is being given.
function GiveKey(targetPlayerId, vehicle)
-- This function is not yet implemented
-- Will call the corresponding callback
end

---Attempt to Remove a key from a target player for the specified vehicle.
---@param targetPlayerId number The ID of the target player from whom the key is being removed.
---@param vehicle number The entity number of the vehicle from which the key is being removed.
function RemoveKey(targetPlayerId, vehicle)
-- This function is not yet implemented
-- Will call the corresponding callback
end

---Toggles the state of a vehicle's doors. If a door is open, it will be closed, and if it's closed, it will be opened.
---@param vehicle number The entity number of the vehicle for which the door state is being toggled.
function ToggleVehicleDoor(vehicle)
-- This function is not yet implemented
-- Will call the corresponding callback
end

---Checks if player has vehicle keys
---@param plate string The plate number of the vehicle.
---@return boolean? `true` if player has vehicle keys, `nil` otherwise.
Expand Down
88 changes: 1 addition & 87 deletions server/functions.lua
Original file line number Diff line number Diff line change
@@ -1,89 +1,3 @@
---Checks for the existence of a key.
---@param entity number The entity (vehicle) where we check for the existence of a key.
---@param citizenid string The CitizenID of the player whose key we check for.
---@return boolean? `true` if the player has a key for the vehicle, nil otherwise.
function HasKey(entity, citizenid)
if not entity or type(entity) ~= 'number' or not citizenid or type(citizenid) ~= 'string' then return end
local ent = Entity(entity)
if not ent or not ent.state.keys then return end
return ent.state.keys[citizenid]
end

---Adds a key to the selected vehicle entity and returns a success status.
---@param entity number The entity (vehicle) to which the key is added.
---@param citizenid string The CitizenID of the player whose key is being added.
---@param doorState number | nil -- Sets the doorState of the vehicle if present
---@return boolean? `true` if the key was successfully added, nil otherwise.
function GiveKey(entity, citizenid, doorState)
if not entity or type(entity) ~= 'number' or not citizenid or type(citizenid) ~= 'string' then return end

local ent = Entity(entity)
if not ent then return end

if doorState then
ent.state:set('doorState', doorState, true)
end

local keyholders = ent.state.keys or {}

if not keyholders[citizenid] then
keyholders[citizenid] = true
ent.state:set('keys', keyholders, true)
return true
end
end

---Removes a key from the selected vehicle entity and returns a success status.
---@param entity number The entity (vehicle) from which the key is removed.
---@param citizenid string The CitizenID of the player whose key is being removed.
---@return boolean? `true` if the key was successfully removed, `nil` otherwise.
function RemoveKey(entity, citizenid)
if not entity or type(entity) ~= 'number' or not citizenid or type(citizenid) ~= 'string' then
return
end

local ent = Entity(entity)
if not ent then return end

local keyholders = ent.state.keys
if keyholders and keyholders[citizenid] then
keyholders[citizenid] = nil
ent.state:set('keys', keyholders, true)
return true
end
end

---Sets the door state of the vehicle.
---@param entity number The entity (vehicle) for which the door state is updated.
---@param doorState number The door state number to update.
---@return boolean? `true` if the door state was successfully updated, `nil` otherwise.
function SetDoorState(entity, doorState)
if not entity or type(entity) ~= 'number' or not doorState or type(doorState) ~= 'number' then return end

local ent = Entity(entity)
if not ent then return end

ent.state:set('doorState', doorState, true)
return true
end

---Toggles the door state of the vehicle between open and closed.
---@param entity number The entity (vehicle) for which the door state is being toggled.
---@return number | nil returns the new doorState of the vehicle
function ToggleDoorState(entity)
if not entity or type(entity) ~= 'number' then return end

local ent = Entity(entity)
if not ent then return end
if ent.state.doorState and ent.state.doorState ~= 0 then
ent.state:set('doorState', 1, true)
return 1
else
ent.state:set('doorState', 0, true)
return 0
end
end

local public = {}
local config = require 'config.server'
local isDebug = GetConvar('ox:printlevel:' .. cache.resource, GetConvar('ox:printlevel', 'info')) == 'debug'
Expand Down Expand Up @@ -132,7 +46,7 @@ function public.addPlayer(src)
local citizenid = getCitizenId(src)
if not citizenid then return end

local vehicles = MySQL.query.await('SELECT * FROM player_vehicles WHERE citizenid = ?', { citizenid })
local vehicles = MySQL.query.await('SELECT plate FROM player_vehicles WHERE citizenid = ?', { citizenid })

local platesAssociations = {}
for i = 1, #vehicles do
Expand Down
72 changes: 0 additions & 72 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,45 +46,6 @@ RegisterNetEvent('qb-vehiclekeys:server:setVehLockState', function(vehNetId, sta
SetVehicleDoorsLocked(NetworkGetEntityFromNetworkId(vehNetId), state)
end)

---Gives a key to an entity based on the player's CitizenID.
---@param id integer The player's ID.
---@param netId number The network ID of the entity.
---@param doorState number | nil Sets the door state if given
RegisterNetEvent('qb-vehiclekeys:server:GiveKey', function(id, netId, doorState)
if source == -1 then
-- This event is not yet implemented
else
-- drop player
end
end)

---Removes a key from an entity based on the player's CitizenID.
---@param id integer The player's ID.
---@param netId number The network ID of the entity.
RegisterNetEvent('vehiclekeys:server:RemoveKey', function(id, netId)
if source == -1 then
-- This event is not yet implemented
else
-- drop player
end
end)

exports('RemoveKey', RemoveKey)

---Sets the door state to a desired value.
---This event is expected to be called only by the server.
---@param netId number The network ID of the entity.
---@param doorState number | nil Sets the door state if given
RegisterNetEvent('vehiclekeys:server:SetDoorState', function(netId, doorState)
if source == -1 then
-- This event is not yet implemented
else
-- drop player
end
end)

exports('SetDoorState', SetDoorState)

RegisterNetEvent('QBCore:Server:OnPlayerLoaded', function()
addPlayer(source --[[@as integer]])
end)
Expand All @@ -97,36 +58,3 @@ end)
AddEventHandler('playerDropped', function()
removePlayer(source --[[@as integer]])
end)

-----------------------
---- Callbacks ----
-----------------------

---Gives a key to an entity based on the target player's CitizenID but only if the owner already has a key.
---@param source number ID of the player
---@param netId number The network ID of the entity.
---@param targetPlayerId number ID of the target player who receives the key
---@return boolean?
lib.callback.register('vehiclekeys:server:GiveKey', function(source, netId, targetPlayerId)
if not source or not netId or not targetPlayerId then return end
-- This callback is not yet implemented
end)

---Removes a key from an entity based on the target player's CitizenID but only if the owner has a key.
---@param source number ID of the player
---@param netId number The network ID of the entity.
---@param targetPlayerId number ID of the target player who receives the key
---@return boolean?
lib.callback.register('vehiclekeys:server:RemoveKey', function(source, netId, targetPlayerId)
if not source or not netId or not targetPlayerId then return end
-- This callback is not yet implemented
end)

---Toggles the door state of the vehicle between open and closed.
---@param source number ID of the player
---@param netId number The network ID of the entity
---@return number | nil -- Returns the current Door State
lib.callback.register('vehiclekeys:server:ToggleDoorState', function(source, netId)
if not source or not netId then return end
-- This callback is not yet implemented
end)

0 comments on commit 31cc649

Please sign in to comment.