Files
martbost 654cd7b408 Network Tour (Marty 2026-11-17): mission rotation per hunter + free-tier tour stops
- lib/rotation.js: a round = every active house mission; the board offers only missions not found this
  round, never-visited first, then least recently found; mid-round additions join as not found, paused
  missions are skipped; the start gate refuses a mission found earlier this round. State per member in
  rotation.json (start, rounds); everyone starts at 0 so round 1 = their whole history. Paid missions
  keep their rules; daily limits, draws, pool and tiers unchanged. Setting rotationOn (default 1).
- Board: "Network tour: X of Y sites this round" with a progress bar and the sites left; New to you tag.
- Badge sweep renamed Full Network Tour (art unchanged), earned by the old rule or a closed round;
  celebrated on the board every round, round count on the card, Telegram posts it once.
- Free tier: POL only for discovery (first find of a mission). A repeat find is a tour stop: counts as
  the day's hunt and for the tour, 0 POL, ad credits through a signed call to InstantAdPay
  (polgrant.credits -> POST /api/partner/credits, idempotent on ref tour:<row id>); settings
  tourStopCredits 10 / tourStopCreditsDailyMax 10; unconfirmed stops stay owed and retry every 2 min.
  Game Pass holders unchanged. Toured free members see the Game Pass line on the board.
- Admin: Network Tour section (settings, rounds per hunter, credits sent/owed), GET /api/admin/rotation.
- Board daily line now passes the tier (a Pass holder saw 3, not 5).
- fridaybank: http for a local stub IAP (tests). Tests: test/rotation.js (27 checks), run.js waits for
  /health instead of a fixed 1.2 s. Guides: how-a-hunt-works, faq, free-account explain the tour.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 09:20:42 -06:00

102 lines
9.2 KiB
JavaScript

