Spectator registrations were held in a per-process ConcurrentDictionary,
causing spectators to miss updates when game actions were handled by a
different pod than the one that processed game.spectate.
Changes:
- SpectatorRegistryService: replace ConcurrentDictionary with Redis Set
operations (SADD/SREM/SMEMBERS/DEL) via IConnectionMultiplexer.
All four methods are now async (RegisterAsync, UnregisterAsync,
GetSpectatorsAsync, RemoveSessionAsync). TTL of 24h on each SADD
for self-expiry on abnormal session end.
- RedisKeys: add SpectatorSet(Guid sessionId) -> "spectators:{id}"
- SOR.Game.csproj: add StackExchange.Redis package reference
- GameHandlerBase: all GetSpectators/RemoveSession call sites updated
to await the new async methods (BroadcastToAllViewersAsync,
PushStateToPlayersAsync x3, FinalizeGame x2, BroadcastPlayerEliminated)
- GameSpectateHandler: await RegisterAsync
- GameStopSpectatingHandler: make HandleAsync truly async, await UnregisterAsync
- ServiceRegistration: update comment to reflect Redis backing
Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)
Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
60 lines
2.0 KiB
C#
60 lines
2.0 KiB
C#
namespace SOR.Game.Handlers;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
using SOR.Database.Contexts;
|
|
using SOR.Database.Enums;
|
|
using SOR.Networking.Abstractions.Messaging;
|
|
using SOR.Networking.Abstractions.Transport;
|
|
using SOR.Shared.Networking;
|
|
|
|
/// <summary>
|
|
/// game.spectate — register as a spectator for an active game session.
|
|
/// Returns the current game state filtered for spectator view (both hands hidden).
|
|
/// The spectator will receive game.state_update pushes for the remainder of the game.
|
|
/// </summary>
|
|
internal sealed class GameSpectateHandler : GameHandlerBase, IMessageHandler
|
|
{
|
|
private readonly IMessageSerializer _serializer;
|
|
private readonly GameDbContext _gameDb;
|
|
private readonly SpectatorRegistryService _registry;
|
|
|
|
public GameSpectateHandler(
|
|
IMessageSerializer serializer,
|
|
GameDbContext gameDb,
|
|
SpectatorRegistryService registry)
|
|
{
|
|
_serializer = serializer;
|
|
_gameDb = gameDb;
|
|
_registry = registry;
|
|
}
|
|
|
|
public string MessageType => "game.spectate";
|
|
|
|
public async Task<IServerMessage?> HandleAsync(
|
|
IClientMessage message,
|
|
AuthenticatedConnection connection,
|
|
CancellationToken ct)
|
|
{
|
|
var req = _serializer.DeserializePayload<SpectateRequest>(message.Payload);
|
|
var accountId = Guid.Parse(connection.UserId);
|
|
|
|
var session = await _gameDb.GameSessions
|
|
.AsNoTracking()
|
|
.Where(s => s.Id == req.SessionId && s.Status == GameSessionStatus.Active)
|
|
.SingleOrDefaultAsync(ct)
|
|
?? throw new GameException("SESSION_NOT_FOUND");
|
|
|
|
var state = GameEngine.Deserialize(session.StateJson!);
|
|
|
|
if (state.Phase == GamePhase.Placement)
|
|
throw new GameException("PLACEMENT_IN_PROGRESS");
|
|
|
|
await _registry.RegisterAsync(req.SessionId, accountId);
|
|
|
|
var dto = GameEngine.ToDtoSpectator(state);
|
|
|
|
return ServerMessage.Reply("game.spectate.ack", message.Seq,
|
|
new SpectateResponse(dto));
|
|
}
|
|
}
|