Live-star skybox: game activity feed + procedural star sky

Server:
- GameActivityRegistry singleton tracks subscribed clients
- game.subscribe_activity handler: registers client, returns active session IDs
- game.unsubscribe_activity handler: deregisters client
- BroadcastActivityAsync helper in GameHandlerBase (mirrors spectator push pattern)
- GamePlaceArchSpiritHandler broadcasts "started" when both players have placed
- FinalizeGameAsync broadcasts "ended" to all subscribers

Client:
- Settings.skybox_mode: "live" | "cdn" | "off" (default: "live"), persisted to cfg
- Game.game_activity signal forwarded from game.activity push
- game_screen.gd: live mode sets near-black procedural sky; subscribes to activity
  feed; each active session is an emissive sphere star (280-unit radius, blue-white)
  that fades in on start, twinkles via sine tween, and flash-explodes on end
- CDN and Off modes preserved; hot-swap supported mid-game via Settings.settings_changed
- InGameSettings: SkyboxModeOption OptionButton (Live / CDN / Off)

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:
dingo
2026-03-21 09:49:08 -04:00
co-authored by Claude Happy
parent 5b66f459c0
commit 97b8391608
12 changed files with 376 additions and 15 deletions
@@ -0,0 +1,28 @@
namespace SOR.Game;
using System.Collections.Concurrent;
using System.Collections.Generic;
/// <summary>
/// In-memory registry of client connections that have subscribed to game activity
/// events (session started / ended). Used to drive the live-star skybox feature.
///
/// Access via <see cref="Instance"/> from static helpers (e.g. GameHandlerBase)
/// that cannot use constructor injection. The singleton is initialised when the
/// DI container constructs the registered singleton instance.
/// </summary>
public sealed class GameActivityRegistry
{
/// <summary>The singleton set by the constructor when DI builds this instance.</summary>
public static GameActivityRegistry? Instance { get; private set; }
private readonly ConcurrentDictionary<string, byte> _subscribers = new();
public GameActivityRegistry() { Instance = this; }
public void Subscribe(string userId) => _subscribers.TryAdd(userId, 0);
public void Unsubscribe(string userId) => _subscribers.TryRemove(userId, out _);
/// <summary>Snapshot of current subscriber user IDs (safe to enumerate).</summary>
public IReadOnlyList<string> Subscribers => [.. _subscribers.Keys];
}
+19
View File
@@ -461,6 +461,25 @@ public sealed record GameMetaDelta(
string? ActivePlayerAccountId,
int? TurnNumber);
// ── Skybox activity feed ───────────────────────────────────────────────────────
/// <summary>
/// Subscribe to game activity events so the client can animate its live star skybox.
/// The response includes IDs of currently active sessions to initialise the star field.
/// </summary>
public sealed record GameSubscribeActivityRequest;
public sealed record GameSubscribeActivityResponse(IReadOnlyList<string> ActiveSessionIds);
/// <summary>Unsubscribe from game activity events.</summary>
public sealed record GameUnsubscribeActivityRequest;
public sealed record GameUnsubscribeActivityResponse;
/// <summary>
/// Pushed to all subscribed clients when a game session starts or ends.
/// EventType: "started" | "ended"
/// </summary>
public sealed record GameActivityPush(string EventType, string SessionId);
// ── DTOs ─────────────────────────────────────────────────────────────────────
/// <summary>
@@ -89,6 +89,23 @@ internal abstract class GameHandlerBase
await gameDb.Database.CommitTransactionAsync(ct);
}
/// <summary>
/// Broadcast a game activity event (session started / ended) to all subscribed clients.
/// Used by the live-star skybox feature. Best-effort; subscriber list is in-memory only.
/// </summary>
internal static async Task BroadcastActivityAsync(
IClientPush clientPush,
string eventType,
Guid sessionId,
CancellationToken ct)
{
var subs = GameActivityRegistry.Instance?.Subscribers;
if (subs is null || subs.Count == 0) return;
var push = ServerMessage.Push("game.activity",
new GameActivityPush(eventType, sessionId.ToString()));
await Task.WhenAll(subs.Select(uid => clientPush.PushAsync(uid, push, ct)));
}
/// <summary>
/// Push the updated game state to both players and any current spectators.
/// When <paramref name="prevState"/> is supplied a structured delta (<c>game.state_delta</c>)
@@ -270,6 +287,9 @@ internal abstract class GameHandlerBase
await Task.WhenAll(gameOverTasks);
// Notify skybox activity subscribers that this game has ended.
await BroadcastActivityAsync(clientPush, "ended", session.Id, ct);
// Publish inter-service event so ChatLifecycleHostedService archives the match channel.
await eventBus.PublishAsync(NatsSubjects.GameEnded,
new GameEndedEvent(
@@ -58,10 +58,13 @@ internal sealed class GamePlaceArchSpiritHandler : GameHandlerBase, IMessageHand
await PushStateToPlayersAsync(_clientPush, session, newState, ct, state);
// Start the turn timer once both players have placed and turn 1 begins.
// Start the turn timer and notify skybox activity subscribers once both players have placed.
if (bothPlaced)
{
_timerService.ResetTimer(session.Id, newState.Rules.TurnTimerSeconds,
newState.Rules.TimeLimitMinutes, session.CreatedAt);
await BroadcastActivityAsync(_clientPush, "started", session.Id, ct);
}
return ServerMessage.Reply("game.place_arch_spirit.ack", message.Seq,
new GamePlaceArchSpiritResponse(dto));
@@ -0,0 +1,47 @@
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.subscribe_activity — registers this connection to receive game.activity pushes
/// whenever any game session starts or ends on the server.
///
/// Used by the client to power the live-star skybox: each active session appears as
/// a star that fades in on start and explodes on end.
///
/// The response includes IDs of all currently active sessions so the client can
/// pre-populate the sky with existing stars on first subscribe.
/// </summary>
internal sealed class GameSubscribeActivityHandler : IMessageHandler
{
private readonly GameDbContext _gameDb;
public GameSubscribeActivityHandler(GameDbContext gameDb)
{
_gameDb = gameDb;
}
public string MessageType => "game.subscribe_activity";
public async Task<IServerMessage?> HandleAsync(
IClientMessage message,
AuthenticatedConnection connection,
CancellationToken ct)
{
GameActivityRegistry.Instance?.Subscribe(connection.UserId.ToString());
var activeIds = await _gameDb.GameSessions
.AsNoTracking()
.Where(s => s.Status == GameSessionStatus.Active)
.Select(s => s.Id.ToString())
.ToListAsync(ct);
return ServerMessage.Reply("game.subscribe_activity.ack", message.Seq,
new GameSubscribeActivityResponse(activeIds));
}
}
@@ -0,0 +1,26 @@
namespace SOR.Game.Handlers;
using SOR.Networking.Abstractions.Messaging;
using SOR.Networking.Abstractions.Transport;
using SOR.Shared.Networking;
/// <summary>
/// game.unsubscribe_activity — removes this connection from the activity subscriber list.
/// Called by the client when leaving the game screen.
/// </summary>
internal sealed class GameUnsubscribeActivityHandler : IMessageHandler
{
public string MessageType => "game.unsubscribe_activity";
public Task<IServerMessage?> HandleAsync(
IClientMessage message,
AuthenticatedConnection connection,
CancellationToken ct)
{
GameActivityRegistry.Instance?.Unsubscribe(connection.UserId.ToString());
return Task.FromResult<IServerMessage?>(
ServerMessage.Reply("game.unsubscribe_activity.ack", message.Seq,
new GameUnsubscribeActivityResponse()));
}
}
@@ -35,6 +35,9 @@ public static class ServiceRegistration
// Tracks spectators per session (in-memory; spectators only, players are not registered here).
services.AddSingleton<SpectatorRegistryService>();
// Tracks clients subscribed to game activity events for the live-star skybox.
services.AddSingleton<GameActivityRegistry>();
services.AddTransient<IMessageHandler, GameReadyHandler>();
services.AddTransient<IMessageHandler, GameGetStateHandler>();
services.AddTransient<IMessageHandler, GameGetActiveSessionHandler>();
@@ -69,6 +72,10 @@ public static class ServiceRegistration
services.AddTransient<IMessageHandler, GameSpectateHandler>();
services.AddTransient<IMessageHandler, GameStopSpectatingHandler>();
// Skybox activity feed handlers
services.AddTransient<IMessageHandler, GameSubscribeActivityHandler>();
services.AddTransient<IMessageHandler, GameUnsubscribeActivityHandler>();
// Preset vote service (in-memory, one active vote per lobby at a time).
services.AddSingleton<VoteSessionService>();