diff --git a/src/ws-handler.ts b/src/ws-handler.ts index 1c582d2..ddaaeb6 100644 --- a/src/ws-handler.ts +++ b/src/ws-handler.ts @@ -65,16 +65,19 @@ export function handleMessage(ws: WS, raw: string) { if (!room) { er(ws, "Room not found"); return; } if (room.locked) { er(ws, "Room is locked"); return; } - const name = sanitize(msg.playerName ?? "Player", 24) || "Player"; - const existingId = sanitize(msg.playerId ?? "", 12); + // Calculate numeric ID based on join order + const joinCount = room.players.size + 1; + const playerId = joinCount.toString(); + const name = sanitize(msg.playerName ?? "", 24) || ""; // Keep name for compatibility but don't use it + let player: Player | undefined; - if (existingId && room.players.has(existingId)) { - player = room.players.get(existingId)!; + if (playerId && room.players.has(playerId)) { + player = room.players.get(playerId)!; player.ws = ws; player.isConnected = true; player.name = name; } else { - player = { id: genId(), name, teamIndex: null, ws, isConnected: true }; - room.players.set(player.id, player); + player = { id: playerId, name, teamIndex: null, ws, isConnected: true }; + room.players.set(playerId, player); } wsToPlayer.set(ws, { roomId: room.id, playerId: player.id });