Files
commander-ai-lab/unity-client/Assets/Scripts/UI/Battleground/HumanActionBar.cs
T
KoalaTrapLord 3f5d6d12e8 feat: 4-player Commander Battleground UI (issues #138–#151)
* chore: add battleground UI task tracker (2026-03-28)

* feat(#142): add [NonSerialized] battleground runtime state to CardModel

* feat(#143): add LoadArtCrop and LoadCardRaw (RawImage) to ImageCache

* feat(#148): add GameStateManager singleton and GameState/SeatState POCOs

* feat(#138): add BattlegroundController scene orchestrator

* feat(#139): add LifeTrackerWidget, CommanderDamageRow, PoisonCounter

* feat(#140): add PhaseTrackerWidget and PhaseStepButton

* feat(#141): add TurnOrderBar and TurnOrderPill

* feat(#142): add CardZone and CardView battlefield zone components

* feat(#143): add CommanderZoneWidget with art crop, cast counter, and tax display

* feat(#144): add TokenModel POCO, TokenPanelController, and TokenRowView

* chore: mark issues 1-8 as complete in task tracker

* feat(#146): add StackZoneController, StackItemView, and ManaPoolDisplay

* feat(#147 #150 #151): add LobbySetupModal, ZoneViewerModal, EliminationHandler; add Play Live button to SimulationController

* feat(#151): add Play Live button to SimulationController

* feat(#149): add GameWebSocketClient with reconnect, ping, and main-thread dispatch

* fix: add PostGameAction to ApiClient + update TODO.md all issues complete

- ApiClient.cs: add PostGameAction() wrapper → POST /api/game/action
  (fixes compile error in HumanActionBar.cs)
- TODO.md: mark issues #145, #146, #147, #149, #150, #151 as ✅
  all 14 battleground issues now committed

* feat(game): add routes/game.py + wire lab_api + fix GameWebSocketClient game_id URL

- routes/game.py: POST /api/game/start, POST /api/game/action (new)
- lab_api.py: import + register game_router
- GameWebSocketClient.cs: read game_id from PlayerPrefs, build
  ws://{host}/ws/game/{game_id} — fixes URL mismatch with ws_game.py

* fix(lobby): wire OnConfirm to POST /api/game/start, save game_id to PlayerPrefs

- OnConfirm() now calls ApiClient.PostGameStart() with 4-seat config
- On success: saves BattleSeats + GameId + WsUrl to PlayerPrefs,
  loads Battleground scene
- On failure: shows inline error label (red), re-enables Confirm button
- Added StartGameRequest/Response POCOs matching routes/game.py schema
- Added _errorLabel [SerializeField] for user-facing error feedback
2026-03-29 05:35:29 -04:00

102 lines
4.1 KiB
C#

using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using Newtonsoft.Json;
using CommanderAILab.Services;
namespace CommanderAILab.UI
{
/// <summary>
/// Human player action bar — only active when seat 0 has priority.
/// Buttons post actions to /api/game/action.
/// Subscribes to GameStateManager.OnPriorityChanged.
/// </summary>
public class HumanActionBar : MonoBehaviour
{
[Header("References")]
[SerializeField] private ApiClient apiClient;
[SerializeField] private GameObject actionBar; // root panel
[SerializeField] private Button passPriorityBtn;
[SerializeField] private Button endTurnBtn;
[SerializeField] private Button declareAttackersBtn;
[SerializeField] private Button declareBlockersBtn;
[SerializeField] private TMP_Text priorityLabel;
[SerializeField] private TMP_Text feedbackLabel;
private bool _hasPriority;
// ── Lifecycle ───────────────────────────────────────────
private void Start()
{
passPriorityBtn ?.onClick.AddListener(() => PostAction("pass_priority"));
endTurnBtn ?.onClick.AddListener(() => PostAction("end_turn"));
declareAttackersBtn?.onClick.AddListener(() => PostAction("declare_attackers"));
declareBlockersBtn ?.onClick.AddListener(() => PostAction("declare_blockers"));
if (GameStateManager.Instance != null)
{
GameStateManager.Instance.OnPriorityChanged += OnPriorityChanged;
GameStateManager.Instance.OnPhaseChanged += OnPhaseChanged;
}
SetPriority(false);
}
private void OnDestroy()
{
if (GameStateManager.Instance == null) return;
GameStateManager.Instance.OnPriorityChanged -= OnPriorityChanged;
GameStateManager.Instance.OnPhaseChanged -= OnPhaseChanged;
}
// ── Priority visibility ──────────────────────────────────
private void OnPriorityChanged(int seatIndex)
{
SetPriority(seatIndex == 0);
}
private void OnPhaseChanged(PhaseTrackerWidget.Phase phase, int seat, int turn)
{
// Only show combat buttons during combat phases
bool inCombat = phase >= PhaseTrackerWidget.Phase.DeclareAttackers
&& phase <= PhaseTrackerWidget.Phase.EndCombat;
if (declareAttackersBtn) declareAttackersBtn.gameObject.SetActive(inCombat && _hasPriority);
if (declareBlockersBtn) declareBlockersBtn.gameObject.SetActive(inCombat && _hasPriority);
}
private void SetPriority(bool hasPriority)
{
_hasPriority = hasPriority;
if (actionBar) actionBar.SetActive(hasPriority);
if (priorityLabel) priorityLabel.text = hasPriority ? "Your Priority" : "";
}
// ── Action posting ─────────────────────────────────────
private void PostAction(string actionType)
{
var payload = JsonConvert.SerializeObject(new { action = actionType, seat = 0 });
apiClient?.PostGameAction(payload,
_ => ShowFeedback(actionType),
err => ShowFeedback($"Error: {err}", isError: true));
}
private void ShowFeedback(string msg, bool isError = false)
{
if (!feedbackLabel) return;
StopCoroutine(nameof(FadeOutFeedback));
feedbackLabel.text = msg;
feedbackLabel.color = isError ? Color.red : Color.green;
feedbackLabel.gameObject.SetActive(true);
StartCoroutine(nameof(FadeOutFeedback));
}
private IEnumerator FadeOutFeedback()
{
yield return new WaitForSeconds(1.5f);
if (feedbackLabel) feedbackLabel.gameObject.SetActive(false);
}
}
}