shuffle players implementado correctamente
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user