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' 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', { export const useGameStore = defineStore('game', {
state: (): GameState => ({ state: (): GameState => ({
roomId: '', roomId: '',
@@ -145,6 +169,7 @@ export const useGameStore = defineStore('game', {
const seatIndex = const seatIndex =
typeof seatRaw === 'number' && Number.isFinite(seatRaw) ? seatRaw : index typeof seatRaw === 'number' && Number.isFinite(seatRaw) ? seatRaw : index
const readyRaw = raw.Ready ?? raw.ready const readyRaw = raw.Ready ?? raw.ready
const ready = parseBooleanish(readyRaw)
const displayNameRaw = raw.PlayerName ?? raw.player_name const displayNameRaw = raw.PlayerName ?? raw.player_name
const avatarUrlRaw = raw.AvatarUrl ?? raw.avatar_url const avatarUrlRaw = raw.AvatarUrl ?? raw.avatar_url
const missingSuitRaw = raw.MissingSuit ?? raw.missing_suit const missingSuitRaw = raw.MissingSuit ?? raw.missing_suit
@@ -172,8 +197,8 @@ export const useGameStore = defineStore('game', {
hasHu: previous?.hasHu ?? false, hasHu: previous?.hasHu ?? false,
score: previous?.score ?? 0, score: previous?.score ?? 0,
isReady: isReady:
typeof readyRaw === 'boolean' ready !== null
? readyRaw ? ready
: (previous?.isReady ?? false), : (previous?.isReady ?? false),
} }
}) })

View File

@@ -652,6 +652,23 @@ function readBoolean(source: Record<string, unknown>, ...keys: string[]): boolea
if (typeof value === 'boolean') { if (typeof value === 'boolean') {
return value 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 return null
} }
@@ -1883,13 +1900,13 @@ function handleReadyStateResponse(message: unknown): void {
: typeof source.target === 'string' : typeof source.target === 'string'
? source.target ? source.target
: '' : ''
const ready = readyPayload.ready const ready = readBoolean(readyPayload, 'ready', 'Ready')
if (roomId && roomId !== gameStore.roomId) { if (roomId && roomId !== gameStore.roomId) {
return return
} }
if (typeof ready === 'boolean' && userId) { if (ready !== null && userId) {
applyPlayerReadyState(userId, ready) applyPlayerReadyState(userId, ready)
} }
@@ -2158,6 +2175,7 @@ function toggleReadyState(): void {
roomId: gameStore.roomId, roomId: gameStore.roomId,
payload: { payload: {
ready: nextReady, ready: nextReady,
isReady: nextReady,
}, },
}) })
} }