Add matchmaking + gameplay services (SOR.Matchmaking + SOR.Game)

DB layer:
- Account.RankScore (int, default 1000) — ELO rank for matchmaking
- GameEnums: MatchmakingStatus, GameSessionStatus, GamePhase
- Entities: MatchmakingEntry, GameSession, GameActionLog
- GameDbContext (schema: game) + AddGameDatabase() registration
- Migrations: AddRankScore (Identity), InitialCreate (Game)

SOR.Matchmaking:
- MatchmakerService (BackgroundService) — 5s poll, ELO ±100→±500 range
- Handlers: matchmaking.join, matchmaking.leave, matchmaking.status
- Pushes MatchFoundPush to both players via IClientPush on match

SOR.Game:
- GameState / PlayerState / CardInstance / FieldCard JSONB model
- GameEngine (pure static): Init, Draw, PlayCard, Attack, EndTurn, Concede
- ELO delta (K=32) computed and applied on game end
- Handlers: game.ready, game.get_state, game.draw, game.play_card,
            game.attack, game.end_turn, game.concede
- State filtered per-viewer (opponent hand/deck hidden)
- GameOverPush with rank deltas to both players

Admin pages:
- /game/matchmaking — live queue stats, active entries, recent activity
- /game/sessions   — session stats, filterable by status/account

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-02-26 06:57:13 -05:00
co-authored by Claude Happy
parent b5c9385e73
commit 2058a47a92
40 changed files with 3360 additions and 0 deletions
@@ -0,0 +1,33 @@
namespace SOR.Database.Entities.Game;
/// <summary>
/// Immutable audit log of every action taken during a game session.
/// Used for replay, dispute resolution, and analytics.
///
/// PayloadJson stores the raw action request (draw, play_card, attack, etc.)
/// as JSONB so no schema changes are needed when actions evolve.
/// </summary>
public sealed class GameActionLog
{
public Guid Id { get; set; } = Guid.NewGuid();
public Guid GameSessionId { get; set; }
/// <summary>AccountId of the player who took the action.</summary>
public Guid ActorAccountId { get; set; }
/// <summary>Action type — mirrors the game message type (e.g. "game.play_card").</summary>
public string ActionType { get; set; } = string.Empty;
/// <summary>JSONB: serialized action request payload.</summary>
public string? PayloadJson { get; set; }
/// <summary>1-based sequence number within the game session.</summary>
public int SequenceNumber { get; set; }
public DateTimeOffset OccurredAt { get; set; }
// ── Navigation ───────────────────────────────────────────────────────────
public GameSession Session { get; set; } = null!;
}
@@ -0,0 +1,63 @@
namespace SOR.Database.Entities.Game;
using SOR.Database.Enums;
/// <summary>
/// Represents a single game match between two players.
///
/// Live game state is stored as JSONB in StateJson and is updated after every action.
/// The full GameState object (board, hands, decks, etc.) is serialized there.
///
/// Cross-schema: all account and deck IDs are bare Guids — no EF foreign keys.
/// </summary>
public sealed class GameSession
{
public Guid Id { get; set; } = Guid.NewGuid();
// ── Players ──────────────────────────────────────────────────────────────
public Guid PlayerOneAccountId { get; set; }
public Guid PlayerOneDeckId { get; set; }
public Guid PlayerTwoAccountId { get; set; }
public Guid PlayerTwoDeckId { get; set; }
// ── Readiness ────────────────────────────────────────────────────────────
/// <summary>Set to true when player one sends game.ready.</summary>
public bool PlayerOneReady { get; set; }
/// <summary>Set to true when player two sends game.ready.</summary>
public bool PlayerTwoReady { get; set; }
// ── Lifecycle ────────────────────────────────────────────────────────────
public GameSessionStatus Status { get; set; } = GameSessionStatus.Initializing;
public DateTimeOffset CreatedAt { get; set; }
public DateTimeOffset? StartedAt { get; set; }
public DateTimeOffset? EndedAt { get; set; }
// ── Result ───────────────────────────────────────────────────────────────
/// <summary>AccountId of the winning player. Null until game ends.</summary>
public Guid? WinnerAccountId { get; set; }
/// <summary>
/// "concede" | "health" (ArchSpirit destroyed) | "disconnect" | "timeout"
/// </summary>
public string? WinCondition { get; set; }
// ── Live state ───────────────────────────────────────────────────────────
/// <summary>
/// JSONB blob — the full live GameState object.
/// Updated after every game action.
/// The GameEngine reads and writes this exclusively.
/// </summary>
public string? StateJson { get; set; }
// ── Navigation ───────────────────────────────────────────────────────────
public ICollection<GameActionLog> ActionLog { get; set; } = new List<GameActionLog>();
}
@@ -0,0 +1,33 @@
namespace SOR.Database.Entities.Game;
using SOR.Database.Enums;
/// <summary>
/// Represents a player's entry in the matchmaking queue.
/// Created when a player joins the queue; status updated when matched or cancelled.
///
/// Cross-schema: AccountId and DeckId are bare Guids — no EF foreign keys.
/// </summary>
public sealed class MatchmakingEntry
{
public Guid Id { get; set; } = Guid.NewGuid();
/// <summary>Identity schema account (bare Guid — no EF FK).</summary>
public Guid AccountId { get; set; }
/// <summary>Deck the player intends to use for the match.</summary>
public Guid DeckId { get; set; }
/// <summary>ELO rank score at the time the player joined the queue.</summary>
public int RankScore { get; set; }
public MatchmakingStatus Status { get; set; } = MatchmakingStatus.Searching;
public DateTimeOffset JoinedAt { get; set; }
/// <summary>Null until a match is found or entry is cancelled.</summary>
public DateTimeOffset? ResolvedAt { get; set; }
/// <summary>Set when status transitions to Matched.</summary>
public Guid? MatchedGameSessionId { get; set; }
}