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>
This commit is contained in:
gamer147
2026-06-03 23:30:47 -04:00
parent 4b38a9d3e0
commit b6af8bfb7d
2 changed files with 122 additions and 0 deletions

View File

@@ -42,6 +42,37 @@ internal static class KnownListBuilder
return null; 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 /// <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> /// and passes through unchanged (F2). Null for a missing/empty list.</summary>
public static IReadOnlyList<OppoTargetEntry>? RenameTargets(object? targetList) public static IReadOnlyList<OppoTargetEntry>? RenameTargets(object? targetList)

View File

@@ -111,4 +111,95 @@ public class KnownListBuilderTests
Assert.That(KnownListBuilder.RenameTargets(null), Is.Null); Assert.That(KnownListBuilder.RenameTargets(null), Is.Null);
Assert.That(KnownListBuilder.RenameTargets(new List<object?>()), Is.Null); Assert.That(KnownListBuilder.RenameTargets(new List<object?>()), Is.Null);
} }
// An add op as it arrives in a RawBody: { "add": { "idx": [..], "isSelf": n, "card": { "cardId": n } } }
private static Dictionary<string, object?> AddOp(long[] idxs, long cardId, long isSelf = 1) => new()
{
["add"] = new Dictionary<string, object?>
{
["idx"] = idxs.Select(i => (object?)i).ToList(),
["isSelf"] = isSelf,
["card"] = new Dictionary<string, object?> { ["cardId"] = cardId },
}
};
[Test]
public void MineAddOps_yields_idx_to_cardId_for_every_idx_in_an_add_op()
{
var orderList = new List<object?> { AddOp(new[] { 31L, 32L }, 900111010L) };
var mined = KnownListBuilder.MineAddOps(orderList).ToList();
Assert.That(mined, Is.EquivalentTo(new[] { (31, 900111010L), (32, 900111010L) }));
}
[Test]
public void MineAddOps_skips_add_ops_for_the_opponent_isSelf_0()
{
// A card given to the opponent (isSelf:0) belongs in the other side's map — deferred.
var orderList = new List<object?> { AddOp(new[] { 31L }, 900111010L, isSelf: 0) };
Assert.That(KnownListBuilder.MineAddOps(orderList), Is.Empty);
}
[Test]
public void MineAddOps_skips_choice_adds_with_no_concrete_cardId()
{
// { "add": { "idx":[46], "card": { "candidates":[...] }, "isChoice":"1" } } — identity undetermined.
var orderList = new List<object?>
{
new Dictionary<string, object?>
{
["add"] = new Dictionary<string, object?>
{
["idx"] = new List<object?> { 46L },
["isSelf"] = 1L,
["card"] = new Dictionary<string, object?>
{
["candidates"] = new List<object?> { 810041260L, 101041020L },
},
["isChoice"] = "1",
}
}
};
Assert.That(KnownListBuilder.MineAddOps(orderList), Is.Empty);
}
[Test]
public void MineAddOps_skips_copy_token_adds_with_baseIdx_and_no_cardId()
{
// RegisterCopyToken.MakeCardData → { "baseIdx": N, "isPremium": 0 } — no cardId, deferred.
var orderList = new List<object?>
{
new Dictionary<string, object?>
{
["add"] = new Dictionary<string, object?>
{
["idx"] = new List<object?> { 33L },
["isSelf"] = 1L,
["card"] = new Dictionary<string, object?> { ["baseIdx"] = 12L, ["isPremium"] = 0L },
}
}
};
Assert.That(KnownListBuilder.MineAddOps(orderList), Is.Empty);
}
[Test]
public void MineAddOps_ignores_non_add_ops_and_null()
{
Assert.That(KnownListBuilder.MineAddOps(OrderListMove(3, 10, 20)), Is.Empty);
Assert.That(KnownListBuilder.MineAddOps(null), Is.Empty);
}
[Test]
public void MineAddOps_yields_from_multiple_add_ops_in_one_orderList()
{
var orderList = new List<object?>
{
new Dictionary<string, object?> { ["move"] = new Dictionary<string, object?>
{ ["idx"] = new List<object?> { 3L }, ["isSelf"] = 1L, ["from"] = 10L, ["to"] = 30L } },
AddOp(new[] { 31L }, 900111010L),
AddOp(new[] { 32L }, 900811090L),
};
var mined = KnownListBuilder.MineAddOps(orderList).ToList();
Assert.That(mined, Is.EquivalentTo(new[] { (31, 900111010L), (32, 900811090L) }));
}
} }