Files
SVSimServer/SVSim.BattleNode/Sessions/Dispatch/KnownListBuilder.cs
gamer147 b6af8bfb7d feat(battle-node): mine generated-token cardIds from orderList add ops
KnownListBuilder.MineAddOps extracts (idx,cardId) from isSelf:1 add ops,
skipping cross-side gifts and choice tokens. Bullet-3 audit F1.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-03 23:30:47 -04:00

106 lines
5.5 KiB
C#

using SVSim.BattleNode.Protocol.Bodies;
namespace SVSim.BattleNode.Sessions.Dispatch;
/// <summary>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 <c>Dictionary&lt;string,object?&gt;</c> / <c>List&lt;object?&gt;</c> with numeric leaves boxed
/// as long/int/double (see MsgEnvelope.FromJson).</summary>
internal static class KnownListBuilder
{
/// <summary>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).</summary>
public static KnownCardEntry? BuildPlayedCard(
IReadOnlyDictionary<int, long> 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: "");
}
/// <summary>The <c>to</c> place-state of the FIRST <c>move</c> op whose <c>idx</c> list contains
/// <paramref name="playIdx"/> (the played card's own move; later add/alter ops are the deferred
/// token slice), or null if absent. NOTE: the sender-side <c>to</c> is passed through verbatim —
/// for the vanilla slice we assume send-side and recv-side place-state codes match, pending
/// recv-capture confirmation.</summary>
public static int? ExtractMoveTo(object? orderList, int playIdx)
{
if (orderList is not IEnumerable<object?> ops) return null;
foreach (var op in ops)
{
if (op is not IDictionary<string, object?> opDict) continue;
if (!opDict.TryGetValue("move", out var moveRaw) || moveRaw is not IDictionary<string, object?> move) continue;
if (move.TryGetValue("idx", out var idxRaw) && idxRaw is IEnumerable<object?> idxList)
{
foreach (var i in idxList)
if (AsLong(i) == playIdx && move.TryGetValue("to", out var toRaw))
return (int)AsLong(toRaw);
}
}
return null;
}
/// <summary>Mine generated-token identities from the sender's <c>add</c> ops: yields
/// <c>(idx, cardId)</c> for every idx in each <c>{add:{idx:[...], isSelf:1, card:{cardId}}}</c>
/// op. Skips <c>isSelf:0</c> adds (cross-side gifts — belong in the other side's map, deferred)
/// and any add whose <c>card</c> has no concrete <c>cardId</c> — choice tokens
/// (<c>card:{candidates}</c>, <c>RegisterChoiceAdd</c>), copy tokens (<c>card:{baseIdx}</c>,
/// <c>RegisterCopyToken</c>), and private-group adds (string <c>idx</c>) — all deferred and all
/// caught by the <c>cardId</c>-key / <c>idx</c>-is-list guards. This is the only place a
/// freshly-generated card's identity exists on the wire (bullet-3 audit F1; producing code
/// <c>RegisterToken</c>/<c>RegisterActionBase</c>) — the played-card op itself never carries a
/// <c>cardId</c>.</summary>
public static IEnumerable<(int Idx, long CardId)> MineAddOps(object? orderList)
{
if (orderList is not IEnumerable<object?> ops) yield break;
foreach (var op in ops)
{
if (op is not IDictionary<string, object?> opDict) continue;
if (!opDict.TryGetValue("add", out var addRaw) || addRaw is not IDictionary<string, object?> add) continue;
add.TryGetValue("isSelf", out var isSelfRaw);
if (AsLong(isSelfRaw) != 1) continue; // own tokens only; cross-side gifts deferred
if (!add.TryGetValue("card", out var cardRaw) || cardRaw is not IDictionary<string, object?> card) continue;
if (!card.TryGetValue("cardId", out var cardIdRaw)) continue; // candidates/isChoice → no identity yet
var cardId = AsLong(cardIdRaw);
if (!add.TryGetValue("idx", out var idxRaw) || idxRaw is not IEnumerable<object?> idxList) continue;
foreach (var i in idxList)
yield return ((int)AsLong(i), cardId);
}
}
/// <summary>Rename <c>targetList</c> -> <c>oppoTargetList</c>; <c>isSelf</c> is actor-relative
/// and passes through unchanged (F2). Null for a missing/empty list.</summary>
public static IReadOnlyList<OppoTargetEntry>? RenameTargets(object? targetList)
{
if (targetList is not IEnumerable<object?> entries) return null;
var result = new List<OppoTargetEntry>();
foreach (var e in entries)
{
if (e is not IDictionary<string, object?> 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;
}
/// <summary>Coerce a boxed RawBody numeric leaf (long/int/double/decimal/string) to long; 0 for
/// null/unparseable.</summary>
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,
};
}