Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
197 lines
7.5 KiB
TypeScript
197 lines
7.5 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useState } from "react";
|
|
import { Trophy } from "lucide-react";
|
|
import { cn } from "@/lib/utils";
|
|
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?: {
|
|
flappyBird?: {
|
|
gamesPlayed?: number | null;
|
|
totalPasses?: number | null;
|
|
bestScore?: number | null;
|
|
xpEarned?: number | null;
|
|
} | null;
|
|
} | null;
|
|
} | null;
|
|
}
|
|
|
|
interface LeaderboardRow {
|
|
id: number;
|
|
userId: number | null;
|
|
name: string;
|
|
gamesPlayed: number;
|
|
totalPasses: number;
|
|
bestScore: number;
|
|
xpEarned: number;
|
|
isCurrentUser: boolean;
|
|
isOutsideTopTen?: boolean;
|
|
}
|
|
|
|
interface FlappyLeaderboardProps {
|
|
refreshKey?: number;
|
|
currentUserId?: number | string;
|
|
activePilots?: PresenceParticipant[];
|
|
}
|
|
|
|
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 FlappyLeaderboard({ refreshKey = 0, currentUserId, activePilots = [] }: FlappyLeaderboardProps) {
|
|
const [rows, setRows] = useState<LeaderboardRow[] | null>(null);
|
|
const [failed, setFailed] = useState(false);
|
|
|
|
useEffect(() => {
|
|
let cancelled = false;
|
|
setFailed(false);
|
|
|
|
const params = new URLSearchParams({
|
|
sort: "-progression.minigames.flappyBird.bestScore",
|
|
// Fetch enough profiles to keep the current pilot visible even when
|
|
// they have not completed a scored run yet.
|
|
limit: "100",
|
|
depth: "1",
|
|
});
|
|
|
|
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 flappy = doc.progression?.minigames?.flappyBird ?? {};
|
|
const user = typeof doc.user === "object" && doc.user !== null ? doc.user : null;
|
|
const name =
|
|
user?.displayName || doc.profileTitle || user?.username || `Pilot #${doc.id}`;
|
|
return {
|
|
id: doc.id,
|
|
userId: user?.id ?? null,
|
|
name,
|
|
gamesPlayed: flappy.gamesPlayed ?? 0,
|
|
totalPasses: flappy.totalPasses ?? 0,
|
|
bestScore: flappy.bestScore ?? 0,
|
|
xpEarned: flappy.xpEarned ?? 0,
|
|
isCurrentUser: user ? String(user.id) === String(currentUserId) : false,
|
|
};
|
|
});
|
|
const scored = mapped.filter((row) => row.bestScore > 0);
|
|
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;
|
|
};
|
|
}, [refreshKey, currentUserId]);
|
|
|
|
return (
|
|
<div className="w-full max-w-[420px] 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">Flight Simulator Leaderboard</h2>
|
|
<span className="ml-auto text-xs text-muted-foreground">Top 10 · best run (bird-free)</span>
|
|
</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">{activePilots.length}</span>{" "}
|
|
{activePilots.length === 1 ? "pilot is" : "pilots are"} flying now
|
|
{activePilots.length > 0 && (
|
|
<span className="ml-1 text-foreground">({activePilots.map((pilot) => pilot.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 pilots on the board yet. Be the first to pass a pipe!
|
|
</p>
|
|
) : (
|
|
<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">Pilot</th>
|
|
<th className="px-2 py-2 text-right font-medium">Games</th>
|
|
<th className="px-2 py-2 text-right font-medium">Passes</th>
|
|
<th className="px-2 py-2 text-right font-medium">Best</th>
|
|
<th className="px-4 py-2 text-right font-medium">XP</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody className="divide-y divide-border">
|
|
{rows.map((row, i) => (
|
|
<tr
|
|
key={row.id}
|
|
className={cn(
|
|
row.isCurrentUser && "bg-accent/40",
|
|
row.isOutsideTopTen && "text-muted-foreground opacity-70",
|
|
)}
|
|
title={row.isOutsideTopTen ? "Pilot status: not currently ranked in the top 10" : undefined}
|
|
>
|
|
<td className="px-4 py-2">
|
|
<span className={rankClass(i)}>{row.isOutsideTopTen ? "-" : i + 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 && activePilots.some((pilot) => pilot.id === row.userId) && (
|
|
<span className="size-2 rounded-full bg-emerald-400" title="Playing now" aria-label="Playing 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.totalPasses}
|
|
</td>
|
|
<td className="px-2 py-2 text-right font-semibold tabular-nums">{row.bestScore}</td>
|
|
<td className="px-4 py-2 text-right tabular-nums text-muted-foreground">
|
|
{row.xpEarned}
|
|
</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|