105 lines · 3.8 KB
Raw Download
1
<?php
2
require_once __DIR__ . '/src/bracket.php';
3
require_once __DIR__ . '/src/render.php';
4
5
// Every load is a fresh random bracket, so it must never be cached.
6
header('Cache-Control: no-store');
7
8
$data       = bracket_load($_GET['year'] ?? null);
9
$tournament = simulate_tournament($data);
10
$regions    = $tournament['regions'];
11
$rounds     = $data['rounds'];
12
13
/** The date band above one half: five rounds, mirrored on the right. */
14
function header_band(array $columns): string
15
{
16
    $out = "<div class='tournament tournament-header'>\n";
17
    foreach ($columns as $round) {
18
        $out .= "      <ul class='round'>\n" . header_column($round) . "      </ul>\n";
19
    }
20
    return $out . "    </div>\n";
21
}
22
23
/**
24
 * One half of the bracket, from the seeds inward ($side 'left') or from the
25
 * Elite Eight outward ($side 'right').
26
 */
27
function half_bracket(array $top, array $bottom, string $side): string
28
{
29
    $order = $side === 'left' ? [0, 1, 2, 3, 4] : [4, 3, 2, 1, 0];
30
    $out   = "<div class='tournament'>\n";
31
32
    foreach ($order as $n) {
33
        $entries = array_merge($top['rounds'][$n], $bottom['rounds'][$n]);
34
35
        if ($n === 0) {
36
            $out .= "      <ul class='round seed'>\n"
37
                . seed_column($entries, $side) . "      </ul>\n";
38
            continue;
39
        }
40
41
        // The Sweet 16 column labels each region in the spacer between its pair.
42
        $labels = $n === 3 ? [$top['label'], $bottom['label']] : [];
43
        $out .= "      <ul class='round round-$n'>\n"
44
            . round_column($entries, $side, $labels) . "      </ul>\n";
45
    }
46
    return $out . "    </div>\n";
47
}
48
49
$leftHeads  = [$rounds[0], $rounds[1], $rounds[2], $rounds[3], $rounds[4]];
50
$rightHeads = array_reverse($leftHeads);
51
?>
52
<!DOCTYPE html>
53
<html lang="en">
54
<head>
55
	<meta charset="utf-8">
56
	<meta name="viewport" content="width=device-width, initial-scale=1">
57
	<title>Jeff's Random Bracket Generator - <?= e((string) $data['year']) ?></title>
58
	<!-- https://realfavicongenerator.net -->
59
	<link rel="apple-touch-icon" sizes="180x180" href="https://jefftml.com/apple-touch-icon.png">
60
	<link rel="icon" type="image/png" sizes="16x16" href="https://jefftml.com/favicon-16x16.png">
61
	<link rel="mask-icon" href="https://jefftml.com/safari-pinned-tab.svg" color="#272822">
62
	<meta name="theme-color" content="#272822">
63
	<link rel="stylesheet" href="style.css">
64
</head>
65
<body>
66
  <section>
67
    <div class="intro">
68
      <p>Generate a random bracket for the <?= e((string) $data['year']) ?> men's basketball tournament. Refresh to generate a new random bracket. Prints better in landscape. <a href="https://github.com/jefe317/bracket">PHP</a> by <a href="https://toot.io/@jefftml">@jefftml</a>. Layout by <a href="https://codepen.io/kenielsen/pen/aVYJLX">Kristen Nielsen</a>.</p>
69
    </div>
70
    <div class="bracket">
71
      <div class="half">
72
        <?= header_band($leftHeads) ?>
73
        <?= half_bracket($regions['TL'], $regions['BL'], 'left') ?>
74
      </div>
75
      <div class="half center">
76
        <?= header_band([null, $rounds[5], null]) ?>
77
        <div class='tournament'>
78
          <ul class='round semi-final'>
79
<?= single_column($tournament['left'], 'game-left game-top') ?>
80
          </ul>
81
          <ul class='round finals'>
82
<?= single_column($tournament['champion'], 'game final') ?>
83
          </ul>
84
          <ul class='round semi-final'>
85
<?= single_column($tournament['right'], 'game-right game-top') ?>
86
          </ul>
87
        </div>
88
      </div>
89
      <div class="half">
90
        <?= header_band($rightHeads) ?>
91
        <?= half_bracket($regions['TR'], $regions['BR'], 'right') ?>
92
      </div>
93
    </div>
94
95
    <div class="mobile-views">
96
<?= champion_card($tournament) ?>
97
<?php foreach ($regions as $region) {
98
    echo region_panel($region);
99
} ?>
100
<?= final_four_panel($tournament, $rounds) ?>
101
    </div>
102
  </section>
103
</body>
104
</html>
105