Files
SVSimServer/SVSim.BattleNode/Sessions/Participants/ScriptedBotParticipant.cs
gamer147 0a8a84b2cc refactor(battle-node): unify TurnEndFinal / Retire-Kill / gameplay-forwarder dispatch across types
Three dispatch arms had Type-based branching that was either wrong or
unnecessary. Unified per the audit doc's recommended order, grounded in
verified facts about each participant's PushAsync.

(1) TurnEndFinal — was branched: PvP broadcast TurnEnd+Judge (wrong on a
game-end signal); Scripted pushed BattleFinish(LifeWin). Unified:
  - forward the envelope to other (matches prod TK2 capture
    battle-traffic_tk2_regular.ndjson:273 — loser receives TurnEndFinal
    from server before BattleFinish)
  - push BattleFinish(LifeWin) to from (winner)
  - push BattleFinish(LifeLose) to other (loser)
  - Phase → Terminal

  Requires ScriptedBotParticipant.PushAsync to no longer fire its 3-frame
  burst on TurnEndFinal (previously it reacted to both TurnEnd and
  TurnEndFinal). The dispatch arm now owns TurnEndFinal's response; the
  bot reacting too would race with the BattleFinish push. Bot still
  fires on regular TurnEnd as before.

(2) Retire / Kill — was branched: PvP pushed Lose=0 (NotFinish) /
Win=1 (NoContest); Scripted pushed BuildBattleFinishNoContest() (Win=1).
Both shipped wrong RESULT_CODE values; the audit doc's outstanding item
documented this. Unified:
  - push BattleFinish(RetireLose=106) to from (the retirer)
  - push BattleFinish(RetireWin=105) to other (the survivor)
  - Phase → Terminal

  Added RetireWin=105 / RetireLose=106 to BattleResult enum with the
  same player-perspective convention.

(3) PvP gameplay forwarder (TurnStart / PlayActions / Echo /
TurnEndActions / JudgeResult) — had a redundant `Type == BattleType.Pvp`
guard. Verified that BothAfterReady() is naturally only true when both
participants are RealParticipant (ScriptedBot / NoOpBot don't implement
IHasHandshakePhase per RealParticipant.cs:20-23 / Participants/*.cs grep).
Dropped the redundant guard.

Bot type still has its dedicated InitBattle/Loaded/TurnEnd arms above
the unified ones, so Bot-specific behavior is unchanged.

Tests: 177 battle-node tests passing.
- Updated 9 tests to match the unified dispatch (paired BattleFinish
  pushes, correct RESULT_CODE values, forwarded TurnEndFinal envelope).
- ScriptedBotParticipantTests.PushAsync_TurnEndFinal_* rewritten to
  assert the bot does NOT fire on TurnEndFinal (was asserting it did).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-02 18:37:24 -04:00

60 lines
3.1 KiB
C#

using System.Linq;
using SVSim.BattleNode.Bridge;
using SVSim.BattleNode.Lifecycle;
using SVSim.BattleNode.Protocol;
namespace SVSim.BattleNode.Sessions.Participants;
/// <summary>
/// Server-scripted opponent (today's v1.2 testing-harness behavior, repackaged).
/// On <see cref="PushAsync"/> with <c>TurnEnd</c> or <c>TurnEndFinal</c>, fires
/// <see cref="FrameEmitted"/> three times: <c>OpponentTurnStart</c>,
/// <c>OpponentTurnEnd</c>, <c>OpponentJudge</c>. All other URIs are swallowed
/// (no opponent reaction needed for v1.2 behavior).
/// </summary>
/// <remarks>
/// ViewerId, Context are fixtures matching <see cref="ScriptedLifecycle.FakeOpponentViewerId"/>
/// and a scripted opponent profile. The Context fixture is the source of truth for the
/// scripted opponent's half of Matched and BattleStart (cosmetics, deck count, class/chara) —
/// <see cref="BattleSession.ComputeFrames"/> reads <c>other.Context</c> for those frames.
/// Deal still uses fixed scripted frames that ignore Context.
/// </remarks>
public sealed class ScriptedBotParticipant : IBattleParticipant
{
public long ViewerId => ScriptedLifecycle.FakeOpponentViewerId;
public MatchContext Context { get; } = new(
// 30 dummy card ids so oppoCtx.SelfDeckCardIds.Count == 30 — prod frame[2] (Matched)
// shipped OppoDeckCount: 30.
SelfDeckCardIds: Enumerable.Range(1, 30).Select(_ => 0L).ToList(),
// BattleStart opponent half (frame[5]): ClassId/CharaId both "8" (neutral test class).
ClassId: "8", CharaId: "8", CardMasterName: "card_master_node_10015",
// Matched opponent half (frame[2]): cosmetic fields from the prod capture.
CountryCode: "JPN", UserName: "Opponent", SleeveId: "704141010",
EmblemId: "400001100", DegreeId: "120027", FieldId: 5, IsOfficial: 0,
BattleType: 0);
public event Func<MsgEnvelope, CancellationToken, Task>? FrameEmitted;
public async Task PushAsync(MsgEnvelope envelope, bool noStock, CancellationToken ct)
{
// React to the player's TurnEnd with the three-frame burst (TurnStart / TurnEnd /
// Judge) — that's the v1.2 "scripted bot takes its turn" behavior. Everything else
// (including TurnEndFinal) is silently swallowed: TurnEndFinal is the player's
// game-end signal and is handled directly by the BattleSession dispatch arm, which
// pushes BattleFinish per-side; the bot doesn't need to react.
if (envelope.Uri is NetworkBattleUri.TurnEnd)
{
await EmitAsync(ScriptedLifecycle.BuildOpponentTurnStart(), ct).ConfigureAwait(false);
await EmitAsync(ScriptedLifecycle.BuildOpponentTurnEnd(), ct).ConfigureAwait(false);
await EmitAsync(ScriptedLifecycle.BuildOpponentJudge(), ct).ConfigureAwait(false);
}
}
public Task RunAsync(CancellationToken ct) => Task.CompletedTask;
public Task TerminateAsync(BattleFinishReason reason) => Task.CompletedTask;
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
private Task EmitAsync(MsgEnvelope env, CancellationToken ct) =>
FrameEmitted?.Invoke(env, ct) ?? Task.CompletedTask;
}