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


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
