Add Redeems: channel points, built-in rewards and bits Power-ups

- reward_variations (src/db.js in the wiring commit): each trigger has variations,
  one is picked at random weighted by rarity (Common 50 / Uncommon 25 / Rare 12 /
  Ultra Rare 3), like Twitch alert variations.
- /redeems page: rewards listed live from Twitch, plus Power-ups and built-in
  rewards; per variation name, rarity (with % chance), sound + volume, show cassette
  or sound only, caption, sticker, on/off, play/test/two-click delete.
- Special styles: Gigantify -> giant emote, Celebration -> bouncing emotes,
  Highlight my message / Message Effect -> big taped note.
- redeems-seed.json: first-run defaults rebuilt from the Twitch alert setup
  (96 variations, 21 triggers); only loaded when the table is empty.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Bashy 2026-11-20 23:18:15 +02:00
parent 1766e37336
commit 553cc35b9e
4 changed files with 372 additions and 0 deletions

89
src/redeems.js Normal file
View file

@ -0,0 +1,89 @@
// Channel point rewards, built-in channel point rewards and bits Power-ups.
// Each trigger (kind + key) has variations; one enabled variation is picked at random,
// weighted by rarity (like Twitch alert variations), and played as a polaroid or sound-only alert.
const db = require('./db');
const alerts = require('./alerts');
const dispatch = require('./dispatch');
// relative weight per rarity (Twitch does not publish its numbers; tune here).
// chance of a variation = its weight / sum of the weights of the trigger's ENABLED variations
const RARITY = {
common: { label: 'Common', weight: 50 },
uncommon: { label: 'Uncommon', weight: 25 },
rare: { label: 'Rare', weight: 12 },
ultra_rare: { label: 'Ultra Rare', weight: 3 },
};
// built-in (automatic) channel point rewards, channel.channel_points_automatic_reward_redemption.add v2
const AUTO_REWARDS = {
send_highlighted_message: 'Highlight my message',
single_message_bypass_sub_mode: 'Send a message in sub-only mode',
random_sub_emote_unlock: 'Unlock a random sub emote',
chosen_sub_emote_unlock: 'Choose an emote to unlock',
chosen_modified_sub_emote_unlock: 'Modify a single emote',
};
// bits Power-ups, channel.bits.use with type = power_up
const POWER_UPS = {
message_effect: 'Power-up: Message Effect',
celebration: 'Power-up: On-Screen Celebration',
gigantify_an_emote: 'Power-up: Gigantify an Emote',
};
// how a trigger is drawn on the overlay (everything else: cassette tape)
const STYLE = {
'power_up:gigantify_an_emote': 'emote', // the viewer's emote, giant
'power_up:celebration': 'emote_rain', // copies of the emote bouncing around the screen
'auto_reward:send_highlighted_message': 'message', // big taped paper note with the message
'power_up:message_effect': 'message', // same as highlight
};
const LINE = { emote: 'gigantified an emote!', emote_rain: 'is celebrating!', message: 'highlighted:' };
const emoteUrl = id => id ? `https://static-cdn.jtvnw.net/emoticons/v2/${encodeURIComponent(id)}/default/dark/3.0` : null;
function pick(variations) {
const pool = variations.filter(v => v.enabled);
const total = pool.reduce((s, v) => s + (RARITY[v.rarity]?.weight || 1), 0);
let r = Math.random() * total;
for (const v of pool) { r -= RARITY[v.rarity]?.weight || 1; if (r < 0) return v; }
return pool[pool.length - 1] || null;
}
function toAlert(v, { user, input, emote }) {
const style = STYLE[`${v.kind}:${v.key}`] || 'cassette';
return {
type: 'redeem', kind: v.kind, key: v.key, style,
user, reward: v.title, variation: v.name, rarity: v.rarity, input: input || null,
// special styles have their own wording unless the caption was customised
caption: style !== 'cassette' && v.caption === 'redeemed it! ♡' ? LINE[style] : v.caption,
sound: v.sound || null, volume: v.volume, polaroid: !!v.polaroid, art: v.art || null, emote: emoteUrl(emote),
};
}
// legacy action event type for the actions system / activity log
const LEGACY = { reward: 'channel_points', auto_reward: 'auto_reward', power_up: 'power_up' };
// A redemption arrived from Twitch.
function handle(kind, key, title, { user, input, emote }, raw) {
if (title) db.renameTrigger(kind, key, title);
const v = pick(db.getVariationsFor(kind, key));
if (!v) {
// nothing configured (or all off): still log it and run legacy actions
dispatch.dispatchEvent(LEGACY[kind], key, user, raw);
return null;
}
const alert = toAlert(v, { user, input, emote });
alerts.send(alert, raw);
return alert;
}
// Dashboard test: one specific variation, or a random pick for the whole trigger.
function test({ id, kind, key }) {
const v = id ? db.getVariation(id) : pick(db.getVariationsFor(kind, key));
if (!v) return false;
const input = ['send_highlighted_message', 'message_effect'].includes(v.key) ? 'this is what a highlighted message looks like on stream!'
: v.kind === 'reward' ? 'test input' : null;
alerts.sendTestAlert(toAlert({ ...v, enabled: 1 }, { user: 'ChickenWhisperer', input, emote: v.kind === 'power_up' ? '25' : null })); // 25 = Kappa
return true;
}
module.exports = { RARITY, AUTO_REWARDS, POWER_UPS, STYLE, pick, handle, test };