Per-player playtime tracking + sparkline hover tooltips

- New GET /api/players/playtime (authed): reads each vanilla stats file
  under /home/minecraft/server/world/stats and maps uuid→name via
  usercache.json. Returns playtime in ticks and hours, plus file mtime
  as "last seen". Memoized 60s via lib/cache#memoAsync.
- New PlaytimeLeaderboard card in the Players tab:
    - Rank, avatar (click → PlayerDrawer), relative last-seen, hours.
    - Top 10 by default with toggle to show all; combined hours badge.
- PlayerDrawer surfaces the player's total hours + last-seen using the
  same cached playtime query, plus fills in UUID if the player is not
  in ops/whitelist/banned.
- lib/time.ts: formatHours() — minutes under 1h, "H.Hh" under a day,
  "Dd Hh" above.
- Analytics sparklines gain hover/touch tooltips:
    - Timestamps threaded through from each MetricEntry.
    - SVG overlay captures onMouseMove/onTouchMove, computes nearest
      index by x, draws a dashed guide line + focus dot.
    - Floating tooltip shows HH:MM + formatted value with unit; edge
      clamping keeps it in-frame at the extremes.
    - Per-chart formatValue so RAM shows GB, CPU 0 decimals, Players
      integer, TPS 1 decimal. Keeps the peak-player marker intact.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
hurkicorgi 2026-04-13 06:24:13 -06:00
parent 7ada9ec7d9
commit 9ccd4ca772
6 changed files with 413 additions and 38 deletions

View file

@ -10,6 +10,8 @@ import { Input } from "@/components/ui/input";
import { Separator } from "@/components/ui/separator";
import { PlayerAvatar } from "@/components/PlayerAvatar";
import { onAppEvent } from "@/lib/events";
import { formatHours, timeAgo } from "@/lib/time";
import type { PlaytimeEntry } from "@/components/PlaytimeLeaderboard";
type PlayerData = {
ops: { name: string; uuid: string; level: number }[];
@ -45,6 +47,17 @@ export function PlayerDrawer() {
staleTime: 10_000,
});
const playtime = useQuery<PlaytimeEntry[]>({
queryKey: ["players-playtime"],
queryFn: () => fetch("/api/players/playtime").then((r) => r.json()),
enabled: authed && !!name,
staleTime: 60_000,
});
const myPlaytime = name
? playtime.data?.find((p) => p.name === name)
: undefined;
// Use existing analytics cache (last entry) to determine online now
const analytics = queryClient.getQueryData<MetricEntry[]>(["analytics", 6]);
const onlineNow =
@ -144,6 +157,26 @@ export function PlayerDrawer() {
</p>
) : (
<>
{myPlaytime && (
<div className="grid grid-cols-2 gap-3">
<div className="rounded-md bg-muted p-3">
<p className="text-xs text-muted-foreground">Playtime</p>
<p className="text-lg font-bold tabular-nums">
{formatHours(myPlaytime.playtimeHours)}
</p>
</div>
<div className="rounded-md bg-muted p-3">
<p className="text-xs text-muted-foreground">Last seen</p>
<p
className="text-sm font-medium"
title={new Date(myPlaytime.lastPlayedMs).toLocaleString()}
>
{timeAgo(myPlaytime.lastPlayedMs)}
</p>
</div>
</div>
)}
{bannedEntry?.reason && (
<div>
<p className="text-xs font-semibold text-muted-foreground uppercase tracking-wide mb-1">
@ -226,6 +259,7 @@ export function PlayerDrawer() {
{players.data?.ops.find((p) => p.name === name)?.uuid ||
players.data?.whitelist.find((p) => p.name === name)?.uuid ||
players.data?.banned.find((p) => p.name === name)?.uuid ||
myPlaytime?.uuid ||
"—"}
</span>
</p>