|
@@ -1,7 +1,6 @@
|
|
|
// services/room.ts
|
|
|
import Taro from '@tarojs/taro'
|
|
|
import {
|
|
|
- type RecentRoom,
|
|
|
type Room,
|
|
|
type RoomUserInfo,
|
|
|
RoomRole,
|
|
@@ -60,7 +59,7 @@ const mockRooms: Record<string, Room> = {
|
|
|
// 房间API - 适配新数据结构
|
|
|
export const roomService = {
|
|
|
// 获取最近房间
|
|
|
- async getRecentRooms(): Promise<RecentRoom[]> {
|
|
|
+ async getRecentRooms(): Promise<Room[]> {
|
|
|
try {
|
|
|
// 从缓存获取
|
|
|
const cachedRooms = Taro.getStorageSync('recentRooms')
|
|
@@ -74,19 +73,26 @@ export const roomService = {
|
|
|
roomId: 'room_001',
|
|
|
name: '海龟汤房间',
|
|
|
gameTitle: '海龟汤',
|
|
|
- lastVisitTime: Date.now() - 3600000,
|
|
|
- hasPassword: false,
|
|
|
+ gameId: '1',
|
|
|
+ maxPlayers: 10,
|
|
|
+ visibility: RoomVisibility.PUBLIC,
|
|
|
+ hosterId: 'host_123',
|
|
|
+ createTime: Date.now() - 3600000,
|
|
|
status: RoomStatus.WAITING,
|
|
|
- playerCount: 1
|
|
|
+ users: []
|
|
|
},
|
|
|
{
|
|
|
roomId: 'room_002',
|
|
|
name: '狼人杀小队',
|
|
|
- gameTitle: '狼人杀',
|
|
|
- lastVisitTime: Date.now() - 86400000,
|
|
|
- hasPassword: true,
|
|
|
- status: RoomStatus.PLAYING,
|
|
|
- playerCount: 1
|
|
|
+ gameTitle: '狼人杀',
|
|
|
+ gameId: '2',
|
|
|
+ maxPlayers: 8,
|
|
|
+ visibility: RoomVisibility.PRIVATE,
|
|
|
+ password: '1234',
|
|
|
+ hosterId: 'host_456',
|
|
|
+ createTime: Date.now() - 86400000,
|
|
|
+ status: RoomStatus.WAITING,
|
|
|
+ users: []
|
|
|
}
|
|
|
]
|
|
|
} catch (error) {
|
|
@@ -96,20 +102,20 @@ export const roomService = {
|
|
|
},
|
|
|
|
|
|
// 检查房间是否存在
|
|
|
- async checkRoomExist(roomId: string): Promise<{exists: boolean, needPassword: boolean}> {
|
|
|
+ async checkRoomExist(gameId: string, roomId: string): Promise<{exists: boolean, needPassword: boolean}> {
|
|
|
try {
|
|
|
// 先查询本地存储
|
|
|
const recentRooms = Taro.getStorageSync('recentRooms')
|
|
|
if (recentRooms) {
|
|
|
const rooms = JSON.parse(recentRooms)
|
|
|
- const room = rooms.find((r: RecentRoom) => r.roomId === roomId)
|
|
|
+ const room = rooms.find((r: Room) => r.roomId === `${gameId}-${roomId}`)
|
|
|
if (room) {
|
|
|
- return { exists: true, needPassword: room.hasPassword }
|
|
|
+ return { exists: true, needPassword: room.visibility === RoomVisibility.PRIVATE }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 如果本地没有,检查mock数据
|
|
|
- const mockRoom = mockRooms[roomId]
|
|
|
+ const mockRoom = mockRooms[`${gameId}-${roomId}`]
|
|
|
if (mockRoom) {
|
|
|
return {
|
|
|
exists: true,
|
|
@@ -125,10 +131,10 @@ export const roomService = {
|
|
|
},
|
|
|
|
|
|
// 验证房间密码
|
|
|
- async verifyRoomPassword(roomId: string, password: string): Promise<{valid: boolean}> {
|
|
|
+ async verifyRoomPassword(gameId: string, roomId: string, password: string): Promise<{valid: boolean}> {
|
|
|
try {
|
|
|
// 简化实现:仅检查mock数据中的密码
|
|
|
- const mockRoom = mockRooms[roomId]
|
|
|
+ const mockRoom = mockRooms[`${gameId}-${roomId}`]
|
|
|
if (mockRoom && mockRoom.visibility === RoomVisibility.PRIVATE) {
|
|
|
return { valid: mockRoom.password === password }
|
|
|
}
|
|
@@ -141,28 +147,17 @@ export const roomService = {
|
|
|
},
|
|
|
|
|
|
// 创建新房间 - 适配新数据结构
|
|
|
- async createRoom(roomData: Room, userInfo: UserInfo): Promise<{roomId: string | null, success: boolean, message?: string}> {
|
|
|
+ async createRoom(roomData: Room, userInfo: UserInfo): Promise<{success: boolean, message?: string}> {
|
|
|
try {
|
|
|
if (!userInfo.openid) {
|
|
|
- return { roomId: null, success: false, message: '用户未登录' }
|
|
|
+ return { success: false, message: '用户未登录' }
|
|
|
}
|
|
|
|
|
|
// 本地存储一份房间信息
|
|
|
const recentRooms = Taro.getStorageSync('recentRooms') || '[]'
|
|
|
const rooms = JSON.parse(recentRooms)
|
|
|
|
|
|
- // 添加到最近房间
|
|
|
- const roomInfo: RecentRoom = {
|
|
|
- roomId: roomData.roomId,
|
|
|
- name: roomData.name,
|
|
|
- gameTitle: roomData.gameTitle,
|
|
|
- lastVisitTime: roomData.createTime,
|
|
|
- hasPassword: roomData.visibility === RoomVisibility.PRIVATE,
|
|
|
- status: roomData.status,
|
|
|
- playerCount: roomData.users.length
|
|
|
- }
|
|
|
-
|
|
|
- rooms.unshift(roomInfo)
|
|
|
+ rooms.unshift(roomData)
|
|
|
// 只保留最近10个房间
|
|
|
if (rooms.length > 10) {
|
|
|
rooms.pop()
|
|
@@ -173,18 +168,19 @@ export const roomService = {
|
|
|
// 简化实现:仅存储在本地缓存
|
|
|
const allRooms = Taro.getStorageSync('allRooms') || '{}'
|
|
|
const roomsObj = JSON.parse(allRooms)
|
|
|
- roomsObj[roomData.roomId] = roomData
|
|
|
+ const roomKey = `${roomData.gameId}-${roomData.roomId}`
|
|
|
+ roomsObj[roomKey] = roomData
|
|
|
Taro.setStorageSync('allRooms', JSON.stringify(roomsObj))
|
|
|
|
|
|
- return { roomId: roomData.roomId, success: true }
|
|
|
+ return { success: true }
|
|
|
} catch (error) {
|
|
|
console.error('创建房间失败:', error)
|
|
|
- return { roomId: null, success: false, message: '创建房间失败' }
|
|
|
+ return { success: false, message: '创建房间失败' }
|
|
|
}
|
|
|
},
|
|
|
|
|
|
// 加入房间 - 适配新数据结构
|
|
|
- async joinRoom(roomId: string, userData: RoomUserInfo): Promise<{success: boolean, roomData?: Room, message?: string}> {
|
|
|
+ async joinRoom(gameId: string, roomId: string, userData: RoomUserInfo): Promise<{success: boolean, roomData?: Room, message?: string}> {
|
|
|
try {
|
|
|
// 查找房间 - 先从本地缓存查询
|
|
|
let room: Room | null = null
|
|
@@ -192,12 +188,12 @@ export const roomService = {
|
|
|
// 检查本地缓存中是否有该房间
|
|
|
const allRooms = Taro.getStorageSync('allRooms') || '{}'
|
|
|
const roomsObj = JSON.parse(allRooms)
|
|
|
-
|
|
|
- if (roomsObj[roomId]) {
|
|
|
- room = roomsObj[roomId]
|
|
|
- } else if (mockRooms[roomId]) {
|
|
|
+ const roomKey = `${gameId}-${roomId}`
|
|
|
+ if (roomsObj[roomKey]) {
|
|
|
+ room = roomsObj[roomKey]
|
|
|
+ } else if (mockRooms[roomKey]) {
|
|
|
// 使用mock数据
|
|
|
- room = mockRooms[roomId]
|
|
|
+ room = mockRooms[roomKey]
|
|
|
}
|
|
|
|
|
|
if (!room) {
|
|
@@ -216,19 +212,24 @@ export const roomService = {
|
|
|
room.users.push(userData)
|
|
|
|
|
|
// 更新本地缓存
|
|
|
- roomsObj[roomId] = room
|
|
|
+ roomsObj[roomKey] = room
|
|
|
Taro.setStorageSync('allRooms', JSON.stringify(roomsObj))
|
|
|
}
|
|
|
|
|
|
// 保存到最近加入的房间
|
|
|
- const roomInfo: RecentRoom = {
|
|
|
+ const roomInfo: Room = {
|
|
|
roomId: room.roomId,
|
|
|
name: room.name,
|
|
|
+ gameId: room.gameId,
|
|
|
gameTitle: room.gameTitle,
|
|
|
- lastVisitTime: Date.now(),
|
|
|
- hasPassword: room.visibility === RoomVisibility.PRIVATE,
|
|
|
+ maxPlayers: room.maxPlayers,
|
|
|
+ visibility: room.visibility,
|
|
|
+ hosterId: room.hosterId,
|
|
|
+ createTime: room.createTime,
|
|
|
+ startTime: room.startTime,
|
|
|
+ endTime: room.endTime,
|
|
|
status: room.status,
|
|
|
- playerCount: room.users.length
|
|
|
+ users: room.users
|
|
|
}
|
|
|
|
|
|
// 更新最近房间列表
|
|
@@ -242,13 +243,13 @@ export const roomService = {
|
|
|
},
|
|
|
|
|
|
// 保存到最近加入的房间
|
|
|
- async saveToRecentRooms(roomInfo: RecentRoom): Promise<void> {
|
|
|
+ async saveToRecentRooms(roomInfo: Room): Promise<void> {
|
|
|
try {
|
|
|
const recentRooms = Taro.getStorageSync('recentRooms') || '[]'
|
|
|
const rooms = JSON.parse(recentRooms)
|
|
|
|
|
|
// 检查是否已存在
|
|
|
- const existingIndex = rooms.findIndex((r: RecentRoom) => r.roomId === roomInfo.roomId)
|
|
|
+ const existingIndex = rooms.findIndex((r: Room) => r.roomId === roomInfo.roomId)
|
|
|
if (existingIndex !== -1) {
|
|
|
// 移除已存在的
|
|
|
rooms.splice(existingIndex, 1)
|
|
@@ -269,20 +270,21 @@ export const roomService = {
|
|
|
},
|
|
|
|
|
|
// 退出房间
|
|
|
- async leaveRoom(roomId: string, userId: string): Promise<{success: boolean, message?: string}> {
|
|
|
+ async leaveRoom(gameId: string, roomId: string, userId: string): Promise<{success: boolean, message?: string}> {
|
|
|
try {
|
|
|
// 从本地缓存找到房间
|
|
|
const allRooms = Taro.getStorageSync('allRooms') || '{}'
|
|
|
const roomsObj = JSON.parse(allRooms)
|
|
|
+ const roomKey = `${gameId}-${roomId}`
|
|
|
|
|
|
- if (roomsObj[roomId]) {
|
|
|
- const room = roomsObj[roomId]
|
|
|
+ if (roomsObj[roomKey]) {
|
|
|
+ const room = roomsObj[roomKey]
|
|
|
|
|
|
// 过滤掉当前用户
|
|
|
room.users = room.users.filter(u => u.openid !== userId)
|
|
|
|
|
|
// 更新本地缓存
|
|
|
- roomsObj[roomId] = room
|
|
|
+ roomsObj[roomKey] = room
|
|
|
Taro.setStorageSync('allRooms', JSON.stringify(roomsObj))
|
|
|
}
|
|
|
|
|
@@ -294,19 +296,20 @@ export const roomService = {
|
|
|
},
|
|
|
|
|
|
// 获取房间信息
|
|
|
- async getRoomInfo(roomId: string): Promise<Room | null> {
|
|
|
+ async getRoomInfo(gameId: string, roomId: string): Promise<Room | null> {
|
|
|
try {
|
|
|
// 检查本地缓存中是否有该房间
|
|
|
const allRooms = Taro.getStorageSync('allRooms') || '{}'
|
|
|
const roomsObj = JSON.parse(allRooms)
|
|
|
+ const roomKey = `${gameId}-${roomId}`
|
|
|
|
|
|
- if (roomsObj[roomId]) {
|
|
|
- return roomsObj[roomId]
|
|
|
+ if (roomsObj[roomKey]) {
|
|
|
+ return roomsObj[roomKey]
|
|
|
}
|
|
|
|
|
|
// 如果本地没有,使用mock数据
|
|
|
- if (mockRooms[roomId]) {
|
|
|
- return mockRooms[roomId]
|
|
|
+ if (mockRooms[roomKey]) {
|
|
|
+ return mockRooms[roomKey]
|
|
|
}
|
|
|
|
|
|
return null
|
|
@@ -388,96 +391,16 @@ export const roomService = {
|
|
|
}
|
|
|
},
|
|
|
|
|
|
- // 获取我创建的房间/游戏
|
|
|
- async getCreatedRooms(userId: string): Promise<any[]> {
|
|
|
- try {
|
|
|
- // 合并进行中和已结束的,筛选出我创建的
|
|
|
- const activeRooms = await this.getActiveRooms(userId)
|
|
|
- const endedGames = await this.getEndedGames(userId)
|
|
|
-
|
|
|
- const createdRooms = activeRooms.filter((room: Room) => room.hosterId === userId)
|
|
|
- const createdGames = endedGames.filter((game: any) => game.hosterId === userId)
|
|
|
-
|
|
|
- // 合并结果并按时间排序
|
|
|
- const result = [
|
|
|
- ...createdRooms.map((room: Room) => ({
|
|
|
- ...room,
|
|
|
- isActive: true,
|
|
|
- time: room.createTime
|
|
|
- })),
|
|
|
- ...createdGames.map((game: any) => ({
|
|
|
- ...game,
|
|
|
- isActive: false,
|
|
|
- time: game.endTime
|
|
|
- }))
|
|
|
- ].sort((a, b) => b.time - a.time)
|
|
|
-
|
|
|
- return result
|
|
|
- } catch (error) {
|
|
|
- console.error('获取我创建的房间/游戏失败:', error)
|
|
|
- return []
|
|
|
- }
|
|
|
- },
|
|
|
-
|
|
|
- // 获取我参与的房间/游戏
|
|
|
- async getJoinedRooms(userId: string): Promise<any[]> {
|
|
|
- try {
|
|
|
- // 合并进行中和已结束的,筛选出我参与但不是我创建的
|
|
|
- const activeRooms = await this.getActiveRooms(userId)
|
|
|
- const endedGames = await this.getEndedGames(userId)
|
|
|
-
|
|
|
- const joinedRooms = activeRooms.filter((room: Room) =>
|
|
|
- room.hosterId !== userId &&
|
|
|
- room.users &&
|
|
|
- room.users.some((u) => u.openid === userId)
|
|
|
- )
|
|
|
-
|
|
|
- const joinedGames = endedGames.filter((game: any) =>
|
|
|
- game.hosterId !== userId &&
|
|
|
- game.users &&
|
|
|
- game.users.some((u: any) => u.openid === userId)
|
|
|
- )
|
|
|
-
|
|
|
- // 合并结果并按时间排序
|
|
|
- const result = [
|
|
|
- ...joinedRooms.map((room: Room) => ({
|
|
|
- ...room,
|
|
|
- isActive: true,
|
|
|
- time: room.createTime
|
|
|
- })),
|
|
|
- ...joinedGames.map((game: any) => ({
|
|
|
- ...game,
|
|
|
- isActive: false,
|
|
|
- time: game.endTime
|
|
|
- }))
|
|
|
- ].sort((a, b) => b.time - a.time)
|
|
|
-
|
|
|
- return result
|
|
|
- } catch (error) {
|
|
|
- console.error('获取我参与的房间/游戏失败:', error)
|
|
|
- return []
|
|
|
- }
|
|
|
- },
|
|
|
-
|
|
|
- // 创建房间用户信息
|
|
|
- createRoomUserInfo(userInfo: UserInfo, isHoster: boolean = false): RoomUserInfo {
|
|
|
- return {
|
|
|
- ...userInfo,
|
|
|
- roomRole: isHoster ? RoomRole.HOSTER : RoomRole.PLAYER,
|
|
|
- joinTime: Date.now(),
|
|
|
- isReady: isHoster
|
|
|
- }
|
|
|
- },
|
|
|
-
|
|
|
// 更新房间中的用户信息
|
|
|
- async updateUserInRoom(roomId: string, userId: string, userData: Partial<RoomUserInfo>): Promise<{success: boolean, message?: string}> {
|
|
|
+ async updateUserInRoom(gameId: string, roomId: string, userId: string, userData: Partial<RoomUserInfo>): Promise<{success: boolean, message?: string}> {
|
|
|
try {
|
|
|
// 从本地缓存找到房间
|
|
|
const allRooms = Taro.getStorageSync('allRooms') || '{}'
|
|
|
const roomsObj = JSON.parse(allRooms)
|
|
|
+ const roomKey = `${gameId}-${roomId}`
|
|
|
|
|
|
- if (roomsObj[roomId]) {
|
|
|
- const room = roomsObj[roomId]
|
|
|
+ if (roomsObj[roomKey]) {
|
|
|
+ const room = roomsObj[roomKey]
|
|
|
|
|
|
// 找到对应用户
|
|
|
const userIndex = room.users.findIndex(u => u.openid === userId)
|
|
@@ -489,7 +412,7 @@ export const roomService = {
|
|
|
}
|
|
|
|
|
|
// 更新本地缓存
|
|
|
- roomsObj[roomId] = room
|
|
|
+ roomsObj[roomKey] = room
|
|
|
Taro.setStorageSync('allRooms', JSON.stringify(roomsObj))
|
|
|
|
|
|
return { success: true }
|
|
@@ -520,26 +443,27 @@ export const roomService = {
|
|
|
},
|
|
|
|
|
|
// 更新房间状态
|
|
|
- async updateRoomStatus(roomId: string, status: RoomStatus): Promise<{success: boolean, message?: string}> {
|
|
|
+ async updateRoomStatus(gameId: string, roomId: string, status: RoomStatus): Promise<{success: boolean, message?: string}> {
|
|
|
try {
|
|
|
// 从本地缓存找到房间
|
|
|
const allRooms = Taro.getStorageSync('allRooms') || '{}'
|
|
|
const roomsObj = JSON.parse(allRooms)
|
|
|
+ const roomKey = `${gameId}-${roomId}`
|
|
|
|
|
|
- if (roomsObj[roomId]) {
|
|
|
- const room = roomsObj[roomId]
|
|
|
+ if (roomsObj[roomKey]) {
|
|
|
+ const room = roomsObj[roomKey]
|
|
|
|
|
|
// 更新状态
|
|
|
room.status = status
|
|
|
|
|
|
// 更新本地缓存
|
|
|
- roomsObj[roomId] = room
|
|
|
+ roomsObj[roomKey] = room
|
|
|
Taro.setStorageSync('allRooms', JSON.stringify(roomsObj))
|
|
|
|
|
|
// 同时更新最近房间列表中的状态
|
|
|
const recentRooms = Taro.getStorageSync('recentRooms') || '[]'
|
|
|
const rooms = JSON.parse(recentRooms)
|
|
|
- const roomIndex = rooms.findIndex((r: RecentRoom) => r.roomId === roomId)
|
|
|
+ const roomIndex = rooms.findIndex((r: Room) => r.roomId === roomId)
|
|
|
|
|
|
if (roomIndex !== -1) {
|
|
|
rooms[roomIndex].status = status
|