refactor(game): 重构游戏动作处理和WebSocket连接管理

- 重构sendGameAction函数参数结构,添加上下文支持
- 新增sendStartGame和sendLeaveRoom函数统一处理游戏开始和离开房间逻辑
- 移除路由相关依赖,简化ChengduGamePage组件
- 更新WebSocket客户端实现,添加状态变化订阅功能
- 移除requestId生成函数和相关参数,精简消息结构
- 优化座位玩家卡片数据模型,移除在线状态和金钱字段
- 整理游戏阶段常量定义,添加标签映射
- 移除过期的游戏状态字段如needDraw、lastDiscardTile等
- 添加座位类型定义和改进游戏类型文件组织结构
This commit is contained in:
2026-03-25 13:34:47 +08:00
parent 4f6ef1d0ec
commit 148e21f3b0
13 changed files with 236 additions and 867 deletions

View File

@@ -1,37 +1,145 @@
import type { ActionButtonState } from "./types.ts"
import { wsClient } from "@/ws/client" // 新增
import type {ActionButtonState} from "./types.ts"
import {wsClient} from "../ws/client.ts"
export function sendGameAction(type: ActionButtonState['type']): void {
// 原来是判断 ws这里改成用 wsClient 的状态(简单处理)
if (!currentUserId.value) {
export function sendGameAction(
params: {
type: ActionButtonState['type']
userID: string
roomId: string
selectedTile?: string | null
},
ctx: {
actionPending: { value: boolean }
logWsSend: (msg: any) => void
pushWsMessage: (msg: string) => void
}
): void {
const {type, userID, roomId, selectedTile} = params
const {actionPending, logWsSend, pushWsMessage} = ctx
// 简单登录判断
if (!userID) {
console.log('当前用户未登录')
return
}
const requestId = createRequestId(type)
const payload: Record<string, unknown> = {}
if (type === 'discard' && selectedTile.value) {
payload.tile = selectedTile.value
payload.discard_tile = selectedTile.value
payload.code = selectedTile.value
// 出牌
if (type === 'discard' && selectedTile) {
payload.tile = selectedTile
payload.discard_tile = selectedTile
payload.code = selectedTile
}
actionPending.value = true
const message = {
type,
sender: currentUserId.value,
sender: userID,
target: 'room',
roomId: roomState.value.id || roomId.value,
roomId,
seq: Date.now(),
requestId,
trace_id: createRequestId('trace'),
payload,
}
logWsSend(message)
wsClient.send(message)
pushWsMessage(`[client] 请求${type}`)
}
wsClient.send(message) // ✅ 改这里
pushWsMessage(`[client] 请求${type} requestId=${requestId}`)
export function sendStartGame(params: {
userID: string
roomId: string
canStartGame: boolean
startGamePending: { value: boolean }
logWsSend: (msg: any) => void
pushWsMessage: (msg: string) => void
}): void {
const {
userID,
roomId,
canStartGame,
startGamePending,
logWsSend,
pushWsMessage
} = params
if (!canStartGame || startGamePending.value) {
return
}
if (!userID) {
return
}
startGamePending.value = true
const message = {
type: 'start_game',
sender: userID,
target: 'room',
roomId,
seq: Date.now(),
payload: {},
}
logWsSend(message)
wsClient.send(message)
pushWsMessage(`[client] 请求开始游戏`)
}
export function sendLeaveRoom(params: {
userID: string
roomId: string
wsError: { value: string }
leaveRoomPending: { value: boolean }
logWsSend: (msg: any) => void
pushWsMessage: (msg: string) => void
}): boolean {
const {
userID,
roomId,
wsError,
leaveRoomPending,
logWsSend,
pushWsMessage
} = params
if (!userID) {
wsError.value = '缺少当前用户 ID无法退出房间'
return false
}
if (!roomId) {
wsError.value = '缺少房间 ID无法退出房间'
return false
}
leaveRoomPending.value = true
const message = {
type: 'leave_room',
sender: userID,
target: 'room',
roomId: roomId,
seq: Date.now(),
payload: {},
}
logWsSend(message)
wsClient.send(message)
pushWsMessage(`[client] 请求退出房间`)
return true
}