
playerInfo = {}
local players = {}

playerInfo.players = players

playerInfo.getPlayer = function (id)
	for i=1,#players do
		if players[i].id == id then
			return players[i]
		end
	end
	return nil
end

playerInfo.addPlayer = function (id, userName)

	local oldPlayer = playerInfo.getPlayer(id)
	if oldPlayer then
		return oldPlayer
	end

	local newPlayer = { id = id, commandList = {}, name = userName or "n/a" }
	table.insert(players, newPlayer)

	--print("Added p" .. id)

	return newPlayer
end

playerInfo.addPlayerCommands = function (id, sequence, commands)

	local player = playerInfo.getPlayer(id)
	if player then
		--print("Player " .. id .. ", sequence " .. sequence)
		table.insert(player.commandList, {sequence = sequence, commands = commands})
	end
end

playerInfo.areAllPlayersDone = function (sequence)
	for i=1,#players do
		local found = false
		for c=1,#(players[i].commandList) do
			if players[i].commandList[c].sequence == sequence then
				found = true
				break
			end
		end
		if not found then
			return false
		end
	end
	return true
end

playerInfo.getPlayerCommands = function (sequence, doPop)
	local list = {}

	for i=1,#players do

		for index, command in pairs(players[i].commandList) do
			if command.sequence == sequence then

				if #(command.commands) > 0 then
					table.insert(list, {id=players[i].id, commands=command.commands})
				end
				if doPop then
					table.remove(players[i].commandList, index)
				end
				break
			end
		end
	end

	return list
end
