prepare("SELECT * FROM tournaments WHERE id = ?");
$stmt->execute([$tournamentId]);
$tournament = $stmt->fetch();
if (!$tournament) {
header("Location: /tournaments.php");
exit;
}
$message = '';
// Generate bracket
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['generate_bracket'])) {
// Check if matches already exist
$stmt = db()->prepare("SELECT COUNT(*) FROM matches WHERE tournament_id = ?");
$stmt->execute([$tournamentId]);
if ($stmt->fetchColumn() == 0) {
$teamsStmt = db()->query("SELECT * FROM teams");
$teams = $teamsStmt->fetchAll();
shuffle($teams);
$round = 1;
$numTeams = count($teams);
for ($i = 0; $i < $numTeams; $i += 2) {
$team1_id = $teams[$i]['id'];
$team2_id = isset($teams[$i+1]) ? $teams[$i+1]['id'] : null;
$stmt = db()->prepare("INSERT INTO matches (tournament_id, round, team1_id, team2_id) VALUES (?, ?, ?, ?)");
$stmt->execute([$tournamentId, $round, $team1_id, $team2_id]);
if ($team2_id === null) { // Bye week
$matchId = db()->lastInsertId();
$stmt = db()->prepare("UPDATE matches SET winner_id = ? WHERE id = ?");
$stmt->execute([$team1_id, $matchId]);
}
}
$message = '
Bracket generated successfully!
';
} else {
$message = 'Bracket has already been generated.
';
}
}
// Update winner
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_winner'])) {
$matchId = $_POST['match_id'];
$winnerId = $_POST['winner_id'];
$stmt = db()->prepare("UPDATE matches SET winner_id = ? WHERE id = ?");
$stmt->execute([$winnerId, $matchId]);
$message = 'Winner updated!
';
}
// Generate next round
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['next_round'])) {
$stmt = db()->prepare("SELECT MAX(round) FROM matches WHERE tournament_id = ?");
$stmt->execute([$tournamentId]);
$currentRound = $stmt->fetchColumn();
$stmt = db()->prepare("SELECT winner_id FROM matches WHERE tournament_id = ? AND round = ? AND winner_id IS NOT NULL");
$stmt->execute([$tournamentId, $currentRound]);
$winners = $stmt->fetchAll(PDO::FETCH_COLUMN);
if (count($winners) > 1) {
$nextRound = $currentRound + 1;
for ($i = 0; $i < count($winners); $i += 2) {
$team1_id = $winners[$i];
$team2_id = isset($winners[$i+1]) ? $winners[$i+1] : null;
$stmt = db()->prepare("INSERT INTO matches (tournament_id, round, team1_id, team2_id) VALUES (?, ?, ?, ?)");
$stmt->execute([$tournamentId, $nextRound, $team1_id, $team2_id]);
if ($team2_id === null) { // Bye week
$matchId = db()->lastInsertId();
$stmt = db()->prepare("UPDATE matches SET winner_id = ? WHERE id = ?");
$stmt->execute([$team1_id, $matchId]);
}
}
$message = 'Next round generated!
';
} else {
$message = 'Tournament finished!
';
}
}
// Fetch matches
$matchesStmt = db()->prepare("
SELECT m.id, m.round, t1.name as team1_name, t2.name as team2_name, m.team1_id, m.team2_id, w.name as winner_name
FROM matches m
LEFT JOIN teams t1 ON m.team1_id = t1.id
LEFT JOIN teams t2 ON m.team2_id = t2.id
LEFT JOIN teams w ON m.winner_id = w.id
WHERE m.tournament_id = ?
ORDER BY m.round, m.id
");
$matchesStmt->execute([$tournamentId]);
$matches = $matchesStmt->fetchAll();
$rounds = [];
foreach ($matches as $match) {
$rounds[$match['round']][] = $match;
}
?>
prepare("SELECT MAX(round) FROM matches WHERE tournament_id = ?");
$stmt->execute([$tournamentId]);
$currentRound = $stmt->fetchColumn();
$stmt = db()->prepare("SELECT COUNT(*) FROM matches WHERE tournament_id = ? AND round = ? AND winner_id IS NULL");
$stmt->execute([$tournamentId, $currentRound]);
$unfinishedMatches = $stmt->fetchColumn();
if ($unfinishedMatches == 0) {
$stmt = db()->prepare("SELECT COUNT(DISTINCT winner_id) FROM matches WHERE tournament_id = ? AND round = ?");
$stmt->execute([$tournamentId, $currentRound]);
$distinctWinners = $stmt->fetchColumn();
if ($distinctWinners > 1) {
echo '';
} else {
echo 'Winner: ' . $rounds[$currentRound][0]['winner_name'] . '
';
}
}
?>