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>
184 lines
8.1 KiB
C#
184 lines
8.1 KiB
C#
using System.Collections.Immutable;
|
|
using SVSim.BattleNode.Bridge;
|
|
using SVSim.BattleNode.Protocol;
|
|
using SVSim.BattleNode.Protocol.Bodies;
|
|
|
|
namespace SVSim.BattleNode.Lifecycle;
|
|
|
|
/// <summary>
|
|
/// v1 hand-rolled scripted opponent. Static frame builders for the five lifecycle uris
|
|
/// (Matched / BattleStart / Deal / Swap response / Ready) plus a trivial opponent TurnStart
|
|
/// the dispatch pushes after the player's TurnEnd. The values are templated from the TK2
|
|
/// captures at <c>data_dumps/captures/battle-traffic_tk2_regular.ndjson</c> — anything
|
|
/// hardcoded here came from a real prod frame, with names + provenance in
|
|
/// <see cref="ScriptedProfiles"/>. The player-half of Matched/BattleStart now reads from
|
|
/// <see cref="MatchContext"/> instead of <see cref="ScriptedProfiles"/>.
|
|
/// </summary>
|
|
public static class ScriptedLifecycle
|
|
{
|
|
/// <summary>
|
|
/// Viewer id we present as the opponent on every scripted push. Out-of-range vs. real
|
|
/// viewer ids so it can't collide with a real account in the auth pipeline.
|
|
/// </summary>
|
|
public const long FakeOpponentViewerId = 999_999_999L;
|
|
|
|
public static MsgEnvelope BuildMatched(
|
|
MatchContext selfCtx, MatchContext oppoCtx,
|
|
long selfViewerId, long oppoViewerId,
|
|
string battleId, long seed) =>
|
|
EnvelopeForPush(NetworkBattleUri.Matched,
|
|
new MatchedBody(
|
|
SelfInfo: new MatchedSelfInfo(
|
|
CountryCode: selfCtx.CountryCode,
|
|
UserName: selfCtx.UserName,
|
|
SleeveId: selfCtx.SleeveId,
|
|
EmblemId: selfCtx.EmblemId,
|
|
DegreeId: selfCtx.DegreeId,
|
|
FieldId: selfCtx.FieldId,
|
|
IsOfficial: selfCtx.IsOfficial,
|
|
OppoId: oppoViewerId,
|
|
Seed: seed),
|
|
OppoInfo: new MatchedOppoInfo(
|
|
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);
|
|
|
|
public static MsgEnvelope BuildBattleStart(
|
|
MatchContext selfCtx, MatchContext oppoCtx, long selfViewerId) =>
|
|
EnvelopeForPush(NetworkBattleUri.BattleStart,
|
|
new BattleStartBody(
|
|
TurnState: 0, // player goes first
|
|
BattleType: selfCtx.BattleType,
|
|
SelfInfo: new BattleStartSelfInfo(
|
|
Rank: ScriptedProfiles.PlayerRank,
|
|
BattlePoint: ScriptedProfiles.PlayerBattlePoint,
|
|
ClassId: selfCtx.ClassId,
|
|
CharaId: selfCtx.CharaId,
|
|
CardMasterName: selfCtx.CardMasterName),
|
|
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() =>
|
|
EnvelopeForPush(NetworkBattleUri.Deal,
|
|
new DealBody(
|
|
Self: new[] { new PosIdx(0, 1), new PosIdx(1, 2), new PosIdx(2, 3) },
|
|
Oppo: new[] { new PosIdx(0, 1), new PosIdx(1, 2), new PosIdx(2, 3) }));
|
|
|
|
/// <summary>
|
|
/// Initial 3-card hand idxs from <see cref="BuildDeal"/>. Each position in this array
|
|
/// is one card; the value is the card's deck idx. <see cref="ImmutableArray{T}"/> enforces
|
|
/// the "read-only constant" contract at the type level — callers cannot mutate it, even
|
|
/// accidentally (the prior <c>long[]</c> allowed in-place modification by anyone with the
|
|
/// field reference).
|
|
/// </summary>
|
|
private static readonly ImmutableArray<long> InitialHand = ImmutableArray.Create<long>(1, 2, 3);
|
|
|
|
/// <summary>
|
|
/// Compute the player's hand after a mulligan. For every idx in <paramref name="swapIndices"/>
|
|
/// that is currently in the hand, replace it with the next unused deck idx (starting at 4,
|
|
/// since 1..3 were dealt). Positions of kept cards are preserved.
|
|
/// </summary>
|
|
public static long[] ComputeHandAfterSwap(IReadOnlyList<long> swapIndices)
|
|
{
|
|
var hand = InitialHand.ToArray();
|
|
var nextDeckIdx = 4L;
|
|
for (var pos = 0; pos < hand.Length; pos++)
|
|
{
|
|
if (swapIndices.Contains(hand[pos]))
|
|
{
|
|
hand[pos] = nextDeckIdx++;
|
|
}
|
|
}
|
|
return hand;
|
|
}
|
|
|
|
public static MsgEnvelope BuildSwapResponse(IReadOnlyList<long> hand) =>
|
|
EnvelopeForPush(NetworkBattleUri.Swap,
|
|
new SwapResponseBody(Self: BuildPosIdxList(hand)));
|
|
|
|
public static MsgEnvelope BuildReady(IReadOnlyList<long> hand) =>
|
|
EnvelopeForPush(NetworkBattleUri.Ready,
|
|
new ReadyBody(
|
|
Self: BuildPosIdxList(hand),
|
|
Oppo: BuildPosIdxList(InitialHand),
|
|
IdxChangeSeed: ScriptedProfiles.ReadyIdxChangeSeed,
|
|
Spin: ScriptedProfiles.ReadySpin));
|
|
|
|
/// <summary>
|
|
/// First half of the v1.1 scripted opponent turn cycle: pushed after the player's
|
|
/// TurnEnd, transitions the client into "Opponent's turn…" state. Paired with
|
|
/// <see cref="BuildOpponentTurnEnd"/>, which immediately follows and hands control
|
|
/// back to the player.
|
|
/// </summary>
|
|
public static MsgEnvelope BuildOpponentTurnStart() =>
|
|
EnvelopeForPush(NetworkBattleUri.TurnStart,
|
|
new OpponentTurnStartBody(Spin: ScriptedProfiles.OpponentTurnStartSpin));
|
|
|
|
/// <summary>
|
|
/// Server-pushed TurnEnd transition that closes the opponent's turn and hands control
|
|
/// back to the player. Paired with <see cref="BuildOpponentTurnStart"/> in the v1.1 loop.
|
|
/// Wire shape from prod capture battle-traffic_tk2_regular.ndjson L18:
|
|
/// <c>{"uri":"TurnEnd","turnState":0,"resultCode":1,"playSeq":N}</c>.
|
|
/// </summary>
|
|
public static MsgEnvelope BuildOpponentTurnEnd() =>
|
|
EnvelopeForPush(NetworkBattleUri.TurnEnd, new TurnEndBody(TurnState: 0));
|
|
|
|
/// <summary>
|
|
/// Server-pushed Judge frame that follows the opponent's TurnEnd and unblocks the
|
|
/// client's <c>JudgeOperation</c> → <c>ControlTurnStartPlayer</c>, transitioning to the
|
|
/// player's next turn. Without this frame the client hangs on "Opponent's turn…" —
|
|
/// see <c>data_dumps/captures/battle-traffic.ndjson</c> line 14 (client emits its own
|
|
/// Judge then waits forever).
|
|
/// </summary>
|
|
public static MsgEnvelope BuildOpponentJudge() =>
|
|
EnvelopeForPush(NetworkBattleUri.Judge, new JudgeBody(Spin: ScriptedProfiles.OpponentJudgeSpin));
|
|
|
|
private static IReadOnlyList<PosIdx> BuildPosIdxList(IReadOnlyList<long> hand)
|
|
{
|
|
var list = new List<PosIdx>(hand.Count);
|
|
for (var pos = 0; pos < hand.Count; pos++)
|
|
{
|
|
list.Add(new PosIdx(Pos: pos, Idx: (int)hand[pos]));
|
|
}
|
|
return list;
|
|
}
|
|
|
|
private static IReadOnlyList<DeckCardRef> BuildPlayerDeck(IReadOnlyList<long> cardIds)
|
|
{
|
|
var deck = new List<DeckCardRef>(cardIds.Count);
|
|
for (var i = 0; i < cardIds.Count; i++)
|
|
{
|
|
deck.Add(new DeckCardRef(Idx: i + 1, CardId: cardIds[i]));
|
|
}
|
|
return deck;
|
|
}
|
|
|
|
private static MsgEnvelope EnvelopeForPush(NetworkBattleUri uri, IMsgBody body, string? bid = null) =>
|
|
new(uri,
|
|
ViewerId: FakeOpponentViewerId,
|
|
Uuid: WireConstants.ServerUuid,
|
|
Bid: bid,
|
|
Try: 0,
|
|
Cat: EmitCategory.Battle,
|
|
PubSeq: null,
|
|
PlaySeq: null,
|
|
Body: body);
|
|
}
|