Files
sor/Server/src/SOR.Admin/Components/Pages/Game/GameSessionsPage.razor
T
58c5778bfb Remove stale per-page Authorize(Roles) attributes
All 26 pages had @attribute [Authorize(Roles = "Admin,SuperAdmin")] which
conflicts with the Keycloak role sor-admin. The fallback policy in Program.cs
already enforces RequireRole("sor-admin") globally, making these attributes
redundant. Removing them fixes Access Denied for all admin pages.

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-04-04 14:00:03 -04:00

292 lines
12 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
@page "/game/sessions"
@using Microsoft.EntityFrameworkCore
@using SOR.Database.Contexts
@using SOR.Database.Enums
@inject GameDbContext Db
@inject ChatDbContext ChatDb
@inject IJSRuntime JS
<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>
<div class="d-flex gap-2">
<AuthorizeView Roles="SuperAdmin">
<button class="btn btn-danger btn-sm" @onclick="PurgeAllAsync">
<i class="bi bi-fire me-1"></i>Purge All
</button>
</AuthorizeView>
<button class="btn btn-outline-danger btn-sm" @onclick="AbandonAllActiveAsync">
<i class="bi bi-x-circle me-1"></i>Abandon All Active
</button>
<button class="btn btn-outline-secondary btn-sm" @onclick="LoadAsync">
<i class="bi bi-arrow-clockwise me-1"></i>Refresh
</button>
</div>
</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>
<th></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">
<a href="/game/sessions/@s.Id" class="text-decoration-none">@s.Id.ToString()[..8]…</a>
</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>
<td>
@if (s.Status is nameof(GameSessionStatus.Active) or nameof(GameSessionStatus.Initializing))
{
<button class="btn btn-outline-danger btn-sm py-0"
@onclick="() => AbandonSessionAsync(s.Id)">
Abandon
</button>
}
</td>
</tr>
}
@if (!_sessions.Any())
{
<tr>
<td colspan="9" 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 async Task AbandonSessionAsync(Guid id)
{
var now = DateTimeOffset.UtcNow;
await Db.GameSessions
.Where(s => s.Id == id &&
(s.Status == GameSessionStatus.Active || s.Status == GameSessionStatus.Initializing))
.ExecuteUpdateAsync(s => s
.SetProperty(x => x.Status, GameSessionStatus.Abandoned)
.SetProperty(x => x.EndedAt, now)
.SetProperty(x => x.WinCondition, "admin"));
await LoadAsync();
}
private async Task PurgeAllAsync()
{
if (!await JS.InvokeAsync<bool>("confirm", "Permanently delete ALL sessions, action logs, and match chat channels?"))
return;
// Delete match chat channels (cascade removes members + messages).
await ChatDb.Channels
.Where(c => c.ChannelType == ChatChannelType.Match)
.ExecuteDeleteAsync();
// Cascade delete on action_logs means deleting sessions removes logs too.
await Db.GameSessions.ExecuteDeleteAsync();
_page = 1;
await LoadAsync();
}
private async Task AbandonAllActiveAsync()
{
var now = DateTimeOffset.UtcNow;
await Db.GameSessions
.Where(s => s.Status == GameSessionStatus.Active || s.Status == GameSessionStatus.Initializing)
.ExecuteUpdateAsync(s => s
.SetProperty(x => x.Status, GameSessionStatus.Abandoned)
.SetProperty(x => x.EndedAt, now)
.SetProperty(x => x.WinCondition, "admin"));
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",
};
}