// Login-ad showings per day per surface (Marty, 2026-10-18). The sign-in interstitial now runs on // the dashboard AND inside the games' sign-in kit; before raising the 3-slot cap he wants a week of // combined numbers. One row per Central day, surface ('dashboard' or a game slug) and kind: // 'member' (a member's slot was shown), 'house' (the house login ad), 'none' (a sign-in with no // login ad to show). MySQL table login_shows when the DB is on, else DATA_DIR/login-shows.json. 'use strict'; const fs = require('fs'); const path = require('path'); const db = require('./db'); let DATA_DIR = null, J = null, ready = null; const ctDay = t => new Date(t === undefined ? Date.now() : t).toLocaleDateString('en-CA', { timeZone: 'America/Chicago' }); const FILE = () => path.join(DATA_DIR, 'login-shows.json'); function init(opts) { DATA_DIR = opts.dataDir; } function ensure() { if (!ready) ready = db.q(`CREATE TABLE IF NOT EXISTS login_shows ( day VARCHAR(10) NOT NULL, surface VARCHAR(40) NOT NULL, kind VARCHAR(10) NOT NULL, n INT NOT NULL DEFAULT 0, PRIMARY KEY (day, surface, kind) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`).catch(e => { ready = null; throw e; }); return ready; } function load() { if (J) return J; try { J = JSON.parse(fs.readFileSync(FILE(), 'utf8')); } catch (e) { J = {}; } return J; } // surface: 'dashboard' | game slug; ad: what ads.serve returned (null = nothing to show) async function record(surface, ad) { const s = String(surface || 'dashboard').toLowerCase().replace(/[^a-z0-9_-]/g, '').slice(0, 40) || 'dashboard'; const kind = !ad ? 'none' : ad.house ? 'house' : 'member'; const d = ctDay(); try { if (db.enabled()) { await ensure(); await db.q('INSERT INTO login_shows (day,surface,kind,n) VALUES (?,?,?,1) ON DUPLICATE KEY UPDATE n=n+1', [d, s, kind]); } else { const k = d + '|' + s + '|' + kind, st = load(); st[k] = (st[k] || 0) + 1; fs.writeFileSync(FILE(), JSON.stringify(st)); } } catch (e) { console.error('login shows', e.message); } } // last `days` Central days, newest first: { day, dashboard: {member,house,none}, games: {member,house,none}, byGame: {slug: shown} } async function report(days) { days = Math.max(1, Math.min(90, Number(days) || 14)); const since = ctDay(Date.now() - (days - 1) * 86400000); let rows = []; if (db.enabled()) { await ensure(); rows = (await db.q('SELECT day, surface, kind, n FROM login_shows WHERE day>=?', [since])).map(r => ({ day: String(r.day), surface: r.surface, kind: r.kind, n: Number(r.n) })); } else { for (const [k, n] of Object.entries(load())) { const [day, surface, kind] = k.split('|'); if (day >= since) rows.push({ day, surface, kind, n }); } } const out = {}; const blank = () => ({ member: 0, house: 0, none: 0 }); for (let i = 0; i < days; i++) { const d = ctDay(Date.now() - i * 86400000); out[d] = { day: d, dashboard: blank(), games: blank(), byGame: {} }; } for (const r of rows) { const o = out[r.day]; if (!o || !['member', 'house', 'none'].includes(r.kind)) continue; if (r.surface === 'dashboard') o.dashboard[r.kind] += r.n; else { o.games[r.kind] += r.n; if (r.kind !== 'none') o.byGame[r.surface] = (o.byGame[r.surface] || 0) + r.n; } } return Object.values(out).sort((a, b) => (a.day < b.day ? 1 : -1)); } module.exports = { init, record, report };