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.
This commit is contained in:
Tom Stellard 2021-02-09 06:53:22 -08:00 committed by Tom Stellard
parent 347d74a3c4
commit 8a3100a28c
3 changed files with 64 additions and 0 deletions

View File

@ -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;j<sids->len;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);

View File

@ -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.
*/

View File

@ -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);