From 030d3b805791dd1258f9ab2ded63b5ddb6a0b829 Mon Sep 17 00:00:00 2001 From: gamer147 Date: Wed, 3 Jun 2026 17:56:12 -0400 Subject: [PATCH] feat(battle-node): KnownListBuilder pure transforms (knownList synth, target rename) Co-Authored-By: Claude Opus 4.8 --- .../Sessions/Dispatch/KnownListBuilder.cs | 71 +++++++++++++++++ .../Sessions/KnownListBuilderTests.cs | 79 +++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 SVSim.BattleNode/Sessions/Dispatch/KnownListBuilder.cs create mode 100644 SVSim.UnitTests/BattleNode/Sessions/KnownListBuilderTests.cs diff --git a/SVSim.BattleNode/Sessions/Dispatch/KnownListBuilder.cs b/SVSim.BattleNode/Sessions/Dispatch/KnownListBuilder.cs new file mode 100644 index 0000000..7735d8f --- /dev/null +++ b/SVSim.BattleNode/Sessions/Dispatch/KnownListBuilder.cs @@ -0,0 +1,71 @@ +using SVSim.BattleNode.Protocol.Bodies; + +namespace SVSim.BattleNode.Sessions.Dispatch; + +/// Pure transforms from the active player's RawBody sub-structures to the opponent-facing +/// shapes. No session state, no wire I/O — unit-testable in isolation. RawBody nested values arrive +/// as Dictionary<string,object?> / List<object?> with numeric leaves boxed +/// as long/int/double (see MsgEnvelope.FromJson). +internal static class KnownListBuilder +{ + /// The played card's knownList entry, or null when its identity can't be synthesized + /// (token idx not in the deck map, or no matching move op). spellboost/attachTarget default to + /// 0/"" for the vanilla slice; cost/clan/tribe are deferred (receiver re-derives from cardId). + public static KnownCardEntry? BuildPlayedCard( + IReadOnlyDictionary deckMap, int playIdx, object? orderList) + { + if (!deckMap.TryGetValue(playIdx, out var cardId)) return null; + var to = ExtractMoveTo(orderList, playIdx); + if (to is null) return null; + return new KnownCardEntry(Idx: playIdx, CardId: cardId, To: to.Value, Spellboost: 0, AttachTarget: ""); + } + + /// The to place-state of the move op whose idx list contains + /// , or null if absent. + public static int? ExtractMoveTo(object? orderList, int playIdx) + { + if (orderList is not IEnumerable ops) return null; + foreach (var op in ops) + { + if (op is not IDictionary opDict) continue; + if (!opDict.TryGetValue("move", out var moveRaw) || moveRaw is not IDictionary move) continue; + if (move.TryGetValue("idx", out var idxRaw) && idxRaw is IEnumerable idxList) + { + foreach (var i in idxList) + if (AsLong(i) == playIdx && move.TryGetValue("to", out var toRaw)) + return (int)AsLong(toRaw); + } + } + return null; + } + + /// Rename targetList -> oppoTargetList; isSelf is actor-relative + /// and passes through unchanged (F2). Null for a missing/empty list. + public static IReadOnlyList? RenameTargets(object? targetList) + { + if (targetList is not IEnumerable entries) return null; + var result = new List(); + foreach (var e in entries) + { + if (e is not IDictionary d) continue; + d.TryGetValue("targetIdx", out var targetIdxRaw); + d.TryGetValue("isSelf", out var isSelfRaw); + result.Add(new OppoTargetEntry( + TargetIdx: (int)AsLong(targetIdxRaw), + IsSelf: (int)AsLong(isSelfRaw))); + } + return result.Count == 0 ? null : result; + } + + /// Coerce a boxed RawBody numeric leaf (long/int/double/decimal/string) to long; 0 for + /// null/unparseable. + public static long AsLong(object? value) => value switch + { + long l => l, + int i => i, + double d => (long)d, + decimal m => (long)m, + string s when long.TryParse(s, out var p) => p, + _ => 0, + }; +} diff --git a/SVSim.UnitTests/BattleNode/Sessions/KnownListBuilderTests.cs b/SVSim.UnitTests/BattleNode/Sessions/KnownListBuilderTests.cs new file mode 100644 index 0000000..796eea2 --- /dev/null +++ b/SVSim.UnitTests/BattleNode/Sessions/KnownListBuilderTests.cs @@ -0,0 +1,79 @@ +using NUnit.Framework; +using SVSim.BattleNode.Sessions.Dispatch; + +namespace SVSim.UnitTests.BattleNode.Sessions; + +[TestFixture] +public class KnownListBuilderTests +{ + // orderList as it arrives in a RawBody: a list of single-key op dicts. + private static List OrderListMove(int idx, int from, int to) => new() + { + new Dictionary + { + ["move"] = new Dictionary + { + ["idx"] = new List { (long)idx }, + ["isSelf"] = 1L, ["from"] = (long)from, ["to"] = (long)to, + } + } + }; + + [Test] + public void ExtractMoveTo_returns_to_for_matching_idx() + { + var to = KnownListBuilder.ExtractMoveTo(OrderListMove(3, 10, 20), playIdx: 3); + Assert.That(to, Is.EqualTo(20)); + } + + [Test] + public void ExtractMoveTo_returns_null_when_no_move_op_matches() + { + Assert.That(KnownListBuilder.ExtractMoveTo(OrderListMove(3, 10, 20), playIdx: 99), Is.Null); + Assert.That(KnownListBuilder.ExtractMoveTo(null, playIdx: 3), Is.Null); + } + + [Test] + public void BuildPlayedCard_synthesizes_entry_for_deck_card() + { + var deckMap = new Dictionary { [3] = 128821011L }; + var entry = KnownListBuilder.BuildPlayedCard(deckMap, playIdx: 3, orderList: OrderListMove(3, 10, 20)); + + Assert.That(entry, Is.Not.Null); + Assert.That(entry!.Idx, Is.EqualTo(3)); + Assert.That(entry.CardId, Is.EqualTo(128821011L)); + Assert.That(entry.To, Is.EqualTo(20)); + Assert.That(entry.Spellboost, Is.EqualTo(0)); + Assert.That(entry.AttachTarget, Is.EqualTo("")); + } + + [Test] + public void BuildPlayedCard_returns_null_for_token_idx_not_in_deck() + { + var deckMap = new Dictionary { [3] = 128821011L }; + var entry = KnownListBuilder.BuildPlayedCard(deckMap, playIdx: 31, orderList: OrderListMove(31, 10, 20)); + Assert.That(entry, Is.Null); + } + + [Test] + public void RenameTargets_passes_isSelf_through_verbatim() + { + var targetList = new List + { + new Dictionary { ["targetIdx"] = 8L, ["isSelf"] = 0L }, + }; + var renamed = KnownListBuilder.RenameTargets(targetList); + + Assert.That(renamed, Is.Not.Null); + Assert.That(renamed!.Count, Is.EqualTo(1)); + Assert.That(renamed[0].TargetIdx, Is.EqualTo(8)); + Assert.That(renamed[0].IsSelf, Is.EqualTo(0)); + } + + [Test] + public void RenameTargets_returns_null_for_missing_or_empty() + { + Assert.That(KnownListBuilder.RenameTargets(null), Is.Null); + Assert.That(KnownListBuilder.RenameTargets(new List()), Is.Null); + } +}