Rich per-player stats + pick-your-metric leaderboard
- lib/player-stats.ts: single computePlayerStats() reads every world/stats/<uuid>.json plus world/advancements/<uuid>.json and joins with usercache.json. Returns a flat PlayerStats record per player: playtime, longest life, mob/player kills, deaths, K/D, damage dealt and taken (HP-scaled), blocks mined, items used / crafted / picked up (+ unique), distance (summed across all _one_cm stats), chest opens, nether/end trips, villager trades, fish caught, animals bred, and advancements (non-recipe) / recipes unlocked. - New GET /api/players/stats (authed, 60s memo). Existing /api/players/playtime now returns a thin projection of the same computed data (shared cache key keeps both endpoints cheap). - New components/Leaderboard.tsx with a metric select grouped into Time / Combat / World / Exploration / Progression / Economy (22 metrics). Sorts descending, top 10 with "show all" toggle, smart number formatting (1.2k / 3.4M / HP / km). Replaces the old PlaytimeLeaderboard in the Players tab. - PlayerDrawer upgraded: uses the full stats payload, shows small tiles for Kills / Deaths / K/D / Advs / Mined / Crafted / Distance alongside Playtime + Last seen. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
9ccd4ca772
commit
3a69dc9243
7 changed files with 521 additions and 214 deletions
|
|
@ -1,71 +1,11 @@
|
|||
import { NextResponse } from "next/server";
|
||||
import { readdirSync, readFileSync, statSync } from "fs";
|
||||
import { join } from "path";
|
||||
import { auth } from "@/lib/auth";
|
||||
import { memoAsync } from "@/lib/cache";
|
||||
import { computePlayerStats } from "@/lib/player-stats";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
export const runtime = "nodejs";
|
||||
|
||||
const STATS_DIR = "/home/minecraft/server/world/stats";
|
||||
const USERCACHE = "/home/minecraft/server/usercache.json";
|
||||
const TICKS_PER_HOUR = 20 * 60 * 60;
|
||||
|
||||
type Entry = {
|
||||
uuid: string;
|
||||
name: string;
|
||||
playtimeTicks: number;
|
||||
playtimeHours: number;
|
||||
lastPlayedMs: number;
|
||||
};
|
||||
|
||||
type UserCacheEntry = { name: string; uuid: string };
|
||||
|
||||
function loadNameMap(): Map<string, string> {
|
||||
try {
|
||||
const arr = JSON.parse(readFileSync(USERCACHE, "utf8")) as UserCacheEntry[];
|
||||
return new Map(arr.map((e) => [e.uuid, e.name]));
|
||||
} catch {
|
||||
return new Map();
|
||||
}
|
||||
}
|
||||
|
||||
function computePlaytime(): Entry[] {
|
||||
const names = loadNameMap();
|
||||
let files: string[];
|
||||
try {
|
||||
files = readdirSync(STATS_DIR).filter((f) => f.endsWith(".json"));
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
|
||||
const out: Entry[] = [];
|
||||
for (const f of files) {
|
||||
const uuid = f.replace(/\.json$/, "");
|
||||
const path = join(STATS_DIR, f);
|
||||
try {
|
||||
const data = JSON.parse(readFileSync(path, "utf8")) as {
|
||||
stats?: { "minecraft:custom"?: Record<string, number> };
|
||||
};
|
||||
const ticks = Number(
|
||||
data.stats?.["minecraft:custom"]?.["minecraft:play_time"] || 0
|
||||
);
|
||||
const mtime = statSync(path).mtimeMs;
|
||||
out.push({
|
||||
uuid,
|
||||
name: names.get(uuid) || uuid.slice(0, 8),
|
||||
playtimeTicks: ticks,
|
||||
playtimeHours: Math.round((ticks / TICKS_PER_HOUR) * 100) / 100,
|
||||
lastPlayedMs: mtime,
|
||||
});
|
||||
} catch {
|
||||
// Skip unreadable/corrupt stat files
|
||||
}
|
||||
}
|
||||
out.sort((a, b) => b.playtimeTicks - a.playtimeTicks);
|
||||
return out;
|
||||
}
|
||||
|
||||
export async function GET() {
|
||||
const session = await auth();
|
||||
if (!session) {
|
||||
|
|
@ -73,9 +13,18 @@ export async function GET() {
|
|||
}
|
||||
|
||||
try {
|
||||
const data = await memoAsync("players:playtime", 60_000, async () =>
|
||||
computePlaytime()
|
||||
const full = await memoAsync("players:stats", 60_000, async () =>
|
||||
computePlayerStats()
|
||||
);
|
||||
const data = full
|
||||
.map((p) => ({
|
||||
uuid: p.uuid,
|
||||
name: p.name,
|
||||
playtimeTicks: Math.round(p.playtimeHours * 20 * 60 * 60),
|
||||
playtimeHours: p.playtimeHours,
|
||||
lastPlayedMs: p.lastPlayedMs,
|
||||
}))
|
||||
.sort((a, b) => b.playtimeHours - a.playtimeHours);
|
||||
return NextResponse.json(data, {
|
||||
headers: { "Cache-Control": "private, max-age=60" },
|
||||
});
|
||||
|
|
|
|||
28
app/api/players/stats/route.ts
Normal file
28
app/api/players/stats/route.ts
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import { NextResponse } from "next/server";
|
||||
import { auth } from "@/lib/auth";
|
||||
import { memoAsync } from "@/lib/cache";
|
||||
import { computePlayerStats } from "@/lib/player-stats";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function GET() {
|
||||
const session = await auth();
|
||||
if (!session) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 403 });
|
||||
}
|
||||
|
||||
try {
|
||||
const data = await memoAsync("players:stats", 60_000, async () =>
|
||||
computePlayerStats()
|
||||
);
|
||||
return NextResponse.json(data, {
|
||||
headers: { "Cache-Control": "private, max-age=60" },
|
||||
});
|
||||
} catch (e) {
|
||||
return NextResponse.json(
|
||||
{ error: (e as Error).message },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue