refactor(battle-node): WireConstants for SIO event names + crypto RNG battle id

This commit is contained in:
gamer147
2026-06-01 11:53:01 -04:00
parent 133346e3e8
commit ef3d7bb82b
4 changed files with 45 additions and 12 deletions

View File

@@ -1,3 +1,4 @@
using System.Security.Cryptography;
using SVSim.BattleNode.Sessions;
namespace SVSim.BattleNode.Bridge;
@@ -23,8 +24,13 @@ public sealed class MatchingBridge : IMatchingBridge
public PendingMatch RegisterPendingBattle(long viewerId)
{
// 12-digit decimal battle id mirrors the captures (e.g. "975695075012").
// Cast to long before Math.Abs to avoid OverflowException on int.MinValue.
var battleId = (Math.Abs((long)Guid.NewGuid().GetHashCode()) % 1_000_000_000_000L).ToString("D12");
// Two unbiased 6-digit draws concatenated — RandomNumberGenerator.GetInt32 uses
// rejection sampling so the result is uniform on [0, 10^6). The previous
// implementation (Guid.GetHashCode + mod) collapsed 128 bits into ~32 of entropy
// and tripped Math.Abs(int.MinValue) UB on the unlucky hash.
var hi = RandomNumberGenerator.GetInt32(0, 1_000_000);
var lo = RandomNumberGenerator.GetInt32(0, 1_000_000);
var battleId = $"{hi:D6}{lo:D6}";
_store.RegisterPending(new PendingBattle(battleId, viewerId));
return new PendingMatch(battleId, _options.NodeServerUrl);
}