188 lines
7.2 KiB
PHP
188 lines
7.2 KiB
PHP
<?php
|
|
require_once __DIR__ . '/includes/header.php';
|
|
|
|
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
|
|
header("Location: /tournaments.php");
|
|
exit;
|
|
}
|
|
|
|
$tournamentId = $_GET['id'];
|
|
|
|
// Fetch tournament details
|
|
$stmt = db()->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 = '<div class="alert alert-success">Bracket generated successfully!</div>';
|
|
} else {
|
|
$message = '<div class="alert alert-warning">Bracket has already been generated.</div>';
|
|
}
|
|
}
|
|
|
|
// 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 = '<div class="alert alert-success">Winner updated!</div>';
|
|
}
|
|
|
|
// 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 = '<div class="alert alert-success">Next round generated!</div>';
|
|
} else {
|
|
$message = '<div class="alert alert-info">Tournament finished!</div>';
|
|
}
|
|
}
|
|
|
|
|
|
// 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;
|
|
}
|
|
|
|
?>
|
|
|
|
<h2><?php echo htmlspecialchars($tournament['name']); ?></h2>
|
|
|
|
<?php echo $message; ?>
|
|
|
|
<?php if (count($matches) == 0): ?>
|
|
<form method="POST" class="mb-4">
|
|
<button type="submit" name="generate_bracket" class="btn btn-primary">Generate Bracket</button>
|
|
</form>
|
|
<?php else: ?>
|
|
<?php
|
|
$stmt = db()->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 '<form method="POST" class="mb-4"><button type="submit" name="next_round" class="btn btn-success">Generate Next Round</button></form>';
|
|
} else {
|
|
echo '<div class="alert alert-success"><strong>Winner:</strong> ' . $rounds[$currentRound][0]['winner_name'] . '</div>';
|
|
}
|
|
}
|
|
?>
|
|
<?php endif; ?>
|
|
|
|
|
|
<div class="row">
|
|
<?php foreach ($rounds as $round => $round_matches): ?>
|
|
<div class="col-md-4">
|
|
<div class="card mb-4">
|
|
<div class="card-header">
|
|
<h3>Round <?php echo $round; ?></h3>
|
|
</div>
|
|
<ul class="list-group list-group-flush">
|
|
<?php foreach ($round_matches as $match): ?>
|
|
<li class="list-group-item">
|
|
<form method="POST">
|
|
<input type="hidden" name="match_id" value="<?php echo $match['id']; ?>">
|
|
<strong><?php echo htmlspecialchars($match['team1_name']); ?></strong> vs <strong><?php echo htmlspecialchars($match['team2_name'] ?? 'BYE'); ?></strong>
|
|
<hr>
|
|
<?php if ($match['winner_name']): ?>
|
|
<p>Winner: <strong><?php echo htmlspecialchars($match['winner_name']); ?></strong></p>
|
|
<?php elseif ($match['team2_name']): ?>
|
|
<div class="form-group">
|
|
<label>Set Winner:</label>
|
|
<select name="winner_id" class="form-control">
|
|
<option value="<?php echo $match['team1_id']; ?>"><?php echo htmlspecialchars($match['team1_name']); ?></option>
|
|
<option value="<?php echo $match['team2_id']; ?>"><?php echo htmlspecialchars($match['team2_name']); ?></option>
|
|
</select>
|
|
</div>
|
|
<button type="submit" name="update_winner" class="btn btn-sm btn-primary">Save</button>
|
|
<?php endif; ?>
|
|
</form>
|
|
</li>
|
|
<?php endforeach; ?>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
|
|
|
|
<?php
|
|
require_once __DIR__ . '/includes/footer.php';
|
|
?>
|