fix(ability-prompts): fix 3 client wiring gaps

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 <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
dingo
2026-04-21 17:13:04 -04:00
co-authored by Claude Happy
parent c45db289ad
commit 7a917cf93b
6 changed files with 76 additions and 29 deletions
@@ -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<CardInstance> rawCandidates = cost.Type switch
{
CostType.DiscardFromHand =>
player.Hand,
CostType.SendSpiritFromBattlefieldToDiscard =>
player.Field.OfType<FieldCard>().Where(f => f.CardType == "Spirit").Cast<CardInstance>(),
CostType.DealDamageToOwnSpirit =>
player.Field.OfType<FieldCard>().Where(f => f.CardType == "Spirit").Cast<CardInstance>(),
_ => Enumerable.Empty<CardInstance>()
};
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
}
@@ -50,4 +50,11 @@ public sealed class PendingCostPick
/// <summary>JSON-serialized DurationModel? — forceDuration passed to ExecuteActions on resume.</summary>
public string? ForceDurationJson { get; set; }
/// <summary>
/// JSON-serialized array of <c>{ instanceId, name, cardType }</c> 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.
/// </summary>
public string CandidatesJson { get; set; } = "[]";
}
+7 -1
View File
@@ -846,7 +846,13 @@ public sealed record CostPickRequestPush(
/// <summary>Who is picking: "SelfPick", "EnemySeePick", "EnemyRandomPick", "EnemyPick".</summary>
string PickMode,
/// <summary>JSON-serialized Filter[]? constraining valid selections. Null = no constraint.</summary>
string? FiltersJson);
string? FiltersJson,
/// <summary>
/// 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.
/// </summary>
string CandidatesJson = "[]");
/// <summary>
/// Sent by the picker to submit their card/unit selections for a pending cost.
@@ -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;
/// <summary>
/// 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.
/// </summary>
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;
}
}
@@ -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);
}
/// <summary>
/// Pushes any active ability-pause prompts to the appropriate chooser player.
/// Called both from <see cref="PushStateToPlayersAsync"/> (after every state mutation)
/// and from <see cref="GameGetStateHandler"/> (reconnect/sync path).
/// </summary>
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<Abilities.Models.Action[]>(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)
{