Files
martbost cf5a9674a2 Friday Bonus Hunts: InstantAdPay counts the banked days
Each day Saturday through Thursday (Central) that a member opens an inbox ad
and watches a video banks one extra PolHunter hunt for the following Friday.
If their inbox had no unread solo ad that day, the video alone counts, so
nobody is blocked by an empty inbox. These are the two slowest-delivering ad
types (about 6-7 views per campaign per day against 40-50 for banners), so
the game sends attention exactly where advertisers are short of it.

fridaybank.js counts from video_seen and solo_inbox.read_ts with DST-safe
Central midnights. /api/my/friday-bank feeds a card on the Earn tab (week
chips, today's two steps with links to the right sub-tab);
/api/hunt/friday-bank answers PolHunter, HMAC-signed with the existing
HUNT_SSO_SECRET, 5-minute window. QA member walk: 0 bugs.

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

77 lines
4.0 KiB
JavaScript

// Friday Bonus Hunts (Marty, 2026-09-26).
//
// PolHunter hunters bank one extra FRIDAY hunt for each day, Saturday through Thursday (Central),
// that they do both of the things InstantAdPay's two slowest ad types need:
// - watch at least one video ad (video_seen has a row for that day), and
// - open at least one inbox (solo) ad that day (solo_inbox.read_ts falls on that day).
// Fairness rule: a member whose inbox had NO unread solo ad at the end of a day can't open one,
// so for them the video alone counts that day. Nobody is blocked by an empty inbox.
//
// Why these two: they were the worst-delivering ad types on 2026-09-26 (about 6-7 views per
// campaign per day against 40-50 for banners and text), because they only get a view when a
// member chooses to open them. The game gives members a reason to.
//
// This module only COUNTS. PolHunter decides how many of the banked hunts can be used (capped
// by how many missions exist) and only on the Friday itself.
'use strict';
let db = null;
function init(deps) { db = deps && deps.db; }
const TZ = 'America/Chicago';
const dayOf = ms => new Date(ms).toLocaleDateString('en-CA', { timeZone: TZ });
// Midnight Central at the START of a 'YYYY-MM-DD' day, as epoch ms. Found by testing the two
// possible offsets (CDT -5, CST -6) instead of assuming one, so it stays right across DST.
function ctStart(dayKey) {
for (const off of [5, 6]) {
const t = Date.parse(dayKey + 'T00:00:00Z') + off * 3600000;
if (dayOf(t) === dayKey && dayOf(t - 1) !== dayKey) return t;
}
return Date.parse(dayKey + 'T05:00:00Z');
}
function addDays(dayKey, n) {
const d = new Date(dayKey + 'T12:00:00Z'); d.setUTCDate(d.getUTCDate() + n); return d.toISOString().slice(0, 10);
}
function weekdayOf(dayKey) { return new Date(dayKey + 'T12:00:00Z').getUTCDay(); } // 0 Sun .. 5 Fri
// The Friday this week's banking counts toward: today if today is Friday, otherwise the next one.
function fridayFor(now) {
const today = dayOf(now || Date.now());
const wd = weekdayOf(today);
return addDays(today, (5 - wd + 7) % 7);
}
// { friday, today, isFriday, window:[Sat..Thu], days:[{day, video, solo, soloNeeded, banked, future}], banked }
async function bankFor(email, now) {
now = now || Date.now();
const e = String(email || '').trim().toLowerCase();
const friday = fridayFor(now), today = dayOf(now);
const days = []; for (let i = 6; i >= 1; i--) days.push(addDays(friday, -i)); // Sat .. Thu
const out = { friday, today, isFriday: today === friday, days: [], banked: 0 };
if (!e || !db || !db.enabled()) { out.days = days.map(d => ({ day: d, video: false, solo: false, soloNeeded: true, banked: false, future: d > today })); return out; }
const winStart = ctStart(days[0]), winEnd = ctStart(friday);
const vids = await db.q('SELECT DISTINCT day FROM video_seen WHERE email=? AND day>=? AND day<=?', [e, days[0], days[5]]);
const vset = new Set(vids.map(r => String(r.day instanceof Date ? r.day.toISOString().slice(0, 10) : r.day).slice(0, 10)));
// every solo row that could matter: delivered before the window closed
const solos = await db.q('SELECT delivered, read_ts FROM solo_inbox WHERE email=? AND delivered < ?', [e, winEnd]);
for (const d of days) {
const s = ctStart(d), eod = ctStart(addDays(d, 1));
const future = d > today;
const video = vset.has(d);
const solo = solos.some(r => r.read_ts && Number(r.read_ts) >= s && Number(r.read_ts) < eod);
// unread at the end of that day = delivered before end of day and not read by then
const unreadAtEnd = solos.some(r => Number(r.delivered) < eod && (!r.read_ts || Number(r.read_ts) >= eod));
// for today, "end of day" hasn't happened; they still need to open one if one is waiting now
const soloNeeded = d === today ? solos.some(r => Number(r.delivered) <= now && !r.read_ts) || solo : unreadAtEnd || solo;
const banked = !future && video && (solo || !soloNeeded);
out.days.push({ day: d, video, solo, soloNeeded, banked, future });
if (banked) out.banked++;
}
return out;
}
module.exports = { init, bankFor, fridayFor, ctStart };