#include "blitz/protocol/packets/PlayerListPacket.h"

#include "blitz/common/VarInt.h"

namespace blitz {
namespace protocol {

DataBuffer PlayerListPacket::Serialize(bool packetID) const {
	DataBuffer data;

	WritePacketID(data, packetID);
	data << VarInt(m_Players.size());
	for (auto [playerID, playerInfo] : m_Players) {
		data << playerID << playerInfo.name;
	}
	return data;
}

void PlayerListPacket::Deserialize(DataBuffer& data) {
	VarInt playerCount;
	data >> playerCount;

	for (std::size_t i = 0; i < playerCount.GetValue(); i++) {
		std::uint8_t playerID;
		PlayerInfo playerInfo;
		data >> playerID >> playerInfo.name;
		m_Players.insert({playerID, playerInfo});
	}
}

}  // namespace protocol
}  // namespace blitz
