import { BinaryStreamReader } from "../binary/readerStream";
import { Player } from "../client.svelte";

export async function readHello(reader: BinaryStreamReader) {
  const count = await reader.readUInt8();
  const players: Record<number, Player> = {};
  for (let i = 0; i < count; i++) {
    const id = await reader.readUInt32();
    const name = await reader.readFixedString(0x20);
    const stage = await reader.readFixedString(0x40);
    const position = await reader.readArray(3, async reader => await reader.readFloat32());

    players[id] = new Player(
      name,
      id,
      position,
      stage,
    );
  };
  const peers = await reader.readArray(await reader.readUInt32(), async (reader) => {
    return await reader.readFixedString(36);
  });

  return { players, peers };
}
