Video Power-ups: Custom Power-ups, ?parts=video overlay, new redeems editor

- Custom Power-ups (bits) are redeem triggers: channel.custom_power_up_redemption.add
  -> redeems.handle('custom_power_up', ...) with the bits amount; listed live from
  Helix /bits/custom_power_ups on the Redeems page; labels/replay in Recent activity.
- Variations can show a video (reward_variations.video): style 'video' plays only on
  the opt-in ?parts=video overlay (public/alerts/video.js), full-screen, one at a time,
  with a yellow camcorder box ("TRIGGERED <name>", viewer, bits, POWER-UP) in the alert
  corner. Any source size; 2560x1440 like HUDFX gives sharper video.
- Redeems page rewritten as one editor: Display (Cassette / Sound only / Video),
  volume slider, sticker thumbnails, live % chances, one Save button + Ctrl+S
  (POST /redeems/save), tests/add/delete save first, unsaved edits kept as a browser
  draft with an inline restore bar (no dialogs).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Bashy 2026-11-22 16:10:56 +02:00
parent 19a0965eb2
commit b3caac7a31
11 changed files with 512 additions and 135 deletions

View file

@ -13,12 +13,16 @@ const PARTS = ['chat', 'alerts', 'poll', 'hypebar', 'raid']; // 'nowplaying' i
// ?parts=chat,raid only those parts
// ?parts=nochat everything except chat (no<part>, several allowed: nochat,noraid)
// no ?parts / nothing valid: everything
// ?parts=video full-screen video redeems + their notification box (public/alerts/video.js).
// Opt-in only (never part of "everything"), so it lives in its own OBS source.
const OPT_IN = ['video'];
function partsFrom(q) {
if (!q) return PARTS;
const tokens = String(q).split(',').map(s => s.trim().toLowerCase()).filter(Boolean);
const optIn = tokens.filter(t => OPT_IN.includes(t));
const excluded = tokens.filter(t => t.startsWith('no') && PARTS.includes(t.slice(2))).map(t => t.slice(2));
if (excluded.length) return PARTS.filter(p => !excluded.includes(p));
const asked = tokens.filter(p => PARTS.includes(p));
if (excluded.length) return [...PARTS.filter(p => !excluded.includes(p)), ...optIn];
const asked = tokens.filter(p => PARTS.includes(p) || OPT_IN.includes(p));
return asked.length ? asked : PARTS;
}

View file

