274 lines
9.9 KiB
TypeScript
274 lines
9.9 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useState } from "react";
|
|
import { Flag, Trophy } from "lucide-react";
|
|
import {
|
|
Select,
|
|
SelectContent,
|
|
SelectItem,
|
|
SelectTrigger,
|
|
SelectValue,
|
|
} from "@/components/ui/select";
|
|
import { cn } from "@/lib/utils";
|
|
import type { EodDifficulty } from "@/app/(frontend)/eod/actions";
|
|
import type { PresenceParticipant } from "@/hooks/useRealtimePresence";
|
|
|
|
interface ApiUser {
|
|
id: number;
|
|
username?: string | null;
|
|
displayName?: string | null;
|
|
payloadDisplayName?: string | null;
|
|
}
|
|
|
|
interface ApiProfile {
|
|
id: number;
|
|
profileTitle?: string | null;
|
|
user?: ApiUser | number | null;
|
|
progression?: {
|
|
minigames?: {
|
|
eodMinesweeper?: {
|
|
gamesPlayed?: number | null;
|
|
wins?: number | null;
|
|
bestScore?: number | null;
|
|
xpEarned?: number | null;
|
|
bestTrainingTime?: number | null;
|
|
bestPerimeterTime?: number | null;
|
|
bestSupplyRoadTime?: number | null;
|
|
bestTrainingFlags?: number | null;
|
|
bestPerimeterFlags?: number | null;
|
|
bestSupplyRoadFlags?: number | null;
|
|
} | null;
|
|
} | null;
|
|
} | null;
|
|
}
|
|
|
|
interface LeaderboardRow {
|
|
id: number;
|
|
userId: number | null;
|
|
name: string;
|
|
gamesPlayed: number;
|
|
wins: number;
|
|
bestScore: number;
|
|
xpEarned: number;
|
|
bestTime: number;
|
|
bestFlags: number;
|
|
isCurrentUser: boolean;
|
|
isOutsideTopTen?: boolean;
|
|
}
|
|
|
|
interface EodLeaderboardProps {
|
|
refreshKey?: number;
|
|
currentUserId?: number | string;
|
|
activeTeams?: PresenceParticipant[];
|
|
difficulty: EodDifficulty;
|
|
onDifficultyChange: (difficulty: EodDifficulty) => void;
|
|
}
|
|
|
|
function rankClass(index: number) {
|
|
if (index === 0) return "font-bold text-amber-400";
|
|
if (index === 1) return "font-semibold text-slate-300";
|
|
if (index === 2) return "font-semibold text-amber-700";
|
|
return "text-muted-foreground";
|
|
}
|
|
|
|
export function EodLeaderboard({
|
|
refreshKey = 0,
|
|
currentUserId,
|
|
activeTeams = [],
|
|
difficulty,
|
|
onDifficultyChange,
|
|
}: EodLeaderboardProps) {
|
|
const [rows, setRows] = useState<LeaderboardRow[] | null>(null);
|
|
const [failed, setFailed] = useState(false);
|
|
|
|
useEffect(() => {
|
|
let cancelled = false;
|
|
setFailed(false);
|
|
|
|
const difficultyLabel =
|
|
difficulty === "training"
|
|
? "Training"
|
|
: difficulty === "perimeter"
|
|
? "Perimeter"
|
|
: "SupplyRoad";
|
|
const timeField = `progression.minigames.eodMinesweeper.best${difficultyLabel}Time`;
|
|
const flagsField = `progression.minigames.eodMinesweeper.best${difficultyLabel}Flags`;
|
|
const params = new URLSearchParams({
|
|
sort: `${timeField},${flagsField}`,
|
|
limit: "100",
|
|
depth: "1",
|
|
});
|
|
params.set(`where[${timeField}][greater_than]`, "0");
|
|
|
|
fetch(`/api/profiles?${params.toString()}`)
|
|
.then((res) => {
|
|
if (!res.ok) throw new Error("leaderboard fetch failed");
|
|
return res.json() as Promise<{ docs?: ApiProfile[] }>;
|
|
})
|
|
.then((data) => {
|
|
if (cancelled) return;
|
|
const mapped: LeaderboardRow[] = (data.docs ?? []).map((doc) => {
|
|
const eod = doc.progression?.minigames?.eodMinesweeper ?? {};
|
|
const user = typeof doc.user === "object" && doc.user !== null ? doc.user : null;
|
|
const name =
|
|
user?.displayName || doc.profileTitle || user?.username || `Operator #${doc.id}`;
|
|
return {
|
|
id: doc.id,
|
|
userId: user?.id ?? null,
|
|
name,
|
|
gamesPlayed: eod.gamesPlayed ?? 0,
|
|
wins: eod.wins ?? 0,
|
|
bestScore: eod.bestScore ?? 0,
|
|
xpEarned: eod.xpEarned ?? 0,
|
|
bestTime: eod[`best${difficultyLabel}Time` as keyof typeof eod] ?? 0,
|
|
bestFlags: eod[`best${difficultyLabel}Flags` as keyof typeof eod] ?? 0,
|
|
isCurrentUser: user ? String(user.id) === String(currentUserId) : false,
|
|
};
|
|
});
|
|
const scored = mapped
|
|
.filter((row) => row.bestTime > 0)
|
|
.sort((a, b) => a.bestTime - b.bestTime || a.bestFlags - b.bestFlags);
|
|
const visible = scored.slice(0, 10);
|
|
const current = mapped.find((row) => row.isCurrentUser);
|
|
if (current && !visible.some((row) => row.id === current.id)) {
|
|
visible.push({ ...current, isOutsideTopTen: true });
|
|
}
|
|
setRows(visible);
|
|
})
|
|
.catch(() => {
|
|
if (!cancelled) setFailed(true);
|
|
});
|
|
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}, [difficulty, refreshKey, currentUserId]);
|
|
|
|
return (
|
|
<div className="w-full max-w-[520px] rounded-xl border border-border bg-card">
|
|
<div className="flex items-center gap-2 border-b border-border px-4 py-3">
|
|
<Trophy className="size-4 text-amber-400" />
|
|
<h2 className="text-sm font-semibold">Minefield Clearance Leaderboard</h2>
|
|
<div className="ml-auto flex items-center gap-2 text-xs text-muted-foreground">
|
|
<Select
|
|
value={difficulty}
|
|
onValueChange={(value) => onDifficultyChange(value as EodDifficulty)}
|
|
>
|
|
<SelectTrigger aria-label="Leaderboard sector" className="h-8 w-[150px] text-xs">
|
|
<SelectValue />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem value="training">Training Pad</SelectItem>
|
|
<SelectItem value="perimeter">FOB Perimeter</SelectItem>
|
|
<SelectItem value="supplyRoad">Main Supply Road</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
<span>Top 10 · fastest clear</span>
|
|
</div>
|
|
</div>
|
|
<div
|
|
className="border-b border-border px-4 py-2 text-xs text-muted-foreground"
|
|
aria-live="polite"
|
|
>
|
|
<span className="font-medium text-emerald-400">
|
|
{activeTeams.length} defusal teams on site
|
|
</span>
|
|
{activeTeams.length > 0 && (
|
|
<span className="ml-1 text-foreground">
|
|
({activeTeams.map((team) => team.name).join(", ")})
|
|
</span>
|
|
)}
|
|
</div>
|
|
|
|
{failed ? (
|
|
<p className="px-4 py-4 text-sm text-muted-foreground">Could not load the leaderboard.</p>
|
|
) : rows === null ? (
|
|
<div className="space-y-2 px-4 py-4">
|
|
<div className="h-4 w-full animate-pulse rounded bg-border" />
|
|
<div className="h-4 w-3/4 animate-pulse rounded bg-border" />
|
|
<div className="h-4 w-1/2 animate-pulse rounded bg-border" />
|
|
</div>
|
|
) : rows.length === 0 ? (
|
|
<p className="px-4 py-4 text-sm text-muted-foreground">
|
|
No clearances logged yet. Take the first sector!
|
|
</p>
|
|
) : (
|
|
<div className="overflow-x-auto">
|
|
<table className="w-full text-sm">
|
|
<thead>
|
|
<tr className="text-left text-xs uppercase tracking-wider text-muted-foreground">
|
|
<th className="px-4 py-2 font-medium">#</th>
|
|
<th className="px-2 py-2 font-medium">Operator</th>
|
|
<th className="px-2 py-2 text-right font-medium">Games</th>
|
|
<th className="px-2 py-2 text-right font-medium">Wins</th>
|
|
<th className="px-2 py-2 text-right font-medium">Time</th>
|
|
<th className="px-2 py-2 text-right font-medium">
|
|
<Flag className="ml-auto size-3" aria-label="Flags" />
|
|
</th>
|
|
<th className="px-4 py-2 text-right font-medium">XP</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody className="divide-y divide-border">
|
|
{rows.map((row, index) => (
|
|
<tr
|
|
key={row.id}
|
|
className={cn(
|
|
row.isCurrentUser && "bg-accent/40",
|
|
row.isOutsideTopTen && "text-muted-foreground opacity-70",
|
|
)}
|
|
title={
|
|
row.isOutsideTopTen
|
|
? "Operator status (not currently ranked in the top 10)"
|
|
: undefined
|
|
}
|
|
>
|
|
<td className="px-4 py-2">
|
|
<span className={rankClass(index)}>
|
|
{row.isOutsideTopTen ? "-" : index + 1}
|
|
</span>
|
|
</td>
|
|
<td className="px-2 py-2 font-medium">
|
|
<span className="flex items-center gap-2">
|
|
<span className="max-w-[180px] truncate">{row.name}</span>
|
|
{row.isCurrentUser && (
|
|
<span className="shrink-0 text-xs text-muted-foreground">(you)</span>
|
|
)}
|
|
{row.isOutsideTopTen && (
|
|
<span className="shrink-0 text-[10px] font-normal uppercase tracking-wide text-muted-foreground">
|
|
unranked
|
|
</span>
|
|
)}
|
|
{row.userId !== null &&
|
|
activeTeams.some((team) => team.id === row.userId) && (
|
|
<span
|
|
className="size-2 rounded-full bg-emerald-400"
|
|
title="Clearing now"
|
|
aria-label="Clearing now"
|
|
/>
|
|
)}
|
|
</span>
|
|
</td>
|
|
<td className="px-2 py-2 text-right tabular-nums text-muted-foreground">
|
|
{row.gamesPlayed}
|
|
</td>
|
|
<td className="px-2 py-2 text-right tabular-nums text-muted-foreground">
|
|
{row.wins}
|
|
</td>
|
|
<td className="px-2 py-2 text-right font-semibold tabular-nums">
|
|
{row.bestTime}s
|
|
</td>
|
|
<td className="px-2 py-2 text-right font-semibold tabular-nums">
|
|
{row.bestFlags}
|
|
</td>
|
|
<td className="px-4 py-2 text-right tabular-nums text-muted-foreground">
|
|
{row.xpEarned}
|
|
</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|