Files
polhunter/lib/social.js
T

163 lines
13 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Leaderboard, badges and share links. All derived from the payout ledger; nothing new is stored.
//
// A "find" is any non-failed payout (paid, sent, due or queued): the hunter did the work. POL counts
// paid drips only, so the board never shows money that has not left the faucet. Days and weeks are
// Central (ctDay), the same clock as the pool.
//
// settings.leaderboardExclude: member ids kept off the public boards (default: the company account #1).
'use strict';
const store = require('./store');
const { ctDay } = require('./missions');
const rewards = require('./rewards');
const missions = require('./missions');
const BADGES = [
{ id: 'first', name: 'First Find', icon: '\u{1F3AF}', why: 'your first code claimed' },
{ id: 'hunter', name: 'Hunter', icon: '\u{1F9ED}', why: 'five finds' },
{ id: 'tracker', name: 'Tracker', icon: '\u{1F526}', why: 'twenty finds' },
{ id: 'sweep', name: 'Full Network Tour',icon: '\u{1F5FA}️', why: 'a find on every site in one round of the network tour' }, // renamed 2026-11-17 (the art still reads FULL SWEEP; only the title changed)
{ id: 'lucky', name: 'Lucky Find', icon: '\u{1F48E}', why: 'one drip of 0.4 POL or more' },
{ id: 'streak', name: 'On a Streak', icon: '\u{1F525}', why: 'finds on three days in a row' },
{ id: 'top', name: 'Top Hunter', icon: '\u{1F3C6}', why: 'won a weekly prize as #1' },
{ id: 'recruiter', name: 'Recruiter', icon: '\u{1F91D}', why: 'brought a hunter who found their first code' },
// 2026-09-26: tiers on top of the originals, all derived from the ledger, so they count work already done
{ id: 'weekstreak', name: '7-Day Streak', icon: '⚡', why: 'finds on seven days in a row' },
{ id: 'weeksweep', name: 'Week Sweep', icon: '\u{1F5D3}\uFE0F', why: 'a find on every site inside one week' },
{ id: 'scout', name: 'Scout', icon: '\u{1F6A9}', why: 'brought three hunters who found their first code' },
{ id: 'guide', name: 'Guide', icon: '\u{1F3EE}', why: 'brought ten hunters who found their first code' },
{ id: 'trailboss', name: 'Trail Boss', icon: '\u{1F3D4}\uFE0F', why: 'brought twenty-five hunters who found their first code' },
// Ad Detective (Mystery Hunt Box): correct answers, never finds
{ id: 'detective', name: 'Detective', icon: '\u{1F50E}', why: 'solved your first Ad Detective case' },
{ id: 'sleuth', name: 'Sleuth', icon: '\u{1F463}', why: 'solved five Ad Detective cases' },
{ id: 'investigator', name: 'Investigator', icon: '\u{1F4CC}', why: 'solved fifteen Ad Detective cases' },
{ id: 'masterdetective', name: 'Master Detective', icon: '\u{1F5DD}\uFE0F', why: 'solved thirty Ad Detective cases' },
];
const PRIZE_DEFAULTS = { weeklyPrizes: [3, 2, 1], weeklyMinFinds: 10 };
function prizeRules() { const s = rewards.settings(); return { pol: Array.isArray(s.weeklyPrizes) && s.weeklyPrizes.length ? s.weeklyPrizes.map(Number) : PRIZE_DEFAULTS.weeklyPrizes, minFinds: Number(s.weeklyMinFinds) > 0 ? Number(s.weeklyMinFinds) : PRIZE_DEFAULTS.weeklyMinFinds }; }
function excluded() { const s = rewards.settings(); const list = Array.isArray(s.leaderboardExclude) ? s.leaderboardExclude : [1]; return new Set(list.map(Number)); }
function finds() { return store.read('payouts', []).filter(p => p.status !== 'failed' && !p.prize && !p.ref && !p.detective); } // prize, referral and Ad Detective drips are not finds
// Central day arithmetic on 'YYYY-MM-DD' keys
function dayMinus(dayKey, n) { const [y, m, d] = dayKey.split('-').map(Number); const t = Date.UTC(y, m - 1, d) - n * 86400000; return new Date(t).toISOString().slice(0, 10); }
// the Monday that starts a day's week (weeks run Monday to Sunday, Central)
function weekOf(dayKey) { const [y, m, d] = dayKey.split('-').map(Number); const dow = (new Date(Date.UTC(y, m - 1, d)).getUTCDay() + 6) % 7; return dayMinus(dayKey, dow); }
function inPeriod(p, period, today) {
if (period === 'all') return true;
if (period === 'month') return p.day.slice(0, 7) === today.slice(0, 7);
return weekOf(p.day) === weekOf(today); // week: this Monday-to-Sunday week
}
function badgesFor(memberId, list) {
const mine = (list || finds()).filter(p => p.memberId === Number(memberId));
const out = new Set();
// Ad Detective cases solved: their own family, earned even by someone with no finds counted yet
const all = store.read('payouts', []);
const solved = all.filter(p => p.memberId === Number(memberId) && p.detective && p.status !== 'failed').length;
if (solved >= 1) out.add('detective'); if (solved >= 5) out.add('sleuth'); if (solved >= 15) out.add('investigator'); if (solved >= 30) out.add('masterdetective');
if (!mine.length) return BADGES.filter(b => out.has(b.id));
out.add('first');
if (mine.length >= 5) out.add('hunter');
if (mine.length >= 20) out.add('tracker');
const sites = new Set(missions.list().filter(m => m.active !== false).map(m => m.site));
if (sites.size && [...sites].every(s => mine.some(p => p.site === s))) out.add('sweep'); // the original rule, so nobody loses it
try { if (require('./rotation').rounds(memberId) >= 1) out.add('sweep'); } catch (e) {} // a closed round of the Network Tour (once per round, count on the card)
if (mine.some(p => p.pol >= 0.4)) out.add('lucky');
const days = new Set(mine.map(p => p.day));
for (const d of days) { if (days.has(dayMinus(d, 1)) && days.has(dayMinus(d, 2))) { out.add('streak'); break; } }
for (const d of days) { let run = true; for (let i = 1; i < 7; i++) if (!days.has(dayMinus(d, i))) { run = false; break; } if (run) { out.add('weekstreak'); break; } }
if (sites.size) { const wk = new Map(); for (const p of mine) { const w = weekOf(p.day); if (!wk.has(w)) wk.set(w, new Set()); wk.get(w).add(p.site); } for (const s of wk.values()) if ([...sites].every(x => s.has(x))) { out.add('weeksweep'); break; } }
if (store.read('payouts', []).some(p => p.memberId === Number(memberId) && p.prize && p.prize.rank === 1 && p.status !== 'failed')) out.add('top');
// earned by a bounty, not a match: someone they brought turned up and found their first code
const brought = all.filter(p => p.memberId === Number(memberId) && p.ref && p.ref.kind === 'bounty' && p.status !== 'failed').length;
if (brought >= 1) out.add('recruiter'); if (brought >= 3) out.add('scout'); if (brought >= 10) out.add('guide'); if (brought >= 25) out.add('trailboss');
return BADGES.filter(b => out.has(b.id));
}
function leaderboard(period, limit) {
const today = ctDay(); const ex = excluded(); const all = finds();
const by = new Map();
for (const p of all) {
if (ex.has(p.memberId) || !inPeriod(p, period, today)) continue;
const r = by.get(p.memberId) || { memberId: p.memberId, who: p.username ? '@' + p.username : '#' + p.memberId, finds: 0, pol: 0, last: 0 };
r.finds++; if (p.status === 'paid') r.pol += p.pol; r.last = Math.max(r.last, p.at); by.set(p.memberId, r);
}
const rows = [...by.values()].sort((a, b) => b.finds - a.finds || b.pol - a.pol || a.last - b.last);
return rows.slice(0, limit || 25).map((r, i) => Object.assign(r, { rank: i + 1, pol: Math.round(r.pol * 10000) / 10000, badges: badgesFor(r.memberId, all).map(b => b.icon) }));
}
// standings for one fixed week (Monday key), qualified by the minimum finds
function weekRows(monday) {
const ex = excluded(); const all = finds(); const end = dayMinus(monday, -6); const by = new Map();
for (const p of all) {
if (ex.has(p.memberId) || p.day < monday || p.day > end) continue;
const r = by.get(p.memberId) || { memberId: p.memberId, who: p.username ? '@' + p.username : '#' + p.memberId, username: p.username || null, email: p.email, wallet: p.wallet, finds: 0, pol: 0, last: 0 };
r.finds++; if (p.status === 'paid') r.pol += p.pol; r.last = Math.max(r.last, p.at); if (p.wallet) r.wallet = p.wallet; by.set(p.memberId, r);
}
return [...by.values()].sort((a, b) => b.finds - a.finds || b.pol - a.pol || a.last - b.last);
}
// award one week, once: prize drips go on the ledger as due, the faucet pays them like any drip
function awardWeek(monday) {
if (!/^\d{4}-\d{2}-\d{2}$/.test(monday || '') || weekOf(monday) !== monday) return { error: 'not a Monday key' };
const done = store.read('prizes', []);
if (done.some(x => x.week === monday)) return { skipped: 'already awarded', week: monday };
const rules = prizeRules();
// weekly prizes are for Game Pass holders (Marty 2026-11-02: the Pass is the purchase that funds the game; confirmed 2026-11-23, with
// the leaderboard page now saying so in plain words); with GAMEPASS off, everyone qualifies as before
let passOk = () => true; try { const gp = require('./polgrant'); if (gp.on && gp.on()) passOk = r => { try { return !!gp.passOf(r.memberId, r.email).active; } catch (e) { return false; } }; } catch (e) {}
const winners = weekRows(monday).filter(r => r.finds >= rules.minFinds && passOk(r)).slice(0, rules.pol.length).map((r, i) => Object.assign({ rank: i + 1, prizePol: rules.pol[i] }, r));
const day = ctDay(), at = Date.now();
const recs = winners.map(w => ({ id: 'prize' + monday.replace(/-/g, '') + '-' + w.rank, memberId: w.memberId, email: w.email, wallet: w.wallet, username: w.username, missionId: 'prize:' + monday, site: 'Weekly prize #' + w.rank, pol: w.prizePol, day, at, status: 'due', tx: null, paidAt: null, error: null, prize: { week: monday, rank: w.rank } }));
if (recs.length) store.update('payouts', [], all => { all.push(...recs); return all; });
const entry = { week: monday, at, rules, winners: winners.map(w => ({ rank: w.rank, memberId: w.memberId, who: w.who, finds: w.finds, pol: w.pol, prizePol: w.prizePol })) };
store.update('prizes', [], all => { all.push(entry); return all; });
return entry;
}
// ONE-TIME catch-up for a week that was already awarded (Marty 2026-11-23: the week of 11-16 paid only the one Pass holder while the
// leaderboard page did not say a Pass was needed, so he paid places 2 and 3 once). It ignores the Pass on purpose: the top hunters
// by finds are picked again, and anyone in that list who was not paid for the week gets the prize of their place. Nobody already
// paid is touched, nothing is paid twice (a payout row for that member and week blocks it), and a second call adds nothing.
function awardFill(monday) {
if (!/^\d{4}-\d{2}-\d{2}$/.test(monday || '') || weekOf(monday) !== monday) return { error: 'not a Monday key' };
const done = store.read('prizes', []), cur = done.find(x => x.week === monday);
if (!cur) return { error: 'that week has not been awarded yet' };
const rules = prizeRules(), paidTo = new Set(store.read('payouts', []).filter(p => p.prize && p.prize.week === monday).map(p => p.memberId));
const top = weekRows(monday).filter(r => r.finds >= rules.minFinds).slice(0, rules.pol.length).map((r, i) => Object.assign({ rank: i + 1, prizePol: rules.pol[i] }, r));
const add = top.filter(w => !paidTo.has(w.memberId));
if (!add.length) return { week: monday, winners: [], filled: 0 };
const day = ctDay(), at = Date.now();
const recs = add.map(w => ({ id: 'prize' + monday.replace(/-/g, '') + '-f' + w.rank, memberId: w.memberId, email: w.email, wallet: w.wallet, username: w.username, missionId: 'prize:' + monday, site: 'Weekly prize #' + w.rank, pol: w.prizePol, day, at, status: 'due', tx: null, paidAt: null, error: null, prize: { week: monday, rank: w.rank, fill: true } }));
store.update('payouts', [], all => { all.push(...recs); return all; });
const added = add.map(w => ({ rank: w.rank, memberId: w.memberId, who: w.who, finds: w.finds, pol: w.pol, prizePol: w.prizePol }));
store.update('prizes', [], all => { const e = all.find(x => x.week === monday); if (e) { e.winners = e.winners.concat(added).sort((a, b) => a.rank - b.rank); e.filledAt = at; } return all; });
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) {
const last = dayMinus(weekOf(ctDay()), 7);
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(() => {});
return r;
}
function prizes() { const all = store.read('prizes', []); return { rules: prizeRules(), week: weekOf(ctDay()), last: all.length ? all[all.length - 1] : null, history: all.slice(-8).reverse() }; }
function rankOf(memberId, period) {
const rows = leaderboard(period, 100000); const i = rows.findIndex(r => r.memberId === Number(memberId));
return i < 0 ? null : { rank: i + 1, of: rows.length, finds: rows[i].finds, pol: rows[i].pol };
}
// the member's share link: PolHunter's landing with their IAP referral, which the landing turns into
// their instantadpay.com/join/<ref> link for everyone who signs up from it
// the public name used in share URLs: the IAP username, else m<memberId>; and back again
function nameOf(me) { return me.username ? String(me.username).toLowerCase() : 'm' + me.memberId; }
function memberByName(who) {
const w = String(who || '').toLowerCase(); if (!w) return null;
const all = store.read('payouts', []);
const p = all.find(x => (x.username && String(x.username).toLowerCase() === w) || 'm' + x.memberId === w);
return p ? { memberId: p.memberId, username: p.username || null, who: p.username ? '@' + p.username : '#' + p.memberId } : null;
}
function shareLink(site, me) { const ref = me.username || me.memberId; return site + '/?r=' + encodeURIComponent(String(ref)); }
module.exports = { BADGES, badgesFor, leaderboard, rankOf, shareLink, weekOf, weekRows, awardWeek, awardFill, awardDue, prizes, prizeRules, nameOf, memberByName };