/*
Cassette tape renderer for redeem alerts. Called by polaroid.js (shared queue):
window.CassetteAlert.show(d, cur, { url, len, volume }) -> Promise (resolves when ejected)
d: { title (variation), sub (reward), rarity, sticker (URL), name (user), line (strip text), msg (viewer input) }
cur: { anims, timer, audio } from polaroid.js, so reset/skip can stop it.
Timeline: slides in (0.7 s), sticker + strip pop on, reels spin and tape winds over the sound's length,
stays animation + 3 s or until the sound ends, then ejects upward.
*/
(function () {
const AFTER_ANIM = 3000, SOUND_LEAD = 600, ANIM_END = 1200;
// sticker when a variation has none: Actions → Alerts → Default redeem sticker (else a plain shape)
const defaultSticker = () => (window.S && window.S.alert_theme && window.S.alert_theme.defaultSticker) || (window.ALERTS_BASE || '/alerts/') + 'art/placeholder.svg';
const RAR = { common:['r-common','COMMON'], uncommon:['r-uncommon','UNCOMMON'], rare:['r-rare','★ RARE'], ultra_rare:['r-ultra','★ ULTRA RARE ★'] };
const esc = s => String(s ?? '').replace(/[&<>"]/g, c => ({ '&':'&', '<':'<', '>':'>', '"':'"' }[c]));
const between = (a, b) => a + Math.random() * (b - a);
let el = null;
function root() {
if (!el) { el = document.createElement('div'); el.id = 'cassette-alert'; document.body.appendChild(el); }
return el;
}
function reel(cx, id) {
const teeth = Array.from({ length: 6 }, (_, i) =>
``).join('');
return `
${teeth}
`;
}
function svg() {
const uid = 'c' + Math.random().toString(36).slice(2, 8);
return ``;
}
function show(d, cur, { url, len, volume }) {
const el = root();
const [cls, label] = RAR[d.rarity] || RAR.common;
const ultra = d.rarity === 'ultra_rare';
el.className = cls;
el.innerHTML = `
${svg()}
SIDE A${label}
${esc(d.title)}
${d.sub && d.sub !== d.title ? esc(d.sub) : ''}
${d.icon ? `
` : ''}
${ultra ? '
' + '
'.repeat(6) : ''}
${esc(d.name)} ${esc(d.line)}${d.msg ? `“${esc(d.msg)}”` : ''}
`;
el.style.visibility = 'visible';
const q = s => el.querySelector(s);
const A = (node, frames, opts) => { const a = node.animate(frames, { fill:'both', ...opts }); cur.anims.push(a); return a; };
const tilt = between(-4, 4);
A(q('.tape-wrap'), [
{ transform:'translateX(-620px) rotate(-14deg)', opacity:0 },
{ transform:`translateX(18px) rotate(${tilt + 2}deg)`, opacity:1, offset:.72 },
{ transform:`translateX(-4px) rotate(${tilt - .6}deg)`, offset:.88 },
{ transform:`translateX(0) rotate(${tilt}deg)`, opacity:1 },
], { duration:700, easing:'cubic-bezier(.2,.8,.3,1)' });
if (q('.ico')) A(q('.ico'), [ // the reward's / Power-up's Twitch icon, top-left corner
{ transform:'rotate(10deg) scale(0)', opacity:0 },
{ transform:'rotate(-18deg) scale(1.2)', opacity:1, offset:.6 },
{ transform:'rotate(-10deg) scale(1)', opacity:1 },
], { delay:560, duration:360, easing:'ease-out' });
A(q('.stk'), [
{ transform:'rotate(-30deg) scale(0)', opacity:0 },
{ transform:'rotate(8deg) scale(1.2)', opacity:1, offset:.6 },
{ transform:'rotate(0deg) scale(1)', opacity:1 },
], { delay:650, duration:380, easing:'ease-out' });
A(q('.strip'), [
{ transform:'translateY(-20px) rotate(-6deg) scale(1.1)', opacity:0 },
{ transform:'translateY(0) rotate(-2deg) scale(1)', opacity:1 },
], { delay:850, duration:320, easing:'cubic-bezier(.3,.7,.4,1)' });
// reels spin and tape winds while the sound plays
const playMs = Math.max(len, 600);
const turns = Math.round(playMs / 1000 * 540);
['reelL', 'reelR'].forEach(id => A(q(`[data-id="${id}"]`), [{ transform:'rotate(0deg)' }, { transform:`rotate(${turns}deg)` }],
{ delay:SOUND_LEAD, duration:playMs, easing:'linear', fill:'none' }));
A(q('[data-id="reelL-tape"]'), [{ transform:'scale(1)' }, { transform:'scale(.55)' }], { delay:SOUND_LEAD, duration:playMs, easing:'linear' });
A(q('[data-id="reelR-tape"]'), [{ transform:'scale(.55)' }, { transform:'scale(1)' }], { delay:SOUND_LEAD, duration:playMs, easing:'linear' });
A(q('.tape-wrap'), [{ translate:'0 0' }, { translate:'0 -2px' }, { translate:'0 0' }],
{ delay:SOUND_LEAD, duration:420, iterations:Math.ceil(playMs / 420), easing:'ease-in-out', fill:'none', composite:'add' });
if (ultra) {
A(q('.foil'), [{ opacity:0 }, { opacity:1, offset:.2 }, { opacity:1, offset:.8 }, { opacity:0 }],
{ delay:700, duration:1400, iterations:3, easing:'ease-in-out', fill:'none' });
el.querySelectorAll('.spark').forEach((s, i) => {
s.style.left = `${between(10, 410)}px`; s.style.top = `${between(-10, 250)}px`;
A(s, [{ opacity:0, transform:'scale(0) rotate(0deg)' }, { opacity:1, transform:'scale(1) rotate(45deg)', offset:.5 }, { opacity:0, transform:'scale(0) rotate(90deg)' }],
{ delay:800 + i * 230, duration:900, iterations:4, fill:'none' });
});
}
if (url) {
const a = new Audio(url); a.volume = volume;
cur.timer = setTimeout(() => { cur.audio = a; a.play().catch(err => console.warn('[alerts] sound blocked:', err.message)); }, SOUND_LEAD);
}
const exitAt = Math.max(ANIM_END + AFTER_ANIM, url ? SOUND_LEAD + len : 0);
const exit = A(el, [
{ transform:'translateY(0)', opacity:1 },
{ transform:'translateY(14px)', opacity:1, offset:.2 },
{ transform:'translateY(-420px) rotate(-8deg)', opacity:0 },
], { delay:exitAt, duration:600, easing:'cubic-bezier(.5,0,.8,.4)' });
return exit.finished.then(() => { el.style.visibility = 'hidden'; cur.anims.forEach(a => a.cancel()); }).catch(() => {});
}
function hide() { if (el) el.style.visibility = 'hidden'; }
window.CassetteAlert = { show, hide };
})();