shuffle players implementado correctamente

This commit is contained in:
2025-08-15 20:03:26 -06:00
parent 9b84008f19
commit f214174bab
4 changed files with 234 additions and 40 deletions

View File

@@ -68,6 +68,7 @@ class ColyseusService {
} else {
// No client-side persistence; user must confirm name once per session
}
// Resume request is now triggered by Lobby.vue after listeners are attached
});
room.onMessage("nameUpdated", (data) => {
@@ -186,6 +187,37 @@ class ColyseusService {
}
}
async joinShuffledGameRoom(roomId: string, role: string, playerName: string, playerColor: string): Promise<Room> {
try {
const uuid = this.getUuidFromPath();
console.log(`[Colyseus] Joining shuffled room ${roomId} as ${role} with UUID ${uuid}`);
// Update local values
this.playerName.value = playerName;
this.playerColor.value = playerColor;
const gameRoom = await this.client.joinById(roomId, {
playerName,
playerColor,
uuid,
isShuffleJoin: true,
role
});
try { this.sessionId.value = (gameRoom as any).sessionId || this.sessionId.value; } catch {}
this.gameRoom.value = gameRoom;
this.currentRoom = gameRoom;
// Register reconnection token on server for this UUID
try { (gameRoom as any).send("registerReconnection", (gameRoom as any).reconnectionToken || ""); } catch {}
return gameRoom;
} catch (error) {
console.error("Failed to join shuffled game room:", error);
throw error;
}
}
sendClick(): void {
if (this.gameRoom.value) {
this.gameRoom.value.send("click");

View File

@@ -137,19 +137,72 @@ onMounted(async () => {
// Listen for server-initiated resume to existing game (fallback joinById)
room.onMessage("resumeGame", async (data: any) => {
if (guardResume()) return;
try {
const gameRoom = await colyseusService.joinGameRoom(data.roomId);
// Leave lobby before navigating
if (colyseusService.lobbyRoom.value) {
colyseusService.lobbyRoom.value.leave();
colyseusService.lobbyRoom.value = null;
const tryJoin = async (attempt = 1): Promise<void> => {
try {
const gameRoom = await colyseusService.joinGameRoom(data.roomId);
if (colyseusService.lobbyRoom.value) {
colyseusService.lobbyRoom.value.leave();
colyseusService.lobbyRoom.value = null;
}
await router.push(`/${routeUuid.value}/demo`);
} catch (error: any) {
const msg = String(error?.message || error);
if (attempt < 3 && (msg.includes('locked') || msg.includes('full'))) {
setTimeout(() => tryJoin(attempt + 1), 300);
return;
}
console.error('Auto-join failed:', error);
// allow future resume if this one failed entirely
resumed = false;
}
await router.push(`/${routeUuid.value}/demo`);
} catch (error) {
console.error('Auto-join failed:', error);
}
};
await tryJoin(1);
});
// Listen for shuffle redirect with complete player information
room.onMessage("shuffleRedirect", async (data: any) => {
if (guardResume()) return;
console.log('[Lobby] Received shuffle redirect:', data);
// Update player info before joining
if (data.playerName) {
colyseusService.playerName.value = data.playerName;
}
if (data.playerColor) {
colyseusService.playerColor.value = data.playerColor;
}
const tryJoin = async (attempt = 1): Promise<void> => {
try {
// Join with shuffle flag to bypass normal restrictions
const gameRoom = await colyseusService.joinShuffledGameRoom(
data.roomId,
data.role,
data.playerName,
data.playerColor
);
if (colyseusService.lobbyRoom.value) {
colyseusService.lobbyRoom.value.leave();
colyseusService.lobbyRoom.value = null;
}
await router.push(`/${routeUuid.value}/demo`);
} catch (error: any) {
const msg = String(error?.message || error);
if (attempt < 3) {
setTimeout(() => tryJoin(attempt + 1), 500);
return;
}
console.error('Shuffle join failed:', error);
resumed = false;
}
};
await tryJoin(1);
});
// After listeners are attached, ask server if we should resume (shuffle/currentRoom)
try { room.send("resumeMe"); } catch {}
// Keep color input synced with server-updated color
watch(() => colyseusService.playerColor.value, (c) => {
if (c && c !== colorInput.value) colorInput.value = c;