From 7a917cf93b162e068a6fff8c24d97960191862bf Mon Sep 17 00:00:00 2001 From: dingo Date: Tue, 21 Apr 2026 17:13:04 -0400 Subject: [PATCH] fix(ability-prompts): fix 3 client wiring gaps MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. JSON case normalization Parse CostGroupJson, ActionGroupJson, BranchesJson, RevealedCardsJson through _normalize_keys in game_screen.gd — server serialises these inner payloads with _jsonOpts (no naming policy = PascalCase), but the client was reading lowercase keys, producing empty option lists. 2. Cost pick candidate list - Add CandidatesJson to PendingCostPick; populated in AbilitySystem.PayCosts with the actor's eligible cards (hand for DiscardFromHand, field spirits for SendSpiritFromBattlefieldToDiscard / DealDamageToOwnSpirit). - Thread CandidatesJson through CostPickRequestPush → GameHandlerBase push. - Client _on_cost_pick_request now uses candidatesJson from the push instead of Game.get_my_player().hand, fixing opponent-pick and battlefield-cost UIs. 3. Reconnect recovery via game.get_state - Extract all pending-prompt pushes into static PushPendingPromptsAsync in GameHandlerBase (called from PushStateToPlayersAsync as before). - Inject IClientPush into GameGetStateHandler; call PushPendingPromptsAsync after building the reply so any active pause prompt is re-sent to the chooser on manual state sync / reconnect. Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude Co-Authored-By: Happy --- .../origin-client/scenes/game/game_screen.gd | 32 +++++++++---------- .../src/SOR.Game/Abilities/AbilitySystem.cs | 16 ++++++++++ .../Abilities/Models/PendingCostPick.cs | 7 ++++ Server/src/SOR.Game/GameMessages.cs | 8 ++++- .../SOR.Game/Handlers/GameGetStateHandler.cs | 17 ++++++++-- .../src/SOR.Game/Handlers/GameHandlerBase.cs | 25 +++++++++------ 6 files changed, 76 insertions(+), 29 deletions(-) diff --git a/Client/origin-client/scenes/game/game_screen.gd b/Client/origin-client/scenes/game/game_screen.gd index eaf6ffc..2963f03 100644 --- a/Client/origin-client/scenes/game/game_screen.gd +++ b/Client/origin-client/scenes/game/game_screen.gd @@ -4431,16 +4431,16 @@ func _on_cost_pick_request(req: Dictionary) -> void: vbox.add_child(title) vbox.add_child(HSeparator.new()) - # List hand cards (most interactive costs are hand discards). - var my_player := Game.get_my_player() - var hand : Array = my_player.get("hand", []) as Array if my_player is Dictionary else [] - if hand.is_empty(): - var lbl := Label.new(); lbl.text = "(hand empty)" + # Use server-supplied candidate list (covers hand, field, and opponent-pick cases). + var candidates_raw = JSON.parse_string(str(req.get("candidatesJson", "[]"))) + var candidates : Array = candidates_raw as Array if candidates_raw is Array else [] + if candidates.is_empty(): + var lbl := Label.new(); lbl.text = "(no candidates)" lbl.add_theme_font_size_override("font_size", 11) vbox.add_child(lbl) else: - for card in hand: - var card_d : Dictionary = card as Dictionary + for cand in candidates: + var card_d : Dictionary = cand as Dictionary var iid : String = str(card_d.get("instanceId", "")) var name_ : String = str(card_d.get("name", iid)) var btn := Button.new() @@ -4484,10 +4484,10 @@ func _on_cost_group_pick_request(req: Dictionary) -> void: _pending_source_id = str(req.get("sourceInstanceId", "")) var group_raw = JSON.parse_string(str(req.get("costGroupJson", "{}"))) - var group : Dictionary = group_raw as Dictionary if group_raw is Dictionary else {} + var group : Dictionary = _normalize_keys(group_raw) as Dictionary if group_raw is Dictionary else {} var entries : Array = group.get("entries", []) as Array var groups : Array = group.get("groups", []) as Array - var logic : String = str(group.get("logicOp", "OR")) + var logic : String = str(group.get("logicop", group.get("logicOp", "OR"))) _hide_pending_prompt_panel() var panel := PanelContainer.new() @@ -4506,7 +4506,7 @@ func _on_cost_group_pick_request(req: Dictionary) -> void: var option_idx := 0 for entry in entries: var entry_d : Dictionary = entry as Dictionary - var label : String = str(entry_d.get("typeStr", "Option %d" % (option_idx + 1))) + var label : String = str(entry_d.get("typestr", entry_d.get("typeStr", "Option %d" % (option_idx + 1)))) var btn := Button.new(); btn.text = label btn.add_theme_font_size_override("font_size", 12) var cap_idx : int = option_idx @@ -4518,7 +4518,7 @@ func _on_cost_group_pick_request(req: Dictionary) -> void: option_idx += 1 for grp in groups: var grp_d : Dictionary = grp as Dictionary - var lop : String = str(grp_d.get("logicOp", "OR")) + var lop : String = str(grp_d.get("logicop", grp_d.get("logicOp", "OR"))) var btn := Button.new(); btn.text = "Group (%s)" % lop btn.add_theme_font_size_override("font_size", 12) var cap_idx : int = option_idx @@ -4542,10 +4542,10 @@ func _on_action_group_pick_request(req: Dictionary) -> void: _pending_source_id = str(req.get("sourceInstanceId", "")) var group_raw = JSON.parse_string(str(req.get("actionGroupJson", "{}"))) - var group : Dictionary = group_raw as Dictionary if group_raw is Dictionary else {} + var group : Dictionary = _normalize_keys(group_raw) as Dictionary if group_raw is Dictionary else {} var actions : Array = group.get("actions", []) as Array var subgrps : Array = group.get("groups", []) as Array - var logic : String = str(group.get("logicOp", "OR")) + var logic : String = str(group.get("logicop", group.get("logicOp", "OR"))) _hide_pending_prompt_panel() var panel := PanelContainer.new() @@ -4581,7 +4581,7 @@ func _on_action_group_pick_request(req: Dictionary) -> void: option_idx += 1 for grp in subgrps: var grp_d : Dictionary = grp as Dictionary - var lop : String = str(grp_d.get("logicOp", "OR")) + var lop : String = str(grp_d.get("logicop", grp_d.get("logicOp", "OR"))) var btn := Button.new(); btn.text = "Group (%s)" % lop btn.add_theme_font_size_override("font_size", 12) var cap_idx : int = option_idx @@ -4605,7 +4605,7 @@ func _on_branch_pick_request(req: Dictionary) -> void: _pending_source_id = str(req.get("sourceInstanceId", "")) var branches_raw = JSON.parse_string(str(req.get("branchesJson", "[]"))) - var branches : Array = branches_raw as Array if branches_raw is Array else [] + var branches : Array = _normalize_keys(branches_raw) as Array if branches_raw is Array else [] _hide_pending_prompt_panel() var panel := PanelContainer.new() @@ -4766,7 +4766,7 @@ func _on_reveal_choice_pick_request(req: Dictionary) -> void: _reveal_ordered_indices.clear() var cards_raw = JSON.parse_string(str(req.get("revealedCardsJson", "[]"))) - var cards : Array = cards_raw as Array if cards_raw is Array else [] + var cards : Array = _normalize_keys(cards_raw) as Array if cards_raw is Array else [] _reveal_total_cards = cards.size() _hide_pending_prompt_panel() diff --git a/Server/src/SOR.Game/Abilities/AbilitySystem.cs b/Server/src/SOR.Game/Abilities/AbilitySystem.cs index ee3ef28..38715a9 100644 --- a/Server/src/SOR.Game/Abilities/AbilitySystem.cs +++ b/Server/src/SOR.Game/Abilities/AbilitySystem.cs @@ -505,6 +505,21 @@ public static class AbilitySystem // Remaining costs after this one. var remaining = costs.Skip(ci + 1).ToArray(); + // Compute candidates: the actor's eligible cards for this cost type. + IEnumerable rawCandidates = cost.Type switch + { + CostType.DiscardFromHand => + player.Hand, + CostType.SendSpiritFromBattlefieldToDiscard => + player.Field.OfType().Where(f => f.CardType == "Spirit").Cast(), + CostType.DealDamageToOwnSpirit => + player.Field.OfType().Where(f => f.CardType == "Spirit").Cast(), + _ => Enumerable.Empty() + }; + var candidateItems = rawCandidates + .Select(c => new { instanceId = c.InstanceId, name = c.Name, cardType = c.CardType }) + .ToArray(); + ctx.State.PendingCostPick = new PendingCostPick { ActorId = ctx.ActorId, @@ -522,6 +537,7 @@ public static class AbilitySystem ? JsonSerializer.Serialize(range, _jsonOpts) : null, ForceDurationJson = forceDuration is not null ? JsonSerializer.Serialize(forceDuration, _jsonOpts) : null, + CandidatesJson = JsonSerializer.Serialize(candidateItems), }; return true; // paused } diff --git a/Server/src/SOR.Game/Abilities/Models/PendingCostPick.cs b/Server/src/SOR.Game/Abilities/Models/PendingCostPick.cs index a3b473d..70ac02b 100644 --- a/Server/src/SOR.Game/Abilities/Models/PendingCostPick.cs +++ b/Server/src/SOR.Game/Abilities/Models/PendingCostPick.cs @@ -50,4 +50,11 @@ public sealed class PendingCostPick /// JSON-serialized DurationModel? — forceDuration passed to ExecuteActions on resume. public string? ForceDurationJson { get; set; } + + /// + /// JSON-serialized array of { instanceId, name, cardType } objects representing + /// eligible cards/units the picker may select. Pre-computed from the actor's zone at + /// pause time so the opponent client can display them even when the zone is normally hidden. + /// + public string CandidatesJson { get; set; } = "[]"; } diff --git a/Server/src/SOR.Game/GameMessages.cs b/Server/src/SOR.Game/GameMessages.cs index 9e86a09..8980786 100644 --- a/Server/src/SOR.Game/GameMessages.cs +++ b/Server/src/SOR.Game/GameMessages.cs @@ -846,7 +846,13 @@ public sealed record CostPickRequestPush( /// Who is picking: "SelfPick", "EnemySeePick", "EnemyRandomPick", "EnemyPick". string PickMode, /// JSON-serialized Filter[]? constraining valid selections. Null = no constraint. - string? FiltersJson); + string? FiltersJson, + /// + /// JSON-serialized array of { instanceId, name, cardType } — pre-computed eligible + /// cards/units from the actor's zone. Included so the opponent client can display + /// choices even when the zone is normally hidden in GameStateDto. + /// + string CandidatesJson = "[]"); /// /// Sent by the picker to submit their card/unit selections for a pending cost. diff --git a/Server/src/SOR.Game/Handlers/GameGetStateHandler.cs b/Server/src/SOR.Game/Handlers/GameGetStateHandler.cs index 2a80220..19a10ff 100644 --- a/Server/src/SOR.Game/Handlers/GameGetStateHandler.cs +++ b/Server/src/SOR.Game/Handlers/GameGetStateHandler.cs @@ -3,27 +3,32 @@ namespace SOR.Game.Handlers; using Microsoft.EntityFrameworkCore; using SOR.Database.Contexts; using SOR.Networking.Abstractions.Messaging; +using SOR.Networking.Abstractions.Push; using SOR.Networking.Abstractions.Transport; using SOR.Shared.Networking; /// /// game.get_state — returns the current game state filtered for the caller's perspective. -/// Useful for reconnects or initial load. +/// Useful for reconnects or initial load. After sending the state reply, also pushes any +/// active ability-pause prompts so the client can reconstruct the correct UI on reconnect. /// internal sealed class GameGetStateHandler : GameHandlerBase, IMessageHandler { private readonly IMessageSerializer _serializer; private readonly GameDbContext _gameDb; private readonly SorIdentityDbContext _identityDb; + private readonly IClientPush _clientPush; public GameGetStateHandler( IMessageSerializer serializer, GameDbContext gameDb, - SorIdentityDbContext identityDb) + SorIdentityDbContext identityDb, + IClientPush clientPush) { _serializer = serializer; _gameDb = gameDb; _identityDb = identityDb; + _clientPush = clientPush; } public string MessageType => "game.get_state"; @@ -67,7 +72,13 @@ internal sealed class GameGetStateHandler : GameHandlerBase, IMessageHandler state.Players[pid].DisplayName = dn; } - return ServerMessage.Reply("game.get_state.ack", message.Seq, + var reply = ServerMessage.Reply("game.get_state.ack", message.Seq, new GameGetStateResponse(GameEngine.ToDto(state, actorId))); + + // Re-push any active ability-pause prompts so the client can rebuild its UI + // even if the original push was missed (reconnect, tab switch, etc.). + await PushPendingPromptsAsync(_clientPush, state, ct); + + return reply; } } diff --git a/Server/src/SOR.Game/Handlers/GameHandlerBase.cs b/Server/src/SOR.Game/Handlers/GameHandlerBase.cs index a0694e1..0e3070a 100644 --- a/Server/src/SOR.Game/Handlers/GameHandlerBase.cs +++ b/Server/src/SOR.Game/Handlers/GameHandlerBase.cs @@ -250,22 +250,29 @@ internal abstract class GameHandlerBase await Task.WhenAll(tasks); } - // After the state update is sent to all players, check whether a triggered ability - // is waiting for client target input and push the request to the owning player. + await PushPendingPromptsAsync(clientPush, state, ct); + } + + /// + /// Pushes any active ability-pause prompts to the appropriate chooser player. + /// Called both from (after every state mutation) + /// and from (reconnect/sync path). + /// + internal static async Task PushPendingPromptsAsync( + IClientPush clientPush, + GameState state, + CancellationToken ct) + { if (state.PendingTriggeredTarget is { } pending) { - // Build the ActionJson for the first client-pick action in the pending list. - // We serialize only the first action that actually needs input so the client - // gets a minimal, unambiguous definition (N=1 unit pick for "Warrant Cover", etc.) var firstClientPickAction = JsonSerializer .Deserialize(pending.ActionsJson) ?.FirstOrDefault(AbilitySystem.ActionNeedsClientTargets_Public); var actionJson = firstClientPickAction is not null ? JsonSerializer.Serialize(firstClientPickAction) - : pending.ActionsJson; // fallback: send raw actions list + : pending.ActionsJson; - // Route to ChooserActorId (may be opponent/ally for Chooser.Opponent/Ally abilities). var targetPlayer = string.IsNullOrEmpty(pending.ChooserActorId) ? pending.ActorId : pending.ChooserActorId; @@ -279,7 +286,6 @@ internal abstract class GameHandlerBase pending.RangeJson)), ct); } - // Push prompts for all other ability-pause types. if (state.PendingCostPick is { } cp) await clientPush.PushAsync(cp.PickerActorId, ServerMessage.Push("game.ability.cost_pick_request", @@ -289,7 +295,8 @@ internal abstract class GameHandlerBase cp.CostType, cp.Amount, cp.PickMode, - cp.FiltersJson)), ct); + cp.FiltersJson, + cp.CandidatesJson)), ct); if (state.PendingCostGroupPick is { } cgp) {