feat(battle-node): PlayActionsHandler synthesizes knownList (vanilla deck-card slice)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
gamer147
2026-06-03 17:59:54 -04:00
parent 030d3b8057
commit 506d286529
3 changed files with 122 additions and 16 deletions

View File

@@ -49,7 +49,7 @@ public sealed class BattleSession
[NetworkBattleUri.Kill] = retireKill,
[NetworkBattleUri.TurnStart] = new TurnStartHandler(),
[NetworkBattleUri.Judge] = new JudgeHandler(),
[NetworkBattleUri.PlayActions] = forwardWhenReady,
[NetworkBattleUri.PlayActions] = new PlayActionsHandler(),
[NetworkBattleUri.Echo] = forwardWhenReady,
[NetworkBattleUri.TurnEndActions] = forwardWhenReady,
[NetworkBattleUri.JudgeResult] = forwardWhenReady,

View File

@@ -0,0 +1,34 @@
using SVSim.BattleNode.Protocol;
using SVSim.BattleNode.Protocol.Bodies;
namespace SVSim.BattleNode.Sessions.Dispatch.Handlers;
/// <summary>PvP PlayActions translator (vanilla deck-card slice). Synthesizes the opponent-facing
/// knownList from the sender's idx->cardId map + the orderList move op, renames targetList ->
/// oppoTargetList, drops orderList, consumes keyAction. Token plays (idx>deck) degrade to
/// {playIdx,type} with a debug log. Scripted/Bot drop (no rule).</summary>
internal sealed class PlayActionsHandler : IFrameHandler
{
public IReadOnlyList<DispatchRoute> Handle(FrameDispatchContext ctx)
{
if (ctx.Type != BattleType.Pvp || !ctx.BothAfterReady())
return Array.Empty<DispatchRoute>();
var entries = (ctx.Env.Body as RawBody)?.Entries ?? new Dictionary<string, object?>();
var playIdx = (int)KnownListBuilder.AsLong(entries.GetValueOrDefault("playIdx"));
var type = (int)KnownListBuilder.AsLong(entries.GetValueOrDefault("type"));
var deckMap = ctx.State.GetOrSeedDeckMap(ctx.From);
var played = KnownListBuilder.BuildPlayedCard(deckMap, playIdx, entries.GetValueOrDefault("orderList"));
var oppoTargets = KnownListBuilder.RenameTargets(entries.GetValueOrDefault("targetList"));
var body = new PlayActionsBroadcastBody(
PlayIdx: playIdx,
Type: type,
KnownList: played is null ? null : new[] { played },
OppoTargetList: oppoTargets);
var frame = ctx.Env with { Body = body };
return new[] { new DispatchRoute(ctx.Other, frame, false) };
}
}

View File

