From 51c6e872f3957a10c5e85b3d785a10d553b3f0e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=96=B9=E6=88=90?= Date: Mon, 19 Jan 2026 16:54:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=AB=E9=9B=B7=E4=BA=BA=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages-game/game/minesweeper/play.vue | 26 +++++++++++++++++++++++++- utils/nakamaManager.js | 21 +++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/pages-game/game/minesweeper/play.vue b/pages-game/game/minesweeper/play.vue index d7e3a06..149805c 100644 --- a/pages-game/game/minesweeper/play.vue +++ b/pages-game/game/minesweeper/play.vue @@ -607,7 +607,6 @@ export default { this.addLog('system', this.isSpectator ? '🔭 正在切入观察视角...' : '🚪 正在恢复战局...'); try { await nakamaManager.joinMatch(this.matchId); - // 加入成功,确保缓存是最新的 uni.setStorageSync('minesweeper_last_match_id', this.matchId); this.addLog('system', '✅ 接入成功'); setTimeout(() => { @@ -620,6 +619,13 @@ export default { uni.removeStorageSync('minesweeper_last_match_id'); } } + + // 加入大厅频道用于统计在线人数 + this.joinLobby(); + + // 启动在线人数轮询 + this.startOnlineCountPolling(); + } catch (e) { console.error('[初始化错误]', e); this.addLog('system', '❌ 初始化失败: ' + e.message); @@ -1086,6 +1092,24 @@ export default { console.warn('获取在线人数失败:', e); } }, + async joinLobby() { + try { + // 加入 minesweeper_lobby 频道, Type 1 (Room), Persistence True, Hidden False + await nakamaManager.joinChat('minesweeper_lobby', 1, true, false); + console.log('Successfully joined minesweeper lobby'); + } catch (error) { + console.warn('Failed to join lobby channel:', error); + } + }, + startOnlineCountPolling() { + if (this.onlineCountInterval) clearInterval(this.onlineCountInterval); + this.fetchOnlineCount(); // Fetch immediately + this.onlineCountInterval = setInterval(() => { + if (this.isConnected) { + this.fetchOnlineCount(); + } + }, 5000); // Poll every 5 seconds + }, getItemIcon(itemId) { const map = { medkit: '💊', bomb_timer: '⏰', poison: '☠️', shield: '🛡️', skip: '⏭️', magnifier: '🔍', knife: '🔪', revive: '💖', lightning: '⚡', chest: '📦', curse: '👻' }; return map[itemId] || '🎁'; diff --git a/utils/nakamaManager.js b/utils/nakamaManager.js index 1ef15bc..b6f2456 100644 --- a/utils/nakamaManager.js +++ b/utils/nakamaManager.js @@ -379,6 +379,27 @@ class NakamaManager { return response.match; } + /** + * 加入聊天频道 + * @param target 频道名称 (e.g., "minesweeper_lobby") + * @param type 频道类型 (1=Room, 2=Direct, 3=Group) + * @param persistence 是否持久化 + * @param hidden 是否隐身 + */ + async joinChat(target, type, persistence, hidden) { + console.log(`[Nakama] Joining chat channel: ${target}`); + const response = await this._send({ + channel_join: { + target: target, + type: type, + persistence: persistence, + hidden: hidden + } + }); + console.log('[Nakama] Joined channel:', response.channel?.id); + return response.channel; + } + /** * 调用 RPC */