// Member welcome offer from the games (Marty, 2026-10-05). After a win in a network game, a signed-in player who is not a // member yet sees one card a day: "Become an InstantAdPay member for $5". The card links to /my?from=#buy; this module // remembers which game sent them, and when that account's FIRST package ($5 or more) lands on chain it grants the welcome // bonus credits (purchased-grade, once per account) and counts the conversion for that game. The game perk itself is handed // out by each game the next time the player signs in there as a member (the sign-in token carries memberId). // // Settings (siteConfig): starterOffer '1'|'0' (on), starterBonusCredits (100), starterWindowDays (14: the game visit must be // this recent). Every number comes from the chain index, never a marketing counter. Games are a marketing cost // (Marty 2026-10-05); this is how we see what each one converts. 'use strict'; const fs = require('fs'); const path = require('path'); let X = {}, FILE = null, S = null; const GAMES = { driprunner: { name: 'Drip Runner', perk: 'a free hangar upgrade', back: '/api/my/driprunner' }, clutterhunt: { name: 'ClutterHunt', perk: '5 bonus hints', back: '/api/my/clutterhunt' }, spellwheel: { name: 'SpellWheel', perk: '5 bonus hints', back: '/api/my/spellwheel' }, gemkingdom: { name: 'Gem Kingdom', perk: '50 Crowns', back: '/api/my/gemkingdom' }, spookydoors: { name: 'Spooky Doors', perk: 'a lucky caramel apple', back: '/api/my/spookydoors' }, polywarriors: { name: 'PolyWarriors', perk: 'a free warrior recruit', back: '/api/my/polywarriors' }, }; function init(opts) { X = opts; FILE = path.join(opts.dataDir, 'starter.json'); S = load(); } function load() { try { return Object.assign({ from: {}, granted: {}, conv: {}, visits: {} }, JSON.parse(fs.readFileSync(FILE, 'utf8'))); } catch (e) { return { from: {}, granted: {}, conv: {}, visits: {} }; } } function save() { try { fs.writeFileSync(FILE + '.tmp', JSON.stringify(S)); fs.renameSync(FILE + '.tmp', FILE); } catch (e) {} } function cfg() { const c = (X.siteConfig && X.siteConfig()) || {}; return { on: String(c.starterOffer == null ? '1' : c.starterOffer) === '1', bonus: Math.max(0, Math.round(Number(c.starterBonusCredits == null ? 100 : c.starterBonusCredits) || 0)), windowDays: Math.max(1, Number(c.starterWindowDays) || 14) }; } const known = g => Object.prototype.hasOwnProperty.call(GAMES, String(g || '')); // a member who has bought before cannot get the welcome bonus; the chain index decides function hasPurchased(memberId) { if (!memberId) return false; try { return X.chain.recentEvents(1e9).some(e => e.type === 'Purchase' && e.buyerId === memberId); } catch (e) { return false; } } // the card in a game was tapped and the player reached InstantAdPay (signed in): remember the game function setFrom(email, game) { if (!email || !known(game)) return null; const e = String(email).toLowerCase(); S.from[e] = { game, at: Date.now() }; const day = new Date().toLocaleDateString('en-CA', { timeZone: 'America/Chicago' }); S.visits[game] = S.visits[game] || {}; S.visits[game][day] = (S.visits[game][day] || 0) + 1; save(); return S.from[e]; } // what the Buy page shows this member async function view(email, memberId) { const c = cfg(), e = String(email || '').toLowerCase(), f = S.from[e]; const fresh = f && Date.now() - f.at < c.windowDays * 86400000 ? f : null, bought = hasPurchased(memberId); const g = fresh ? GAMES[fresh.game] : null; return { on: c.on, bonus: c.bonus, from: fresh ? fresh.game : null, game: g ? g.name : null, perk: g ? g.perk : null, back: g ? g.back : null, eligible: c.on && !!fresh && !bought, granted: !!Object.values(S.granted).find(x => x.email === e) }; } // public numbers for the game cards (no account needed) function publicView() { const c = cfg(); return { on: c.on, bonus: c.bonus, games: Object.fromEntries(Object.entries(GAMES).map(([k, v]) => [k, { name: v.name, perk: v.perk }])) }; } // a Purchase on chain: the buyer's first package, $5 or more, with a recent game visit -> welcome bonus, once per account async function onEvent(ev) { if (!ev || ev.type !== 'Purchase' || (ev.priceCents || 0) < 500) return null; const c = cfg(); if (!c.on || S.granted[ev.tx]) return null; let earlier = false; try { earlier = X.chain.recentEvents(1e9).some(e => e.type === 'Purchase' && e.buyerId === ev.buyerId && e.tx !== ev.tx && (e.block < ev.block || (e.block === ev.block && e.li < ev.li))); } catch (e) {} if (earlier) return null; const a = await X.accounts.byMemberId(ev.buyerId); if (!a || !a.email) return null; const e = String(a.email).toLowerCase(), f = S.from[e]; if (!f || ev.ts - f.at > c.windowDays * 86400000 || ev.ts < f.at - 60000) return null; // the game visit came first, recently if (Object.values(S.granted).some(x => x.email === e)) return null; // once per account const g = GAMES[f.game] || { name: f.game }; S.granted[ev.tx] = { email: e, memberId: ev.buyerId, game: f.game, bonus: c.bonus, cents: ev.priceCents, at: Date.now() }; S.conv[f.game] = S.conv[f.game] || { buyers: 0, cents: 0 }; S.conv[f.game].buyers++; S.conv[f.game].cents += ev.priceCents; save(); if (c.bonus > 0) await X.ads.addEarned(e, c.bonus, { purchased: true, log: { kind: 'bonus', note: 'Member welcome bonus: +' + c.bonus + ' credits (you came from ' + g.name + ')', ref: 'starter:' + String(ev.tx).slice(0, 30) } }); return S.granted[ev.tx]; } // admin: what each game converted (card taps that reached InstantAdPay, first packages, dollars) function adminView() { const out = {}; for (const k of Object.keys(GAMES)) { const v = S.visits[k] || {}; out[k] = { name: GAMES[k].name, visits: Object.values(v).reduce((n, x) => n + x, 0), buyers: (S.conv[k] || {}).buyers || 0, usd: ((S.conv[k] || {}).cents || 0) / 100 }; } return { settings: cfg(), games: out, granted: Object.values(S.granted).length }; } module.exports = { init, GAMES, setFrom, view, publicView, onEvent, adminView, cfg };