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
@@ -78,6 +78,16 @@
<i class="bi bi-joystick me-2"></i>Sessions
</NavLink>
</li>
<li class="nav-item">
<NavLink class="nav-link text-white-50 py-1" href="/game/ranked-rules">
<i class="bi bi-sliders me-2"></i>Ranked Rules
</NavLink>
</li>
<li class="nav-item">
<NavLink class="nav-link text-white-50 py-1" href="/game/custom-lobbies">
<i class="bi bi-people-fill me-2"></i>Custom Lobbies
</NavLink>
</li>
</ul>
</li>
<li class="nav-item">
@@ -0,0 +1,162 @@
@page "/game/custom-lobbies"
@using SOR.Database.Contexts
@using SOR.Database.Entities.Game
@using SOR.Database.Enums
@using Microsoft.EntityFrameworkCore
@inject GameDbContext GameDb
<PageTitle>Custom Lobbies — SOR Admin</PageTitle>
<div class="d-flex justify-content-between align-items-center mb-4">
<h1 class="h3 mb-0">Custom Lobbies</h1>
<button class="btn btn-outline-secondary btn-sm" @onclick="LoadAsync">
<i class="bi bi-arrow-clockwise me-1"></i>Refresh
</button>
</div>
@if (_loading)
{
<div class="d-flex align-items-center gap-2 text-muted">
<div class="spinner-border spinner-border-sm"></div>Loading…
</div>
}
else
{
<!-- Stats row -->
<div class="row g-3 mb-4">
<div class="col-sm-3">
<div class="card text-center p-3">
<div class="fs-2 fw-bold text-primary">@_lobbies.Count(l => l.Status == CustomLobbyStatus.Open)</div>
<div class="text-muted small">Open</div>
</div>
</div>
<div class="col-sm-3">
<div class="card text-center p-3">
<div class="fs-2 fw-bold text-warning">@_lobbies.Count(l => l.Status == CustomLobbyStatus.Full)</div>
<div class="text-muted small">Full</div>
</div>
</div>
<div class="col-sm-3">
<div class="card text-center p-3">
<div class="fs-2 fw-bold text-success">@_lobbies.Count(l => l.Status == CustomLobbyStatus.Started)</div>
<div class="text-muted small">Started Today</div>
</div>
</div>
<div class="col-sm-3">
<div class="card text-center p-3">
<div class="fs-2 fw-bold text-secondary">@_lobbies.Count(l => l.Status == CustomLobbyStatus.Cancelled)</div>
<div class="text-muted small">Cancelled Today</div>
</div>
</div>
</div>
<!-- Filter -->
<div class="mb-3 d-flex gap-2 align-items-center">
<label class="form-label mb-0 fw-semibold">Status:</label>
<select class="form-select form-select-sm w-auto" @bind="_statusFilter">
<option value="">All</option>
<option value="Open">Open</option>
<option value="Full">Full</option>
<option value="Started">Started</option>
<option value="Cancelled">Cancelled</option>
</select>
</div>
<div class="table-responsive">
<table class="table table-sm table-hover align-middle">
<thead class="table-dark">
<tr>
<th>Lobby Name</th>
<th>Host</th>
<th>Guest</th>
<th>Status</th>
<th>Rules (SP/Turn, Hand)</th>
<th>Created</th>
<th>Session</th>
</tr>
</thead>
<tbody>
@foreach (var lobby in FilteredLobbies)
{
<tr>
<td><code>@lobby.Name</code></td>
<td><code class="text-muted small">@lobby.HostAccountId.ToString()[..8]…</code></td>
<td>
@if (lobby.GuestAccountId.HasValue)
{
<code class="text-muted small">@lobby.GuestAccountId.Value.ToString()[..8]…</code>
}
else
{
<span class="text-muted">—</span>
}
</td>
<td>
<span class="badge @StatusBadge(lobby.Status)">@lobby.Status</span>
</td>
<td>
<span class="text-muted small">
+@(lobby.SpAccumulationPerTurnOverride ?? 3) SP/turn,
@(lobby.OpeningHandSizeOverride ?? 6) card hand
</span>
</td>
<td class="text-muted small">@lobby.CreatedAt.ToLocalTime().ToString("MM/dd HH:mm")</td>
<td>
@if (lobby.GameSessionId.HasValue)
{
<code class="text-muted small">@lobby.GameSessionId.Value.ToString()[..8]…</code>
}
else
{
<span class="text-muted">—</span>
}
</td>
</tr>
}
@if (!FilteredLobbies.Any())
{
<tr><td colspan="7" class="text-center text-muted py-3">No lobbies found.</td></tr>
}
</tbody>
</table>
</div>
}
@code {
private List<CustomLobby> _lobbies = new();
private bool _loading = true;
private string _statusFilter = "";
protected override async Task OnInitializedAsync() => await LoadAsync();
private async Task LoadAsync()
{
_loading = true;
var cutoff = DateTimeOffset.UtcNow.Date;
_lobbies = await GameDb.CustomLobbies
.AsNoTracking()
.Where(l => l.Status == CustomLobbyStatus.Open
|| l.Status == CustomLobbyStatus.Full
|| l.CreatedAt >= cutoff)
.OrderByDescending(l => l.CreatedAt)
.Take(200)
.ToListAsync();
_loading = false;
}
private IEnumerable<CustomLobby> FilteredLobbies =>
string.IsNullOrEmpty(_statusFilter)
? _lobbies
: _lobbies.Where(l => l.Status.ToString() == _statusFilter);
private static string StatusBadge(CustomLobbyStatus status) => status switch
{
CustomLobbyStatus.Open => "bg-primary",
CustomLobbyStatus.Full => "bg-warning text-dark",
CustomLobbyStatus.Started => "bg-success",
CustomLobbyStatus.Cancelled => "bg-secondary",
_ => "bg-secondary",
};
}
@@ -0,0 +1,183 @@
@page "/game/ranked-rules"
@using SOR.Database.Contexts
@using SOR.Database.Entities.Game
@using Microsoft.EntityFrameworkCore
@inject GameDbContext GameDb
@inject ILogger<RankedRulesPage> Logger
<PageTitle>Ranked Rules — SOR Admin</PageTitle>
<div class="d-flex justify-content-between align-items-center mb-4">
<h1 class="h3 mb-0">Ranked Play Rules</h1>
<button class="btn btn-success" @onclick="SaveAsync" disabled="@_saving">
@if (_saving) { <span class="spinner-border spinner-border-sm me-1"></span> }
Save Rules
</button>
</div>
@if (_saved)
{
<div class="alert alert-success alert-dismissible fade show" role="alert">
Ranked rules saved successfully.
<button type="button" class="btn-close" @onclick="() => _saved = false"></button>
</div>
}
@if (_error is not null)
{
<div class="alert alert-danger">@_error</div>
}
@if (_rules is null)
{
<div class="d-flex align-items-center gap-2 text-muted">
<div class="spinner-border spinner-border-sm"></div>Loading…
</div>
}
else
{
<div class="row g-4">
<div class="col-md-6">
<div class="card">
<div class="card-header">Opening Hand</div>
<div class="card-body">
<div class="mb-3">
<label class="form-label fw-semibold">Opening Hand Size</label>
<input type="number" class="form-control" min="1" max="20"
@bind="_rules.OpeningHandSize" />
<div class="form-text">Cards each player draws at game start (before turn 1).</div>
</div>
<div class="form-check form-switch mb-2">
<input class="form-check-input" type="checkbox" role="switch" id="skipDraw"
@bind="_rules.FirstPlayerSkipsDraw" />
<label class="form-check-label" for="skipDraw">
First player skips Turn 1 draw
</label>
</div>
<div class="form-text text-muted">
When enabled, P1 goes straight to Main on turn 1. P2 still draws once
at the start of their first turn, giving them one extra card total.
</div>
</div>
</div>
</div>
<div class="col-md-6">
<div class="card">
<div class="card-header">Spirit Points (SP)</div>
<div class="card-body">
<div class="mb-3">
<label class="form-label fw-semibold">SP Accumulation Per Turn</label>
<input type="number" class="form-control" min="0" max="100"
@bind="_rules.SpAccumulationPerTurn" />
<div class="form-text">
SP gained at the start of each player's turn. SP accumulates — it never
resets between turns. All players start at 0 SP.
</div>
</div>
<div class="form-check form-switch mb-2">
<input class="form-check-input" type="checkbox" role="switch" id="spCap"
@bind="_rules.SpHasCap" />
<label class="form-check-label" for="spCap">Enforce SP cap</label>
</div>
@if (_rules.SpHasCap)
{
<div class="mt-2">
<label class="form-label">SP Cap</label>
<input type="number" class="form-control" min="1" max="999"
@bind="_rules.SpCap" />
</div>
}
</div>
</div>
</div>
<div class="col-md-6">
<div class="card">
<div class="card-header">Draw Phase</div>
<div class="card-body">
<div class="mb-3">
<label class="form-label fw-semibold">Cards Drawn Per Turn</label>
<input type="number" class="form-control" min="0" max="10"
@bind="_rules.DrawPerTurn" />
<div class="form-text">Cards drawn during the Draw phase each turn.</div>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<div class="card">
<div class="card-header text-muted">Custom Default</div>
<div class="card-body">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="isCustomDefault"
@bind="_rules.IsCustomDefault" />
<label class="form-check-label" for="isCustomDefault">
Also use as custom lobby default
</label>
</div>
<div class="form-text">
When enabled, custom lobbies inherit these rules before applying their own overrides.
</div>
</div>
</div>
</div>
</div>
}
@code {
private GameRuleSet? _rules;
private bool _saving;
private bool _saved;
private string? _error;
protected override async Task OnInitializedAsync()
{
_rules = await GameDb.GameRuleSets
.Where(r => r.IsRankedDefault)
.FirstOrDefaultAsync();
if (_rules is null)
{
_rules = new GameRuleSet
{
Name = "Ranked",
IsRankedDefault = true,
IsCustomDefault = true,
OpeningHandSize = 6,
FirstPlayerSkipsDraw = true,
SpAccumulationPerTurn = 3,
DrawPerTurn = 1,
SpHasCap = false,
SpCap = 0,
CreatedAt = DateTimeOffset.UtcNow,
UpdatedAt = DateTimeOffset.UtcNow,
};
GameDb.GameRuleSets.Add(_rules);
await GameDb.SaveChangesAsync();
}
}
private async Task SaveAsync()
{
if (_rules is null) return;
_saving = true;
_error = null;
try
{
_rules.UpdatedAt = DateTimeOffset.UtcNow;
await GameDb.SaveChangesAsync();
_saved = true;
}
catch (Exception ex)
{
_error = ex.Message;
Logger.LogError(ex, "Failed to save ranked rules.");
}
finally
{
_saving = false;
}
}
}