nombre persitente por navegador

This commit is contained in:
2025-08-11 14:59:37 -06:00
parent 0b9c121f73
commit e5e5ad452a
8 changed files with 253 additions and 26 deletions

View File

@@ -14,18 +14,27 @@ export class NameManager {
generateUniquePlayerName(baseName: string, sessionId: string): string {
const normalizedName = baseName.trim().toLowerCase();
if (!normalizedName) {
return this.generateUniquePlayerName('player', sessionId);
// Default base name when none is provided
return this.generateUniquePlayerName('guest', sessionId);
}
const currentCounter = this.nameCounters.get(normalizedName) || 0;
const newCounter = currentCounter + 1;
this.nameCounters.set(normalizedName, newCounter);
// Try exact name if not in use; otherwise, append incremental suffixes
const isInUse = (name: string) => {
for (const val of this.sessionToName.values()) {
if (val === name) return true;
}
return false;
};
let uniqueName = normalizedName;
if (isInUse(uniqueName)) {
let n = 2;
while (isInUse(`${normalizedName}-${n}`)) n++;
uniqueName = `${normalizedName}-${n}`;
}
const uniqueName = newCounter === 1 ? normalizedName : `${normalizedName}-${newCounter}`;
this.sessionToName.set(sessionId, uniqueName);
return uniqueName;
}
@@ -43,4 +52,4 @@ export class NameManager {
getAllActivePlayers(): string[] {
return Array.from(this.sessionToName.values());
}
}
}