diff --git a/lib/polgrant.js b/lib/polgrant.js index 24e6dc5..38e514f 100644 --- a/lib/polgrant.js +++ b/lib/polgrant.js @@ -58,7 +58,9 @@ async function grant({ memberId, email, game, kind, pol, ref }) { const { status, json } = await post('/api/partner/polgrant', body); if (status !== 200 || !json || typeof json.allowed !== 'number') return { allowed: 0, deferred: 0, carried: 0, pass: false, retry: true, error: 'http ' + status + (json && json.error ? ': ' + json.error : '') }; const out = { allowed: r4(Math.max(0, json.allowed)), deferred: r4(Math.max(0, Number(json.deferred) || 0)), carried: r4(Math.max(0, Number(json.carried) || 0)), pass: !!(json.pass && (json.pass === true || json.pass.active)), day: json.day || ctDay(), capHit: json.capHit || null, enforced: json.enforced !== false, repeat: !!json.repeat, suspended: !!json.suspended, lifetimeHit: !!json.lifetimeHit, freeLifetime: Number(json.freeLifetime) || 0, waiting: r4(Math.max(0, Number(json.waiting) || 0)), retry: false }; - rememberPass(body.memberId, body.email, json.pass, json.passUntil || (json.pass && json.pass.until)); + // a REPEATED ref answers with the pass state of the day it was first granted (IAP gamepass.grant prev.pass), which can be weeks + // old: never let that overwrite what we know now (2026-11-23, @bizventures recorded as no pass while holding one) + if (!json.repeat) rememberPass(body.memberId, body.email, json.pass, json.passUntil || (json.pass && json.pass.until)); return out; } catch (e) { return { allowed: 0, deferred: 0, carried: 0, pass: false, retry: true, error: String(e && e.message || e).slice(0, 120) }; } } @@ -92,6 +94,18 @@ async function fetchPass(memberId, email) { return passOf(memberId, email); } catch (e) { return cur; } } +// ask InstantAdPay NOW, whatever the stored copy says (the weekly prize run, 2026-11-23): { ok, active }. ok false = IAP did not +// answer, so the caller must wait and ask again, never decide on the stored copy. +async function freshPass(memberId, email) { + const who = whoOf(memberId, email); if (!ready() || !who) return { ok: false, active: false }; + try { + const { status, json } = await get('/api/partner/player', who, { m: Number(memberId) || 0, e: String(email || '').toLowerCase() }); + if (status === 404) { rememberPass(memberId, email, false); return { ok: true, active: false }; } + if (status !== 200 || !json || json.pass == null) return { ok: false, active: false }; + rememberPass(memberId, email, json.pass, json.pass && json.pass.until); + return { ok: true, active: passOf(memberId, email).active }; + } catch (e) { return { ok: false, active: false }; } +} // what the lobby shows: { on, active, until, untilText, url } function view(memberId, email) { const p = whoOf(memberId, email) ? passOf(memberId, email) : { active: false, until: null }; @@ -172,4 +186,4 @@ async function credits({ memberId, email, credits: n, ref, note }) { try { const { status, json } = await post('/api/partner/credits', body); if (status === 200 && json && json.ok) return { ok: true, credits: Number(json.credits) || 0, repeat: !!json.repeat }; return { ok: false, error: 'http ' + status + (json && json.error ? ': ' + json.error : '') }; } catch (e) { return { ok: false, error: String(e && e.message || e).slice(0, 120) }; } } -module.exports = { use, on, whoOf, grant, voidGrant, credits, passOf, rememberPass, fetchPass, view, note, enqueue, queue, queued, dequeue, retry, startRetry, stopRetry, tournamentPending, tournamentPaid, PASS_URL, RETRY_MS, GIVE_UP_MS, ctDay, r4, _cfg: () => cfg }; +module.exports = { use, on, whoOf, grant, voidGrant, credits, passOf, rememberPass, fetchPass, freshPass, view, note, enqueue, queue, queued, dequeue, retry, startRetry, stopRetry, tournamentPending, tournamentPaid, PASS_URL, RETRY_MS, GIVE_UP_MS, ctDay, r4, _cfg: () => cfg }; diff --git a/lib/social.js b/lib/social.js index 94e81b6..1b1c835 100644 --- a/lib/social.js +++ b/lib/social.js @@ -133,8 +133,27 @@ function awardFill(monday) { return { week: monday, winners: added, filled: added.length }; } // on every tick: the most recent completed week gets awarded on the first tick after it ends -function awardDue(notify) { +// 2026-11-23: before the award, every hunter who could win (minimum finds reached) has their Game Pass asked fresh from InstantAdPay, +// because awardWeek reads the stored copy and a stale "no pass" cost @bizventures 2nd place. If InstantAdPay does not answer for any +// of them, nothing is awarded on this tick and the next tick (2 minutes) tries again. +let awarding = false; +async function awardDue(notify) { const last = dayMinus(weekOf(ctDay()), 7); + if (awarding || store.read('prizes', []).some(x => x.week === last)) return null; + awarding = true; + try { + let gp = null; try { gp = require('./polgrant'); } catch (e) {} + if (gp && gp.on && gp.on() && gp.freshPass) { + const rules = prizeRules(); + for (const r of weekRows(last).filter(x => x.finds >= rules.minFinds)) { + const f = await gp.freshPass(r.memberId, r.email); + if (!f.ok) { console.warn('prizes: pass check did not answer for', r.who, '- award waits for the next tick'); return null; } + } + } + return awardNow(last, notify); + } finally { awarding = false; } +} +function awardNow(last, notify) { if (store.read('prizes', []).some(x => x.week === last)) return null; const r = awardWeek(last); if (r && r.winners && r.winners.length && notify) notify('prize', r).catch(() => {}); diff --git a/server.js b/server.js index b4a3bd6..ff7bf9e 100644 --- a/server.js +++ b/server.js @@ -456,6 +456,6 @@ async function postMissedBadges() { } setInterval(() => postMissedFinds().catch(() => {}), 2 * 60000); setTimeout(() => postMissedFinds().catch(() => {}), 20000); // weekly prizes: the week that just ended is awarded on the first tick after Sunday, Central -setInterval(() => { try { social.awardDue(notify); } catch (e) { console.error('prizes', e.message); } }, 2 * 60000); +setInterval(() => { Promise.resolve().then(() => social.awardDue(notify)).catch(e => console.error('prizes', e.message)); }, 2 * 60000); server.listen(PORT, () => console.log(`PolHunter on :${PORT} — outbound: ${outbound() ? 'ON' : 'OFF'} — sign-ups: ${signupsOpen() ? 'open' : 'CLOSED'} — curtain: ${CURTAIN ? 'up' : 'down'} — sso: ${sso.enabled() ? 'on' : 'off'} — faucet: ${faucetOn ? faucet.address() + ' chain ' + (process.env.HUNT_CHAIN_ID || '?') : 'off'}`));