Neuplan C5.1–C5.3, die zweite Runde: - Rahmen zum Ausrichten in der Vorschau; „Gesicht im Rahmen“ nur, wenn der Browser es wirklich erkennt (FaceDetector, mit detect()-Probe laut Chrome-Doku) – sonst keine Behauptung. - Licht getrennt nach Mitte und Rand: Gegenlicht wird erkannt und steht als Hinweis direkt auf der Vorschau; bei gutem Bild steht dort nichts. - Leitung in Worten, gemessen gegen den echten Bedarf der Gastkamera (640×480, KAMERA_OBEN 1,2 Mbit/s in saal-weiche.js) statt „720p“; eine Wache hält die Zahl gegen KAMERA_OBEN. - Regie bekommt „<Name> ist bereit.“ angesagt, einmal beim Wechsel. - Server nimmt licht_mitte, licht_rand, gesicht gestutzt an. - pruef-reaktion 1062/0, pruef-zwischenraum-blick 49/0 (Gegenproben an künstlichen Bildern, Regie am Handy 390 px), mess-reaktion grün. Co-Authored-By: Claude Opus 5.5 <[email protected]>
332 lines
17 KiB
JavaScript
332 lines
17 KiB
JavaScript
/* =====================================================================
|
||
DIE BACKSTAGE DES GASTES (04.11.2026, Neuplan C5.5/C5.6 + C12)
|
||
|
||
Ein Gast auf der Bühne hatte bisher zwei Fragen ohne Antwort:
|
||
„Weiß die Regie, dass ich so weit bin?“ und „Sieht sie mich
|
||
überhaupt?“. Drei Dinge beantworten sie:
|
||
|
||
· ICH BIN BEREIT – der Gast meldet, dass Kamera, Mikro und Licht
|
||
stimmen. Erst dann steht er im Cockpit als „bereit ✓“.
|
||
· HAND HEBEN – er möchte etwas sagen. Die Regie sieht ✋ mit Namen
|
||
und nimmt die Hand herunter; der Gast sieht den Stand.
|
||
· „DIE REGIE SIEHT DICH“ – NICHT GERATEN, SONDERN GEMELDET. Das
|
||
Gerät der Regie sagt alle paar Sekunden, von welchem Gast bei ihm
|
||
wirklich Bild ankommt (Videospur lebt, liefert Bilder, hat eine
|
||
Größe) und ob die Seite gerade vor Augen ist. Daraus – und nur
|
||
daraus – entsteht der Satz beim Gast. Kommt keine Meldung, sagt er
|
||
das (dritter Ausgang), statt „sieht dich“ zu behaupten.
|
||
|
||
WO DER ZUSTAND WOHNT: „bereit“ und „Hand“ an der Gastzeile
|
||
(`reaktion_gaeste.bereit_am`/`hand_am`). Geht der Gast, wird die Zeile
|
||
gelöscht – damit ist beides weg, ohne eigene Aufräumarbeit. Eine neue
|
||
Einladung (`INSERT OR REPLACE`) fängt leer an. Die Empfangsmeldungen
|
||
der Regie leben nur im Speicher: Sie gelten 15 Sekunden, und nach
|
||
einem Neustart melden die Seiten sie ohnehin neu.
|
||
|
||
WER ES ERFÄHRT: nur die Regie (DogFather und VanVan) und der Gast
|
||
selbst. Dass jemand die Hand hebt, ist eine Bitte an die Regie, kein
|
||
Rundruf an den Saal.
|
||
|
||
Hängt hinter denselben Schranken wie jeder Reaction-Weg (angemeldet,
|
||
nur Team Dogi, gleiche Herkunft).
|
||
===================================================================== */
|
||
import { db, protokolliere, echteIp } from "./workspace.js";
|
||
import { stromAn } from "./workspace-chat.js";
|
||
|
||
const FRISCH_MS = 15_000;
|
||
const jetzt = () => new Date().toISOString();
|
||
|
||
/* Bremse je Person: Wer zwanzigmal je Minute die Hand hebt, löst sonst
|
||
zwanzig Rundrufe an die Regie aus. */
|
||
const bremse = new Map();
|
||
function zuSchnell(personId) {
|
||
const t = Date.now();
|
||
const z = (bremse.get(personId) || []).filter((x) => x > t - 60_000);
|
||
if (z.length >= 20) { bremse.set(personId, z); return true; }
|
||
z.push(t);
|
||
bremse.set(personId, z);
|
||
return false;
|
||
}
|
||
|
||
/* hostId -> { zeit, sichtbar, gaeste: Map(gastId -> { bild, ton }) } */
|
||
const empfang = new Map();
|
||
|
||
function regieIds(HOST_ROLLEN) {
|
||
const rollen = [...HOST_ROLLEN];
|
||
return db().prepare(`SELECT id FROM personen WHERE aktiv = 1 AND rolle IN (${rollen.map(() => "?").join(",")})`)
|
||
.all(...rollen).map((z) => z.id);
|
||
}
|
||
|
||
/** Sitzt gerade jemand von der Regie im Saal? (Lebenszeichen der letzten zwei Minuten) */
|
||
function regieImSaal(HOST_ROLLEN) {
|
||
const rollen = [...HOST_ROLLEN];
|
||
const grenze = new Date(Date.now() - 120_000).toISOString();
|
||
return !!db().prepare(`SELECT 1 FROM reaktion_dabei d JOIN personen p ON p.id = d.person_id
|
||
WHERE d.gesehen >= ? AND p.rolle IN (${rollen.map(() => "?").join(",")}) LIMIT 1`).get(grenze, ...rollen);
|
||
}
|
||
|
||
/** Was weiß die Regie von diesem Gast? Vier Antworten, aus Meldungen abgeleitet.
|
||
* ja – bei mindestens einer Regie kommt sein Bild an, und ihre Seite ist vor Augen
|
||
* hoert – kein Bild, aber sein Ton kommt an (Kamera aus)
|
||
* nein – die Regie meldet: kein Bild, kein Ton – oder sie schaut gerade nicht hin
|
||
* unbekannt – keine frische Meldung, obwohl die Regie im Saal sitzt */
|
||
export function siehtFuer(gastId, HOST_ROLLEN) {
|
||
const t = Date.now();
|
||
const frisch = [...empfang.values()].filter((e) => t - e.zeit < FRISCH_MS);
|
||
if (!frisch.length) {
|
||
return regieImSaal(HOST_ROLLEN) ? { sieht: "unbekannt", grund: "keine_meldung" } : { sieht: "nein", grund: "regie_weg" };
|
||
}
|
||
const vorAugen = frisch.filter((e) => e.sichtbar);
|
||
if (vorAugen.some((e) => e.gaeste.get(gastId)?.bild)) return { sieht: "ja", grund: "" };
|
||
if (vorAugen.some((e) => e.gaeste.get(gastId)?.ton)) return { sieht: "hoert", grund: "kein_bild" };
|
||
if (!vorAugen.length) return { sieht: "nein", grund: "regie_schaut_weg" };
|
||
return { sieht: "nein", grund: "kommt_nicht_an" };
|
||
}
|
||
|
||
function gastZeile(personId) {
|
||
return db().prepare("SELECT person_id, stand, bereit_am, hand_am, check_json FROM reaktion_gaeste WHERE person_id = ?").get(personId) || null;
|
||
}
|
||
|
||
/* ==== DER GEPRÜFTE ZWISCHENRAUM (04.11.2026, Neuplan C5) ==================
|
||
Wer zusagt, wartet im Backstage (stand 'backstage'). Dort misst seine
|
||
Seite Licht, Pegel, Echo-Unterdrückung und Leitung und schickt die ZAHLEN
|
||
hierher; die Regie sieht sie, bevor sie ihn auf die Bühne holt. Dazu der
|
||
kurze Draht: Nachrichten nur zwischen Gast und Regie, nie an den Saal.
|
||
Der Draht lebt nur im Arbeitsspeicher (höchstens zehn je Gast) – er ist
|
||
ein Zuruf, kein Protokoll, und geht mit dem Backstage. */
|
||
const DRAHT = new Map();
|
||
const DRAHT_MAX = 10;
|
||
const DRAHT_TEXT_MAX = 200;
|
||
function drahtVon(id) { return DRAHT.get(id) || []; }
|
||
function drahtDazu(id, von, text) {
|
||
const l = drahtVon(id).concat({ von, text, zeit: new Date().toISOString() }).slice(-DRAHT_MAX);
|
||
DRAHT.set(id, l);
|
||
return l;
|
||
}
|
||
const zahl = (x, min, max) => (Number.isFinite(Number(x)) ? Math.min(max, Math.max(min, Number(x))) : null);
|
||
function checkLesen(z) {
|
||
try { return z?.check_json ? JSON.parse(z.check_json) : null; } catch { return null; }
|
||
}
|
||
|
||
export function backstageWege(backstageRouter, { nurHost, kleinerRumpf, HOST_ROLLEN }) {
|
||
backstageRouter.use(["/workspace/api/reaktion/backstage"], kleinerRumpf(2048));
|
||
|
||
const anRegie = (extra = []) => {
|
||
try { stromAn([...new Set([...regieIds(HOST_ROLLEN), ...extra])], "reaktion", { was: "backstage" }); } catch { /* der Takt holt es nach */ }
|
||
};
|
||
|
||
/* Der Stand – je nach Rolle ein anderer Ausschnitt. */
|
||
backstageRouter.get("/workspace/api/reaktion/backstage", (req, res) => {
|
||
try {
|
||
res.set("Cache-Control", "no-store");
|
||
const p = req.person;
|
||
if (HOST_ROLLEN.has(p?.rolle)) {
|
||
const gaeste = db().prepare(`SELECT g.person_id AS id, g.bereit_am, g.hand_am, p.name
|
||
FROM reaktion_gaeste g JOIN personen p ON p.id = g.person_id
|
||
WHERE g.stand = 'dabei' ORDER BY g.platz`).all()
|
||
.map((g) => ({
|
||
id: g.id, name: g.name, bereit: !!g.bereit_am, hand: !!g.hand_am,
|
||
hand_seit_sek: g.hand_am ? Math.max(0, Math.round((Date.now() - Date.parse(g.hand_am)) / 1000)) : null,
|
||
}));
|
||
/* Wer im Backstage wartet – mit allem, was die Regie vor dem Holen
|
||
wissen muss. */
|
||
const warten = db().prepare(`SELECT g.person_id AS id, g.bereit_am, g.check_json, g.seit, p.name
|
||
FROM reaktion_gaeste g JOIN personen p ON p.id = g.person_id
|
||
WHERE g.stand = 'backstage' ORDER BY g.seit`).all()
|
||
.map((g) => ({ id: g.id, name: g.name, bereit: !!g.bereit_am, check: checkLesen(g), draht: drahtVon(g.id) }));
|
||
/* Der Draht geht mit dem Backstage: Wer weder dort noch auf der
|
||
Bühne steht, dessen Zurufe werden vergessen – sonst stünden sie
|
||
bei der nächsten Einladung wieder da. */
|
||
const noch = new Set([...gaeste.map((g) => g.id), ...warten.map((g) => g.id)]);
|
||
for (const id of [...DRAHT.keys()]) if (!noch.has(id)) DRAHT.delete(id);
|
||
return res.json({ rolle: "regie", gaeste, warten });
|
||
}
|
||
const z = gastZeile(p.id);
|
||
if (!z || !["dabei", "gefragt", "backstage"].includes(z.stand)) return res.json({ rolle: "zuschauer" });
|
||
if (z.stand === "backstage") {
|
||
/* Im Backstage sendet er an niemanden – die Regie sieht ihn ehrlich
|
||
noch nicht, sie sieht seine Prüfwerte. */
|
||
return res.json({ rolle: "gast", stand: "backstage", bereit: !!z.bereit_am, hand: false,
|
||
check: checkLesen(z), draht: drahtVon(p.id), regie: { sieht: "nein", grund: "backstage" } });
|
||
}
|
||
if (z.stand === "gefragt") {
|
||
/* Wer erst gefragt ist, sendet noch nichts (`binSender` ist falsch) –
|
||
die Regie KANN ihn nicht sehen. Das ist keine Vermutung, sondern
|
||
der Ablauf. */
|
||
return res.json({ rolle: "gast", stand: "gefragt", bereit: false, hand: false, regie: { sieht: "nein", grund: "noch_nicht_zugesagt" } });
|
||
}
|
||
return res.json({ rolle: "gast", stand: "dabei", bereit: !!z.bereit_am, hand: !!z.hand_am, regie: siehtFuer(p.id, HOST_ROLLEN) });
|
||
} catch (f) {
|
||
console.error("[backstage] lesen:", f?.message);
|
||
res.status(503).json({ fehler: "nicht_verfuegbar" });
|
||
}
|
||
});
|
||
|
||
/* „Ich bin bereit“ – nur für sich selbst, nur auf der Bühne. */
|
||
backstageRouter.post("/workspace/api/reaktion/backstage/bereit", (req, res) => {
|
||
try {
|
||
const z = gastZeile(req.person.id);
|
||
/* Seit 04.11.2026 auch im Backstage – dort gehört „Ich bin bereit“
|
||
eigentlich hin: Erst dann holt die Regie. */
|
||
if (!z || (z.stand !== "dabei" && z.stand !== "backstage")) return res.status(409).json({ fehler: "nicht_auf_der_buehne" });
|
||
if (zuSchnell(req.person.id)) return res.status(429).json({ fehler: "zu_schnell" });
|
||
const bereit = req.body?.bereit !== false;
|
||
db().prepare("UPDATE reaktion_gaeste SET bereit_am = ? WHERE person_id = ?").run(bereit ? jetzt() : "", req.person.id);
|
||
protokolliere("reaktion_gast_bereit", { personId: req.person.id, detail: `${req.person.name}: ${bereit ? "bereit" : "doch nicht"}`, ip: echteIp(req) });
|
||
anRegie([req.person.id]);
|
||
res.json({ bereit });
|
||
} catch (f) {
|
||
console.error("[backstage] bereit:", f?.message);
|
||
res.status(503).json({ fehler: "nicht_verfuegbar" });
|
||
}
|
||
});
|
||
|
||
/* Hand heben / senken – der Gast für sich selbst. */
|
||
/* Die Prüfwerte aus dem Backstage: nur der Gast selbst, nur dort. Gezählt
|
||
wird nur, was eine Zahl im erwarteten Bereich ist. */
|
||
backstageRouter.post("/workspace/api/reaktion/backstage/pruefung", (req, res) => {
|
||
try {
|
||
const z = gastZeile(req.person.id);
|
||
if (!z || z.stand !== "backstage") return res.status(409).json({ fehler: "nicht_im_backstage" });
|
||
if (zuSchnell(req.person.id)) return res.status(429).json({ fehler: "zu_schnell" });
|
||
const b = req.body || {};
|
||
const check = {
|
||
kamera: b.kamera === true, mikro: b.mikro === true,
|
||
licht: zahl(b.licht, 0, 255),
|
||
/* Mitte und Rand für Gegenlicht; das Gesicht nur als ja/nein – null
|
||
heißt „der Browser kann es nicht erkennen“, nie „passt“. */
|
||
licht_mitte: zahl(b.licht_mitte, 0, 255),
|
||
licht_rand: zahl(b.licht_rand, 0, 255),
|
||
gesicht: b.gesicht === true ? true : b.gesicht === false ? false : null,
|
||
pegel_db: zahl(b.pegel_db, -100, 0),
|
||
echo: b.echo === true ? true : b.echo === false ? false : null,
|
||
leitung_kbit: zahl(b.leitung_kbit, 0, 1_000_000),
|
||
zeit: jetzt(),
|
||
};
|
||
db().prepare("UPDATE reaktion_gaeste SET check_json = ? WHERE person_id = ?").run(JSON.stringify(check), req.person.id);
|
||
anRegie();
|
||
res.json({ check });
|
||
} catch (f) {
|
||
console.error("[backstage] pruefung:", f?.message);
|
||
res.status(503).json({ fehler: "nicht_verfuegbar" });
|
||
}
|
||
});
|
||
|
||
/* ==== DIE LEITUNGSPROBE (04.11.2026, Neuplan C5.3 „Leitung gemessen“) ====
|
||
Die Seite des Gastes schickt höchstens 2 MB Füllbytes und misst selbst,
|
||
wie lange das dauert; hier wird nur mitgezählt und weggeworfen. Eigener
|
||
Pfad AUSSERHALB von …/backstage, weil dort die 2-KB-Grenze gilt. Nur für
|
||
Gäste im Backstage oder auf der Bühne, höchstens drei je Minute. Der
|
||
Rumpf wird roh gelesen (kein JSON) und nie gespeichert. */
|
||
const PROBE_MAX = 2 * 1024 * 1024;
|
||
const probeZeiten = new Map();
|
||
backstageRouter.post("/workspace/api/reaktion/leitungsprobe", (req, res) => {
|
||
const z = gastZeile(req.person.id);
|
||
if (!z || (z.stand !== "backstage" && z.stand !== "dabei")) { req.resume(); return res.status(409).json({ fehler: "nicht_im_backstage" }); }
|
||
const angesagt = Number(req.headers["content-length"]);
|
||
if (!Number.isFinite(angesagt) || angesagt > PROBE_MAX) { req.resume(); return res.status(413).json({ fehler: "zu_lang" }); }
|
||
const jetztMs = Date.now();
|
||
const l = (probeZeiten.get(req.person.id) || []).filter((t) => jetztMs - t < 60_000);
|
||
if (l.length >= 3) { req.resume(); return res.status(429).json({ fehler: "zu_schnell" }); }
|
||
l.push(jetztMs);
|
||
probeZeiten.set(req.person.id, l);
|
||
let n = 0;
|
||
req.on("data", (stueck) => {
|
||
n += stueck.length;
|
||
if (n > PROBE_MAX) { req.destroy(); }
|
||
});
|
||
req.on("end", () => { if (!res.headersSent) res.json({ bytes: n }); });
|
||
req.on("error", () => { if (!res.headersSent) res.status(400).json({ fehler: "abgebrochen" }); });
|
||
});
|
||
|
||
/* Der kurze Draht: Gast → Regie und Regie → Gast. Nie an den Saal. */
|
||
const drahtText = (t) => String(t ?? "").replace(/[\u0000-\u001f\u007f\u200b-\u200f\u202a-\u202e\u2066-\u2069]/g, " ").trim().slice(0, DRAHT_TEXT_MAX);
|
||
backstageRouter.post("/workspace/api/reaktion/backstage/draht", (req, res) => {
|
||
try {
|
||
const z = gastZeile(req.person.id);
|
||
if (!z || (z.stand !== "backstage" && z.stand !== "dabei")) return res.status(409).json({ fehler: "nicht_im_backstage" });
|
||
if (zuSchnell(req.person.id)) return res.status(429).json({ fehler: "zu_schnell" });
|
||
const text = drahtText(req.body?.text);
|
||
if (!text) return res.status(400).json({ fehler: "nachricht_fehlt" });
|
||
const l = drahtDazu(req.person.id, "gast", text);
|
||
anRegie([req.person.id]);
|
||
res.json({ draht: l });
|
||
} catch (f) {
|
||
console.error("[backstage] draht:", f?.message);
|
||
res.status(503).json({ fehler: "nicht_verfuegbar" });
|
||
}
|
||
});
|
||
backstageRouter.post("/workspace/api/reaktion/backstage/draht/:id(\\d+)", nurHost, (req, res) => {
|
||
try {
|
||
const id = Number(req.params.id);
|
||
const z = gastZeile(id);
|
||
if (!z || (z.stand !== "backstage" && z.stand !== "dabei")) return res.status(404).json({ fehler: "nicht_gefunden" });
|
||
if (zuSchnell(req.person.id)) return res.status(429).json({ fehler: "zu_schnell" });
|
||
const text = drahtText(req.body?.text);
|
||
if (!text) return res.status(400).json({ fehler: "nachricht_fehlt" });
|
||
const l = drahtDazu(id, "regie", text);
|
||
anRegie([id]);
|
||
res.json({ draht: l });
|
||
} catch (f) {
|
||
console.error("[backstage] draht regie:", f?.message);
|
||
res.status(503).json({ fehler: "nicht_verfuegbar" });
|
||
}
|
||
});
|
||
|
||
backstageRouter.post("/workspace/api/reaktion/backstage/hand", (req, res) => {
|
||
try {
|
||
const z = gastZeile(req.person.id);
|
||
if (!z || z.stand !== "dabei") return res.status(409).json({ fehler: "nicht_auf_der_buehne" });
|
||
if (zuSchnell(req.person.id)) return res.status(429).json({ fehler: "zu_schnell" });
|
||
const oben = req.body?.oben !== false;
|
||
/* Schon oben bleibt oben: Die Zeit zählt ab dem ERSTEN Heben, sonst
|
||
rutscht ein wiederholtes Tippen in der Reihenfolge nach hinten. */
|
||
if (oben && z.hand_am) return res.json({ hand: true });
|
||
db().prepare("UPDATE reaktion_gaeste SET hand_am = ? WHERE person_id = ?").run(oben ? jetzt() : "", req.person.id);
|
||
anRegie([req.person.id]);
|
||
res.json({ hand: oben });
|
||
} catch (f) {
|
||
console.error("[backstage] hand:", f?.message);
|
||
res.status(503).json({ fehler: "nicht_verfuegbar" });
|
||
}
|
||
});
|
||
|
||
/* Die Regie nimmt eine Hand herunter. */
|
||
backstageRouter.post("/workspace/api/reaktion/backstage/hand/:id(\\d+)/runter", nurHost, (req, res) => {
|
||
try {
|
||
const id = Number(req.params.id);
|
||
const a = db().prepare("UPDATE reaktion_gaeste SET hand_am = '' WHERE person_id = ? AND stand = 'dabei'").run(id);
|
||
if (!a.changes) return res.status(404).json({ fehler: "nicht_gefunden" });
|
||
anRegie([id]);
|
||
res.json({ hand: false });
|
||
} catch (f) {
|
||
console.error("[backstage] hand runter:", f?.message);
|
||
res.status(503).json({ fehler: "nicht_verfuegbar" });
|
||
}
|
||
});
|
||
|
||
/* Was bei der Regie WIRKLICH ankommt – gemessen von ihrer Seite. */
|
||
backstageRouter.post("/workspace/api/reaktion/backstage/empfang", nurHost, (req, res) => {
|
||
try {
|
||
const roh = Array.isArray(req.body?.gaeste) ? req.body.gaeste.slice(0, 8) : [];
|
||
const gaeste = new Map();
|
||
for (const g of roh) {
|
||
const id = Number(g?.id);
|
||
if (Number.isInteger(id) && id > 0) gaeste.set(id, { bild: g.bild === true, ton: g.ton === true });
|
||
}
|
||
const betroffen = db().prepare("SELECT person_id FROM reaktion_gaeste WHERE stand = 'dabei'").all().map((z) => z.person_id);
|
||
const vorher = new Map(betroffen.map((id) => [id, siehtFuer(id, HOST_ROLLEN).sieht]));
|
||
empfang.set(req.person.id, { zeit: Date.now(), sichtbar: req.body?.sichtbar === true, gaeste });
|
||
for (const [h, e] of empfang) if (Date.now() - e.zeit > 4 * FRISCH_MS) empfang.delete(h);
|
||
/* Nur wer sich ÄNDERT, bekommt Bescheid – sonst wäre jeder Takt der
|
||
Regie ein Rundruf an alle Gäste. */
|
||
const geaendert = betroffen.filter((id) => vorher.get(id) !== siehtFuer(id, HOST_ROLLEN).sieht);
|
||
if (geaendert.length) { try { stromAn(geaendert, "reaktion", { was: "backstage" }); } catch { /* der Takt holt es nach */ } }
|
||
res.json({ ok: true, geaendert: geaendert.length });
|
||
} catch (f) {
|
||
console.error("[backstage] empfang:", f?.message);
|
||
res.status(503).json({ fehler: "nicht_verfuegbar" });
|
||
}
|
||
});
|
||
}
|