Skip to content

Commit 410155a

Browse files
committed
feat: add resume_tournament function with catch-up scheduling
1 parent 7b36650 commit 410155a

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
CREATE OR REPLACE FUNCTION public.resume_tournament(_tournament_id uuid)
2+
RETURNS void
3+
LANGUAGE plpgsql
4+
AS $$
5+
DECLARE
6+
bracket_row tournament_brackets%ROWTYPE;
7+
_tournament_status text;
8+
BEGIN
9+
SELECT status INTO _tournament_status FROM tournaments WHERE id = _tournament_id;
10+
11+
IF _tournament_status != 'Paused' THEN
12+
RAISE EXCEPTION 'Tournament is not paused' USING ERRCODE = '22000';
13+
END IF;
14+
15+
-- Set status back to Live
16+
UPDATE tournaments SET status = 'Live' WHERE id = _tournament_id;
17+
18+
-- Schedule all ready brackets that were blocked during pause
19+
FOR bracket_row IN
20+
SELECT tb.*
21+
FROM tournament_brackets tb
22+
INNER JOIN tournament_stages ts ON ts.id = tb.tournament_stage_id
23+
WHERE ts.tournament_id = _tournament_id
24+
AND tb.match_id IS NULL
25+
AND tb.finished = false
26+
AND tb.tournament_team_id_1 IS NOT NULL
27+
AND tb.tournament_team_id_2 IS NOT NULL
28+
ORDER BY tb.round, tb.match_number
29+
LOOP
30+
PERFORM schedule_tournament_match(bracket_row);
31+
END LOOP;
32+
33+
-- Recalculate ETAs
34+
PERFORM calculate_tournament_bracket_start_times(_tournament_id);
35+
END;
36+
$$;

0 commit comments

Comments
 (0)