Login ad in the games: the sponsor interstitial after a fresh in-game sign-in, counted per surface

- Game sign-in kit (game-signin.js): after a fresh email sign-in (never a remembered
  pass) the kit shows the login ad before the hand-off to /auth?t=, same as the
  dashboard: sponsor card, Open Ad (a counted click through /api/ads/click), sponsor
  framed in a full-screen overlay when frameable else a new tab, 10 s countdown
  (loginDwellSeconds), then Continue to the game. No ad, an error, a non-200 or a
  7 s stall all go straight to the game. A game page whose CSP blocks the frame gets a
  new-tab link instead. mount option loginAd: false switches it off.
- GET /api/game/ads/login?game=<slug>: CORS only for that game's GAME_AUTH origins
  (dev: localhost); the dashboard /api/ads/slot route keeps no CORS. Both share
  loginAdFor() (ads.serve: impression + lastShownDay as before). Drip Runner and
  PolyBreak are marked frames: false (their CSP is default-src 'self'), so their ad
  opens in a new tab; frame checks for games ignore an instantadpay.com-only
  frame-ancestors allowance.
- loginshows.js: every login-ad request tallied per Central day, surface (dashboard or
  game slug) and kind (member slot, house, none); MySQL table login_shows (created on
  first use) or DATA_DIR/login-shows.json. GET /api/admin/login-shows + a Login-ad
  showings card on the admin Traffic pane (dashboard vs games, house, no-ad, by game).
- The kit is served no-cache so games pick it up without a tag bump; admin.js tag bumped.
- Chatbot fact updated (slot rules + games), KNOWLEDGE_DATE 2026-10-18.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-10-18 06:17:41 -05:00
parent d520eb67af
commit 017017ad63
6 changed files with 211 additions and 24 deletions
+61
View File
@@ -0,0 +1,61 @@
// 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 };