feat(game): 实现游戏房间状态管理和WebSocket连接功能
- 添加路由参数解析和房间状态初始化逻辑 - 实现房间玩家座位视图计算和状态映射 - 集成WebSocket客户端连接管理和重连机制 - 添加房间数据持久化存储功能 - 实现游戏界面状态显示和用户交互控制 - 更新WS代理目标地址配置 - 重构房间状态管理模块分离到独立store
This commit is contained in:
45
src/store/index.ts
Normal file
45
src/store/index.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { ref } from 'vue'
|
||||
import type {
|
||||
ActiveRoomState,
|
||||
ActiveRoomSelectionInput,
|
||||
} from './state'
|
||||
import { readActiveRoomSnapshot, saveActiveRoom } from './storage'
|
||||
|
||||
const activeRoom = ref<ActiveRoomState | null>(readActiveRoomSnapshot())
|
||||
|
||||
function normalizeRoom(input: ActiveRoomSelectionInput): ActiveRoomState {
|
||||
return {
|
||||
roomId: input.roomId,
|
||||
roomName: input.roomName ?? '',
|
||||
gameType: input.gameType ?? 'chengdu',
|
||||
ownerId: input.ownerId ?? '',
|
||||
maxPlayers: input.maxPlayers ?? 4,
|
||||
playerCount: input.playerCount ?? input.players?.length ?? 0,
|
||||
status: input.status ?? 'waiting',
|
||||
createdAt: input.createdAt ?? '',
|
||||
updatedAt: input.updatedAt ?? '',
|
||||
players: input.players ?? [],
|
||||
myHand: [],
|
||||
game: {
|
||||
state: {
|
||||
wall: [],
|
||||
scores: {},
|
||||
dealerIndex: -1,
|
||||
currentTurn: -1,
|
||||
phase: 'waiting',
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// 设置当前房间
|
||||
export function setActiveRoom(input: ActiveRoomSelectionInput) {
|
||||
const next = normalizeRoom(input)
|
||||
activeRoom.value = next
|
||||
saveActiveRoom(next)
|
||||
}
|
||||
|
||||
// 使用房间状态
|
||||
export function useActiveRoomState() {
|
||||
return activeRoom
|
||||
}
|
||||
Reference in New Issue
Block a user