feat(game): 添加对多种数据类型的布尔值解析支持
- 实现了数字类型(1/0)到布尔值的转换逻辑
- 添加了字符串类型('true'/'false', '1'/'0')到布尔值的转换
- 在ChengduGamePage.vue中使用readBoolean函数处理就绪状态
- 更新了游戏存储中的就绪状态解析逻辑
- 在发送就绪状态时同时包含ready和isReady字段
- 统一了布尔值判断逻辑,提高代码健壮性
This commit is contained in:
@@ -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,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user