Server: assign sequential numeric IDs (1-2-3...) to players on join instead of random strings

This commit is contained in:
2026-04-08 18:53:24 -05:00
parent b76bcbffb2
commit c00db744c5

View File

@@ -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 });