feat(emulated-entrypoint): InProcessPairUp service for TK2 PvP matching
Tiny per-mode FCFS slot. First poller parks; second pairs and triggers bridge.RegisterBattle(p1, p2, Pvp). Match cached for first poller's next poll (consume-on-read). No MMR, no cross-mode, no timeouts -- the proper queue API is a separate spec; this is the smallest thing that lets TK2 PvP work end-to-end.
This commit is contained in:
20
SVSim.EmulatedEntrypoint/Matching/IMatchingPairUpService.cs
Normal file
20
SVSim.EmulatedEntrypoint/Matching/IMatchingPairUpService.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using SVSim.BattleNode.Bridge;
|
||||
|
||||
namespace SVSim.EmulatedEntrypoint.Matching;
|
||||
|
||||
/// <summary>
|
||||
/// Minimal in-process matching queue stand-in. The proper queue API is a separate
|
||||
/// spec; this is enough to actually pair two viewers polling /do_matching on the
|
||||
/// same mode.
|
||||
/// </summary>
|
||||
public interface IMatchingPairUpService
|
||||
{
|
||||
/// <summary>
|
||||
/// Try to pair the calling viewer with an already-waiting partner.
|
||||
/// Returns the resolved <see cref="PendingMatch"/> when a partner was found
|
||||
/// (either this call paired with a waiter, or a previous pairing's result is
|
||||
/// still cached for this viewer). Returns null if this viewer is the first
|
||||
/// arriver and should be parked (caller returns 3001 RETRY).
|
||||
/// </summary>
|
||||
Task<PendingMatch?> TryPairAsync(string mode, BattlePlayer player, CancellationToken ct);
|
||||
}
|
||||
62
SVSim.EmulatedEntrypoint/Matching/InProcessPairUp.cs
Normal file
62
SVSim.EmulatedEntrypoint/Matching/InProcessPairUp.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using System.Collections.Concurrent;
|
||||
using SVSim.BattleNode.Bridge;
|
||||
using SVSim.BattleNode.Sessions;
|
||||
|
||||
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).
|
||||
/// </summary>
|
||||
public sealed class InProcessPairUp : IMatchingPairUpService
|
||||
{
|
||||
private readonly IMatchingBridge _bridge;
|
||||
private readonly ConcurrentDictionary<string, ModeSlot> _slots = new();
|
||||
|
||||
public InProcessPairUp(IMatchingBridge bridge)
|
||||
{
|
||||
_bridge = bridge;
|
||||
}
|
||||
|
||||
public Task<PendingMatch?> TryPairAsync(string mode, BattlePlayer player, CancellationToken ct)
|
||||
{
|
||||
var slot = _slots.GetOrAdd(mode, _ => new ModeSlot());
|
||||
lock (slot.Lock)
|
||||
{
|
||||
// 1. Already-resolved match cached for this viewer? Consume + return.
|
||||
if (slot.Resolved.TryGetValue(player.ViewerId, out var cached))
|
||||
{
|
||||
slot.Resolved.Remove(player.ViewerId);
|
||||
return Task.FromResult<PendingMatch?>(cached);
|
||||
}
|
||||
|
||||
// 2. Someone already waiting in this slot? Pair with them.
|
||||
if (slot.Waiting is not null)
|
||||
{
|
||||
if (slot.Waiting.ViewerId == player.ViewerId)
|
||||
{
|
||||
// Same viewer polled twice while parked — keep them parked.
|
||||
return Task.FromResult<PendingMatch?>(null);
|
||||
}
|
||||
var p1 = slot.Waiting;
|
||||
var p2 = player;
|
||||
slot.Waiting = 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<PendingMatch?>(match);
|
||||
}
|
||||
|
||||
// 3. Empty slot — park this caller.
|
||||
slot.Waiting = player;
|
||||
return Task.FromResult<PendingMatch?>(null);
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class ModeSlot
|
||||
{
|
||||
public BattlePlayer? Waiting { get; set; }
|
||||
public Dictionary<long, PendingMatch> Resolved { get; } = new();
|
||||
public object Lock { get; } = new();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user