const service = require("../../services/game-service"); const { GAME_MODES } = require("../../utils/constants"); Page({ data: { code: "", room: null, modeName: "", isHost: false, showStartButton: false, meReady: false, canStart: false, startHint: "", errorMessage: "", starting: false }, onLoad(options) { this.setData({ code: options.code || "" }); this.loadRoom(true); }, onShow() { if (this.data.code) this.loadRoom(false); }, onPullDownRefresh() { this.loadRoom(false); wx.stopPullDownRefresh(); }, loadRoom(addBots) { let room = service.getRoom(this.data.code); if (!room) { this.setData({ room: null, errorMessage: "房间不存在或本地数据已被清理" }); return; } if (addBots && room.players.length < Math.min(6, room.settings.maxPlayers)) { room = service.addDemoPlayers( room.code, Math.min(6, room.settings.maxPlayers) ); } if (room.status !== "waiting") { const target = room.status === "finished" ? "result" : "game"; wx.redirectTo({ url: `/pages/${target}/${target}?code=${room.code}` }); return; } const me = room.players.find((player) => player.id === service.USER_ID); const isHost = room.hostId === service.USER_ID; const canOperate = isHost || room.code === "731204"; const enoughPlayers = room.players.length >= 4; const seekersValid = room.settings.seekerCount < room.players.length; const everyoneReady = room.players.every((player) => player.ready); let startHint = "分配身份并开始"; if (!enoughPlayers) startHint = "至少需要 4 人"; else if (!seekersValid) startHint = "寻找者人数过多"; else if (!everyoneReady) startHint = "等待所有玩家准备"; this.setData({ room, errorMessage: "", modeName: GAME_MODES[room.settings.mode].name, isHost, showStartButton: canOperate, meReady: me ? me.ready : false, canStart: canOperate && enoughPlayers && seekersValid && everyoneReady, startHint }); }, toggleReady() { try { service.toggleReady(this.data.code); wx.vibrateShort({ type: "light" }); this.loadRoom(false); } catch (error) { wx.showToast({ title: error.message, icon: "none" }); } }, startGame() { if (this.data.starting) return; this.setData({ starting: true }); try { service.startGame(this.data.code); wx.redirectTo({ url: `/pages/game/game?code=${this.data.code}` }); } catch (error) { this.setData({ starting: false }); wx.showToast({ title: error.message, icon: "none" }); } }, copyCode() { wx.setClipboardData({ data: this.data.code, success: () => wx.showToast({ title: "房间码已复制", icon: "success" }) }); }, backHome() { service.clearCurrentRoom(); wx.reLaunch({ url: "/pages/home/home" }); }, onShareAppMessage() { return { title: `来加入「${this.data.room.name}」`, path: `/pages/join/join?code=${this.data.code}` }; } });