Skip to content

Commit 363caf6

Browse files
authored
feature: auto cancel tournament pause (#110)
Co-authored-by: Flegma <Flegma@users.noreply.github.com>
1 parent 666df81 commit 363caf6

37 files changed

Lines changed: 329 additions & 127 deletions

File tree

hasura/enums/match-mode.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
insert into e_match_mode ("value", "description") values
2+
('admin', 'Match must be scheduled and started by an admin user'),
3+
('auto', 'Match is automatically scheduled by the system')
4+
on conflict(value) do update set "description" = EXCLUDED."description"

hasura/enums/tournament-statuses.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ insert into e_tournament_status ("value", "description") values
77
('Live', 'Live'),
88
('Cancelled', 'Cancelled'),
99
('CancelledMinTeams', 'Cancelled because it did not meet minimum number of teams'),
10-
('Finished', 'Finished')
10+
('Finished', 'Finished'),
11+
('Paused', 'Paused')
1112
on conflict(value) do update set "description" = EXCLUDED."description"

hasura/fixtures/fixtures.sql

Lines changed: 45 additions & 73 deletions
Large diffs are not rendered by default.

hasura/functions/match/can_cancel_match.sql

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@ CREATE OR REPLACE FUNCTION public.can_cancel_match(match public.matches, hasura_
22
RETURNS boolean
33
LANGUAGE plpgsql STABLE
44
AS $$
5-
DECLARE
65
BEGIN
7-
IF is_match_organizer(match, hasura_session) AND (
8-
match.status != 'Finished' AND
9-
match.status != 'Tie' AND
10-
match.status != 'Canceled' AND
11-
match.status != 'Forfeit' AND
12-
match.status != 'Surrendered'
13-
) THEN
14-
RETURN true;
6+
IF NOT is_match_organizer(match, hasura_session) THEN
7+
RETURN false;
158
END IF;
169

17-
RETURN false;
10+
IF match.status IN ('Finished', 'Tie', 'Canceled', 'Forfeit', 'Surrendered') THEN
11+
RETURN false;
12+
END IF;
13+
14+
RETURN true;
1815
END;
1916
$$;

hasura/functions/match/can_schedule_match.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ DECLARE
66
lineup_1_ready boolean;
77
lineup_2_ready boolean;
88
BEGIN
9-
IF (match.status != 'Canceled' AND match.status != 'PickingPlayers' AND match.status != 'Scheduled' AND match.status != 'Surrendered' AND match.status != 'Forfeit') THEN
9+
IF (match.status != 'Canceled' AND match.status != 'PickingPlayers' AND match.status != 'Scheduled' AND match.status != 'WaitingForCheckIn' AND match.status != 'Surrendered' AND match.status != 'Forfeit') THEN
1010
return false;
1111
END IF;
1212

hasura/functions/match/get_team_name.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ BEGIN
2727
RETURN _team_name;
2828
END IF;
2929

30-
SELECT tt.name
30+
SELECT COALESCE(tt.name, t2.name)
3131
INTO _team_name
3232
FROM matches m
3333
JOIN tournament_brackets tb ON tb.match_id = m.id
@@ -41,6 +41,7 @@ BEGIN
4141
match_lineup.id = m.lineup_2_id
4242
AND tt.id = tb.tournament_team_id_2
4343
)
44+
LEFT JOIN teams t2 ON t2.id = tt.team_id
4445
WHERE m.id = (
4546
SELECT match_id
4647
FROM match_lineups
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CREATE OR REPLACE FUNCTION public.can_pause_tournament(
2+
tournament public.tournaments,
3+
hasura_session json
4+
)
5+
RETURNS boolean
6+
LANGUAGE plpgsql STABLE
7+
AS $$
8+
BEGIN
9+
IF tournament.status != 'Live' THEN
10+
RETURN false;
11+
END IF;
12+
13+
RETURN is_tournament_organizer(tournament, hasura_session);
14+
END;
15+
$$;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CREATE OR REPLACE FUNCTION public.can_resume_tournament(
2+
tournament public.tournaments,
3+
hasura_session json
4+
)
5+
RETURNS boolean
6+
LANGUAGE plpgsql STABLE
7+
AS $$
8+
BEGIN
9+
IF tournament.status != 'Paused' THEN
10+
RETURN false;
11+
END IF;
12+
13+
RETURN is_tournament_organizer(tournament, hasura_session);
14+
END;
15+
$$;

hasura/functions/tournaments/clone_match_options_with_best_of.sql

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ BEGIN
2929
overtime, knife_round, mr, best_of, coaches, number_of_substitutes,
3030
map_veto, timeout_setting, tech_timeout_setting, map_pool_id, type,
3131
regions, prefer_dedicated_server, invite_code, lobby_access,
32-
region_veto, ready_setting, check_in_setting, default_models, tv_delay
32+
region_veto, ready_setting, check_in_setting, default_models, tv_delay,
33+
auto_cancellation, match_mode, auto_cancel_duration, live_match_timeout
3334
) VALUES (
3435
match_options_record.overtime, match_options_record.knife_round,
3536
match_options_record.mr, _target_best_of,
@@ -40,7 +41,9 @@ BEGIN
4041
match_options_record.prefer_dedicated_server, match_options_record.invite_code,
4142
match_options_record.lobby_access, match_options_record.region_veto,
4243
match_options_record.ready_setting, match_options_record.check_in_setting,
43-
match_options_record.default_models, match_options_record.tv_delay
44+
match_options_record.default_models, match_options_record.tv_delay,
45+
match_options_record.auto_cancellation, match_options_record.match_mode,
46+
match_options_record.auto_cancel_duration, match_options_record.live_match_timeout
4447
)
4548
RETURNING id INTO cloned_id;
4649

hasura/functions/tournaments/schedule_next_round_robin_matches.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ BEGIN
9696
END IF;
9797
END IF;
9898

99-
-- Schedule all brackets that are ready
100-
IF array_length(brackets_to_schedule, 1) > 0 THEN
99+
-- Schedule all brackets that are ready (skip if tournament is paused or auto_start is off)
100+
IF array_length(brackets_to_schedule, 1) > 0 AND should_auto_schedule(stage_id) THEN
101101
FOREACH bracket_id IN ARRAY brackets_to_schedule LOOP
102102
SELECT * INTO bracket_row FROM tournament_brackets WHERE id = bracket_id;
103-
103+
104104
IF bracket_row.match_id IS NULL THEN
105105
PERFORM schedule_tournament_match(bracket_row);
106106
END IF;

0 commit comments

Comments
 (0)