Skip to content

Commit

Permalink
feat: ox cron, playerId in commands as mandatory
Browse files Browse the repository at this point in the history
  • Loading branch information
artur-michalak committed May 15, 2024
1 parent bb05905 commit 223d71c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 21 deletions.
2 changes: 1 addition & 1 deletion client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ end

local function areKeysJobShared(veh)
local vehName = GetDisplayNameFromVehicleModel(GetEntityModel(veh))
local vehPlate = GetVehicleNumberPlateText(veh)
local vehPlate = qbx.getVehiclePlate(veh)
for job, v in pairs(config.sharedKeys) do
if job == QBX.PlayerData.job.name then
if config.sharedKeys[job].requireOnduty and not QBX.PlayerData.job.onduty then return false end
Expand Down
2 changes: 1 addition & 1 deletion config/server.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
return {
keysCleanTime = 300 -- ~5min
runClearCronMinutes = 5,
}
9 changes: 3 additions & 6 deletions server/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ lib.addCommand(locale('addcom.givekeys'), {
name = locale('addcom.givekeys_id'),
type = 'playerId',
help = locale('addcom.givekeys_id_help'),
optional = true
},
{
name = locale('addcom.givekeys_plate'),
Expand Down Expand Up @@ -47,7 +46,7 @@ local function getPlayersVehiclePlate(source, plate)
local vehicle = GetVehiclePedIsIn(ped, false)

if vehicle == 0 then return end
plate = GetVehicleNumberPlateText(vehicle)
plate = qbx.getVehiclePlate(vehicle)
end

return plate
Expand All @@ -60,7 +59,6 @@ lib.addCommand(locale('addcom.addkeys'), {
name = locale('addcom.addkeys_id'),
type = 'playerId',
help = locale('addcom.addkeys_id_help'),
optional = true
},
{
name = locale('addcom.addkeys_plate'),
Expand All @@ -71,7 +69,7 @@ lib.addCommand(locale('addcom.addkeys'), {
},
restricted = 'group.admin',
}, function (source, args)
local playerId = args[locale('addcom.addkeys_id')] or source
local playerId = args[locale('addcom.addkeys_id')]
local plate = getPlayersVehiclePlate(source, args[locale('addcom.addkeys_plate')])

if not playerId or not plate then
Expand All @@ -92,7 +90,6 @@ lib.addCommand(locale('addcom.removekeys'), {
name = locale('addcom.removekeys_id'),
type = 'playerId',
help = locale('addcom.removekeys_id_help'),
optional = true
},
{
name = locale('addcom.removekeys_plate'),
Expand All @@ -103,7 +100,7 @@ lib.addCommand(locale('addcom.removekeys'), {
},
restricted = 'group.admin',
}, function (source, args)
local playerId = args[locale('addcom.removekeys_id')] or source
local playerId = args[locale('addcom.removekeys_id')]
local plate = getPlayersVehiclePlate(source, args[locale('addcom.removekeys_plate')])

if not playerId or not plate then
Expand Down
25 changes: 12 additions & 13 deletions server/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,23 @@ function ToggleDoorState(entity)
end

local public = {}
local config = require 'config.server'
local debug = GetConvarInt(('%s-debug'):format(GetCurrentResourceName()), 0) == 1

local keysList = {} ---holds key status for some time after player logs out (Prevents frustration by crashing the client)
local keysLifetime = {} ---Life timestamp of the keys of a character who has logged out

---CRON: Removes old keys from server memory
CreateThread(function ()
while true do
Wait(300 * 1000)
local time = os.time()
for citizenId, lifetime in pairs(keysLifetime) do
if lifetime + 300 < time then
keysList[citizenId] = nil
keysLifetime[citizenId] = nil
end
---Removes old keys from server memory
lib.cron.new('*/'..config.runClearCronMinutes ..' * * * *', function ()
local time = os.time()
local seconds = config.runClearCronMinutes * 60
for citizenId, lifetime in pairs(keysLifetime) do
if lifetime + seconds < time then
keysList[citizenId] = nil
keysLifetime[citizenId] = nil
end
end
end)
---CRON
end, {debug = debug})

---Gets Citizen Id based on source
---@param source number ID of the player
Expand Down Expand Up @@ -167,7 +166,7 @@ function public.removePlayer(src)
local citizenid = getCitizenId(src)
if not citizenid then return end

keysList[citizenid] = Player(src).state['keysList']
keysList[citizenid] = Player(src).state.keysList
keysLifetime[citizenid] = os.time()

Player(src).state:set('keysList', nil, true)
Expand Down

0 comments on commit 223d71c

Please sign in to comment.