
local player = player
local Color = Color
local table = table
local TEAM_CONNECTING	=	TEAM_CONNECTING
local TEAM_UNASSIGNED	=	TEAM_UNASSIGNED
local TEAM_SPECTATOR	=	TEAM_SPECTATOR
local SERVER = SERVER
local CLIENT = CLIENT
local pairs = pairs
local string = string
local gmod = gmod
local GetNetworkedInt = GetNetworkedInt
local SetNetworkedInt = SetNetworkedInt
local tostring = tostring

module( "team" )

local TeamInfo = {}
local DefaultColor = Color(255, 255, 100, 255)

TeamInfo[TEAM_CONNECTING] 	= { Name = "Joining/Connecting", Color = DefaultColor, Score = 0 }
TeamInfo[TEAM_UNASSIGNED] 	= { Name = "Unassigned", Color = DefaultColor, Score = 0}
TeamInfo[TEAM_SPECTATOR] 	= { Name = "Spectator", Color = DefaultColor, Score = 0}

function SetUp( id, name, color )
	TeamInfo[id] = { Name = name, Color = color, Score = 0 }
end

function TotalDeaths(index)
	local score = 0
	for id,pl in pairs( player.GetAll() ) do
		if (pl:Team() == index) then
			score = score + pl:Deaths()
		end
	end
	return score
end

function TotalFrags(index)
	local score = 0
	for id,pl in pairs( player.GetAll() ) do
		if (pl:Team() == index) then
			score = score + pl:Frags()
		end
	end
	return score
end

function NumPlayers(index)
	return #GetPlayers(index)
end

function GetPlayers(index)
	local TeamPlayers = {}
	
	for id,pl in pairs( player.GetAll() ) do
		if (pl:Team() == index) then
			table.insert(TeamPlayers, pl)
		end
	end
	
	return TeamPlayers
end

function GetScore(index)

	return GetGlobalInt( "Team."..tostring(index)..".Score", 0 )
	
end

function GetName( index )

	if (!TeamInfo[index]) then return "" end
	return TeamInfo[index].Name
	
end

function GetColor( index )

	if (!TeamInfo[index]) then return DefaultColor end
	return TeamInfo[index].Color
	
end

function SetScore(index, score)

	return SetGlobalInt( "Team."..tostring(index)..".Score", score )

end

function AddScore(index, score)

	SetScore( index, GetScore( index ) + score )

end
