From 09a1c32560f197b8ba79cafbdb921702450ce879 Mon Sep 17 00:00:00 2001 From: soul Date: Fri, 20 Nov 2026 01:49:18 +0100 Subject: [PATCH] Make a refused summon ledger-neutral: ItemManager.Mark/Rewind restore the stream and the lot ledger together Co-Authored-By: Claude Sonnet 5.5 --- Assets/Scripts/ServerShared/ItemManager.cs | 19 +++++++++++++++++++ Assets/Scripts/ServerShared/ScenarioStage.cs | 6 +++--- tests/Aetheria.Shared.Tests/ScenarioTests.cs | 8 ++++++-- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/Assets/Scripts/ServerShared/ItemManager.cs b/Assets/Scripts/ServerShared/ItemManager.cs index c91568efd..d5c1e05dd 100644 --- a/Assets/Scripts/ServerShared/ItemManager.cs +++ b/Assets/Scripts/ServerShared/ItemManager.cs @@ -45,6 +45,25 @@ public class ItemManager _logger = logger; } + // A restore point over everything building items draws or mints: the item stream and the lot ledger, which a run save + // persists. Rewind puts both back, so a refused build is neither a draw nor a lot. + public readonly struct Checkpoint + { + internal readonly Random Stream; + internal readonly int NextLot; + internal Checkpoint(Random stream, int nextLot) { Stream = stream; NextLot = nextLot; } + } + + public Checkpoint Mark() => new Checkpoint(Random, Lots.NextLot); + + public void Rewind(Checkpoint mark) + { + Random = mark.Stream; + foreach (var id in Lots.Lots.Keys.Where(id => id >= mark.NextLot).ToList()) Lots.Lots.Remove(id); + Lots.NextLot = mark.NextLot; + foreach (var key in _extracted.Where(entry => entry.Value >= mark.NextLot).Select(entry => entry.Key).ToList()) _extracted.Remove(key); + } + public void Log(string s) { _logger(s); diff --git a/Assets/Scripts/ServerShared/ScenarioStage.cs b/Assets/Scripts/ServerShared/ScenarioStage.cs index ed13ae27b..56bb50ea8 100644 --- a/Assets/Scripts/ServerShared/ScenarioStage.cs +++ b/Assets/Scripts/ServerShared/ScenarioStage.cs @@ -201,7 +201,7 @@ public class ZoneStage // Places ships into a running zone through the admission a run start uses: the script places with the stage's verbs, // and the entities join with their stance toward the player, a pilot when piloted and the knowledge the script stated. - // Returns the placed entities in order, or null with every failure listed and the zone and the item stream unchanged. + // Returns the placed entities in order, or null with every failure listed and the zone and the item stream and lot ledger unchanged. // It draws from the live item stream and does not reseed (a reseed would give every summon the same fits). The anchor // is the player's ship: the stance is written against it. public static IReadOnlyList Summon(ItemManager items, Zone zone, Ship player, Action script, List failures) @@ -216,13 +216,13 @@ public class ZoneStage failures.Add("summon: the player is not in this zone"); return null; } - var stream = items.Random; + var mark = items.Mark(); var stage = new ZoneStage(items, zone); script(stage); failures.AddRange(stage._failures); if (stage._failures.Count > 0) { - items.Random = stream; // a refusal draws nothing: the fits it built are discarded with it + items.Rewind(mark); // a refusal draws and mints nothing: the fits it built are discarded with it return null; } stage.StockUnauthoredAmmunition(); diff --git a/tests/Aetheria.Shared.Tests/ScenarioTests.cs b/tests/Aetheria.Shared.Tests/ScenarioTests.cs index 74df3f8eb..7144fffbf 100644 --- a/tests/Aetheria.Shared.Tests/ScenarioTests.cs +++ b/tests/Aetheria.Shared.Tests/ScenarioTests.cs @@ -469,14 +469,16 @@ public sealed partial class RunStartTests SafeAssert.Unchanged(zone, entities, agents); } - // A refused summon leaves the item stream where it was, so a refusal is not a draw: the next fit is the one it would have been. - // Kills: a summon that keeps the draws of the fits it built before refusing. + // A refused summon leaves the item stream and the lot ledger (which a run save persists) where they were, so a refusal is neither + // a draw nor a mint. Kills: a summon that keeps the draws or the lots of the fits it built before refusing. [Fact] public void ARefusedSummonLeavesTheItemStream() { var zone = Arena(true); var player = Stage(zone, new Scripted(true, stage => stage.Player(stage.Bare("Djinni"), float2(0, 0)))).Player; var before = _items.Random; + var nextLot = _items.Lots.NextLot; + var lots = _items.Lots.Lots.Keys.ToList(); var failures = new List(); Assert.Null(ZoneStage.Summon(_items, zone, player, stage => @@ -487,6 +489,8 @@ public sealed partial class RunStartTests var after = _items.Random; Assert.Equal(before.NextUInt(), after.NextUInt()); + Assert.Equal(nextLot, _items.Lots.NextLot); + Assert.Equal(lots.OrderBy(id => id), _items.Lots.Lots.Keys.OrderBy(id => id)); } // The anchor of a summon is the player's ship, not any ship in the zone. Kills: a summon that takes any zone member as the player.