feat(admin): end AI matches from admin portal
AiMatchService: - GetActiveMatchesAsync: queries CustomLobbies joined to GameSessions to return all running AI vs AI sessions with display names. - ForceEndMatchAsync: marks session Abandoned + lobby Completed; the AI polling loops detect the status change within ~1 s and exit cleanly. - ActiveAiMatch DTO returned by both methods. AiMatchPage: - Active AI Matches section loads on page init and has a Refresh button; each row has an End button that calls ForceEndMatchAsync and refreshes. - Created This Session table also gets an End button per row. - Spinner shown on the End button while the request is in flight. 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:
@@ -228,7 +228,60 @@
|
|||||||
<button class="btn btn-outline-secondary" @onclick="ResetRules">Reset Rules to Default</button>
|
<button class="btn btn-outline-secondary" @onclick="ResetRules">Reset Rules to Default</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Recent matches -->
|
<!-- Active AI matches (from DB) -->
|
||||||
|
<div class="card mb-4">
|
||||||
|
<div class="card-header d-flex justify-content-between align-items-center">
|
||||||
|
<span>Active AI Matches</span>
|
||||||
|
<button class="btn btn-sm btn-outline-secondary" @onclick="RefreshActiveAsync" disabled="@_loadingActive">
|
||||||
|
@if (_loadingActive) { <span class="spinner-border spinner-border-sm me-1"></span> }
|
||||||
|
Refresh
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
@if (_activeMatches.Count == 0)
|
||||||
|
{
|
||||||
|
<div class="card-body text-muted small">No active AI matches.</div>
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
<div class="table-responsive">
|
||||||
|
<table class="table table-sm table-hover mb-0">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Session ID</th>
|
||||||
|
<th>Player 1</th>
|
||||||
|
<th>Player 2</th>
|
||||||
|
<th>Status</th>
|
||||||
|
<th>Started</th>
|
||||||
|
<th></th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
@foreach (var m in _activeMatches)
|
||||||
|
{
|
||||||
|
<tr>
|
||||||
|
<td><span class="font-monospace small">@m.SessionId.ToString()[..8]…</span></td>
|
||||||
|
<td>@m.Player1Name</td>
|
||||||
|
<td>@m.Player2Name</td>
|
||||||
|
<td><span class="badge bg-success">@m.Status</span></td>
|
||||||
|
<td class="text-muted small">@m.CreatedAt.ToString("HH:mm:ss")</td>
|
||||||
|
<td>
|
||||||
|
<button class="btn btn-sm btn-outline-danger"
|
||||||
|
@onclick="() => EndMatchAsync(m.SessionId)"
|
||||||
|
disabled="@_endingSessionIds.Contains(m.SessionId)">
|
||||||
|
@if (_endingSessionIds.Contains(m.SessionId))
|
||||||
|
{ <span class="spinner-border spinner-border-sm"></span> }
|
||||||
|
else { <span>End</span> }
|
||||||
|
</button>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Recent matches created this session -->
|
||||||
@if (_recentMatches.Count > 0)
|
@if (_recentMatches.Count > 0)
|
||||||
{
|
{
|
||||||
<div class="card">
|
<div class="card">
|
||||||
@@ -241,20 +294,26 @@
|
|||||||
<th>Player 1</th>
|
<th>Player 1</th>
|
||||||
<th>Player 2</th>
|
<th>Player 2</th>
|
||||||
<th>Created</th>
|
<th>Created</th>
|
||||||
|
<th></th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
@foreach (var m in _recentMatches)
|
@foreach (var m in _recentMatches)
|
||||||
{
|
{
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td><span class="font-monospace small">@m.SessionId.ToString()[..8]…</span></td>
|
||||||
<a href="/game/sessions/@m.SessionId" class="font-monospace small">
|
|
||||||
@m.SessionId.ToString()[..8]…
|
|
||||||
</a>
|
|
||||||
</td>
|
|
||||||
<td>@m.Player1Name</td>
|
<td>@m.Player1Name</td>
|
||||||
<td>@m.Player2Name</td>
|
<td>@m.Player2Name</td>
|
||||||
<td class="text-muted small">@m.CreatedAt.ToString("HH:mm:ss")</td>
|
<td class="text-muted small">@m.CreatedAt.ToString("HH:mm:ss")</td>
|
||||||
|
<td>
|
||||||
|
<button class="btn btn-sm btn-outline-danger"
|
||||||
|
@onclick="() => EndMatchAsync(m.SessionId)"
|
||||||
|
disabled="@_endingSessionIds.Contains(m.SessionId)">
|
||||||
|
@if (_endingSessionIds.Contains(m.SessionId))
|
||||||
|
{ <span class="spinner-border spinner-border-sm"></span> }
|
||||||
|
else { <span>End</span> }
|
||||||
|
</button>
|
||||||
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
}
|
}
|
||||||
</tbody>
|
</tbody>
|
||||||
@@ -276,7 +335,37 @@
|
|||||||
private bool _busy;
|
private bool _busy;
|
||||||
private string? _error;
|
private string? _error;
|
||||||
|
|
||||||
private readonly List<MatchRecord> _recentMatches = [];
|
private readonly List<MatchRecord> _recentMatches = [];
|
||||||
|
private List<AiMatchService.ActiveAiMatch> _activeMatches = [];
|
||||||
|
private readonly HashSet<Guid> _endingSessionIds = [];
|
||||||
|
private bool _loadingActive;
|
||||||
|
|
||||||
|
protected override async Task OnInitializedAsync()
|
||||||
|
=> await RefreshActiveAsync();
|
||||||
|
|
||||||
|
private async Task RefreshActiveAsync()
|
||||||
|
{
|
||||||
|
_loadingActive = true; StateHasChanged();
|
||||||
|
try { _activeMatches = (await MatchService.GetActiveMatchesAsync()).ToList(); }
|
||||||
|
catch (Exception ex) { _error = "Failed to load active matches: " + ex.Message; }
|
||||||
|
finally { _loadingActive = false; StateHasChanged(); }
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task EndMatchAsync(Guid sessionId)
|
||||||
|
{
|
||||||
|
_endingSessionIds.Add(sessionId); StateHasChanged();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await MatchService.ForceEndMatchAsync(sessionId);
|
||||||
|
await RefreshActiveAsync();
|
||||||
|
}
|
||||||
|
catch (Exception ex) { _error = ex.Message; }
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_endingSessionIds.Remove(sessionId);
|
||||||
|
StateHasChanged();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static string DifficultyName(int d) => d switch
|
private static string DifficultyName(int d) => d switch
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -40,6 +40,94 @@ public sealed class AiMatchService
|
|||||||
_logger = logger;
|
_logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ── Active match query ────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns all game sessions that were started from an AI-vs-AI lobby
|
||||||
|
/// (i.e. the linked CustomLobby has AiSlotsJson set) and are still active.
|
||||||
|
/// </summary>
|
||||||
|
public async Task<IReadOnlyList<ActiveAiMatch>> GetActiveMatchesAsync(
|
||||||
|
CancellationToken ct = default)
|
||||||
|
{
|
||||||
|
// Find active sessions linked to lobbies that have AiSlotsJson populated.
|
||||||
|
var rows = await _gameDb.CustomLobbies
|
||||||
|
.AsNoTracking()
|
||||||
|
.Where(l => l.AiSlotsJson != null
|
||||||
|
&& l.GameSessionId != null
|
||||||
|
&& l.Status == CustomLobbyStatus.Started)
|
||||||
|
.Join(_gameDb.GameSessions.AsNoTracking()
|
||||||
|
.Where(s => s.Status == GameSessionStatus.Active
|
||||||
|
|| s.Status == GameSessionStatus.Initializing),
|
||||||
|
l => l.GameSessionId,
|
||||||
|
s => (Guid?)s.Id,
|
||||||
|
(l, s) => new { Lobby = l, Session = s })
|
||||||
|
.ToListAsync(ct);
|
||||||
|
|
||||||
|
if (rows.Count == 0) return [];
|
||||||
|
|
||||||
|
// Resolve display names from identity DB.
|
||||||
|
var allIds = rows.SelectMany(r => new[] { r.Session.PlayerOneAccountId, r.Session.PlayerTwoAccountId })
|
||||||
|
.Distinct().ToList();
|
||||||
|
var names = await _identityDb.Users
|
||||||
|
.AsNoTracking()
|
||||||
|
.Where(u => allIds.Contains(u.Id))
|
||||||
|
.Select(u => new { u.Id, Name = u.DisplayName ?? u.UserName ?? u.Id.ToString().Substring(0, 8) })
|
||||||
|
.ToDictionaryAsync(u => u.Id, u => u.Name, ct);
|
||||||
|
|
||||||
|
return rows.Select(r => new ActiveAiMatch(
|
||||||
|
r.Session.Id,
|
||||||
|
r.Session.PlayerOneAccountId,
|
||||||
|
names.GetValueOrDefault(r.Session.PlayerOneAccountId, r.Session.PlayerOneAccountId.ToString()[..8]),
|
||||||
|
r.Session.PlayerTwoAccountId,
|
||||||
|
names.GetValueOrDefault(r.Session.PlayerTwoAccountId, r.Session.PlayerTwoAccountId.ToString()[..8]),
|
||||||
|
r.Session.Status,
|
||||||
|
r.Session.CreatedAt,
|
||||||
|
r.Lobby.Name ?? "AI Match")).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Force-end ─────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Immediately terminates an active AI match by marking the session Abandoned
|
||||||
|
/// and the linked lobby Completed. The AI polling loops will detect the status
|
||||||
|
/// change on their next poll (within ~1 s) and exit cleanly.
|
||||||
|
/// </summary>
|
||||||
|
public async Task ForceEndMatchAsync(Guid sessionId, CancellationToken ct = default)
|
||||||
|
{
|
||||||
|
var session = await _gameDb.GameSessions
|
||||||
|
.FirstOrDefaultAsync(s => s.Id == sessionId, ct)
|
||||||
|
?? throw new InvalidOperationException($"Session {sessionId} not found.");
|
||||||
|
|
||||||
|
if (session.Status is GameSessionStatus.Completed or GameSessionStatus.Abandoned)
|
||||||
|
throw new InvalidOperationException("Session is already finished.");
|
||||||
|
|
||||||
|
session.Status = GameSessionStatus.Abandoned;
|
||||||
|
session.EndedAt = DateTimeOffset.UtcNow;
|
||||||
|
|
||||||
|
var lobby = await _gameDb.CustomLobbies
|
||||||
|
.FirstOrDefaultAsync(l => l.GameSessionId == sessionId, ct);
|
||||||
|
if (lobby != null)
|
||||||
|
lobby.Status = CustomLobbyStatus.Completed;
|
||||||
|
|
||||||
|
await _gameDb.SaveChangesAsync(ct);
|
||||||
|
|
||||||
|
_logger.LogInformation("AiMatchService: force-ended session {SessionId}.", sessionId);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── DTOs ──────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
public sealed record ActiveAiMatch(
|
||||||
|
Guid SessionId,
|
||||||
|
Guid Player1Id,
|
||||||
|
string Player1Name,
|
||||||
|
Guid Player2Id,
|
||||||
|
string Player2Name,
|
||||||
|
GameSessionStatus Status,
|
||||||
|
DateTimeOffset CreatedAt,
|
||||||
|
string LobbyName);
|
||||||
|
|
||||||
|
// ── Match creation ────────────────────────────────────────────────────────
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates an AI-vs-AI match and publishes GameStartedEvent.
|
/// Creates an AI-vs-AI match and publishes GameStartedEvent.
|
||||||
/// Returns (sessionId, player1Name, player2Name).
|
/// Returns (sessionId, player1Name, player2Name).
|
||||||
|
|||||||
Reference in New Issue
Block a user