feat(admin): live admin dashboard with solo presence tracking

Solo games run entirely in the browser, so the server previously had no
idea who was playing right now (it only saw logins and finished games).
This adds lightweight presence reporting and an admin dashboard.

Presence (server):
- POST /api/presence (logged-in users): heartbeat while a solo game runs,
  stores username, map (validated), difficulty, wave, since/lastSeen per
  user; entries expire automatically after 90s without a heartbeat
- POST /api/presence/stop: explicit removal when the player exits

Admin API (ADMIN_USERS env, comma-separated usernames):
- GET /api/admin/overview: live solo players, multiplayer room summaries
  (code, mode, map, players — the server already tracks rooms), and
  global stats (accounts, rounds, crystals in circulation)
- GET /api/admin/users?limit=100: user list with stats, newest login first
- publicUser now carries an admin flag; non-admins get 403

Admin UI (src/components/AdminDashboard.vue, served at /admin):
- Login gate for guests/non-admins (guest profiles are detected via
  isLoggedIn, not just user presence)
- KPI cards, live solo table (player, map, difficulty, wave, duration),
  room table, and account table; auto-refresh every 5 seconds
- App.vue renders the dashboard for /admin instead of the game and runs
  a screen watcher that starts/stops the solo presence heartbeat

Config: ADMIN_USERS documented in docker-compose.yml and README.

Tests: 6 new integration checks (admin flag, presence report/stop,
403 guard, overview contents, user list) — 37/37 green, build clean.
Verified end-to-end in the browser: guest gate, admin login, and a live
second player (map/difficulty/wave) appearing in the dashboard.
This commit is contained in:
Tronax 2026-08-17 15:07:02 +02:00
parent 31cb594cb0
commit 69fbb015ab
Signed by: Tronax
SSH key fingerprint: SHA256:2pKKXDZucWvaF/GzXNz0FY53EAO1YDLN80bqS+TTz/o
8 changed files with 679 additions and 3 deletions

View file

@ -316,3 +316,52 @@ export function recordGameResult(userId, { win, score, wave, kills, crystalsEarn
throw e
}
}
// ---------------------------------------------------------------- admin dashboard
export function listUsers(limit = 100) {
const rows = db
.prepare(
`SELECT u.id, u.username, u.display_name, u.crystals, u.created_at, u.last_login,
s.games_played, s.games_won, s.total_kills, s.total_score, s.highest_wave
FROM users u
LEFT JOIN user_stats s ON s.user_id = u.id
ORDER BY u.last_login DESC
LIMIT ?`,
)
.all(Math.max(1, Math.min(500, Math.floor(Number(limit) || 100))))
return rows.map((r) => ({
id: r.id,
username: r.username,
displayName: r.display_name,
crystals: r.crystals,
createdAt: r.created_at,
lastLogin: r.last_login,
stats: {
gamesPlayed: r.games_played ?? 0,
gamesWon: r.games_won ?? 0,
totalKills: r.total_kills ?? 0,
totalScore: r.total_score ?? 0,
highestWave: r.highest_wave ?? 0,
},
}))
}
export function globalStats() {
const r = db
.prepare(
`SELECT COUNT(*) AS users,
COALESCE(SUM(s.games_played), 0) AS gamesPlayed,
COALESCE(SUM(s.games_won), 0) AS gamesWon,
COALESCE(SUM(u.crystals), 0) AS crystalsInCirculation
FROM users u
LEFT JOIN user_stats s ON s.user_id = u.id`,
)
.get()
return {
users: r.users,
gamesPlayed: r.gamesPlayed,
gamesWon: r.gamesWon,
crystalsInCirculation: r.crystalsInCirculation,
}
}