Skip to content

Commit 9bde187

Browse files
committed
wip
1 parent 87af306 commit 9bde187

1 file changed

Lines changed: 37 additions & 49 deletions

File tree

hasura/functions/tournaments/update_tournament_stages.sql

Lines changed: 37 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ DECLARE
1818
seed_1 int;
1919
seed_2 int;
2020
stage_target_size int;
21-
match_counter int;
22-
unassigned_seeds int[];
2321
BEGIN
2422
-- Get tournament status for logging
2523
SELECT status INTO tournament_status
@@ -64,6 +62,7 @@ BEGIN
6462
total_matches_per_group int;
6563
matches_per_round int;
6664
round_count int;
65+
match_counter int;
6766
i int;
6867
j int;
6968
round_num int;
@@ -208,8 +207,6 @@ BEGIN
208207
RAISE NOTICE 'Generated bracket order for stage % with % teams: % (array_length: %)',
209208
stage."order", effective_teams, bracket_order, array_length(bracket_order, 1);
210209

211-
unassigned_seeds := bracket_order;
212-
213210
FOR round_num IN 1..total_rounds LOOP
214211
-- Calculate total matches needed for this round (each match needs 2 teams)
215212
IF round_num = 1 THEN
@@ -221,8 +218,12 @@ BEGIN
221218

222219
RAISE NOTICE ' => Process round %: teams_left_to_assign=%, total_matches_in_round=%', round_num, teams_left_to_assign, matches_in_round;
223220

224-
bracket_idx := 0;
225-
match_counter := 0;
221+
-- Reset bracket index for first round to track seed positions
222+
IF round_num = 1 THEN
223+
bracket_idx := 0;
224+
RAISE NOTICE ' => Round 1: Reset bracket_idx to 0, bracket_order length: %', array_length(bracket_order, 1);
225+
END IF;
226+
226227
-- Create matches alternating between groups
227228
FOR match_idx IN 1..matches_in_round LOOP
228229

@@ -237,57 +238,44 @@ BEGIN
237238
BEGIN
238239
group_num := ((match_idx - 1) % stage.groups) + 1;
239240

240-
raise notice 'round %: unassigned_seeds: %, bracket_idx: %', round_num, unassigned_seeds, bracket_idx;
241-
242-
-- Get seed positions from bracket order (1-based array indexing)
243-
raise notice 'seed 1 index: %', bracket_idx * 2 + 1;
244-
raise notice 'seed 2 index: %', bracket_idx * 2 + 2;
245-
246-
IF unassigned_seeds IS NOT NULL AND bracket_idx * 2 + 1 <= array_length(unassigned_seeds, 1) THEN
247-
seed_1 := unassigned_seeds[bracket_idx * 2 + 1];
248-
ELSE
249-
seed_1 := NULL;
250-
END IF;
251-
252-
IF unassigned_seeds IS NOT NULL AND bracket_idx * 2 + 2 <= array_length(unassigned_seeds, 1) THEN
253-
seed_2 := unassigned_seeds[bracket_idx * 2 + 2];
254-
ELSE
255-
seed_2 := NULL;
256-
END IF;
257-
258-
-- Set to NULL if seed position is beyond effective_teams (for byes)
259-
IF seed_1 IS NOT NULL AND seed_1 > effective_teams THEN
260-
seed_1 := NULL;
261-
END IF;
262-
IF seed_2 IS NOT NULL AND seed_2 > effective_teams THEN
263-
seed_2 := NULL;
264-
END IF;
265-
266241
-- For first round: set seed positions based on bracket order
267242
IF round_num = 1 THEN
268-
IF seed_1 IS NOT NULL AND seed_2 IS NOT NULL THEN
269-
match_counter := match_counter + 1;
270-
INSERT INTO tournament_brackets (round, tournament_stage_id, match_number, "group", team_1_seed, team_2_seed, path)
271-
VALUES (round_num, stage.id, match_counter, group_num, seed_1, seed_2, 'WB')
272-
RETURNING id INTO new_id;
273-
274-
RAISE NOTICE ' => Created round % group % match %: id=%, seeds: % vs % (effective_teams: %, bracket_idx: %)',
275-
round_num, group_num, match_idx, new_id, seed_1, seed_2, effective_teams, bracket_idx;
243+
-- Get seed positions from bracket order (1-based array indexing)
244+
IF bracket_order IS NOT NULL AND bracket_idx * 2 + 1 <= array_length(bracket_order, 1) THEN
245+
seed_1 := bracket_order[bracket_idx * 2 + 1];
246+
ELSE
247+
seed_1 := NULL;
248+
END IF;
249+
250+
IF bracket_order IS NOT NULL AND bracket_idx * 2 + 2 <= array_length(bracket_order, 1) THEN
251+
seed_2 := bracket_order[bracket_idx * 2 + 2];
252+
ELSE
253+
seed_2 := NULL;
254+
END IF;
255+
256+
-- Set to NULL if seed position is beyond effective_teams (for byes)
257+
IF seed_1 IS NOT NULL AND seed_1 > effective_teams THEN
258+
seed_1 := NULL;
259+
END IF;
260+
IF seed_2 IS NOT NULL AND seed_2 > effective_teams THEN
261+
seed_2 := NULL;
276262
END IF;
263+
264+
INSERT INTO tournament_brackets (round, tournament_stage_id, match_number, "group", team_1_seed, team_2_seed, path)
265+
VALUES (round_num, stage.id, match_idx, group_num, seed_1, seed_2, 'WB')
266+
RETURNING id INTO new_id;
267+
268+
RAISE NOTICE ' => Created round % group % match %: id=%, seeds: % vs % (effective_teams: %, bracket_idx: %)',
269+
round_num, group_num, match_idx, new_id, seed_1, seed_2, effective_teams, bracket_idx;
270+
271+
bracket_idx := bracket_idx + 1;
277272
ELSE
278273
-- For other rounds: no seed positions yet (will be set when teams advance)
279-
INSERT INTO tournament_brackets (round, tournament_stage_id, match_number, "group", path, team_1_seed, team_2_seed)
280-
VALUES (round_num, stage.id, match_idx, group_num, 'WB', seed_1, seed_2)
274+
INSERT INTO tournament_brackets (round, tournament_stage_id, match_number, "group", path)
275+
VALUES (round_num, stage.id, match_idx, group_num, 'WB')
281276
RETURNING id INTO new_id;
282277
RAISE NOTICE ' => Created round % group % match %: id=%', round_num, group_num, match_idx, new_id;
283278
END IF;
284-
285-
if(seed_1 IS NOT NULL AND seed_1 IS NOT NULL) then
286-
unassigned_seeds := array_remove(unassigned_seeds, seed_1);
287-
unassigned_seeds := array_remove(unassigned_seeds, seed_2);
288-
end if;
289-
290-
bracket_idx := bracket_idx + 1;
291279
END;
292280
teams_left_to_assign := teams_left_to_assign - 2;
293281
END LOOP;

0 commit comments

Comments
 (0)