Mystery Hunt Box + Ad Detective, badge tiers

Finish the day's Code Hunts to unlock one box a day. It opens to an Ad
Detective case on one site with a fixed 0.15 POL reward shown up front:
visit (the embed records the visit after the dwell, no code), then answer
one question, one attempt. Paid inside the house cap, never counted as a
find. 34-question bank seeded from lib/detective-bank.json; a hunter never
repeats a question until they have seen every one for that site. Admin
panel with per-question stats and retire/restore.

New badges (all ledger-derived): Detective, Sleuth, Investigator, Master
Detective; Scout, Guide, Trail Boss; Week Sweep; 7-Day Streak. Badges
already earned on deploy are marked announced without posting.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-09-26 16:10:17 -05:00
parent 64ab0b505f
commit 469cbf69a5
25 changed files with 1334 additions and 423 deletions
+37 -2
View File
@@ -25,6 +25,7 @@ const social = require('./lib/social');
const badge = require('./lib/badge');
const referrals = require('./lib/referrals'); // pays a hunter for bringing someone who turns up and hunts
const faucet = require('./lib/faucet');
const detective = require('./lib/detective'); // Mystery Hunt Box + Ad Detective (2026-09-26)
const PORT = Number(process.env.PORT || 3000);
const DATA_DIR = process.env.DATA_DIR || path.join(__dirname, 'data');
@@ -42,6 +43,19 @@ const signupsOpen = () => process.env.SIGNUPS === 'open';
store.init(DATA_DIR);
const faucetOn = faucet.init();
detective.init();
// Badges added after launch count work already done, so on the day they ship many hunters hold them at once.
// Those are marked as announced without posting, or the badge sweep would flood Telegram with old news.
// Badges earned from here on are announced as usual. Runs once per new badge id.
(function seedNewBadges() {
const ORIGINAL = ['first', 'hunter', 'tracker', 'sweep', 'lucky', 'streak', 'top', 'recruiter'];
const known = new Set(store.read('badges-known', ORIGINAL));
const fresh = social.BADGES.map(b => b.id).filter(id => !known.has(id)); if (!fresh.length) return;
const members = new Set(store.read('payouts', []).map(p => p.memberId)); let n = 0;
store.update('badges-posted', {}, all => { for (const id of members) { const have = new Set(all[String(id)] || []); for (const b of social.badgesFor(id)) if (fresh.includes(b.id) && !have.has(b.id)) { have.add(b.id); n++; } all[String(id)] = [...have]; } return all; });
store.write('badges-known', [...known, ...fresh]);
console.log('new badges', fresh.join(','), '- marked', n, 'already-earned as announced (no posts)');
})();
// ---- Telegram (outward: gated) ------------------------------------------------------------
async function telegram(text) {
@@ -70,6 +84,7 @@ async function notify(kind, p) {
return notifyOther(kind, p);
}
async function notifyPaid(p) {
if (p.detective) { const ok = await telegram('\u{1F50E} <b>PolHunter</b> · ' + (p.username ? '@' + p.username : '#' + p.memberId) + ' cracked an <b>Ad Detective</b> case on ' + p.site + ' and got <b>' + fmt(p.pol) + ' POL</b> · <a href="' + explorer() + '/tx/' + p.tx + '">verify</a>\n<a href="' + SITE + '">Hunt yours</a>'); if (ok && p.id) rewards.mark(p.id, { posted: Date.now() }); return ok; }
{ const ok = await telegram('\u{1F3AF} <b>PolHunter</b> · ' + (p.username ? '@' + p.username : '#' + p.memberId) + ' found it on ' + p.site + ' and got <b>' + fmt(p.pol) + ' POL</b> · <a href="' + explorer() + '/tx/' + p.tx + '">verify</a>\n<a href="' + SITE + '">Hunt yours</a>'); if (ok && p.id) rewards.mark(p.id, { posted: Date.now() }); return ok; }
}
async function notifyOther(kind, p) {
@@ -230,7 +245,7 @@ const server = http.createServer(async (req, res) => {
return json(res, 200, { me: { memberId: me.memberId, username: me.username, wallet: me.wallet }, missions: missions.forMember(me.memberId).map(m => Object.assign(pubMission(m), { done: done.has(m.id) || wdone.has(m.id) })), drips: rewards.mine(me.memberId).slice(0, 20), faucet: { on: faucetOn }, pool: rewards.pool(), daily: rewards.daily(me.memberId, rewards.doneToday(me.memberId, me.wallet), fbank.bonus), fridayBank: fbank,
badges: social.badgesFor(me.memberId), badgeCards: (() => { const got = new Set(social.badgesFor(me.memberId).map(b => b.id)); const who = social.nameOf(me); return social.BADGES.map(b => ({ id: b.id, name: b.name, icon: b.icon, why: b.why, art: '/badges/badge-' + b.id + '.jpg', earned: got.has(b.id), page: got.has(b.id) ? SITE + '/b/' + who + '/' + b.id : null, image: got.has(b.id) ? SITE + '/badge-img/' + who + '/' + b.id + '.jpg' : null })); })(), rank: { week: social.rankOf(me.memberId, 'week'), month: social.rankOf(me.memberId, 'month'), all: social.rankOf(me.memberId, 'all') },
share: { link: social.shareLink(SITE, me), joinUrl: 'https://instantadpay.com/join/' + encodeURIComponent(String(me.username || me.memberId)) + '?from=polhunter' },
referral: referrals.state(me) });
referral: referrals.state(me), box: detective.view(me) });
}
if (p === '/api/my/start' && req.method === 'POST') {
const b = await readBody(req); const m = missions.get(String(b.missionId || '')); if (!m || !m.active) return json(res, 404, { error: 'That mission is not open.' });
@@ -265,6 +280,23 @@ const server = http.createServer(async (req, res) => {
for (const b of unlocked) notify('badge', { me, id: b.id }).catch(() => {});
return json(res, 200, { ok: true, pol: g.rec.pol, queued: g.queued, unlocked: unlocked.map(b => b.id), message: g.queued ? 'Found it. Today’s POL is spoken for, so yours is queued and pays out next.' : 'Found it. ' + fmt(g.rec.pol) + ' POL is on its way to your wallet.' });
}
// ---- Mystery Hunt Box + Ad Detective
if (p === '/api/my/box/open' && req.method === 'POST') { const r = detective.open(me); return json(res, r.error ? 400 : 200, r.error ? r : { ok: true, box: detective.view(me) }); }
if (p === '/api/my/box/start' && req.method === 'POST') {
if (!me.wallet) return json(res, 400, { error: 'Link a wallet on InstantAdPay first so the reward has somewhere to land.' });
if (limited('boxstart:' + me.memberId, 20, 3600000)) return json(res, 429, { error: 'Easy. Try again in a little while.' });
const r = detective.start(me); return json(res, r.error ? 400 : 200, r);
}
if (p === '/api/my/box/answer' && req.method === 'POST') {
const b = await readBody(req);
if (limited('boxans:' + me.memberId, 10, 3600000)) return json(res, 429, { error: 'Easy. Try again in a little while.' });
const before = new Set(social.badgesFor(me.memberId).map(x => x.id));
const r = detective.answer(me, b.choice); if (r.error) return json(res, 400, r);
const unlocked = r.correct ? social.badgesFor(me.memberId).filter(x => !before.has(x.id)) : [];
for (const x of unlocked) notify('badge', { me, id: x.id }).catch(() => {});
const msg = r.correct ? (r.queued ? 'Case solved. Today\u2019s POL is spoken for, so your ' + fmt(r.pol) + ' POL is queued and pays out next.' : 'Case solved. ' + fmt(r.pol) + ' POL is on its way to your wallet.') : 'Not this time. The answer was: ' + r.answer + '. A new box unlocks tomorrow.';
return json(res, 200, { ok: true, correct: r.correct, pol: r.pol, queued: r.queued, unlocked: unlocked.map(x => x.id), message: msg, box: detective.view(me) });
}
return json(res, 404, { error: 'No such call.' });
}
@@ -282,9 +314,12 @@ const server = http.createServer(async (req, res) => {
return json(res, 200, { ok: true, missions: missions.list() });
}
if (p === '/api/admin/mission' && req.method === 'DELETE') { const b = await readBody(req); missions.remove(String(b.id || '')); return json(res, 200, { ok: true, missions: missions.list() }); }
if (p === '/api/admin/settings' && req.method === 'POST') { const b = await readBody(req); const patch = {}; for (const k of ['minPol', 'maxPol', 'dailyCapPol', 'lowBalancePol', 'weeklyMinFinds', 'drawSkew', 'missionsPerDay', 'refEnabled', 'refBountyPol', 'refMatchPct', 'refMatchDays', 'refNewDays', 'refCapPol', 'refMatchMinPol']) if (b[k] != null && Number(b[k]) >= 0) patch[k] = Number(b[k]); for (const k of ['weeklyPrizes', 'leaderboardExclude']) if (Array.isArray(b[k])) patch[k] = b[k].map(Number).filter(n => n >= 0); return json(res, 200, { ok: true, settings: rewards.setSettings(patch) }); }
if (p === '/api/admin/settings' && req.method === 'POST') { const b = await readBody(req); const patch = {}; for (const k of ['minPol', 'maxPol', 'dailyCapPol', 'lowBalancePol', 'weeklyMinFinds', 'drawSkew', 'missionsPerDay', 'detectiveOn', 'detectivePol', 'refEnabled', 'refBountyPol', 'refMatchPct', 'refMatchDays', 'refNewDays', 'refCapPol', 'refMatchMinPol']) if (b[k] != null && Number(b[k]) >= 0) patch[k] = Number(b[k]); for (const k of ['weeklyPrizes', 'leaderboardExclude']) if (Array.isArray(b[k])) patch[k] = b[k].map(Number).filter(n => n >= 0); return json(res, 200, { ok: true, settings: rewards.setSettings(patch) }); }
// award a week by hand (its Monday key); already-awarded weeks are skipped
if (p === '/api/admin/prizes/award' && req.method === 'POST') { const b = await readBody(req); const r = social.awardWeek(String(b.week || '')); if (r && r.winners && r.winners.length) notify('prize', r).catch(() => {}); return json(res, r && r.error ? 400 : 200, r); }
// Ad Detective question bank: stats per question, add or edit one, retire or restore one
if (p === '/api/admin/detective') return json(res, 200, detective.stats());
if (p === '/api/admin/detective/question' && req.method === 'POST') { const b = await readBody(req); if (b.id && b.active != null && !b.q) return json(res, detective.setActive(String(b.id), !!b.active) ? 200 : 404, { ok: true }); const r = detective.saveQuestion(b); return json(res, r.error ? 400 : 200, r); }
if (p === '/api/admin/drip/retry' && req.method === 'POST') { const b = await readBody(req); rewards.mark(String(b.id || ''), { status: 'due', error: null }); return json(res, 200, { ok: true }); }
if (p === '/api/admin/faucet/tick' && req.method === 'POST') { const r = await faucet.tick(notify); return json(res, 200, Object.assign(r, { balance: await faucet.balance() })); }
if (p === '/api/admin/embed-test') { // mint a token for any mission so the embed can be tried without a hunter