function TeamManager.ClearTeamScores(m)for m,U in ipairs(Teams:GetTeams())do TeamScores[U]=0 DisplayManager:UpdateScore(U,0)end end function TeamManager.HasTeamWon(m)for m,U in ipairs(Teams:GetTeams())do if TeamScores[U]>=Configurations.CAPS_TO_WIN then return U end end return false end function TeamManager.GetWinningTeam(m)local U=0 local b=nil for m,F in ipairs(Teams:GetTeams())do if TeamScores[F]>U then U=TeamScores[F]b=F end end return b end function TeamManager.AreTeamsTied(m)local U=Teams:GetTeams()local b=0 local F=false for m,U in ipairs(U)do if TeamScores[U]==b then F=true elseif TeamScores[U]>b then F=false b=TeamScores[U]end end return F end function TeamManager.AssignPlayerToTeam(U,m)local b local F=math.huge for m,U in pairs(TeamPlayers)do if#U<F then b=m F=#U end end table.insert(TeamPlayers[b],m)m.Neutral=false m.TeamColor=b.TeamColor end function TeamManager.RemovePlayer(U,m)local b=GetTeamFromColor(m.TeamColor)local F=TeamPlayers[b]for U=1,#F,1 do if F[U]==m then table.remove(F,U)return end end end function TeamManager.ShuffleTeams(m)for m,U in ipairs(Teams:GetTeams())do TeamPlayers[U]={}end local U=Players:GetPlayers()while#U>0 do local m=math.random(1,#U)local b=table.remove(U,m)TeamManager:AssignPlayerToTeam(b)end end function TeamManager.AddTeamScore(b,m,U)local F=GetTeamFromColor(m)TeamScores[F]=TeamScores[F]+U DisplayManager:UpdateScore(F,TeamScores[F])end return TeamManager
-- Public Functions

function TeamManager:ClearTeamScores()
	for _, team in ipairs(Teams:GetTeams()) do
		TeamScores[team] = 0
		DisplayManager:UpdateScore(team, 0)
	end
end

function TeamManager:HasTeamWon()
	for _, team in ipairs(Teams:GetTeams()) do
		if TeamScores[team] >= Configurations.CAPS_TO_WIN then
			return team
		end
	end
	return false
end

function TeamManager:GetWinningTeam()
	local highestScore = 0
	local winningTeam = nil
	for _, team in ipairs(Teams:GetTeams()) do
		if TeamScores[team] > highestScore then
			highestScore = TeamScores[team]
			winningTeam = team
		end
	end
	return winningTeam
end

function TeamManager:AreTeamsTied()
	local teams = Teams:GetTeams()
	local highestScore = 0
	local tied = false
	for _, team in ipairs(teams) do
		if TeamScores[team] == highestScore then
			tied = true
		elseif TeamScores[team] > highestScore then
			tied = false
			highestScore = TeamScores[team]
		end
	end
	return tied
end

function TeamManager:AssignPlayerToTeam(player)
	local smallestTeam
	local lowestCount = math.huge
	for team, playerList in pairs(TeamPlayers) do
		if #playerList < lowestCount then
			smallestTeam = team
			lowestCount = #playerList
		end
	end
	table.insert(TeamPlayers[smallestTeam], player)
	player.Neutral = false
	player.TeamColor = smallestTeam.TeamColor
end

function TeamManager:RemovePlayer(player)
	local team = GetTeamFromColor(player.TeamColor)
	local teamTable = TeamPlayers[team]
	for i = 1, #teamTable do
		if teamTable[i] == player then
			table.remove(teamTable, i)
			return
		end
	end
end

function TeamManager:ShuffleTeams()
	for _, team in ipairs(Teams:GetTeams()) do
		TeamPlayers[team] = {}
	end
	local players = Players:GetPlayers()
	while #players > 0 do
		local rIndex = math.random(1, #players)
		local player = table.remove(players, rIndex)
		TeamManager:AssignPlayerToTeam(player)
	end
end

function TeamManager:AddTeamScore(teamColor, score)
	local team = GetTeamFromColor(teamColor)
	TeamScores[team] = TeamScores[team] + score
	DisplayManager:UpdateScore(team, TeamScores[team])
end

return TeamManager
