2004-12-30 17:48:19 +01:00
|
|
|
#include "cup.h"
|
2005-01-09 21:21:22 +01:00
|
|
|
#include "file.h"
|
2004-12-30 17:48:19 +01:00
|
|
|
#include "fixture.h"
|
2005-03-10 21:59:39 +01:00
|
|
|
#include "gui.h"
|
2004-12-30 17:48:19 +01:00
|
|
|
#include "league.h"
|
2005-03-03 13:46:48 +01:00
|
|
|
#include "live_game.h"
|
2004-12-30 17:48:19 +01:00
|
|
|
#include "main.h"
|
|
|
|
#include "maths.h"
|
|
|
|
#include "player.h"
|
|
|
|
#include "start_end.h"
|
2005-03-03 13:46:48 +01:00
|
|
|
#include "table.h"
|
2004-12-30 17:48:19 +01:00
|
|
|
#include "team.h"
|
|
|
|
#include "transfer.h"
|
2005-03-03 13:46:48 +01:00
|
|
|
#include "user.h"
|
2004-12-30 17:48:19 +01:00
|
|
|
#include "variables.h"
|
|
|
|
#include "xml_name.h"
|
|
|
|
|
2005-03-03 13:46:48 +01:00
|
|
|
/** Prototype of a function called at the start or
|
|
|
|
end of a week round. */
|
|
|
|
typedef void(*WeekFunc)(void);
|
|
|
|
|
|
|
|
/** Array of functions called when a week round
|
|
|
|
is ended. */
|
|
|
|
WeekFunc end_week_round_funcs[] =
|
|
|
|
{end_week_round_results, end_week_round_sort_tables,
|
|
|
|
end_week_round_update_fixtures, NULL};
|
|
|
|
|
|
|
|
/** Array of functions called when a week round
|
|
|
|
is started. */
|
|
|
|
WeekFunc start_week_round_funcs[] =
|
|
|
|
{NULL};
|
|
|
|
|
|
|
|
/** Array of functions called when a week
|
|
|
|
is started. */
|
|
|
|
WeekFunc start_week_funcs[] = {NULL};
|
|
|
|
|
2004-12-30 17:48:19 +01:00
|
|
|
/** Generate the teams etc. */
|
|
|
|
void
|
|
|
|
start_new_game(void)
|
|
|
|
{
|
|
|
|
xml_name_read(PLAYER_NAMES_FILE, -1);
|
2005-01-09 21:21:22 +01:00
|
|
|
start_write_variables();
|
2004-12-30 17:48:19 +01:00
|
|
|
start_generate_league_teams();
|
|
|
|
start_new_season();
|
|
|
|
xml_name_read(PLAYER_NAMES_FILE, 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Make new fixtures, nullify things etc. */
|
|
|
|
void
|
|
|
|
start_new_season(void)
|
|
|
|
{
|
2005-01-09 21:21:22 +01:00
|
|
|
gint i;
|
|
|
|
|
|
|
|
xml_name_read(PLAYER_NAMES_FILE, 1000);
|
2004-12-30 17:48:19 +01:00
|
|
|
start_load_cup_teams();
|
2005-01-09 21:21:22 +01:00
|
|
|
|
|
|
|
for(i=0;i<ligs->len;i++)
|
|
|
|
fixture_write_league_fixtures(&lig(i));
|
|
|
|
|
|
|
|
for(i=0;i<cps->len;i++)
|
2005-03-08 09:25:46 +01:00
|
|
|
{
|
2005-01-09 21:21:22 +01:00
|
|
|
fixture_write_cup_fixtures(&cp(i));
|
2005-03-08 09:25:46 +01:00
|
|
|
/*d*/
|
|
|
|
/* printf("%s %d %d %d\n", */
|
|
|
|
/* cp(i).name->str, cp(i).id, */
|
|
|
|
/* g_array_index(cp(i).fixtures, Fixture, 0).week_number, */
|
|
|
|
/* g_array_index(cp(i).fixtures, Fixture, 0).week_round_number); */
|
|
|
|
}
|
2004-12-30 17:48:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Fill some global variables with default values at the
|
|
|
|
beginning of a new game. */
|
|
|
|
void
|
|
|
|
start_write_variables(void)
|
|
|
|
{
|
2005-01-09 21:21:22 +01:00
|
|
|
gint i;
|
|
|
|
|
2004-12-30 17:48:19 +01:00
|
|
|
season = week = week_round = 1;
|
2005-03-03 13:46:48 +01:00
|
|
|
current_user = 0;
|
2005-03-08 09:25:46 +01:00
|
|
|
/* week=24; week_round = 2; */
|
2005-01-09 21:21:22 +01:00
|
|
|
transfer_list = g_array_new(FALSE, FALSE, sizeof(TransferPlayer));
|
2005-03-03 13:46:48 +01:00
|
|
|
|
|
|
|
for(i=0;i<users->len;i++)
|
|
|
|
file_load_user_conf_file(&usr(i));
|
2004-12-30 17:48:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Generate the teams in the leagues. */
|
|
|
|
void
|
|
|
|
start_generate_league_teams(void)
|
|
|
|
{
|
|
|
|
gint i, j;
|
|
|
|
Team *tm;
|
|
|
|
|
|
|
|
if(ligs->len == 0)
|
2005-01-09 21:21:22 +01:00
|
|
|
main_exit_program(EXIT_NO_LEAGUES,
|
|
|
|
"start_generate_league_teams: no leagues found. there must be at least one league in the game.\n");
|
2004-12-30 17:48:19 +01:00
|
|
|
|
|
|
|
for(i=0;i<ligs->len;i++)
|
|
|
|
{
|
|
|
|
for(j=0;j<lig(i).teams->len;j++)
|
|
|
|
{
|
|
|
|
tm = &g_array_index(lig(i).teams, Team, j);
|
|
|
|
tm->stadium.capacity =
|
|
|
|
math_gauss_disti((gint)(lig(i).average_capacity * 0.66),
|
|
|
|
(gint)(lig(i).average_capacity * 1.33));
|
|
|
|
|
|
|
|
team_generate_players(tm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Load the names from the xml files for the cups
|
|
|
|
and generate the teams. */
|
|
|
|
void
|
|
|
|
start_load_cup_teams(void)
|
|
|
|
{
|
|
|
|
gint i, j;
|
|
|
|
|
|
|
|
for(i=0;i<cps->len;i++)
|
|
|
|
if(cp(i).type == CUP_TYPE_INTERNATIONAL)
|
|
|
|
{
|
|
|
|
cup_load_choose_teams(&cp(i));
|
|
|
|
cup_load_choose_team_user(&cp(i));
|
2005-03-03 13:46:48 +01:00
|
|
|
}
|
|
|
|
}
|
2004-12-30 17:48:19 +01:00
|
|
|
|
2005-03-03 13:46:48 +01:00
|
|
|
/** End a week round. */
|
|
|
|
void
|
|
|
|
end_week_round(void)
|
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
gboolean new_week = TRUE;
|
|
|
|
WeekFunc *end_func = end_week_round_funcs;
|
|
|
|
|
|
|
|
while(*end_func != NULL)
|
|
|
|
{
|
|
|
|
(*end_func)();
|
|
|
|
end_func++;
|
|
|
|
}
|
|
|
|
|
|
|
|
week_round++;
|
|
|
|
|
|
|
|
for(i=0;i<ligs->len;i++)
|
|
|
|
if(query_fixture_in_week_round(lig(i).id, week, week_round))
|
|
|
|
{
|
|
|
|
new_week = FALSE;
|
|
|
|
break;
|
2004-12-30 17:48:19 +01:00
|
|
|
}
|
2005-03-03 13:46:48 +01:00
|
|
|
|
|
|
|
for(i=0;i<cps->len;i++)
|
|
|
|
if(query_fixture_in_week_round(cp(i).id, week, week_round))
|
|
|
|
{
|
|
|
|
new_week = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(new_week)
|
|
|
|
{
|
|
|
|
week++;
|
|
|
|
week_round = 1;
|
|
|
|
|
|
|
|
start_week();
|
|
|
|
}
|
|
|
|
|
|
|
|
start_week_round();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Calculate the match results of a week round. */
|
|
|
|
void
|
|
|
|
end_week_round_results(void)
|
|
|
|
{
|
|
|
|
gint i, j;
|
|
|
|
|
|
|
|
if(week_round == 1)
|
|
|
|
{
|
|
|
|
for(i=0;i<ligs->len;i++)
|
|
|
|
for(j=0;j<lig(i).fixtures->len;j++)
|
|
|
|
if(g_array_index(lig(i).fixtures, Fixture, j).week_number == week &&
|
|
|
|
g_array_index(lig(i).fixtures, Fixture, j).week_round_number == week_round &&
|
|
|
|
g_array_index(lig(i).fixtures, Fixture, j).attendance == -1)
|
|
|
|
{
|
|
|
|
live_game_calculate_fixture(&g_array_index(lig(i).fixtures, Fixture, j));
|
|
|
|
/*d*/
|
|
|
|
/* printf("%d %d %d %25s %2d - %2d %-25s\n", week, week_round, */
|
|
|
|
/* g_array_index(lig(i).fixtures, Fixture, j).clid, */
|
|
|
|
/* g_array_index(lig(i).fixtures, Fixture, j).teams[0]->name->str, */
|
|
|
|
/* g_array_index(lig(i).fixtures, Fixture, j).result[0][0], */
|
|
|
|
/* g_array_index(lig(i).fixtures, Fixture, j).result[1][0], */
|
|
|
|
/* g_array_index(lig(i).fixtures, Fixture, j).teams[1]->name->str); */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(i=0;i<cps->len;i++)
|
|
|
|
for(j=0;j<cp(i).fixtures->len;j++)
|
|
|
|
if(g_array_index(cp(i).fixtures, Fixture, j).week_number == week &&
|
|
|
|
g_array_index(cp(i).fixtures, Fixture, j).week_round_number == week_round &&
|
2005-03-08 09:25:46 +01:00
|
|
|
g_array_index(cp(i).fixtures, Fixture, j).attendance == -1)
|
2005-03-03 13:46:48 +01:00
|
|
|
{
|
|
|
|
live_game_calculate_fixture(&g_array_index(cp(i).fixtures, Fixture, j));
|
|
|
|
/*d*/
|
2005-03-10 21:59:39 +01:00
|
|
|
/* printf("%d %d %d %25s %d %d %d - %d %d %d %-25s\n", week, week_round, */
|
|
|
|
/* g_array_index(cp(i).fixtures, Fixture, j).clid, */
|
|
|
|
/* g_array_index(cp(i).fixtures, Fixture, j).teams[0]->name->str, */
|
|
|
|
/* g_array_index(cp(i).fixtures, Fixture, j).result[0][0], */
|
|
|
|
/* g_array_index(cp(i).fixtures, Fixture, j).result[0][1], */
|
|
|
|
/* g_array_index(cp(i).fixtures, Fixture, j).result[0][2], */
|
|
|
|
/* g_array_index(cp(i).fixtures, Fixture, j).result[1][0], */
|
|
|
|
/* g_array_index(cp(i).fixtures, Fixture, j).result[1][1], */
|
|
|
|
/* g_array_index(cp(i).fixtures, Fixture, j).result[1][2], */
|
|
|
|
/* g_array_index(cp(i).fixtures, Fixture, j).teams[1]->name->str); */
|
2005-03-03 13:46:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Sort league and cup tables. */
|
|
|
|
void
|
|
|
|
end_week_round_sort_tables(void)
|
|
|
|
{
|
|
|
|
gint i, j;
|
|
|
|
|
|
|
|
for(i=0;i<ligs->len;i++)
|
|
|
|
if(query_fixture_in_week_round(lig(i).id, week, week_round))
|
|
|
|
g_array_sort_with_data(lig(i).table.elements,
|
|
|
|
(GCompareDataFunc)table_element_compare_func,
|
|
|
|
GINT_TO_POINTER(lig(i).id));
|
|
|
|
|
|
|
|
for(i=0;i<cps->len;i++)
|
|
|
|
if(cp(i).tables != NULL && cp(i).tables->len != 0 &&
|
|
|
|
query_fixture_in_week_round(cp(i).id, week, week_round) &&
|
|
|
|
g_array_index(cp(i).fixtures, Fixture, cp(i).fixtures->len - 1).round ==
|
|
|
|
g_array_index(cp(i).tables, Table, 0).round)
|
|
|
|
for(j=0;j<cp(i).tables->len;j++)
|
|
|
|
g_array_sort_with_data(g_array_index(cp(i).tables, Table, j).elements,
|
|
|
|
(GCompareDataFunc)table_element_compare_func,
|
|
|
|
GINT_TO_POINTER(cp(i).id));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Update cup fixtures. */
|
|
|
|
void
|
|
|
|
end_week_round_update_fixtures(void)
|
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
for(i=0;i<cps->len;i++)
|
|
|
|
if(cp(i).next_fixture_update_week == week &&
|
|
|
|
cp(i).next_fixture_update_week_round == week_round)
|
|
|
|
fixture_update(&cp(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Start a new week round. */
|
|
|
|
void
|
|
|
|
start_week_round(void)
|
|
|
|
{
|
|
|
|
WeekFunc *start_func = start_week_round_funcs;
|
|
|
|
|
|
|
|
while(*start_func != NULL)
|
|
|
|
{
|
|
|
|
(*start_func)();
|
|
|
|
start_func++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Start a new week. */
|
|
|
|
void
|
|
|
|
start_week(void)
|
|
|
|
{
|
|
|
|
WeekFunc *start_func = start_week_funcs;
|
|
|
|
|
|
|
|
while(*start_func != NULL)
|
|
|
|
{
|
|
|
|
(*start_func)();
|
|
|
|
start_func++;
|
|
|
|
}
|
2004-12-30 17:48:19 +01:00
|
|
|
}
|