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
@@ -21,11 +21,14 @@ namespace RPS.Client
private RpsState _state;
private Choice _localChoice = Choice.None;
private string _status = "waiting for match...";
private string _leftPlayerName = "You";
private string _rightPlayerName = "Opponent";
private float _elapsed;
public void OnEnter(IGameClientCtx ctx)
{
_ctx = ctx;
ApplyRoomPlayers();
EnsureEventSystem();
_ctx.Assets.Load(UiPrefabPath, OnPrefabLoaded);
_ctx.Logger.Info("rps client entered");
@@ -43,6 +46,7 @@ namespace RPS.Client
_status = _state.Phase == RpsPhase.Finished
? (_state.Winner == 2 ? "finished: draw" : "finished: seat " + _state.Winner + " wins")
: "round " + _state.Round + " / " + _state.Phase;
ApplyRoomPlayers();
Render();
}
@@ -93,10 +97,42 @@ namespace RPS.Client
GameObject instance = Object.Instantiate(prefab);
instance.name = "UI_RockPaperScissors";
Object.DontDestroyOnLoad(instance);
_view = new RpsView(instance, SubmitChoice);
_view = new RpsView(instance, SubmitChoice, _leftPlayerName, _rightPlayerName);
Render();
}
private void ApplyRoomPlayers()
{
if (_ctx?.Players == null)
{
return;
}
var players = _ctx.Players.GetRoomPlayers();
if (players.Count > 0)
{
_leftPlayerName = DisplayName(players[0], "You");
}
if (players.Count > 1)
{
_rightPlayerName = DisplayName(players[1], players[1].IsAI ? "AI" : "Opponent");
}
_view?.SetPlayerNames(_leftPlayerName, _rightPlayerName);
}
private static string DisplayName(PlayerInfo player, string fallback)
{
if (player == null || string.IsNullOrEmpty(player.Name))
{
return fallback;
}
if (player.Level > 0)
{
return player.Name + " Lv." + player.Level;
}
return player.Name;
}
private void Render()
{
_view?.Render(_state, _localChoice, _status, _elapsed);
@@ -145,7 +181,7 @@ namespace RPS.Client
private readonly Sprite _paperSprite;
private readonly Sprite _scissorsSprite;
public RpsView(GameObject root, System.Action<Choice> choose)
public RpsView(GameObject root, System.Action<Choice> choose, string leftPlayerName, string rightPlayerName)
{
_root = root;
_rockButton = Find<Button>("Btn_Rock");
@@ -178,8 +214,8 @@ namespace RPS.Client
if (_scissorsButton != null) _scissorsButton.onClick.AddListener(() => choose(Choice.Scissors));
if (_confirmButton != null) _confirmButton.gameObject.SetActive(false);
SetText(_leftNameText, "You");
SetText(_rightNameText, "Opponent");
SetText(_leftNameText, string.IsNullOrEmpty(leftPlayerName) ? "You" : leftPlayerName);
SetText(_rightNameText, string.IsNullOrEmpty(rightPlayerName) ? "Opponent" : rightPlayerName);
}
public void Render(RpsState state, Choice localChoice, string status, float elapsed)
@@ -214,7 +250,8 @@ namespace RPS.Client
SetText(_rightRevealText, ChoiceDisplay(state.Choices[1], state.Phase));
SetText(_resultText, ResultText(state));
SetText(_finalRowsText, FinalRows(state));
SetText(_rightNameText, state.IsAi[1] ? "AI" : "Opponent");
if (state.IsAi[1] && _rightNameText != null && string.IsNullOrEmpty(_rightNameText.text))
SetText(_rightNameText, "AI");
SetIcon(_leftChoiceIcon, state.Choices[0]);
SetIcon(_rightChoiceIcon, state.Choices[1]);
SetButtons(state.Phase == RpsPhase.Choosing && localChoice == Choice.None);
@@ -226,6 +263,12 @@ namespace RPS.Client
SetText(_totalTimeText, elapsed.ToString("0.0") + "s / 60s");
}
public void SetPlayerNames(string leftPlayerName, string rightPlayerName)
{
SetText(_leftNameText, string.IsNullOrEmpty(leftPlayerName) ? "You" : leftPlayerName);
SetText(_rightNameText, string.IsNullOrEmpty(rightPlayerName) ? "Opponent" : rightPlayerName);
}
public void SetProgress(RpsState state)
{
if (_totalProgressFill != null)