fix(battle-node): collision-safe battle-id registration + viewer eviction

RegisterPending → TryRegisterPending (TryAdd instead of indexer) so
battle-id collisions return false instead of silently evicting a live
battle. MatchingBridge retries with fresh IDs on collision (max 5).

Before registering, EvictStaleForViewer removes any stale pending
battle the viewer left behind, enforcing the one-pending-per-viewer
invariant that was previously comment-asserted.

Store tests switched to per-test local stores to fix a race under
the assembly-wide ParallelScope.All.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
gamer147
2026-06-04 22:13:20 -04:00
parent c6fb411861
commit 564b1d678f
5 changed files with 60 additions and 27 deletions

View File

@@ -24,20 +24,35 @@ public sealed class MatchingBridge : IMatchingBridge
_options = options;
}
private const int MaxIdRetries = 5;
public PendingMatch RegisterBattle(BattlePlayer p1, BattlePlayer? p2, BattleType type)
{
ValidateContract(p1, p2, type);
EvictStaleForViewer(p1.ViewerId);
if (p2 is not null) EvictStaleForViewer(p2.ViewerId);
// 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);
for (var attempt = 0; attempt < MaxIdRetries; attempt++)
{
var hi = RandomNumberGenerator.GetInt32(0, BattleIdHalfExclusiveMax);
var lo = RandomNumberGenerator.GetInt32(0, BattleIdHalfExclusiveMax);
var battleId = hi.ToString(halfFormat) + lo.ToString(halfFormat);
if (_store.TryRegisterPending(new PendingBattle(battleId, type, p1, p2)))
return new PendingMatch(battleId, _options.NodeServerUrl);
}
throw new InvalidOperationException(
$"Failed to mint a unique battle id after {MaxIdRetries} attempts.");
}
private void EvictStaleForViewer(long viewerId)
{
var stale = _store.TryFindPendingForViewer(viewerId);
if (stale is not null)
_store.RemovePending(stale.BattleId);
}
private static void ValidateContract(BattlePlayer p1, BattlePlayer? p2, BattleType type)