Files
sor/Server/src/SOR.Database/Entities/Game/GameSession.cs
T
2058a47a92 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>
2026-02-26 06:57:13 -05:00

64 lines
3.0 KiB
C#

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>();
}