Skip to content

Commit 60f5f0c

Browse files
authored
Fix match sorting algorithm (#1314)
This PR alters the match scheduling logic Now, the algorithm schedules all matches for each stage in sequence (i.e. all matches from stage A, then all matches from stage B). Matches from stage items inside a single stage are interleaved. To guarantee that the correct order is followed, rounds are sorted according to their id. Lower round id are scheduled first. Fix #1312
1 parent 36359af commit 60f5f0c

File tree

1 file changed

+24
-30
lines changed

1 file changed

+24
-30
lines changed

backend/bracket/logic/planning/matches.py

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,47 +29,41 @@ async def schedule_all_unscheduled_matches(
2929
if len(stages) < 1 or len(courts) < 1:
3030
return
3131

32-
stage = stages[0]
33-
stage_items = sorted(stage.stage_items, key=lambda x: x.name)
34-
35-
# First schedule all matches from the first stage
36-
for i, stage_item in enumerate(stage_items):
37-
court = courts[min(i, len(courts) - 1)]
38-
start_time = tournament.start_time
39-
position_in_schedule = 0
40-
for round_ in stage_item.rounds:
41-
for match in round_.matches:
42-
if match.start_time is None and match.position_in_schedule is None:
43-
await sql_reschedule_match_and_determine_duration_and_margin(
44-
court.id,
45-
start_time,
46-
position_in_schedule,
47-
match,
48-
tournament,
49-
)
32+
time_last_match_from_previous_stage = tournament.start_time
33+
position_last_match_from_previous_stage = 0
5034

51-
start_time += timedelta(minutes=match.duration_minutes)
52-
position_in_schedule += 1
35+
for stage in stages:
36+
stage_items = sorted(stage.stage_items, key=lambda x: x.name)
5337

54-
# Then, all other stages
55-
for stage in stages[1:]:
56-
start_time = tournament.start_time
57-
position_in_schedule = 0
58-
for stage_item in stage.stage_items:
59-
for round_ in stage_item.rounds:
60-
for match in round_.matches:
61-
start_time += timedelta(minutes=match.duration_minutes)
62-
position_in_schedule += 1
38+
stage_start_time = time_last_match_from_previous_stage
39+
stage_position_in_schedule = position_last_match_from_previous_stage
6340

41+
for i, stage_item in enumerate(stage_items):
42+
court = courts[min(i, len(courts) - 1)]
43+
start_time = stage_start_time
44+
position_in_schedule = stage_position_in_schedule
45+
for round_ in sorted(stage_item.rounds, key=lambda r: r.id):
46+
for match in round_.matches:
6447
if match.start_time is None and match.position_in_schedule is None:
6548
await sql_reschedule_match_and_determine_duration_and_margin(
66-
courts[-1].id,
49+
court.id,
6750
start_time,
6851
position_in_schedule,
6952
match,
7053
tournament,
7154
)
7255

56+
start_time += timedelta(minutes=match.duration_minutes)
57+
position_in_schedule += 1
58+
59+
time_last_match_from_previous_stage = max(
60+
time_last_match_from_previous_stage, start_time
61+
)
62+
63+
position_last_match_from_previous_stage = max(
64+
position_last_match_from_previous_stage, position_in_schedule
65+
)
66+
7367
await update_start_times_of_matches(tournament_id)
7468

7569

0 commit comments

Comments
 (0)