nombre persitente por navegador
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user