1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
import { useEffect, useState } from "react";
import { createApiClient } from "../api/client";
import type { components } from "../api/schema";
import BorderedContainer from "../components/BorderedContainer";
import UserIcon from "../components/UserIcon";
import { APP_NAME } from "../config";
import { usePageTitle } from "../hooks/usePageTitle";
type Tournament = components["schemas"]["Tournament"];
type TournamentMatch = components["schemas"]["TournamentMatch"];
type TournamentEntry = components["schemas"]["TournamentEntry"];
function getBorderColor(match: TournamentMatch, userID?: number): string {
if (!match.winner_user_id) {
return "border-black";
}
if (userID !== undefined && match.winner_user_id === userID) {
return "border-brand-600";
}
return "border-gray-400";
}
function getBgColor(match: TournamentMatch, userID?: number): string {
if (!match.winner_user_id) {
return "bg-black";
}
if (userID !== undefined && match.winner_user_id === userID) {
return "bg-brand-600";
}
return "bg-gray-400";
}
function getWinnerBgColor(match: TournamentMatch | undefined): string {
if (!match?.winner_user_id) {
return "bg-black";
}
return "bg-brand-600";
}
function PlayerCard({ entry }: { entry: TournamentEntry | undefined }) {
if (!entry) {
return (
<div className="flex flex-col items-center gap-1 p-2 opacity-30">
<span className="text-gray-400 text-sm">BYE</span>
</div>
);
}
return (
<BorderedContainer>
<div className="flex flex-col items-center gap-1">
<span className="font-medium text-sm truncate max-w-full">
{entry.user.display_name}
</span>
{entry.user.icon_path && (
<UserIcon
iconPath={entry.user.icon_path}
displayName={entry.user.display_name}
className="w-12 h-12"
/>
)}
</div>
</BorderedContainer>
);
}
function Connector({
position,
colSpan,
match,
isTopRound,
leftChildMatch,
rightChildMatch,
}: {
position: number;
colSpan: number;
match: TournamentMatch | undefined;
isTopRound: boolean;
leftChildMatch?: TournamentMatch;
rightChildMatch?: TournamentMatch;
}) {
const leftHalf = colSpan / 2;
const rightHalf = colSpan - leftHalf;
const leftBorderColor = match
? getBorderColor(match, match.player1?.user_id)
: "border-black";
const rightBorderColor = match
? getBorderColor(match, match.player2?.user_id)
: "border-black";
// Leg colors: use child match winner color so vertical lines don't change color midway
const leftLegColor = leftChildMatch
? getWinnerBgColor(leftChildMatch)
: match
? getBgColor(match, match.player1?.user_id)
: "bg-black";
const rightLegColor = rightChildMatch
? getWinnerBgColor(rightChildMatch)
: match
? getBgColor(match, match.player2?.user_id)
: "bg-black";
const winnerBg = getWinnerBgColor(match);
return (
<div
style={{
gridColumn: `${position * colSpan + 1} / span ${colSpan}`,
}}
>
{/* Center stem going UP to parent round */}
<div className={`flex justify-center ${isTopRound ? "h-24" : "h-12"}`}>
<div className={`w-1 ${winnerBg}`} />
</div>
{/* Horizontal line */}
<div className="flex justify-center">
<div className={`w-1/4 border-t-4 ${leftBorderColor}`} />
<div className={`w-1/4 border-t-4 ${rightBorderColor}`} />
</div>
{/* Two legs going DOWN to child elements */}
<div
className="grid h-12"
style={{
gridTemplateColumns: `repeat(${colSpan}, 1fr)`,
}}
>
<div
className="flex justify-center"
style={{ gridColumn: `1 / span ${leftHalf}` }}
>
<div className={`w-1 ${leftLegColor}`} />
</div>
<div
className="flex justify-center"
style={{ gridColumn: `${leftHalf + 1} / span ${rightHalf}` }}
>
<div className={`w-1 ${rightLegColor}`} />
</div>
</div>
</div>
);
}
function TournamentBracket({ tournament }: { tournament: Tournament }) {
const { bracket_size, num_rounds, entries, matches } = tournament;
const matchByKey = new Map<string, TournamentMatch>();
for (const m of matches) {
matchByKey.set(`${m.round}-${m.position}`, m);
}
const entryBySeed = new Map<number, TournamentEntry>();
for (const e of entries) {
entryBySeed.set(e.seed, e);
}
const bracketSeeds = standardBracketSeeds(bracket_size);
// Build rows top-to-bottom: final → ... → round 0 → players
const rows: React.ReactNode[] = [];
// Rounds from top (final) to bottom (round 0)
for (let round = num_rounds - 1; round >= 0; round--) {
const numPositions = bracket_size / (1 << (round + 1));
const colSpan = bracket_size / numPositions;
// Connectors for this round
const connectors: React.ReactNode[] = [];
for (let pos = 0; pos < numPositions; pos++) {
const match = matchByKey.get(`${round}-${pos}`);
const leftChildMatch =
round > 0 ? matchByKey.get(`${round - 1}-${pos * 2}`) : undefined;
const rightChildMatch =
round > 0 ? matchByKey.get(`${round - 1}-${pos * 2 + 1}`) : undefined;
connectors.push(
<Connector
key={`conn-${round}-${pos}`}
position={pos}
colSpan={colSpan}
match={match}
isTopRound={round === num_rounds - 1}
leftChildMatch={leftChildMatch}
rightChildMatch={rightChildMatch}
/>,
);
}
rows.push(
<div
key={`conn-row-${round}`}
className="grid"
style={{
gridTemplateColumns: `repeat(${bracket_size}, 1fr)`,
}}
>
{connectors}
</div>,
);
}
// Player stems row (vertical lines from round-0 connectors to player cards)
const playerStems: React.ReactNode[] = [];
for (let slot = 0; slot < bracket_size; slot++) {
const matchPos = Math.floor(slot / 2);
const match = matchByKey.get(`0-${matchPos}`);
const isPlayer1 = slot % 2 === 0;
const stemColor = match
? getBgColor(
match,
isPlayer1 ? match.player1?.user_id : match.player2?.user_id,
)
: "bg-black";
playerStems.push(
<div
key={`stem-${slot}`}
className="flex justify-center h-12"
style={{ gridColumn: `${slot + 1} / span 1` }}
>
<div className={`w-1 ${stemColor}`} />
</div>,
);
}
rows.push(
<div
key="player-stems"
className="grid"
style={{ gridTemplateColumns: `repeat(${bracket_size}, 1fr)` }}
>
{playerStems}
</div>,
);
// Player cards row (bottom)
const playerCards: React.ReactNode[] = [];
for (let slot = 0; slot < bracket_size; slot++) {
const seed = bracketSeeds[slot]!;
const entry = entryBySeed.get(seed);
playerCards.push(
<div
key={`player-${slot}`}
style={{ gridColumn: `${slot + 1} / span 1` }}
>
<PlayerCard entry={entry} />
</div>,
);
}
rows.push(
<div
key="players"
className="grid gap-1"
style={{ gridTemplateColumns: `repeat(${bracket_size}, 1fr)` }}
>
{playerCards}
</div>,
);
return <div className="flex flex-col gap-0">{rows}</div>;
}
// Exported for testing as standardBracketSeedsForTest
export { standardBracketSeeds as standardBracketSeedsForTest };
function standardBracketSeeds(bracketSize: number): number[] {
const seeds = new Array<number>(bracketSize).fill(0);
seeds[0] = 1;
for (let size = 2; size <= bracketSize; size *= 2) {
const temp = new Array<number>(size).fill(0);
for (let i = 0; i < size / 2; i++) {
temp[i * 2] = seeds[i]!;
temp[i * 2 + 1] = size + 1 - seeds[i]!;
}
for (let i = 0; i < size; i++) {
seeds[i] = temp[i]!;
}
}
return seeds;
}
export default function TournamentPage({
tournamentId,
}: {
tournamentId: string;
}) {
usePageTitle(`Tournament | ${APP_NAME}`);
const id = Number(tournamentId);
const isValidId = id > 0;
const [tournament, setTournament] = useState<Tournament | null>(null);
const [loading, setLoading] = useState(isValidId);
const [error, setError] = useState<string | null>(
isValidId ? null : "Invalid tournament ID",
);
useEffect(() => {
if (!isValidId) {
return;
}
const apiClient = createApiClient();
apiClient
.getTournament(id)
.then(({ tournament }) => setTournament(tournament))
.catch(() => setError("Failed to load tournament"))
.finally(() => setLoading(false));
}, [id, isValidId]);
if (loading) {
return (
<div className="min-h-screen bg-gray-100 flex items-center justify-center">
<p className="text-gray-500">Loading...</p>
</div>
);
}
if (error || !tournament) {
return (
<div className="min-h-screen bg-gray-100 flex items-center justify-center">
<p className="text-red-500">{error || "Failed to load tournament"}</p>
</div>
);
}
return (
<div className="p-6 bg-gray-100 min-h-screen">
<div className="max-w-6xl mx-auto">
<h1 className="text-3xl font-bold text-transparent bg-clip-text bg-brand-600 text-center mb-8">
{tournament.display_name}
</h1>
<TournamentBracket tournament={tournament} />
</div>
</div>
);
}
|