@@ -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;
0 commit comments