BUG FIX: resolve_round now reads liar_keys directly from DB via
performResolveRound() helper, bypassing loadRoom()'s redaction
(liarKeys=[] when roundOutcome is null) — fixes 0 points + no_liar bug.
resolve logic extracted into performResolveRound() used by both
manual resolve_round and auto-resolve in lock_vote.
Migration V7:
- gtl_rooms: reveal_index INT DEFAULT -1, imposter_weights VARCHAR(500)
- gtl_players: vote_locked TINYINT, vote_request TINYINT
API:
- lock_vote: stores JSON votes array, locks player, auto-resolves when all locked
- request_voting: democratic vote-start; opens voting when all players request
- reveal_next: increments reveal_index one answer at a time
- reveal/reveal_all: sets reveal_index=9999 (show all)
- update_weights: saves imposter_weights JSON per room
- resolveVotes(): handles votes[] array instead of single voteFor string
- new_round: resets vote_locked, vote_request, reveal_index, active_emojis
- assign_questions: resets vote state + reveal_index on each new question set
Layout:
- wrap max-width 640→960px for grid layout
- Player view: 3-col grid (sidebar scores | gameplay | emoji reactions)
- Host view: 2-col grid (sidebar players+tally+scores | main content)
- Compact sb-row sidebar rows with avatar + name + score
Voting:
- Multi-select checkboxes (client-side) + "Stimme abschicken" lock button
- Niemand exclusive: checking it clears other selections and vice versa
- Once locked: checkboxes hidden, confirmation shown
- Auto-resolve triggers when last player locks vote
Reveal:
- "Nächste aufdecken" reveals one answer at a time via reveal_index
- "Alle aufdecken" reveals everything at once
- Host sees which answers are still hidden vs revealed
- Players only see answers up to current reveal_index
Imposter weights:
- JSON field in host UI e.g. {"3-5":1,"6-9":2,"10+":3}
- randomImposters() reads weights to pick appropriate count
- Saved per-room via update_weights API
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
198 lines
9 KiB
PHP
198 lines
9 KiB
PHP
<?php
|
|
/**
|
|
* Automatische DB-Migrationen.
|
|
* Wird von api.php und auth.php eingebunden.
|
|
* Führt nur fehlende Versionen aus — ein SELECT pro Request nach dem Setup.
|
|
*/
|
|
declare(strict_types=1);
|
|
|
|
const LATEST_MIGRATION = 7;
|
|
|
|
function runMigrations(PDO $pdo, string $dbname): void
|
|
{
|
|
// Migrations-Tabelle anlegen (immer sicher)
|
|
$pdo->exec("CREATE TABLE IF NOT EXISTS `gtl_migrations` (
|
|
`version` INT UNSIGNED NOT NULL,
|
|
`applied_at` INT UNSIGNED NOT NULL,
|
|
PRIMARY KEY (`version`)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
|
|
|
|
$current = (int) $pdo->query("SELECT COALESCE(MAX(version),0) FROM gtl_migrations")->fetchColumn();
|
|
if ($current >= LATEST_MIGRATION) return;
|
|
|
|
// ── V1: Basis-Tabellen ────────────────────────────────────
|
|
if ($current < 1) {
|
|
$pdo->exec("CREATE TABLE IF NOT EXISTS `gtl_rooms` (
|
|
`code` VARCHAR(12) NOT NULL,
|
|
`pass_hash` VARCHAR(255) NOT NULL,
|
|
`revealed` TINYINT(1) NOT NULL DEFAULT 0,
|
|
`created_at` INT UNSIGNED NOT NULL,
|
|
`updated_at` INT UNSIGNED NOT NULL,
|
|
PRIMARY KEY (`code`)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
|
|
|
|
$pdo->exec("CREATE TABLE IF NOT EXISTS `gtl_players` (
|
|
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
|
|
`room_code` VARCHAR(12) NOT NULL,
|
|
`pkey` VARCHAR(40) NOT NULL,
|
|
`name` VARCHAR(40) NOT NULL,
|
|
`question` TEXT NULL,
|
|
`answer` TEXT NULL,
|
|
`answered` TINYINT(1) NOT NULL DEFAULT 0,
|
|
`score` INT NOT NULL DEFAULT 0,
|
|
`updated_at`INT UNSIGNED NOT NULL,
|
|
PRIMARY KEY (`id`),
|
|
UNIQUE KEY `uniq_room_player` (`room_code`,`pkey`),
|
|
CONSTRAINT `fk_player_room` FOREIGN KEY (`room_code`)
|
|
REFERENCES `gtl_rooms`(`code`) ON DELETE CASCADE
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
|
|
|
|
markMigration($pdo, 1);
|
|
}
|
|
|
|
// ── V2: Neue Spalten + User/Statistik/Verlauf-Tabellen ───
|
|
if ($current < 2) {
|
|
// gtl_rooms
|
|
addColumnIfMissing($pdo, $dbname, 'gtl_rooms', 'liar_keys', 'TEXT NULL AFTER revealed');
|
|
addColumnIfMissing($pdo, $dbname, 'gtl_rooms', 'normal_question', 'TEXT NULL AFTER liar_keys');
|
|
addColumnIfMissing($pdo, $dbname, 'gtl_rooms', 'imposter_question', 'TEXT NULL AFTER normal_question');
|
|
addColumnIfMissing($pdo, $dbname, 'gtl_rooms', 'round_num', 'INT UNSIGNED NOT NULL DEFAULT 1 AFTER imposter_question');
|
|
addColumnIfMissing($pdo, $dbname, 'gtl_rooms', 'voted_out_keys', 'TEXT NULL AFTER round_num');
|
|
addColumnIfMissing($pdo, $dbname, 'gtl_rooms', 'round_outcome', 'VARCHAR(20) NULL AFTER voted_out_keys');
|
|
dropColumnIfExists($pdo, $dbname, 'gtl_rooms', 'liar_key'); // legacy
|
|
|
|
// gtl_players
|
|
addColumnIfMissing($pdo, $dbname, 'gtl_players', 'user_id', 'INT UNSIGNED NULL AFTER name');
|
|
addColumnIfMissing($pdo, $dbname, 'gtl_players', 'vote_for', 'VARCHAR(40) NULL AFTER score');
|
|
|
|
// gtl_users
|
|
$pdo->exec("CREATE TABLE IF NOT EXISTS `gtl_users` (
|
|
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
|
|
`username` VARCHAR(40) NOT NULL,
|
|
`credential_hash` VARCHAR(255) NULL,
|
|
`avatar` VARCHAR(255) NULL,
|
|
`role` ENUM('admin','user') NOT NULL DEFAULT 'user',
|
|
`created_at` INT UNSIGNED NOT NULL,
|
|
`last_login` INT UNSIGNED NULL,
|
|
PRIMARY KEY (`id`),
|
|
UNIQUE KEY `uniq_username` (`username`)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
|
|
|
|
// gtl_user_stats
|
|
$pdo->exec("CREATE TABLE IF NOT EXISTS `gtl_user_stats` (
|
|
`user_id` INT UNSIGNED NOT NULL,
|
|
`games_played` INT UNSIGNED NOT NULL DEFAULT 0,
|
|
`games_won` INT UNSIGNED NOT NULL DEFAULT 0,
|
|
`games_lost` INT UNSIGNED NOT NULL DEFAULT 0,
|
|
`times_imposter` INT UNSIGNED NOT NULL DEFAULT 0,
|
|
`imposter_wins` INT UNSIGNED NOT NULL DEFAULT 0,
|
|
`imposter_losses` INT UNSIGNED NOT NULL DEFAULT 0,
|
|
`innocent_wins` INT UNSIGNED NOT NULL DEFAULT 0,
|
|
`innocent_losses` INT UNSIGNED NOT NULL DEFAULT 0,
|
|
PRIMARY KEY (`user_id`),
|
|
CONSTRAINT `fk_stats_user` FOREIGN KEY (`user_id`) REFERENCES `gtl_users`(`id`) ON DELETE CASCADE
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
|
|
|
|
// gtl_game_rounds
|
|
$pdo->exec("CREATE TABLE IF NOT EXISTS `gtl_game_rounds` (
|
|
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
|
|
`room_code` VARCHAR(12) NOT NULL,
|
|
`round_num` INT UNSIGNED NOT NULL DEFAULT 1,
|
|
`played_at` INT UNSIGNED NOT NULL,
|
|
`outcome` VARCHAR(20) NULL,
|
|
PRIMARY KEY (`id`),
|
|
KEY `idx_room` (`room_code`)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
|
|
|
|
// gtl_round_players
|
|
$pdo->exec("CREATE TABLE IF NOT EXISTS `gtl_round_players` (
|
|
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
|
|
`round_id` INT UNSIGNED NOT NULL,
|
|
`user_id` INT UNSIGNED NULL,
|
|
`player_name` VARCHAR(40) NOT NULL,
|
|
`pkey` VARCHAR(40) NOT NULL,
|
|
`was_imposter`TINYINT(1) NOT NULL DEFAULT 0,
|
|
`voted_out` TINYINT(1) NOT NULL DEFAULT 0,
|
|
PRIMARY KEY (`id`),
|
|
UNIQUE KEY `uniq_round_player` (`round_id`,`pkey`),
|
|
CONSTRAINT `fk_rp_round` FOREIGN KEY (`round_id`) REFERENCES `gtl_game_rounds`(`id`) ON DELETE CASCADE
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
|
|
|
|
markMigration($pdo, 2);
|
|
}
|
|
|
|
// ── V3: Antworten + Fragen im Verlauf speichern ──────────
|
|
if ($current < 3) {
|
|
addColumnIfMissing($pdo, $dbname, 'gtl_round_players', 'question', 'TEXT NULL AFTER voted_out');
|
|
addColumnIfMissing($pdo, $dbname, 'gtl_round_players', 'answer', 'TEXT NULL AFTER question');
|
|
markMigration($pdo, 3);
|
|
}
|
|
|
|
// ── V4: uploads-Verzeichnis anlegen ──────────────────────
|
|
if ($current < 4) {
|
|
$dir = __DIR__ . '/uploads/avatars';
|
|
if (!is_dir($dir)) {
|
|
mkdir($dir, 0755, true);
|
|
}
|
|
markMigration($pdo, 4);
|
|
}
|
|
|
|
// ── V5: Voting-Steuerung ──────────────────────────────────
|
|
if ($current < 5) {
|
|
addColumnIfMissing($pdo, $dbname, 'gtl_rooms', 'voting_open',
|
|
'TINYINT(1) NOT NULL DEFAULT 0 AFTER round_outcome');
|
|
markMigration($pdo, 5);
|
|
}
|
|
|
|
// ── V6: Live-Emoji-Reaktionen ──────────────────────────────
|
|
if ($current < 6) {
|
|
addColumnIfMissing($pdo, $dbname, 'gtl_rooms', 'active_emojis',
|
|
'TEXT NULL DEFAULT NULL AFTER voting_open');
|
|
markMigration($pdo, 6);
|
|
}
|
|
|
|
// ── V7: Schrittweises Aufdecken, Imposter-Gewichte, Vote-Lock, Vote-Request ──
|
|
if ($current < 7) {
|
|
addColumnIfMissing($pdo, $dbname, 'gtl_rooms', 'reveal_index',
|
|
'INT NOT NULL DEFAULT -1 AFTER active_emojis');
|
|
addColumnIfMissing($pdo, $dbname, 'gtl_rooms', 'imposter_weights',
|
|
'VARCHAR(500) NULL DEFAULT NULL AFTER reveal_index');
|
|
addColumnIfMissing($pdo, $dbname, 'gtl_players', 'vote_locked',
|
|
'TINYINT(1) NOT NULL DEFAULT 0');
|
|
addColumnIfMissing($pdo, $dbname, 'gtl_players', 'vote_request',
|
|
'TINYINT(1) NOT NULL DEFAULT 0');
|
|
markMigration($pdo, 7);
|
|
}
|
|
}
|
|
|
|
// ── Hilfsfunktionen ──────────────────────────────────────────
|
|
|
|
function columnExists(PDO $pdo, string $db, string $table, string $col): bool
|
|
{
|
|
$stmt = $pdo->prepare(
|
|
"SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
|
|
WHERE TABLE_SCHEMA=? AND TABLE_NAME=? AND COLUMN_NAME=?"
|
|
);
|
|
$stmt->execute([$db, $table, $col]);
|
|
return (int) $stmt->fetchColumn() > 0;
|
|
}
|
|
|
|
function addColumnIfMissing(PDO $pdo, string $db, string $table, string $col, string $def): void
|
|
{
|
|
if (!columnExists($pdo, $db, $table, $col)) {
|
|
$pdo->exec("ALTER TABLE `$table` ADD COLUMN `$col` $def");
|
|
}
|
|
}
|
|
|
|
function dropColumnIfExists(PDO $pdo, string $db, string $table, string $col): void
|
|
{
|
|
if (columnExists($pdo, $db, $table, $col)) {
|
|
$pdo->exec("ALTER TABLE `$table` DROP COLUMN `$col`");
|
|
}
|
|
}
|
|
|
|
function markMigration(PDO $pdo, int $version): void
|
|
{
|
|
$pdo->prepare("INSERT IGNORE INTO gtl_migrations (version, applied_at) VALUES (?,?)")
|
|
->execute([$version, time()]);
|
|
}
|