From 2780e8bdaa5ace19c568a449e08a88d3a54d4eaf Mon Sep 17 00:00:00 2001 From: gyboth Date: Tue, 18 Nov 2008 18:32:37 +0000 Subject: [PATCH] Cup choose team cleanup; skip_group_check added. --- src/cup.c | 305 ++++++++++++++++++++++------------------- src/cup.h | 7 + src/cup_struct.h | 3 + src/xml_cup.c | 7 + src/xml_loadsave_cup.c | 6 + 5 files changed, 188 insertions(+), 140 deletions(-) diff --git a/src/cup.c b/src/cup.c index f0c11711..05fe026d 100644 --- a/src/cup.c +++ b/src/cup.c @@ -85,6 +85,7 @@ cup_choose_team_new(void) new.start_idx = new.end_idx = -1; new.randomly = FALSE; new.generate = FALSE; + new.skip_group_check = FALSE; return new; } @@ -279,10 +280,8 @@ cup_get_team_pointers(Cup *cup, gint round) void cup_load_choose_team(Cup *cup, GPtrArray *teams, const CupChooseTeam *ct) { - gint i, j, start, end; + gint i; gint debug_num = teams->len; - gint number_of_teams = 0; - GPtrArray *cup_teams_sorted = NULL; const League *league = NULL; const Cup *cup_temp = NULL; @@ -293,149 +292,174 @@ cup_load_choose_team(Cup *cup, GPtrArray *teams, const CupChooseTeam *ct) cup_get_choose_team_league_cup(ct, &league, &cup_temp); if(cup_temp == NULL) - { - if(ct->number_of_teams == -1) - { - for(j=0;jtable.elements->len;j++) - { - g_ptr_array_add( - teams, team_of_id( - g_array_index(league->table.elements, TableElement, j).team_id)); - g_ptr_array_add( - cup->team_names, - g_strdup(team_of_id(g_array_index(league->table.elements, TableElement, j).team_id)->name)); - } - } - else - { - start = ct->start_idx - 1; - end = ct->end_idx - start; - - gint order[end]; - for(j=0;jrandomly) - math_generate_permutation(order, start, start + end - 1); - - for(j = 0; j < end; j++) - { - if(debug > 80) - g_print("team %s isinint %d numteams %d\n", - team_of_id(g_array_index(league->table.elements, - TableElement, order[j]).team_id)->name, - query_team_is_in_cups( - team_of_id(g_array_index(league->table.elements, - TableElement, order[j]).team_id), - cup->group), - number_of_teams); - - if(!query_team_is_in_cups( - team_of_id(g_array_index(league->table.elements, TableElement, order[j]).team_id), cup->group)) - { - g_ptr_array_add(teams, - team_of_id(g_array_index(league->table.elements, TableElement, order[j]).team_id)); - g_ptr_array_add( - cup->team_names, - g_strdup(team_of_id(g_array_index(league->table.elements, TableElement, order[j]).team_id)->name)); - number_of_teams++; - - if(number_of_teams == ct->number_of_teams) - break; - } - } - } - - if(ct->number_of_teams != -1 && - number_of_teams != ct->number_of_teams) - main_exit_program(EXIT_CHOOSE_TEAM_ERROR, - "cup_load_choose_team (1): not enough teams (that don't participate in international cups yet) found in chooseteam %s for cup %s (%d specified, %d found) cup group %d.\n ", - ct->sid, cup->name, ct->number_of_teams, - number_of_teams, cup->group); - } + cup_load_choose_team_from_league(cup, league, teams, ct); else - { - if(season == 1 && cup->add_week == 0) - { - if(lig(0).teams->len < ct->number_of_teams) - main_exit_program(EXIT_CHOOSE_TEAM_ERROR, - "cup_load_choose_team: not enough teams in league 0 for chooseteam %s (%d; required: %d) in cup %s\n", - ct->sid, lig(0).teams->len, - ct->number_of_teams, cup->name); - - gint permutation[lig(0).teams->len]; - math_generate_permutation(permutation, 0, lig(0).teams->len - 1); - - for(i = ct->start_idx - 1; i <= ct->end_idx - 1; i++) - { - if(!query_team_is_in_cups( - &g_array_index(lig(0).teams, - Team, permutation[i - ct->start_idx + 1]), cup->group)) - { - g_ptr_array_add(teams, &g_array_index(lig(0).teams, - Team, permutation[i - ct->start_idx + 1])); - g_ptr_array_add(cup->team_names, - g_strdup(g_array_index(lig(0).teams, - Team, permutation[i - ct->start_idx + 1]).name)); - number_of_teams++; - } - - if(number_of_teams == ct->number_of_teams) - break; - } - - if(number_of_teams != ct->number_of_teams) - main_exit_program(EXIT_CHOOSE_TEAM_ERROR, - "cup_load_choose_team (2): not enough teams found in league 0 for chooseteam %s (%d; required: %d) in cup %s (group %d)\n", - ct->sid, number_of_teams, - ct->number_of_teams, cup->name, cup->group); - } - else - { - cup_teams_sorted = cup_get_teams_sorted(cup_temp); - - if(ct->number_of_teams == -1) - { - start = 0; - end = cup_teams_sorted->len; - } - else - { - start = ct->start_idx - 1; - end = ct->end_idx; - } - - for(j = start; j < end; j++) - { - if(!query_team_is_in_cups( - (Team*)g_ptr_array_index(cup_teams_sorted, j), cup->group)) - { - g_ptr_array_add(teams, g_ptr_array_index(cup_teams_sorted, j)); - g_ptr_array_add(cup->team_names, - g_strdup(((Team*)g_ptr_array_index(cup_teams_sorted, j))->name)); - number_of_teams++; - - if(number_of_teams == ct->number_of_teams) - break; - } - } - - g_ptr_array_free(cup_teams_sorted, TRUE); - - if(ct->number_of_teams != -1 && - number_of_teams != ct->number_of_teams) - main_exit_program(EXIT_CHOOSE_TEAM_ERROR, - "cup_load_choose_team(3): not enough teams (that don't participate in international cups yet) found in chooseteam %s for cup %s (%d specified, %d found) cup group %d.\n ", - ct->sid, cup->name, - ct->number_of_teams, number_of_teams, cup->group); - } - } + cup_load_choose_team_from_cup(cup, cup_temp, teams, ct); if(debug > 80) for(i=debug_num;ilen;i++) g_print("cup_load_choose_team: %d %s \n", i, ((Team*)g_ptr_array_index(teams, i))->name); } +void +cup_load_choose_team_from_cup(Cup *cup, const Cup *cup_temp, GPtrArray *teams, const CupChooseTeam *ct) +{ + gint i; + gint start, end; + gint number_of_teams; + GPtrArray *cup_teams_sorted; + + number_of_teams = 0; + cup_teams_sorted = NULL; + + if(season == 1 && cup->add_week == 0) + { + if(lig(0).teams->len < ct->number_of_teams) + main_exit_program(EXIT_CHOOSE_TEAM_ERROR, + "cup_load_choose_team_from_cup: not enough teams in league 0 for chooseteam %s (%d; required: %d) in cup %s\n", + ct->sid, lig(0).teams->len, + ct->number_of_teams, cup->name); + + gint permutation[lig(0).teams->len]; + math_generate_permutation(permutation, 0, lig(0).teams->len - 1); + + for(i = ct->start_idx - 1; i <= ct->end_idx - 1; i++) + { + if(ct->skip_group_check || + !query_team_is_in_cups( + &g_array_index(lig(0).teams, + Team, permutation[i - ct->start_idx + 1]), cup->group)) + { + g_ptr_array_add(teams, &g_array_index(lig(0).teams, + Team, permutation[i - ct->start_idx + 1])); + g_ptr_array_add(cup->team_names, + g_strdup(g_array_index(lig(0).teams, + Team, permutation[i - ct->start_idx + 1]).name)); + number_of_teams++; + } + + if(number_of_teams == ct->number_of_teams) + break; + } + + if(number_of_teams != ct->number_of_teams) + main_exit_program(EXIT_CHOOSE_TEAM_ERROR, + "cup_load_choose_team_from_cup (2): not enough teams found in league 0 for chooseteam %s (%d; required: %d) in cup %s (group %d)\n", + ct->sid, number_of_teams, + ct->number_of_teams, cup->name, cup->group); + } + else + { + cup_teams_sorted = cup_get_teams_sorted(cup_temp); + + if(ct->number_of_teams == -1) + { + start = 0; + end = cup_teams_sorted->len; + } + else + { + start = ct->start_idx - 1; + end = ct->end_idx; + } + + for(i = start; i < end; i++) + { + if(ct->skip_group_check || + !query_team_is_in_cups( + (Team*)g_ptr_array_index(cup_teams_sorted, i), cup->group)) + { + g_ptr_array_add(teams, g_ptr_array_index(cup_teams_sorted, i)); + g_ptr_array_add(cup->team_names, + g_strdup(((Team*)g_ptr_array_index(cup_teams_sorted, i))->name)); + number_of_teams++; + + if(number_of_teams == ct->number_of_teams) + break; + } + } + + g_ptr_array_free(cup_teams_sorted, TRUE); + + if(ct->number_of_teams != -1 && + number_of_teams != ct->number_of_teams) + main_exit_program(EXIT_CHOOSE_TEAM_ERROR, + "cup_load_choose_team_from_cup (3): not enough teams (that don't participate in international cups yet) found in chooseteam %s for cup %s (%d specified, %d found) cup group %d.\n ", + ct->sid, cup->name, + ct->number_of_teams, number_of_teams, cup->group); + } +} +void +cup_load_choose_team_from_league(Cup *cup, const League *league, + GPtrArray *teams, const CupChooseTeam *ct) +{ + gint start, end; + gint number_of_teams; + gint j; + + number_of_teams = 0; + + if(ct->number_of_teams == -1) + { + for(j=0;jtable.elements->len;j++) + { + g_ptr_array_add( + teams, team_of_id( + g_array_index(league->table.elements, TableElement, j).team_id)); + g_ptr_array_add( + cup->team_names, + g_strdup(team_of_id(g_array_index(league->table.elements, TableElement, j).team_id)->name)); + } + } + else + { + start = ct->start_idx - 1; + end = ct->end_idx - start; + + gint order[end]; + for(j=0;jrandomly) + math_generate_permutation(order, start, start + end - 1); + + for(j = 0; j < end; j++) + { + if(debug > 80) + g_print("team %s isinint %d numteams %d\n", + team_of_id(g_array_index(league->table.elements, + TableElement, order[j]).team_id)->name, + query_team_is_in_cups( + team_of_id(g_array_index(league->table.elements, + TableElement, order[j]).team_id), + cup->group), + number_of_teams); + + if(ct->skip_group_check || + !query_team_is_in_cups( + team_of_id(g_array_index(league->table.elements, TableElement, order[j]).team_id), cup->group)) + { + g_ptr_array_add(teams, + team_of_id(g_array_index(league->table.elements, TableElement, order[j]).team_id)); + g_ptr_array_add( + cup->team_names, + g_strdup(team_of_id(g_array_index(league->table.elements, TableElement, order[j]).team_id)->name)); + number_of_teams++; + + if(number_of_teams == ct->number_of_teams) + break; + } + } + } + + if(ct->number_of_teams != -1 && + number_of_teams != ct->number_of_teams) + main_exit_program(EXIT_CHOOSE_TEAM_ERROR, + "cup_load_choose_team_from_league (1): not enough teams (that don't participate in international cups yet) found in chooseteam %s for cup %s (%d specified, %d found) cup group %d.\n ", + ct->sid, cup->name, ct->number_of_teams, + number_of_teams, cup->group); +} + /** 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) @@ -517,7 +541,8 @@ cup_load_choose_team_generate(Cup *cup, CupRound *cup_round, const CupChooseTeam for(j = 0; j < end_idx; j++) { - if(!query_team_is_in_cups(&g_array_index(teams_local, Team, permutation[j]), cup->group)) + if(ct->skip_group_check || + !query_team_is_in_cups(&g_array_index(teams_local, Team, permutation[j]), cup->group)) { g_array_append_val(cup_round->teams, g_array_index(teams_local, Team, permutation[j])); g_array_index(cup_round->teams, Team, cup_round->teams->len - 1).clid = cup->id; diff --git a/src/cup.h b/src/cup.h index 676b9186..c0ba8c3b 100644 --- a/src/cup.h +++ b/src/cup.h @@ -62,6 +62,13 @@ cup_load_choose_team_generate(Cup *cup, CupRound *cup_round, const CupChooseTeam void cup_load_choose_team(Cup *cup, GPtrArray *teams, const CupChooseTeam *ct); +void +cup_load_choose_team_from_league(Cup *cup, const League *league, + GPtrArray *teams, const CupChooseTeam *ct); + +void +cup_load_choose_team_from_cup(Cup *cup, const Cup *cup_team, GPtrArray *teams, const CupChooseTeam *ct); + gint cup_get_first_week_of_cup_round(Cup *cup, gint cup_round); diff --git a/src/cup_struct.h b/src/cup_struct.h index a192d53b..98fb3c28 100644 --- a/src/cup_struct.h +++ b/src/cup_struct.h @@ -115,6 +115,9 @@ typedef struct a league file or taken from one of the country's leagues or cups. Default: FALSE. */ gboolean generate; + /** Whether to skip the checking if a team participates in other + of the same cup groupcups. */ + gboolean skip_group_check; } CupChooseTeam; /** Structure representing a cup. */ diff --git a/src/xml_cup.c b/src/xml_cup.c index dff0cad6..52ade1ab 100644 --- a/src/xml_cup.c +++ b/src/xml_cup.c @@ -69,6 +69,7 @@ #define TAG_CHOOSE_TEAM_END_IDX "end_idx" #define TAG_CHOOSE_TEAM_RANDOMLY "randomly" #define TAG_CHOOSE_TEAM_GENERATE "generate" +#define TAG_CHOOSE_TEAM_SKIP_GROUP_CHECK "skip_group_check" /** * Enum with the states used in the XML parser functions. @@ -110,6 +111,7 @@ enum XmlCupStates STATE_CHOOSE_TEAM_END_IDX, STATE_CHOOSE_TEAM_RANDOMLY, STATE_CHOOSE_TEAM_GENERATE, + STATE_CHOOSE_TEAM_SKIP_GROUP_CHECK, STATE_END }; @@ -216,6 +218,8 @@ xml_cup_read_start_element (GMarkupParseContext *context, state = STATE_CHOOSE_TEAM_RANDOMLY; else if(strcmp(element_name, TAG_CHOOSE_TEAM_GENERATE) == 0) state = STATE_CHOOSE_TEAM_GENERATE; + else if(strcmp(element_name, TAG_CHOOSE_TEAM_SKIP_GROUP_CHECK) == 0) + state = STATE_CHOOSE_TEAM_SKIP_GROUP_CHECK; else g_warning("xml_cup_read_start_element: unknown tag: %s; I'm in state %d\n", element_name, state); @@ -275,6 +279,7 @@ xml_cup_read_end_element (GMarkupParseContext *context, strcmp(element_name, TAG_CHOOSE_TEAM_START_IDX) == 0 || strcmp(element_name, TAG_CHOOSE_TEAM_END_IDX) == 0 || strcmp(element_name, TAG_CHOOSE_TEAM_RANDOMLY) == 0 || + strcmp(element_name, TAG_CHOOSE_TEAM_SKIP_GROUP_CHECK) == 0 || strcmp(element_name, TAG_CHOOSE_TEAM_GENERATE) == 0) state = STATE_CHOOSE_TEAM; else if(strcmp(element_name, TAG_CUP) != 0) @@ -366,6 +371,8 @@ xml_cup_read_text (GMarkupParseContext *context, new_choose_team.randomly = int_value; else if(state == STATE_CHOOSE_TEAM_GENERATE) new_choose_team.generate = int_value; + else if(state == STATE_CHOOSE_TEAM_SKIP_GROUP_CHECK) + new_choose_team.skip_group_check = int_value; } /** diff --git a/src/xml_loadsave_cup.c b/src/xml_loadsave_cup.c index bfa99cef..90f9c893 100644 --- a/src/xml_loadsave_cup.c +++ b/src/xml_loadsave_cup.c @@ -49,6 +49,7 @@ enum TAG_CUP_CHOOSE_TEAM_END_IDX, TAG_CUP_CHOOSE_TEAM_RANDOMLY, TAG_CUP_CHOOSE_TEAM_GENERATE, + TAG_CUP_CHOOSE_TEAM_SKIP_GROUP_CHECK, TAG_CUP_ROUND, TAG_CUP_ROUND_NEW_TEAMS, TAG_CUP_ROUND_BYES, @@ -155,6 +156,7 @@ xml_loadsave_cup_end_element (GMarkupParseContext *context, tag == TAG_CUP_CHOOSE_TEAM_END_IDX || tag == TAG_CUP_CHOOSE_TEAM_SID || tag == TAG_CUP_CHOOSE_TEAM_GENERATE || + tag == TAG_CUP_CHOOSE_TEAM_SKIP_GROUP_CHECK || tag == TAG_CUP_CHOOSE_TEAM_RANDOMLY) state = TAG_CUP_CHOOSE_TEAM; else if(tag == TAG_CUP_ROUND_HOME_AWAY || @@ -242,6 +244,8 @@ xml_loadsave_cup_text (GMarkupParseContext *context, new_choose_team.randomly = int_value; else if(state == TAG_CUP_CHOOSE_TEAM_GENERATE) new_choose_team.generate = int_value; + else if(state == TAG_CUP_CHOOSE_TEAM_SKIP_GROUP_CHECK) + new_choose_team.skip_group_check = int_value; else if(state == TAG_CUP_ROUND_HOME_AWAY) new_round.home_away = int_value; else if(state == TAG_CUP_ROUND_NEW_TEAMS) @@ -472,6 +476,8 @@ xml_loadsave_cup_write_choose_team(FILE *fil, const CupChooseTeam *choose_team) TAG_CUP_CHOOSE_TEAM_RANDOMLY, I2); xml_write_int(fil, choose_team->generate, TAG_CUP_CHOOSE_TEAM_GENERATE, I2); + xml_write_int(fil, choose_team->skip_group_check, + TAG_CUP_CHOOSE_TEAM_SKIP_GROUP_CHECK, I2); fprintf(fil, "%s\n", I1, TAG_CUP_CHOOSE_TEAM); }