-- Runs when player dies or leaves. If no player is left, end round

local function checkPlayerCount()	
	if #PlayerManager.ActivePlayers == 0 then
		RaceManager.OnRaceFinishedEvent:Fire()
	end
end

local function addActivePlayer(player, speeder)
	if speeder then
		PlayerManager.Selections[player] = speeder
	end
	
	for playerKey, whichPlayer in pairs(PlayerManager.ActivePlayers) do
		if whichPlayer == player then
			-- Player is already in table
			return
		end
	end
	table.insert(PlayerManager.ActivePlayers, player)
	CheckpointEvent:FireClient(player, {{"setActive", true}})
	
	if #PlayerManager.ActivePlayers >= GameSettings.minimumPlayers then
		onMiminumActivePlayersEvent:Fire()
	end
end

local function removeActivePlayer(player)
	CheckpointEvent:FireClient(player, {{"setActive", false}})
	
	for playerKey, whichPlayer in pairs(PlayerManager.ActivePlayers) do
		if whichPlayer == player then
			table.remove(PlayerManager.ActivePlayers, playerKey)
			checkPlayerCount()
			return
		end
	end
end
