feat(game): 添加对多种数据类型的布尔值解析支持

- 实现了数字类型(1/0)到布尔值的转换逻辑
- 添加了字符串类型('true'/'false', '1'/'0')到布尔值的转换
- 在ChengduGamePage.vue中使用readBoolean函数处理就绪状态
- 更新了游戏存储中的就绪状态解析逻辑
- 在发送就绪状态时同时包含ready和isReady字段
- 统一了布尔值判断逻辑,提高代码健壮性
This commit is contained in:
2026-03-31 17:26:54 +08:00
parent 06b25bde62
commit 100d950eb8
2 changed files with 47 additions and 4 deletions

View File

@@ -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),
}
})

View File

@@ -652,6 +652,23 @@ function readBoolean(source: Record<string, unknown>, ...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,
},
})
}