Files
polhunter/lib/fridaybank.js
T
martbost b9e0e40a93 Friday Bonus Hunts: extra Friday hunts banked on InstantAdPay
lib/fridaybank.js asks InstantAdPay (HMAC with HUNT_SSO_SECRET, 3-minute
cache) how many days Sat-Thu the hunter opened an inbox ad and watched a
video. On Friday that many extra hunts are added to the daily limit, capped at
live missions minus the normal three so every bonus has a mission to hunt.
If InstantAdPay can't be reached the bonus is 0: it never grants hunts it could
not verify. The board shows the week's chips and today's steps; the done-for-
today overlay now points at banking. dailyMsg counts past five.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 15:22:23 -05:00

57 lines
2.8 KiB
JavaScript

// Friday Bonus Hunts, PolHunter side (Marty, 2026-09-26).
//
// InstantAdPay counts the days (Saturday through Thursday, Central) on which a hunter opened an
// inbox ad and watched a video there; each such day banks one extra hunt, usable ONLY on the
// Friday that follows. This module asks InstantAdPay for that count, signed with the same
// shared secret as the sign-in hand-off, and turns it into today's bonus.
//
// Usable bonus = banked, capped at (live missions - the normal daily limit): every mission can be
// found once a day, so a bonus larger than the missions left would be a promise with nothing to
// hunt. With 8 missions and 3 a day that cap is 5; it rises on its own as missions are added.
//
// Failure is safe in one direction only: if InstantAdPay can't be reached, the bonus is 0 and the
// hunter keeps their normal missions. It never grants hunts it could not verify.
'use strict';
const crypto = require('crypto');
const https = require('https');
const IAP = String(process.env.IAP_URL || 'https://instantadpay.com').replace(/\/+$/, '');
const cache = new Map(); // email -> { at, data }
const TTL = 3 * 60 * 1000;
function fetchJson(url) {
return new Promise((resolve) => {
const rq = https.get(url, { timeout: 8000 }, r => {
let raw = ''; r.setEncoding('utf8'); r.on('data', d => { raw += d; });
r.on('end', () => { try { resolve(r.statusCode === 200 ? JSON.parse(raw) : null); } catch (e) { resolve(null); } });
});
rq.on('timeout', () => { rq.destroy(); resolve(null); });
rq.on('error', () => resolve(null));
});
}
async function bankFor(email) {
const e = String(email || '').trim().toLowerCase();
const secret = String(process.env.HUNT_SSO_SECRET || '').trim();
if (!e || secret.length < 32) return null;
const hit = cache.get(e);
if (hit && Date.now() - hit.at < TTL) return hit.data;
const ts = Date.now();
const sig = crypto.createHmac('sha256', secret).update('fridaybank:' + e + ':' + ts).digest('hex');
const data = await fetchJson(IAP + '/api/hunt/friday-bank?email=' + encodeURIComponent(e) + '&ts=' + ts + '&sig=' + sig);
if (data && Array.isArray(data.days)) { cache.set(e, { at: Date.now(), data }); return data; }
if (hit) return hit.data; // a stale answer beats none for display
return null;
}
// { banked, usable, bonus (today's extra hunts), isFriday, friday, today, days, cap }
async function forHunter(me, activeMissions, base) {
const b = await bankFor(me && me.email);
const cap = Math.max(0, Number(activeMissions || 0) - Number(base || 3));
if (!b) return { banked: 0, usable: 0, bonus: 0, isFriday: false, cap, available: false };
const usable = Math.min(b.banked, cap);
return { banked: b.banked, usable, bonus: b.isFriday ? usable : 0, isFriday: b.isFriday, friday: b.friday, today: b.today, days: b.days, cap, available: true };
}
module.exports = { forHunter, bankFor };