Mid-Game-Join + DB-Upgrade (Migration V11, Foreign Keys)
- V11: late_join-Flag, sticker-Spalte, gtl_groups(+members), gtl_fake_ads, last_seen/current_room - FK-Re-Engineering: user_id -> gtl_users.id ON DELETE CASCADE fuer gtl_players & gtl_round_players (defensiv: Orphan-Cleanup + try/catch, blockiert Migrationen nie) - Nachzuegler: player_join bei laufender Runde -> Zuschauer (answer/vote_locked/late_join gesetzt) - Nachzuegler aus Voting-/Reveal-Zaehlung, Auswertung, Verlauf und Statistik exkludiert - new_round/assign_questions setzen late_join zurueck; Host-Liste zeigt Zuschauer-Icon Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
91e9470a63
commit
182e3bfb2e
3 changed files with 130 additions and 14 deletions
38
api.php
38
api.php
|
|
@ -58,7 +58,7 @@ function loadRoom(PDO $pdo, string $code, bool $revealLiars = false): ?array
|
|||
|
||||
$ps = $pdo->prepare(
|
||||
'SELECT p.pkey, p.name, p.user_id, p.question, p.answer, p.answered, p.score,
|
||||
p.vote_for, p.vote_locked, p.vote_request, u.avatar, u.color
|
||||
p.vote_for, p.vote_locked, p.vote_request, p.late_join, u.avatar, u.color, u.sticker
|
||||
FROM gtl_players p
|
||||
LEFT JOIN gtl_users u ON u.id = p.user_id
|
||||
WHERE p.room_code = ? ORDER BY p.id ASC'
|
||||
|
|
@ -90,6 +90,8 @@ function loadRoom(PDO $pdo, string $code, bool $revealLiars = false): ?array
|
|||
'votes' => $parsedVotes,
|
||||
'voteLocked' => (bool) ($row['vote_locked'] ?? false),
|
||||
'voteRequest' => (bool) ($row['vote_request'] ?? false),
|
||||
'lateJoin' => (bool) ($row['late_join'] ?? false),
|
||||
'sticker' => $row['sticker'] ?? null,
|
||||
];
|
||||
}
|
||||
|
||||
|
|
@ -258,8 +260,10 @@ function performResolveRound(PDO $pdo, string $code, int $now): array
|
|||
|
||||
$state = loadRoom($pdo, $code);
|
||||
$players = $state['players'];
|
||||
// Nachzügler (Zuschauer dieser Runde) zählen nicht für Voting/Verlauf/Statistik
|
||||
$activePlayers = array_filter($players, fn($p) => empty($p['lateJoin']));
|
||||
|
||||
$votedOut = resolveVotes(array_values($players), $liarKeys);
|
||||
$votedOut = resolveVotes(array_values($activePlayers), $liarKeys);
|
||||
|
||||
if (empty($liarKeys)) {
|
||||
$outcome = 'no_liar';
|
||||
|
|
@ -301,7 +305,7 @@ function performResolveRound(PDO $pdo, string $code, int $now): array
|
|||
(round_id, user_id, player_name, pkey, was_imposter, voted_out, question, answer)
|
||||
VALUES (?,?,?,?,?,?,?,?)'
|
||||
);
|
||||
foreach ($players as $p) {
|
||||
foreach ($activePlayers as $p) {
|
||||
$ins->execute([
|
||||
$roundId, $p['userId'], $p['name'], $p['pkey'],
|
||||
in_array($p['pkey'], $liarKeys, true) ? 1 : 0,
|
||||
|
|
@ -328,7 +332,7 @@ function performResolveRound(PDO $pdo, string $code, int $now): array
|
|||
'UPDATE gtl_players SET score=score+1, updated_at=? WHERE room_code=? AND pkey=?'
|
||||
);
|
||||
|
||||
foreach ($players as $p) {
|
||||
foreach ($activePlayers as $p) {
|
||||
$wasImposter = in_array($p['pkey'], $liarKeys, true);
|
||||
$wasVotedOut = in_array($p['pkey'], $votedOut, true);
|
||||
$playerWon = $wasImposter ? !$wasVotedOut : ($outcome === 'innocents_win');
|
||||
|
|
@ -408,6 +412,22 @@ try {
|
|||
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' => loadRoom($pdo, $code)]);
|
||||
break;
|
||||
|
||||
|
|
@ -486,7 +506,7 @@ try {
|
|||
$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, updated_at=?
|
||||
'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) {
|
||||
|
|
@ -619,7 +639,7 @@ try {
|
|||
$rvStmt->execute([$code]);
|
||||
$rvRoom = $rvStmt->fetch();
|
||||
if (!$rvRoom) respond(['error' => 'Raum nicht gefunden.'], 404);
|
||||
$playerCount = (int) $pdo->query('SELECT COUNT(*) FROM gtl_players WHERE room_code=' . $pdo->quote($code))->fetchColumn();
|
||||
$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);
|
||||
|
|
@ -631,7 +651,7 @@ try {
|
|||
->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');
|
||||
$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=?')
|
||||
|
|
@ -665,7 +685,7 @@ try {
|
|||
$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');
|
||||
$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 = performResolveRound($pdo, $code, $now);
|
||||
|
|
@ -772,7 +792,7 @@ try {
|
|||
$pdo->prepare(
|
||||
'UPDATE gtl_players
|
||||
SET question=NULL, answer=NULL, answered=0, vote_for=NULL,
|
||||
vote_locked=0, vote_request=0, updated_at=?
|
||||
vote_locked=0, vote_request=0, late_join=0, updated_at=?
|
||||
WHERE room_code=?'
|
||||
)->execute([$now, $code]);
|
||||
|
||||
|
|
|
|||
96
migrate.php
96
migrate.php
|
|
@ -6,7 +6,7 @@
|
|||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
const LATEST_MIGRATION = 10;
|
||||
const LATEST_MIGRATION = 11;
|
||||
|
||||
function runMigrations(PDO $pdo, string $dbname): void
|
||||
{
|
||||
|
|
@ -193,6 +193,70 @@ function runMigrations(PDO $pdo, string $dbname): void
|
|||
'TINYINT(1) NOT NULL DEFAULT 0');
|
||||
markMigration($pdo, 10);
|
||||
}
|
||||
|
||||
// ── V11: Mid-Game-Join, Custom-Sticker, Gruppen, Fake-Ads, Foreign Keys ──
|
||||
if ($current < 11) {
|
||||
// Nachzügler-Flag (rundenbezogen): Spieler, die mitten in einer Runde joinen
|
||||
addColumnIfMissing($pdo, $dbname, 'gtl_players', 'late_join',
|
||||
'TINYINT(1) NOT NULL DEFAULT 0 AFTER vote_request');
|
||||
|
||||
// Custom-Profil-Sticker (Dateiname in uploads/sticker/)
|
||||
addColumnIfMissing($pdo, $dbname, 'gtl_users', 'sticker',
|
||||
'VARCHAR(255) NULL DEFAULT NULL AFTER avatar');
|
||||
|
||||
// Herden / Gang-System
|
||||
$pdo->exec("CREATE TABLE IF NOT EXISTS `gtl_groups` (
|
||||
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
`group_name` VARCHAR(60) NOT NULL,
|
||||
`created_by` INT UNSIGNED NULL,
|
||||
`created_at` INT UNSIGNED NOT NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `idx_creator` (`created_by`),
|
||||
CONSTRAINT `fk_group_creator` FOREIGN KEY (`created_by`)
|
||||
REFERENCES `gtl_users`(`id`) ON DELETE SET NULL
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
|
||||
|
||||
// Mitgliedschaften (Mapping User ↔ Gruppe) – nötig für Online-/Lobby-Übersicht
|
||||
$pdo->exec("CREATE TABLE IF NOT EXISTS `gtl_group_members` (
|
||||
`group_id` INT UNSIGNED NOT NULL,
|
||||
`user_id` INT UNSIGNED NOT NULL,
|
||||
`joined_at`INT UNSIGNED NOT NULL,
|
||||
PRIMARY KEY (`group_id`,`user_id`),
|
||||
KEY `idx_member_user` (`user_id`),
|
||||
CONSTRAINT `fk_gm_group` FOREIGN KEY (`group_id`) REFERENCES `gtl_groups`(`id`) ON DELETE CASCADE,
|
||||
CONSTRAINT `fk_gm_user` FOREIGN KEY (`user_id`) REFERENCES `gtl_users`(`id`) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
|
||||
|
||||
// Fake-Werbung (Admin-Joke)
|
||||
$pdo->exec("CREATE TABLE IF NOT EXISTS `gtl_fake_ads` (
|
||||
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
`image_url` VARCHAR(500) NOT NULL,
|
||||
`target_url` VARCHAR(500) NULL,
|
||||
`is_active` TINYINT(1) NOT NULL DEFAULT 1,
|
||||
`created_at` INT UNSIGNED NOT NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `idx_active` (`is_active`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
|
||||
|
||||
// Re-Engineering auf echte Foreign Keys (user_id → gtl_users.id, ON DELETE CASCADE).
|
||||
// Defensiv: erst verwaiste Verweise auf NULL setzen, dann FK anlegen.
|
||||
addForeignKeyIfMissing($pdo, $dbname, 'gtl_players', 'fk_player_user',
|
||||
'user_id', 'gtl_users', 'id', 'ON DELETE CASCADE');
|
||||
addForeignKeyIfMissing($pdo, $dbname, 'gtl_round_players', 'fk_rp_user',
|
||||
'user_id', 'gtl_users', 'id', 'ON DELETE CASCADE');
|
||||
|
||||
// Online-Tracking für die Herden-Übersicht
|
||||
addColumnIfMissing($pdo, $dbname, 'gtl_users', 'last_seen',
|
||||
'INT UNSIGNED NULL DEFAULT NULL AFTER last_login');
|
||||
addColumnIfMissing($pdo, $dbname, 'gtl_users', 'current_room',
|
||||
'VARCHAR(12) NULL DEFAULT NULL AFTER last_seen');
|
||||
|
||||
// Sticker-Upload-Verzeichnis
|
||||
$sdir = __DIR__ . '/uploads/sticker';
|
||||
if (!is_dir($sdir)) { @mkdir($sdir, 0755, true); }
|
||||
|
||||
markMigration($pdo, 11);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Hilfsfunktionen ──────────────────────────────────────────
|
||||
|
|
@ -214,6 +278,36 @@ function addColumnIfMissing(PDO $pdo, string $db, string $table, string $col, st
|
|||
}
|
||||
}
|
||||
|
||||
function foreignKeyExists(PDO $pdo, string $db, string $table, string $constraint): bool
|
||||
{
|
||||
$stmt = $pdo->prepare(
|
||||
"SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
|
||||
WHERE CONSTRAINT_SCHEMA=? AND TABLE_NAME=? AND CONSTRAINT_NAME=? AND CONSTRAINT_TYPE='FOREIGN KEY'"
|
||||
);
|
||||
$stmt->execute([$db, $table, $constraint]);
|
||||
return (int) $stmt->fetchColumn() > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Legt einen Foreign Key an, falls noch nicht vorhanden.
|
||||
* Räumt vorher verwaiste Verweise auf (→ NULL) und kapselt Fehler,
|
||||
* damit eine fehlschlagende FK-Anlage die Migrationen niemals blockiert.
|
||||
*/
|
||||
function addForeignKeyIfMissing(PDO $pdo, string $db, string $table, string $constraint,
|
||||
string $col, string $refTable, string $refCol, string $onDelete): void
|
||||
{
|
||||
if (foreignKeyExists($pdo, $db, $table, $constraint)) return;
|
||||
if (!columnExists($pdo, $db, $table, $col)) return;
|
||||
try {
|
||||
$pdo->exec("UPDATE `$table` SET `$col`=NULL
|
||||
WHERE `$col` IS NOT NULL AND `$col` NOT IN (SELECT `$refCol` FROM `$refTable`)");
|
||||
$pdo->exec("ALTER TABLE `$table` ADD CONSTRAINT `$constraint`
|
||||
FOREIGN KEY (`$col`) REFERENCES `$refTable`(`$refCol`) $onDelete");
|
||||
} catch (Throwable $e) {
|
||||
// Daten-Inkonsistenz o. ä. → FK nicht erzwingen, Migration trotzdem abschließen
|
||||
}
|
||||
}
|
||||
|
||||
function dropColumnIfExists(PDO $pdo, string $db, string $table, string $col): void
|
||||
{
|
||||
if (columnExists($pdo, $db, $table, $col)) {
|
||||
|
|
|
|||
10
play.html
10
play.html
|
|
@ -1356,13 +1356,15 @@ const App = (() => {
|
|||
nameEl.textContent=p.name;
|
||||
nameEl.style.color=nameColor(p);
|
||||
const badge=row.querySelector('[data-badge]');
|
||||
// Während der Voting-Phase: Sanduhr (überlegt noch) / Häkchen (abgestimmt).
|
||||
// Sonst: Antwort-Status (abgegeben / wartet).
|
||||
const want=votingActive ? (p.voteLocked?'voted':'voting') : (p.answered?'done':'wait');
|
||||
// Nachzügler = Zuschauer dieser Runde. Sonst: während Voting Sanduhr/Häkchen,
|
||||
// davor Antwort-Status (abgegeben / wartet).
|
||||
const want=p.lateJoin ? 'late'
|
||||
: (votingActive ? (p.voteLocked?'voted':'voting') : (p.answered?'done':'wait'));
|
||||
if(badge.dataset.state!==want){
|
||||
badge.dataset.state=want;
|
||||
badge.className='badge '+(want==='voted'||want==='done'?'done':'wait');
|
||||
badge.textContent=want==='voted'?'✅':want==='voting'?'⏳':want==='done'?'✓':'…';
|
||||
badge.textContent=want==='late'?'👀':want==='voted'?'✅':want==='voting'?'⏳':want==='done'?'✓':'…';
|
||||
badge.title=want==='late'?'Später beigetreten – Zuschauer':'';
|
||||
}
|
||||
});
|
||||
wrap.querySelectorAll('[data-row]').forEach(row=>{if(!seen.has(row.dataset.row))row.remove();});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue