Files
sor/Server/src/SOR.Database/Entities/Game/GameSession.cs
T
682a525193 Fix gameplay engine rules + add RuleSet/CustomLobby system
Engine corrections (per gameplay.md + user spec):
- Opening draw: both players draw 6 cards at game start
- P1 skips Draw phase on turn 1 (already has opening hand);
  P2 draws once at start of their first turn → 7 cards total
- SP: starts at 0, accumulates +3/turn (never resets, no cap by default)
- ST: starts at 0, earned only through sacrifice (no per-turn grant)
- No entry sickness: units can attack/move/ability the turn they are summoned
- Defense restores to MaxDefense at start of each turn (§6.9.3)
- Rename Graveyard → Discard throughout (GameState, GameEngine, GameMessages)
- Remove IsEntrySick flag from CardInstance and CardInstanceDto

GameRuleSet / CustomLobby infrastructure:
- GameRuleSet DB entity: admin-configurable rules (IsRankedDefault, IsCustomDefault)
- GameRules record (SOR.Game): in-memory config embedded in GameState.Rules
- GameSession.RuleSnapshotJson: immutable per-session rule snapshot (jsonb)
- MatchmakerService: loads ranked ruleset and snapshots it into each GameSession
- GameReadyHandler: deserializes rule snapshot and passes rules to GameEngine.Initialize
- CustomLobby DB entity: host+guest, rule overrides, GameSessionId link
- CustomLobbyStatus enum: Open / Full / Started / Cancelled

Custom game handlers (4):
- game.custom.create — host creates lobby with optional rule overrides
- game.custom.join  — guest joins; when full, creates GameSession + pushes both players
- game.custom.leave — host cancels or guest leaves
- game.custom.list  — paged list of Open lobbies

Admin portal:
- /game/ranked-rules — edit the ranked play ruleset (auto-seeds default on first visit)
- /game/custom-lobbies — read-only view of active and recent custom lobbies
- NavMenu: added Ranked Rules + Custom Lobbies links under Game

Migration: AddGameRulesAndCustomLobby (game_rule_sets, custom_lobbies,
  rule_snapshot column on game_sessions)

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 07:55:32 -05:00

71 lines
3.3 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; }
/// <summary>
/// JSONB blob — snapshot of the GameRuleSet applied to this session at creation.
/// Immutable once the session is created; in-flight games are not affected by
/// admin changes to the live ruleset.
/// </summary>
public string? RuleSnapshotJson { get; set; }
// ── Navigation ───────────────────────────────────────────────────────────
public ICollection<GameActionLog> ActionLog { get; set; } = new List<GameActionLog>();
}