Files

61 lines
3.4 KiB
JavaScript

// Member welcome offer (Marty, 2026-10-05): turn players into InstantAdPay members through the $5 package.
//
// After a chapter clear, a signed-in player with no InstantAdPay member id yet (the hand-off carried memberId 0, so they
// play on the negative email id) gets one card a Central day: "Become an InstantAdPay member". Never a guest, never
// during a run. InstantAdPay owns the purchase and its bonus credits; the bonus number comes from its public endpoint
// (cached ten minutes) and the offer stays off while InstantAdPay says it is off. If the endpoint cannot be read the
// card still shows, without a number.
//
// The perk here: the first time a member (memberId > 0) comes through the hand-off, one free hangar upgrade. It is a
// token on the player (freeUpgrades) that makes the next hangar purchase cost nothing, drips or POL. A free POL item
// moves no POL: nothing leaves the pool and nothing is paid out, so Marty's "no POL the game did not earn" rule holds.
//
// Player record fields: starterShown (the Central day the card last showed), perkAt (ms, granted once, never again),
// freeUpgrades (tokens left), perkNotice (the one-time lobby message is still to be shown).
'use strict';
const game = require('./game');
const ledger = require('./ledger');
const IAP = () => String(process.env.IAP_URL || 'https://instantadpay.com').replace(/\/+$/, '');
const PERK = 'a free hangar upgrade';
const NOTICE = 'Member perk: a free hangar upgrade. Thanks for joining InstantAdPay!';
let cache = { at: 0, v: null };
// InstantAdPay's public offer numbers: { on, bonus } or null when unreadable (a failure is remembered for a minute)
async function publicOffer() {
if (Date.now() - cache.at < 10 * 60000) return cache.v;
try {
const r = await fetch(IAP() + '/api/starter/public', { signal: AbortSignal.timeout(5000) }); if (!r.ok) throw new Error('http ' + r.status);
const j = await r.json(); const b = Number(j.bonus);
cache = { at: Date.now(), v: { on: j.on !== false, bonus: isFinite(b) && b > 0 ? Math.round(b) : null } };
} catch (e) { cache = { at: Date.now() - 9 * 60000, v: null }; }
return cache.v;
}
// the card for this clear, or null. Counts as shown the moment it is handed out.
async function offerFor(me) {
if (!me || me.guest || Number(me.memberId) > 0) return null;
const p = game.player(me.memberId); const today = ledger.ctDay();
if (!p || p.starterShown === today) return null;
const pub = await publicOffer();
if (pub && pub.on === false) return null;
let fresh = false; game.updPlayer(me.memberId, x => { if (x.starterShown !== today) { x.starterShown = today; fresh = true; } });
if (!fresh) return null;
return { bonus: pub ? pub.bonus : null, perk: PERK, url: IAP() + '/my?from=driprunner#buy' };
}
// the hand-off: a member's first visit as a member gets the free upgrade, once per account
function grantPerk(memberId) {
if (!(Number(memberId) > 0)) return false; let granted = false;
game.updPlayer(memberId, x => { if (!x.perkAt) { x.perkAt = Date.now(); x.freeUpgrades = (Number(x.freeUpgrades) || 0) + 1; x.perkNotice = true; granted = true; } });
return granted;
}
// the one-time lobby message: handed out once, then cleared
function takeNotice(memberId) {
const p = game.player(memberId); if (!p || !p.perkNotice) return null;
game.updPlayer(memberId, x => { delete x.perkNotice; });
return NOTICE;
}
module.exports = { publicOffer, offerFor, grantPerk, takeNotice, PERK };