version 0.8.0
This commit is contained in:
parent
f22007003a
commit
e5ca9ef194
95 changed files with 5601 additions and 4042 deletions
109
app/Controllers/Api/VotingController.php
Normal file
109
app/Controllers/Api/VotingController.php
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/** API-Controller (Voting) - 1:1 aus api.php. */
|
||||
final class VotingController
|
||||
{
|
||||
public function open_voting(): void
|
||||
{
|
||||
$pdo = Database::pdo();
|
||||
$now = time();
|
||||
$authUserId = $_SESSION['user_id'] ?? null;
|
||||
$body = input();
|
||||
$code = slugCode((string) ($body['code'] ?? ''));
|
||||
$pdo->prepare('UPDATE gtl_rooms SET voting_open=1, updated_at=? WHERE code=?')
|
||||
->execute([$now, $code]);
|
||||
respond(['ok' => true, 'state' => RoomService::loadRoom($pdo, $code)]);
|
||||
}
|
||||
|
||||
public function request_voting(): void
|
||||
{
|
||||
$pdo = Database::pdo();
|
||||
$now = time();
|
||||
$authUserId = $_SESSION['user_id'] ?? null;
|
||||
// Demokratischer Voting-Antrag: sobald ALLE Spieler angefragt haben → auto-open
|
||||
$body = input();
|
||||
$code = slugCode((string) ($body['code'] ?? ''));
|
||||
$pkey = (string) ($body['pkey'] ?? '');
|
||||
|
||||
// Antrag nur erlauben, wenn alle Antworten aufgedeckt sind
|
||||
$rvStmt = $pdo->prepare('SELECT reveal_index, revealed_keys FROM gtl_rooms WHERE code=?');
|
||||
$rvStmt->execute([$code]);
|
||||
$rvRoom = $rvStmt->fetch();
|
||||
if (!$rvRoom) respond(['error' => 'Raum nicht gefunden.'], 404);
|
||||
$playerCount = (int) $pdo->query('SELECT COUNT(*) FROM gtl_players WHERE late_join=0 AND room_code=' . $pdo->quote($code))->fetchColumn();
|
||||
$revealedKeys = (!empty($rvRoom['revealed_keys'])) ? (json_decode($rvRoom['revealed_keys'], true) ?: []) : [];
|
||||
$allRevealed = $playerCount > 0
|
||||
&& ((int) ($rvRoom['reveal_index'] ?? -1) >= 9999 || count($revealedKeys) >= $playerCount);
|
||||
if (!$allRevealed) {
|
||||
respond(['error' => 'Erst müssen alle Antworten aufgedeckt werden.'], 409);
|
||||
}
|
||||
|
||||
$pdo->prepare('UPDATE gtl_players SET vote_request=1, updated_at=? WHERE room_code=? AND pkey=?')
|
||||
->execute([$now, $code, $pkey]);
|
||||
$pdo->prepare('UPDATE gtl_rooms SET updated_at=? WHERE code=?')->execute([$now, $code]);
|
||||
// Wenn alle Spieler request gesetzt haben → Voting öffnen
|
||||
$pending = $pdo->prepare('SELECT COUNT(*) FROM gtl_players WHERE room_code=? AND vote_request=0 AND late_join=0');
|
||||
$pending->execute([$code]);
|
||||
if ((int) $pending->fetchColumn() === 0) {
|
||||
$pdo->prepare('UPDATE gtl_rooms SET voting_open=1, updated_at=? WHERE code=?')
|
||||
->execute([$now, $code]);
|
||||
}
|
||||
respond(['ok' => true, 'state' => RoomService::loadRoom($pdo, $code)]);
|
||||
}
|
||||
|
||||
public function lock_vote(): void
|
||||
{
|
||||
$pdo = Database::pdo();
|
||||
$now = time();
|
||||
$authUserId = $_SESSION['user_id'] ?? null;
|
||||
// Stimme final abschicken. Sperrt die Auswahl; wenn alle gesperrt → Auto-Auswertung.
|
||||
$body = input();
|
||||
$code = slugCode((string) ($body['code'] ?? ''));
|
||||
$pkey = (string) ($body['pkey'] ?? '');
|
||||
$votes = array_values(array_filter((array) ($body['votes'] ?? []), 'is_string'));
|
||||
|
||||
$vLock = $pdo->prepare('SELECT voting_open FROM gtl_rooms WHERE code=?');
|
||||
$vLock->execute([$code]);
|
||||
$vLockRow = $vLock->fetch();
|
||||
if (!$vLockRow || !$vLockRow['voting_open']) {
|
||||
respond(['error' => 'Das Voting ist noch nicht geöffnet.'], 403);
|
||||
}
|
||||
// Niemand ist exklusiv; eigene pkey nicht erlaubt
|
||||
if (in_array('__nobody__', $votes, true)) {
|
||||
$votes = ['__nobody__'];
|
||||
} else {
|
||||
$votes = array_values(array_filter($votes, fn($v) => $v !== $pkey));
|
||||
}
|
||||
$pdo->prepare(
|
||||
'UPDATE gtl_players SET vote_for=?, vote_locked=1, updated_at=? WHERE room_code=? AND pkey=?'
|
||||
)->execute([json_encode($votes), $now, $code, $pkey]);
|
||||
$pdo->prepare('UPDATE gtl_rooms SET updated_at=? WHERE code=?')->execute([$now, $code]);
|
||||
|
||||
// Auto-Auswertung wenn alle Spieler abgestimmt haben
|
||||
$unlocked = $pdo->prepare('SELECT COUNT(*) FROM gtl_players WHERE room_code=? AND vote_locked=0 AND late_join=0');
|
||||
$unlocked->execute([$code]);
|
||||
if ((int) $unlocked->fetchColumn() === 0) {
|
||||
$newState = RoomService::performResolveRound($pdo, $code, $now);
|
||||
respond(['ok' => true, 'autoResolved' => true, 'state' => $newState]);
|
||||
}
|
||||
respond(['ok' => true, 'state' => RoomService::loadRoom($pdo, $code)]);
|
||||
}
|
||||
|
||||
public function resolve_round(): void
|
||||
{
|
||||
$pdo = Database::pdo();
|
||||
$now = time();
|
||||
$authUserId = $_SESSION['user_id'] ?? null;
|
||||
// Manueller Trigger durch Host — nutzt den gemeinsamen Helper
|
||||
$body = input();
|
||||
$code = slugCode((string) ($body['code'] ?? ''));
|
||||
$check = RoomService::loadRoom($pdo, $code);
|
||||
if ($check === null) respond(['error' => 'Raum nicht gefunden.'], 404);
|
||||
if (!$check['revealed'] && $check['revealIndex'] < 0) {
|
||||
respond(['error' => 'Runde noch nicht aufgedeckt.'], 400);
|
||||
}
|
||||
$newState = RoomService::performResolveRound($pdo, $code, $now);
|
||||
respond(['ok' => true, 'state' => $newState]);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue