Server: assign sequential numeric IDs (1-2-3...) to players on join instead of random strings
This commit is contained in:
@@ -65,16 +65,19 @@ export function handleMessage(ws: WS, raw: string) {
|
|||||||
if (!room) { er(ws, "Room not found"); return; }
|
if (!room) { er(ws, "Room not found"); return; }
|
||||||
if (room.locked) { er(ws, "Room is locked"); return; }
|
if (room.locked) { er(ws, "Room is locked"); return; }
|
||||||
|
|
||||||
const name = sanitize(msg.playerName ?? "Player", 24) || "Player";
|
// Calculate numeric ID based on join order
|
||||||
const existingId = sanitize(msg.playerId ?? "", 12);
|
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;
|
let player: Player | undefined;
|
||||||
|
|
||||||
if (existingId && room.players.has(existingId)) {
|
if (playerId && room.players.has(playerId)) {
|
||||||
player = room.players.get(existingId)!;
|
player = room.players.get(playerId)!;
|
||||||
player.ws = ws; player.isConnected = true; player.name = name;
|
player.ws = ws; player.isConnected = true; player.name = name;
|
||||||
} else {
|
} else {
|
||||||
player = { id: genId(), name, teamIndex: null, ws, isConnected: true };
|
player = { id: playerId, name, teamIndex: null, ws, isConnected: true };
|
||||||
room.players.set(player.id, player);
|
room.players.set(playerId, player);
|
||||||
}
|
}
|
||||||
|
|
||||||
wsToPlayer.set(ws, { roomId: room.id, playerId: player.id });
|
wsToPlayer.set(ws, { roomId: room.id, playerId: player.id });
|
||||||
|
|||||||
Reference in New Issue
Block a user