/** * routes/hunt-log.js — receives hunt telemetry from the client and appends it * to a daily log file on the server. Debug aid: confirms spawns / detections / * captures are firing server-side even when something looks wrong on-device. * * Wire in server.js: * import huntLogRoutes from './routes/hunt-log.js'; * app.use('/api/hunt-log', huntLogRoutes); * * Log file: logs/hunt-YYYY-MM-DD.log (one JSON object per line) */ import { Router } from 'express'; import { appendFile, mkdir } from 'node:fs/promises'; import { dirname, join } from 'node:path'; import { fileURLToPath } from 'node:url'; const __dirname = dirname(fileURLToPath(import.meta.url)); const LOG_DIR = join(__dirname, '..', 'logs'); const router = Router(); // Accept a single event or a batch. Keep it small + permissive. router.post('/', async (req, res) => { try { const body = req.body || {}; const events = Array.isArray(body.events) ? body.events : [body]; const day = new Date().toISOString().slice(0, 10); const file = join(LOG_DIR, `hunt-${day}.log`); await mkdir(LOG_DIR, { recursive: true }); const lines = events.map(e => JSON.stringify({ t: new Date().toISOString(), ip: req.ip, type: e.type || 'unknown', ghost: e.ghost || null, color: e.color || null, rarity: e.rarity || null, haunt: typeof e.haunt === 'number' ? Math.round(e.haunt) : null, pos: e.pos || null, // {x,y,z} spawn position when present session: e.session || null, // client session id })).join('\n') + '\n'; await appendFile(file, lines, 'utf8'); res.json({ ok: true, logged: events.length }); } catch (err) { // never let logging break the hunt console.error('hunt-log error:', err.message); res.status(200).json({ ok: false }); } }); export default router;