From 3e4ead3aaec8438b39f6e949ddc8ed84f857178f Mon Sep 17 00:00:00 2001 From: Tom Stellard Date: Sun, 11 Jul 2021 12:59:06 -0700 Subject: [PATCH] Reset all cups at the beginning of each season This fixes a bug in league fixture scheduling where league matches would be scheduled around cup matches from the previous season. This caused league matches to be scheduled in later week rounds which in some cases caused these matches to not be played. --- src/cup.c | 16 +++++++++++++++- src/cup.h | 3 +++ src/league.c | 2 +- src/start_end.c | 17 +++++++++++------ 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/cup.c b/src/cup.c index 87b10f78..48993d4d 100644 --- a/src/cup.c +++ b/src/cup.c @@ -440,7 +440,7 @@ cup_load_choose_team_from_cup(Cup *cup, const Cup *cup_temp, GPtrArray *teams, c /* Self-referential cup or no? */ cup_teams_sorted = (cup == cup_temp) ? cup_get_last_season_results(cup) : - cup_get_teams_sorted(cup_temp); + cup_get_most_recent_results(cup_temp); start = cup_choose_team_compute_start_idx(ct); end = cup_choose_team_compute_end_idx(ct, cup_teams_sorted->len) + 1; @@ -1430,3 +1430,17 @@ cup_get_last_season_results(const Cup *cup) { return g_ptr_array_index(cup->history, cup->history->len - 1); } + +/** @return The most recent cup results. If the cup has no fixtures scheduled + * for this year, this function returns last year's results. If it does have + * fixtures schedule then this year's results are returned (even if the cup + * is not complete yet). The caller is responsible for freeing the returned + * GPtrArray. + */ +GPtrArray * +cup_get_most_recent_results(const Cup *cup) +{ + if (cup->fixtures->len == 0) + return misc_copy_ptr_array(cup_get_last_season_results(cup)); + return cup_get_teams_sorted(cup); +} diff --git a/src/cup.h b/src/cup.h index e29b01bf..f09b672a 100644 --- a/src/cup.h +++ b/src/cup.h @@ -148,4 +148,7 @@ query_cup_hidden(const Cup *cup); GPtrArray * cup_get_last_season_results(const Cup *cup); +GPtrArray * +cup_get_most_recent_results(const Cup *cup); + #endif diff --git a/src/league.c b/src/league.c index 3963423e..53aae66d 100644 --- a/src/league.c +++ b/src/league.c @@ -1206,7 +1206,7 @@ league_get_team_movements(League *league, GArray *team_movements) { prom_cup = cup_from_sid(g_array_index(league->prom_rel.prom_games, PromGames, i).cup_sid); - prom_games_teams = cup_get_teams_sorted(prom_cup); + prom_games_teams = cup_get_most_recent_results(prom_cup); league_get_team_movements_prom_games(league, &g_array_index(league->prom_rel.prom_games, PromGames, i), team_movements, prom_games_teams, TRUE); diff --git a/src/start_end.c b/src/start_end.c index d0df8c5a..b9f85369 100644 --- a/src/start_end.c +++ b/src/start_end.c @@ -168,13 +168,18 @@ start_new_season(void) g_ptr_array_remove_index(acps, i); } - /* Deal with cups that have to take place before promotion/relegation. */ - for(i=cps->len - 1; i >= 0; i--) - if(cp(i).add_week == -1) { - /* Reset cup to save its history. */ - cup_reset(&cp(i)); + for(i=cps->len - 1; i >= 0; i--) { + /* Reset all cups. We need to make sure all cups get reset + * before the start of the next season. Otherwise, the fixtures + * from last year's cup will interfere with scheduling the league + * fixtures. Also, cup_reset() saves this season's results in the + * history list. */ + cup_reset(&cp(i)); + + /* Deal with cups that have to take place before promotion/relegation. */ + if(cp(i).add_week == -1) fixture_write_cup_fixtures(&cp(i)); - } + } if(season > 1) {