Phase 3: GameTurnTimerService — per-turn auto-end timer

- GameTurnTimerService singleton manages CancellationTokenSource per session.
  Fires AutoEndTurnAsync on expiry: acquires row lock, calls GameEngine.EndTurn,
  saves state, pushes to players, finalizes game if turn-limit reached.
  Caps effective delay by TimeLimitMinutes remaining wall time.
- FinalizeGameAsync (GameHandlerBase) calls GameTurnTimerService.Instance?.CancelTimer
  so all game-over paths (attack, concede, force-concede) cleanly cancel timers.
- GamePlaceArchSpiritHandler: injects timer service; ResetTimer when bothPlaced.
- GameEndTurnHandler: injects timer service; ResetTimer on normal end-turn,
  CancelTimer (via FinalizeGameAsync) on turn-limit game-over.
- ServiceRegistration: singleton + AddHostedService delegate for lifetime callbacks.
- GameHandlerBase: SaveStateAsync, PushStateToPlayersAsync, FinalizeGameAsync
  promoted from protected static → internal static for timer service access.

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-18 23:11:15 -04:00
co-authored by Claude Happy
parent 93d27e9270
commit 2c40fb036f
5 changed files with 231 additions and 19 deletions
@@ -58,7 +58,7 @@ internal abstract class GameHandlerBase
/// Persist the updated state JSON and append an action log entry,
/// then commit the transaction opened by <see cref="LoadActiveSessionAsync"/>.
/// </summary>
protected static async Task SaveStateAsync(
internal static async Task SaveStateAsync(
GameDbContext gameDb,
GameSession session,
GameState state,
@@ -92,7 +92,7 @@ internal abstract class GameHandlerBase
/// <summary>
/// Push the updated (filtered) game state to both players.
/// </summary>
protected static async Task PushStateToPlayersAsync(
internal static async Task PushStateToPlayersAsync(
IClientPush clientPush,
GameSession session,
GameState state,
@@ -120,7 +120,7 @@ internal abstract class GameHandlerBase
/// Finalize a completed game: update session status, adjust ELO ranks,
/// award OriginPoints, push game-over to both players, publish GameEndedEvent.
/// </summary>
protected static async Task FinalizeGameAsync(
internal static async Task FinalizeGameAsync(
GameDbContext gameDb,
SorIdentityDbContext identityDb,
IClientPush clientPush,
@@ -131,6 +131,9 @@ internal abstract class GameHandlerBase
string winCondition,
CancellationToken ct)
{
// Cancel any pending turn timer — the game is over.
GameTurnTimerService.Instance?.CancelTimer(session.Id);
// These fields may already be set by the calling handler (so they are committed
// atomically with the StateJson update in SaveStateAsync). Setting them here again
// is idempotent and ensures correctness even if called without the pre-set pattern.