Implementar sistema UUID para nombres únicos globales
- Agregado UUID persistente en base de datos local (LokiJS) - Nombres únicos por UUID en lugar de sessionId - Nombres persisten entre reconexiones mientras el servidor esté activo - Migración automática de perfiles existentes - Registrados handlers para evitar warnings de mensajes no encontrados
This commit is contained in:
@@ -49,7 +49,11 @@ class ColyseusService {
|
||||
|
||||
async joinLobby(): Promise<Room> {
|
||||
try {
|
||||
const room = await this.client.joinOrCreate("lobby");
|
||||
// Initialize DB first to get UUID
|
||||
await localDB.init();
|
||||
const uuid = localDB.getUUID();
|
||||
|
||||
const room = await this.client.joinOrCreate("lobby", { uuid });
|
||||
this.lobbyRoom.value = room;
|
||||
this.currentRoom = room;
|
||||
// Require explicit confirmation each time we join the lobby (auto-confirm if saved name exists)
|
||||
@@ -58,22 +62,29 @@ class ColyseusService {
|
||||
room.onMessage("welcome", async (data) => {
|
||||
this.sessionId.value = data.sessionId;
|
||||
if (data.color) this.playerColor.value = data.color;
|
||||
// Initialize local DB and optionally auto-apply saved profile
|
||||
try {
|
||||
await localDB.init();
|
||||
const profile = localDB.getLocalPlayer();
|
||||
// Apply saved color silently
|
||||
if (profile?.color && profile.color !== this.playerColor.value) {
|
||||
this.setPlayerColor(profile.color);
|
||||
|
||||
// If server already has a name for us, use it
|
||||
if (data.name) {
|
||||
this.playerName.value = data.name;
|
||||
this.nameConfirmed.value = true;
|
||||
} else {
|
||||
// Initialize local DB and optionally auto-apply saved profile
|
||||
try {
|
||||
await localDB.init();
|
||||
const profile = localDB.getLocalPlayer();
|
||||
// Apply saved color silently
|
||||
if (profile?.color && profile.color !== this.playerColor.value) {
|
||||
this.setPlayerColor(profile.color);
|
||||
}
|
||||
if (profile?.name) {
|
||||
this.playerName.value = profile.name;
|
||||
try { localDB.setName(profile.name); } catch {}
|
||||
this.setPlayerName(profile.name);
|
||||
this.nameConfirmed.value = true;
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn("Local DB init failed", e);
|
||||
}
|
||||
if (profile?.name) {
|
||||
this.playerName.value = profile.name;
|
||||
try { localDB.setName(profile.name); } catch {}
|
||||
this.setPlayerName(profile.name);
|
||||
this.nameConfirmed.value = true;
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn("Local DB init failed", e);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -119,7 +130,8 @@ class ColyseusService {
|
||||
|
||||
async setPlayerName(name: string): Promise<void> {
|
||||
if (this.lobbyRoom.value) {
|
||||
this.lobbyRoom.value.send("setName", name);
|
||||
const uuid = localDB.getUUID();
|
||||
this.lobbyRoom.value.send("setName", { name, uuid });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ import Loki from "lokijs";
|
||||
|
||||
export interface LocalPlayerDoc {
|
||||
id: string; // fixed id for local profile
|
||||
uuid: string; // persistent unique identifier
|
||||
name: string;
|
||||
color: string;
|
||||
stats: {
|
||||
@@ -29,13 +30,19 @@ class LocalDBService {
|
||||
this.players = this.db!.addCollection<LocalPlayerDoc>("players", { unique: ["id"] });
|
||||
}
|
||||
// Ensure local profile exists
|
||||
if (!this.players.by("id", "local")) {
|
||||
let localPlayer = this.players.by("id", "local");
|
||||
if (!localPlayer) {
|
||||
this.players.insert({
|
||||
id: "local",
|
||||
uuid: this.generateUUID(),
|
||||
name: "",
|
||||
color: "#667eea",
|
||||
stats: { totalClicks: 0, gamesPlayed: 0, wins: 0, losses: 0 }
|
||||
});
|
||||
} else if (!localPlayer.uuid) {
|
||||
// Migrate existing profiles to have a UUID
|
||||
localPlayer.uuid = this.generateUUID();
|
||||
this.players.update(localPlayer);
|
||||
}
|
||||
this.initialized = true;
|
||||
this.db!.saveDatabase(() => resolve());
|
||||
@@ -85,6 +92,25 @@ class LocalDBService {
|
||||
this.col.update(doc);
|
||||
this.db?.saveDatabase();
|
||||
}
|
||||
|
||||
getUUID(): string {
|
||||
const doc = this.getLocalPlayer();
|
||||
if (!doc.uuid) {
|
||||
doc.uuid = this.generateUUID();
|
||||
this.col.update(doc);
|
||||
this.db?.saveDatabase();
|
||||
}
|
||||
return doc.uuid;
|
||||
}
|
||||
|
||||
private generateUUID(): string {
|
||||
// Simple UUID v4 generator
|
||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
||||
const r = Math.random() * 16 | 0;
|
||||
const v = c === 'x' ? r : (r & 0x3 | 0x8);
|
||||
return v.toString(16);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export const localDB = new LocalDBService();
|
||||
export const localDB = new LocalDBService();
|
||||
Reference in New Issue
Block a user