Skip to content

Commit

Permalink
feat(server): function implementation
Browse files Browse the repository at this point in the history
* GiveKey() function impl

* RemoveKey() function impl

* SetDoorState() function impl

* ToggleDoorState() function impl

* exception handling in HasKey

* cleanup

* fixing client HasKey function

* bit cleanup and exception handling on server
  • Loading branch information
adweex authored Dec 17, 2023
1 parent 04966aa commit 6d26cd0
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
7 changes: 5 additions & 2 deletions client/functions.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
--- 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, false otherwise.
---@return boolean | nil if the player has a key for the vehicle, nil otherwise.
function HasKey(vehicle)
return Entity(vehicle).state.keys[QBX.PlayerData.citizenid]
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.
Expand Down
57 changes: 52 additions & 5 deletions server/functions.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
--- 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 | nil if the player has a key for the vehicle, nil otherwise.
function HasKey(entity, citizenid)
return Entity(entity).state.keys[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.
Expand All @@ -11,28 +15,71 @@ end
---@param doorState number | nil -- Sets the doorState of the vehicle if present
---@return boolean | nil `true` if the key was successfully added, `nil` otherwise.
function GiveKey(entity, citizenid, doorState)
-- This function is not yet implemented
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 | nil `true` if the key was successfully removed, `nil` otherwise.
function RemoveKey(entity, citizenid)
-- This function is not yet implemented
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 | nil `true` if the door state was successfully updated, `nil` otherwise.
function SetDoorState(entity, doorState)
-- This function is not yet implemented
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)
-- This function is not yet implemented
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

0 comments on commit 6d26cd0

Please sign in to comment.