feat(replay): add BattleContextStore for start->finish handoff
Bridges the start-time -> finish-time gap. /finish carries neither battle_id nor opponent identity; this store holds both for the finish handler to compose a ViewerBattleHistory row. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
26
SVSim.Database/Services/Replay/BattleContext.cs
Normal file
26
SVSim.Database/Services/Replay/BattleContext.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
namespace SVSim.Database.Services.Replay;
|
||||
|
||||
/// <summary>
|
||||
/// Per-viewer battle context captured at start time (do_matching/start) and consumed
|
||||
/// at finish time. Lives in <see cref="IBattleContextStore"/> for the duration of a
|
||||
/// single battle. See docs/superpowers/specs/2026-06-10-replay-info-design.md.
|
||||
/// </summary>
|
||||
public sealed record BattleContext(
|
||||
long BattleId,
|
||||
int BattleType,
|
||||
int DeckFormat,
|
||||
int TwoPickType,
|
||||
int SelfClassId,
|
||||
int SelfSubClassId,
|
||||
int SelfCharaId,
|
||||
string SelfRotationId,
|
||||
int OpponentViewerId,
|
||||
string OpponentName,
|
||||
int OpponentClassId,
|
||||
int OpponentSubClassId,
|
||||
int OpponentCharaId,
|
||||
string OpponentCountryCode,
|
||||
long OpponentEmblemId,
|
||||
long OpponentDegreeId,
|
||||
string OpponentRotationId,
|
||||
DateTime BattleStartTime);
|
||||
17
SVSim.Database/Services/Replay/IBattleContextStore.cs
Normal file
17
SVSim.Database/Services/Replay/IBattleContextStore.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
namespace SVSim.Database.Services.Replay;
|
||||
|
||||
/// <summary>
|
||||
/// In-memory per-viewer battle context store. Bridges the start-time → finish-time
|
||||
/// gap: the /finish request body carries neither battle_id nor opponent identity,
|
||||
/// so this stash holds everything the finish hook needs to compose a
|
||||
/// ViewerBattleHistory row.
|
||||
/// </summary>
|
||||
public interface IBattleContextStore
|
||||
{
|
||||
/// <summary>Store the viewer's active battle context. Overwrites any prior entry.</summary>
|
||||
void Set(long viewerId, BattleContext ctx);
|
||||
|
||||
/// <summary>Atomic read+clear. Returns null when no context (server restart,
|
||||
/// non-tracked family, already taken). Finish handlers must tolerate null.</summary>
|
||||
BattleContext? TakeFor(long viewerId);
|
||||
}
|
||||
20
SVSim.Database/Services/Replay/InMemoryBattleContextStore.cs
Normal file
20
SVSim.Database/Services/Replay/InMemoryBattleContextStore.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace SVSim.Database.Services.Replay;
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="ConcurrentDictionary{TKey, TValue}"/>-backed in-memory store.
|
||||
/// Lives as a singleton in DI. Server restart drops in-flight contexts —
|
||||
/// acceptable per spec (history is best-effort; finish handlers warn-log
|
||||
/// and continue when context is missing).
|
||||
/// </summary>
|
||||
public sealed class InMemoryBattleContextStore : IBattleContextStore
|
||||
{
|
||||
private readonly ConcurrentDictionary<long, BattleContext> _contexts = new();
|
||||
|
||||
public void Set(long viewerId, BattleContext ctx)
|
||||
=> _contexts[viewerId] = ctx;
|
||||
|
||||
public BattleContext? TakeFor(long viewerId)
|
||||
=> _contexts.TryRemove(viewerId, out var ctx) ? ctx : null;
|
||||
}
|
||||
Reference in New Issue
Block a user