add admin actions + set player rating at signup
This commit is contained in:
@@ -1,8 +1,18 @@
|
||||
// src/routes/(app)/admin/+page.server.ts
|
||||
import { db } from '$lib/server/db';
|
||||
import { user } from '$lib/server/db/auth.schema';
|
||||
import { playerRating } from '$lib/server/db/schema';
|
||||
import { eq, desc } from 'drizzle-orm';
|
||||
import {
|
||||
playerRating,
|
||||
match,
|
||||
matchQueue,
|
||||
matchInvite,
|
||||
notification,
|
||||
matchGame,
|
||||
matchGameScore,
|
||||
matchParticipant,
|
||||
matchParticipantPlayer
|
||||
} from '$lib/server/db/schema';
|
||||
import { eq, desc, inArray, sql } from 'drizzle-orm';
|
||||
import { requireRole, setUserRole, isRootAdmin } from '$lib/server/permissions';
|
||||
import type { PageServerLoad, Actions } from './$types';
|
||||
|
||||
@@ -33,9 +43,26 @@ export const load: PageServerLoad = async ({ locals }) => {
|
||||
})
|
||||
);
|
||||
|
||||
const [activeMatches, queueSize, pendingInvites] = await Promise.all([
|
||||
db
|
||||
.select({ count: sql<number>`count(*)` })
|
||||
.from(match)
|
||||
.where(inArray(match.state, ['CREATED', 'LOBBY', 'ROLLING', 'PICKING', 'PLAYING'])),
|
||||
db.select({ count: sql<number>`count(*)` }).from(matchQueue),
|
||||
db
|
||||
.select({ count: sql<number>`count(*)` })
|
||||
.from(matchInvite)
|
||||
.where(eq(matchInvite.status, 'pending'))
|
||||
]);
|
||||
|
||||
return {
|
||||
users: usersWithRatings,
|
||||
actorIsRootAdmin: isRootAdmin(locals.user!.email)
|
||||
actorIsRootAdmin: isRootAdmin(locals.user!.email),
|
||||
stats: {
|
||||
activeMatches: Number(activeMatches[0].count),
|
||||
queueSize: Number(queueSize[0].count),
|
||||
pendingInvites: Number(pendingInvites[0].count)
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
@@ -60,5 +87,66 @@ export const actions: Actions = {
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
},
|
||||
|
||||
cancelAllMatches: async ({ locals }) => {
|
||||
requireRole(locals, 'admin');
|
||||
|
||||
const cancelled = await db
|
||||
.update(match)
|
||||
.set({ state: 'CANCELLED', finishedAt: new Date() })
|
||||
.where(inArray(match.state, ['CREATED', 'LOBBY', 'ROLLING', 'PICKING', 'PLAYING']))
|
||||
.returning({ id: match.id });
|
||||
|
||||
await db.delete(matchQueue);
|
||||
|
||||
return { success: true, message: `Cancelled ${cancelled.length} match${cancelled.length !== 1 ? 'es' : ''} and cleared queue` };
|
||||
},
|
||||
|
||||
clearQueue: async ({ locals }) => {
|
||||
requireRole(locals, 'admin');
|
||||
const deleted = await db.delete(matchQueue).returning({ id: matchQueue.id });
|
||||
return { success: true, message: `Removed ${deleted.length} queue entr${deleted.length !== 1 ? 'ies' : 'y'}` };
|
||||
},
|
||||
|
||||
expireInvites: async ({ locals }) => {
|
||||
requireRole(locals, 'admin');
|
||||
const expired = await db
|
||||
.update(matchInvite)
|
||||
.set({ status: 'expired' })
|
||||
.where(eq(matchInvite.status, 'pending'))
|
||||
.returning({ id: matchInvite.id });
|
||||
return { success: true, message: `Expired ${expired.length} pending invite${expired.length !== 1 ? 's' : ''}` };
|
||||
},
|
||||
|
||||
clearNotifications: async ({ locals }) => {
|
||||
requireRole(locals, 'admin');
|
||||
const deleted = await db.delete(notification).returning({ id: notification.id });
|
||||
return { success: true, message: `Cleared ${deleted.length} notification${deleted.length !== 1 ? 's' : ''}` };
|
||||
},
|
||||
|
||||
resetRatings: async ({ locals }) => {
|
||||
requireRole(locals, 'admin');
|
||||
const reset = await db
|
||||
.update(playerRating)
|
||||
.set({ elo: sql`COALESCE(initial_elo, 1000)`, wins: 0, losses: 0, updatedAt: new Date() })
|
||||
.returning({ id: playerRating.id });
|
||||
return { success: true, message: `Reset ${reset.length} rating${reset.length !== 1 ? 's' : ''} to initial ELO` };
|
||||
},
|
||||
|
||||
clearMatchHistory: async ({ locals }) => {
|
||||
requireRole(locals, 'admin');
|
||||
|
||||
await db.delete(matchGameScore);
|
||||
await db.delete(matchGame);
|
||||
await db.delete(matchParticipantPlayer);
|
||||
await db.delete(matchParticipant);
|
||||
await db.update(match).set({ winnerId: null });
|
||||
await db.delete(match);
|
||||
await db.delete(matchQueue);
|
||||
await db.delete(matchInvite);
|
||||
await db.delete(notification);
|
||||
|
||||
return { success: true, message: 'Cleared all match data, invites, and notifications' };
|
||||
}
|
||||
};
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
let { data } = $props();
|
||||
|
||||
let actionError = $state('');
|
||||
let actionSuccess = $state('');
|
||||
|
||||
const roleBadge: Record<string, { label: string; color: string }> = {
|
||||
player: { label: 'Player', color: 'bg-surface-600 text-text-secondary border-border' },
|
||||
@@ -36,6 +37,9 @@
|
||||
{#if actionError}
|
||||
<p class="mt-3 text-sm text-red-400">{actionError}</p>
|
||||
{/if}
|
||||
{#if actionSuccess}
|
||||
<p class="mt-3 text-sm text-green-400">{actionSuccess}</p>
|
||||
{/if}
|
||||
|
||||
<!-- Users Table -->
|
||||
<div class="mt-6 rounded-lg border border-border bg-surface-800">
|
||||
@@ -126,6 +130,53 @@
|
||||
{/each}
|
||||
</div>
|
||||
|
||||
<!-- Admin Tools -->
|
||||
<div class="mt-6 rounded-lg border border-border bg-surface-800 p-5">
|
||||
<h2 class="text-sm font-600">Tools</h2>
|
||||
|
||||
<!-- Stats -->
|
||||
<div class="mt-3 flex gap-4 text-xs text-text-secondary">
|
||||
<span>{data.stats.activeMatches} active match{data.stats.activeMatches !== 1 ? 'es' : ''}</span>
|
||||
<span>{data.stats.queueSize} in queue</span>
|
||||
<span>{data.stats.pendingInvites} pending invite{data.stats.pendingInvites !== 1 ? 's' : ''}</span>
|
||||
</div>
|
||||
|
||||
<!-- Actions -->
|
||||
<div class="mt-4 flex flex-wrap gap-2">
|
||||
{#each [
|
||||
{ action: 'cancelAllMatches', label: 'Cancel All Matches', confirm: 'Cancel all active matches and clear the queue?', color: 'border-yellow-500/30 text-yellow-400 hover:bg-yellow-500/10' },
|
||||
{ action: 'clearQueue', label: 'Clear Queue', confirm: 'Remove everyone from the queue?', color: 'border-border text-text-secondary hover:bg-surface-700' },
|
||||
{ action: 'expireInvites', label: 'Expire All Invites', confirm: 'Expire all pending invites?', color: 'border-border text-text-secondary hover:bg-surface-700' },
|
||||
{ action: 'clearNotifications', label: 'Clear Notifications', confirm: 'Delete all notifications?', color: 'border-border text-text-secondary hover:bg-surface-700' },
|
||||
{ action: 'resetRatings', label: 'Reset Ratings', confirm: 'Reset all ELO ratings to initial values? This cannot be undone.', color: 'border-red-500/30 text-red-400 hover:bg-red-500/10' },
|
||||
{ action: 'clearMatchHistory', label: 'Clear All Match Data', confirm: 'Delete ALL matches, scores, invites, and notifications? This cannot be undone.', color: 'border-red-500/30 text-red-400 hover:bg-red-500/10' },
|
||||
] as tool}
|
||||
<form
|
||||
method="post"
|
||||
action="?/{tool.action}"
|
||||
use:enhance={() => {
|
||||
actionError = '';
|
||||
actionSuccess = '';
|
||||
return async ({ result, update }) => {
|
||||
if (result.type === 'success' && (result.data as any)?.message) {
|
||||
actionSuccess = (result.data as any).message;
|
||||
await update();
|
||||
} else if (result.type === 'success' && (result.data as any)?.error) {
|
||||
actionError = (result.data as any).error;
|
||||
}
|
||||
};
|
||||
}}
|
||||
>
|
||||
<button
|
||||
type="submit"
|
||||
onclick={(e) => { if (!confirm(tool.confirm)) e.preventDefault(); }}
|
||||
class="rounded-md border px-3 py-1.5 text-xs font-500 transition-colors {tool.color}"
|
||||
>{tool.label}</button>
|
||||
</form>
|
||||
{/each}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Permission Info -->
|
||||
<div class="mt-6 rounded-lg border border-border bg-surface-800 p-5">
|
||||
<h2 class="text-sm font-600">Permission Levels</h2>
|
||||
|
||||
Reference in New Issue
Block a user