heystreamer/views/redeems.ejs
Bashy 4fda6060bf TTS redeems, skip button, per-source chat placement
- Redeem variations can have a TTS voice: the viewer's text is cleaned
  (links out, repeats collapsed, max length, blocked words -> not read),
  spoken as "<name> says: ..." and shown as the big paper note; the
  variation's sound is not played when TTS is on (picker greyed out)
- Settings: TTS max length, say-the-name, blocked words (never sent to
  overlay pages: db.getOverlaySettings)
- Dashboard "Skip current alert" -> /alerts/skip -> overlay skips it
- db.migrate()/addColumn: new columns on existing databases
- Chat: ?chat=left,top,width,height and ?chatflow=up per browser source
  (e.g. the Intermission panel); default unchanged

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 14:34:41 +02:00

148 lines
9 KiB
Text

<style>
.trig { border:1px solid var(--pico-muted-border-color); border-radius:10px; padding:.4rem .9rem; margin-bottom:.6rem; }
.trig > summary { display:flex; align-items:center; gap:.6rem; flex-wrap:wrap; cursor:pointer; }
.trig > summary img { width:22px; height:22px; }
.trig .meta { color:var(--pico-muted-color); font-size:.85rem; }
.var { display:grid; grid-template-columns: 1.4fr 1fr 2fr .8fr; gap:.4rem .6rem; align-items:end;
padding:.6rem 0; border-top:1px dashed var(--pico-muted-border-color); }
.var label { font-size:.75rem; margin:0; color:var(--pico-muted-color); }
.var input, .var select { margin:0; padding:.3rem .5rem; font-size:.85rem; height:auto; }
.var input[type=checkbox] { padding:0; }
.var .row2 { grid-column: 1 / -1; display:flex; gap:.6rem; flex-wrap:wrap; align-items:center; }
.var .row2 .grow { flex:1; min-width:220px; }
.var button { margin:0; padding:.25rem .7rem; font-size:.8rem; width:auto; }
.var.off { opacity:.55; }
.chance { font-size:.8rem; color:var(--pico-muted-color); }
.btn-sm { margin:0; padding:.25rem .7rem; font-size:.8rem; width:auto; }
.danger { color:#f88; border-color:#f88; }
.warn { color:#e6b800; }
@media (max-width: 800px) { .var { grid-template-columns: 1fr 1fr; } }
</style>
<h2>Redeems</h2>
<p class="meta">
Each redeem plays <strong>one</strong> of its enabled variations, picked at random by rarity
(<% for (const [k, r] of Object.entries(rarity)) { %><%= r.label %> <%= r.weight %><% if (k !== 'ultra_rare') { %> · <% } %><% } %>, relative weights).
Sounds: built-in library, or upload more on the <a href="/media">Media</a> page.
<% if (!twitchOk) { %><br><span class="warn">Could not load your rewards from Twitch right now, so only rewards that already have variations are listed.</span><% } %>
</p>
<% for (const g of groups) { %>
<h3 style="margin-top:1.5rem;"><%= g.name %></h3>
<% for (const t of g.items) {
const on = t.variations.filter(v => v.enabled);
const total = on.reduce((s, v) => s + (rarity[v.rarity]?.weight || 1), 0);
const anchor = `t-${t.kind}-${t.key}`; %>
<details class="trig" id="<%= anchor %>">
<summary>
<% if (t.image) { %><img src="<%= t.image %>" alt=""><% } %>
<strong><%= t.title %></strong>
<% if (t.cost != null) { %><span class="meta"><%= t.cost %> pts</span><% } %>
<% if (t.missing) { %><span class="badge badge-red">not on Twitch anymore</span><% } %>
<% if (t.live === false) { %><span class="badge badge-grey">paused on Twitch</span><% } %>
<% if (t.variations.some(v => v.tts_voice_id)) { %><span class="badge badge-green" title="At least one variation reads the viewer's text aloud">TTS</span><% } %>
<span class="meta">
<%= t.variations.length ? `${on.length}/${t.variations.length} variation${t.variations.length === 1 ? '' : 's'} on` : 'no alert' %>
</span>
<% if (on.length) { %>
<button type="button" class="outline btn-sm" data-test-trigger data-kind="<%= t.kind %>" data-key="<%= t.key %>">Test random</button>
<% } %>
</summary>
<% for (const v of t.variations) { %>
<form method="POST" action="/redeems/variation/<%= v.id %>" class="var<%= v.enabled ? '' : ' off' %>">
<div><label>Name (cassette label)</label><input name="name" value="<%= v.name %>" required></div>
<div><label>Rarity</label>
<select name="rarity">
<% for (const [k, r] of Object.entries(rarity)) { %><option value="<%= k %>" <%= v.rarity === k ? 'selected' : '' %>><%= r.label %></option><% } %>
</select>
<% if (v.enabled && total) { %><span class="chance"><%= Math.round(100 * (rarity[v.rarity]?.weight || 1) / total) %>% chance</span><% } %>
</div>
<div><label>Sound</label>
<select name="sound" data-sound>
<option value="">— silent —</option>
<% for (const s of sounds) { %><option value="<%= s.url %>" <%= v.sound === s.url ? 'selected' : '' %>><%= s.label %></option><% } %>
</select>
</div>
<div><label>Volume</label><input type="number" name="volume" min="0" max="1" step="0.05" value="<%= v.volume %>"></div>
<div class="row2">
<label><input type="checkbox" name="enabled" <%= v.enabled ? 'checked' : '' %>> on</label>
<label title="Off = only the sound plays, no cassette on screen (still waits its turn in the queue)"><input type="checkbox" name="polaroid" <%= v.polaroid ? 'checked' : '' %>> show cassette</label>
<span class="grow"><input name="caption" value="<%= v.caption %>" title="Text after the viewer's name on the paper strip. {reward} = reward name, {variation} = this variation's name. The viewer's typed message is added automatically."></span>
<select name="art" style="width:auto;" title="Sticker in the cassette's corner">
<option value="">sticker: headset Streamer</option>
<% for (const st of stickers) { %><option value="<%= st.url %>" <%= v.art === st.url ? 'selected' : '' %>>sticker: <%= st.label %></option><% } %>
</select>
<select name="tts_voice_id" data-tts style="width:auto;" title="Read the viewer's typed text aloud with a voice from the TTS page, shown as a big paper note. The voice replaces the sound. The reward needs 'Require Viewer to Enter Text' on Twitch.">
<option value="">TTS: off</option>
<% for (const tv of ttsVoices) { %><option value="<%= tv.id %>" <%= v.tts_voice_id === tv.id ? 'selected' : '' %>>TTS: <%= tv.name %></option><% } %>
</select>
<button type="button" class="outline btn-sm" data-play title="Play the sound here in the browser">▶</button>
<button type="submit" class="btn-sm">Save</button>
<button type="button" class="outline btn-sm" data-test-var="<%= v.id %>" title="Show it on the overlay">Test</button>
<button type="button" class="outline btn-sm danger" data-del>Delete</button>
<button type="submit" class="btn-sm danger" data-del-confirm formaction="/redeems/variation/<%= v.id %>/delete" hidden>Really delete</button>
</div>
</form>
<% } %>
<form method="POST" action="/redeems/variation" style="margin:.5rem 0 .3rem;">
<input type="hidden" name="kind" value="<%= t.kind %>">
<input type="hidden" name="key" value="<%= t.key %>">
<input type="hidden" name="title" value="<%= t.title %>">
<button type="submit" class="outline btn-sm">+ Add variation</button>
</form>
</details>
<% } %>
<% } %>
<p><small id="redeem-status"></small></p>
<script>
(() => {
const status = document.getElementById('redeem-status');
const say = t => { status.textContent = t; };
// open the trigger we just edited
if (location.hash) { const d = document.getElementById(decodeURIComponent(location.hash.slice(1))); if (d) d.open = true; }
let player = null;
document.querySelectorAll('[data-play]').forEach(b => b.addEventListener('click', () => {
const url = b.closest('form').querySelector('[data-sound]').value;
if (player) player.pause();
if (!url) return say('No sound selected');
player = new Audio(url);
player.volume = parseFloat(b.closest('form').querySelector('[name=volume]').value) || 0.8;
player.play().catch(e => say('Could not play: ' + e.message));
}));
const post = async (url, body) => {
try {
const r = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body || {}) });
say(r.ok ? 'Sent to overlay (save first if you changed something)' : `Failed (${r.status})`);
} catch (e) { say('Failed: ' + e.message); }
};
document.querySelectorAll('[data-test-var]').forEach(b => b.addEventListener('click', () => post(`/redeems/variation/${b.dataset.testVar}/test`)));
document.querySelectorAll('[data-test-trigger]').forEach(b => b.addEventListener('click', e => {
e.preventDefault(); // inside <summary>: do not toggle
post('/redeems/trigger/test', { kind: b.dataset.kind, key: b.dataset.key });
}));
// TTS on = no sound: grey out the sound picker (the server ignores it too)
document.querySelectorAll('[data-tts]').forEach(sel => {
const form = sel.closest('form'), snd = form.querySelector('[data-sound]');
const sync = () => {
snd.disabled = !!sel.value;
snd.title = sel.value ? 'TTS is on: the voice replaces the sound' : '';
};
sel.addEventListener('change', sync); sync();
form.addEventListener('submit', () => { snd.disabled = false; }); // still submit it, so turning TTS off brings the sound back
});
// two-step delete, no browser dialog
document.querySelectorAll('[data-del]').forEach(b => b.addEventListener('click', () => {
const c = b.parentElement.querySelector('[data-del-confirm]');
b.hidden = true; c.hidden = false;
setTimeout(() => { b.hidden = false; c.hidden = true; }, 4000);
}));
})();
</script>