From ac0886389aa0eb1122e24f806ab904371f57960e Mon Sep 17 00:00:00 2001 From: gamer147 Date: Sat, 6 Jun 2026 12:23:51 -0400 Subject: [PATCH] feat(battle-engine M13): M3 spell emits PlayActions headless via OperateMgr -> NetworkBattleSender (O1 read = GO) Co-Authored-By: Claude Opus 4.8 --- .../EmitPathReadOracleTests.cs | 62 ++++++++++++++++ SVSim.BattleEngine.Tests/HeadlessFixture.cs | 72 ++++++++++++++++++- .../Rng/HeadlessNetworkBattleMgr.cs | 18 ++--- .../Shim/Generated/_IfaceImpl.g.cs | 10 ++- .../Shim/View/HeadlessHandViewStub.cs | 28 ++++++++ 5 files changed, 179 insertions(+), 11 deletions(-) create mode 100644 SVSim.BattleEngine.Tests/EmitPathReadOracleTests.cs create mode 100644 SVSim.BattleEngine/Shim/View/HeadlessHandViewStub.cs diff --git a/SVSim.BattleEngine.Tests/EmitPathReadOracleTests.cs b/SVSim.BattleEngine.Tests/EmitPathReadOracleTests.cs new file mode 100644 index 0000000..09a123f --- /dev/null +++ b/SVSim.BattleEngine.Tests/EmitPathReadOracleTests.cs @@ -0,0 +1,62 @@ +using NUnit.Framework; +using Wizard; +using Wizard.Battle; + +namespace SVSim.BattleEngine.Tests +{ + // M13 (hub O1, deterministic): the first headless observation of the EMIT path. Drive the proven M3 + // fixed-damage spell (900124030) through mgr.OperateMgr.PlayCard on a NetworkBattleManagerBase-derived + // mgr and confirm the engine reaches its emission path (RealTimeNetworkAgent.OnEmit fires PlayActions) + // without crashing, while the committed state still matches the M3 direct-ActionProcessor oracle. + // Liveness only (E4); structural frame decoding + the RNG rand-list (M14) are deferred. + [TestFixture] + public class EmitPathReadOracleTests + { + // Reset the process globals this fixture mutates so no later fixture inherits them: + // - ToolboxGame.RealTimeNetworkAgent: the injected agent (the solo oracles don't read it, but + // clearing it is defensive, mirroring RandomDrawOracleTests.ResetRandomDrawGate). + // - BattleManagerBase.IsForecast: the network emit harness sets this FALSE (the emit path needs it); + // every solo oracle and EnsureInitialized assume it TRUE. EnsureInitialized only sets it once + // (guarded), so without restoring it here a solo fixture running after this one would see + // IsForecast=false and un-suppressed VFX. Restore the EnsureInitialized default. + [TearDown] + public void ResetGlobals() + { + Wizard.ToolboxGame.SetRealTimeNetworkBattle(null); + BattleManagerBase.IsForecast = true; + } + + [Test] + public void M3_spell_driven_via_OperateMgr_reaches_emit_without_crashing() + { + var (mgr, emitted) = HeadlessEngineEnv.NewNetworkEmitBattle(); + var player = mgr.BattlePlayer; + var enemy = mgr.BattleEnemy; + + int leaderLifeBefore = enemy.Class.Life; + + var spell = HeadlessEngineEnv.CreateHeadlessHandCard( + HeadlessEngineEnv.SpellId, index: 1, isPlayer: true, mgr); + player.HandCardList.Add(spell); + int cost = spell.Cost; + player.Pp = 10; + + Assert.DoesNotThrow( + () => mgr.OperateMgr.PlayCard(spell, isPlayer: true, selectCards: null), + "OperateMgr.PlayCard threw driving the M3 spell through the emit path"); + + Assert.Multiple(() => + { + // Emit reached: OnEmit fired with PlayActions (the O1 liveness signal). + Assert.That(emitted, Does.Contain(NetworkBattleDefine.NetworkBattleURI.PlayActions), + "the engine did not reach a PlayActions emit"); + // State intact vs the M3 direct-path oracle. + Assert.That(enemy.Class.Life, Is.EqualTo(leaderLifeBefore - 3), "enemy leader should take 3"); + Assert.That(player.Pp, Is.EqualTo(10 - cost), "PP should be paid"); + Assert.That(player.HandCardList, Does.Not.Contain(spell), "spell should leave the hand"); + Assert.That(player.CemeteryList, Does.Contain(spell), "spell should land in the cemetery"); + Assert.That(player.ClassAndInPlayCardList, Does.Not.Contain(spell), "a spell does not occupy the board"); + }); + } + } +} diff --git a/SVSim.BattleEngine.Tests/HeadlessFixture.cs b/SVSim.BattleEngine.Tests/HeadlessFixture.cs index 0d49ab0..40c1efc 100644 --- a/SVSim.BattleEngine.Tests/HeadlessFixture.cs +++ b/SVSim.BattleEngine.Tests/HeadlessFixture.cs @@ -216,6 +216,16 @@ namespace SVSim.BattleEngine.Tests new System.Collections.Generic.Dictionary { ["fieldId"] = 1 }, isWatchReplayRecovery: false); GameMgr.GetIns().SetNetworkUserInfoData(netUser); + + // The network emit path's payload builder (RealTimeNetworkAgent.CreateEmitData) reads + // Cute.Certification.Udid (RealTimeNetworkAgent.cs:1407). The Udid getter lazily decodes from + // Toolbox.SavedataManager (Certification.cs:35), which is null headless. Seed the private static + // backing field with a non-empty placeholder so the getter short-circuits before touching the + // savedata manager. The value is opaque to the engine (it's just echoed into the emit dict). + typeof(Cute.Certification) + .GetField("udid", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic) + .SetValue(null, "headless-udid"); + _done = true; } @@ -353,8 +363,24 @@ namespace SVSim.BattleEngine.Tests { EnsureInitialized(); // sets IsForecast = true among other globals var mgr = new HeadlessNetworkBattleMgr(new HeadlessContentsCreator(), rng); - mgr.IsRecovery = true; // collapse wait delays to 0 (F1) + // NOTE: IsRecovery is left FALSE here (unlike the solo NewAuthoritativeBattle). The network + // emit path is gated on !IsRecovery in BOTH places: NetworkStandardBattleMgr.SendPlayCard + // (NetworkStandardBattleMgr.cs:155) and the OnSetCardComplete->SendPlayCard subscription in + // SetUpNetworkOperateEvent (NetworkBattleManagerBase.cs:927, which early-returns under + // IsRecovery). With IsRecovery=true the play would resolve state but never emit. (The solo + // NewAuthoritativeBattle uses IsRecovery=true only to collapse VFX wait delays; here the no-op + // view shims absorb the real view layer instead — see the IsForecast=false block below.) + // IsForecast MUST be false on the network emit path. BattleManagerBase.IsVirtualBattle is + // `=> IsForecast` (BattleManagerBase.cs:657), and NetworkStandardBattleMgr.SendPlayCard is gated + // on `!IsVirtualBattle` (NetworkStandardBattleMgr.cs:155) — under IsForecast=true the play + // resolves state but the emit is suppressed. EnsureInitialized leaves IsForecast=true (correct + // for the direct-ActionProcessor solo oracles, where it suppresses VFX); clear it here so the + // genuine emit fires. The cost is that VFX registration is no longer short-circuited, so the + // play exercises the real view layer — those view touches are satisfied by the no-op view shims + // (InitCardTemplates, the HandView/DetailPanel fills below). M3's damage is literal, immune to + // any play-count bump the OperateMgr path adds vs the direct path. + BattleManagerBase.IsForecast = false; var player = mgr.BattlePlayer; var enemy = mgr.BattleEnemy; SetField(player, "_opponentBattlePlayer", enemy); @@ -364,6 +390,12 @@ namespace SVSim.BattleEngine.Tests InitLeaderLife(mgr); // a 0-life leader reads as game-over and blocks plays InitCardTemplates(mgr); // play/draw VFX touches the card view layer + // The OperateMgr emit path runs SetupActionProcessorEvent (skipped by the direct-ActionProcessor + // solo oracles), which subscribes BattleMgr.DetailMgr.DetailPanelControl.UpdateCardDescriptionOnEvent + // to OnPlayComplete (BattlePlayerBase.cs:1431). DetailMgr is created in CreateManager but its + // DetailPanelControl (a UI control) is null headless. Seed the engine's own NullDetailPanelControl + // no-op so the play-complete event resolves without touching the UI. + mgr.DetailMgr.DetailPanelControl = new NullDetailPanelControl(); // Inject a headless RealTimeNetworkAgent so NetworkBattleSender's ToolboxGame.RealTimeNetworkAgent // .* calls resolve, and subscribe OnEmit. GetUninitializedObject skips the MonoBehaviour Awake. @@ -376,6 +408,25 @@ namespace SVSim.BattleEngine.Tests // public setter runs cleanly headless. Prepared (50) is the real enum member (RealTimeNetworkAgent.cs:35). agent.SetCurrentMatchingStatus(RealTimeNetworkAgent.MatchingStatus.Prepared); + // EmitMsgPack -> AddActionSequence (RealTimeNetworkAgent.cs:1773, fired for the PlayActions URI) + // does `_gungnir._actionSequenceNum++` and `NetworkLogger.LogInfo(...)`. On the + // GetUninitializedObject agent both are null (the real ctor builds them at :289/:301). Seed an + // uninitialized Gungnir (its ctor news a ConnectionReporter + Ticks — unneeded; AddActionSequence + // only touches the int counter) and the engine's own NetworkNullLogger no-op so the action-seq + // bookkeeping runs without crashing. Neither drives game state. + SetField(agent, "_gungnir", + System.Runtime.Serialization.FormatterServices.GetUninitializedObject(typeof(Gungnir))); + SetProperty(agent, "NetworkLogger", new NetworkNullLogger()); + + // Suppress the actual socket transmission. After OnEmit fires (RealTimeNetworkAgent.cs:1270, the + // O1 liveness signal), EmitMsgPack -> EmitMsgUriPack reaches the stockEmitMessageMgr / _manager.Socket + // network I/O (RealTimeNetworkAgent.cs:1444+/1487) — none of which exists headless. The engine's + // OWN _notEmit flag (set in recovery/replay) short-circuits EmitMsgUriPack at :1438 BEFORE any of + // that, so the emit stays genuine (OnEmit already fired through the real send path) while the + // byte-push is skipped. This is the only honest way to terminate the path headless: we are NOT + // faking OnEmit, only declining to open a socket we cannot open. + SetField(agent, "_notEmit", true); + var emitted = new System.Collections.Generic.List(); agent.OnEmit += uri => emitted.Add(uri); Wizard.ToolboxGame.SetRealTimeNetworkBattle(agent); @@ -393,6 +444,25 @@ namespace SVSim.BattleEngine.Tests $"{obj.GetType().Name} has no field '{name}'"); f.SetValue(obj, value); } + + // Set a property whose setter is non-public (e.g. RealTimeNetworkAgent.NetworkLogger has a + // protected setter). Walks the type hierarchy because the declaring type may be a base class. + private static void SetProperty(object obj, string name, object value) + { + var t = obj.GetType(); + System.Reflection.PropertyInfo p = null; + while (t != null && p == null) + { + p = t.GetProperty(name, + System.Reflection.BindingFlags.Instance | + System.Reflection.BindingFlags.NonPublic | + System.Reflection.BindingFlags.Public); + t = t.BaseType; + } + if (p == null) throw new System.InvalidOperationException( + $"{obj.GetType().Name} has no property '{name}'"); + p.SetValue(obj, value); + } } // Test-side replica of the engine's own StandardBattleMgrContentsCreator (the practice/solo diff --git a/SVSim.BattleEngine/Rng/HeadlessNetworkBattleMgr.cs b/SVSim.BattleEngine/Rng/HeadlessNetworkBattleMgr.cs index a85d859..c8e89a2 100644 --- a/SVSim.BattleEngine/Rng/HeadlessNetworkBattleMgr.cs +++ b/SVSim.BattleEngine/Rng/HeadlessNetworkBattleMgr.cs @@ -1,17 +1,19 @@ -using Wizard.BattleMgr; - namespace SVSim.BattleEngine.Rng { // The headless authoritative NETWORK battle mgr — the emitting twin of HeadlessBattleMgr. Emission - // lives on NetworkBattleManagerBase (NetworkBattleSender's ctor demands one), so the M13 emit read - // needs this subclass; HeadlessBattleMgr : SingleBattleMgr cannot reach the sender. RNG overrides are - // identical to HeadlessBattleMgr (the same BattleManagerBase virtuals + RandomSourceBridge), so the - // M14 rand-list emit reuses this mgr unchanged. M13's deterministic card never exercises a roll. - public sealed class HeadlessNetworkBattleMgr : NetworkBattleManagerBase + // lives on the NetworkBattleSender, and the *override that actually invokes it* (SendPlayCard -> + // NetworkSender.SendPlayCard) lives on NetworkStandardBattleMgr, NOT NetworkBattleManagerBase: the + // base SendPlayCard is an empty virtual no-op (NetworkBattleManagerBase.cs:598). NetworkStandardBattleMgr + // is the production standard-PvP mgr; its ctor also creates the NetworkSender (NetworkStandardBattleMgr.cs:92), + // so no manual sender wiring is needed here. Extending the base instead would resolve state but never + // emit (spec §7 risk 2a). RNG overrides are identical to HeadlessBattleMgr (the same BattleManagerBase + // virtuals + RandomSourceBridge), so the M14 rand-list emit reuses this mgr unchanged. M13's + // deterministic card never exercises a roll. + public sealed class HeadlessNetworkBattleMgr : NetworkStandardBattleMgr { private readonly IRandomSource _rng; - public HeadlessNetworkBattleMgr(IBattleMgrContentsCreator contentsCreator, IRandomSource rng = null) + public HeadlessNetworkBattleMgr(Wizard.BattleMgr.IBattleMgrContentsCreator contentsCreator, IRandomSource rng = null) : base(contentsCreator) { _rng = rng ?? new SeededRandomSource(contentsCreator.RandomSeed); diff --git a/SVSim.BattleEngine/Shim/Generated/_IfaceImpl.g.cs b/SVSim.BattleEngine/Shim/Generated/_IfaceImpl.g.cs index 34a9b4a..53e5765 100644 --- a/SVSim.BattleEngine/Shim/Generated/_IfaceImpl.g.cs +++ b/SVSim.BattleEngine/Shim/Generated/_IfaceImpl.g.cs @@ -212,7 +212,10 @@ namespace Wizard.Battle.View { ITurnEndButtonUI global::Wizard.Battle.View.IBattlePlayerView.TurnEndButtonUI { get => default!; } GameObject global::Wizard.Battle.View.IBattlePlayerView.EpIcon { get => default!; } bool global::Wizard.Battle.View.IBattlePlayerView.IsSelecting { get => default!; } - HandViewBase global::Wizard.Battle.View.IBattlePlayerView.HandView { get => default!; } + // HEADLESS FILL (M13): generator emitted `default!` (null); the OperateMgr emit path calls + // BattleView.HandView.RemoveCardFromView (BattlePlayerBase.cs:1422). Redirect to a shared no-op + // HandViewBase so the presentation call is a safe no-op (the played card is never in its list). + HandViewBase global::Wizard.Battle.View.IBattlePlayerView.HandView { get => global::Wizard.Battle.View.HeadlessHandViewStub.Instance; } HandControl global::Wizard.Battle.View.IBattlePlayerView.HandControl { get => default!; } BattleCardBase global::Wizard.Battle.View.IBattlePlayerView.SelectSkillActCard { get => default!; } GameObject global::Wizard.Battle.View.IBattlePlayerView.TurnEndBtn { get => default!; } @@ -312,7 +315,10 @@ namespace Wizard.Battle.View { ITurnEndButtonUI global::Wizard.Battle.View.IBattlePlayerView.TurnEndButtonUI { get => default!; } GameObject global::Wizard.Battle.View.IBattlePlayerView.EpIcon { get => default!; } bool global::Wizard.Battle.View.IBattlePlayerView.IsSelecting { get => default!; } - HandViewBase global::Wizard.Battle.View.IBattlePlayerView.HandView { get => default!; } + // HEADLESS FILL (M13): generator emitted `default!` (null); the OperateMgr emit path calls + // BattleView.HandView.RemoveCardFromView (BattlePlayerBase.cs:1422). Redirect to a shared no-op + // HandViewBase so the presentation call is a safe no-op (the played card is never in its list). + HandViewBase global::Wizard.Battle.View.IBattlePlayerView.HandView { get => global::Wizard.Battle.View.HeadlessHandViewStub.Instance; } HandControl global::Wizard.Battle.View.IBattlePlayerView.HandControl { get => default!; } BattleCardBase global::Wizard.Battle.View.IBattlePlayerView.SelectSkillActCard { get => default!; } GameObject global::Wizard.Battle.View.IBattlePlayerView.TurnEndBtn { get => default!; } diff --git a/SVSim.BattleEngine/Shim/View/HeadlessHandViewStub.cs b/SVSim.BattleEngine/Shim/View/HeadlessHandViewStub.cs new file mode 100644 index 0000000..66b177e --- /dev/null +++ b/SVSim.BattleEngine/Shim/View/HeadlessHandViewStub.cs @@ -0,0 +1,28 @@ +// AUTHORED SHIM (not copied). A non-null no-op HandViewBase the headless emit path needs. +// +// On the OperateMgr/network play path, BattlePlayerBase.SetupActionProcessorEvent subscribes an +// OnBeforePlayCard handler that calls BattleView.HandView.RemoveCardFromView (BattlePlayerBase.cs:1422) — +// a pure presentation-layer hand-card removal. The m1_stub_gen generated IBattlePlayerView.HandView getter +// returns default! (null), so the call NREs. The direct-ActionProcessor solo oracles never hit this +// (SetupActionProcessorEvent is OperateMgr-only). Seed a single shared no-op HandViewBase whose card-view +// list is non-null so RemoveCardFromView is a safe no-op (the played card is never in this view's list, so +// the abstract RearrangeHand is never reached). Nothing here touches game state. + +using UnityEngine; + +namespace Wizard.Battle.View +{ + public sealed class HeadlessHandViewStub : HandViewBase + { + // Shared instance the generated IBattlePlayerView.HandView getters return headless. + public static readonly HeadlessHandViewStub Instance = new HeadlessHandViewStub(); + + // Base param ctor initializes the protected _battleCardViewList (the default ctor leaves it null, + // which RemoveCardFromView would NRE on). CreateHandControl is overridden to a null no-op below. + public HeadlessHandViewStub() : base(null, null) { } + + protected override void RearrangeHand(float rearrangeTime, bool isNewReplayMoveTurn = false) { } + + protected override HandControl CreateHandControl(GameObject handGameObject, BattleCamera battleCamera) => null; + } +}