diff --git a/src/cup.c b/src/cup.c index 8f2064e1..46db3413 100644 --- a/src/cup.c +++ b/src/cup.c @@ -201,6 +201,19 @@ query_cup_choose_team_is_league(const gchar *sid) return FALSE; } +/** @return TRUE if the team(s) referenced by \p sid need to be + * generated. + */ +static gboolean +cup_choose_team_should_generate(const CupChooseTeam *ct) +{ + if (g_str_has_prefix(ct->sid, "LEAGUE") || g_str_has_prefix(ct->sid, "CUP")) + return FALSE; + + return !country_get_league_sid(&country, ct->sid) && + !country_get_cup_sid(&country, ct->sid); +} + /** Write the cup or league of the chooseteam into the appropriate pointer and return TRUE; return FALSE if no cup/league is found. */ void @@ -298,7 +311,7 @@ cup_get_team_pointers(Cup *cup, gint round, GPtrArray *teams_sorted, gboolean pr if(choose_team->preload == preload) { - if(choose_team->generate) + if(cup_choose_team_should_generate(choose_team)) cup_load_choose_team_generate(cup, cup_round, choose_team); else cup_load_choose_team(cup, teams, teams_sorted, choose_team); @@ -1169,7 +1182,7 @@ query_cup_begins(const Cup *cup) cup_round = &g_array_index(cup->rounds, CupRound, j); for(i=0;ichoose_teams->len;i++) - if(!g_array_index(cup_round->choose_teams,CupChooseTeam, i).generate) + if(!cup_choose_team_should_generate(&g_array_index(cup_round->choose_teams,CupChooseTeam, i))) { cup_get_choose_team_league_cup( &g_array_index(cup_round->choose_teams, diff --git a/src/league.c b/src/league.c index 966b83c4..09fb7281 100644 --- a/src/league.c +++ b/src/league.c @@ -380,6 +380,25 @@ league_cup_average_capacity(gint clid) return sum / (gfloat)cnt; } + +/** This is the same as @see league_index_from_sid, except it does not exit + * the program if the sid is not found. + * @returns The index of the league in the country's league array or -1 if + * not found. + */ +gint +country_get_index_from_sid(const Country *country, const gchar *sid) +{ + gint i; + + for (i = 0; i < country->leagues->len; i++) { + const League *league = &g_array_index(country->leagues, League, i); + if (!strcmp(league->sid, sid)) + return i; + } + return -1; +} + /** Get the index of the league with the specified string id. */ gint league_index_from_sid(const gchar *sid) @@ -400,6 +419,44 @@ league_index_from_sid(const gchar *sid) return -1; } +/** @returns TRUE if the league with \p sid is in \p country. */ +gboolean +country_has_league_sid(const Country *country, const gchar *sid) +{ + return country_get_index_from_sid(country, sid) != -1; +} + + +/** @return A Cup object if the cup with \p sid belongs to \p country. + * NULL otherwise. + */ +Cup * +country_get_cup_sid(const Country *country, const gchar *sid) +{ + int i; + for (i = 0; i < country->cups->len; i++) { + Cup *cup = &g_array_index(country->cups, Cup, i); + if (!strcmp(cup->sid, sid)) + return cup; + } + return NULL; +} + +/** @return A League object if the league with \p sid belongs to \p country. + * NULL otherwise. + */ +League * +country_get_league_sid(Country *country, const gchar *sid) +{ + int i; + for (i = 0; i < country->leagues->len; i++) { + League *league = &g_array_index(country->leagues, League, i); + if (!strcmp(league->sid, sid)) + return league; + } + return NULL; +} + /** Remove the team with the specified id from the teams array without freeing the memory (used in promotion/relegation). */ void diff --git a/src/league.h b/src/league.h index 0e69dbda..bbe6d771 100644 --- a/src/league.h +++ b/src/league.h @@ -94,9 +94,21 @@ league_cup_average_capacity(gint clid); void league_get_team_movements(League *league, GArray *team_movements); +gint +country_get_index_from_sid(const Country *country, const gchar *sid); + gint league_index_from_sid(const gchar *sid); +gboolean +country_has_league_sid(const Country *country, const gchar *sid); + +Cup * +country_get_cup_sid(const Country *country, const gchar *sid); + +League * +country_get_league_sid(Country *country, const gchar *sid); + void league_remove_team_with_id(League *league, gint id);