From 473e5b075e32c5080f7c1919094c5973606cf97d Mon Sep 17 00:00:00 2001 From: dingo Date: Tue, 21 Apr 2026 11:30:01 -0400 Subject: [PATCH] fix(abilities): nested pause during intra-action replay captures correct remainder When ApplyIntraActionContinuation replays IntraActionStep items and a step's effect itself pauses (emitting PendingTilePick / PendingPlayerPick / PendingRevealChoicePick), the new pause model may already have an IntraActionContinuationJson written by BuildIntraActionContinuationJson. That value is wrong in the replay context because BuildIntraActionContinuationJson assumes all remaining targets get the same homogeneous effect list, whereas IntraActionStep items each carry their own per-step effect list. Fix: convert ApplyIntraActionContinuation loops to indexed, set ctx position fields (CurrentEffectIndex, CurrentActionAllEffects, CurrentActionTargets, CurrentTargetIndex, CurrentActionDuration) per iteration for consistency, then after any nested pause override the IntraActionContinuationJson directly via new SetPendingIntraActionContinuation helper. The override is built from the precise remaining slice: - remaining effects on the current step's target (effects[(ei+1)..]) - all remaining steps as-is (steps[(si+1)..]) This is correct regardless of whether different steps have different effect lists. Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude Co-Authored-By: Happy --- .../src/SOR.Game/Abilities/AbilitySystem.cs | 74 +++++++++++++++++-- 1 file changed, 68 insertions(+), 6 deletions(-) diff --git a/Server/src/SOR.Game/Abilities/AbilitySystem.cs b/Server/src/SOR.Game/Abilities/AbilitySystem.cs index b603bf7..ee3ef28 100644 --- a/Server/src/SOR.Game/Abilities/AbilitySystem.cs +++ b/Server/src/SOR.Game/Abilities/AbilitySystem.cs @@ -4533,6 +4533,14 @@ public static class AbilitySystem /// Call this in resume methods after completing the effect-specific work and /// before executing the outer ContinuationActionsJson. /// + /// + /// Each can have a different effect list (e.g. the first step + /// is only remaining effects on the originally-paused target, subsequent steps are the full + /// effect list for each remaining target). If a nested pause occurs mid-replay we directly + /// rebuild the next IntraActionContinuationJson from the remaining slice rather than + /// relying on , which assumes a homogeneous + /// per-target effect list and would produce wrong results here. + /// private static void ApplyIntraActionContinuation( string? intraJson, DurationModel? actionDuration, @@ -4544,23 +4552,77 @@ public static class AbilitySystem catch { return; } if (steps is null) return; - foreach (var step in steps) + for (int si = 0; si < steps.Length; si++) { - var tgt = FindTargetByRef(ctx.State, step.TargetRef); + var step = steps[si]; + var tgt = FindTargetByRef(ctx.State, step.TargetRef); if (tgt is null) continue; + Effect[]? effects; try { effects = JsonSerializer.Deserialize(step.EffectsJson, _jsonOpts); } catch { continue; } if (effects is null) continue; - foreach (var eff in effects) + + for (int ei = 0; ei < effects.Length; ei++) { - ExecuteEffect(eff, tgt, actionDuration, ctx); - if (ctx.State.HasPendingInteraction()) return; + // Set position fields so ExecuteEffect has consistent context, but note that + // we will override any IntraActionContinuationJson it writes (see below). + ctx.CurrentEffectIndex = ei; + ctx.CurrentActionAllEffects = effects; + ctx.CurrentActionTargets = [tgt]; + ctx.CurrentTargetIndex = 0; + ctx.CurrentActionDuration = actionDuration; + + ExecuteEffect(effects[ei], tgt, actionDuration, ctx); + + if (ctx.State.HasPendingInteraction()) + { + // A nested pause was set. BuildIntraActionContinuationJson may have + // already written an IntraActionContinuationJson based on the single-target + // ctx snapshot above, which is incomplete. Override it with the precise + // remaining slice: remaining effects on this step's target + remaining steps. + var remEffects = effects[(ei + 1)..]; + var newSteps = new List(); + if (remEffects.Length > 0) + newSteps.Add(new IntraActionStep + { + TargetRef = step.TargetRef, + EffectsJson = JsonSerializer.Serialize(remEffects, _jsonOpts), + }); + for (int rs = si + 1; rs < steps.Length; rs++) + newSteps.Add(steps[rs]); + + SetPendingIntraActionContinuation(ctx.State, + newSteps.Count > 0 ? JsonSerializer.Serialize(newSteps, _jsonOpts) : null); + return; + } + } + + if (ctx.State.HasPendingInteraction()) + { + // Pause between effects (edge case). Remaining = steps after si. + var remSteps = new List(); + for (int rs = si + 1; rs < steps.Length; rs++) + remSteps.Add(steps[rs]); + SetPendingIntraActionContinuation(ctx.State, + remSteps.Count > 0 ? JsonSerializer.Serialize(remSteps, _jsonOpts) : null); + return; } - if (ctx.State.HasPendingInteraction()) return; } } + /// + /// Overwrites the IntraActionContinuationJson on whichever pause model is currently + /// set in . Used by to + /// replace any stale value written by . + /// + private static void SetPendingIntraActionContinuation(GameState state, string? intraJson) + { + if (state.PendingTilePick is { } tp) { tp.IntraActionContinuationJson = intraJson; return; } + if (state.PendingPlayerPick is { } pp) { pp.IntraActionContinuationJson = intraJson; return; } + if (state.PendingRevealChoicePick is { } rcp) { rcp.IntraActionContinuationJson = intraJson; } + } + /// Internal metadata envelope stored in ActiveEffect.DurationJson. private sealed class TrackedDuration {