File tree Expand file tree Collapse file tree 1 file changed +20
-11
lines changed
hasura/functions/tournaments Expand file tree Collapse file tree 1 file changed +20
-11
lines changed Original file line number Diff line number Diff line change 11CREATE OR REPLACE FUNCTION public .assign_seeds_to_teams(tournament tournaments) RETURNS VOID
22 LANGUAGE plpgsql
33 AS $$
4- DECLARE
5- stage record;
6- max_existing_seed int ;
74BEGIN
8- WITH max_existing_seed AS (
9- SELECT COALESCE( MAX (seed), 0 ) as max_seed
5+ WITH eligible_count AS (
6+ SELECT COUNT ( * ) as total
107 FROM tournament_teams
11- WHERE tournament_id = tournament .id
8+ WHERE tournament_id = tournament .id
129 AND eligible_at IS NOT NULL
1310 ),
11+ taken_seeds AS (
12+ SELECT seed
13+ FROM tournament_teams
14+ WHERE tournament_id = tournament .id
15+ AND eligible_at IS NOT NULL
16+ AND seed IS NOT NULL
17+ ),
18+ available_seeds AS (
19+ SELECT s AS seed_number, ROW_NUMBER() OVER (ORDER BY RANDOM()) as rn
20+ FROM eligible_count ec
21+ CROSS JOIN LATERAL generate_series(1 , ec .total ::int ) s
22+ WHERE s NOT IN (SELECT seed FROM taken_seeds)
23+ ),
1424 teams_to_seed AS (
15- SELECT id,
16- mes .max_seed + ROW_NUMBER() OVER (ORDER BY RANDOM()) as assigned_seed
25+ SELECT id, ROW_NUMBER() OVER (ORDER BY RANDOM()) as rn
1726 FROM tournament_teams
18- CROSS JOIN max_existing_seed mes
19- WHERE tournament_id = tournament .id
27+ WHERE tournament_id = tournament .id
2028 AND eligible_at IS NOT NULL
2129 AND seed IS NULL
2230 )
2331 UPDATE tournament_teams tt
24- SET seed = tts . assigned_seed
32+ SET seed = avs . seed_number
2533 FROM teams_to_seed tts
34+ JOIN available_seeds avs ON avs .rn = tts .rn
2635 WHERE tt .id = tts .id ;
2736END;
2837$$;
You can’t perform that action at this time.
0 commit comments