'Ungültiges Emoji.'], 400); // Load existing, prune stale (>4s), append new, keep max 20 $emojiStmt = $pdo->prepare('SELECT active_emojis FROM gtl_rooms WHERE code=?'); $emojiStmt->execute([$code]); $emojiRow = $emojiStmt->fetch(); if (!$emojiRow) respond(['error' => 'Raum nicht gefunden.'], 404); $list = (!empty($emojiRow['active_emojis'])) ? (json_decode($emojiRow['active_emojis'], true) ?: []) : []; $list = array_filter($list, fn($e) => ($e['ts'] ?? 0) >= ($now - 4)); $list[] = ['emoji' => $emoji, 'ts' => $now, 'id' => uniqid()]; $list = array_values(array_slice($list, -20)); $pdo->prepare('UPDATE gtl_rooms SET active_emojis=?, updated_at=? WHERE code=?') ->execute([json_encode($list), $now, $code]); respond(['ok' => true]); } /** Globale Reaktion (Emoji ODER Bild) per id senden. */ public function send_reaction(): void { $pdo = Database::pdo(); $body = input(); $code = slugCode((string) ($body['code'] ?? '')); $id = (int) ($body['id'] ?? 0); $st = $pdo->prepare('SELECT emoji, image FROM gtl_reactions WHERE id=?'); $st->execute([$id]); $r = $st->fetch(); if (!$r) respond(['error' => 'Unbekannte Reaktion.'], 400); $item = !empty($r['emoji']) ? ['emoji' => $r['emoji']] : ['sticker' => $r['image']]; $this->broadcast($pdo, $code, $item); respond(['ok' => true]); } /** Sticker fliegen lassen: globaler Sticker per id, sonst der eigene Profil-Sticker. */ public function send_sticker(): void { $pdo = Database::pdo(); $authUserId = $_SESSION['user_id'] ?? null; $body = input(); $code = slugCode((string) ($body['code'] ?? '')); $id = (int) ($body['id'] ?? 0); if ($id > 0) { $st = $pdo->prepare('SELECT file FROM gtl_stickers WHERE id=?'); $st->execute([$id]); $file = (string) ($st->fetchColumn() ?: ''); if ($file === '') respond(['error' => 'Unbekannter Sticker.'], 400); $this->broadcast($pdo, $code, ['sticker' => $file]); respond(['ok' => true]); } if (!$authUserId) respond(['error' => 'Bitte erst einloggen.'], 401); $st = $pdo->prepare('SELECT sticker FROM gtl_users WHERE id=?'); $st->execute([$authUserId]); $stickerFile = (string) ($st->fetchColumn() ?: ''); if ($stickerFile === '' || !preg_match('/^[A-Za-z0-9_.\-]+$/', $stickerFile)) { respond(['error' => 'Kein gültiger Sticker vorhanden.'], 400); } $this->broadcast($pdo, $code, ['sticker' => '/uploads/sticker/' . $stickerFile]); respond(['ok' => true]); } /** Soundboard: globalen Sound per id bei allen abspielen. */ public function send_sound(): void { $pdo = Database::pdo(); $body = input(); $code = slugCode((string) ($body['code'] ?? '')); $id = (int) ($body['id'] ?? 0); $st = $pdo->prepare('SELECT file FROM gtl_sounds WHERE id=?'); $st->execute([$id]); $file = (string) ($st->fetchColumn() ?: ''); if ($file === '') respond(['error' => 'Unbekannter Sound.'], 400); $this->broadcast($pdo, $code, ['sound' => $file]); respond(['ok' => true]); } /** Hängt eine Reaktion/Sound/Sticker an die Live-Broadcast-Liste des Raums an. */ private function broadcast(PDO $pdo, string $code, array $item): void { $now = time(); $st = $pdo->prepare('SELECT active_emojis FROM gtl_rooms WHERE code=?'); $st->execute([$code]); $row = $st->fetch(); if (!$row) respond(['error' => 'Raum nicht gefunden.'], 404); $list = (!empty($row['active_emojis'])) ? (json_decode($row['active_emojis'], true) ?: []) : []; $list = array_filter($list, fn($e) => ($e['ts'] ?? 0) >= ($now - 4)); $item['ts'] = $now; $item['id'] = uniqid(); $list[] = $item; $list = array_values(array_slice($list, -20)); $pdo->prepare('UPDATE gtl_rooms SET active_emojis=?, updated_at=? WHERE code=?') ->execute([json_encode($list), $now, $code]); } public function send_chat(): void { $pdo = Database::pdo(); $now = time(); $authUserId = $_SESSION['user_id'] ?? null; $body = input(); $code = slugCode((string) ($body['code'] ?? '')); $sender = substr(trim((string) ($body['name'] ?? '')), 0, 40); $message = substr(trim((string) ($body['message'] ?? '')), 0, 500); if ($code === '' || $sender === '' || $message === '') { respond(['error' => 'Fehlende Parameter.'], 400); } $pdo->prepare( 'INSERT INTO gtl_chat_messages (room_code, player_name, message, created_at) VALUES (?,?,?,?)' )->execute([$code, $sender, $message, $now]); // Keep only last 200 messages per room $pdo->prepare( 'DELETE FROM gtl_chat_messages WHERE room_code=? AND id NOT IN (SELECT id FROM (SELECT id FROM gtl_chat_messages WHERE room_code=? ORDER BY id DESC LIMIT 200) t)' )->execute([$code, $code]); respond(['ok' => true]); } }