version 0.8.0
This commit is contained in:
parent
f22007003a
commit
e5ca9ef194
95 changed files with 5601 additions and 4042 deletions
268
app/Controllers/Api/GameController.php
Normal file
268
app/Controllers/Api/GameController.php
Normal file
|
|
@ -0,0 +1,268 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/** API-Controller (Game) - 1:1 aus api.php. */
|
||||
final class GameController
|
||||
{
|
||||
public function assign_questions(): void
|
||||
{
|
||||
$pdo = Database::pdo();
|
||||
$now = time();
|
||||
$authUserId = $_SESSION['user_id'] ?? null;
|
||||
$body = input();
|
||||
$code = slugCode((string) ($body['code'] ?? ''));
|
||||
$normalQ = substr(trim((string) ($body['normalQuestion'] ?? '')), 0, 2000);
|
||||
$imposterQ = substr(trim((string) ($body['imposterQuestion'] ?? '')), 0, 2000);
|
||||
$imposterKeys = array_values(array_filter(
|
||||
(array) ($body['imposterKeys'] ?? []),
|
||||
fn($k) => is_string($k) && $k !== ''
|
||||
));
|
||||
|
||||
if ($normalQ === '') respond(['error' => 'Normale Frage darf nicht leer sein.'], 400);
|
||||
if ($imposterQ === '') respond(['error' => 'Imposter-Frage darf nicht leer sein.'], 400);
|
||||
|
||||
if (!empty($imposterKeys)) {
|
||||
$in = implode(',', array_fill(0, count($imposterKeys), '?'));
|
||||
$chk = $pdo->prepare("SELECT pkey FROM gtl_players WHERE room_code=? AND pkey IN ($in)");
|
||||
$chk->execute(array_merge([$code], $imposterKeys));
|
||||
$valid = array_column($chk->fetchAll(), 'pkey');
|
||||
$imposterKeys = array_values(array_intersect($imposterKeys, $valid));
|
||||
}
|
||||
|
||||
$pdo->prepare(
|
||||
'UPDATE gtl_rooms
|
||||
SET normal_question=?, imposter_question=?, liar_keys=?,
|
||||
voted_out_keys=NULL, round_outcome=NULL, voting_open=0,
|
||||
reveal_index=-1, revealed_keys=NULL, questions_revealed=0, updated_at=?
|
||||
WHERE code=?'
|
||||
)->execute([$normalQ, $imposterQ, json_encode($imposterKeys), $now, $code]);
|
||||
|
||||
$ps = $pdo->prepare('SELECT pkey FROM gtl_players WHERE room_code=?');
|
||||
$ps->execute([$code]);
|
||||
$upd = $pdo->prepare(
|
||||
'UPDATE gtl_players SET question=?, vote_for=NULL, vote_locked=0, vote_request=0, late_join=0, updated_at=?
|
||||
WHERE room_code=? AND pkey=?'
|
||||
);
|
||||
foreach (array_column($ps->fetchAll(), 'pkey') as $pk) {
|
||||
$q = in_array($pk, $imposterKeys, true) ? $imposterQ : $normalQ;
|
||||
$upd->execute([$q, $now, $code, $pk]);
|
||||
}
|
||||
|
||||
respond(['ok' => true, 'state' => RoomService::loadRoom($pdo, $code)]);
|
||||
}
|
||||
|
||||
public function submit_answer(): void
|
||||
{
|
||||
$pdo = Database::pdo();
|
||||
$now = time();
|
||||
$authUserId = $_SESSION['user_id'] ?? null;
|
||||
$body = input();
|
||||
$code = slugCode((string) ($body['code'] ?? ''));
|
||||
$pkey = (string) ($body['pkey'] ?? '');
|
||||
$answer = substr(trim((string) ($body['answer'] ?? '')), 0, 2000);
|
||||
if ($answer === '') respond(['error' => 'Antwort darf nicht leer sein.'], 400);
|
||||
|
||||
$pdo->prepare(
|
||||
'UPDATE gtl_players SET answer=?, answered=1, updated_at=? WHERE room_code=? AND pkey=?'
|
||||
)->execute([$answer, $now, $code, $pkey]);
|
||||
$pdo->prepare('UPDATE gtl_rooms SET updated_at=? WHERE code=?')->execute([$now, $code]);
|
||||
respond(['ok' => true]);
|
||||
}
|
||||
|
||||
public function delete_answer(): void
|
||||
{
|
||||
$pdo = Database::pdo();
|
||||
$now = time();
|
||||
$authUserId = $_SESSION['user_id'] ?? null;
|
||||
$body = input();
|
||||
$code = slugCode((string) ($body['code'] ?? ''));
|
||||
$pkey = (string) ($body['pkey'] ?? '');
|
||||
if ($code === '' || $pkey === '') respond(['error' => 'Fehlende Parameter.'], 400);
|
||||
|
||||
$pdo->prepare(
|
||||
'UPDATE gtl_players SET answer=NULL, answered=0, vote_for=NULL, updated_at=?
|
||||
WHERE room_code=? AND pkey=?'
|
||||
)->execute([$now, $code, $pkey]);
|
||||
respond(['ok' => true, 'state' => RoomService::loadRoom($pdo, $code)]);
|
||||
}
|
||||
|
||||
public function reveal(): void
|
||||
{
|
||||
$pdo = Database::pdo();
|
||||
$now = time();
|
||||
$authUserId = $_SESSION['user_id'] ?? null;
|
||||
// Alle gleichzeitig aufdecken
|
||||
$body = input();
|
||||
$code = slugCode((string) ($body['code'] ?? ''));
|
||||
// Collect all player pkeys for this room
|
||||
$pkStmt = $pdo->prepare('SELECT pkey FROM gtl_players WHERE room_code=?');
|
||||
$pkStmt->execute([$code]);
|
||||
$allKeys = array_column($pkStmt->fetchAll(), 'pkey');
|
||||
$pdo->prepare('UPDATE gtl_rooms SET revealed=1, reveal_index=9999, revealed_keys=?, updated_at=? WHERE code=?')
|
||||
->execute([json_encode($allKeys), $now, $code]);
|
||||
respond(['ok' => true, 'state' => RoomService::loadRoom($pdo, $code)]);
|
||||
}
|
||||
|
||||
public function reveal_answer(): void
|
||||
{
|
||||
$pdo = Database::pdo();
|
||||
$now = time();
|
||||
$authUserId = $_SESSION['user_id'] ?? null;
|
||||
// Gezielt eine Antwort aufdecken (Host klickt Spielerkarte)
|
||||
$body = input();
|
||||
$code = slugCode((string) ($body['code'] ?? ''));
|
||||
$rpkey = (string) ($body['pkey'] ?? '');
|
||||
if ($rpkey === '') respond(['error' => 'pkey fehlt.'], 400);
|
||||
$rkStmt = $pdo->prepare('SELECT revealed_keys FROM gtl_rooms WHERE code=?');
|
||||
$rkStmt->execute([$code]);
|
||||
$rkRow = $rkStmt->fetch();
|
||||
if (!$rkRow) respond(['error' => 'Raum nicht gefunden.'], 404);
|
||||
$j2 = fn($v) => (!empty($v) && is_string($v)) ? (json_decode($v, true) ?: []) : [];
|
||||
$rkeys = $j2($rkRow['revealed_keys'] ?? '');
|
||||
if (!in_array($rpkey, $rkeys, true)) {
|
||||
$rkeys[] = $rpkey;
|
||||
}
|
||||
$pdo->prepare('UPDATE gtl_rooms SET revealed=1, reveal_index=?, revealed_keys=?, updated_at=? WHERE code=?')
|
||||
->execute([count($rkeys), json_encode($rkeys), $now, $code]);
|
||||
respond(['ok' => true, 'state' => RoomService::loadRoom($pdo, $code)]);
|
||||
}
|
||||
|
||||
public function reveal_questions(): void
|
||||
{
|
||||
$pdo = Database::pdo();
|
||||
$now = time();
|
||||
$authUserId = $_SESSION['user_id'] ?? null;
|
||||
// Leiter deckt die echte Frage für die Imposter auf (Knopfdruck)
|
||||
$body = input();
|
||||
$code = slugCode((string) ($body['code'] ?? ''));
|
||||
$pdo->prepare('UPDATE gtl_rooms SET questions_revealed=1, updated_at=? WHERE code=?')
|
||||
->execute([$now, $code]);
|
||||
respond(['ok' => true, 'state' => RoomService::loadRoom($pdo, $code)]);
|
||||
}
|
||||
|
||||
public function reveal_next(): void
|
||||
{
|
||||
$pdo = Database::pdo();
|
||||
$now = time();
|
||||
$authUserId = $_SESSION['user_id'] ?? null;
|
||||
// Legacy: Nächste Antwort sequenziell aufdecken (fallback)
|
||||
$body = input();
|
||||
$code = slugCode((string) ($body['code'] ?? ''));
|
||||
$riRow = $pdo->prepare('SELECT reveal_index FROM gtl_rooms WHERE code=?');
|
||||
$riRow->execute([$code]);
|
||||
$riData = $riRow->fetch();
|
||||
if (!$riData) respond(['error' => 'Raum nicht gefunden.'], 404);
|
||||
$newIdx = (int) $riData['reveal_index'] + 1;
|
||||
$pdo->prepare('UPDATE gtl_rooms SET revealed=1, reveal_index=?, updated_at=? WHERE code=?')
|
||||
->execute([$newIdx, $now, $code]);
|
||||
respond(['ok' => true, 'state' => RoomService::loadRoom($pdo, $code)]);
|
||||
}
|
||||
|
||||
public function update_weights(): void
|
||||
{
|
||||
$pdo = Database::pdo();
|
||||
$now = time();
|
||||
$authUserId = $_SESSION['user_id'] ?? null;
|
||||
$body = input();
|
||||
$code = slugCode((string) ($body['code'] ?? ''));
|
||||
$weights = (string) ($body['weights'] ?? '');
|
||||
$decoded = json_decode($weights, true);
|
||||
if (!is_array($decoded)) respond(['error' => 'Ungültiges JSON für Imposter-Gewichte.'], 400);
|
||||
$pdo->prepare('UPDATE gtl_rooms SET imposter_weights=?, updated_at=? WHERE code=?')
|
||||
->execute([$weights, $now, $code]);
|
||||
respond(['ok' => true]);
|
||||
}
|
||||
|
||||
public function get_host_history(): void
|
||||
{
|
||||
$pdo = Database::pdo();
|
||||
$now = time();
|
||||
$authUserId = $_SESSION['user_id'] ?? null;
|
||||
$code = slugCode((string) ($_GET['code'] ?? ''));
|
||||
if ($code === '') respond(['error' => 'Code fehlt.'], 400);
|
||||
|
||||
$stmt = $pdo->prepare(
|
||||
'SELECT r.id AS round_id, r.round_num, r.played_at, r.outcome,
|
||||
rp.player_name, rp.pkey, rp.was_imposter, rp.voted_out,
|
||||
rp.question, rp.answer, u.avatar
|
||||
FROM gtl_game_rounds r
|
||||
JOIN gtl_round_players rp ON rp.round_id = r.id
|
||||
LEFT JOIN gtl_users u ON u.id = rp.user_id
|
||||
WHERE r.room_code = ?
|
||||
ORDER BY r.round_num DESC, rp.id ASC'
|
||||
);
|
||||
$stmt->execute([$code]);
|
||||
$rows = $stmt->fetchAll();
|
||||
|
||||
$grouped = [];
|
||||
foreach ($rows as $row) {
|
||||
$rn = (int) $row['round_num'];
|
||||
if (!isset($grouped[$rn])) {
|
||||
$grouped[$rn] = [
|
||||
'round_num' => $rn,
|
||||
'played_at' => (int) $row['played_at'],
|
||||
'outcome' => $row['outcome'],
|
||||
'players' => [],
|
||||
];
|
||||
}
|
||||
$grouped[$rn]['players'][] = [
|
||||
'name' => $row['player_name'],
|
||||
'pkey' => $row['pkey'],
|
||||
'avatar' => $row['avatar'] ?? null,
|
||||
'was_imposter'=> (bool) $row['was_imposter'],
|
||||
'voted_out' => (bool) $row['voted_out'],
|
||||
'question' => $row['question'] ?? '',
|
||||
'answer' => $row['answer'] ?? '',
|
||||
];
|
||||
}
|
||||
|
||||
respond(['ok' => true, 'history' => array_values($grouped)]);
|
||||
}
|
||||
|
||||
public function score(): void
|
||||
{
|
||||
$pdo = Database::pdo();
|
||||
$now = time();
|
||||
$authUserId = $_SESSION['user_id'] ?? null;
|
||||
$body = input();
|
||||
$code = slugCode((string) ($body['code'] ?? ''));
|
||||
$pkey = (string) ($body['pkey'] ?? '');
|
||||
$delta = (int) ($body['delta'] ?? 0);
|
||||
$pdo->prepare('UPDATE gtl_players SET score=score+?, updated_at=? WHERE room_code=? AND pkey=?')
|
||||
->execute([$delta, $now, $code, $pkey]);
|
||||
respond(['ok' => true]);
|
||||
}
|
||||
|
||||
public function new_round(): void
|
||||
{
|
||||
$pdo = Database::pdo();
|
||||
$now = time();
|
||||
$authUserId = $_SESSION['user_id'] ?? null;
|
||||
$body = input();
|
||||
$code = slugCode((string) ($body['code'] ?? ''));
|
||||
$state = RoomService::loadRoom($pdo, $code);
|
||||
|
||||
// Aktuelle Runde zuerst archivieren (falls noch nicht geschehen)
|
||||
if ($state && !empty($state['players'])) {
|
||||
RoomService::archiveRound($pdo, $state, $now);
|
||||
}
|
||||
|
||||
$pdo->prepare(
|
||||
'UPDATE gtl_rooms
|
||||
SET revealed=0, liar_keys=NULL, voted_out_keys=NULL, round_outcome=NULL,
|
||||
normal_question=NULL, imposter_question=NULL, voting_open=0,
|
||||
reveal_index=-1, revealed_keys=NULL, questions_revealed=0, active_emojis=NULL,
|
||||
round_num=round_num+1, updated_at=?
|
||||
WHERE code=?'
|
||||
)->execute([$now, $code]);
|
||||
|
||||
$pdo->prepare(
|
||||
'UPDATE gtl_players
|
||||
SET question=NULL, answer=NULL, answered=0, vote_for=NULL,
|
||||
vote_locked=0, vote_request=0, late_join=0, updated_at=?
|
||||
WHERE room_code=?'
|
||||
)->execute([$now, $code]);
|
||||
|
||||
respond(['ok' => true, 'state' => RoomService::loadRoom($pdo, $code)]);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue