From 100d950eb8a067f7f1fc2494f114fae0be47ceb6 Mon Sep 17 00:00:00 2001 From: wsy182 <2392948297@qq.com> Date: Tue, 31 Mar 2026 17:26:54 +0800 Subject: [PATCH] =?UTF-8?q?feat(game):=20=E6=B7=BB=E5=8A=A0=E5=AF=B9?= =?UTF-8?q?=E5=A4=9A=E7=A7=8D=E6=95=B0=E6=8D=AE=E7=B1=BB=E5=9E=8B=E7=9A=84?= =?UTF-8?q?=E5=B8=83=E5=B0=94=E5=80=BC=E8=A7=A3=E6=9E=90=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 实现了数字类型(1/0)到布尔值的转换逻辑 - 添加了字符串类型('true'/'false', '1'/'0')到布尔值的转换 - 在ChengduGamePage.vue中使用readBoolean函数处理就绪状态 - 更新了游戏存储中的就绪状态解析逻辑 - 在发送就绪状态时同时包含ready和isReady字段 - 统一了布尔值判断逻辑,提高代码健壮性 --- src/store/gameStore.ts | 29 +++++++++++++++++++++++++++-- src/views/ChengduGamePage.vue | 22 ++++++++++++++++++++-- 2 files changed, 47 insertions(+), 4 deletions(-) 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, }, }) }