diff --git a/src/store/gameStore.ts b/src/store/gameStore.ts index 6c7db13..3993b7f 100644 --- a/src/store/gameStore.ts +++ b/src/store/gameStore.ts @@ -9,6 +9,30 @@ import { readStoredAuth } from '../utils/auth-storage' import type { Tile } from '../types/tile' +function parseBooleanish(value: unknown): boolean | null { + if (typeof value === 'boolean') { + return value + } + if (typeof value === 'number') { + if (value === 1) { + return true + } + if (value === 0) { + return false + } + } + if (typeof value === 'string') { + const normalized = value.trim().toLowerCase() + if (normalized === 'true' || normalized === '1') { + return true + } + if (normalized === 'false' || normalized === '0') { + return false + } + } + return null +} + export const useGameStore = defineStore('game', { state: (): GameState => ({ roomId: '', @@ -145,6 +169,7 @@ export const useGameStore = defineStore('game', { const seatIndex = typeof seatRaw === 'number' && Number.isFinite(seatRaw) ? seatRaw : index const readyRaw = raw.Ready ?? raw.ready + const ready = parseBooleanish(readyRaw) const displayNameRaw = raw.PlayerName ?? raw.player_name const avatarUrlRaw = raw.AvatarUrl ?? raw.avatar_url const missingSuitRaw = raw.MissingSuit ?? raw.missing_suit @@ -172,8 +197,8 @@ export const useGameStore = defineStore('game', { hasHu: previous?.hasHu ?? false, score: previous?.score ?? 0, isReady: - typeof readyRaw === 'boolean' - ? readyRaw + ready !== null + ? ready : (previous?.isReady ?? false), } }) diff --git a/src/views/ChengduGamePage.vue b/src/views/ChengduGamePage.vue index 68683d9..657da66 100644 --- a/src/views/ChengduGamePage.vue +++ b/src/views/ChengduGamePage.vue @@ -652,6 +652,23 @@ function readBoolean(source: Record, ...keys: string[]): boolea if (typeof value === 'boolean') { return value } + if (typeof value === 'number') { + if (value === 1) { + return true + } + if (value === 0) { + return false + } + } + if (typeof value === 'string') { + const normalized = value.trim().toLowerCase() + if (normalized === 'true' || normalized === '1') { + return true + } + if (normalized === 'false' || normalized === '0') { + return false + } + } } return null } @@ -1883,13 +1900,13 @@ function handleReadyStateResponse(message: unknown): void { : typeof source.target === 'string' ? source.target : '' - const ready = readyPayload.ready + const ready = readBoolean(readyPayload, 'ready', 'Ready') if (roomId && roomId !== gameStore.roomId) { return } - if (typeof ready === 'boolean' && userId) { + if (ready !== null && userId) { applyPlayerReadyState(userId, ready) } @@ -2158,6 +2175,7 @@ function toggleReadyState(): void { roomId: gameStore.roomId, payload: { ready: nextReady, + isReady: nextReady, }, }) }