local g=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local w=g:WaitForChild("\077\111\100\117\108\101\115")local v=require(w.src.utils.Logger)local A=require(w.src.thunks.playerFinishedRoom)local C=require(w.src.TouchItem)local Q=game.Workspace:WaitForChild("\080\108\097\099\101")local t=Q:findFirstChild("\077\097\112\115")local function c(g)return function(w)v:i("\083\116\097\114\116\105\110\103 \105\110\102\105\110\105\116\101 \103\097\109\101 \102\111\114 \114\111\111\109\058"..g)local Q=(w:getState()).rooms[g]if not t then v:w("\077\097\112\115 \102\111\108\100\101\114 \100\111\101\115 \110\111\116 \101\120\105\115\116\115\033")return end local c=t:findFirstChild(Q.modelName)if not c then v:w("\077\097\112\115 \102\111\108\100\101\114  \105\115 \109\105\115\115\105\110\103 "..(Q.modelName.." \111\098\106\101\099\116\033"))return end local U=c:findFirstChild("\070\105\110\105\115\104\080\108\097\099\101\104\111\108\100\101\114",true)if U then local Q={}C.create(U,function(C)if not Q[C]then v:d("\084\111\117\099\104\101\100 \070\105\110\105\115\104 \102\111\114 \112\108\097\121\101\114\058"..C.Name)Q[C]=true w:dispatch(A(C,g))spawn(function()wait(1)Q[C]=false end)end end)else v:w("\070\105\110\105\115\104\080\108\097\099\101\104\111\108\100\101\114 \105\115 \109\105\115\115\105\110\103 \102\114\111\109 "..(g.." \109\097\112 \111\098\106\101\099\116\033"))end end end return c
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local Modules = ReplicatedStorage:WaitForChild('Modules')
local logger = require(Modules.src.utils.Logger)
local playerFinishedRoom = require(Modules.src.thunks.playerFinishedRoom)
local TouchItem = require(Modules.src.TouchItem)

local Place = game.Workspace:WaitForChild('Place')
local MapsFolder = Place:findFirstChild('Maps')

local function startInfiniteGame(roomId)
	return function(store)
		logger:i('Starting infinite game for room:' .. roomId)

		local room = store:getState().rooms[roomId]

		if not MapsFolder then
			logger:w('Maps folder does not exists!')
			return
		end

		local mapObj = MapsFolder:findFirstChild(room.modelName)

		if not mapObj then
			logger:w('Maps folder  is missing ' .. room.modelName .. ' object!')
			return
		end

		local finishPlaceholder = mapObj:findFirstChild('FinishPlaceholder', true)
		if finishPlaceholder then
			local finishedPlayers = {}

			TouchItem.create(finishPlaceholder, function(player)
				if not finishedPlayers[player] then
					logger:d('Touched Finish for player:' .. player.Name)
					finishedPlayers[player] = true
					store:dispatch(playerFinishedRoom(player, roomId))
					spawn(function()
						wait(1)
						finishedPlayers[player] = false
					end)
				end
			end)
		else
			logger:w('FinishPlaceholder is missing from ' .. roomId .. ' map object!')
		end
	end
end

return startInfiniteGame