@ -20,6 +20,31 @@ function soundOptions() {
return [...lib, ...uploads];
}
// videos for 'Video' variations: files in public/alerts/videos + videos uploaded on the Media page
function videoOptions() {
const lib = listDir('alerts/videos', /\.(webm|mp4|mov)$/i).map(f => ({ url: `/alerts/videos/${f}`, label: f }));
const uploads = db.getAllMedia().filter(m => m.media_type === 'video')
.map(m => ({ url: `/media/${m.filename}`, label: `${m.original_name} (uploaded)` }));
return [...lib, ...uploads];
}
// live list of the channel's Custom Power-ups (bits); null when Twitch is not reachable
let loggedPowerUps = false;
async function twitchPowerUps() {
const u = twitch.getUser();
if (!u) return null;
try {
const res = await twitch.helix(`/bits/custom_power_ups?broadcaster_id=${u.twitch_id}`);
if (!res.ok) { console.warn(`[redeems] custom power-ups: ${res.status} ${await res.text()}`); return null; }
const data = (await res.json()).data || [];
if (!loggedPowerUps) { loggedPowerUps = true; console.log(`[redeems] custom power-ups (first load): ${JSON.stringify(data).slice(0, 1500)}`); }
return data.map(p => ({
kind: 'custom_power_up', key: p.id, title: p.title, bits: p.bits ?? p.cost ?? null,
live: p.is_enabled ?? p.enabled ?? true, image: p.image?.url_1x || p.default_image?.url_1x || null,
}));
} catch (e) { console.warn('[redeems] custom power-ups:', e.message); return null; }
}
// live list of the channel's custom rewards (title, cost, icon); null when Twitch is not reachable
async function twitchRewards() {
const u = twitch.getUser();
@ -51,8 +76,17 @@ router.get('/', async (req, res) => {
rewards.push({ kind: 'reward', key: v.key, title: v.title, cost: null, missing: !!fromTwitch });
}
}
const fromTwitchPU = await twitchPowerUps();
const powerUps = fromTwitchPU ? [...fromTwitchPU] : [];
for (const v of variations) {
if (v.kind === 'custom_power_up' && !powerUps.some(p => p.key === v.key)) {
powerUps.push({ kind: 'custom_power_up', key: v.key, title: v.title, bits: null, missing: !!fromTwitchPU });
}
}
const uniq = (list) => list.filter((r, i) => list.findIndex(x => x.key === r.key) === i);
const groups = [
{ name: 'Custom Power-ups (bits)', id: 'custom-power-ups', items: uniq(powerUps).sort((a, b) => (a.bits ?? 1e9) - (b.bits ?? 1e9)),
empty: fromTwitchPU ? 'No Custom Power-ups yet: create them on Twitch (Creator Dashboard → Viewer Rewards → Power-ups).' : 'Could not load your Custom Power-ups from Twitch right now.' },
{ name: 'Channel point rewards', items: uniq(rewards).sort((a, b) => (a.cost ?? 1e9) - (b.cost ?? 1e9)) },
{ name: 'Bits Power-ups', items: Object.entries(redeems.POWER_UPS).map(([key, title]) => ({ kind: 'power_up', key, title })) },
{ name: 'Built-in channel point rewards', items: Object.entries(redeems.AUTO_REWARDS).map(([key, title]) => ({ kind: 'auto_reward', key, title })) },
@ -62,6 +96,7 @@ router.get('/', async (req, res) => {
res.render('redeems', {
groups, twitchOk: !!fromTwitch, rarity: redeems.RARITY,
sounds: soundOptions(),
videos: videoOptions(),
ttsVoices: db.getAllTtsVoices(),
stickers: [
...listDir('alerts/stickers', /\.(png|webp|gif)$/i).map(f => ({ url: `/alerts/stickers/${f}`, label: f.replace(/\.\w+$/, '') })),
@ -74,28 +109,50 @@ const back = (res, v) => res.redirect(`/redeems#t-${encodeURIComponent(`${v.kind
router.post('/variation', (req, res) => {
const { kind, key, title } = req.body;
if (!['reward', 'auto_reward', 'power_up'].includes(kind) || !key) return res.status(400).send('bad trigger');
if (!['reward', 'auto_reward', 'power_up', 'custom_power_up'].includes(kind) || !key) return res.status(400).send('bad trigger');
const n = db.getVariationsFor(kind, key).length;
const v = db.createVariation({ kind, key, title: title || key, name: n ? `${title} ${n + 1}` : (title || key) });
back(res, v);
});
// one variation's fields from the editor. display: cassette | sound (sound only) | video
function cleanVariation(v, b) {
const vol = parseFloat(b.volume);
const display = ['cassette', 'sound', 'video'].includes(b.display) ? b.display : (v.video ? 'video' : v.polaroid ? 'cassette' : 'sound');
const videos = new Set(videoOptions().map(o => o.url));
const video = display === 'video' && videos.has(b.video) ? b.video : '';
const on = x => x === true || x === 'on' || x === 1 || x === '1';
return {
name: String(b.name || '').trim().slice(0, 80) || v.name,
rarity: redeems.RARITY[b.rarity] ? b.rarity : v.rarity,
sound: String(b.sound || ''),
volume: isNaN(vol) ? v.volume : Math.max(0, Math.min(1, vol)),
polaroid: display === 'cassette' ? 1 : 0,
video,
caption: String(b.caption || '').trim().slice(0, 120) || 'redeemed it! ♡',
art: String(b.art || ''),
enabled: on(b.enabled) ? 1 : 0,
tts_voice_id: db.getTtsVoice(parseInt(b.tts_voice_id, 10)) ? parseInt(b.tts_voice_id, 10) : null,
};
}
// Save all: { variations: [{ id, ...fields }] } from the editor (one Save button / Ctrl+S)
router.post('/save', express.json({ limit: '1mb' }), (req, res) => {
const list = Array.isArray(req.body?.variations) ? req.body.variations : [];
let saved = 0;
for (const b of list) {
const v = db.getVariation(parseInt(b.id, 10));
if (!v) continue;
db.updateVariation(v.id, cleanVariation(v, b));
saved++;
}
res.json({ ok: true, saved });
});
router.post('/variation/:id', (req, res) => {
const v = db.getVariation(req.params.id);
if (!v) return res.status(404).send('not found');
const b = req.body;
const vol = parseFloat(b.volume);
db.updateVariation(v.id, {
name: (b.name || '').trim() || v.name,
rarity: redeems.RARITY[b.rarity] ? b.rarity : v.rarity,
sound: b.sound || '',
volume: isNaN(vol) ? v.volume : Math.max(0, Math.min(1, vol)),
polaroid: b.polaroid === 'on' ? 1 : 0,
caption: (b.caption || '').trim() || 'redeemed it! ♡',
art: b.art || '',
enabled: b.enabled === 'on' ? 1 : 0,
tts_voice_id: db.getTtsVoice(parseInt(b.tts_voice_id, 10)) ? parseInt(b.tts_voice_id, 10) : null,
});
db.updateVariation(v.id, cleanVariation(v, req.body));
back(res, v);
});