Skip to content

Commit 48f6d41

Browse files
committed
bug: fix ordering
1 parent fcdc605 commit 48f6d41

1 file changed

Lines changed: 66 additions & 21 deletions

File tree

hasura/functions/tournaments/link_stage_brackets.sql

Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ DECLARE
1111
current_count int;
1212
i int;
1313
target_idx int;
14+
wb_match_ids uuid[];
15+
lb_match_ids uuid[];
16+
gf_match_ids uuid[];
17+
interleaved_ids uuid[];
18+
wb_count int;
19+
lb_count int;
20+
gf_count int;
21+
max_matches int;
22+
j int;
23+
match_info text;
1424
BEGIN
1525
-- Collect next stage round-1 matches in order
1626
SELECT array_agg(tb.id ORDER BY tb.match_number ASC)
@@ -27,43 +37,78 @@ BEGIN
2737
-- Collect all top-round matches from the current stage across all groups
2838
-- For double elimination, we want to link final matches (highest round per path)
2939
-- This ensures we only link WB final, LB final, and GF (if exists), not all matches from max round
40+
-- Collect WB and LB matches separately so we can interleave them
41+
-- Collect matches by path
3042
SELECT array_agg(tb.id ORDER BY tb."group" ASC, tb.match_number ASC)
31-
INTO current_top_ids
43+
INTO wb_match_ids
3244
FROM tournament_brackets tb
3345
WHERE tb.tournament_stage_id = current_stage_id
3446
AND tb.round = top_round
47+
AND COALESCE(tb.path, 'WB') = 'WB'
3548
AND (
36-
-- Link if it's the highest round for this path, or if path is NULL (single elim)
3749
tb.path IS NULL
3850
OR tb.round = (
3951
SELECT MAX(tb2.round)
4052
FROM tournament_brackets tb2
4153
WHERE tb2.tournament_stage_id = current_stage_id
42-
AND COALESCE(tb2.path, 'WB') = COALESCE(tb.path, 'WB')
54+
AND COALESCE(tb2.path, 'WB') = 'WB'
4355
AND tb2."group" = tb."group"
4456
)
4557
);
4658

59+
SELECT array_agg(tb.id ORDER BY tb."group" ASC, tb.match_number ASC)
60+
INTO lb_match_ids
61+
FROM tournament_brackets tb
62+
WHERE tb.tournament_stage_id = current_stage_id
63+
AND tb.round = top_round
64+
AND tb.path = 'LB'
65+
AND tb.round = (
66+
SELECT MAX(tb2.round)
67+
FROM tournament_brackets tb2
68+
WHERE tb2.tournament_stage_id = current_stage_id
69+
AND tb2.path = 'LB'
70+
AND tb2."group" = tb."group"
71+
);
72+
73+
SELECT array_agg(tb.id ORDER BY tb."group" ASC, tb.match_number ASC)
74+
INTO gf_match_ids
75+
FROM tournament_brackets tb
76+
WHERE tb.tournament_stage_id = current_stage_id
77+
AND tb.round = top_round
78+
AND tb.path = 'GF';
79+
80+
wb_count := COALESCE(array_length(wb_match_ids, 1), 0);
81+
lb_count := COALESCE(array_length(lb_match_ids, 1), 0);
82+
gf_count := COALESCE(array_length(gf_match_ids, 1), 0);
83+
max_matches := GREATEST(wb_count, lb_count);
84+
85+
-- Interleave WB and LB matches: WB1, LB1, WB2, LB2, etc.
86+
interleaved_ids := ARRAY[]::uuid[];
87+
FOR j IN 1..max_matches LOOP
88+
IF j <= wb_count THEN
89+
interleaved_ids := interleaved_ids || wb_match_ids[j];
90+
END IF;
91+
IF j <= lb_count THEN
92+
interleaved_ids := interleaved_ids || lb_match_ids[j];
93+
END IF;
94+
END LOOP;
95+
96+
-- Add GF matches at the end
97+
IF gf_count > 0 THEN
98+
interleaved_ids := interleaved_ids || gf_match_ids;
99+
END IF;
100+
101+
current_top_ids := interleaved_ids;
47102
current_count := COALESCE(array_length(current_top_ids, 1), 0);
48103

49-
-- Debug: show which matches are being linked and all matches at top_round
50-
DECLARE
51-
match_info text;
52-
all_matches_info text;
53-
BEGIN
54-
SELECT string_agg(tb.path || ' R' || tb.round || ' M' || tb.match_number, ', ' ORDER BY tb."group", tb.match_number)
55-
INTO match_info
56-
FROM tournament_brackets tb
57-
WHERE tb.id = ANY(current_top_ids);
58-
59-
SELECT string_agg(tb.path || ' R' || tb.round || ' M' || tb.match_number, ', ' ORDER BY tb."group", tb.match_number)
60-
INTO all_matches_info
61-
FROM tournament_brackets tb
62-
WHERE tb.tournament_stage_id = current_stage_id AND tb.round = top_round;
63-
64-
RAISE NOTICE 'Linking stage: top_round=%, current_count=% matches being linked (%), all matches at round %: (%)',
65-
top_round, current_count, match_info, top_round, all_matches_info;
66-
END;
104+
-- Debug: show which matches are being linked
105+
SELECT string_agg(tb.path || ' R' || tb.round || ' M' || tb.match_number, ', ' ORDER BY array_position(current_top_ids, tb.id))
106+
INTO match_info
107+
FROM tournament_brackets tb
108+
WHERE tb.id = ANY(current_top_ids);
109+
110+
RAISE NOTICE 'Linking stage: top_round=%, current_count=% matches being linked (%)',
111+
top_round, current_count, match_info;
67112

68113
-- Distribute all current top-round matches evenly across next stage round-1 matches
69114
FOR i IN 1..current_count LOOP

0 commit comments

Comments
 (0)