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,183 @@
@page "/game/matchmaking"
@attribute [Authorize(Roles = "Admin,SuperAdmin")]
@using Microsoft.EntityFrameworkCore
@using SOR.Database.Contexts
@using SOR.Database.Enums
@inject GameDbContext Db
<PageTitle>Matchmaking Queue – SOR Admin</PageTitle>
<div class="d-flex justify-content-between align-items-center mb-4">
<h1 class="mb-0">Matchmaking Queue</h1>
<button class="btn btn-outline-secondary btn-sm" @onclick="LoadAsync">
<i class="bi bi-arrow-clockwise me-1"></i>Refresh
</button>
</div>
<!-- Stats -->
<div class="row g-3 mb-4">
<div class="col-md-4">
<div class="card shadow-sm text-center py-3">
<div class="display-6 fw-bold text-primary">@_searchingCount</div>
<div class="text-muted small">Currently Searching</div>
</div>
</div>
<div class="col-md-4">
<div class="card shadow-sm text-center py-3">
<div class="display-6 fw-bold text-success">@_matchedToday</div>
<div class="text-muted small">Matched Today</div>
</div>
</div>
<div class="col-md-4">
<div class="card shadow-sm text-center py-3">
<div class="display-6 fw-bold text-secondary">@_cancelledToday</div>
<div class="text-muted small">Cancelled Today</div>
</div>
</div>
</div>
@if (_loading)
{
<p class="text-muted">Loading…</p>
}
else
{
<!-- Active Queue -->
<div class="card shadow-sm mb-4">
<div class="card-header fw-medium d-flex justify-content-between">
<span>Active Queue</span>
<span class="badge bg-primary">@_searchingEntries.Count players waiting</span>
</div>
<div class="card-body p-0">
<table class="table table-sm table-hover mb-0">
<thead class="table-light">
<tr>
<th>Account</th>
<th class="text-end">Rank</th>
<th>Waiting</th>
<th>Deck</th>
<th>Entry ID</th>
</tr>
</thead>
<tbody>
@foreach (var e in _searchingEntries)
{
<tr>
<td class="font-monospace small">@e.AccountId.ToString()[..8]…</td>
<td class="text-end fw-medium">@e.RankScore</td>
<td class="text-muted small">@FormatWait(e.JoinedAt)</td>
<td class="font-monospace small text-muted">@e.DeckId.ToString()[..8]…</td>
<td class="font-monospace small text-muted">@e.Id.ToString()[..8]…</td>
</tr>
}
@if (!_searchingEntries.Any())
{
<tr>
<td colspan="5" class="text-muted text-center py-3">Queue is empty.</td>
</tr>
}
</tbody>
</table>
</div>
</div>
<!-- Recent Entries -->
<div class="card shadow-sm">
<div class="card-header fw-medium">Recent Activity (last 100)</div>
<div class="card-body p-0">
<table class="table table-sm table-hover mb-0">
<thead class="table-light">
<tr>
<th>Joined</th>
<th>Account</th>
<th class="text-end">Rank</th>
<th>Status</th>
<th>Wait</th>
</tr>
</thead>
<tbody>
@foreach (var e in _recentEntries)
{
<tr>
<td class="text-muted small">@e.JoinedAt.ToString("HH:mm:ss")</td>
<td class="font-monospace small">@e.AccountId.ToString()[..8]…</td>
<td class="text-end">@e.RankScore</td>
<td>
<span class="badge @StatusBadge(e.Status)">@e.Status</span>
</td>
<td class="small text-muted">
@(e.ResolvedAt.HasValue
? FormatDuration(e.ResolvedAt.Value - e.JoinedAt)
: FormatWait(e.JoinedAt))
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
}
@code {
private bool _loading = true;
private int _searchingCount, _matchedToday, _cancelledToday;
private List<QueueRow> _searchingEntries = new();
private List<QueueRow> _recentEntries = new();
private record QueueRow(
Guid Id,
Guid AccountId,
Guid DeckId,
int RankScore,
string Status,
DateTimeOffset JoinedAt,
DateTimeOffset? ResolvedAt);
protected override async Task OnInitializedAsync() => await LoadAsync();
private async Task LoadAsync()
{
_loading = true;
var todayStart = DateTimeOffset.UtcNow.Date;
_searchingCount = await Db.MatchmakingEntries.CountAsync(e => e.Status == MatchmakingStatus.Searching);
_matchedToday = await Db.MatchmakingEntries.CountAsync(e => e.Status == MatchmakingStatus.Matched && e.ResolvedAt >= todayStart);
_cancelledToday = await Db.MatchmakingEntries.CountAsync(e => e.Status == MatchmakingStatus.Cancelled && e.ResolvedAt >= todayStart);
_searchingEntries = await Db.MatchmakingEntries
.Where(e => e.Status == MatchmakingStatus.Searching)
.OrderBy(e => e.JoinedAt)
.Select(e => new QueueRow(e.Id, e.AccountId, e.DeckId, e.RankScore, e.Status.ToString(), e.JoinedAt, e.ResolvedAt))
.ToListAsync();
_recentEntries = await Db.MatchmakingEntries
.OrderByDescending(e => e.JoinedAt)
.Take(100)
.Select(e => new QueueRow(e.Id, e.AccountId, e.DeckId, e.RankScore, e.Status.ToString(), e.JoinedAt, e.ResolvedAt))
.ToListAsync();
_loading = false;
}
private static string FormatWait(DateTimeOffset joinedAt)
{
var wait = DateTimeOffset.UtcNow - joinedAt;
return FormatDuration(wait);
}
private static string FormatDuration(TimeSpan ts)
{
if (ts.TotalHours >= 1) return $"{(int)ts.TotalHours}h {ts.Minutes}m";
if (ts.TotalMinutes >= 1) return $"{(int)ts.TotalMinutes}m {ts.Seconds}s";
return $"{(int)ts.TotalSeconds}s";
}
private static string StatusBadge(string status) => status switch
{
nameof(MatchmakingStatus.Searching) => "bg-primary",
nameof(MatchmakingStatus.Matched) => "bg-success",
nameof(MatchmakingStatus.Cancelled) => "bg-secondary",
_ => "bg-light text-dark",
};
}