From 8a3100a28c668ec03d58e413398ce87ffa293f15 Mon Sep 17 00:00:00 2001 From: Tom Stellard Date: Tue, 9 Feb 2021 06:53:22 -0800 Subject: [PATCH] Add support for specifying cups in choose_team_sid tag for generated teams This makes it possible to include national cup winners in international cups. --- src/cup.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/league.c | 17 +++++++++++++++++ src/league.h | 3 +++ 3 files changed, 64 insertions(+) diff --git a/src/cup.c b/src/cup.c index 446edb6f..7936a1f2 100644 --- a/src/cup.c +++ b/src/cup.c @@ -537,6 +537,43 @@ cup_load_choose_team_from_league(Cup *cup, const League *league, number_of_teams, cup->group); } +/** This function is used when a CupChooseTeam object references a cup + * that is outside of the users's league. It returns a list of teamas + * that would participate in this cup. This is a best effort + * function and the goal is to just get enough teams to satisfy the + * requirements of the CupChooseTeam object, so it may omit some + * teams especially if the cup definition is complex. + */ +static void +cup_generate_team_list(const Cup *cup, GArray *teams, gboolean league_talents) +{ + int i; + const League *top_league = NULL; + for (i = 0; i < cup->rounds->len; i++) { + const CupRound *round = &g_array_index(cup->rounds, CupRound, i); + int j; + for (j = 0; j < round->choose_teams->len; j++) { + const CupChooseTeam *ct = &g_array_index(round->choose_teams, CupChooseTeam, j); + const League *league = bygfoot_get_league_sid(ct->sid); + if (!league) + continue; + if (!top_league || (league->layer < top_league->layer)) + top_league = league; + if (league->layer == 1) + break; + } + } + if (!top_league) + return; + + for (i = 0; i < top_league->teams->len; i++) { + Team *team = &g_array_index(top_league->teams, Team, i); + if (league_talents) + team->average_talent = top_league->average_talent; + g_ptr_array_add(teams, team); + } +} + /** Load the teams specified in the chooseteam from a non-country league. */ void cup_load_choose_team_generate(Cup *cup, CupRound *cup_round, const CupChooseTeam *ct) @@ -560,6 +597,13 @@ cup_load_choose_team_generate(Cup *cup, CupRound *cup_round, const CupChooseTeam for(j=0;jlen;j++) { const gchar *sid = g_ptr_array_index(sids, j); + Cup *generate_cup = bygfoot_get_cup_sid(sid); + if (generate_cup) { + cup_generate_team_list(generate_cup, teams_local, + query_league_cup_has_property(cup->id, "league_talents")); + continue; + } + if(!query_cup_choose_team_is_league(sid)) { const League *league = bygfoot_get_league_sid(sid); diff --git a/src/league.c b/src/league.c index 3bc25921..4c57692e 100644 --- a/src/league.c +++ b/src/league.c @@ -442,6 +442,23 @@ country_get_cup_sid(const Country *country, const gchar *sid) return NULL; } +/** Get the cup with \p sid from the list of all countries. + * @returns A pointer to a Cup object or NULL if the cup is not found. + */ +Cup * +bygfoot_get_cup_sid(const gchar *sid) +{ + int i; + for (i = 0; i < country_list->len; i++) { + Country *country = g_ptr_array_index(country_list, i); + Cup *cup = country_get_cup_sid(country, sid); + if (cup) + return cup; + } + return NULL; +} + + /** @return A League object if the league with \p sid belongs to \p country. * NULL otherwise. */ diff --git a/src/league.h b/src/league.h index 3a2ca247..0f15d096 100644 --- a/src/league.h +++ b/src/league.h @@ -106,6 +106,9 @@ country_has_league_sid(const Country *country, const gchar *sid); Cup * country_get_cup_sid(const Country *country, const gchar *sid); +Cup * +bygfoot_get_cup_sid(const gchar *sid); + League * country_get_league_sid(Country *country, const gchar *sid);