Files
drip-runner/lib/badge.js
T
martbost a4d6597f9a Chapter 5 panel position corrected
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-30 15:25:54 -05:00

49 lines
4.2 KiB
JavaScript

// Badge cards for Telegram (Marty, 2026-09-30: "unlocks posted on Telegram with some catchy-looking badges,
// along with how much they earned"). The artwork is pre-rendered (public/badges/<id>.png, 1080x1080,
// tools/render-badges.mjs) with a blank plate at the bottom; ffmpeg stamps the player's name and the
// earnings line onto the plate when a card is posted. Cached on the volume: DATA_DIR/badges/.
'use strict';
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const { spawn } = require('child_process');
const BADGES = {
magnet: 'Drip Magnet', ship: 'Courier Mk II', shield: 'Shield Core', slow: 'Slow Field',
ch1: 'First Chapter', ch3: 'Chapter 3', ch5: 'Chapter 5', ch10: 'Chapter 10', ch15: 'Chapter 15', ch20: 'Chapter 20',
refuel5: 'Five Refuels', refuel25: '25 Refuels', refuel100: '100 Refuels', paid: 'Paid in POL'
};
const CHAPTER_BADGES = { 1: 'ch1', 3: 'ch3', 5: 'ch5', 10: 'ch10', 15: 'ch15', 20: 'ch20' };
const REFUEL_BADGES = { 5: 'refuel5', 25: 'refuel25', 100: 'refuel100' };
let PUBLIC_DIR = null, CACHE_DIR = null;
function init(opts) { PUBLIC_DIR = opts.publicDir; CACHE_DIR = path.join(opts.dataDir, 'badges'); try { fs.mkdirSync(CACHE_DIR, { recursive: true }); } catch (e) {} }
const art = id => path.join(PUBLIC_DIR, 'badges', id + '.jpg');
// where each artwork's blank glass panel sits (fractions of height, measured on the kie.ai renders of 2026-09-30)
const PLATE = { ch1: [0.752, 0.949], ch3: [0.758, 0.949], ch5: [0.782, 0.948], ch10: [0.783, 0.95], ch15: [0.754, 0.93], ch20: [0.739, 0.949],
magnet: [0.758, 0.949], ship: [0.732, 0.949], shield: [0.754, 0.95], slow: [0.76, 0.945], refuel5: [0.76, 0.944], refuel25: [0.762, 0.949], refuel100: [0.76, 0.949], paid: [0.775, 0.961] };
function font(name) { const f = path.join(PUBLIC_DIR, 'fonts', name); if (fs.existsSync(f)) return f; for (const d of ['/usr/share/fonts/truetype/dejavu', '/usr/share/fonts/dejavu', '/usr/share/fonts/TTF']) { const g = path.join(d, 'DejaVuSans-Bold.ttf'); if (fs.existsSync(g)) return g; } return null; }
const ffText = t => String(t).replace(/\\/g, '\\\\').replace(/'/g, "\u2019").replace(/:/g, '\\:').replace(/%/g, '%%').replace(/,/g, '\\,');
// ffmpeg filter paths: forward slashes, drive colon escaped
const ffPath = p => String(p).replace(/\\/g, '/').replace(/:/g, '\\:');
// the card: name on the plate's upper line, the earnings line under it
function render(id, who, line2, tag) {
return new Promise(resolve => {
if (!BADGES[id] || !fs.existsSync(art(id))) return resolve(null);
const f1 = font('Orbitron-Bold.ttf'), f2 = font('Manrope-Bold.ttf'); if (!f1 || !f2) return resolve(null);
const name = String(who || '').slice(0, 24), sub = String(line2 || '').slice(0, 70), tg = String(tag || '').slice(0, 40);
const out = path.join(CACHE_DIR, id + '-' + crypto.createHash('sha1').update(name + '|' + sub + '|' + tg).digest('hex').slice(0, 12) + '.jpg');
if (fs.existsSync(out)) return resolve(out);
const H = 1080, pl = PLATE[id] || [0.76, 0.95], mid = (pl[0] + pl[1]) / 2 * H;
const nameY = Math.round(mid - (tg ? 44 : 52)), subY = Math.round(mid + (tg ? 26 : 18)), tagY = Math.round(pl[0] * H + 16);
const vf = "drawtext=fontfile='" + ffPath(f1) + "':text='" + ffText(name) + "':fontcolor=#FFFFFF:fontsize=58:borderw=3:bordercolor=#22D3EE@0.9:shadowcolor=#000000@0.7:shadowx=0:shadowy=4:x=(w-text_w)/2:y=" + nameY
+ ",drawtext=fontfile='" + ffPath(f2) + "':text='" + ffText(sub) + "':fontcolor=#FFE08A:fontsize=32:shadowcolor=#000000@0.7:shadowx=0:shadowy=2:x=(w-text_w)/2:y=" + subY
+ (tg ? ",drawtext=fontfile='" + ffPath(f2) + "':text='" + ffText(tg) + "':fontcolor=#FF8A48:fontsize=22:shadowcolor=#000000@0.7:shadowx=0:shadowy=2:x=(w-text_w)/2:y=" + tagY : ''); // Manrope: Orbitron has no middle dot
const p = spawn('ffmpeg', ['-v', 'error', '-y', '-i', art(id), '-vf', vf, '-frames:v', '1', '-q:v', '3', out], { stdio: ['ignore', 'ignore', 'pipe'] });
let err = ''; p.stderr.on('data', c => { err += c; });
p.on('error', () => resolve(null));
p.on('close', code => { if (code === 0 && fs.existsSync(out)) resolve(out); else { console.error('badge render failed', code, err.slice(0, 300)); resolve(null); } });
});
}
module.exports = { BADGES, CHAPTER_BADGES, REFUEL_BADGES, init, render };