Badge cards posted to Telegram: hangar unlocks, chapter milestones (1,3,5,10,15,20), refuel milestones (5,25,100), payouts; neon artwork + ffmpeg-stamped name/earnings; trial wording (coming soon, would have earned) until the admin flips it; admin announce route

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-09-30 15:04:38 -05:00
parent fad0767648
commit 505f5cdae4
23 changed files with 191 additions and 6 deletions
+55 -4
View File
@@ -22,6 +22,7 @@ const game = require('./lib/game');
const ledger = require('./lib/ledger');
const faucet = require('./lib/faucet');
const spawn = require('./lib/spawn');
const badge = require('./lib/badge');
const PORT = Number(process.env.PORT || 3000);
const DATA_DIR = process.env.DATA_DIR || path.join(__dirname, 'data');
@@ -35,6 +36,7 @@ const outbound = () => process.env.OUTBOUND === 'on';
const IS_PROD = process.env.NODE_ENV === 'production';
store.init(DATA_DIR);
badge.init({ publicDir: PUBLIC_DIR, dataDir: DATA_DIR });
const faucetOn = faucet.init();
// ---- Telegram to Marty (outward: gated) -------------------------------------------------------
@@ -44,10 +46,49 @@ async function telegram(text) {
try { const r = await fetch('https://api.telegram.org/bot' + tok + '/sendMessage', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ chat_id: chat, text, parse_mode: 'HTML', disable_web_page_preview: true }) }); return r.ok; } catch (e) { return false; }
}
const fmt = n => Number(n).toLocaleString('en-US', { maximumFractionDigits: 4 });
// ---- public badge posts (Marty, 2026-09-30): unlocks, chapter and refuel milestones, payouts, as photo cards ----
// DR_TG_PUBLIC_CHAT / DR_TG_PUBLIC_TOPIC = the room (PolHunter's), DR_TG_PUBLIC_GENERAL=on also posts the photo to the general channel.
async function telegramPhoto(file, caption, general) {
if (!outbound()) return false;
const tok = process.env.DR_TG_TOKEN, chat = process.env.DR_TG_PUBLIC_CHAT || process.env.DR_TG_CHAT, topic = process.env.DR_TG_PUBLIC_CHAT ? process.env.DR_TG_PUBLIC_TOPIC : null;
if (!tok || !chat || !file) return false;
try {
const fd = new FormData(); fd.append('chat_id', chat); fd.append('caption', caption); fd.append('parse_mode', 'HTML'); if (topic && !general) fd.append('message_thread_id', String(topic));
fd.append('photo', new Blob([fs.readFileSync(file)], { type: 'image/jpeg' }), 'badge.jpg');
const r = await fetch('https://api.telegram.org/bot' + tok + '/sendPhoto', { method: 'POST', body: fd }); return r.ok;
} catch (e) { return false; }
}
const nameOf = me => me.username ? '@' + me.username : '#' + me.memberId;
const trial = () => ledger.settings().trial !== false; // Marty, 2026-09-30: while testing, say so, and show what they WOULD have earned once live
function earningsLine(memberId) {
const a = ledger.account(memberId); const p = game.player(memberId) || {}; const refuels = store.read('refuels', []).filter(r => r.memberId === Number(memberId)).length;
return (trial() ? 'would have earned ' : 'earned ') + fmt(a.earnedTotal || 0) + ' POL so far \u00b7 ' + refuels + ' refuel' + (refuels === 1 ? '' : 's') + ' \u00b7 chapter ' + (p.chapter || 1);
}
// one post per (member, badge); a payout card is one per payout row
async function announce(id, me, opts) {
opts = opts || {}; const key = opts.key || id;
const posted = store.read('posted', {}); const mine = new Set(posted[String(me.memberId)] || []);
if (mine.has(key)) return false;
store.update('posted', {}, all => { all[String(me.memberId)] = [...mine, key]; return all; });
const line2 = opts.line2 || earningsLine(me.memberId);
const file = await badge.render(id, nameOf(me), line2, trial() ? 'COMING SOON \u00b7 NOW IN TESTING' : '');
const head = trial() ? opts.caption.replace('<b>Drip Runner</b>', '<b>Drip Runner</b> <i>(coming soon \u00b7 now in testing)</i>') : opts.caption;
const foot = trial() ? '<i>Testers are playing a preview. When Drip Runner goes live, amounts like this pay out in POL to the wallet on your InstantAdPay account.</i>' : '<i>Play from your InstantAdPay dashboard.</i>';
const cap = head + '\n' + line2 + '\n' + foot;
if (!file) return telegram(cap);
const ok = await telegramPhoto(file, cap);
if (ok && String(process.env.DR_TG_PUBLIC_GENERAL || 'off') === 'on') await telegramPhoto(file, cap, true);
return ok;
}
const ITEM_NAMES = { magnet: 'Drip Magnet', ship: 'Courier Mk II', shield: 'Shield Core', slow: 'Slow Field' };
function announceUnlock(me, item, paid) { if (!badge.BADGES[item]) return; announce(item, me, { caption: '\u{1F3AE} <b>Drip Runner</b> \u00b7 <b>' + nameOf(me) + '</b> unlocked the <b>' + ITEM_NAMES[item] + '</b> (' + paid + ')' }).catch(() => {}); }
function announceChapter(me, ch, pol) { const id = badge.CHAPTER_BADGES[ch]; if (!id) return; announce(id, me, { caption: '\u{1F3C1} <b>Drip Runner</b> \u00b7 <b>' + nameOf(me) + '</b> cleared <b>Chapter ' + ch + '</b>' + (pol ? ' \u00b7 <b>+' + fmt(pol) + ' POL</b>' : '') }).catch(() => {}); }
function announceRefuels(me) { const n = store.read('refuels', []).filter(r => r.memberId === Number(me.memberId)).length; const id = badge.REFUEL_BADGES[n]; if (!id) return; announce(id, me, { caption: '\u26FD <b>Drip Runner</b> \u00b7 <b>' + nameOf(me) + '</b> has refuelled <b>' + n + ' times</b>. Every refuel is a verified visit to a member\u2019s page on InstantAdPay.' }).catch(() => {}); }
function announcePaid(p) { announce('paid', { memberId: p.memberId, username: p.username }, { key: 'paid:' + p.id, caption: '\u{1F4B8} <b>Drip Runner</b> \u00b7 <b>' + nameOf(p) + '</b> was paid <b>' + fmt(p.pol) + ' POL</b> \u00b7 <a href="' + explorer() + '/tx/' + p.tx + '">verify</a>', line2: 'paid ' + fmt(p.pol) + ' POL to their wallet \u00b7 ' + earningsLine(p.memberId).replace(/^earned [^\u00b7]+\u00b7 /, '') }).catch(() => {}); }
const explorer = () => Number(process.env.DR_CHAIN_ID) === 80002 ? 'https://amoy.polygonscan.com' : 'https://polygonscan.com';
async function notify(kind, p) {
const who = p && (p.username ? '@' + p.username : '#' + p.memberId);
if (kind === 'paid') return telegram('\u{1F3AE} <b>Drip Runner</b> · paid ' + who + ' <b>' + fmt(p.pol) + ' POL</b> · <a href="' + explorer() + '/tx/' + p.tx + '">verify</a>');
if (kind === 'paid') { announcePaid(p); return telegram('\u{1F3AE} <b>Drip Runner</b> · paid ' + who + ' <b>' + fmt(p.pol) + ' POL</b> · <a href="' + explorer() + '/tx/' + p.tx + '">verify</a>'); }
if (kind === 'failed') return telegram('❌ <b>Drip Runner</b> · payout to ' + who + ' failed: ' + p.error);
if (kind === 'low') return telegram('⚠️ <b>Drip Runner wallet is low</b>: ' + fmt(p.balance) + ' POL in ' + p.address + ' (needs ' + fmt(p.threshold) + ').');
if (kind === 'refused') return telegram('\u{1F6AB} <b>Drip Runner</b> \u00b7 ' + who + ' (' + p.email + ') tried to sign in but is not on the tester list. Add them under Admin > Settings > Testers: ' + SITE + '/admin');
@@ -169,12 +210,22 @@ const server = http.createServer(async (req, res) => {
const { body } = await readBody(req); const patch = {};
for (const k of ['usdPerCredit', 'creditsPerVisit', 'poolSharePct', 'usdPerPolFallback', 'dripPol', 'dailyCapPol', 'thresholdPol', 'seedPol', 'lowBalancePol']) if (body[k] != null && body[k] !== '') patch[k] = Number(body[k]);
if (body.signups && ['testers', 'key', 'open', 'closed'].includes(body.signups)) patch.signups = body.signups;
if (body.trial === 'on' || body.trial === 'off') patch.trial = body.trial === 'on';
if (Array.isArray(body.testers)) patch.testers = body.testers.map(x => String(x).trim().toLowerCase()).filter(Boolean).slice(0, 500);
if (body.itemPol && typeof body.itemPol === 'object') patch.itemPol = Object.assign({}, ledger.settings().itemPol, body.itemPol);
if (body.itemDrips && typeof body.itemDrips === 'object') patch.itemDrips = Object.assign({}, ledger.settings().itemDrips, body.itemDrips);
return json(res, 200, { ok: true, settings: ledger.setSettings(patch) });
}
if (p === '/api/admin/faucet/tick' && req.method === 'POST') return json(res, 200, await faucet.tick(notify));
if (p === '/api/admin/announce' && req.method === 'POST') { // post (or re-post with force) a badge for a member: {memberId, id}
const { body } = await readBody(req); const pl = game.player(Number(body.memberId)); if (!pl) return json(res, 404, { error: 'no such player' });
const me = { memberId: pl.memberId, username: pl.username }; const id = String(body.id || '');
if (body.force) store.update('posted', {}, all => { all[String(pl.memberId)] = (all[String(pl.memberId)] || []).filter(k => k !== id); return all; });
const ch = Object.keys(badge.CHAPTER_BADGES).find(k => badge.CHAPTER_BADGES[k] === id);
if (ch) { announceChapter(me, Number(ch), 0); return json(res, 200, { ok: true, kind: 'chapter' }); }
if (ITEM_NAMES[id]) { announceUnlock(me, id, 'hangar'); return json(res, 200, { ok: true, kind: 'unlock' }); }
return json(res, 400, { error: 'unknown badge id' });
}
if (p === '/api/admin/handoff' && req.method === 'POST') { // a test token for a synthetic member (local checks only)
const { body } = await readBody(req); return json(res, 200, { url: SITE + '/auth?t=' + sso.mint({ memberId: Number(body.memberId) || 999001, email: body.email || 'tester@example.com', wallet: body.wallet || null, username: body.username || 'tester' }) });
}
@@ -187,7 +238,7 @@ const server = http.createServer(async (req, res) => {
}
if (p === '/api/balance' && req.method === 'GET') return json(res, 200, ledger.balance(me.memberId));
if (p === '/api/run/start' && req.method === 'POST') { const { body } = await readBody(req); const r = game.start(me, body.W, body.H); return json(res, 200, { runId: r.id, seed: r.seed, W: r.W, H: r.H, speed: r.speed, chapter: r.chapter, goal: r.goal, armed: r.armed, ship: r.ship }); }
if (p === '/api/run/events' && req.method === 'POST') { const { body } = await readBody(req); const r = game.events(me, String(body.runId || ''), body.t, body.events, !!body.goal); if (r.cleared) r.pool = ledger.pool().available; return json(res, r.error ? 400 : 200, r); }
if (p === '/api/run/events' && req.method === 'POST') { const { body } = await readBody(req); const r = game.events(me, String(body.runId || ''), body.t, body.events, !!body.goal); if (r.cleared) { r.pool = ledger.pool().available; if (r.cleared.first) announceChapter(me, r.cleared.chapter, r.cleared.pol); } return json(res, r.error ? 400 : 200, r); }
if (p === '/api/run/end' && req.method === 'POST') { const { body } = await readBody(req); return json(res, 200, game.end(me, String(body.runId || ''))); }
// refuel: hand the player to IAP for a verified visit; the run resumes when IAP confirms
if (p === '/api/refuel/start' && req.method === 'POST') {
@@ -205,10 +256,10 @@ const server = http.createServer(async (req, res) => {
// the browser may carry IAP's signed receipt: it counts even when the server callback was slow
if (!it.done && body.receipt) { const rc = sso.open(body.receipt); if (rc.ok && rc.claims.kind === 'refuel-receipt' && rc.claims.nonce === nonce) it = await creditRefuel(it, nonce, { campaignId: rc.claims.campaignId, credits: rc.claims.credits, via: 'receipt' }); }
if (!it.done) return json(res, 200, { ok: false, reason: body.none ? 'none' : 'unverified' });
intents.upd(nonce, x => { x.consumed = true; }); game.refuelled(me, it.runId);
intents.upd(nonce, x => { x.consumed = true; }); game.refuelled(me, it.runId); announceRefuels(me);
return json(res, 200, { ok: true, pool: ledger.pool().available });
}
if (p === '/api/hangar/buy' && req.method === 'POST') { const { body } = await readBody(req); const r = game.buy(me, String(body.item || '')); if (!r.error) { r.player = game.view(me); r.balance = ledger.balance(me.memberId); } return json(res, r.error ? 400 : 200, r); }
if (p === '/api/hangar/buy' && req.method === 'POST') { const { body } = await readBody(req); const item = String(body.item || ''); const r = game.buy(me, item); if (!r.error) { r.player = game.view(me); r.balance = ledger.balance(me.memberId); announceUnlock(me, item, r.paid); } return json(res, r.error ? 400 : 200, r); }
return json(res, 404, { error: 'no such route' });
}
res.writeHead(404, Object.assign({ 'Content-Type': 'text/plain' }, SEC)); res.end('Not found');