feat: add minigame room player service

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
ud18010
2026-07-29 12:24:58 +08:00
co-authored by Claude Opus 4.8
parent 053a95b4fb
commit 31afce6882
13 changed files with 1878 additions and 10 deletions
@@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using TMPro;
using UnityEngine;
@@ -20,6 +21,7 @@ namespace XGame.MiniGame
private MiniGameNetChannel _net;
private GameClientCtx _ctx;
private MiniGameAssetLoader _assets;
private RoomPlayerService _players;
private bool _running;
private bool _waitingForResultConfirm;
private string _gameId;
@@ -43,9 +45,22 @@ namespace XGame.MiniGame
// 大厅链路:MatchFound 由大厅通道消费(sendMatchRequest=false 时宿主收不到),
// 由外部把房间信息注入,否则结算弹框无法判断胜负(_selfPlayerId 恒 0)。
public void SetMatchInfo(string roomId, int selfPlayerId)
{
SetMatchInfo(roomId, selfPlayerId, null);
}
public void SetMatchInfo(string roomId, int selfPlayerId, IReadOnlyList<PlayerInfo> players)
{
_roomId = roomId;
_selfPlayerId = selfPlayerId;
if (_players == null)
{
_players = new RoomPlayerService();
}
if (players != null)
{
_players.SetRoomPlayers(players);
}
}
private IEnumerator CoEnter(string cdnBase, string gameId, int version, XWebSocket sock, bool sendMatchRequest)
@@ -69,9 +84,14 @@ namespace XGame.MiniGame
// 4) 反射实例化 IGameClient + 注入 ctx
_client = asmLoader.CreateClient();
_assets = new MiniGameAssetLoader(dl.LocalDir, dl.Manifest);
if (_players == null)
{
_players = new RoomPlayerService();
}
_ctx = new GameClientCtx(
_assets,
new UnityLogger($"[{gameId}]"),
_players,
msg => { if (_net != null) _net.SendGame(msg); },
() => ExitGame());
@@ -96,6 +116,11 @@ namespace XGame.MiniGame
MatchFoundMsg found = MatchFoundMsg.Decode(f.Payload);
_roomId = found.RoomId;
_selfPlayerId = found.SelfPlayerId;
if (_players == null)
{
_players = new RoomPlayerService();
}
_players.SetRoomPlayers(found.Players);
Debug.Log("[MiniGameHost] 房间就绪: " + _roomId);
break;
case FrameworkOpcode.RoomEnd:
@@ -122,6 +147,10 @@ namespace XGame.MiniGame
if (_client != null) { SafeCall(() => _client.OnExit(), "OnExit"); _client = null; }
_net = null;
_ctx = null;
if (_players != null)
{
_players.SetRoomPlayers(null);
}
if (_assets != null) { _assets.UnloadAll(); _assets = null; } // 卸载本小游戏资源 AB
Debug.Log("[MiniGameHost] 已退出小游戏: " + _gameId);
OnGameExited?.Invoke();