Files
dogfather-universe/workspace/assets/js/nachbericht-puls.js
T
DogFatherGitandClaude Opus 5.5 a4ed2a0b2c Nachbericht: Rudel-Puls als zusätzliche Kurve
nachbericht-puls.js hört auf nb:geladen und zeigt den Puls der Sendung
mit den Momenten, nur wenn es einen gibt. Regie mit Zahlen, Saal nur
die Form.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-04 13:44:26 +01:00

108 lines
5.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* =====================================================================
DER RUDEL-PULS IM NACHBERICHT (04.11.2026, Neuplan M1.5)
Eine zusätzliche Kurve: Reaktionen und Chat-Beiträge je Sekunde über
die ganze Sendung, mit den Momenten als Marken. Nur, wenn es für diese
Sendung einen Puls gibt (aufgezeichnet wird seit dem 04.11.2026).
Die Regie bekommt die Zahlen, alle anderen nur die Form (Stufen 0–10)
– das entscheidet der Server (/workspace/api/reaktion/puls/verlauf),
diese Seite blendet nichts „aus“, was sie gar nicht bekommen hat.
Hört auf `nb:geladen` (nachbericht.js) und baut ihren Abschnitt selbst;
`window.__nbPuls` trägt den Stand für die Prüfung.
===================================================================== */
(() => {
'use strict';
const $ = (id) => document.getElementById(id);
const el = (tag, cls, text) => { const e = document.createElement(tag); if (cls) e.className = cls; if (text != null) e.textContent = text; return e; };
const svgEl = (tag, attr) => { const e = document.createElementNS('http://www.w3.org/2000/svg', tag); for (const [k, v] of Object.entries(attr)) e.setAttribute(k, v); return e; };
const mmss = (s) => (s >= 3600 ? `${Math.floor(s / 3600)}:${String(Math.floor((s % 3600) / 60)).padStart(2, '0')} h` : `${Math.floor(s / 60)}:${String(Math.floor(s % 60)).padStart(2, '0')} min`);
let folge = 0;
function bauen() {
if ($('nb-puls')) return $('nb-puls');
const nach = $('nb-technik') || $('nb-verlauf');
if (!nach) return null;
const teil = el('section', 'nb__puls');
teil.id = 'nb-puls';
teil.hidden = true;
teil.setAttribute('aria-labelledby', 'nb-puls-titel');
const h = el('h2', '', 'Rudel-Puls');
h.id = 'nb-puls-titel';
const legende = el('p', 'nb__legende');
legende.append(el('span', 'nb__l nb__l--puls', 'Reaktionen + Chat je Sekunde'), ' ', el('span', 'nb__l nb__l--moment', 'Momente'));
const svg = svgEl('svg', { class: 'nb__pulskurve', id: 'nb-pulskurve', viewBox: '0 0 800 90', preserveAspectRatio: 'none', role: 'img', 'aria-label': 'Rudel-Puls' });
const text = el('p', 'nb__hinweis', '');
text.id = 'nb-puls-text';
teil.append(h, legende, svg, text);
nach.after(teil);
return teil;
}
async function laden(j) {
const meine = ++folge;
const teil = bauen();
if (!teil) return;
teil.hidden = true;
window.__nbPuls = null;
if (!j?.da || !j.sendung?.id) return;
let p = null;
try {
const a = await fetch(`/workspace/api/reaktion/puls/verlauf?id=${encodeURIComponent(j.sendung.id)}`, { credentials: 'same-origin', cache: 'no-store' });
p = await a.json().catch(() => null);
if (!a.ok) p = null;
} catch { p = null; }
if (meine !== folge) return;
window.__nbPuls = p;
if (!p?.da) return;
zeichnen(p, j);
teil.hidden = false;
}
function zeichnen(p, j) {
const svg = $('nb-pulskurve');
svg.textContent = '';
const reihe = p.regie ? p.werte : p.stufen;
const n = reihe.length;
const B = 800, H = 90, P = 6;
/* Höchstens 400 Säulen: Je Säule die SPITZE ihres Abschnitts – ein
Mittelwert würde einen kurzen Ausbruch glattbügeln, und genau den
sucht man hier. */
const stuecke = Math.min(400, n);
const je = n / stuecke;
const spitzen = [];
for (let i = 0; i < stuecke; i++) {
let m = 0;
for (let k = Math.floor(i * je); k < Math.max(Math.floor(i * je) + 1, Math.floor((i + 1) * je)); k++) m = Math.max(m, reihe[k] || 0);
spitzen.push(m);
}
const max = spitzen.reduce((a, v) => (v > a ? v : a), 0) || 1;
const x = (i) => P + (i / Math.max(1, stuecke - 1)) * (B - 2 * P);
const y = (v) => H - P - (v / max) * (H - 2 * P);
const punkte = spitzen.map((v, i) => `${x(i).toFixed(1)},${y(v).toFixed(1)}`);
svg.append(svgEl('path', { d: `M${x(0)},${H - P} L${punkte.join(' L')} L${x(stuecke - 1)},${H - P} Z`, class: 'nb__pulsflaeche' }));
svg.append(svgEl('polyline', { points: punkte.join(' '), class: 'nb__pulslinie', fill: 'none' }));
const start = Date.parse(j.sendung.begonnen || '') || null;
const momente = (j.momente || []).filter((m) => m.zeit);
if (start) {
for (const m of momente) {
const s = (Date.parse(m.zeit) - start) / 1000;
if (!(s >= 0 && s <= n)) continue;
const mx = Math.min(B - P, Math.max(P, P + (s / Math.max(1, n)) * (B - 2 * P)));
svg.append(svgEl('line', { x1: mx, x2: mx, y1: P, y2: H - P, class: `nb__marke nb__marke--${m.art || 'markiert'}` }));
}
}
/* Wo war am meisten los? Die Stelle in Minuten nach dem Start. */
let spitzeBei = 0;
reihe.forEach((v, i) => { if (v > reihe[spitzeBei]) spitzeBei = i; });
const satz = p.regie
? `Am meisten los nach ${mmss(spitzeBei)}: ${p.max} Reaktionen und Beiträge in einer Sekunde.`
: `Am meisten los nach ${mmss(spitzeBei)}.`;
$('nb-puls-text').textContent = satz;
svg.setAttribute('aria-label', `Rudel-Puls über ${mmss(n)}. ${satz}${momente.length ? ` ${momente.length} Momente.` : ''}`);
}
window.addEventListener('nb:geladen', (e) => { laden(e.detail); });
})();