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.actions.rooms.setRoomCountDown)local C=require(w.src.thunks.startGame)local Q=require(w.M)local t=require(w.src.GlobalConfig)local function c(g)return function(w)v:i("\083\116\097\114\116\105\110\103 \103\097\109\101 \108\111\111\112 \102\111\114 \114\111\111\109\058"..g)w:dispatch(A(g,nil,"\087\097\105\116\105\110\103 \112\108\097\121\101\114\115"))spawn(function()while true do local c=(w:getState()).rooms[g]local U=c.playersWaiting local B=Q.count(U)==1 local S=c.startTime~=nil local T=B and not S if T then v:d("\065\108\109\111\115\116 \115\116\097\114\116\105\110\103 \103\097\109\101\046")local c=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116")w:dispatch(A(g,t.WAIT_TIME,"\083\116\097\114\116\105\110\103 \103\097\109\101"))delay(t.WAIT_TIME,function()c:Fire(true)end)spawn(function()while true do wait(.1)local A=(w:getState()).rooms[g]if Q.count(A.playersWaiting)==0 then v:d("\078\111 \112\108\097\121\101\114\115 \119\097\105\116\105\110\103\046")c:Fire(false)break end end end)local U=c.event:Wait()if U then v:d("\071\097\109\101 \119\105\108\108 \115\116\097\114\116 \102\111\114 \114\111\111\109\058 "..g)w:dispatch(A(g,nil,"\076\111\097\100\105\110\103 \109\097\122\101"))wait(.5)w:dispatch(C(g))else v:d("\071\097\109\101 \119\105\108\108 \110\111\116 \115\116\097\114\116 \102\111\114 \114\111\111\109\058 "..g)w:dispatch(A(g,nil,"\087\097\105\116\105\110\103 \112\108\097\121\101\114\115"))end end wait(1)end end)end end return c
--[[
    This is a thunk that checks if game needs to be started  
]]
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local Modules = ReplicatedStorage:WaitForChild('Modules')
local logger = require(Modules.src.utils.Logger)
local setRoomCountDown = require(Modules.src.actions.rooms.setRoomCountDown)
local startGame = require(Modules.src.thunks.startGame)
local M = require(Modules.M)

local GlobalConfig = require(Modules.src.GlobalConfig)

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

		store:dispatch(setRoomCountDown(roomId, nil, 'Waiting players'))

		spawn(function()
			while true do
				local room = store:getState().rooms[roomId]
				local playersWaiting = room.playersWaiting

				local hasEnoughWaiting = M.count(playersWaiting) == 1
				local isGameRunningAlready = room.startTime ~= nil
				local canStartGame = hasEnoughWaiting and not isGameRunningAlready

				if canStartGame then
					logger:d('Almost starting game.')

					local gameStartedEvent = Instance.new('BindableEvent')

					store:dispatch(
						setRoomCountDown(roomId, GlobalConfig.WAIT_TIME, 'Starting game')
					)

					delay(GlobalConfig.WAIT_TIME, function()
						gameStartedEvent:Fire(true)
					end)

					spawn(function()
						while true do
							wait(0.1)

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

							if M.count(r.playersWaiting) == 0 then
								logger:d('No players waiting.')
								gameStartedEvent:Fire(false)
								break
							end
						end
					end)

					local gameWillStart = gameStartedEvent.event:Wait()

					if gameWillStart then
						logger:d('Game will start for room: ' .. roomId)
						store:dispatch(setRoomCountDown(roomId, nil, 'Loading maze'))
						wait(0.5)
						store:dispatch(startGame(roomId))
					else
						logger:d('Game will not start for room: ' .. roomId)
						store:dispatch(setRoomCountDown(roomId, nil, 'Waiting players'))
					end
				end

				wait(1)
			end
		end)
	end
end

return startRoomGameLoop