heystreamer/public/alerts/poll.js
Bashy 2659993a7d Poll card and emote effects use the corner sticker from Actions -> Alerts
The heart on the winning poll option and the picture when Twitch sends no emote were still a
fixed file name; they now come from the alert theme (placeholder when none is set).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 11:24:56 +02:00

92 lines
4.7 KiB
JavaScript

/*
Live poll card for the overlay. Not part of the alert queue: it stays up while the poll runs.
PollCard.update(phase, poll) phase: begin | progress | end
poll: { id, title, choices:[{ id, title, votes }], ends_at, status } (status on end: completed | archived | terminated)
Votes animate live; on 'completed' the winner(s) get a heart and a WINNER stamp, then the card drops away.
*/
(function () {
const COLORS = ['#f4a0a8', '#a88be0', '#7fcfa6', '#f6c75b', '#8cc8f0'];
// heart on the winning option: the corner sticker from Actions → Alerts, else a plain shape
const heart = () => (window.S && window.S.alert_theme && window.S.alert_theme.heart) || (window.ALERTS_BASE || '/alerts/') + 'art/placeholder.svg';
const SHOW_RESULT_MS = 9000;
const esc = s => String(s ?? '').replace(/[&<>"]/g, c => ({ '&':'&amp;', '<':'&lt;', '>':'&gt;', '"':'&quot;' }[c]));
let el = null, pollId = null, timer = null, hideTimer = null, endsAt = 0;
function root() {
if (!el) { el = document.createElement('div'); el.id = 'poll-card'; document.body.appendChild(el); }
return el;
}
function build(poll) {
const el = root();
clearTimeout(hideTimer); el.getAnimations().forEach(a => a.cancel());
pollId = poll.id;
el.innerHTML = `<div class="head"><span>POLL</span><span class="timer"></span></div>
<div class="q">${esc(poll.title)}</div>
${poll.choices.map((c, i) => `<div class="c" data-id="${esc(c.id)}">
<div class="row"><span class="t">${esc(c.title)}</span><span class="n">0</span></div>
<div class="track"><div class="fill" style="background-color:${COLORS[i % COLORS.length]}"></div></div>
<img class="heart" src="${heart()}" alt=""></div>`).join('')}
<div class="stamp">WINNER</div>`;
el.style.visibility = 'visible';
const tilt = (Math.random() * 4 - 2).toFixed(2);
el.animate([
{ transform:`translateX(-560px) rotate(-8deg)`, opacity:0 },
{ transform:`translateX(10px) rotate(${+tilt + 1}deg)`, opacity:1, offset:.75 },
{ transform:`translateX(0) rotate(${tilt}deg)`, opacity:1 },
], { duration:650, easing:'cubic-bezier(.2,.8,.3,1)', fill:'forwards' });
}
function votes(poll) {
const total = poll.choices.reduce((s, c) => s + c.votes, 0);
for (const c of poll.choices) {
const row = el.querySelector(`.c[data-id="${CSS.escape(c.id)}"]`);
if (!row) continue;
const pct = total ? Math.round(100 * c.votes / total) : 0;
row.querySelector('.n').textContent = `${c.votes} · ${pct}%`;
row.querySelector('.fill').style.width = `${total ? Math.max(3, pct) : 0}%`;
}
return total;
}
function tick() {
const s = Math.max(0, Math.round((endsAt - Date.now()) / 1000));
const t = el.querySelector('.timer');
if (t) t.textContent = `${Math.floor(s / 60)}:${String(s % 60).padStart(2, '0')} left`;
}
function hide(delay) {
clearTimeout(hideTimer);
hideTimer = setTimeout(() => {
el.animate([{ transform:'translateY(0)', opacity:1 }, { transform:'translateY(160px) rotate(10deg)', opacity:0 }],
{ duration:550, easing:'ease-in', fill:'forwards' }).finished.then(() => { el.style.visibility = 'hidden'; pollId = null; }).catch(() => {});
}, delay);
}
function update(phase, poll) {
if (!poll || !poll.choices) return;
if (phase === 'begin' || pollId !== poll.id) build(poll);
votes(poll);
if (poll.ends_at) { endsAt = Date.parse(poll.ends_at); clearInterval(timer); timer = setInterval(tick, 500); tick(); }
if (phase !== 'end') return;
clearInterval(timer);
const t = el.querySelector('.timer');
if (poll.status !== 'completed') { if (t) t.textContent = 'cancelled'; hide(1500); return; }
if (t) t.textContent = ''; // the WINNER stamp takes this corner
const max = Math.max(...poll.choices.map(c => c.votes));
el.querySelectorAll('.c').forEach(row => {
const c = poll.choices.find(x => x.id === row.dataset.id);
const win = max > 0 && c.votes === max;
row.classList.add(win ? 'win' : 'lose');
if (win) row.querySelector('.heart').animate([
{ transform:'rotate(-40deg) scale(0)', opacity:0 }, { transform:'rotate(10deg) scale(1.3)', opacity:1, offset:.6 }, { transform:'rotate(6deg) scale(1)', opacity:1 },
], { delay:300, duration:450, easing:'ease-out', fill:'forwards' });
});
if (max > 0) el.querySelector('.stamp').animate([
{ transform:'rotate(-12deg) scale(2.2)', opacity:0 }, { transform:'rotate(-12deg) scale(.9)', opacity:1, offset:.7 }, { transform:'rotate(-12deg) scale(1)', opacity:1 },
], { delay:700, duration:380, easing:'ease-in', fill:'forwards' });
hide(SHOW_RESULT_MS);
}
window.PollCard = { update };
})();