2026-04-10 17:30:13 +02:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
import { useEffect, useState } from "react";
|
2026-04-11 15:17:56 +02:00
|
|
|
import { PublicProfileScreen } from "./PublicProfileScreen";
|
2026-04-10 17:30:13 +02:00
|
|
|
|
|
|
|
|
type LeaderboardRow = {
|
|
|
|
|
id: string;
|
|
|
|
|
name: string;
|
|
|
|
|
totalGames: number;
|
|
|
|
|
wins: number;
|
|
|
|
|
soloGames: number;
|
|
|
|
|
soloWins: number;
|
2026-04-14 15:40:44 +02:00
|
|
|
soloAvgClicks: number | null;
|
2026-04-10 17:30:13 +02:00
|
|
|
multiGames: number;
|
|
|
|
|
multiWins: number;
|
2026-04-14 15:40:44 +02:00
|
|
|
dailyGames: number;
|
|
|
|
|
dailyWins: number;
|
|
|
|
|
dailyAvgClicks: number | null;
|
|
|
|
|
blitzGames: number;
|
|
|
|
|
blitzWins: number;
|
|
|
|
|
blitzBestTime: number | null;
|
2026-04-10 17:30:13 +02:00
|
|
|
avgClicks: number | null;
|
|
|
|
|
bestTime: number | null;
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-14 15:40:44 +02:00
|
|
|
type Mode = "all" | "solo" | "multi" | "daily" | "blitz";
|
2026-04-10 17:30:13 +02:00
|
|
|
|
|
|
|
|
function fmt(s: number): string {
|
|
|
|
|
const m = Math.floor(s / 60);
|
|
|
|
|
const sec = Math.floor(s % 60);
|
|
|
|
|
return `${m}:${String(sec).padStart(2, "0")}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const MEDALS = ["🥇", "🥈", "🥉"];
|
|
|
|
|
|
|
|
|
|
export function LeaderboardScreen({ onBack }: { onBack: () => void }) {
|
|
|
|
|
const [rows, setRows] = useState<LeaderboardRow[]>([]);
|
|
|
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
|
const [mode, setMode] = useState<Mode>("all");
|
2026-04-11 15:17:56 +02:00
|
|
|
const [viewingUserId, setViewingUserId] = useState<string | null>(null);
|
2026-04-10 17:30:13 +02:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
fetch("/api/leaderboard")
|
|
|
|
|
.then((r) => r.json())
|
2026-04-11 22:25:34 +02:00
|
|
|
.then((data) => {
|
|
|
|
|
setRows(data);
|
|
|
|
|
setLoading(false);
|
|
|
|
|
});
|
2026-04-10 17:30:13 +02:00
|
|
|
}, []);
|
|
|
|
|
|
2026-04-11 15:17:56 +02:00
|
|
|
if (viewingUserId) {
|
2026-04-11 22:25:34 +02:00
|
|
|
return (
|
|
|
|
|
<PublicProfileScreen
|
|
|
|
|
userId={viewingUserId}
|
|
|
|
|
onBack={() => setViewingUserId(null)}
|
|
|
|
|
/>
|
|
|
|
|
);
|
2026-04-11 15:17:56 +02:00
|
|
|
}
|
|
|
|
|
|
2026-04-10 18:58:41 +02:00
|
|
|
const sorted = [...rows]
|
2026-04-11 22:25:34 +02:00
|
|
|
.filter((r) =>
|
|
|
|
|
mode === "solo"
|
|
|
|
|
? r.soloGames > 0
|
|
|
|
|
: mode === "multi"
|
|
|
|
|
? r.multiGames > 0
|
2026-04-14 15:40:44 +02:00
|
|
|
: mode === "daily"
|
|
|
|
|
? r.dailyGames > 0
|
|
|
|
|
: mode === "blitz"
|
|
|
|
|
? r.blitzGames > 0
|
|
|
|
|
: true,
|
2026-04-11 22:25:34 +02:00
|
|
|
)
|
2026-04-10 18:58:41 +02:00
|
|
|
.sort((a, b) => {
|
2026-04-11 22:25:34 +02:00
|
|
|
if (mode === "solo")
|
|
|
|
|
return b.soloWins - a.soloWins || b.soloGames - a.soloGames;
|
|
|
|
|
if (mode === "multi")
|
|
|
|
|
return b.multiWins - a.multiWins || b.multiGames - a.multiGames;
|
2026-04-14 15:40:44 +02:00
|
|
|
if (mode === "daily")
|
|
|
|
|
return b.dailyWins - a.dailyWins || b.dailyGames - a.dailyGames;
|
|
|
|
|
if (mode === "blitz")
|
|
|
|
|
return b.blitzWins - a.blitzWins || b.blitzGames - a.blitzGames;
|
2026-04-10 18:58:41 +02:00
|
|
|
return b.wins - a.wins || b.totalGames - a.totalGames;
|
|
|
|
|
});
|
2026-04-10 17:30:13 +02:00
|
|
|
|
|
|
|
|
return (
|
2026-04-10 19:04:52 +02:00
|
|
|
<div className="min-h-dvh w-full bg-[#0f0f0f] text-[#f0f0f0] animate-fade-in max-w-2xl mx-auto px-4 py-5 sm:py-6 pb-12">
|
2026-04-10 18:58:41 +02:00
|
|
|
{/* Header */}
|
2026-04-10 19:04:52 +02:00
|
|
|
<div className="flex items-center gap-3 sm:gap-4 mb-5 sm:mb-6">
|
|
|
|
|
<button
|
|
|
|
|
className="min-h-9 px-3 rounded-lg text-xs sm:text-sm font-semibold bg-[#242424] border border-[#2e2e2e] text-[#f0f0f0] hover:bg-[#1a1a1a] cursor-pointer shrink-0"
|
|
|
|
|
onClick={onBack}
|
|
|
|
|
>
|
2026-04-10 18:58:41 +02:00
|
|
|
Retour
|
|
|
|
|
</button>
|
2026-04-10 19:04:52 +02:00
|
|
|
<h2 className="text-xl sm:text-2xl font-black">Classement</h2>
|
2026-04-10 17:30:13 +02:00
|
|
|
</div>
|
|
|
|
|
|
2026-04-10 18:58:41 +02:00
|
|
|
{/* Tabs */}
|
2026-04-14 15:40:44 +02:00
|
|
|
<div className="flex gap-2 mb-4 sm:mb-5 flex-wrap">
|
|
|
|
|
{(["all", "solo", "daily", "blitz", "multi"] as Mode[]).map((m) => (
|
2026-04-10 17:30:13 +02:00
|
|
|
<button
|
|
|
|
|
key={m}
|
2026-04-10 19:04:52 +02:00
|
|
|
className={`flex-1 py-2 rounded-xl text-xs sm:text-sm font-semibold border cursor-pointer transition-colors ${mode === m ? "bg-[#7c3aed] border-[#7c3aed] text-white" : "bg-[#1a1a1a] border-[#2e2e2e] text-[#888] hover:text-[#f0f0f0]"}`}
|
2026-04-10 17:30:13 +02:00
|
|
|
onClick={() => setMode(m)}
|
|
|
|
|
>
|
2026-04-14 15:40:44 +02:00
|
|
|
{m === "all" ? "Global" : m === "solo" ? "Solo" : m === "multi" ? "Multi" : m === "daily" ? "Daily" : "Blitz"}
|
2026-04-10 17:30:13 +02:00
|
|
|
</button>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{loading ? (
|
2026-04-11 22:25:34 +02:00
|
|
|
<div className="flex justify-center py-12">
|
|
|
|
|
<div className="spinner" />
|
|
|
|
|
</div>
|
2026-04-10 17:30:13 +02:00
|
|
|
) : sorted.length === 0 ? (
|
2026-04-11 22:25:34 +02:00
|
|
|
<p className="text-[#888] text-sm text-center py-12">
|
|
|
|
|
Aucune partie jouée pour le moment.
|
|
|
|
|
</p>
|
2026-04-10 17:30:13 +02:00
|
|
|
) : (
|
2026-04-10 19:04:52 +02:00
|
|
|
<div className="flex flex-col gap-2 sm:gap-2.5">
|
2026-04-10 17:30:13 +02:00
|
|
|
{sorted.map((row, i) => {
|
2026-04-11 22:25:34 +02:00
|
|
|
const games =
|
|
|
|
|
mode === "solo"
|
|
|
|
|
? row.soloGames
|
|
|
|
|
: mode === "multi"
|
|
|
|
|
? row.multiGames
|
2026-04-14 15:40:44 +02:00
|
|
|
: mode === "daily"
|
|
|
|
|
? row.dailyGames
|
|
|
|
|
: mode === "blitz"
|
|
|
|
|
? row.blitzGames
|
|
|
|
|
: row.totalGames;
|
2026-04-11 22:25:34 +02:00
|
|
|
const wins =
|
|
|
|
|
mode === "solo"
|
|
|
|
|
? row.soloWins
|
|
|
|
|
: mode === "multi"
|
|
|
|
|
? row.multiWins
|
2026-04-14 15:40:44 +02:00
|
|
|
: mode === "daily"
|
|
|
|
|
? row.dailyWins
|
|
|
|
|
: mode === "blitz"
|
|
|
|
|
? row.blitzWins
|
|
|
|
|
: row.wins;
|
|
|
|
|
const avgClicks =
|
|
|
|
|
mode === "solo"
|
|
|
|
|
? row.soloAvgClicks
|
|
|
|
|
: mode === "daily"
|
|
|
|
|
? row.dailyAvgClicks
|
|
|
|
|
: mode === "all"
|
|
|
|
|
? row.avgClicks
|
|
|
|
|
: null;
|
|
|
|
|
const bestTime =
|
|
|
|
|
mode === "blitz"
|
|
|
|
|
? row.blitzBestTime
|
|
|
|
|
: mode === "all"
|
|
|
|
|
? row.bestTime
|
|
|
|
|
: null;
|
2026-04-10 17:30:13 +02:00
|
|
|
|
|
|
|
|
return (
|
2026-04-11 22:25:34 +02:00
|
|
|
<div
|
|
|
|
|
key={row.id}
|
|
|
|
|
onClick={() => setViewingUserId(row.id)}
|
|
|
|
|
className={`flex items-center gap-2.5 px-3.5 py-3 rounded-xl border cursor-pointer hover:brightness-110 transition-all ${i < 3 ? "border-[#7c3aed] bg-[#7c3aed]/8" : "border-[#2e2e2e] bg-[#1a1a1a]"}`}
|
|
|
|
|
>
|
|
|
|
|
<span className="text-xl w-8 text-center shrink-0">
|
|
|
|
|
{MEDALS[i] ?? `#${i + 1}`}
|
|
|
|
|
</span>
|
2026-04-10 19:06:56 +02:00
|
|
|
<div className="flex-1 min-w-0 flex flex-col gap-1">
|
|
|
|
|
<span className="font-bold text-sm truncate">{row.name}</span>
|
|
|
|
|
<div className="flex flex-wrap gap-x-3 gap-y-0.5">
|
2026-04-11 22:25:34 +02:00
|
|
|
<span className="text-xs text-[#888]">
|
|
|
|
|
<span className="font-bold text-[#f0f0f0]">{wins}</span>{" "}
|
|
|
|
|
victoires
|
|
|
|
|
</span>
|
|
|
|
|
<span className="text-xs text-[#888]">
|
|
|
|
|
<span className="font-bold text-[#f0f0f0]">{games}</span>{" "}
|
|
|
|
|
parties
|
|
|
|
|
</span>
|
2026-04-14 15:40:44 +02:00
|
|
|
{avgClicks != null && (
|
2026-04-11 22:25:34 +02:00
|
|
|
<span className="text-xs text-[#888]">
|
|
|
|
|
<span className="font-bold text-[#f0f0f0]">
|
2026-04-14 15:40:44 +02:00
|
|
|
{avgClicks}
|
2026-04-11 22:25:34 +02:00
|
|
|
</span>{" "}
|
|
|
|
|
clics moy.
|
|
|
|
|
</span>
|
2026-04-10 19:06:56 +02:00
|
|
|
)}
|
2026-04-14 15:40:44 +02:00
|
|
|
{bestTime != null && (
|
2026-04-11 22:25:34 +02:00
|
|
|
<span className="text-xs text-[#888]">
|
|
|
|
|
<span className="font-bold text-[#f0f0f0]">
|
2026-04-14 15:40:44 +02:00
|
|
|
{fmt(bestTime)}
|
2026-04-11 22:25:34 +02:00
|
|
|
</span>{" "}
|
2026-04-14 15:40:44 +02:00
|
|
|
{mode === "blitz" ? "meilleur temps" : "meilleur"}
|
2026-04-11 22:25:34 +02:00
|
|
|
</span>
|
2026-04-10 19:06:56 +02:00
|
|
|
)}
|
2026-04-10 18:58:41 +02:00
|
|
|
</div>
|
2026-04-10 17:30:13 +02:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|