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>
This commit is contained in:
dingo
2026-02-26 07:55:32 -05:00
co-authored by Claude Happy
parent 2058a47a92
commit 682a525193
21 changed files with 1399 additions and 96 deletions
@@ -1,5 +1,6 @@
namespace SOR.Matchmaking;
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
@@ -100,6 +101,12 @@ internal sealed class MatchmakerService : BackgroundService
if (opponent == null)
continue;
// Load the ranked ruleset and snapshot it into the session.
var rankedRules = await gameDb.GameRuleSets
.AsNoTracking()
.Where(r => r.IsRankedDefault)
.FirstOrDefaultAsync(ct);
// Create a game session.
var session = new GameSession
{
@@ -109,6 +116,17 @@ internal sealed class MatchmakerService : BackgroundService
PlayerTwoDeckId = opponent.DeckId,
Status = GameSessionStatus.Initializing,
CreatedAt = now,
RuleSnapshotJson = rankedRules is not null
? JsonSerializer.Serialize(new
{
rankedRules.OpeningHandSize,
rankedRules.FirstPlayerSkipsDraw,
rankedRules.SpAccumulationPerTurn,
rankedRules.DrawPerTurn,
rankedRules.SpHasCap,
rankedRules.SpCap,
})
: null,
};
gameDb.GameSessions.Add(session);