// The Network Tour (Marty 2026-11-17): mission rotation per hunter, and the free tier's tour stops.
//
// Measured 11-03 to 11-17: hunters repeated favorites (old sites 57-73 finds each, the rebuilt network sites 0-6).
// So each hunter works through ROUNDS. A round = every active house mission (advertiser-paid missions are extra
// allowance and stay outside the round). The board offers only the missions this hunter has not found in the
// current round; once every one is found, the round closes and a new one starts. Never-visited missions sort
// first, then the least recently found. A mission added mid-round joins the current round as not found; a paused
// one is skipped and never blocks a round. Daily limits, draws, the pool and the tiers are untouched.
//
// State per member, on the volume (rotation.json): { start, rounds, closed: [ts...] }. `start` = the round began
// after this instant; finds (member id OR wallet, the same rule as doneToday) at or after it count to the round.
// Everyone starts at start 0, so a hunter's first round is their whole history: the sites they never visited are
// exactly what is left, which is the point.
//
// Tour stops (free tier, Marty 2026-11-17): a free member (no active Game Pass, GAMEPASS on) is paid POL only for
// DISCOVERY, a find on a house mission they have never found before. A repeat find is a "tour stop": it counts as
// their hunt for the day, for the round and for the Full Network Tour badge, pays no POL, and grants a few
// InstantAdPay ad credits (settings tourStopCredits / tourStopCreditsDailyMax) through a signed call. A new
// mission pays POL again on each free member's first find of it. Game Pass holders are unchanged.
'use strict';
const store = require('./store');
const missions = require('./missions');
const DEFAULTS = { rotationOn: 1, tourStopCredits: 10, tourStopCreditsDailyMax: 10 };
function cfg() { const s = Object.assign({}, DEFAULTS, store.read('settings', {})); return { on: !!Number(s.rotationOn), credits: Math.max(0, Math.round(Number(s.tourStopCredits) || 0)), dailyMax: Math.max(0, Math.round(Number(s.tourStopCreditsDailyMax) || 0)) }; }
// the missions that make up a round: active house missions (paid ones are extra allowance, not part of the tour)
function roundMissions() { return missions.list().filter(m => m.active && !m.paid); }
// a hunter's house finds, all time, oldest first: non-failed, not prizes / referral drips / detective cases / paid missions
function findsOf(memberId, wallet) {
const w = wallet ? String(wallet).toLowerCase() : null; const id = Number(memberId);
return store.read('payouts', []).filter(p => p.status !== 'failed' && !p.prize && !p.ref && !p.detective && !p.paid && (p.memberId === id || (w && p.wallet && String(p.wallet).toLowerCase() === w)))
.sort((a, b) => (a.at || 0) - (b.at || 0));
}
// has this hunter ever found this mission (all time)? the discovery test
function everFound(memberId, wallet, missionId) { return findsOf(memberId, wallet).some(p => p.missionId === missionId); }
// the round as it stands; closes any finished round on the way (persisted), so every caller sees the same thing
function state(me) {
const key = String(Number(me.memberId)); const finds = findsOf(me.memberId, me.wallet);
const all = store.read('rotation', {}); let st = Object.assign({ start: 0, rounds: 0, closed: [] }, all[key] || {}); let changed = false;
const round = roundMissions(); const ids = new Set(round.map(m => m.id));
for (let guard = 0; guard < 100 && ids.size; guard++) {
const got = new Set(); let closeAt = null;
for (const p of finds) { if ((p.at || 0) < st.start || !ids.has(p.missionId)) continue; got.add(p.missionId); if (got.size === ids.size) { closeAt = p.at || 0; break; } }
if (closeAt == null) break;
st = { start: closeAt + 1, rounds: st.rounds + 1, closed: st.closed.concat([closeAt]).slice(-50) }; changed = true;
}
if (changed) store.update('rotation', {}, a => { a[key] = st; return a; });
const done = new Set(finds.filter(p => (p.at || 0) >= st.start && ids.has(p.missionId)).map(p => p.missionId));
const last = new Map(); for (const p of finds) last.set(p.missionId, p.at || 0); // last find per mission, all time
const left = round.filter(m => !done.has(m.id));
const toured = round.length > 0 && round.every(m => last.has(m.id)); // every active house mission found at least once, ever
return { on: cfg().on, round: st.rounds + 1, rounds: st.rounds, total: round.length, done: round.length - left.length, doneIds: done, left, last, toured, start: st.start };
}
const rounds = memberId => { const st = store.read('rotation', {})[String(Number(memberId))]; return st ? Number(st.rounds) || 0 : 0; };
// the board's order: missions open this round (or every mission with rotation off), never-visited first, then the
// least recently found; the member's daily shuffle (forMember) breaks ties. Paid missions keep their place in the mix.
function order(me, st) {
st = st || state(me); const base = missions.forMember(me.memberId);
const open = st.on ? base.filter(m => m.paid || !st.doneIds.has(m.id)) : base;
const rank = m => st.last.has(m.id) ? st.last.get(m.id) + 1 : 0; // 0 = never found, sorts first
return open.map((m, i) => ({ m, i, r: rank(m) })).sort((a, b) => a.r - b.r || a.i - b.i).map(x => x.m);
}
// may this hunter start this mission? (rotation on: not if it was found earlier in this round; paid missions always)
function blocked(me, m, st) { st = st || state(me); return !!(st.on && !m.paid && st.doneIds.has(m.id)); }
// ---- tour stops --------------------------------------------------------------------------------------------------
// credits already granted (or owed) to this member today for tour stops
function creditsToday(memberId) { const day = missions.ctDay(); return store.read('payouts', []).filter(p => p.memberId === Number(memberId) && p.day === day && p.tourStop).reduce((n, p) => n + (Number(p.tourStop.credits) || 0), 0); }
// a free member's repeat find: the payout row (pol 0, status 'tour') carrying the credits to deliver
function tourStop(me, m) {
const c = cfg(); const credits = Math.max(0, Math.min(c.credits, c.dailyMax - creditsToday(me.memberId)));
const rec = { id: Date.now().toString(36) + Math.random().toString(36).slice(2, 8), memberId: Number(me.memberId), email: me.email, wallet: me.wallet, username: me.username || null,
missionId: m.id, site: m.site, pol: 0, day: missions.ctDay(), at: Date.now(), status: 'tour', tx: null, paidAt: null, error: null,
tourStop: { credits, state: credits > 0 ? 'due' : 'none', tries: 0, sentAt: null } };
store.update('payouts', [], all => { all.push(rec); return all; });
return rec;
}
function markStop(id, patch) { let row = null; store.update('payouts', [], all => { const p = all.find(x => x.id === id); if (p && p.tourStop) { Object.assign(p.tourStop, patch); row = p; } return all; }); return row; }
// deliver one stop's credits through InstantAdPay (polgrant.credits, signed, idempotent on ref 'tour:<row id>')
async function deliver(row, gp) {
if (!row || !row.tourStop || row.tourStop.state !== 'due' || !gp || !gp.credits) return null;
const r = await gp.credits({ memberId: row.memberId, email: row.email, credits: row.tourStop.credits, ref: 'tour:' + row.id, note: 'PolHunter tour stop: ' + row.site });
if (r && r.ok) return markStop(row.id, { state: 'sent', sentAt: Date.now(), granted: r.credits != null ? r.credits : row.tourStop.credits, error: null });
return markStop(row.id, { tries: (row.tourStop.tries || 0) + 1, error: String((r && r.error) || 'no answer').slice(0, 120) }); // stays 'due' (owed): the tick retries
}
// the 2-minute tick: owed credits go out (a stop stays owed until InstantAdPay confirms it)
async function retryDue(gp) { let n = 0; for (const p of store.read('payouts', []).filter(x => x.tourStop && x.tourStop.state === 'due').slice(0, 20)) { const r = await deliver(p, gp); if (r && r.tourStop.state === 'sent') n++; } return n; }
// admin: round stats per hunter (anyone with a house find)
function adminView() {
const seen = new Map(); for (const p of store.read('payouts', [])) if (!p.prize && !p.ref && !p.detective && !p.paid && p.status !== 'failed') seen.set(p.memberId, { memberId: p.memberId, wallet: p.wallet || (seen.get(p.memberId) || {}).wallet, who: p.username ? '@' + p.username : '#' + p.memberId });
const rows = [...seen.values()].map(h => { const s = state(h); return { memberId: h.memberId, who: h.who, round: s.round, rounds: s.rounds, done: s.done, total: s.total, toured: s.toured, left: s.left.map(m => m.site), lastFind: s.last.size ? Math.max(...s.last.values()) : null }; });
rows.sort((a, b) => b.rounds - a.rounds || b.done - a.done);
const stops = store.read('payouts', []).filter(p => p.tourStop);
return { settings: cfg(), hunters: rows, tourStops: { total: stops.length, sent: stops.filter(p => p.tourStop.state === 'sent').length, owed: stops.filter(p => p.tourStop.state === 'due').length, creditsSent: stops.filter(p => p.tourStop.state === 'sent').reduce((n, p) => n + (Number(p.tourStop.granted) || 0), 0), creditsOwed: stops.filter(p => p.tourStop.state === 'due').reduce((n, p) => n + (Number(p.tourStop.credits) || 0), 0) } };
}
module.exports = { DEFAULTS, cfg, roundMissions, findsOf, everFound, state, rounds, order, blocked, creditsToday, tourStop, deliver, retryDue, markStop, adminView };