Paid-traffic source tracking: ?utm_source/src + clickid -> dr.src cookie, per-source visits/guests/runs/sign-ins/new accounts, BeMob postback on a new account (DR_POSTBACK_URL), /api/admin/srcstats

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-10-03 11:30:22 -05:00
parent 305bd435fa
commit 60f1148efa
5 changed files with 146 additions and 35 deletions
+13 -6
View File
@@ -23,6 +23,7 @@ const ledger = require('./lib/ledger');
const faucet = require('./lib/faucet');
const spawn = require('./lib/spawn');
const badge = require('./lib/badge');
const srctrack = require('./lib/srctrack'); // paid-traffic sources (2026-10-03)
const tips = require('./lib/tips'); // facts you learn as you play, priced from the live IAP catalog
const PORT = Number(process.env.PORT || 3000);
@@ -193,6 +194,9 @@ const server = http.createServer(async (req, res) => {
game.updPlayer(r.claims.memberId, x => { if (!x.sponsorRef && r.claims.sponsorRef) x.sponsorRef = String(r.claims.sponsorRef).toLowerCase(); if (r.claims.joinedRef && !x.joinedRef) x.joinedRef = r.claims.joinedRef; });
const meNow = { memberId: Number(r.claims.memberId), username: r.claims.username || null, wallet: r.claims.wallet ? String(r.claims.wallet).toLowerCase() : null };
for (const ch of pendingClears) { const g = ledger.chapterDrip(meNow, ch, true); if (g.pol) announceChapter(meNow, ch, g.pol); } // clears made as a guest pay now
{ const t = srctrack.fromRequest(req); if (t) {
srctrack.bump(t.src, 'signins', r.claims.memberId); game.updPlayer(r.claims.memberId, x => { if (!x.src) x.src = t.src; });
if (r.claims.joinedAt && Date.now() - Number(r.claims.joinedAt) < 30 * 60000) { srctrack.bump(t.src, 'signups', r.claims.memberId); srctrack.postback(t, r.claims.memberId).catch(() => {}); } } }
if (first) notify('signin', { memberId: r.claims.memberId, username: r.claims.username, wallet: r.claims.wallet, via: guest ? 'a share link' : null }).catch(() => {});
res.writeHead(302, { Location: '/play' + (pendingClears.length ? '?collected=' + pendingClears.length : ''), 'Set-Cookie': [sso.cookie(sid)].concat(guest ? [sso.clearGuestCookie()] : []), 'Cache-Control': 'no-store' }); return res.end();
}
@@ -218,10 +222,12 @@ const server = http.createServer(async (req, res) => {
}
// ---- static + pages ----
if (p === '/' || p === '/index.html') { if (curtained(req, res, u)) return; return sendFile(res, path.join(PUBLIC_DIR, 'index.html')); }
// a paid-traffic landing (?src=adsterra&cid=<click id>): remember it (dr.src) and count the visit (2026-10-03)
let srcHdr = null; if (req.method === 'GET' && ['/', '/index.html', '/play', '/signin'].includes(p)) { const t = srctrack.fromQuery(u); if (t) { srcHdr = { 'Set-Cookie': srctrack.cookieHeader(t, IS_PROD) }; srctrack.bump(t.src, 'visits'); } }
if (p === '/' || p === '/index.html') { if (curtained(req, res, u)) return; return sendFile(res, path.join(PUBLIC_DIR, 'index.html'), srcHdr); }
if (p === '/testers') { if (curtained(req, res, u)) return; return sendFile(res, path.join(PUBLIC_DIR, 'testers.html')); } // the pre-launch briefing
if (p === '/play') return sendFile(res, path.join(PUBLIC_DIR, 'play.html'));
if (p === '/signin') return sendFile(res, path.join(PUBLIC_DIR, 'signin.html')); // in-game sign-in (InstantAdPay kit)
if (p === '/play') return sendFile(res, path.join(PUBLIC_DIR, 'play.html'), srcHdr);
if (p === '/signin') return sendFile(res, path.join(PUBLIC_DIR, 'signin.html'), srcHdr); // in-game sign-in (InstantAdPay kit)
if (p === '/admin') return sendFile(res, path.join(PUBLIC_DIR, 'admin.html')); // the page asks for the key; every /api/admin call checks it
if (p === '/spawn.js') return sendFile(res, path.join(__dirname, 'lib', 'spawn.js'));
if (/^\/(badges\/)?[a-z0-9._-]+\.(css|js|png|jpg|svg|ico|webp|mp4)$/i.test(p) && !p.includes('..')) return sendFile(res, path.join(PUBLIC_DIR, p.slice(1)), p.startsWith('/badges/') ? { 'Cache-Control': 'public, max-age=86400', 'Access-Control-Allow-Origin': '*' } : undefined); // badge art is also shown on InstantAdPay and in link previews
@@ -271,6 +277,7 @@ const server = http.createServer(async (req, res) => {
if (body.itemDrips && typeof body.itemDrips === 'object') patch.itemDrips = Object.assign({}, ledger.settings().itemDrips, body.itemDrips);
return json(res, 200, { ok: true, settings: ledger.setSettings(patch) });
}
if (p === '/api/admin/srcstats' && req.method === 'GET') return json(res, 200, srctrack.summary(Number(u.searchParams.get('days')) || 30));
if (p === '/api/admin/faucet/tick' && req.method === 'POST') return json(res, 200, await faucet.tick(notify));
if (p === '/api/admin/announce' && req.method === 'POST') { // post (or re-post with force) a badge for a member: {memberId, id}
const { body } = await readBody(req); const pl = game.player(Number(body.memberId)); if (!pl) return json(res, 404, { error: 'no such player' });
@@ -282,14 +289,14 @@ const server = http.createServer(async (req, res) => {
return json(res, 400, { error: 'unknown badge id' });
}
if (p === '/api/admin/handoff' && req.method === 'POST') { // a test token for a synthetic member (local checks only)
const { body } = await readBody(req); return json(res, 200, { url: SITE + '/auth?t=' + sso.mint({ memberId: Number(body.memberId) || 999001, email: body.email || 'tester@example.com', wallet: body.wallet || null, username: body.username || 'tester' }) });
const { body } = await readBody(req); return json(res, 200, { url: SITE + '/auth?t=' + sso.mint({ memberId: Number(body.memberId) || 999001, email: body.email || 'tester@example.com', wallet: body.wallet || null, username: body.username || 'tester', joinedAt: body.joinedAt || null }) });
}
return json(res, 404, { error: 'no such admin route' });
}
if (p === '/api/guest' && req.method === 'POST') {
if (me) return json(res, 200, { ok: true, already: true });
if (CURTAIN && !hasPass(req)) return json(res, 403, { error: 'Open the tester link or a player\u2019s share link first.' });
const g = sso.startGuest(refCookie(req)); return json(res, 200, { ok: true }, { 'Set-Cookie': sso.guestCookie(g.gid) });
const g = sso.startGuest(refCookie(req)); { const t = srctrack.fromRequest(req); if (t) srctrack.bump(t.src, 'guests', g.memberId); } return json(res, 200, { ok: true }, { 'Set-Cookie': sso.guestCookie(g.gid) });
}
const who = me || sso.guestFrom(req);
if (!who) return json(res, 401, { error: 'Sign in with your InstantAdPay account first.', iap: '/signin', guestOk: !CURTAIN || hasPass(req), signin: signinUrl(req) });
@@ -314,7 +321,7 @@ async function memberApi(req, res, u, p, me) {
signin: me.guest ? signinUrl(req) : null, share: (!me.guest && me.username) ? SITE + '/r/' + encodeURIComponent(me.username) : null, referrals: { count: refs.length, clicks: (store.read('share-hits', {})[String(me.username || '').toLowerCase()] || 0), earned: ledger.balance(me.memberId).refTotal, pct: Number(ledger.settings().refSharePct) || 0 } });
}
if (p === '/api/balance' && req.method === 'GET') return json(res, 200, ledger.balance(me.memberId));
if (p === '/api/run/start' && req.method === 'POST') { const { body } = await readBody(req); const r = game.start(me, body.W, body.H); return json(res, 200, { runId: r.id, seed: r.seed, W: r.W, H: r.H, speed: r.speed, chapter: r.chapter, goal: r.goal, armed: r.armed, ship: r.ship }); }
if (p === '/api/run/start' && req.method === 'POST') { const { body } = await readBody(req); { const t = srctrack.fromRequest(req); if (t) srctrack.bump(t.src, 'runs', me.memberId); } const r = game.start(me, body.W, body.H); return json(res, 200, { runId: r.id, seed: r.seed, W: r.W, H: r.H, speed: r.speed, chapter: r.chapter, goal: r.goal, armed: r.armed, ship: r.ship }); }
if (p === '/api/run/events' && req.method === 'POST') { const { body } = await readBody(req); const r = game.events(me, String(body.runId || ''), body.t, body.events, !!body.goal); if (r.cleared) { r.pool = ledger.pool().available; if (r.cleared.first && !me.guest) announceChapter(me, r.cleared.chapter, r.cleared.pol); } return json(res, r.error ? 400 : 200, r); }
if (p === '/api/run/end' && req.method === 'POST') { const { body } = await readBody(req); return json(res, 200, game.end(me, String(body.runId || ''))); }
// refuel: hand the player to IAP for a verified visit; the run resumes when IAP confirms