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:
@@ -0,0 +1,229 @@
|
||||
@page "/game/sessions"
|
||||
@attribute [Authorize(Roles = "Admin,SuperAdmin")]
|
||||
@using Microsoft.EntityFrameworkCore
|
||||
@using SOR.Database.Contexts
|
||||
@using SOR.Database.Enums
|
||||
@inject GameDbContext Db
|
||||
|
||||
<PageTitle>Game Sessions – SOR Admin</PageTitle>
|
||||
|
||||
<div class="d-flex justify-content-between align-items-center mb-4">
|
||||
<h1 class="mb-0">Game Sessions</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-3">
|
||||
<div class="card shadow-sm text-center py-3">
|
||||
<div class="display-6 fw-bold text-warning">@_initializingCount</div>
|
||||
<div class="text-muted small">Initializing</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-3">
|
||||
<div class="card shadow-sm text-center py-3">
|
||||
<div class="display-6 fw-bold text-success">@_activeCount</div>
|
||||
<div class="text-muted small">Active</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-3">
|
||||
<div class="card shadow-sm text-center py-3">
|
||||
<div class="display-6 fw-bold text-primary">@_completedToday</div>
|
||||
<div class="text-muted small">Completed Today</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-3">
|
||||
<div class="card shadow-sm text-center py-3">
|
||||
<div class="display-6 fw-bold text-secondary">@_abandonedToday</div>
|
||||
<div class="text-muted small">Abandoned Today</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Filters -->
|
||||
<div class="card shadow-sm mb-3">
|
||||
<div class="card-body py-2">
|
||||
<div class="row g-3 align-items-end">
|
||||
<div class="col-md-3">
|
||||
<label class="form-label small fw-medium">Status</label>
|
||||
<select @bind="_filterStatus" class="form-select form-select-sm">
|
||||
<option value="">All</option>
|
||||
@foreach (var s in Enum.GetNames<GameSessionStatus>())
|
||||
{
|
||||
<option value="@s">@s</option>
|
||||
}
|
||||
</select>
|
||||
</div>
|
||||
<div class="col-md-3">
|
||||
<label class="form-label small fw-medium">Player Account ID</label>
|
||||
<input @bind="_filterAccountId" class="form-control form-control-sm font-monospace"
|
||||
placeholder="GUID" />
|
||||
</div>
|
||||
<div class="col-md-2">
|
||||
<button class="btn btn-primary btn-sm w-100 mt-4" @onclick="LoadAsync">
|
||||
<i class="bi bi-search me-1"></i>Search
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@if (_loading)
|
||||
{
|
||||
<p class="text-muted">Loading…</p>
|
||||
}
|
||||
else
|
||||
{
|
||||
<div class="card shadow-sm">
|
||||
<div class="card-header fw-medium d-flex justify-content-between">
|
||||
<span>Sessions</span>
|
||||
<span class="badge bg-secondary">@_total</span>
|
||||
</div>
|
||||
<div class="card-body p-0">
|
||||
<table class="table table-sm table-hover mb-0">
|
||||
<thead class="table-light">
|
||||
<tr>
|
||||
<th>Created</th>
|
||||
<th>Session ID</th>
|
||||
<th>P1 Account</th>
|
||||
<th>P2 Account</th>
|
||||
<th>Status</th>
|
||||
<th>Duration</th>
|
||||
<th>Winner</th>
|
||||
<th>Win Condition</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var s in _sessions)
|
||||
{
|
||||
<tr>
|
||||
<td class="text-muted small">@s.CreatedAt.ToString("yyyy-MM-dd HH:mm")</td>
|
||||
<td class="font-monospace small text-muted">@s.Id.ToString()[..8]…</td>
|
||||
<td class="font-monospace small">@s.PlayerOneAccountId.ToString()[..8]…</td>
|
||||
<td class="font-monospace small">@s.PlayerTwoAccountId.ToString()[..8]…</td>
|
||||
<td><span class="badge @StatusBadge(s.Status)">@s.Status</span></td>
|
||||
<td class="text-muted small">@s.Duration</td>
|
||||
<td class="font-monospace small">
|
||||
@(s.WinnerAccountId.HasValue ? s.WinnerAccountId.Value.ToString()[..8] + "…" : "—")
|
||||
</td>
|
||||
<td class="text-muted small">@(s.WinCondition ?? "—")</td>
|
||||
</tr>
|
||||
}
|
||||
@if (!_sessions.Any())
|
||||
{
|
||||
<tr>
|
||||
<td colspan="8" class="text-muted text-center py-3">No sessions found.</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@if (_totalPages > 1)
|
||||
{
|
||||
<div class="d-flex justify-content-center gap-2 mt-3">
|
||||
<button class="btn btn-sm btn-outline-secondary"
|
||||
disabled="@(_page <= 1)"
|
||||
@onclick="() => ChangePage(_page - 1)">
|
||||
<i class="bi bi-chevron-left"></i>
|
||||
</button>
|
||||
<span class="btn btn-sm disabled">@_page / @_totalPages</span>
|
||||
<button class="btn btn-sm btn-outline-secondary"
|
||||
disabled="@(_page >= _totalPages)"
|
||||
@onclick="() => ChangePage(_page + 1)">
|
||||
<i class="bi bi-chevron-right"></i>
|
||||
</button>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
@code {
|
||||
private bool _loading = true;
|
||||
private int _page = 1, _totalPages = 1, _total;
|
||||
private const int PageSize = 50;
|
||||
|
||||
private int _initializingCount, _activeCount, _completedToday, _abandonedToday;
|
||||
private string _filterStatus = string.Empty;
|
||||
private string _filterAccountId = string.Empty;
|
||||
|
||||
private record SessionRow(
|
||||
Guid Id,
|
||||
Guid PlayerOneAccountId,
|
||||
Guid PlayerTwoAccountId,
|
||||
string Status,
|
||||
DateTimeOffset CreatedAt,
|
||||
string Duration,
|
||||
Guid? WinnerAccountId,
|
||||
string? WinCondition);
|
||||
|
||||
private List<SessionRow> _sessions = new();
|
||||
|
||||
protected override async Task OnInitializedAsync() => await LoadAsync();
|
||||
|
||||
private async Task LoadAsync()
|
||||
{
|
||||
_loading = true;
|
||||
var todayStart = DateTimeOffset.UtcNow.Date;
|
||||
|
||||
_initializingCount = await Db.GameSessions.CountAsync(s => s.Status == GameSessionStatus.Initializing);
|
||||
_activeCount = await Db.GameSessions.CountAsync(s => s.Status == GameSessionStatus.Active);
|
||||
_completedToday = await Db.GameSessions.CountAsync(s => s.Status == GameSessionStatus.Completed && s.EndedAt >= todayStart);
|
||||
_abandonedToday = await Db.GameSessions.CountAsync(s => s.Status == GameSessionStatus.Abandoned && s.EndedAt >= todayStart);
|
||||
|
||||
var q = Db.GameSessions.AsQueryable();
|
||||
|
||||
if (!string.IsNullOrEmpty(_filterStatus) && Enum.TryParse<GameSessionStatus>(_filterStatus, out var st))
|
||||
q = q.Where(s => s.Status == st);
|
||||
|
||||
if (Guid.TryParse(_filterAccountId, out var accId))
|
||||
q = q.Where(s => s.PlayerOneAccountId == accId || s.PlayerTwoAccountId == accId);
|
||||
|
||||
_total = await q.CountAsync();
|
||||
_totalPages = Math.Max(1, (int)Math.Ceiling(_total / (double)PageSize));
|
||||
_page = Math.Clamp(_page, 1, _totalPages);
|
||||
|
||||
_sessions = await q
|
||||
.OrderByDescending(s => s.CreatedAt)
|
||||
.Skip((_page - 1) * PageSize)
|
||||
.Take(PageSize)
|
||||
.Select(s => new SessionRow(
|
||||
s.Id,
|
||||
s.PlayerOneAccountId,
|
||||
s.PlayerTwoAccountId,
|
||||
s.Status.ToString(),
|
||||
s.CreatedAt,
|
||||
s.EndedAt.HasValue && s.StartedAt.HasValue
|
||||
? FormatDurationEf(s.EndedAt.Value - s.StartedAt.Value)
|
||||
: "—",
|
||||
s.WinnerAccountId,
|
||||
s.WinCondition))
|
||||
.ToListAsync();
|
||||
|
||||
_loading = false;
|
||||
}
|
||||
|
||||
private async Task ChangePage(int p)
|
||||
{
|
||||
_page = p;
|
||||
await LoadAsync();
|
||||
}
|
||||
|
||||
private static string FormatDurationEf(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(GameSessionStatus.Initializing) => "bg-warning text-dark",
|
||||
nameof(GameSessionStatus.Active) => "bg-success",
|
||||
nameof(GameSessionStatus.Completed) => "bg-primary",
|
||||
nameof(GameSessionStatus.Abandoned) => "bg-secondary",
|
||||
_ => "bg-light text-dark",
|
||||
};
|
||||
}
|
||||
@@ -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",
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user