mc-dashboard/app/api/players/playtime/route.ts

38 lines
1 KiB
TypeScript
Raw Normal View History

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 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" },
});
} catch (e) {
return NextResponse.json(
{ error: (e as Error).message },
{ status: 500 }
);
}
}