using System.Collections.Immutable;
using SVSim.BattleNode.Bridge;
using SVSim.BattleNode.Protocol;
using SVSim.BattleNode.Protocol.Bodies;
namespace SVSim.BattleNode.Lifecycle;
///
/// 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 data_dumps/captures/battle-traffic_tk2_regular.ndjson — anything
/// hardcoded here came from a real prod frame, with names + provenance in
/// . The player-half of Matched/BattleStart now reads from
/// instead of .
///
public static class ScriptedLifecycle
{
///
/// 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.
///
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, int turnState) =>
EnvelopeForPush(NetworkBattleUri.BattleStart,
new BattleStartBody(
TurnState: turnState, // 0 = this side goes first, 1 = second. Caller decides.
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) }));
///
/// Initial 3-card hand idxs from . Each position in this array
/// is one card; the value is the card's deck idx. enforces
/// the "read-only constant" contract at the type level — callers cannot mutate it, even
/// accidentally (the prior long[] allowed in-place modification by anyone with the
/// field reference).
///
private static readonly ImmutableArray InitialHand = ImmutableArray.Create(1, 2, 3);
///
/// Compute the player's hand after a mulligan. For every idx in
/// 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.
///
public static long[] ComputeHandAfterSwap(IReadOnlyList 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 hand) =>
EnvelopeForPush(NetworkBattleUri.Swap,
new SwapResponseBody(Self: BuildPosIdxList(hand)));
/// Non-interactive opponent (scripted single / AINetwork): oppo is the
/// placeholder (v1 behaviour).
public static MsgEnvelope BuildReady(IReadOnlyList hand) => BuildReady(hand, InitialHand);
/// Both hands known (the mulligan barrier supplies the opponent's
/// post-mulligan hand).
public static MsgEnvelope BuildReady(IReadOnlyList selfHand, IReadOnlyList oppoHand) =>
EnvelopeForPush(NetworkBattleUri.Ready,
new ReadyBody(
Self: BuildPosIdxList(selfHand),
Oppo: BuildPosIdxList(oppoHand),
IdxChangeSeed: ScriptedProfiles.ReadyIdxChangeSeed,
Spin: ScriptedProfiles.ReadySpin));
// --- Client-shaped emissions (legacy scripted-bot scaffolding, pending removal) so the
// session brokers the bot through the same handshake arms as a human. Bodies for the parameterless
// handshake frames are ignored by the session (it reads from.Context / phase); only
// Swap's idxList is consumed (empty = keep the dealt hand).
public static MsgEnvelope BuildClientInitNetwork() => ClientFrame(NetworkBattleUri.InitNetwork, EmitCategory.General);
public static MsgEnvelope BuildClientInitBattle() => ClientFrame(NetworkBattleUri.InitBattle, EmitCategory.General);
public static MsgEnvelope BuildClientLoaded() => ClientFrame(NetworkBattleUri.Loaded, EmitCategory.General);
public static MsgEnvelope BuildClientSwap() =>
new(NetworkBattleUri.Swap,
ViewerId: FakeOpponentViewerId,
Uuid: WireConstants.ServerUuid,
Bid: null,
Try: 0,
Cat: EmitCategory.Battle,
PubSeq: null,
PlaySeq: null,
Body: new RawBody(new Dictionary { ["idxList"] = new List