File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 4545
4646DROP TRIGGER IF EXISTS tbd_tournament_team ON public .tournament_teams ;
4747CREATE TRIGGER tbd_tournament_team BEFORE DELETE ON public .tournament_teams FOR EACH ROW EXECUTE FUNCTION public .tbd_tournament_team ();
48+
49+ CREATE OR REPLACE FUNCTION public .tai_tournament_team()
50+ RETURNS TRIGGER
51+ LANGUAGE plpgsql
52+ AS $$
53+ DECLARE
54+ max_players_per_lineup INT ;
55+ player_steam_id BIGINT ;
56+ BEGIN
57+ IF NEW .team_id IS NOT NULL THEN
58+
59+ SELECT tournament_max_players_per_lineup(t)
60+ INTO max_players_per_lineup
61+ FROM tournaments t
62+ WHERE t .id = NEW .tournament_id ;
63+
64+ FOR player_steam_id IN
65+ SELECT tr .player_steam_id
66+ FROM team_roster tr
67+ LEFT JOIN tournament_team_roster ttr
68+ ON ttr .player_steam_id = tr .player_steam_id
69+ AND ttr .tournament_id = NEW .tournament_id
70+ WHERE tr .team_id = NEW .team_id
71+ AND ttr .player_steam_id IS NULL
72+ ORDER BY
73+ CASE tr .status
74+ WHEN ' Starter' THEN 1
75+ WHEN ' Substitute' THEN 2
76+ WHEN ' Benched' THEN 3
77+ ELSE 4
78+ END
79+ LIMIT max_players_per_lineup
80+ LOOP
81+ INSERT INTO tournament_team_roster (
82+ tournament_team_id,
83+ player_steam_id,
84+ tournament_id
85+ )
86+ VALUES (
87+ NEW .id ,
88+ player_steam_id,
89+ NEW .tournament_id
90+ );
91+ END LOOP;
92+
93+ END IF;
94+
95+ RETURN NEW;
96+ END;
97+ $$;
98+
99+ DROP TRIGGER IF EXISTS tai_tournament_team ON public .tournament_teams ;
100+ CREATE TRIGGER tai_tournament_team AFTER INSERT ON public .tournament_teams FOR EACH ROW EXECUTE FUNCTION public .tai_tournament_team ();
You can’t perform that action at this time.
0 commit comments