66 original_match_options_id uuid;
77 match_options_record match_options%ROWTYPE;
88 final_match_options_id uuid;
9+ _decider_best_of int ;
10+ _effective_best_of int ;
911BEGIN
1012 -- Get match_options_id from stage first, then tournament if stage doesn't have one
1113 SELECT COALESCE(
@@ -15,44 +17,59 @@ BEGIN
1517 FROM tournament_stages ts
1618 INNER JOIN tournaments t ON t .id = ts .tournament_id
1719 WHERE ts .id = _stage_id;
18-
20+
1921 -- If no match_options_id found, return NULL
2022 IF original_match_options_id IS NULL THEN
2123 RETURN NULL ;
2224 END IF;
23-
24- -- Get match_options record and check if best_of needs to be changed
25+
26+ -- Get match_options record
2527 SELECT * INTO match_options_record
2628 FROM match_options
2729 WHERE id = original_match_options_id;
28-
29- -- If best_of is 1, create a new match_options with best_of = 3
30- IF match_options_record .best_of = 1 THEN
31- match_options_record .best_of := 3 ;
32- -- Insert the modified record (id will be auto-generated)
33- INSERT INTO match_options (
34- overtime, knife_round, mr, best_of, coaches, number_of_substitutes,
35- map_veto, timeout_setting, tech_timeout_setting, map_pool_id, type,
36- regions, prefer_dedicated_server, invite_code, lobby_access,
37- region_veto, ready_setting, check_in_setting, default_models, tv_delay
38- ) VALUES (
39- match_options_record .overtime , match_options_record .knife_round ,
40- match_options_record .mr , match_options_record .best_of ,
41- match_options_record .coaches , match_options_record .number_of_substitutes ,
42- match_options_record .map_veto , match_options_record .timeout_setting ,
43- match_options_record .tech_timeout_setting , match_options_record .map_pool_id ,
44- match_options_record .type , match_options_record .regions ,
45- match_options_record .prefer_dedicated_server , match_options_record .invite_code ,
46- match_options_record .lobby_access , match_options_record .region_veto ,
47- match_options_record .ready_setting , match_options_record .check_in_setting ,
48- match_options_record .default_models , match_options_record .tv_delay
49- )
50- RETURNING id INTO final_match_options_id;
30+
31+ -- Read decider_best_of from the stage
32+ SELECT ts .decider_best_of INTO _decider_best_of
33+ FROM tournament_stages ts WHERE ts .id = _stage_id;
34+
35+ -- Determine effective best_of for decider matches
36+ IF _decider_best_of IS NOT NULL THEN
37+ _effective_best_of := _decider_best_of;
5138 ELSE
52- final_match_options_id := original_match_options_id;
39+ -- Legacy behavior: only BO1 -> BO3, others unchanged
40+ IF match_options_record .best_of = 1 THEN
41+ _effective_best_of := 3 ;
42+ ELSE
43+ RETURN original_match_options_id;
44+ END IF;
5345 END IF;
54-
46+
47+ -- If target BO equals current BO, no clone needed
48+ IF _effective_best_of = match_options_record .best_of THEN
49+ RETURN original_match_options_id;
50+ END IF;
51+
52+ -- Clone match_options with the new best_of
53+ match_options_record .best_of := _effective_best_of;
54+ INSERT INTO match_options (
55+ overtime, knife_round, mr, best_of, coaches, number_of_substitutes,
56+ map_veto, timeout_setting, tech_timeout_setting, map_pool_id, type,
57+ regions, prefer_dedicated_server, invite_code, lobby_access,
58+ region_veto, ready_setting, check_in_setting, default_models, tv_delay
59+ ) VALUES (
60+ match_options_record .overtime , match_options_record .knife_round ,
61+ match_options_record .mr , match_options_record .best_of ,
62+ match_options_record .coaches , match_options_record .number_of_substitutes ,
63+ match_options_record .map_veto , match_options_record .timeout_setting ,
64+ match_options_record .tech_timeout_setting , match_options_record .map_pool_id ,
65+ match_options_record .type , match_options_record .regions ,
66+ match_options_record .prefer_dedicated_server , match_options_record .invite_code ,
67+ match_options_record .lobby_access , match_options_record .region_veto ,
68+ match_options_record .ready_setting , match_options_record .check_in_setting ,
69+ match_options_record .default_models , match_options_record .tv_delay
70+ )
71+ RETURNING id INTO final_match_options_id;
72+
5573 RETURN final_match_options_id;
5674END;
5775$$ LANGUAGE plpgsql;
58-
0 commit comments