mirror of
https://github.com/tstellar/bygfoot.git
synced 2025-03-04 02:57:59 +01:00
Load all league and team names at the beginning of the game
This way we don't have to reload the country xml definitions every time we have to generate a new team.
This commit is contained in:
parent
8f18bb3741
commit
c1648270d1
62
src/cup.c
62
src/cup.c
@ -134,7 +134,6 @@ cup_round_new(void)
|
||||
new.two_match_week = FALSE;
|
||||
new.tables = g_array_new(FALSE, FALSE, sizeof(Table));
|
||||
new.choose_teams = g_array_new(FALSE, FALSE, sizeof(CupChooseTeam));
|
||||
new.teams = g_array_new(FALSE, FALSE, sizeof(Team));
|
||||
new.waits = g_array_new(FALSE, FALSE, sizeof(CupRoundWait));
|
||||
new.team_ptrs = g_ptr_array_new();
|
||||
|
||||
@ -173,10 +172,6 @@ cup_reset(Cup *cup)
|
||||
free_tables(&cup_round->tables);
|
||||
cup_round->tables = g_array_new(FALSE, FALSE, sizeof(Table));
|
||||
}
|
||||
|
||||
if(cup_round->teams->len> 0)
|
||||
free_teams_array(&cup_round->teams, TRUE);
|
||||
|
||||
g_ptr_array_free(cup_round->team_ptrs, TRUE);
|
||||
cup_round->team_ptrs = g_ptr_array_new();
|
||||
}
|
||||
@ -303,7 +298,7 @@ cup_get_team_pointers(Cup *cup, gint round, GPtrArray *teams_sorted, gboolean pr
|
||||
if(debug > 60)
|
||||
g_print("cup_get_team_pointers %s round %d\n", cup->name, round);
|
||||
|
||||
existing_teams = cup_round->teams->len;
|
||||
existing_teams = cup_round->team_ptrs->len;
|
||||
|
||||
for(i=0;i<cup_round->choose_teams->len;i++) {
|
||||
const CupChooseTeam *choose_team =
|
||||
@ -317,18 +312,11 @@ cup_get_team_pointers(Cup *cup, gint round, GPtrArray *teams_sorted, gboolean pr
|
||||
cup_load_choose_team(cup, teams, teams_sorted, choose_team);
|
||||
}
|
||||
}
|
||||
if(cup_round->teams->len - existing_teams > 0)
|
||||
while(teams->len + cup_round->teams->len - existing_teams > cup_round->new_teams)
|
||||
{
|
||||
free_team(&g_array_index(cup_round->teams, Team, cup_round->teams->len - 1));
|
||||
g_array_remove_index(cup_round->teams, cup_round->teams->len - 1);
|
||||
}
|
||||
|
||||
for(i=existing_teams;i<cup_round->teams->len;i++)
|
||||
for(i=existing_teams;i<cup_round->team_ptrs->len;i++)
|
||||
{
|
||||
team_generate_players_stadium(&g_array_index(cup_round->teams, Team, i), 0);
|
||||
g_ptr_array_add(cup->teams, &g_array_index(cup_round->teams, Team, i));
|
||||
g_ptr_array_add(teams, &g_array_index(cup_round->teams, Team, i));
|
||||
Team *team = g_ptr_array_index(cup_round->team_ptrs, i);
|
||||
g_ptr_array_add(cup->teams, team);
|
||||
}
|
||||
|
||||
country_lookup_first_team_ids(&country);
|
||||
@ -559,16 +547,14 @@ cup_load_choose_team_generate(Cup *cup, CupRound *cup_round, const CupChooseTeam
|
||||
|
||||
gint j, k;
|
||||
gint number_of_teams, end_idx = -1;
|
||||
GArray *teams_local = NULL;
|
||||
GArray *leagues = NULL;
|
||||
GPtrArray *teams_local = NULL;
|
||||
GPtrArray *sids = NULL;
|
||||
|
||||
if(debug > 60)
|
||||
g_print("cup_load_choose_team_generate: %s, %s \n", cup->name,
|
||||
ct->sid);
|
||||
|
||||
leagues = g_array_new(FALSE, FALSE, sizeof(League));
|
||||
teams_local = g_array_new(FALSE, FALSE, sizeof(Team));
|
||||
teams_local = g_ptr_array_new();
|
||||
sids = misc_randomise_g_pointer_array(misc_separate_strings(ct->sid));
|
||||
|
||||
for(j=0;j<sids->len;j++)
|
||||
@ -576,9 +562,12 @@ cup_load_choose_team_generate(Cup *cup, CupRound *cup_round, const CupChooseTeam
|
||||
const gchar *sid = g_ptr_array_index(sids, j);
|
||||
if(!query_cup_choose_team_is_league(sid))
|
||||
{
|
||||
League *league;
|
||||
xml_league_read(sid, leagues);
|
||||
league = &g_array_index(leagues, League, leagues->len -1);
|
||||
const League *league = bygfoot_get_league_sid(sid);
|
||||
if (!league) {
|
||||
g_warning("Cup: %s choose_team_sid %s not found.",
|
||||
cup->sid, sid);
|
||||
continue;
|
||||
}
|
||||
|
||||
for(k=0; k < league->teams->len; k++)
|
||||
{
|
||||
@ -587,21 +576,17 @@ cup_load_choose_team_generate(Cup *cup, CupRound *cup_round, const CupChooseTeam
|
||||
{
|
||||
team->average_talent = league->average_talent;
|
||||
}
|
||||
g_array_append_val(teams_local, *team);
|
||||
g_ptr_array_add(teams_local, team);
|
||||
}
|
||||
|
||||
free_g_array(&league->teams);
|
||||
free_league(league);
|
||||
}
|
||||
}
|
||||
|
||||
g_array_free(leagues, TRUE);
|
||||
free_gchar_array(&sids);
|
||||
|
||||
/** No teams found. */
|
||||
if(teams_local->len == 0)
|
||||
{
|
||||
free_teams_array(&teams_local, FALSE);
|
||||
g_ptr_array_free(teams_local, TRUE);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -634,13 +619,15 @@ cup_load_choose_team_generate(Cup *cup, CupRound *cup_round, const CupChooseTeam
|
||||
|
||||
for(j = 0; j < end_idx; j++)
|
||||
{
|
||||
Team *team = g_ptr_array_index(teams_local, permutation[j]);
|
||||
if(ct->skip_group_check ||
|
||||
!query_team_is_in_cups(&g_array_index(teams_local, Team, permutation[j]), cup->group))
|
||||
!query_team_is_in_cups(team, 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;
|
||||
g_ptr_array_add(cup->team_names,
|
||||
g_strdup(g_array_index(cup_round->teams, Team, cup_round->teams->len - 1).name));
|
||||
//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;
|
||||
team->clid = cup->id;
|
||||
g_ptr_array_add(cup->team_names, g_strdup(team->name));
|
||||
g_ptr_array_add(cup_round->team_ptrs, team);
|
||||
|
||||
number_of_teams++;
|
||||
}
|
||||
@ -656,12 +643,7 @@ cup_load_choose_team_generate(Cup *cup, CupRound *cup_round, const CupChooseTeam
|
||||
"cup_load_choose_team_generate: not enough teams (that don't participate in international cups yet) found in chooseteam %s for cup %s (%d specified, %d found).\n ",
|
||||
ct->sid, cup->name,
|
||||
ct->number_of_teams, number_of_teams);
|
||||
|
||||
for(j=teams_local->len - 1; j>=0;j--)
|
||||
if(query_team_is_in_cup(&g_array_index(teams_local, Team, j), cup))
|
||||
g_array_remove_index(teams_local, j);
|
||||
|
||||
free_teams_array(&teams_local, FALSE);
|
||||
g_ptr_array_free(teams_local, teams_local->len > 0);
|
||||
}
|
||||
|
||||
/** Return a pointer array of teams ordered corresponding to
|
||||
|
@ -91,9 +91,6 @@ typedef struct
|
||||
/** Whether the two matches of a home/away round are
|
||||
played in one week. */
|
||||
gboolean two_match_week;
|
||||
/** The teams that got loaded for this cup round.
|
||||
Mostly this only happens in the first round. */
|
||||
GArray *teams;
|
||||
/** Pointers to all teams loaded in the cup round; these
|
||||
teams get passed to the fixture generation function
|
||||
together with the teams advancing from the previous round. */
|
||||
|
@ -688,7 +688,6 @@ free_cup_round(CupRound *cup_round)
|
||||
&g_array_index(cup_round->choose_teams, CupChooseTeam, j));
|
||||
|
||||
free_g_array(&cup_round->choose_teams);
|
||||
free_teams_array(&cup_round->teams, FALSE);
|
||||
g_ptr_array_free(cup_round->team_ptrs, TRUE);
|
||||
}
|
||||
}
|
||||
|
16
src/league.c
16
src/league.c
@ -457,6 +457,22 @@ country_get_league_sid(Country *country, const gchar *sid)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/** Get the leauge with \p sid from the list of all countries.
|
||||
* @returns A pointer to a League object or NULL if the cup is not found.
|
||||
*/
|
||||
League *
|
||||
bygfoot_get_league_sid(const gchar *sid)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < country_list->len; i++) {
|
||||
Country *country = g_ptr_array_index(country_list, i);
|
||||
League *league = country_get_league_sid(country, sid);
|
||||
if (league)
|
||||
return league;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/** Remove the team with the specified id from the teams
|
||||
array without freeing the memory (used in promotion/relegation). */
|
||||
void
|
||||
|
@ -109,6 +109,9 @@ country_get_cup_sid(const Country *country, const gchar *sid);
|
||||
League *
|
||||
country_get_league_sid(Country *country, const gchar *sid);
|
||||
|
||||
League *
|
||||
bygfoot_get_league_sid(const gchar *sid);
|
||||
|
||||
void
|
||||
league_remove_team_with_id(League *league, gint id);
|
||||
|
||||
|
@ -315,7 +315,7 @@ load_save_load_game(Bygfoot *bygfoot, const gchar* filename, gboolean create_mai
|
||||
_("Loading miscellaneous..."),
|
||||
PIC_TYPE_LOAD);
|
||||
|
||||
xml_loadsave_misc_read(dirname, prefix);
|
||||
xml_loadsave_misc_read(bygfoot, dirname, prefix);
|
||||
|
||||
if(debug > 60)
|
||||
g_print("load_save_load done \n");
|
||||
|
@ -89,9 +89,47 @@ start_new_game(void)
|
||||
start_write_variables();
|
||||
|
||||
start_generate_league_teams();
|
||||
|
||||
start_load_other_countries();
|
||||
|
||||
start_new_season();
|
||||
}
|
||||
|
||||
/** callback function for start_load_other_countries to load countries and
|
||||
* add them to the country_list.
|
||||
*/
|
||||
static load_country(gpointer country_file, gpointer user_data)
|
||||
{
|
||||
|
||||
GPtrArray *country_list = (GPtrArray*)user_data;
|
||||
Country *new_country = g_malloc0(sizeof(Country));
|
||||
gint i;
|
||||
|
||||
xml_country_read(country_file, new_country);
|
||||
for (i = 0; i < new_country->leagues->len; i++) {
|
||||
League *league = &g_array_index(new_country->leagues, League, i);
|
||||
gint j;
|
||||
for (j = 0; j < league->teams->len; j++) {
|
||||
Team *team = &g_array_index(league->teams, Team, j);
|
||||
team_generate_players_stadium(team, league->average_talent);
|
||||
}
|
||||
}
|
||||
|
||||
g_ptr_array_add(country_list, new_country);
|
||||
}
|
||||
|
||||
/** Load other countries that the user isn't playing, so we can easily reference
|
||||
* the cups and leagues.
|
||||
*/
|
||||
void
|
||||
start_load_other_countries()
|
||||
{
|
||||
GPtrArray *country_files = file_get_country_files();
|
||||
country_list = g_ptr_array_new();
|
||||
|
||||
g_ptr_array_foreach(country_files, load_country, country_list);
|
||||
}
|
||||
|
||||
/** Make new fixtures, nullify things etc. */
|
||||
void
|
||||
start_new_season(void)
|
||||
|
@ -28,6 +28,9 @@
|
||||
|
||||
#include "bygfoot.h"
|
||||
|
||||
void
|
||||
start_load_other_countries(void);
|
||||
|
||||
void
|
||||
start_new_game(void);
|
||||
|
||||
|
20
src/team.c
20
src/team.c
@ -243,11 +243,17 @@ team_of_id(gint id)
|
||||
if(g_array_index(lig(i).teams, Team, j).id == id)
|
||||
return &g_array_index(lig(i).teams, Team, j);
|
||||
|
||||
for(i=0;i<cps->len;i++)
|
||||
for(j = 0; j < cp(i).rounds->len; j++)
|
||||
for(k = 0; k < g_array_index(cp(i).rounds, CupRound, j).teams->len; k++)
|
||||
if(g_array_index(g_array_index(cp(i).rounds, CupRound, j).teams, Team, k).id == id)
|
||||
return &g_array_index(g_array_index(cp(i).rounds, CupRound, j).teams, Team, k);
|
||||
for (i = 0; i < country_list->len; i++) {
|
||||
Country *country = g_ptr_array_index(country_list, i);
|
||||
for (j = 0; j < country->leagues->len; j++) {
|
||||
League *league = &g_array_index(country->leagues, League, j);
|
||||
for (k = 0; k < league->teams->len; k++) {
|
||||
Team *team = &g_array_index(league->teams, Team, k);
|
||||
if (team->id == id)
|
||||
return team;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
main_exit_program(EXIT_POINTER_NOT_FOUND,
|
||||
"team_of_id: team with id %d not found.", id);
|
||||
@ -273,8 +279,8 @@ team_of_sid(const char *sid, const Country *country)
|
||||
const Cup *cup = &g_array_index(country->cups, Cup, i);
|
||||
for (j = 0; j < cup->rounds->len; j++) {
|
||||
const CupRound *round = &g_array_index(cup->rounds, CupRound, j);
|
||||
for (k = 0; k < round->teams->len; k++) {
|
||||
Team * team = &g_array_index(round->teams, Team, k);
|
||||
for (k = 0; k < round->team_ptrs->len; k++) {
|
||||
Team * team = g_ptr_array_index(round->team_ptrs, k);
|
||||
if (!strcmp(team->name, sid))
|
||||
return team;
|
||||
}
|
||||
|
@ -35,6 +35,12 @@
|
||||
*/
|
||||
Country country;
|
||||
|
||||
/**
|
||||
* List of all non-user countries.
|
||||
* @see Country
|
||||
*/
|
||||
GPtrArray *country_list;
|
||||
|
||||
/** The array of human players. @see #User */
|
||||
GArray *users;
|
||||
|
||||
|
15
src/xml.c
15
src/xml.c
@ -73,7 +73,7 @@ xml_load_users(const gchar *dirname, const gchar *basename)
|
||||
}
|
||||
|
||||
void
|
||||
xml_load_league(Bygfoot *bygfoot, const gchar *dirname, const gchar *basename)
|
||||
xml_load_league(Bygfoot *bygfoot, GArray *league_list, const gchar *dirname, const gchar *basename)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
printf("xml_load_league\n");
|
||||
@ -82,15 +82,16 @@ xml_load_league(Bygfoot *bygfoot, const gchar *dirname, const gchar *basename)
|
||||
gchar buf[SMALL], team_file[SMALL];
|
||||
League new = league_new(FALSE);
|
||||
gchar *prefix = g_strndup(basename, strlen(basename) - 4);
|
||||
League *league = NULL;
|
||||
|
||||
g_array_append_val(ligs, new);
|
||||
g_array_append_val(league_list, new);
|
||||
league = &g_array_index(league_list, League, league_list->len - 1);
|
||||
|
||||
sprintf(buf, "%s%s%s", dirname, G_DIR_SEPARATOR_S, basename);
|
||||
sprintf(team_file, "%s%s%s_teams.xml", dirname, G_DIR_SEPARATOR_S, prefix);
|
||||
xml_loadsave_league_read(buf, team_file, &lig(ligs->len - 1));
|
||||
xml_loadsave_league_read(buf, team_file, league);
|
||||
|
||||
sprintf(buf, _("Loading league: %s"),
|
||||
lig(ligs->len - 1).name);
|
||||
sprintf(buf, _("Loading league: %s"), league->name);
|
||||
|
||||
bygfoot_show_progress(bygfoot, bygfoot_get_progress_bar_fraction(bygfoot), buf,
|
||||
PIC_TYPE_LOAD);
|
||||
@ -99,10 +100,10 @@ xml_load_league(Bygfoot *bygfoot, const gchar *dirname, const gchar *basename)
|
||||
g_print("%s\n", buf);
|
||||
|
||||
sprintf(buf, "%s%s%s_fixtures.xml", dirname, G_DIR_SEPARATOR_S, prefix);
|
||||
xml_loadsave_fixtures_read(buf, lig(ligs->len - 1).fixtures);
|
||||
xml_loadsave_fixtures_read(buf, league->fixtures);
|
||||
|
||||
sprintf(buf, "%s%s%s_stat.xml", dirname, G_DIR_SEPARATOR_S, prefix);
|
||||
xml_loadsave_league_stat_read(buf, &lig(ligs->len - 1).stats);
|
||||
xml_loadsave_league_stat_read(buf, &league->stats);
|
||||
|
||||
g_free(prefix);
|
||||
}
|
||||
|
@ -104,7 +104,7 @@ void
|
||||
xml_load_users(const gchar *dirname, const gchar *basename);
|
||||
|
||||
void
|
||||
xml_load_league(Bygfoot *bygfoot, const gchar *dirname, const gchar *basename);
|
||||
xml_load_league(Bygfoot *bygfoot, GArray *league_list, const gchar *dirname, const gchar *basename);
|
||||
|
||||
void
|
||||
xml_load_cup(Bygfoot *bygfoot, Cup *cup, const gchar *dirname, const gchar *basename);
|
||||
|
@ -297,15 +297,6 @@ xml_loadsave_cup_text (GMarkupParseContext *context,
|
||||
new_round.name = g_strdup(buf);
|
||||
else if(state == TAG_CUP_ROUND_BYES)
|
||||
new_round.byes = int_value;
|
||||
else if(state == TAG_CUP_ROUND_TEAMS_FILE)
|
||||
{
|
||||
sprintf(buf2, "%s%s%s", dirname, G_DIR_SEPARATOR_S, buf);
|
||||
xml_loadsave_teams_read(buf2, new_round.teams);
|
||||
|
||||
for(i=0;i<new_round.teams->len;i++)
|
||||
g_ptr_array_add(new_cup->teams,
|
||||
&g_array_index(new_round.teams, Team, i));
|
||||
}
|
||||
else if(state == TAG_CUP_ROUND_TEAM_PTR_ID)
|
||||
g_ptr_array_add(new_round.team_ptrs, GINT_TO_POINTER(int_value));
|
||||
else if(state == TAG_CUP_ROUND_TABLE_FILE)
|
||||
@ -474,17 +465,6 @@ xml_loadsave_cup_write_round(FILE *fil, const gchar *prefix, const Cup *cup, gin
|
||||
|
||||
fprintf(fil, "<_%d>\n", TAG_CUP_ROUND);
|
||||
|
||||
if(cup_round->teams->len > 0)
|
||||
{
|
||||
sprintf(buf, "%s___cup_%d_round_%02d_teams.xml",
|
||||
basename, cup->id, round);
|
||||
xml_write_string(fil, buf, TAG_CUP_ROUND_TEAMS_FILE, I1);
|
||||
|
||||
sprintf(buf, "%s___cup_%d_round_%02d_teams.xml",
|
||||
prefix, cup->id, round);
|
||||
xml_loadsave_teams_write(buf, cup_round->teams);
|
||||
}
|
||||
|
||||
xml_write_string(fil, cup_round->name,
|
||||
TAG_CUP_ROUND_NAME, I1);
|
||||
xml_write_int(fil, cup_round->new_teams,
|
||||
|
@ -114,7 +114,7 @@ xml_loadsave_leagues_cups_text (GMarkupParseContext *context,
|
||||
buf[text_len] = '\0';
|
||||
|
||||
if(state == TAG_LEAGUE_FILE)
|
||||
xml_load_league(bygfoot, dir, buf);
|
||||
xml_load_league(bygfoot, ligs, dir, buf);
|
||||
else if(state == TAG_CUP_FILE)
|
||||
{
|
||||
new_cup = cup_new(FALSE);
|
||||
|
@ -51,9 +51,23 @@ enum XmlLoadSaveCountryTags
|
||||
TAG_MISC_VERSION,
|
||||
TAG_MISC_CURRENT_INTEREST,
|
||||
TAG_MISC_RESERVE_PROMOTION_RULES,
|
||||
TAG_COUNTRIES,
|
||||
TAG_COUNTRY,
|
||||
TAG_LEAGUES_CUPS,
|
||||
TAG_LEAGUE_FILE,
|
||||
TAG_CUP_FILE,
|
||||
TAG_END
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
Country *country;
|
||||
const gchar *directory;
|
||||
Bygfoot *bygfoot;
|
||||
|
||||
} MiscUserData;
|
||||
|
||||
|
||||
|
||||
gint state, countidx, oddidx, betidx;
|
||||
BetMatch new_bet;
|
||||
|
||||
@ -71,6 +85,7 @@ xml_loadsave_misc_start_element (GMarkupParseContext *context,
|
||||
|
||||
gint i;
|
||||
gint tag = xml_get_tag_from_name(element_name);
|
||||
MiscUserData *misc_user_data = (MiscUserData*)user_data;
|
||||
gboolean valid_tag = FALSE;
|
||||
|
||||
for(i=TAG_MISC;i<TAG_END;i++)
|
||||
@ -97,6 +112,13 @@ xml_loadsave_misc_start_element (GMarkupParseContext *context,
|
||||
betidx = (tag == TAG_MISC_BET1);
|
||||
}
|
||||
|
||||
if (tag == TAG_COUNTRY) {
|
||||
misc_user_data->country = g_malloc0(sizeof(Country));
|
||||
misc_user_data->country->leagues = g_array_new(FALSE, FALSE, sizeof(League));
|
||||
misc_user_data->country->cups = g_array_new(FALSE, FALSE, sizeof(Cup));
|
||||
misc_user_data->country->allcups = g_ptr_array_new();
|
||||
}
|
||||
|
||||
if(!valid_tag)
|
||||
debug_print_message("xml_loadsave_misc_start_element: unknown tag: %s; I'm in state %d\n",
|
||||
element_name, state);
|
||||
@ -112,6 +134,7 @@ xml_loadsave_misc_end_element (GMarkupParseContext *context,
|
||||
printf("xml_loadsave_misc_end_element\n");
|
||||
#endif
|
||||
|
||||
MiscUserData *misc_user_data = (MiscUserData*)user_data;
|
||||
gint tag = xml_get_tag_from_name(element_name);
|
||||
|
||||
if(tag == TAG_NAME ||
|
||||
@ -127,6 +150,10 @@ xml_loadsave_misc_end_element (GMarkupParseContext *context,
|
||||
tag == TAG_MISC_WEEK ||
|
||||
tag == TAG_MISC_WEEK_ROUND ||
|
||||
tag == TAG_MISC_BET0 ||
|
||||
tag == TAG_LEAGUES_CUPS ||
|
||||
tag == TAG_LEAGUE_FILE ||
|
||||
tag == TAG_CUP_FILE ||
|
||||
tag == TAG_COUNTRIES ||
|
||||
tag == TAG_MISC_BET1)
|
||||
{
|
||||
state = TAG_MISC;
|
||||
@ -142,8 +169,10 @@ xml_loadsave_misc_end_element (GMarkupParseContext *context,
|
||||
state = (betidx == 0) ? TAG_MISC_BET0 : TAG_MISC_BET1;
|
||||
if(tag == TAG_MISC_BET_ODD)
|
||||
oddidx++;
|
||||
}
|
||||
else if(tag != TAG_MISC)
|
||||
} else if (tag == TAG_COUNTRY) {
|
||||
g_ptr_array_add(country_list, misc_user_data->country);
|
||||
misc_user_data->country = &country;
|
||||
} else if(tag != TAG_MISC)
|
||||
debug_print_message("xml_loadsave_misc_end_element: unknown tag: %s; I'm in state %d\n",
|
||||
element_name, state);
|
||||
}
|
||||
@ -159,9 +188,11 @@ xml_loadsave_misc_text (GMarkupParseContext *context,
|
||||
printf("xml_loadsave_misc_text\n");
|
||||
#endif
|
||||
|
||||
MiscUserData *misc_user_data = (MiscUserData*)user_data;
|
||||
gchar buf[SMALL];
|
||||
gint int_value = -1;
|
||||
gfloat float_value = -1;
|
||||
gchar *dirname = (gchar*)user_data;
|
||||
|
||||
strncpy(buf, text, text_len);
|
||||
buf[text_len] = '\0';
|
||||
@ -170,15 +201,15 @@ xml_loadsave_misc_text (GMarkupParseContext *context,
|
||||
float_value = (gfloat)g_ascii_strtod(buf, NULL) / 10000;
|
||||
|
||||
if(state == TAG_NAME)
|
||||
misc_string_assign(&country.name, buf);
|
||||
misc_string_assign(&misc_user_data->country->name, buf);
|
||||
else if(state == TAG_MISC_RATING)
|
||||
country.rating = int_value;
|
||||
misc_user_data->country->rating = int_value;
|
||||
else if(state == TAG_SYMBOL)
|
||||
misc_string_assign(&country.symbol, buf);
|
||||
misc_string_assign(&misc_user_data->country->symbol, buf);
|
||||
else if(state == TAG_SID)
|
||||
misc_string_assign(&country.sid, buf);
|
||||
misc_string_assign(&misc_user_data->country->sid, buf);
|
||||
else if (state == TAG_MISC_RESERVE_PROMOTION_RULES)
|
||||
country.reserve_promotion_rules = int_value;
|
||||
misc_user_data->country->reserve_promotion_rules = int_value;
|
||||
else if(state == TAG_MISC_SEASON)
|
||||
season = int_value;
|
||||
else if(state == TAG_MISC_WEEK)
|
||||
@ -195,18 +226,29 @@ xml_loadsave_misc_text (GMarkupParseContext *context,
|
||||
new_bet.fix_id = int_value;
|
||||
else if(state == TAG_MISC_CURRENT_INTEREST)
|
||||
current_interest = float_value;
|
||||
else if(state == TAG_LEAGUE_FILE) {
|
||||
xml_load_league(misc_user_data->bygfoot, misc_user_data->country->leagues,
|
||||
misc_user_data->directory, buf);
|
||||
} else if (state == TAG_CUP_FILE) {
|
||||
Cup new_cup = cup_new(FALSE);
|
||||
g_array_append_val(misc_user_data->country->cups, new_cup);
|
||||
xml_load_cup(misc_user_data->bygfoot,
|
||||
&g_array_index(misc_user_data->country->cups, Cup, misc_user_data->country->cups->len - 1),
|
||||
misc_user_data->directory, buf);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** Read a country xml file. */
|
||||
void
|
||||
xml_loadsave_misc_read(const gchar *dirname, const gchar *basename)
|
||||
xml_loadsave_misc_read(Bygfoot *bygfoot, const gchar *dirname, const gchar *basename)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
printf("xml_loadsave_misc_read\n");
|
||||
#endif
|
||||
|
||||
gchar file[SMALL];
|
||||
MiscUserData user_data = { &country, dirname, bygfoot};
|
||||
GMarkupParser parser = {xml_loadsave_misc_start_element,
|
||||
xml_loadsave_misc_end_element,
|
||||
xml_loadsave_misc_text, NULL, NULL};
|
||||
@ -218,7 +260,7 @@ xml_loadsave_misc_read(const gchar *dirname, const gchar *basename)
|
||||
sprintf(file, "%s%s%s___misc.xml", dirname, G_DIR_SEPARATOR_S, basename);
|
||||
|
||||
context =
|
||||
g_markup_parse_context_new(&parser, 0, NULL, NULL);
|
||||
g_markup_parse_context_new(&parser, 0, &user_data, NULL);
|
||||
|
||||
if(!g_file_get_contents(file, &file_contents, &length, &error))
|
||||
{
|
||||
@ -229,6 +271,8 @@ xml_loadsave_misc_read(const gchar *dirname, const gchar *basename)
|
||||
g_ptr_array_free(acps, TRUE);
|
||||
acps = g_ptr_array_new();
|
||||
|
||||
g_ptr_array_free(country_list, TRUE);
|
||||
country_list = g_ptr_array_new();
|
||||
free_bets(TRUE);
|
||||
|
||||
if(g_markup_parse_context_parse(context, file_contents, length, &error))
|
||||
@ -281,6 +325,8 @@ xml_loadsave_misc_write(const gchar *prefix)
|
||||
|
||||
xml_loadsave_misc_write_bets(fil);
|
||||
|
||||
xml_loadsave_misc_write_countries(fil, prefix);
|
||||
|
||||
fprintf(fil, "</_%d>\n", TAG_MISC);
|
||||
fclose(fil);
|
||||
}
|
||||
@ -307,3 +353,53 @@ xml_loadsave_misc_write_bets(FILE *fil)
|
||||
fprintf(fil, "%s</_%d>\n", I0, (i == 0) ? TAG_MISC_BET0 : TAG_MISC_BET1);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
xml_loadsave_misc_write_country(const Country *country, FILE *fil,
|
||||
const gchar *prefix)
|
||||
{
|
||||
gchar buf[SMALL];
|
||||
gchar *basename = g_path_get_basename(prefix);
|
||||
gint i;
|
||||
|
||||
fprintf(fil, "%s<_%d>\n", I1, TAG_COUNTRY);
|
||||
xml_write_string(fil, country->name, TAG_NAME, I0);
|
||||
xml_write_string(fil, country->symbol, TAG_SYMBOL, I0);
|
||||
xml_write_string(fil, country->sid, TAG_SID, I0);
|
||||
xml_write_int(fil, country->reserve_promotion_rules, TAG_MISC_RESERVE_PROMOTION_RULES, I0);
|
||||
xml_write_int(fil, country->rating, TAG_MISC_RATING, I0);
|
||||
|
||||
fprintf(fil, "%s<_%d>\n", I2, TAG_LEAGUES_CUPS);
|
||||
for (i = 0; i < country->leagues->len; i++) {
|
||||
const League *league = &g_array_index(country->leagues, League, i);
|
||||
xml_loadsave_league_write(prefix, league);
|
||||
sprintf(buf, "%s___league_%d.xml", basename, league->id);
|
||||
xml_write_string(fil, buf, TAG_LEAGUE_FILE, I3);
|
||||
}
|
||||
|
||||
for (i = 0; i < country->cups->len; i++) {
|
||||
const Cup *cup = &g_array_index(country->cups, Cup, i);
|
||||
xml_loadsave_cup_write(prefix, cup);
|
||||
sprintf(buf, "%s___cup_%d.xml", basename, cup->id);
|
||||
xml_write_string(fil, buf, TAG_CUP_FILE, I3);
|
||||
}
|
||||
|
||||
fprintf(fil, "%s</_%d>\n", I2, TAG_LEAGUES_CUPS);
|
||||
fprintf(fil, "%s</_%d>\n", I1, TAG_COUNTRY);
|
||||
}
|
||||
|
||||
/** Write non-user countries to the file. */
|
||||
void
|
||||
xml_loadsave_misc_write_countries(FILE *fil, const gchar *prefix)
|
||||
{
|
||||
gint i;
|
||||
|
||||
fprintf(fil, "%s<!-- COUNTRIES--><_%d>\n", I0, TAG_COUNTRIES);
|
||||
|
||||
for (i = 0; i < country_list->len; i++) {
|
||||
const Country *country = g_ptr_array_index(country_list, i);
|
||||
xml_loadsave_misc_write_country(country, fil, prefix);
|
||||
}
|
||||
|
||||
fprintf(fil, "%s</_%d>\n", I0, TAG_COUNTRIES);
|
||||
}
|
||||
|
@ -29,7 +29,7 @@
|
||||
#include "bygfoot.h"
|
||||
|
||||
void
|
||||
xml_loadsave_misc_read(const gchar *dirname, const gchar *basename);
|
||||
xml_loadsave_misc_read(Bygfoot *bygfoot, const gchar *dirname, const gchar *basename);
|
||||
|
||||
void
|
||||
xml_loadsave_misc_write(const gchar *prefix);
|
||||
@ -37,4 +37,6 @@ xml_loadsave_misc_write(const gchar *prefix);
|
||||
void
|
||||
xml_loadsave_misc_write_bets(FILE *fil);
|
||||
|
||||
void
|
||||
xml_loadsave_misc_write_countries(FILE *fil, const gchar *prefix);
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user