refactor(battle-node): generalise BuildMatched/BuildBattleStart for PvP

Both helpers now take the opponent's MatchContext + an explicit seed
instead of pulling ScriptedProfiles.OpponentMatchedProfile / OpponentBattleStartProfile
internally. ScriptedBotParticipant.Context fixture absorbs the cosmetic
fields previously hardcoded in ScriptedProfiles so Scripted's wire bytes
stay identical - verified by integration tests still green.

Phase 2 prep: PvP arms will call the same helpers with the real opponent
participant's Context.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
gamer147
2026-06-01 21:16:11 -04:00
parent 2d7cee38d3
commit 560feb231a
5 changed files with 91 additions and 37 deletions

View File

@@ -22,35 +22,58 @@ public static class ScriptedLifecycle
/// </summary> /// </summary>
public const long FakeOpponentViewerId = 999_999_999L; public const long FakeOpponentViewerId = 999_999_999L;
public static MsgEnvelope BuildMatched(MatchContext ctx, long playerViewerId, long opponentViewerId, string battleId) => public static MsgEnvelope BuildMatched(
MatchContext selfCtx, MatchContext oppoCtx,
long selfViewerId, long oppoViewerId,
string battleId, long seed) =>
EnvelopeForPush(NetworkBattleUri.Matched, EnvelopeForPush(NetworkBattleUri.Matched,
new MatchedBody( new MatchedBody(
SelfInfo: new MatchedSelfInfo( SelfInfo: new MatchedSelfInfo(
CountryCode: ctx.CountryCode, CountryCode: selfCtx.CountryCode,
UserName: ctx.UserName, UserName: selfCtx.UserName,
SleeveId: ctx.SleeveId, SleeveId: selfCtx.SleeveId,
EmblemId: ctx.EmblemId, EmblemId: selfCtx.EmblemId,
DegreeId: ctx.DegreeId, DegreeId: selfCtx.DegreeId,
FieldId: ctx.FieldId, FieldId: selfCtx.FieldId,
IsOfficial: ctx.IsOfficial, IsOfficial: selfCtx.IsOfficial,
OppoId: opponentViewerId, OppoId: oppoViewerId,
Seed: ScriptedProfiles.BattleSeed), Seed: seed),
OppoInfo: ScriptedProfiles.OpponentMatchedProfile with { OppoId = playerViewerId }, OppoInfo: new MatchedOppoInfo(
SelfDeck: BuildPlayerDeck(ctx.SelfDeckCardIds)), CountryCode: oppoCtx.CountryCode,
UserName: oppoCtx.UserName,
SleeveId: oppoCtx.SleeveId,
EmblemId: oppoCtx.EmblemId,
DegreeId: oppoCtx.DegreeId,
FieldId: oppoCtx.FieldId,
IsOfficial: oppoCtx.IsOfficial,
OppoId: selfViewerId,
Seed: seed,
OppoDeckCount: oppoCtx.SelfDeckCardIds.Count),
SelfDeck: BuildPlayerDeck(selfCtx.SelfDeckCardIds)),
bid: battleId); bid: battleId);
public static MsgEnvelope BuildBattleStart(MatchContext ctx, long playerViewerId) => public static MsgEnvelope BuildBattleStart(
MatchContext selfCtx, MatchContext oppoCtx, long selfViewerId) =>
EnvelopeForPush(NetworkBattleUri.BattleStart, EnvelopeForPush(NetworkBattleUri.BattleStart,
new BattleStartBody( new BattleStartBody(
TurnState: 0, // player goes first TurnState: 0, // player goes first
BattleType: ctx.BattleType, BattleType: selfCtx.BattleType,
SelfInfo: new BattleStartSelfInfo( SelfInfo: new BattleStartSelfInfo(
Rank: ScriptedProfiles.PlayerRank, Rank: ScriptedProfiles.PlayerRank,
BattlePoint: ScriptedProfiles.PlayerBattlePoint, BattlePoint: ScriptedProfiles.PlayerBattlePoint,
ClassId: ctx.ClassId, ClassId: selfCtx.ClassId,
CharaId: ctx.CharaId, CharaId: selfCtx.CharaId,
CardMasterName: ctx.CardMasterName), CardMasterName: selfCtx.CardMasterName),
OppoInfo: ScriptedProfiles.OpponentBattleStartProfile)); OppoInfo: new BattleStartOppoInfo(
// Rank/IsMasterRank/BattlePoint/MasterPoint stay hardcoded —
// PvP rank tracking is deferred (per spec § Out of scope).
Rank: "1",
IsMasterRank: "0",
BattlePoint: 0,
MasterPoint: "0",
ClassId: oppoCtx.ClassId,
CharaId: oppoCtx.CharaId,
CardMasterName: oppoCtx.CardMasterName)));
public static MsgEnvelope BuildDeal() => public static MsgEnvelope BuildDeal() =>
EnvelopeForPush(NetworkBattleUri.Deal, EnvelopeForPush(NetworkBattleUri.Deal,

View File

@@ -98,15 +98,19 @@ public sealed class BattleSession
case NetworkBattleUri.InitBattle when Phase == BattleSessionPhase.AwaitingInitBattle: case NetworkBattleUri.InitBattle when Phase == BattleSessionPhase.AwaitingInitBattle:
// Phase 1: push Matched only to the "real" participant. The session reads // Phase 1: push Matched only to the "real" participant. The session reads
// selfInfo from from.Context; opponent half currently comes from // selfInfo from from.Context and oppoInfo from other.Context (the scripted
// ScriptedProfiles inside ScriptedLifecycle.BuildMatched (Phase 2 generalises // bot's Context fixture preserves the prod-captured cosmetics that previously
// to use other.Context for per-perspective Matched). // lived in ScriptedProfiles).
result.Add((from, ScriptedLifecycle.BuildMatched(from.Context, from.ViewerId, other.ViewerId, BattleId), false)); result.Add((from, ScriptedLifecycle.BuildMatched(
from.Context, other.Context,
from.ViewerId, other.ViewerId,
BattleId, ScriptedProfiles.BattleSeed), false));
Phase = BattleSessionPhase.AwaitingLoaded; Phase = BattleSessionPhase.AwaitingLoaded;
break; break;
case NetworkBattleUri.Loaded when Phase == BattleSessionPhase.AwaitingLoaded: case NetworkBattleUri.Loaded when Phase == BattleSessionPhase.AwaitingLoaded:
result.Add((from, ScriptedLifecycle.BuildBattleStart(from.Context, from.ViewerId), false)); result.Add((from, ScriptedLifecycle.BuildBattleStart(
from.Context, other.Context, from.ViewerId), false));
result.Add((from, ScriptedLifecycle.BuildDeal(), false)); result.Add((from, ScriptedLifecycle.BuildDeal(), false));
Phase = BattleSessionPhase.AwaitingSwap; Phase = BattleSessionPhase.AwaitingSwap;
break; break;

View File

@@ -1,3 +1,4 @@
using System.Linq;
using SVSim.BattleNode.Bridge; using SVSim.BattleNode.Bridge;
using SVSim.BattleNode.Lifecycle; using SVSim.BattleNode.Lifecycle;
using SVSim.BattleNode.Protocol; using SVSim.BattleNode.Protocol;
@@ -22,8 +23,12 @@ public sealed class ScriptedBotParticipant : IBattleParticipant
{ {
public long ViewerId => ScriptedLifecycle.FakeOpponentViewerId; public long ViewerId => ScriptedLifecycle.FakeOpponentViewerId;
public MatchContext Context { get; } = new( public MatchContext Context { get; } = new(
SelfDeckCardIds: Array.Empty<long>(), // 30 dummy card ids so oppoCtx.SelfDeckCardIds.Count == 30 (matches the
ClassId: "0", CharaId: "0", CardMasterName: "card_master_node_10015", // hardcoded OppoDeckCount that ScriptedProfiles.OpponentMatchedProfile shipped).
SelfDeckCardIds: Enumerable.Range(1, 30).Select(_ => 0L).ToList(),
// BattleStart opponent half: ClassId/CharaId from ScriptedProfiles.OpponentBattleStartProfile.
ClassId: "8", CharaId: "8", CardMasterName: "card_master_node_10015",
// Matched opponent half: cosmetic fields from ScriptedProfiles.OpponentMatchedProfile.
CountryCode: "JPN", UserName: "Opponent", SleeveId: "704141010", CountryCode: "JPN", UserName: "Opponent", SleeveId: "704141010",
EmblemId: "400001100", DegreeId: "120027", FieldId: 5, IsOfficial: 0, EmblemId: "400001100", DegreeId: "120027", FieldId: 5, IsOfficial: 0,
BattleType: 0); BattleType: 0);

View File

@@ -12,8 +12,9 @@ public class ScriptedLifecycleTests
[Test] [Test]
public void BuildMatched_PutsOppoIdInSelfInfoEqualToTheRealOpponentVid() public void BuildMatched_PutsOppoIdInSelfInfoEqualToTheRealOpponentVid()
{ {
var env = ScriptedLifecycle.BuildMatched(FixtureCtx(), var env = ScriptedLifecycle.BuildMatched(FixtureCtx(), ScriptedBotCtx(),
playerViewerId: 906243102, opponentViewerId: 847666884, battleId: "b"); selfViewerId: 906243102, oppoViewerId: 847666884,
battleId: "b", seed: ScriptedProfiles.BattleSeed);
Assert.That(env.Uri, Is.EqualTo(NetworkBattleUri.Matched)); Assert.That(env.Uri, Is.EqualTo(NetworkBattleUri.Matched));
var body = (MatchedBody)env.Body; var body = (MatchedBody)env.Body;
@@ -25,7 +26,7 @@ public class ScriptedLifecycleTests
[Test] [Test]
public void BuildMatched_ContainsThirtyCardSelfDeck() public void BuildMatched_ContainsThirtyCardSelfDeck()
{ {
var env = ScriptedLifecycle.BuildMatched(FixtureCtx(), 1, 2, "b"); var env = ScriptedLifecycle.BuildMatched(FixtureCtx(), ScriptedBotCtx(), 1, 2, "b", ScriptedProfiles.BattleSeed);
var body = (MatchedBody)env.Body; var body = (MatchedBody)env.Body;
Assert.That(body.SelfDeck.Count, Is.EqualTo(30)); Assert.That(body.SelfDeck.Count, Is.EqualTo(30));
} }
@@ -34,7 +35,7 @@ public class ScriptedLifecycleTests
public void BuildMatched_deck_idxs_pair_1to30_with_context_card_ids() public void BuildMatched_deck_idxs_pair_1to30_with_context_card_ids()
{ {
var draftedDeck = Enumerable.Range(1, 30).Select(i => 200_000_000L + i).ToList(); var draftedDeck = Enumerable.Range(1, 30).Select(i => 200_000_000L + i).ToList();
var env = ScriptedLifecycle.BuildMatched(FixtureCtx(draftedDeck), 1, 2, "b"); var env = ScriptedLifecycle.BuildMatched(FixtureCtx(draftedDeck), ScriptedBotCtx(), 1, 2, "b", ScriptedProfiles.BattleSeed);
var body = (MatchedBody)env.Body; var body = (MatchedBody)env.Body;
for (int i = 0; i < 30; i++) for (int i = 0; i < 30; i++)
@@ -55,7 +56,7 @@ public class ScriptedLifecycleTests
EmblemId = "888", DegreeId = "777", FieldId = 42, IsOfficial = 1, EmblemId = "888", DegreeId = "777", FieldId = 42, IsOfficial = 1,
}; };
var env = ScriptedLifecycle.BuildMatched(ctx, 1, 2, "b"); var env = ScriptedLifecycle.BuildMatched(ctx, ScriptedBotCtx(), 1, 2, "b", ScriptedProfiles.BattleSeed);
var body = (MatchedBody)env.Body; var body = (MatchedBody)env.Body;
Assert.That(body.SelfInfo.CountryCode, Is.EqualTo("JPN")); Assert.That(body.SelfInfo.CountryCode, Is.EqualTo("JPN"));
@@ -70,7 +71,7 @@ public class ScriptedLifecycleTests
[Test] [Test]
public void BuildBattleStart_HasTurnStateZero_AndUsesContextBattleType() public void BuildBattleStart_HasTurnStateZero_AndUsesContextBattleType()
{ {
var env = ScriptedLifecycle.BuildBattleStart(FixtureCtx(), playerViewerId: 1); var env = ScriptedLifecycle.BuildBattleStart(FixtureCtx(), ScriptedBotCtx(), selfViewerId: 1);
var body = (BattleStartBody)env.Body; var body = (BattleStartBody)env.Body;
Assert.That(body.TurnState, Is.EqualTo(0)); Assert.That(body.TurnState, Is.EqualTo(0));
Assert.That(body.BattleType, Is.EqualTo(11)); Assert.That(body.BattleType, Is.EqualTo(11));
@@ -86,7 +87,7 @@ public class ScriptedLifecycleTests
BattleType = 42, BattleType = 42,
}; };
var env = ScriptedLifecycle.BuildBattleStart(ctx, playerViewerId: 1); var env = ScriptedLifecycle.BuildBattleStart(ctx, ScriptedBotCtx(), selfViewerId: 1);
var body = (BattleStartBody)env.Body; var body = (BattleStartBody)env.Body;
Assert.That(body.SelfInfo.ClassId, Is.EqualTo("7")); Assert.That(body.SelfInfo.ClassId, Is.EqualTo("7"));
@@ -185,4 +186,13 @@ public class ScriptedLifecycleTests
CountryCode: "KOR", UserName: "Player", SleeveId: "3000011", CountryCode: "KOR", UserName: "Player", SleeveId: "3000011",
EmblemId: "701441011", DegreeId: "300003", FieldId: 43, IsOfficial: 0, EmblemId: "701441011", DegreeId: "300003", FieldId: 43, IsOfficial: 0,
BattleType: 11); BattleType: 11);
// Mirrors ScriptedBotParticipant.Context — the scripted opponent's MatchContext fixture
// that the new BuildMatched/BuildBattleStart helpers read from for the oppo half.
private static MatchContext ScriptedBotCtx() => new(
SelfDeckCardIds: Enumerable.Range(1, 30).Select(_ => 0L).ToList(),
ClassId: "8", CharaId: "8", CardMasterName: "card_master_node_10015",
CountryCode: "JPN", UserName: "Opponent", SleeveId: "704141010",
EmblemId: "400001100", DegreeId: "120027", FieldId: 5, IsOfficial: 0,
BattleType: 0);
} }

View File

@@ -27,8 +27,8 @@ public class TypedBodyWireShapeTests
// Matching.StartBattleLoad reads it back, and GetSelfDeck().Select(...) crashes // Matching.StartBattleLoad reads it back, and GetSelfDeck().Select(...) crashes
// with "Value cannot be null. Parameter name: source". The prod wire format // with "Value cannot be null. Parameter name: source". The prod wire format
// emits envelope keys (uri first) before body keys; we must too. // emits envelope keys (uri first) before body keys; we must too.
var env = ScriptedLifecycle.BuildMatched(FixtureCtx(), var env = ScriptedLifecycle.BuildMatched(FixtureCtx(), ScriptedBotCtx(),
playerViewerId: 1, opponentViewerId: 2, battleId: "b"); selfViewerId: 1, oppoViewerId: 2, battleId: "b", seed: ScriptedProfiles.BattleSeed);
var json = MsgEnvelope.ToJson(env); var json = MsgEnvelope.ToJson(env);
var uriIdx = json.IndexOf("\"uri\":", StringComparison.Ordinal); var uriIdx = json.IndexOf("\"uri\":", StringComparison.Ordinal);
@@ -45,8 +45,9 @@ public class TypedBodyWireShapeTests
[Test] [Test]
public void BuildMatched_SerializesAllWireKeysExpectedByTheClient() public void BuildMatched_SerializesAllWireKeysExpectedByTheClient()
{ {
var env = ScriptedLifecycle.BuildMatched(FixtureCtx(), var env = ScriptedLifecycle.BuildMatched(FixtureCtx(), ScriptedBotCtx(),
playerViewerId: 906243102, opponentViewerId: 847666884, battleId: "597830888107"); selfViewerId: 906243102, oppoViewerId: 847666884, battleId: "597830888107",
seed: ScriptedProfiles.BattleSeed);
var json = MsgEnvelope.ToJson(env); var json = MsgEnvelope.ToJson(env);
var node = JsonNode.Parse(json)!.AsObject(); var node = JsonNode.Parse(json)!.AsObject();
@@ -85,7 +86,7 @@ public class TypedBodyWireShapeTests
[Test] [Test]
public void BuildBattleStart_SerializesAllWireKeysAndPreservesBattlePointAsymmetry() public void BuildBattleStart_SerializesAllWireKeysAndPreservesBattlePointAsymmetry()
{ {
var env = ScriptedLifecycle.BuildBattleStart(FixtureCtx(), playerViewerId: 906243102); var env = ScriptedLifecycle.BuildBattleStart(FixtureCtx(), ScriptedBotCtx(), selfViewerId: 906243102);
var json = MsgEnvelope.ToJson(env); var json = MsgEnvelope.ToJson(env);
var node = JsonNode.Parse(json)!.AsObject(); var node = JsonNode.Parse(json)!.AsObject();
@@ -193,4 +194,15 @@ public class TypedBodyWireShapeTests
CountryCode: "KOR", UserName: "Player", SleeveId: "3000011", CountryCode: "KOR", UserName: "Player", SleeveId: "3000011",
EmblemId: "701441011", DegreeId: "300003", FieldId: 43, IsOfficial: 0, EmblemId: "701441011", DegreeId: "300003", FieldId: 43, IsOfficial: 0,
BattleType: 11); BattleType: 11);
// Mirrors ScriptedBotParticipant.Context — 30-card deck and the prod-captured opponent
// cosmetics (ClassId/CharaId "8") so the wire bytes asserted below (oppoInfo classId/charaId,
// oppoDeckCount=30, etc.) remain byte-identical after the BuildMatched/BuildBattleStart
// signature change.
private static MatchContext ScriptedBotCtx() => new(
SelfDeckCardIds: Enumerable.Range(1, 30).Select(_ => 0L).ToList(),
ClassId: "8", CharaId: "8", CardMasterName: "card_master_node_10015",
CountryCode: "JPN", UserName: "Opponent", SleeveId: "704141010",
EmblemId: "400001100", DegreeId: "120027", FieldId: 5, IsOfficial: 0,
BattleType: 0);
} }