Skip to content

Commit fe95a2c

Browse files
committed
bug: fix round robin seeding
1 parent bd3a8e7 commit fe95a2c

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

hasura/functions/tournaments/update_tournament_stages.sql

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,33 @@ BEGIN
8484
total_matches_per_group, round_count, matches_per_round;
8585

8686
-- Generate round robin matches for each group
87+
-- Distribute seeds evenly across groups using round-robin pattern:
88+
-- Group 1: seeds 1, 1+groups, 1+2*groups, ...
89+
-- Group 2: seeds 2, 2+groups, 2+2*groups, ...
90+
-- etc.
8791
FOR g IN 1..stage.groups LOOP
8892
RAISE NOTICE ' => Generating RoundRobin matches for group %', g;
8993

90-
-- Calculate which seeds belong to this group
94+
-- Calculate which seeds belong to this group using round-robin distribution
9195
DECLARE
92-
group_start_seed int;
93-
group_end_seed int;
9496
team_seeds int[];
97+
seed_val int;
98+
round_num int;
9599
BEGIN
96-
group_start_seed := (g - 1) * teams_per_group + 1;
97-
group_end_seed := g * teams_per_group;
98-
99-
-- Build array of seeds for this group
100-
-- Teams are distributed: group 1 gets seeds 1-N, group 2 gets seeds N+1-2N, etc.
101100
team_seeds := ARRAY[]::int[];
102-
FOR i IN group_start_seed..LEAST(group_end_seed, effective_teams) LOOP
103-
team_seeds := team_seeds || i;
101+
round_num := 0;
102+
103+
-- Loop through rounds, assigning seeds: g, g+groups, g+2*groups, ...
104+
LOOP
105+
seed_val := g + round_num * stage.groups;
106+
107+
-- Stop if seed exceeds effective_teams
108+
IF seed_val > effective_teams THEN
109+
EXIT;
110+
END IF;
111+
112+
team_seeds := team_seeds || seed_val;
113+
round_num := round_num + 1;
104114
END LOOP;
105115

106116
IF array_length(team_seeds, 1) < 2 THEN
@@ -109,8 +119,8 @@ BEGIN
109119
CONTINUE;
110120
END IF;
111121

112-
RAISE NOTICE ' => Group %: % teams (seeds % to %)',
113-
g, array_length(team_seeds, 1), group_start_seed, LEAST(group_end_seed, effective_teams);
122+
RAISE NOTICE ' => Group %: % teams (seeds: %)',
123+
g, array_length(team_seeds, 1), team_seeds;
114124

115125
-- Use reusable function to create all round robin matches with seeds
116126
-- Teams will be assigned later in seed_tournament

0 commit comments

Comments
 (0)