|
| 1 | +CREATE OR REPLACE FUNCTION public.assign_teams_to_swiss_pools(_stage_id uuid, _round int) |
| 2 | +RETURNS void |
| 3 | +LANGUAGE plpgsql |
| 4 | +AS $$ |
| 5 | +DECLARE |
| 6 | + pool_record RECORD; |
| 7 | + bracket_record RECORD; |
| 8 | + team_count int; |
| 9 | + matches_needed int; |
| 10 | + match_counter int; |
| 11 | + bracket_order int[]; |
| 12 | + i int; |
| 13 | + seed_1_idx int; |
| 14 | + seed_2_idx int; |
| 15 | + team_1_id uuid; |
| 16 | + team_2_id uuid; |
| 17 | + adjacent_team_id uuid; |
| 18 | + used_teams uuid[]; |
| 19 | + teams_to_pair uuid[]; |
| 20 | +BEGIN |
| 21 | + RAISE NOTICE '=== Assigning Teams to Swiss Pools for Round % ===', _round; |
| 22 | + |
| 23 | + used_teams := ARRAY[]::uuid[]; |
| 24 | + |
| 25 | + FOR pool_record IN |
| 26 | + SELECT * FROM get_swiss_team_pools(_stage_id, used_teams) |
| 27 | + ORDER BY wins DESC, losses ASC |
| 28 | + LOOP |
| 29 | + team_count := pool_record.team_count; |
| 30 | + |
| 31 | + IF team_count = 0 THEN |
| 32 | + CONTINUE; |
| 33 | + END IF; |
| 34 | + |
| 35 | + -- Calculate pool group: wins * 100 + losses |
| 36 | + DECLARE |
| 37 | + pool_group numeric; |
| 38 | + BEGIN |
| 39 | + pool_group := pool_record.wins * 100 + pool_record.losses; |
| 40 | + |
| 41 | + RAISE NOTICE ' Pool %-% (group %): % teams', |
| 42 | + pool_record.wins, pool_record.losses, pool_group, team_count; |
| 43 | + |
| 44 | + -- Handle odd number of teams |
| 45 | + adjacent_team_id := NULL; |
| 46 | + teams_to_pair := pool_record.team_ids; |
| 47 | + |
| 48 | + IF team_count % 2 != 0 THEN |
| 49 | + -- Find a team from an adjacent pool |
| 50 | + adjacent_team_id := find_adjacent_swiss_team(_stage_id, pool_record.wins, pool_record.losses, used_teams); |
| 51 | + |
| 52 | + IF adjacent_team_id IS NOT NULL THEN |
| 53 | + teams_to_pair := teams_to_pair || adjacent_team_id; |
| 54 | + used_teams := used_teams || adjacent_team_id; |
| 55 | + RAISE NOTICE ' Borrowed team % from adjacent pool', adjacent_team_id; |
| 56 | + ELSE |
| 57 | + RAISE EXCEPTION 'Odd number of teams in pool %-% and no adjacent team found', |
| 58 | + pool_record.wins, pool_record.losses; |
| 59 | + END IF; |
| 60 | + END IF; |
| 61 | + |
| 62 | + matches_needed := array_length(teams_to_pair, 1) / 2; |
| 63 | + |
| 64 | + -- For Swiss tournaments, use bracket order for pairing |
| 65 | + -- Filter bracket_order to only include valid seed positions (1 to teams_to_pair.length) |
| 66 | + bracket_order := generate_bracket_order(array_length(teams_to_pair, 1)); |
| 67 | + DECLARE |
| 68 | + filtered_order int[]; |
| 69 | + valid_seed int; |
| 70 | + BEGIN |
| 71 | + filtered_order := ARRAY[]::int[]; |
| 72 | + FOREACH valid_seed IN ARRAY bracket_order LOOP |
| 73 | + IF valid_seed >= 1 AND valid_seed <= array_length(teams_to_pair, 1) THEN |
| 74 | + filtered_order := filtered_order || valid_seed; |
| 75 | + END IF; |
| 76 | + END LOOP; |
| 77 | + bracket_order := filtered_order; |
| 78 | + END; |
| 79 | + |
| 80 | + -- Validate we have enough valid seed positions |
| 81 | + IF array_length(bracket_order, 1) < matches_needed * 2 THEN |
| 82 | + RAISE EXCEPTION 'Not enough valid seed positions in bracket order for pool %-% (needed: %, got: %)', |
| 83 | + pool_record.wins, pool_record.losses, matches_needed * 2, array_length(bracket_order, 1); |
| 84 | + END IF; |
| 85 | + |
| 86 | + match_counter := 1; |
| 87 | + FOR i IN 1..matches_needed LOOP |
| 88 | + -- Get seed positions from filtered bracket order |
| 89 | + seed_1_idx := bracket_order[(i - 1) * 2 + 1]; |
| 90 | + seed_2_idx := bracket_order[(i - 1) * 2 + 2]; |
| 91 | + |
| 92 | + team_1_id := teams_to_pair[seed_1_idx]; |
| 93 | + team_2_id := teams_to_pair[seed_2_idx]; |
| 94 | + |
| 95 | + -- Validate that teams are not NULL |
| 96 | + IF team_1_id IS NULL OR team_2_id IS NULL THEN |
| 97 | + RAISE EXCEPTION 'NULL team found in pool %-% at match % (seed_1_idx: %, seed_2_idx: %, teams_to_pair length: %)', |
| 98 | + pool_record.wins, pool_record.losses, match_counter, seed_1_idx, seed_2_idx, array_length(teams_to_pair, 1); |
| 99 | + END IF; |
| 100 | + |
| 101 | + SELECT id INTO bracket_record |
| 102 | + FROM tournament_brackets |
| 103 | + WHERE tournament_stage_id = _stage_id |
| 104 | + AND round = _round |
| 105 | + AND "group" = pool_group |
| 106 | + AND match_number = match_counter |
| 107 | + LIMIT 1; |
| 108 | + |
| 109 | + IF bracket_record IS NULL THEN |
| 110 | + RAISE EXCEPTION 'Bracket record not found for match % in pool %-% (group %)', |
| 111 | + match_counter, pool_record.wins, pool_record.losses, pool_group; |
| 112 | + END IF; |
| 113 | + |
| 114 | + UPDATE tournament_brackets |
| 115 | + SET tournament_team_id_1 = team_1_id, |
| 116 | + tournament_team_id_2 = team_2_id, |
| 117 | + bye = false |
| 118 | + WHERE id = bracket_record.id; |
| 119 | + |
| 120 | + -- Mark both teams as used to prevent double-assignment |
| 121 | + used_teams := used_teams || team_1_id || team_2_id; |
| 122 | + |
| 123 | + RAISE NOTICE ' Match %: Team % vs Team %', match_counter, team_1_id, team_2_id; |
| 124 | + match_counter := match_counter + 1; |
| 125 | + END LOOP; |
| 126 | + END; |
| 127 | + END LOOP; |
| 128 | + |
| 129 | + RAISE NOTICE '=== Team Assignment Complete ==='; |
| 130 | +END; |
| 131 | +$$; |
| 132 | + |
0 commit comments