local W=tick()local S={}function Queue._addToQueue(v)local G=Conf.min_queue_players-Queue._currentQueueCount()if G==1 then W=tick()end S[v]=tick()Queue._sendStatus()end function Queue._removeFromQueue(W)S[W]=nil Queue._sendStatus()end function Queue._closeQueue()local W=S S={}return W end function Queue._currentQueueCount()local W=0 for S,v in pairs(S)do W=W+1 end return W end function Queue._teleportQueue()if Conf.disable_queue then return end local S=Queue._destinationId local v=Queue._gameMode local G=Queue._closeQueue()W=tick()Log.Debug.Queue("\082\101\115\101\114\118\105\110\103 \102\111\114 \037\115",S)local A=TeleportService:ReserveServer(S)Log.Debug.Queue("\082\101\115\101\114\118\101\100 \037\115 \102\111\114 \112\108\097\099\101 \037\115",A,S)local R={}for W,S in pairs(G)do table.insert(R,W)end local f={isolated_server=true;game_mode=v;player_count=#R}Log.Debug.Queue("\084\101\108\101\112\111\114\116\105\110\103 \037\115 \112\108\097\121\101\114\115 \116\111 \037\115 \040\037\115\041",#R,S,v)TeleportService:TeleportToPrivateServer(S,A,R,nil,f)end function Queue.setTimeoutState(S)if S and not Queue._timeoutEnabled then Log.Debug.Queue("\082\101-\101\110\097\098\108\101\100 \116\105\109\101\111\117\116\046  \082\101\115\116\097\114\116\105\110\103 \099\111\117\110\116\100\111\119\110")W=tick()end Queue._timeoutEnabled=S Log.Debug.Queue("\084\105\109\101\111\117\116 \115\116\097\116\101\058 \037\115",tostring(S))Queue._sendStatus()end function Queue.teleportQueue()Log.Debug.Queue("\080\114\111\103\114\101\115\115\105\110\103\046  \066\117\099\107\108\101 \117\112 \102\111\114 \102\117\110\046  \042\077\065\088\073\077\085\077\042 \102\117\110\046\046\046")Queue._teleportQueue()end
-----------------------

local _lastTeleport = tick()
local _currentQueue = {}

function Queue._addToQueue(player)
	local waitingFor = Conf.min_queue_players - Queue._currentQueueCount()

	-- If this was the last player we were waiting for, start the queue timeout countdown
	if waitingFor == 1 then
		_lastTeleport = tick()
	end

	_currentQueue[player] = tick()

	Queue._sendStatus()
end

function Queue._removeFromQueue(player)
	_currentQueue[player] = nil
	Queue._sendStatus()
end

function Queue._closeQueue()
	local thisQueue = _currentQueue
	_currentQueue = {}
	return thisQueue
end

function Queue._currentQueueCount()
	local count = 0

	for _, _ in pairs(_currentQueue) do
		count = count + 1
	end

	return count
end

function Queue._teleportQueue()
	if Conf.disable_queue then
		-- Don't kick off a game if the queue is not mean tot be enabled
		return
	end

	local destinationId = Queue._destinationId
	local gameMode = Queue._gameMode

	local thisQueue = Queue._closeQueue()
	_lastTeleport = tick()

	Log.Debug.Queue("Reserving for %s", destinationId)

	local token = TeleportService:ReserveServer(destinationId)
	Log.Debug.Queue("Reserved %s for place %s", token, destinationId)

	local playerList = {}
	for player, queueTime in pairs(thisQueue) do
		table.insert(playerList, player)
	end

	local creationData = {
		isolated_server = true,
		game_mode = gameMode,
		player_count = #playerList,
	}

	Log.Debug.Queue("Teleporting %s players to %s (%s)", #playerList, destinationId, gameMode)

	TeleportService:TeleportToPrivateServer(destinationId, token, playerList, nil, creationData)
end

function Queue.setTimeoutState(state)
	if state and not Queue._timeoutEnabled then
		Log.Debug.Queue("Re-enabled timeout.  Restarting countdown")
		_lastTeleport = tick()
	end

	Queue._timeoutEnabled = state

	Log.Debug.Queue("Timeout state: %s", tostring(state))

	Queue._sendStatus()
end

function Queue.teleportQueue()
	Log.Debug.Queue("Progressing.  Buckle up for fun.  *MAXIMUM* fun...")

	Queue._teleportQueue()
end