@@ -398,17 +398,83 @@ public class BattleSessionDispatchTests
}
[Test]
public void Pvp_PlayActions_from_A_in_BothAfterReady_forwards_to_B()
public void Pvp_PlayActions_synthesizes_knownList_from_sender_deck()
{
var (s, a, b) = NewPvpSession();
DriveToAfterReady(s, a);
DriveToAfterReady(s, b);
var routes = s.ComputeFrames(a, NewEnvelope(NetworkBattleUri.PlayActions));
var body = MoveOrderList(idx: 3, from: 10, to: 20);
body["playIdx"] = 3L;
body["type"] = 30L;
var routes = s.ComputeFrames(a, EnvWith(NetworkBattleUri.PlayActions, body));
Assert.That(routes.Count, Is.EqualTo(1));
Assert.That(routes[0].Target, Is.SameAs(b));
Assert.That(routes[0].Frame.Uri, Is.EqualTo(NetworkBattleUri.PlayActions));
var pb = (PlayActionsBroadcastBody)routes[0].Frame.Body;
Assert.That(pb.PlayIdx, Is.EqualTo(3));
Assert.That(pb.Type, Is.EqualTo(30));
Assert.That(pb.KnownList!.Count, Is.EqualTo(1));
Assert.That(pb.KnownList[0].Idx, Is.EqualTo(3));
Assert.That(pb.KnownList[0].CardId, Is.EqualTo(100_011_010L)); // PlayerACtx deck cardId
Assert.That(pb.KnownList[0].To, Is.EqualTo(20));
Assert.That(pb.OppoTargetList, Is.Null);
}
[Test]
public void Pvp_PlayActions_renames_targetList_to_oppoTargetList()
{
var (s, a, b) = NewPvpSession();
DriveToAfterReady(s, a);
DriveToAfterReady(s, b);
var body = MoveOrderList(idx: 3, from: 10, to: 20);
body["playIdx"] = 3L;
body["type"] = 31L;
body["targetList"] = new List<object?>
{
new Dictionary<string, object?> { ["targetIdx"] = 8L, ["isSelf"] = 0L },
};
var routes = s.ComputeFrames(a, EnvWith(NetworkBattleUri.PlayActions, body));
var pb = (PlayActionsBroadcastBody)routes[0].Frame.Body;
Assert.That(pb.OppoTargetList!.Count, Is.EqualTo(1));
Assert.That(pb.OppoTargetList[0].TargetIdx, Is.EqualTo(8));
Assert.That(pb.OppoTargetList[0].IsSelf, Is.EqualTo(0));
}
[Test]
public void Pvp_PlayActions_token_idx_degrades_to_no_knownList()
{
var (s, a, b) = NewPvpSession();
DriveToAfterReady(s, a);
DriveToAfterReady(s, b);
var body = MoveOrderList(idx: 31, from: 10, to: 20); // idx 31 > 30-card deck → token
body["playIdx"] = 31L;
body["type"] = 30L;
var routes = s.ComputeFrames(a, EnvWith(NetworkBattleUri.PlayActions, body));
var pb = (PlayActionsBroadcastBody)routes[0].Frame.Body;
Assert.That(routes.Count, Is.EqualTo(1));
Assert.That(pb.PlayIdx, Is.EqualTo(31));
Assert.That(pb.KnownList, Is.Null);
}
[Test]
public void Pvp_PlayActions_when_B_still_AwaitingSwap_drops()
{
var (s, a, b) = NewPvpSession();
DriveToAfterReady(s, a);
// B not AfterReady → not BothAfterReady.
var body = MoveOrderList(3, 10, 20);
body["playIdx"] = 3L; body["type"] = 30L;
var routes = s.ComputeFrames(a, EnvWith(NetworkBattleUri.PlayActions, body));
Assert.That(routes, Is.Empty);
}
[Test]
@@ -450,19 +516,6 @@ public class BattleSessionDispatchTests
Assert.That(routes[0].Target, Is.SameAs(b));
}
[Test]
public void Pvp_PlayActions_when_B_still_AwaitingSwap_drops()
{
var (s, a, b) = NewPvpSession();
DriveToAfterReady(s, a);
// B is still AwaitingInitNetwork — BothAfterReady is false.
var routes = s.ComputeFrames(a, NewEnvelope(NetworkBattleUri.PlayActions));
Assert.That(routes, Is.Empty,
"PvP gameplay forwarding must wait until BOTH sides reach AfterReady.");
}
[Test]
public void Pvp_TurnEnd_from_A_in_BothAfterReady_broadcasts_TurnEnd_plus_Judge_to_both()
{
@@ -776,6 +829,25 @@ public class BattleSessionDispatchTests
Cat: EmitCategory.Battle, PubSeq: null, PlaySeq: null,
Body: new RawBody(new Dictionary<string, object?>()));
private static MsgEnvelope EnvWith(NetworkBattleUri uri, Dictionary<string, object?> body) =>
new(uri, ViewerId: 1, Uuid: "u", Bid: null, Try: 0,
Cat: EmitCategory.Battle, PubSeq: null, PlaySeq: null, Body: new RawBody(body));
private static Dictionary<string, object?> MoveOrderList(int idx, int from, int to) => new()
{
["orderList"] = new List<object?>
{
new Dictionary<string, object?>
{
["move"] = new Dictionary<string, object?>
{
["idx"] = new List<object?> { (long)idx },
["isSelf"] = 1L, ["from"] = (long)from, ["to"] = (long)to,
}
}
}
};
/// <summary>Data-only IBattleParticipant stub for dispatch tests. PushAsync/RunAsync
/// are no-ops; FrameEmitted exists but is never invoked by the test.</summary>
private sealed class FakeParticipant : IBattleParticipant