Skip to content

Commit 7b6e016

Browse files
committed
wip
1 parent 5741ba5 commit 7b6e016

1 file changed

Lines changed: 20 additions & 20 deletions

File tree

hasura/functions/tournaments/generate_swiss_bracket.sql

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ LANGUAGE plpgsql
44
AS $$
55
DECLARE
66
max_rounds int;
7+
wins_needed int; -- Number of wins needed to advance (Valve-style: 3)
78
round_num int;
89
wins int;
910
losses int;
@@ -15,12 +16,11 @@ DECLARE
1516
seed_2 int;
1617
bracket_idx int;
1718
BEGIN
18-
-- Calculate maximum rounds needed
19-
-- In Swiss, teams play until they reach 3 wins or 3 losses
20-
-- Maximum rounds would be if teams alternate wins/losses: 6 rounds (0-0 -> 1-0 -> 1-1 -> 2-1 -> 2-2 -> 3-2 or 2-3)
21-
-- But we'll generate up to 6 rounds to be safe
22-
-- TODO - this is bad
23-
max_rounds := 6;
19+
-- Valve-style Swiss system: teams need 3 wins to advance or 3 losses to be eliminated
20+
-- Max rounds formula: 2 × wins_needed - 1
21+
-- This ensures all teams will either advance or be eliminated
22+
wins_needed := 3;
23+
max_rounds := 2 * wins_needed - 1; -- For 3 wins: 2 × 3 - 1 = 5 rounds
2424

2525
RAISE NOTICE '=== Generating Swiss Bracket for % teams ===', _team_count;
2626
RAISE NOTICE 'Will generate rounds 1 through %', max_rounds;
@@ -80,7 +80,7 @@ BEGIN
8080
bracket_idx := bracket_idx + 1;
8181
END LOOP;
8282

83-
-- Generate subsequent rounds (2-6)
83+
-- Generate subsequent rounds
8484
-- For each round, create pools for all possible W/L combinations
8585
RAISE NOTICE 'Starting generation of rounds 2 through %', max_rounds;
8686
RAISE NOTICE 'About to enter loop for rounds 2 to %', max_rounds;
@@ -91,30 +91,30 @@ BEGIN
9191
RAISE NOTICE '=== Round %: Generating pools ===', round_num;
9292

9393
-- Generate all possible W/L combinations for this round
94-
-- Teams can have 0-3 wins and 0-3 losses, but total wins+losses = round_num - 1
94+
-- Teams can have 0 to wins_needed wins and 0 to wins_needed losses, but total wins+losses = round_num - 1
9595
DECLARE
9696
pools_created int := 0;
9797
matches_created int := 0;
9898
BEGIN
99-
FOR wins IN 0..LEAST(3, round_num - 1) LOOP
99+
FOR wins IN 0..LEAST(wins_needed, round_num - 1) LOOP
100100
losses := (round_num - 1) - wins;
101101

102-
-- Skip if losses > 3 (team would be eliminated)
103-
IF losses > 3 THEN
104-
RAISE NOTICE ' Skipping pool %-% (losses > 3)', wins, losses;
102+
-- Skip if losses > wins_needed (team would be eliminated)
103+
IF losses > wins_needed THEN
104+
RAISE NOTICE ' Skipping pool %-% (losses > %)', wins, losses, wins_needed;
105105
CONTINUE;
106106
END IF;
107107

108-
-- Skip pools where teams would have advanced (3 wins, < 3 losses)
108+
-- Skip pools where teams would have advanced (wins_needed wins, < wins_needed losses)
109109
-- These teams won't play more matches
110-
IF wins = 3 AND losses < 3 THEN
110+
IF wins = wins_needed AND losses < wins_needed THEN
111111
RAISE NOTICE ' Skipping pool %-% (advanced)', wins, losses;
112112
CONTINUE;
113113
END IF;
114114

115-
-- Skip pools where teams would be eliminated (3 losses)
115+
-- Skip pools where teams would be eliminated (wins_needed losses)
116116
-- These teams won't play more matches
117-
IF losses = 3 THEN
117+
IF losses = wins_needed THEN
118118
RAISE NOTICE ' Skipping pool %-% (eliminated)', wins, losses;
119119
CONTINUE;
120120
END IF;
@@ -175,11 +175,11 @@ BEGIN
175175
-- Expected teams = team_count * C(n, k) / 2^n
176176
expected_teams_in_pool := _team_count::numeric * binomial_coefficient / POWER(2, n);
177177

178-
-- Adjust for teams that may have advanced (3 wins) or been eliminated (3 losses) in previous rounds
179-
-- After round 3, teams with 3-0 advance, teams with 0-3 are eliminated
180-
-- After round 4, teams with 3-1 advance, teams with 1-3 are eliminated
178+
-- Adjust for teams that may have advanced (wins_needed wins) or been eliminated (wins_needed losses) in previous rounds
179+
-- After round wins_needed, teams with wins_needed-0 advance, teams with 0-wins_needed are eliminated
180+
-- After round wins_needed+1, teams with wins_needed-1 advance, teams with 1-wins_needed are eliminated
181181
-- So we need to reduce expected teams for later rounds
182-
IF round_num >= 4 THEN
182+
IF round_num >= wins_needed + 1 THEN
183183
-- Rough estimate: by round 4, ~25% of teams may have advanced/eliminated
184184
-- Adjust based on round
185185
DECLARE

0 commit comments

Comments
 (0)