local z={}z.__index=z function z.new(j)local I=setmetatable({gameStageHandler=j},z)return I end function z.disableQueueTimeout(z)z._disableQueueTimeout=true end function z.startGame(z,...)MatchmakingDestination.startGame(...)end function z.initialize(z)for z,j in pairs(Players:GetPlayers())do Util.loadCharacter(j)end spawn(function()DamageHandler.setup()end)MatchmakingDestination.run(function()z.gameStageHandler:nextStage()end)spawn(function()while not MatchmakingDestination.isPlaying()do if z.gameStageHandler.currentStageClassName~="\087\097\105\116\105\110\103"or z._disableQueueTimeout then return end local j=tick()-z.gameStageHandler.timeStageStarted if j<Conf.queue_time_limit or#Players:getPlayers()<Conf.min_queue_players then wait(1)z.gameStageHandler._queueWaitTime=j else MatchmakingDestination.startGame()break end end end)end function z.destroy(z)for z,j in pairs(Players:GetPlayers())do if j.Character then local z=j.Character:FindFirstChild("\087\097\105\116\105\110\103\070\111\114\099\101\070\105\101\108\100")if z then z:Destroy()end for z,j in pairs(j.Character:GetChildren())do if j:IsA("\084\111\111\108")then j:Destroy()end end end for z,j in pairs(j.Backpack:GetChildren())do if j:IsA("\084\111\111\108")then j:Destroy()end end end end
--[[
	Stage implementation
]]

local Waiting = {}
Waiting.__index = Waiting

function Waiting.new(gameStageHandler)
	local self = setmetatable({
		gameStageHandler = gameStageHandler,
	}, Waiting)

	return self
end

function Waiting:disableQueueTimeout()
	self._disableQueueTimeout = true
end

function Waiting:startGame(...)
	MatchmakingDestination.startGame(...)
end

function Waiting:initialize()
	-- Spawn every player onto the boat
	for _, player in pairs(Players:GetPlayers()) do
		Util.loadCharacter(player)
	end

	spawn(function()
		DamageHandler.setup()
	end)

	MatchmakingDestination.run(function()
		self.gameStageHandler:nextStage()
	end)

	spawn(function()
		while not MatchmakingDestination.isPlaying() do
			if self.gameStageHandler.currentStageClassName ~= "Waiting" or self._disableQueueTimeout then
				return
			end

			local waitTime = tick() - self.gameStageHandler.timeStageStarted

			if waitTime < Conf.queue_time_limit or #Players:getPlayers() < Conf.min_queue_players then
				wait(1)
				self.gameStageHandler._queueWaitTime = waitTime
			else
				MatchmakingDestination.startGame()
				break
			end
		end
	end)
end

function Waiting:destroy()
	-- TODO:  Any teardown for MatchmakingDestination

	-- Reset the character back to the default state
	for _, player in pairs(Players:GetPlayers()) do
		if player.Character then
			-- Remove the force field from the character
			local forceField = player.Character:FindFirstChild("WaitingForceField")
			if forceField then
				forceField:Destroy()
			end

			-- Remove all equipped tools that are not the building tool
			for _, instance in pairs(player.Character:GetChildren()) do
				if instance:IsA("Tool") then
					instance:Destroy()
				end
			end
		end

		-- Remove all tools from the player's backpack that are not the building tool
		for _, instance in pairs(player.Backpack:GetChildren()) do
			if instance:IsA("Tool") then
				instance:Destroy()
			end
		end
	end
end
