Phase 4: Spectator support — SpectatorRegistryService + handlers
- SpectatorRegistryService singleton: ConcurrentDictionary<Guid, ImmutableHashSet<Guid>> tracking spectators per session. Static Instance for use in static base-class helpers. - GameEngine.ToDtoSpectator: delegates to ToDto(state, "") — both hands hidden. - GameHandlerBase.PushStateToPlayersAsync: extended to push hand-hidden state_update to all registered spectators via SpectatorRegistryService.Instance. - GameHandlerBase.FinalizeGameAsync: pushes game.over to spectators then calls RemoveSession to evict the entry from the registry. - GameSpectateHandler (game.spectate): validates session Active, registers spectator, returns current spectator-view state as ack. - GameStopSpectatingHandler (game.spectate.stop): unregisters the caller. - GameMessages.cs: SpectateRequest/Response, StopSpectatingRequest/Response. - ServiceRegistration.cs: register SpectatorRegistryService singleton + both handlers. 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>
This commit is contained in:
@@ -90,7 +90,8 @@ internal abstract class GameHandlerBase
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Push the updated (filtered) game state to both players.
|
||||
/// Push the updated (filtered) game state to both players and any current spectators.
|
||||
/// Spectators receive a hand-hidden view via SpectatorRegistryService.Instance.
|
||||
/// </summary>
|
||||
internal static async Task PushStateToPlayersAsync(
|
||||
IClientPush clientPush,
|
||||
@@ -101,13 +102,31 @@ internal abstract class GameHandlerBase
|
||||
var p1Id = session.PlayerOneAccountId.ToString();
|
||||
var p2Id = session.PlayerTwoAccountId.ToString();
|
||||
|
||||
await Task.WhenAll(
|
||||
var tasks = new List<Task>
|
||||
{
|
||||
clientPush.PushAsync(p1Id,
|
||||
ServerMessage.Push("game.state_update",
|
||||
new GameStateUpdatePush(GameEngine.ToDto(state, p1Id))), ct),
|
||||
clientPush.PushAsync(p2Id,
|
||||
ServerMessage.Push("game.state_update",
|
||||
new GameStateUpdatePush(GameEngine.ToDto(state, p2Id))), ct));
|
||||
new GameStateUpdatePush(GameEngine.ToDto(state, p2Id))), ct),
|
||||
};
|
||||
|
||||
var registry = SpectatorRegistryService.Instance;
|
||||
if (registry != null)
|
||||
{
|
||||
var spectators = registry.GetSpectators(session.Id);
|
||||
if (spectators.Count > 0)
|
||||
{
|
||||
var spectatorDto = GameEngine.ToDtoSpectator(state);
|
||||
foreach (var specId in spectators)
|
||||
tasks.Add(clientPush.PushAsync(specId.ToString(),
|
||||
ServerMessage.Push("game.state_update",
|
||||
new GameStateUpdatePush(spectatorDto)), ct));
|
||||
}
|
||||
}
|
||||
|
||||
await Task.WhenAll(tasks);
|
||||
}
|
||||
|
||||
// OP config snapshot — read from RuleSnapshotJson at finalization time.
|
||||
@@ -189,11 +208,25 @@ internal abstract class GameHandlerBase
|
||||
WinnerOpDelta : winnerOp,
|
||||
LoserOpDelta : loserOp);
|
||||
|
||||
await Task.WhenAll(
|
||||
var gameOverTasks = new List<Task>
|
||||
{
|
||||
clientPush.PushAsync(winnerAccountId.ToString(),
|
||||
ServerMessage.Push("game.over", push), ct),
|
||||
clientPush.PushAsync(loserAccountId.ToString(),
|
||||
ServerMessage.Push("game.over", push), ct));
|
||||
ServerMessage.Push("game.over", push), ct),
|
||||
};
|
||||
|
||||
// Push game.over to spectators then evict the session from the registry.
|
||||
var spectatorRegistry = SpectatorRegistryService.Instance;
|
||||
if (spectatorRegistry != null)
|
||||
{
|
||||
foreach (var specId in spectatorRegistry.GetSpectators(session.Id))
|
||||
gameOverTasks.Add(clientPush.PushAsync(specId.ToString(),
|
||||
ServerMessage.Push("game.over", push), ct));
|
||||
spectatorRegistry.RemoveSession(session.Id);
|
||||
}
|
||||
|
||||
await Task.WhenAll(gameOverTasks);
|
||||
|
||||
// Publish inter-service event so ChatLifecycleHostedService archives the match channel.
|
||||
await eventBus.PublishAsync(NatsSubjects.GameEnded,
|
||||
|
||||
Reference in New Issue
Block a user