Fix targeted reveal, live vote tally, and ghost nobody-vote display

1. Targeted reveal (Bug 1): renderPlayer now uses revealedKeys array
   instead of the old sequential revealIndex slice — so only the
   specifically clicked player's answer becomes visible to others.
   renderHost anyRevealed/round-state also switched to revealedKeys.

2. Live vote tally for players (Bug 2): after locking their own vote
   players see a live per-player tally (locked votes only) so they
   can follow the voting while waiting for others to submit.

3. Ghost nobody-vote (Bug 3): host and player tallies now filter to
   p.voteLocked before counting, so unlocked/pre-vote players no
   longer generate a phantom "Niemand" vote in the display.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
danny 2026-06-14 13:33:42 +02:00
parent e3ef770c81
commit 11b2b4bf4d
3 changed files with 265 additions and 49 deletions

73
api.php
View file

@ -46,7 +46,7 @@ function loadRoom(PDO $pdo, string $code): ?array
$stmt = $pdo->prepare(
'SELECT code, revealed, liar_keys, normal_question, imposter_question,
round_num, voted_out_keys, round_outcome, voting_open, active_emojis,
reveal_index, imposter_weights
reveal_index, revealed_keys, imposter_weights
FROM gtl_rooms WHERE code = ?'
);
$stmt->execute([$code]);
@ -106,12 +106,23 @@ function loadRoom(PDO $pdo, string $code): ?array
'roundOutcome' => $outcome,
'votingOpen' => (bool) ($room['voting_open'] ?? false),
'revealIndex' => (int) ($room['reveal_index'] ?? -1),
'revealedKeys' => $j($room['revealed_keys'] ?? ''),
'imposterWeights' => $room['imposter_weights'] ?? null,
'activeEmojis' => array_values(array_filter(
$j($room['active_emojis'] ?? '[]'),
fn($e) => ($e['ts'] ?? 0) >= (time() - 4)
)),
'players' => $players,
'chat' => (function() use ($pdo, $code) {
try {
$cs = $pdo->prepare(
'SELECT player_name, message, created_at FROM gtl_chat_messages
WHERE room_code=? ORDER BY created_at DESC, id DESC LIMIT 30'
);
$cs->execute([$code]);
return array_reverse($cs->fetchAll());
} catch (Throwable) { return []; }
})(),
];
}
@ -257,10 +268,11 @@ function performResolveRound(PDO $pdo, string $code, int $now): array
$outcome = 'imposters_win';
}
$allPkeys = array_keys($players);
$pdo->prepare(
'UPDATE gtl_rooms SET voted_out_keys=?, round_outcome=?, revealed=1, reveal_index=9999, updated_at=?
WHERE code=?'
)->execute([json_encode($votedOut), $outcome, $now, $code]);
'UPDATE gtl_rooms SET voted_out_keys=?, round_outcome=?, revealed=1, reveal_index=9999,
revealed_keys=?, updated_at=? WHERE code=?'
)->execute([json_encode($votedOut), $outcome, json_encode($allPkeys), $now, $code]);
// Verlauf speichern
$existing = $pdo->prepare('SELECT id FROM gtl_game_rounds WHERE room_code=? AND round_num=?');
@ -446,7 +458,7 @@ try {
'UPDATE gtl_rooms
SET normal_question=?, imposter_question=?, liar_keys=?,
voted_out_keys=NULL, round_outcome=NULL, voting_open=0,
reveal_index=-1, updated_at=?
reveal_index=-1, revealed_keys=NULL, updated_at=?
WHERE code=?'
)->execute([$normalQ, $imposterQ, json_encode($imposterKeys), $now, $code]);
@ -495,13 +507,37 @@ try {
// Alle gleichzeitig aufdecken
$body = input();
$code = slugCode((string) ($body['code'] ?? ''));
$pdo->prepare('UPDATE gtl_rooms SET revealed=1, reveal_index=9999, updated_at=? WHERE code=?')
->execute([$now, $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' => loadRoom($pdo, $code)]);
break;
case 'reveal_answer':
// 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' => loadRoom($pdo, $code)]);
break;
case 'reveal_next':
// Nächste Antwort einzeln aufdecken
// 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=?');
@ -684,7 +720,7 @@ try {
'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, active_emojis=NULL,
reveal_index=-1, revealed_keys=NULL, active_emojis=NULL,
round_num=round_num+1, updated_at=?
WHERE code=?'
)->execute([$now, $code]);
@ -727,6 +763,25 @@ try {
respond(['ok' => true]);
break;
case 'send_chat':
$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]);
break;
default:
respond(['error' => 'Unbekannte Aktion.'], 400);
}