Skip to content

Commit 87148da

Browse files
committed
bug: fix missing bracket fo lb
1 parent 33c04c0 commit 87148da

3 files changed

Lines changed: 72 additions & 14 deletions

File tree

hasura/functions/tournaments/link_round_group_matches.sql

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,27 @@ BEGIN
4141
END IF;
4242

4343
-- Distribute current round matches to next round matches within the same group
44-
-- For WB: Each pair of matches links to one match (Match 1,2 → Round 2 Match 1; Match 3,4 → Round 2 Match 2)
45-
-- For LB: Each match links 1-to-1 by match number (Match 1 → Round 2 Match 1; Match 2 → Round 2 Match 2)
44+
-- WB pattern: always pair two matches into one next match
45+
-- e.g. Match 1,2 → next Match 1; Match 3,4 → next Match 2
46+
-- LB pattern:
47+
-- - If next round has the same number of matches as current_round (next_count >= current_count),
48+
-- use 1-to-1 mapping by match number (Match i → next Match i).
49+
-- - If next round has fewer matches than current_round (next_count < current_count),
50+
-- pair two matches into one next match, like WB.
4651
FOR i IN 1..current_count LOOP
47-
-- Calculate target index based on path
52+
-- Calculate target index based on path and relative round sizes
4853
IF COALESCE(_path, 'WB') = 'LB' THEN
49-
-- Losers bracket: 1-to-1 mapping by match number
50-
target_idx := i;
54+
-- Losers bracket:
55+
IF next_count >= current_count THEN
56+
-- Same or larger number of matches in next round: 1-to-1 mapping
57+
target_idx := i;
58+
ELSE
59+
-- Fewer matches in next round (e.g. LB Round 2 → LB Round 3 in an 8-team DE):
60+
-- pair two current matches into one next match
61+
target_idx := ((i - 1) / 2) + 1;
62+
END IF;
5163
ELSE
52-
-- Winners bracket: pairing pattern (every 2 matches go to the same parent match)
64+
-- Winners bracket: always pairing pattern (every 2 matches go to the same parent match)
5365
target_idx := ((i - 1) / 2) + 1;
5466
END IF;
5567

hasura/functions/tournaments/update_tournament_stages.sql

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,14 @@ BEGIN
272272
lb_prev_match_ids := lb_match_ids;
273273
END LOOP;
274274

275-
-- Create Grand Final per group only if next stage needs exactly 1 team (to determine champion)
276-
-- Otherwise, skip Grand Final and both WB and LB champions advance directly
277-
-- Grand Final stays in the winners group (group g) for consistency
275+
-- For stages that produce a single champion, create a consolidation final in LB
276+
-- and a Grand Final in WB:
277+
-- - LB extra round (round = wb_round_count + 1, path='LB'):
278+
-- participants = loser of WB final + winner of LB final
279+
-- - WB extra round (round = wb_round_count + 1, path='WB'):
280+
-- participants = winner of WB final + winner of LB extra round
278281
IF wb_round_count > 0 AND next_stage_max_teams = 1 THEN
282+
-- Identify WB and LB finals (last round in each path for this group)
279283
SELECT id INTO wb_final_id
280284
FROM tournament_brackets
281285
WHERE tournament_stage_id = stage.id AND path = 'WB' AND round = wb_round_count AND "group" = g
@@ -287,14 +291,47 @@ BEGIN
287291
ORDER BY match_number ASC LIMIT 1;
288292

289293
IF wb_final_id IS NOT NULL AND lb_final_id IS NOT NULL THEN
294+
-- Create LB consolidation final (extra LB round)
290295
INSERT INTO tournament_brackets (round, tournament_stage_id, match_number, "group", path)
291-
VALUES (wb_round_count + 1, stage.id, 1, g, 'GF')
296+
VALUES (wb_round_count + 1, stage.id, 1, loser_group_num, 'LB')
292297
RETURNING id INTO gf_id;
293298

294-
UPDATE tournament_brackets SET parent_bracket_id = gf_id WHERE id = wb_final_id;
295-
UPDATE tournament_brackets SET parent_bracket_id = gf_id WHERE id = lb_final_id;
296-
297-
RAISE NOTICE ' => Created Grand Final for group % (next_stage needs 1 team)', g;
299+
-- Winner of LB final advances to LB consolidation via parent_bracket_id
300+
UPDATE tournament_brackets
301+
SET parent_bracket_id = gf_id
302+
WHERE id = lb_final_id;
303+
304+
-- Loser of WB final drops into LB consolidation via loser_parent_bracket_id
305+
UPDATE tournament_brackets
306+
SET loser_parent_bracket_id = gf_id
307+
WHERE id = wb_final_id;
308+
309+
RAISE NOTICE ' => Created LB consolidation final for group % (round %, path LB)', g, wb_round_count + 1;
310+
311+
-- Create WB Grand Final as an extra WB round
312+
INSERT INTO tournament_brackets (round, tournament_stage_id, match_number, "group", path)
313+
VALUES (wb_round_count + 1, stage.id, 1, g, 'WB')
314+
RETURNING id INTO gf_id;
315+
316+
-- Winner of WB final advances to Grand Final
317+
UPDATE tournament_brackets
318+
SET parent_bracket_id = gf_id
319+
WHERE id = wb_final_id;
320+
321+
-- Winner of LB consolidation final advances to Grand Final
322+
UPDATE tournament_brackets
323+
SET parent_bracket_id = gf_id
324+
WHERE id = (
325+
SELECT id
326+
FROM tournament_brackets
327+
WHERE tournament_stage_id = stage.id
328+
AND path = 'LB'
329+
AND round = wb_round_count + 1
330+
AND "group" = loser_group_num
331+
ORDER BY match_number ASC LIMIT 1
332+
);
333+
334+
RAISE NOTICE ' => Created WB Grand Final for group % (round %, path WB)', g, wb_round_count + 1;
298335
END IF;
299336
ELSE
300337
RAISE NOTICE ' => Skipping Grand Final for group % (next_stage_max_teams=%, both WB and LB champions advance directly)', g, next_stage_max_teams;

hasura/metadata/databases/default/tables/public_tournament_brackets.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ object_relationships:
1414
- name: match
1515
using:
1616
foreign_key_constraint_on: match_id
17+
- name: parent_bracket
18+
using:
19+
manual_configuration:
20+
column_mapping:
21+
parent_bracket_id: id
22+
insertion_order: null
23+
remote_table:
24+
name: tournament_brackets
25+
schema: public
1726
- name: stage
1827
using:
1928
foreign_key_constraint_on: tournament_stage_id

0 commit comments

Comments
 (0)