version 0.8.0
This commit is contained in:
parent
f22007003a
commit
e5ca9ef194
95 changed files with 5601 additions and 4042 deletions
187
app/Controllers/Api/RoomController.php
Normal file
187
app/Controllers/Api/RoomController.php
Normal file
|
|
@ -0,0 +1,187 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/** API-Controller (Room) - 1:1 aus api.php. */
|
||||
final class RoomController
|
||||
{
|
||||
public function host_open(): void
|
||||
{
|
||||
$pdo = Database::pdo();
|
||||
$now = time();
|
||||
$authUserId = $_SESSION['user_id'] ?? null;
|
||||
$body = input();
|
||||
$code = slugCode((string) ($body['code'] ?? ''));
|
||||
$pass = (string) ($body['pass'] ?? '');
|
||||
if ($code === '' || $pass === '') respond(['error' => 'Code und Passwort erforderlich.'], 400);
|
||||
|
||||
$stmt = $pdo->prepare('SELECT pass_hash FROM gtl_rooms WHERE code = ?');
|
||||
$stmt->execute([$code]);
|
||||
$existing = $stmt->fetch();
|
||||
|
||||
if ($existing === false) {
|
||||
$pdo->prepare(
|
||||
'INSERT INTO gtl_rooms (code, pass_hash, revealed, round_num, created_at, updated_at)
|
||||
VALUES (?, ?, 0, 1, ?, ?)'
|
||||
)->execute([$code, password_hash($pass, PASSWORD_DEFAULT), $now, $now]);
|
||||
} elseif (!password_verify($pass, $existing['pass_hash'])) {
|
||||
respond(['error' => 'Falsches Passwort.'], 403);
|
||||
}
|
||||
|
||||
respond(['ok' => true, 'state' => RoomService::loadRoom($pdo, $code)]);
|
||||
}
|
||||
|
||||
public function player_join(): void
|
||||
{
|
||||
$pdo = Database::pdo();
|
||||
$now = time();
|
||||
$authUserId = $_SESSION['user_id'] ?? null;
|
||||
if (!$authUserId) respond(['error' => 'Bitte erst einloggen.'], 401);
|
||||
$body = input();
|
||||
$code = slugCode((string) ($body['code'] ?? ''));
|
||||
$name = substr(trim((string) ($body['name'] ?? '')), 0, 40);
|
||||
if ($code === '' || $name === '') respond(['error' => 'Code und Name erforderlich.'], 400);
|
||||
|
||||
$chk = $pdo->prepare('SELECT 1 FROM gtl_rooms WHERE code = ?');
|
||||
$chk->execute([$code]);
|
||||
if ($chk->fetch() === false) respond(['error' => 'Raum existiert nicht. Spielleiter muss ihn zuerst eröffnen.'], 404);
|
||||
|
||||
$pkey = playerKey($name);
|
||||
$pdo->prepare(
|
||||
'INSERT INTO gtl_players (room_code, pkey, name, user_id, answered, score, updated_at)
|
||||
VALUES (?, ?, ?, ?, 0, 0, ?)
|
||||
ON DUPLICATE KEY UPDATE name=VALUES(name), user_id=VALUES(user_id), updated_at=VALUES(updated_at)'
|
||||
)->execute([$code, $pkey, $name, $authUserId, $now]);
|
||||
|
||||
// Nachzügler-Logik: Läuft bereits eine Runde (Frage gesetzt, nicht aufgelöst),
|
||||
// wird ein NEU beitretender Spieler für diese Runde zum Zuschauer. Er blockiert
|
||||
// weder Reveal noch Auto-Voting und wird bei new_round wieder voll aktiv.
|
||||
$rstmt = $pdo->prepare('SELECT normal_question, round_outcome FROM gtl_rooms WHERE code=?');
|
||||
$rstmt->execute([$code]);
|
||||
$rrow = $rstmt->fetch();
|
||||
$roundActive = $rrow && !empty($rrow['normal_question']) && empty($rrow['round_outcome']);
|
||||
if ($roundActive) {
|
||||
$pdo->prepare(
|
||||
"UPDATE gtl_players
|
||||
SET answer='Zuschauer / Später beigetreten', answered=1,
|
||||
vote_locked=1, late_join=1, updated_at=?
|
||||
WHERE room_code=? AND pkey=? AND (question IS NULL OR question='')"
|
||||
)->execute([$now, $code, $pkey]);
|
||||
}
|
||||
|
||||
respond(['ok' => true, 'pkey' => $pkey, 'state' => RoomService::loadRoom($pdo, $code)]);
|
||||
}
|
||||
|
||||
public function admin_spectate(): void
|
||||
{
|
||||
$pdo = Database::pdo();
|
||||
$now = time();
|
||||
$authUserId = $_SESSION['user_id'] ?? null;
|
||||
if (!$authUserId) respond(['error' => 'Bitte erst einloggen.'], 401);
|
||||
$roleStmt = $pdo->prepare('SELECT role FROM gtl_users WHERE id = ?');
|
||||
$roleStmt->execute([$authUserId]);
|
||||
if ($roleStmt->fetchColumn() !== 'admin') respond(['error' => 'Keine Berechtigung.'], 403);
|
||||
|
||||
$body = input();
|
||||
$code = slugCode((string) ($body['code'] ?? ''));
|
||||
if ($code === '') respond(['error' => 'Raum-Code erforderlich.'], 400);
|
||||
$state = RoomService::loadRoom($pdo, $code, true);
|
||||
if ($state === null) respond(['error' => 'Raum nicht gefunden.'], 404);
|
||||
// Remember this admin is spectating this room → state polls keep revealing imposters
|
||||
$_SESSION['spectate'][$code] = true;
|
||||
respond(['ok' => true, 'state' => $state]);
|
||||
}
|
||||
|
||||
public function state(): void
|
||||
{
|
||||
$pdo = Database::pdo();
|
||||
$now = time();
|
||||
$authUserId = $_SESSION['user_id'] ?? null;
|
||||
$code = slugCode((string) ($_GET['code'] ?? ''));
|
||||
// Update activity timestamp so room stays alive while anyone polls
|
||||
$pdo->prepare('UPDATE gtl_rooms SET updated_at=? WHERE code=?')->execute([$now, $code]);
|
||||
// Präsenz für die Herden-Übersicht: dieser User ist gerade in diesem Raum
|
||||
if ($authUserId) {
|
||||
$pdo->prepare('UPDATE gtl_users SET last_seen=?, current_room=? WHERE id=?')
|
||||
->execute([$now, $code, $authUserId]);
|
||||
}
|
||||
// Lazy cleanup: delete rooms inactive for 12+ hours
|
||||
$pdo->exec('DELETE FROM gtl_rooms WHERE updated_at < ' . ($now - 43200));
|
||||
// Admin spectators keep seeing imposters while polling (flag set via admin_spectate)
|
||||
$spectating = !empty($_SESSION['spectate'][$code]);
|
||||
$state = RoomService::loadRoom($pdo, $code, $spectating);
|
||||
if ($state === null) respond(['error' => 'Raum nicht gefunden.'], 404);
|
||||
respond(['ok' => true, 'state' => $state]);
|
||||
}
|
||||
|
||||
public function delete_room(): void
|
||||
{
|
||||
$pdo = Database::pdo();
|
||||
$now = time();
|
||||
$authUserId = $_SESSION['user_id'] ?? null;
|
||||
$body = input();
|
||||
$code = slugCode((string) ($body['code'] ?? ''));
|
||||
$pass = (string) ($body['pass'] ?? '');
|
||||
$stmt = $pdo->prepare('SELECT pass_hash FROM gtl_rooms WHERE code=?');
|
||||
$stmt->execute([$code]);
|
||||
$row = $stmt->fetch();
|
||||
if (!$row || !password_verify($pass, $row['pass_hash'])) {
|
||||
respond(['error' => 'Falsches Passwort.'], 403);
|
||||
}
|
||||
$pdo->prepare('DELETE FROM gtl_rooms WHERE code=?')->execute([$code]);
|
||||
respond(['ok' => true]);
|
||||
}
|
||||
|
||||
public function remove_player(): void
|
||||
{
|
||||
$pdo = Database::pdo();
|
||||
$now = time();
|
||||
$authUserId = $_SESSION['user_id'] ?? null;
|
||||
$body = input();
|
||||
$code = slugCode((string) ($body['code'] ?? ''));
|
||||
$pkey = (string) ($body['pkey'] ?? '');
|
||||
|
||||
$pdo->prepare('DELETE FROM gtl_players WHERE room_code=? AND pkey=?')
|
||||
->execute([$code, $pkey]);
|
||||
|
||||
$stmt = $pdo->prepare('SELECT liar_keys FROM gtl_rooms WHERE code=?');
|
||||
$stmt->execute([$code]);
|
||||
$row = $stmt->fetch();
|
||||
if ($row && !empty($row['liar_keys'])) {
|
||||
$keys = json_decode($row['liar_keys'], true) ?: [];
|
||||
$keys = array_values(array_filter($keys, fn($k) => $k !== $pkey));
|
||||
$pdo->prepare('UPDATE gtl_rooms SET liar_keys=?, updated_at=? WHERE code=?')
|
||||
->execute([json_encode($keys), $now, $code]);
|
||||
}
|
||||
respond(['ok' => true]);
|
||||
}
|
||||
|
||||
public function reset_scores(): void
|
||||
{
|
||||
$pdo = Database::pdo();
|
||||
$now = time();
|
||||
$authUserId = $_SESSION['user_id'] ?? null;
|
||||
$body = input();
|
||||
$code = slugCode((string) ($body['code'] ?? ''));
|
||||
$pdo->prepare('UPDATE gtl_players SET score=0, updated_at=? WHERE room_code=?')
|
||||
->execute([$now, $code]);
|
||||
respond(['ok' => true]);
|
||||
}
|
||||
|
||||
/** Liste aktiver Lobbys (für die Lobby-Übersicht im Join-Screen). */
|
||||
public function active_rooms(): void
|
||||
{
|
||||
$pdo = Database::pdo();
|
||||
$cutoff = time() - 150; // ~2,5 Min ohne Aktivität => nicht mehr gelistet
|
||||
$stmt = $pdo->prepare(
|
||||
'SELECT r.code, r.round_num, r.voting_open, r.updated_at, COUNT(p.id) AS players
|
||||
FROM gtl_rooms r
|
||||
JOIN gtl_players p ON p.room_code = r.code
|
||||
WHERE r.updated_at >= ?
|
||||
GROUP BY r.code, r.round_num, r.voting_open, r.updated_at
|
||||
ORDER BY r.updated_at DESC
|
||||
LIMIT 30'
|
||||
);
|
||||
$stmt->execute([$cutoff]);
|
||||
respond(['ok' => true, 'rooms' => $stmt->fetchAll()]);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue