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:
@@ -0,0 +1,43 @@
|
||||
// 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 + '.png');
|
||||
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 vf = "drawtext=fontfile='" + ffPath(f1) + "':text='" + ffText(name) + "':fontcolor=#FFFFFF:fontsize=62:borderw=3:bordercolor=#22D3EE@0.9:shadowcolor=#000000@0.6:shadowx=0:shadowy=4:x=(w-text_w)/2:y=808"
|
||||
+ ",drawtext=fontfile='" + ffPath(f2) + "':text='" + ffText(sub) + "':fontcolor=#FFC24B:fontsize=36:x=(w-text_w)/2:y=910"
|
||||
+ (tg ? ",drawtext=fontfile='" + ffPath(f2) + "':text='" + ffText(tg) + "':fontcolor=#FF6A1A:fontsize=24:x=(w-text_w)/2:y=771" : ''); // 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 };
|
||||
Reference in New Issue
Block a user