Feat: game explorer, action log result capture, session purge

DB / Server:
- Added ResultJson (jsonb, nullable) to GameActionLog entity + EF migration
- Updated SaveStateAsync to accept resultJson (optional, default null)
- All 11 action handlers now serialize GameEngine.ToDto() result into the log

Admin — GameSessionsPage:
- Session IDs are now clickable links → /game/sessions/{id}
- "Abandon All Active" bulk button (previous)
- "Purge All" button (SuperAdmin only, JS confirm) — hard-deletes all sessions
  and action logs via cascade

Admin — GameSessionDetailPage (/game/sessions/{id}):
- Session header: status badge, P1/P2 display names (click to copy GUID)
- Info cards: Player 1, Player 2, Created timestamp, Duration/WinCondition
- Action log table: seq#, time, actor name (click to copy GUID), action type
- Expandable Payload (JSON) and Result (JSON) per row
- Actor display names resolved from SorIdentityDbContext

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-03-09 09:33:40 -04:00
co-authored by Claude Happy
parent 60697460cb
commit 4fa4c21b6e
18 changed files with 376 additions and 25 deletions
@@ -0,0 +1,283 @@
@page "/game/sessions/{Id:guid}"
@attribute [Authorize(Roles = "Admin,SuperAdmin")]
@using Microsoft.EntityFrameworkCore
@using SOR.Database.Contexts
@using SOR.Database.Enums
@inject GameDbContext Db
@inject SorIdentityDbContext IdentityDb
<PageTitle>Game Session – SOR Admin</PageTitle>
@if (_loading)
{
<p class="text-muted">Loading…</p>
}
else if (_session is null)
{
<p class="text-danger">Session not found.</p>
}
else
{
<!-- Header -->
<div class="d-flex justify-content-between align-items-start mb-4">
<div>
<a href="/game/sessions" class="text-muted small text-decoration-none">← Sessions</a>
<h1 class="mb-0 mt-1">Game Explorer</h1>
<div class="font-monospace text-muted small mt-1"
style="cursor:pointer" title="Click to copy"
@onclick='() => CopyToClipboard(Id.ToString())'>
@Id — <span class="badge @StatusBadge(_session.Status)">@_session.Status</span>
</div>
</div>
</div>
<!-- Session info cards -->
<div class="row g-3 mb-4">
<div class="col-md-3">
<div class="card shadow-sm py-2 px-3">
<div class="small text-muted">Player 1</div>
<div class="fw-semibold">
<span style="cursor:pointer" title="Click to copy full ID"
@onclick='() => CopyToClipboard(_session.P1Id)'>
@(_session.P1Name ?? _session.P1Id[..8] + "…")
</span>
</div>
</div>
</div>
<div class="col-md-3">
<div class="card shadow-sm py-2 px-3">
<div class="small text-muted">Player 2</div>
<div class="fw-semibold">
<span style="cursor:pointer" title="Click to copy full ID"
@onclick='() => CopyToClipboard(_session.P2Id)'>
@(_session.P2Name ?? _session.P2Id[..8] + "…")
</span>
</div>
</div>
</div>
<div class="col-md-3">
<div class="card shadow-sm py-2 px-3">
<div class="small text-muted">Created</div>
<div class="fw-semibold">@_session.CreatedAt.ToString("yyyy-MM-dd HH:mm:ss")</div>
</div>
</div>
<div class="col-md-3">
<div class="card shadow-sm py-2 px-3">
<div class="small text-muted">Duration / Result</div>
<div class="fw-semibold">@_session.Duration</div>
@if (_session.WinCondition is not null)
{
<div class="small text-muted">@_session.WinCondition</div>
}
</div>
</div>
</div>
<!-- Action log -->
<div class="card shadow-sm">
<div class="card-header fw-medium d-flex justify-content-between">
<span>Action Log</span>
<span class="badge bg-secondary">@_logs.Count entries</span>
</div>
<div class="card-body p-0">
<table class="table table-sm table-hover mb-0">
<thead class="table-light">
<tr>
<th style="width:48px">#</th>
<th style="width:90px">Time</th>
<th style="width:160px">Actor</th>
<th>Action</th>
<th style="width:80px">Payload</th>
<th style="width:80px">Result</th>
</tr>
</thead>
<tbody>
@foreach (var log in _logs)
{
<tr>
<td class="text-muted font-monospace small">@log.Seq</td>
<td class="text-muted small">@log.Time</td>
<td>
<span class="small" style="cursor:pointer" title="Click to copy GUID"
@onclick='() => CopyToClipboard(log.ActorId)'>
@(log.ActorName ?? log.ActorId[..8] + "…")
</span>
</td>
<td class="font-monospace small">@log.ActionType</td>
<td>
@if (log.Payload is not null)
{
<button class="btn btn-outline-secondary btn-sm py-0 px-2"
@onclick="() => ToggleExpand(log, true)">
@(log.PayloadExpanded ? "hide" : "JSON")
</button>
}
</td>
<td>
@if (log.Result is not null)
{
<button class="btn btn-outline-primary btn-sm py-0 px-2"
@onclick="() => ToggleExpand(log, false)">
@(log.ResultExpanded ? "hide" : "JSON")
</button>
}
</td>
</tr>
@if (log.PayloadExpanded && log.Payload is not null)
{
<tr>
<td colspan="6" class="p-0">
<pre class="m-0 p-3 bg-light small" style="white-space:pre-wrap;word-break:break-all;max-height:300px;overflow:auto">@log.Payload</pre>
</td>
</tr>
}
@if (log.ResultExpanded && log.Result is not null)
{
<tr>
<td colspan="6" class="p-0">
<pre class="m-0 p-3 bg-light small" style="white-space:pre-wrap;word-break:break-all;max-height:300px;overflow:auto">@log.Result</pre>
</td>
</tr>
}
}
@if (!_logs.Any())
{
<tr>
<td colspan="6" class="text-muted text-center py-3">No action logs.</td>
</tr>
}
</tbody>
</table>
</div>
</div>
}
<!-- JS for copy-to-clipboard -->
<script>
window.copyText = (text) => navigator.clipboard.writeText(text);
</script>
@code {
[Parameter] public Guid Id { get; set; }
[Inject] private IJSRuntime JS { get; set; } = default!;
private bool _loading = true;
private SessionInfo? _session;
private List<LogRow> _logs = new();
private sealed class SessionInfo
{
public string P1Id { get; init; } = "";
public string? P1Name { get; init; }
public string P2Id { get; init; } = "";
public string? P2Name { get; init; }
public string Status { get; init; } = "";
public DateTimeOffset CreatedAt { get; init; }
public string Duration { get; init; } = "—";
public string? WinCondition { get; init; }
}
private sealed class LogRow
{
public int Seq { get; init; }
public string Time { get; init; } = "";
public string ActorId { get; init; } = "";
public string? ActorName { get; init; }
public string ActionType { get; init; } = "";
public string? Payload { get; init; }
public string? Result { get; init; }
public bool PayloadExpanded { get; set; }
public bool ResultExpanded { get; set; }
}
protected override async Task OnInitializedAsync()
{
var session = await Db.GameSessions.FindAsync(Id);
if (session is null) { _loading = false; return; }
// Resolve display names for both players.
var playerIds = new[] { session.PlayerOneAccountId, session.PlayerTwoAccountId };
var accounts = await IdentityDb.Users
.Where(u => playerIds.Contains(u.Id))
.Select(u => new { u.Id, u.DisplayName, u.Email })
.ToListAsync();
string NameOf(Guid id)
{
var a = accounts.FirstOrDefault(x => x.Id == id);
return a?.DisplayName ?? a?.Email ?? id.ToString();
}
_session = new SessionInfo
{
P1Id = session.PlayerOneAccountId.ToString(),
P1Name = NameOf(session.PlayerOneAccountId),
P2Id = session.PlayerTwoAccountId.ToString(),
P2Name = NameOf(session.PlayerTwoAccountId),
Status = session.Status.ToString(),
CreatedAt = session.CreatedAt,
Duration = session.EndedAt.HasValue && session.StartedAt.HasValue
? FormatDuration(session.EndedAt.Value - session.StartedAt.Value)
: "ongoing",
WinCondition = session.WinCondition,
};
// Load action logs ordered by sequence.
var rawLogs = await Db.ActionLogs
.Where(l => l.GameSessionId == Id)
.OrderBy(l => l.SequenceNumber)
.ToListAsync();
// Bulk-resolve actor display names.
var actorIds = rawLogs.Select(l => l.ActorAccountId).Distinct().ToArray();
var actorAccounts = await IdentityDb.Users
.Where(u => actorIds.Contains(u.Id))
.Select(u => new { u.Id, u.DisplayName, u.Email })
.ToListAsync();
string ActorName(Guid id)
{
var a = actorAccounts.FirstOrDefault(x => x.Id == id);
return a?.DisplayName ?? a?.Email ?? id.ToString();
}
_logs = rawLogs.Select(l => new LogRow
{
Seq = l.SequenceNumber,
Time = l.OccurredAt.ToString("HH:mm:ss"),
ActorId = l.ActorAccountId.ToString(),
ActorName = ActorName(l.ActorAccountId),
ActionType = l.ActionType,
Payload = l.PayloadJson,
Result = l.ResultJson,
}).ToList();
_loading = false;
}
private void ToggleExpand(LogRow row, bool isPayload)
{
if (isPayload) row.PayloadExpanded = !row.PayloadExpanded;
else row.ResultExpanded = !row.ResultExpanded;
}
private async Task CopyToClipboard(string text)
=> await JS.InvokeVoidAsync("copyText", text);
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(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",
};
}
@@ -4,12 +4,18 @@
@using SOR.Database.Contexts
@using SOR.Database.Enums
@inject GameDbContext Db
@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>
@@ -106,7 +112,9 @@ else
{
<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">
<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>
@@ -239,6 +247,16 @@ else
await LoadAsync();
}
private async Task PurgeAllAsync()
{
if (!await JS.InvokeAsync<bool>("confirm", "Permanently delete ALL sessions and action logs?"))
return;
// 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;