feat(matching): per-mode policy + AI-fallback branch in InProcessPairUp
InProcessPairUp now consults ModePolicyRegistry per call and reads the fallback threshold from MatchingConfig via IServiceScopeFactory (singleton service consuming a scoped IGameConfigService). New behavior for PvpFirstThenAiFallback modes: when the calling viewer IS the slot's waiter and Now - WaitingSince >= threshold, the waiter unparks and the bridge resolves a Bot match. PvpOnly modes (TK2) keep parking forever (modulo a 5-minute stale-waiter eviction backstop). TimeProvider is injected so tests can drive time forward with FakeTimeProvider — 7 new tests cover the four key transitions (stay-parked / pair-pvp / fall-back / stale-evict) plus per-mode isolation. Fixture uses [FixtureLifeCycle(InstancePerTestCase)] because the assembly is Parallelizable(ParallelScope.All). Program.cs registers ModePolicyRegistry with three rows: TK2 PvpOnly, rotation/unlimited rank PvpFirstThenAiFallback. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -1,64 +1,129 @@
|
||||
using System.Collections.Concurrent;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using SVSim.BattleNode.Bridge;
|
||||
using SVSim.BattleNode.Sessions;
|
||||
using SVSim.Database.Models.Config;
|
||||
using SVSim.Database.Services;
|
||||
|
||||
namespace SVSim.EmulatedEntrypoint.Matching;
|
||||
|
||||
/// <summary>
|
||||
/// In-process FCFS pair-up: one waiting slot per mode, plus a per-viewer cache of
|
||||
/// resolved matches for the first arriver's next poll (consume-on-read).
|
||||
/// In-process pair-up service: one slot per mode, FCFS pairing for PvP, plus an
|
||||
/// AI-fallback branch for modes whose <see cref="ModePolicy"/> is
|
||||
/// <see cref="PolicyKind.PvpFirstThenAiFallback"/>. The proper matching-queue API
|
||||
/// is a separate spec; this is the Phase-2 + Phase-3 placeholder.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Singleton (process-wide slot state) consuming a scoped <see cref="IGameConfigService"/>
|
||||
/// via <see cref="IServiceScopeFactory"/>. The config read is cheap — one DB read per
|
||||
/// pair-up call — and avoids caching policy decisions across config edits.
|
||||
/// </remarks>
|
||||
public sealed class InProcessPairUp : IMatchingPairUpService
|
||||
{
|
||||
/// <summary>
|
||||
/// Safety backstop: if a waiter has been parked for more than this and a new
|
||||
/// arriver shows up, treat the slot as empty (the original waiter has
|
||||
/// presumably stopped polling). Well above the AI-fallback threshold so it
|
||||
/// only fires for PvpOnly modes.
|
||||
/// </summary>
|
||||
private static readonly TimeSpan StaleWaiterEvictionAge = TimeSpan.FromMinutes(5);
|
||||
|
||||
private readonly IMatchingBridge _bridge;
|
||||
private readonly ModePolicyRegistry _policies;
|
||||
private readonly IServiceScopeFactory _scopeFactory;
|
||||
private readonly TimeProvider _clock;
|
||||
private readonly ConcurrentDictionary<string, ModeSlot> _slots = new();
|
||||
|
||||
public InProcessPairUp(IMatchingBridge bridge)
|
||||
public InProcessPairUp(
|
||||
IMatchingBridge bridge,
|
||||
ModePolicyRegistry policies,
|
||||
IServiceScopeFactory scopeFactory,
|
||||
TimeProvider clock)
|
||||
{
|
||||
_bridge = bridge;
|
||||
_policies = policies;
|
||||
_scopeFactory = scopeFactory;
|
||||
_clock = clock;
|
||||
}
|
||||
|
||||
public Task<PairUpResult?> TryPairAsync(string mode, BattlePlayer player, CancellationToken ct)
|
||||
{
|
||||
var policy = _policies.For(mode);
|
||||
var threshold = TimeSpan.FromSeconds(GetThresholdSeconds());
|
||||
var slot = _slots.GetOrAdd(mode, _ => new ModeSlot());
|
||||
|
||||
lock (slot.Lock)
|
||||
{
|
||||
// 1. Already-resolved match cached for this viewer? Consume + return.
|
||||
// This caller is the FIRST arriver picking up their cached pair — owner role.
|
||||
// The caller is the FIRST arriver picking up their cached pair — owner role.
|
||||
if (slot.Resolved.TryGetValue(player.ViewerId, out var cached))
|
||||
{
|
||||
slot.Resolved.Remove(player.ViewerId);
|
||||
return Task.FromResult<PairUpResult?>(new PairUpResult(cached, IsOwner: true, IsAiFallback: false));
|
||||
return Task.FromResult<PairUpResult?>(
|
||||
new PairUpResult(cached.Match, IsOwner: true, IsAiFallback: cached.IsAiFallback));
|
||||
}
|
||||
|
||||
// 2. Someone already waiting in this slot? Pair with them.
|
||||
// This caller is the SECOND arriver who triggered the pair — joiner role.
|
||||
if (slot.Waiting is not null)
|
||||
// 2. Stale waiter eviction backstop.
|
||||
if (slot.Waiting is not null && slot.WaitingSince is { } since
|
||||
&& _clock.GetUtcNow() - since > StaleWaiterEvictionAge)
|
||||
{
|
||||
slot.Waiting = null;
|
||||
slot.WaitingSince = null;
|
||||
}
|
||||
|
||||
// 3. Different viewer already waiting? Pair them.
|
||||
if (slot.Waiting is not null && slot.Waiting.ViewerId != player.ViewerId)
|
||||
{
|
||||
if (slot.Waiting.ViewerId == player.ViewerId)
|
||||
{
|
||||
// Same viewer polled twice while parked — keep them parked.
|
||||
return Task.FromResult<PairUpResult?>(null);
|
||||
}
|
||||
var p1 = slot.Waiting;
|
||||
var p2 = player;
|
||||
slot.Waiting = null;
|
||||
slot.WaitingSince = null;
|
||||
var match = _bridge.RegisterBattle(p1, p2, BattleType.Pvp);
|
||||
// Cache the result for the FIRST arriver's next poll (consume-on-read).
|
||||
slot.Resolved[p1.ViewerId] = match;
|
||||
return Task.FromResult<PairUpResult?>(new PairUpResult(match, IsOwner: false, IsAiFallback: false));
|
||||
// Cache for the FIRST arriver's next poll (consume-on-read).
|
||||
slot.Resolved[p1.ViewerId] = (match, IsAiFallback: false);
|
||||
return Task.FromResult<PairUpResult?>(
|
||||
new PairUpResult(match, IsOwner: false, IsAiFallback: false));
|
||||
}
|
||||
|
||||
// 3. Empty slot — park this caller.
|
||||
slot.Waiting = player;
|
||||
// 4. Caller IS the waiter AND policy permits AI fallback AND threshold elapsed?
|
||||
if (slot.Waiting?.ViewerId == player.ViewerId
|
||||
&& policy.Kind == PolicyKind.PvpFirstThenAiFallback
|
||||
&& slot.WaitingSince is { } parkedAt
|
||||
&& _clock.GetUtcNow() - parkedAt >= threshold)
|
||||
{
|
||||
slot.Waiting = null;
|
||||
slot.WaitingSince = null;
|
||||
var match = _bridge.RegisterBattle(player, null, BattleType.Bot);
|
||||
return Task.FromResult<PairUpResult?>(
|
||||
new PairUpResult(match, IsOwner: true, IsAiFallback: true));
|
||||
}
|
||||
|
||||
// 5. Park (first time only — preserve WaitingSince across sub-threshold re-polls).
|
||||
if (slot.Waiting is null)
|
||||
{
|
||||
slot.Waiting = player;
|
||||
slot.WaitingSince = _clock.GetUtcNow();
|
||||
}
|
||||
return Task.FromResult<PairUpResult?>(null);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resolves the current AI-fallback threshold from the scoped
|
||||
/// <see cref="IGameConfigService"/>. Singleton-safe via per-call scope creation.
|
||||
/// </summary>
|
||||
private int GetThresholdSeconds()
|
||||
{
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
var config = scope.ServiceProvider.GetRequiredService<IGameConfigService>();
|
||||
return config.Get<MatchingConfig>().RankBattleAiFallbackThresholdSeconds;
|
||||
}
|
||||
|
||||
private sealed class ModeSlot
|
||||
{
|
||||
public BattlePlayer? Waiting { get; set; }
|
||||
public Dictionary<long, PendingMatch> Resolved { get; } = new();
|
||||
public DateTimeOffset? WaitingSince { get; set; }
|
||||
public Dictionary<long, (PendingMatch Match, bool IsAiFallback)> Resolved { get; } = new();
|
||||
public object Lock { get; } = new();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,9 +128,15 @@ public class Program
|
||||
// in appsettings*.json — see appsettings.Development.json for SoloDefaultsToScripted.
|
||||
builder.Configuration.GetSection("BattleNode").Bind(opt);
|
||||
});
|
||||
// In-process FCFS pair-up for TK2 PvP /do_matching. Singleton: per-mode state is
|
||||
// process-wide. Proper queue API is a separate spec; this is enough to actually
|
||||
// pair two viewers polling the same mode end-to-end.
|
||||
// In-process FCFS pair-up for TK2 PvP /do_matching, plus rank-battle's AI-fallback
|
||||
// branch. Singleton: per-mode state is process-wide. Proper queue API is a separate
|
||||
// spec; this is enough to actually pair two viewers polling the same mode end-to-end.
|
||||
builder.Services.AddSingleton(new ModePolicyRegistry(new[]
|
||||
{
|
||||
new ModePolicy("arena_two_pick_battle", PolicyKind.PvpOnly),
|
||||
new ModePolicy("rotation_rank_battle", PolicyKind.PvpFirstThenAiFallback),
|
||||
new ModePolicy("unlimited_rank_battle", PolicyKind.PvpFirstThenAiFallback),
|
||||
}));
|
||||
builder.Services.AddSingleton<IMatchingPairUpService, InProcessPairUp>();
|
||||
|
||||
builder.Services.AddTransient<ShadowverseTranslationMiddleware>();
|
||||
|
||||
Reference in New Issue
Block a user