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

namespace blitz {
namespace protocol {

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

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

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

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

}  // namespace protocol
}  // namespace blitz
