Skip to content

Commit

Permalink
refactor(server/queue): move joining timeout code to a new function
Browse files Browse the repository at this point in the history
  • Loading branch information
D4isDAVID authored and ChatDisabled committed Dec 24, 2023
1 parent fc54a8c commit a81b39e
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions server/queue.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,31 @@ end
local joiningPlayers = {}
local joiningPlayerCount = 0

---@param license string
local function awaitPlayerJoinsOrDisconnects(license)
local joiningData
while true do
joiningData = joiningPlayers[license]

-- wait until the player finally joins or disconnects while installing server content
-- this may result in waiting ~2 additional minutes if the player disconnects as FXServer will think that the player exists
while DoesPlayerExist(joiningData.source --[[@as string]]) do
Wait(1000)
end

-- wait until either the player reconnects or was disconnected for too long
while joiningPlayers[license] and joiningPlayers[license].source == joiningData.source and (os.time() - joiningData.timestamp) < config.joiningTimeoutSeconds do
Wait(1000)
end

-- if the player disconnected for too long stop waiting for them
if joiningPlayers[license] and joiningPlayers[license].source == joiningData.source then
removePlayerJoining(license)
break
end
end
end

---@param source Source
---@param license string
local function updatePlayerJoining(source, license)
Expand Down Expand Up @@ -140,27 +165,7 @@ local function awaitPlayerQueue(source, license, deferrals)
dequeue(license)
deferrals.done()

local joiningData
while true do
joiningData = joiningPlayers[license]

-- wait until the player finally joins or disconnects while installing server content
-- this may result in waiting ~2 additional minutes if the player disconnects as FXServer will think that the player exists
while DoesPlayerExist(joiningData.source --[[@as string]]) do
Wait(1000)
end

-- wait until either the player reconnects or was disconnected for too long
while joiningPlayers[license] and joiningPlayers[license].source == joiningData.source and (os.time() - joiningData.timestamp) < config.joiningTimeoutSeconds do
Wait(1000)
end

-- if the player disconnected for too long stop waiting for them
if joiningPlayers[license] and joiningPlayers[license].source == joiningData.source then
removePlayerJoining(license)
break
end
end
awaitPlayerJoinsOrDisconnects(license)
end

return {
Expand Down

0 comments on commit a81b39e

Please sign in to comment.