nombre persitente por navegador
This commit is contained in:
@@ -32,15 +32,11 @@ export class LobbyRoom extends Room<LobbyState> {
|
||||
|
||||
onJoin(client: Client, options: any) {
|
||||
console.log(`[LobbyRoom] ${client.sessionId} joined lobby`);
|
||||
|
||||
const defaultName = `guest`;
|
||||
const uniqueName = NameManager.getInstance().generateUniquePlayerName(defaultName, client.sessionId);
|
||||
|
||||
this.state.addPlayer(client.sessionId, uniqueName);
|
||||
// Do NOT assign a default name on join. Wait until client presses "Set Name".
|
||||
this.state.addPlayer(client.sessionId, "");
|
||||
|
||||
client.send("welcome", {
|
||||
sessionId: client.sessionId,
|
||||
assignedName: uniqueName,
|
||||
color: this.state.players.get(client.sessionId)?.color || "#667eea"
|
||||
});
|
||||
|
||||
@@ -100,6 +96,11 @@ export class LobbyRoom extends Room<LobbyState> {
|
||||
private async handleQuickPlay(client: Client) {
|
||||
const player = this.state.players.get(client.sessionId);
|
||||
if (!player || player.inGame) return;
|
||||
// Prevent players without a confirmed name from joining games
|
||||
if (!player.name || !player.name.trim()) {
|
||||
client.send("error", { message: "Please set a name before joining a game." });
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// First try to find an available room
|
||||
@@ -138,6 +139,10 @@ export class LobbyRoom extends Room<LobbyState> {
|
||||
private async handleJoinRoom(client: Client, roomId: string) {
|
||||
const player = this.state.players.get(client.sessionId);
|
||||
if (!player || player.inGame) return;
|
||||
if (!player.name || !player.name.trim()) {
|
||||
client.send("error", { message: "Please set a name before joining a game." });
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Verify the room exists and is available
|
||||
|
||||
@@ -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