using System.Security.Cryptography;
using SVSim.BattleNode.Sessions;
namespace SVSim.BattleNode.Bridge;
///
/// In-process implementation of . The HTTP-side
/// matching queue calls once it has decided "these two
/// play each other" or "this viewer is solo (bot)."
///
public sealed class MatchingBridge : IMatchingBridge
{
/// Battle id is two zero-padded decimal halves concatenated (e.g. "975695" + "075012").
/// The half-width and the draw bound must stay coupled: bound == 10^digits.
private const int BattleIdHalfDigits = 6;
private const int BattleIdHalfExclusiveMax = 1_000_000; // 10^BattleIdHalfDigits
private readonly IBattleSessionStore _store;
private readonly BattleNodeOptions _options;
public MatchingBridge(IBattleSessionStore store, BattleNodeOptions options)
{
_store = store;
_options = options;
}
public PendingMatch RegisterBattle(BattlePlayer p1, BattlePlayer? p2, BattleType type)
{
ValidateContract(p1, p2, type);
// Decimal battle id mirrors the captures (e.g. "975695075012"): two unbiased
// BattleIdHalfDigits-wide draws concatenated. RandomNumberGenerator.GetInt32 uses
// rejection sampling so each half is uniform on [0, BattleIdHalfExclusiveMax).
var hi = RandomNumberGenerator.GetInt32(0, BattleIdHalfExclusiveMax);
var lo = RandomNumberGenerator.GetInt32(0, BattleIdHalfExclusiveMax);
var halfFormat = "D" + BattleIdHalfDigits;
var battleId = hi.ToString(halfFormat) + lo.ToString(halfFormat);
_store.RegisterPending(new PendingBattle(battleId, type, p1, p2));
return new PendingMatch(battleId, _options.NodeServerUrl);
}
private static void ValidateContract(BattlePlayer p1, BattlePlayer? p2, BattleType type)
{
if (p1 is null) throw new ArgumentNullException(nameof(p1));
switch (type)
{
case BattleType.Pvp:
if (p2 is null) throw new ArgumentException("Pvp requires both p1 and p2.", nameof(p2));
if (p1.ViewerId == p2.ViewerId)
throw new ArgumentException("Pvp requires distinct viewer ids.", nameof(p2));
break;
case BattleType.Bot:
if (p2 is not null) throw new ArgumentException("Bot must have p2==null.", nameof(p2));
break;
default:
throw new ArgumentOutOfRangeException(nameof(type), type, "Unknown BattleType.");
}
}
}