Fix for tracker id 2942033: Spectator Bugs in Multiplayer-Games

Patch by rookie86
This commit is contained in:
gunnar_g 2010-01-29 07:38:11 +00:00
parent 8e7917bd5f
commit cad59262ce
1 changed files with 285 additions and 291 deletions

View File

@ -144,25 +144,25 @@ league_cup_get_index_from_clid(gint clid)
if(clid < ID_CUP_START)
{
for(i=0;i<ligs->len;i++)
if(lig(i).id == clid)
{
index = i;
break;
}
for(i=0;i<ligs->len;i++)
if(lig(i).id == clid)
{
index = i;
break;
}
}
else
for(i=0;i<cps->len;i++)
if(cp(i).id == clid)
{
index = i;
break;
}
for(i=0;i<cps->len;i++)
if(cp(i).id == clid)
{
index = i;
break;
}
if(index == -1)
main_exit_program(EXIT_POINTER_NOT_FOUND,
"league_cup_get_index_from_clid: couldn't find league or cup with id %d\n",
clid);
main_exit_program(EXIT_POINTER_NOT_FOUND,
"league_cup_get_index_from_clid: couldn't find league or cup with id %d\n",
clid);
return index;
}
@ -180,12 +180,12 @@ league_from_clid(gint clid)
gint i;
for(i=0;i<ligs->len;i++)
if(lig(i).id == clid)
return &lig(i);
if(lig(i).id == clid)
return &lig(i);
main_exit_program(EXIT_POINTER_NOT_FOUND,
"league_from_clid: didn't find league with id %d\n",
clid);
"league_from_clid: didn't find league with id %d\n",
clid);
return NULL;
}
@ -205,42 +205,42 @@ league_cup_get_next_clid(gint clid, gboolean count_inactive)
if(clid < ID_CUP_START)
{
for(i=0;i<ligs->len;i++)
if(lig(i).id == clid)
break;
for(i=0;i<ligs->len;i++)
if(lig(i).id == clid)
break;
if(i != ligs->len - 1)
{
if(query_league_active(&lig(i + 1)) || count_inactive)
return_value = lig(i + 1).id;
else
return_value = league_cup_get_next_clid(lig(i + 1).id, count_inactive);
}
else if(acps->len > 0)
return_value = acp(0)->id;
else
{
if(query_league_active(&lig(0)) || count_inactive)
return_value = lig(0).id;
else
return_value = league_cup_get_next_clid(lig(0).id, count_inactive);
}
if(i != ligs->len - 1)
{
if(query_league_active(&lig(i + 1)) || count_inactive)
return_value = lig(i + 1).id;
else
return_value = league_cup_get_next_clid(lig(i + 1).id, count_inactive);
}
else if(acps->len > 0)
return_value = acp(0)->id;
else
{
if(query_league_active(&lig(0)) || count_inactive)
return_value = lig(0).id;
else
return_value = league_cup_get_next_clid(lig(0).id, count_inactive);
}
}
else
{
for(i=0;i<acps->len;i++)
if(acp(i)->id == clid)
break;
for(i=0;i<acps->len;i++)
if(acp(i)->id == clid)
break;
if(i != acps->len - 1)
return_value = acp(i + 1)->id;
else
{
if(query_league_active(&lig(0)) || count_inactive)
return_value = lig(0).id;
else
return_value = league_cup_get_next_clid(lig(0).id, count_inactive);
}
if(i != acps->len - 1)
return_value = acp(i + 1)->id;
else
{
if(query_league_active(&lig(0)) || count_inactive)
return_value = lig(0).id;
else
return_value = league_cup_get_next_clid(lig(0).id, count_inactive);
}
}
return return_value;
@ -261,42 +261,42 @@ league_cup_get_previous_clid(gint clid, gboolean count_inactive)
if(clid < ID_CUP_START)
{
for(i=ligs->len - 1;i>=0;i--)
if(lig(i).id == clid)
break;
for(i=ligs->len - 1;i>=0;i--)
if(lig(i).id == clid)
break;
if(i != 0)
{
if(query_league_active(&lig(i - 1)) || count_inactive)
return_value = lig(i - 1).id;
else
return_value = league_cup_get_previous_clid(lig(i - 1).id, count_inactive);
}
else if(acps->len > 0)
return_value = acp(acps->len - 1)->id;
else
{
if(query_league_active(&lig(ligs->len - 1)) || count_inactive)
return_value = lig(ligs->len - 1).id;
else
return_value = league_cup_get_previous_clid(lig(ligs->len - 1).id, count_inactive);
}
if(i != 0)
{
if(query_league_active(&lig(i - 1)) || count_inactive)
return_value = lig(i - 1).id;
else
return_value = league_cup_get_previous_clid(lig(i - 1).id, count_inactive);
}
else if(acps->len > 0)
return_value = acp(acps->len - 1)->id;
else
{
if(query_league_active(&lig(ligs->len - 1)) || count_inactive)
return_value = lig(ligs->len - 1).id;
else
return_value = league_cup_get_previous_clid(lig(ligs->len - 1).id, count_inactive);
}
}
else
{
for(i=acps->len - 1;i>=0;i--)
if(acp(i)->id == clid)
break;
for(i=acps->len - 1;i>=0;i--)
if(acp(i)->id == clid)
break;
if(i != 0)
return_value = acp(i - 1)->id;
else
{
if(query_league_active(&lig(ligs->len - 1)) || count_inactive)
return_value = lig(ligs->len - 1).id;
else
return_value = league_cup_get_previous_clid(lig(ligs->len - 1).id, count_inactive);
}
if(i != 0)
return_value = acp(i - 1)->id;
else
{
if(query_league_active(&lig(ligs->len - 1)) || count_inactive)
return_value = lig(ligs->len - 1).id;
else
return_value = league_cup_get_previous_clid(lig(ligs->len - 1).id, count_inactive);
}
}
return return_value;
@ -316,10 +316,10 @@ league_cup_get_next_fixture(gint clid, gint week_number, gint week_round_number)
GArray *fixtures = league_cup_get_fixtures(clid);
for(i=0;i<fixtures->len;i++)
if(g_array_index(fixtures, Fixture, i).week_number > week_number ||
(g_array_index(fixtures, Fixture, i).week_number == week_number &&
g_array_index(fixtures, Fixture, i).week_round_number >= week_round_number))
return &g_array_index(fixtures, Fixture, i);
if(g_array_index(fixtures, Fixture, i).week_number > week_number ||
(g_array_index(fixtures, Fixture, i).week_number == week_number &&
g_array_index(fixtures, Fixture, i).week_round_number >= week_round_number))
return &g_array_index(fixtures, Fixture, i);
return NULL;
}
@ -338,10 +338,10 @@ league_cup_get_previous_fixture(gint clid, gint week_number, gint week_round_num
GArray *fixtures = league_cup_get_fixtures(clid);
for(i=fixtures->len - 1;i>=0;i--)
if(g_array_index(fixtures, Fixture, i).week_number < week_number ||
(g_array_index(fixtures, Fixture, i).week_number == week_number &&
g_array_index(fixtures, Fixture, i).week_round_number < week_round_number))
return &g_array_index(fixtures, Fixture, i);
if(g_array_index(fixtures, Fixture, i).week_number < week_number ||
(g_array_index(fixtures, Fixture, i).week_number == week_number &&
g_array_index(fixtures, Fixture, i).week_round_number < week_round_number))
return &g_array_index(fixtures, Fixture, i);
return NULL;
}
@ -362,26 +362,20 @@ league_cup_average_capacity(gint clid)
const GPtrArray *teamsp = NULL;
if(clid < ID_CUP_START)
{
teams = (GArray*)league_cup_get_teams(clid);
for(i=0;i<teams->len;i++)
if(team_is_user(&g_array_index(teams, Team, i)) == -1)
{
sum += g_array_index(teams, Team, i).stadium.capacity;
cnt++;
}
{
teams = (GArray*)league_cup_get_teams(clid);
for(i=0;i<teams->len;i++)
sum += g_array_index(teams, Team, i).stadium.capacity;
cnt++;
}
else
{
teamsp = (GPtrArray*)league_cup_get_teams(clid);
for(i=0;i<teamsp->len;i++)
if(team_is_user((Team*)g_ptr_array_index(teamsp, i)) == -1)
{
sum += ((Team*)g_ptr_array_index(teamsp, i))->stadium.capacity;
cnt++;
}
teamsp = (GPtrArray*)league_cup_get_teams(clid);
for(i=0;i<teamsp->len;i++)
sum += ((Team*)g_ptr_array_index(teamsp, i))->stadium.capacity;
cnt++;
}
return sum / (gfloat)cnt;
}
@ -396,11 +390,11 @@ league_index_from_sid(const gchar *sid)
gint i;
for(i=0;i<ligs->len;i++)
if(strcmp(lig(i).sid, sid) == 0)
return i;
if(strcmp(lig(i).sid, sid) == 0)
return i;
main_exit_program(EXIT_INT_NOT_FOUND,
"league_index_from_sid: no index found for sid '%s'.\n", sid);
"league_index_from_sid: no index found for sid '%s'.\n", sid);
return -1;
}
@ -417,14 +411,14 @@ league_remove_team_with_id(League *league, gint id)
gint i;
for(i=0;i<league->teams->len;i++)
if(g_array_index(league->teams, Team, i).id == id)
{
g_array_remove_index(league->teams, i);
return;
}
if(g_array_index(league->teams, Team, i).id == id)
{
g_array_remove_index(league->teams, i);
return;
}
debug_print_message("league_remove_team_with_id: team with id %d in league %s not found\n",
id, league->name);
id, league->name);
}
@ -441,41 +435,41 @@ league_season_start(League *league)
gint i, j;
gint idx = league_index_from_sid(league->sid);
gboolean user_champ =
(team_is_user(
team_of_id(
g_array_index(league_table((&lig(0)))->elements, TableElement, 0).team_id)) != -1);
(team_is_user(
team_of_id(
g_array_index(league_table((&lig(0)))->elements, TableElement, 0).team_id)) != -1);
gboolean league_above_talent =
(team_get_average_talents(league->teams) > league->average_talent *
const_float("float_season_end_league_above_talent_factor") && !user_champ);
(team_get_average_talents(league->teams) > league->average_talent *
const_float("float_season_end_league_above_talent_factor") && !user_champ);
gboolean team_is_top = FALSE;
Team *tm = NULL;
gfloat team_change_lower =
const_float("float_season_end_team_change_lower"),
user_champ_addition =
const_float("float_season_end_user_champ_addition"),
user_champ_best_teams_addition =
const_float("float_season_end_user_champ_best_teams_addition"),
team_change_upper =
const_float("float_season_end_team_change_upper");
const_float("float_season_end_team_change_lower"),
user_champ_addition =
const_float("float_season_end_user_champ_addition"),
user_champ_best_teams_addition =
const_float("float_season_end_user_champ_best_teams_addition"),
team_change_upper =
const_float("float_season_end_team_change_upper");
gint user_champ_best_teams_limit =
const_int("int_season_end_user_champ_best_teams_limit");
const_int("int_season_end_user_champ_best_teams_limit");
gfloat team_change_factor = 0;
if(user_champ)
{
tm = team_of_id(
g_array_index(league_table((&lig(0)))->elements, TableElement, 0).team_id);
tm->luck = MAX(tm->luck * const_float("float_season_end_user_champ_luck_factor"),
const_float("float_luck_limit"));
tm = team_of_id(
g_array_index(league_table((&lig(0)))->elements, TableElement, 0).team_id);
tm->luck = MAX(tm->luck * const_float("float_season_end_user_champ_luck_factor"),
const_float("float_luck_limit"));
}
else
for(i=0;i<users->len;i++)
if(usr(i).tm->luck < 1)
usr(i).tm->luck =
MIN(usr(i).tm->luck * const_float("float_season_end_user_champ_luck_factor_regen"), 1);
for(i=0;i<users->len;i++)
if(usr(i).tm->luck < 1)
usr(i).tm->luck =
MIN(usr(i).tm->luck * const_float("float_season_end_user_champ_luck_factor_regen"), 1);
/** Reset tables */
for(i = league->tables->len - 1; i >= 0; i--)
@ -487,38 +481,38 @@ league_season_start(League *league)
/*d*/
/* if(league == &lig(0)) */
/* printf("league %s av %.2f ab %d\n", league->name, */
/* team_get_average_talents(league->teams), league_above_talent); */
/* printf("league %s av %.2f ab %d\n", league->name, */
/* team_get_average_talents(league->teams), league_above_talent); */
for(i=0;i<league->teams->len;i++)
{
team_is_top =
(team_get_league_rank(&g_array_index(league->teams, Team, i), -1) <=
user_champ_best_teams_limit && idx == 0 && user_champ);
team_is_top =
(team_get_league_rank(&g_array_index(league->teams, Team, i), -1) <=
user_champ_best_teams_limit && idx == 0 && user_champ);
team_change_factor =
(team_is_user(&g_array_index(league->teams, Team, i)) == -1) *
math_rnd(team_change_lower + user_champ * user_champ_addition +
team_is_top * user_champ_best_teams_addition -
league_above_talent * (user_champ_addition / 2),
team_change_upper + user_champ * user_champ_addition +
team_is_top * user_champ_best_teams_addition -
league_above_talent * (user_champ_addition / 2));
team_change_factor =
(team_is_user(&g_array_index(league->teams, Team, i)) == -1) *
math_rnd(team_change_lower + user_champ * user_champ_addition +
team_is_top * user_champ_best_teams_addition -
league_above_talent * (user_champ_addition / 2),
team_change_upper + user_champ * user_champ_addition +
team_is_top * user_champ_best_teams_addition -
league_above_talent * (user_champ_addition / 2));
for(j=0;j<g_array_index(league->teams, Team, i).players->len;j++)
player_season_start(
&g_array_index(
g_array_index(
league->teams, Team, i).players, Player, j), team_change_factor);
for(j=0;j<g_array_index(league->teams, Team, i).players->len;j++)
player_season_start(
&g_array_index(
g_array_index(
league->teams, Team, i).players, Player, j), team_change_factor);
g_array_index(league->teams, Team, i).stadium.average_attendance =
g_array_index(league->teams, Team, i).stadium.games =
g_array_index(league->teams, Team, i).stadium.possible_attendance = 0;
g_array_index(league->teams, Team, i).stadium.average_attendance =
g_array_index(league->teams, Team, i).stadium.games =
g_array_index(league->teams, Team, i).stadium.possible_attendance = 0;
}
/* if(league == &lig(0)) */
/* printf("2 league %s av %.2f\n", league->name, */
/* team_get_average_talents(league->teams)); */
/* printf("2 league %s av %.2f\n", league->name, */
/* team_get_average_talents(league->teams)); */
}
/** Find out whether the team with specified rank in the league
@ -535,8 +529,8 @@ query_league_rank_in_prom_games(const League *league, gint rank)
const CupRound *cup_round = NULL;
for(i=0;i<ligs->len;i++)
if(query_league_has_prom_games((&lig(i))))
{
if(query_league_has_prom_games((&lig(i))))
{
for(l = 0; l < lig(i).prom_rel.prom_games->len; l++)
{
cup = cup_from_sid(g_array_index(lig(i).prom_rel.prom_games, PromGames, l).cup_sid);
@ -565,7 +559,7 @@ query_league_rank_in_prom_games(const League *league, gint rank)
}
}
}
}
}
return FALSE;
}
@ -581,8 +575,8 @@ query_league_matches_in_week(const League *league, gint week_number)
gint i;
for(i=0;i<league->fixtures->len;i++)
if(g_array_index(league->fixtures, Fixture, i).week_number == week_number)
return TRUE;
if(g_array_index(league->fixtures, Fixture, i).week_number == week_number)
return TRUE;
return FALSE;
}
@ -606,32 +600,32 @@ league_get_team_movements_prom_rel(const League *league, GArray *team_movements)
for(i=0;i<elements->len;i++)
{
elem = &g_array_index(elements, PromRelElement, i);
dest_sids = misc_separate_strings(
elem->dest_sid);
gint dest_idcs_int[dest_sids->len];
gint dest_idcs_order[dest_sids->len];
dest_sids = misc_separate_strings(
elem->dest_sid);
gint dest_idcs_int[dest_sids->len];
gint dest_idcs_order[dest_sids->len];
for(j=0;j<dest_sids->len;j++)
dest_idcs_int[j] =
league_index_from_sid((gchar*)g_ptr_array_index(dest_sids, j));
for(j=0;j<dest_sids->len;j++)
dest_idcs_int[j] =
league_index_from_sid((gchar*)g_ptr_array_index(dest_sids, j));
for(j=elem->ranks[0];
j<=elem->ranks[1]; j++)
{
dest_idcs = g_array_new(FALSE, FALSE, sizeof(gint));
math_generate_permutation(dest_idcs_order, 0, dest_sids->len - 1);
for(j=elem->ranks[0];
j<=elem->ranks[1]; j++)
{
dest_idcs = g_array_new(FALSE, FALSE, sizeof(gint));
math_generate_permutation(dest_idcs_order, 0, dest_sids->len - 1);
for(k=0;k<dest_sids->len;k++)
g_array_append_val(dest_idcs, dest_idcs_int[dest_idcs_order[k]]);
new_move.tm = *(g_array_index(g_array_index(league->tables, Table, elem->from_table).elements, TableElement, j - 1).team);
new_move.prom_rel_type = elem->type;
new_move.dest_idcs = dest_idcs;
new_move.dest_assigned = FALSE;
g_array_append_val(team_movements, new_move);
}
for(k=0;k<dest_sids->len;k++)
g_array_append_val(dest_idcs, dest_idcs_int[dest_idcs_order[k]]);
new_move.tm = *(g_array_index(g_array_index(league->tables, Table, elem->from_table).elements, TableElement, j - 1).team);
new_move.prom_rel_type = elem->type;
new_move.dest_idcs = dest_idcs;
new_move.dest_assigned = FALSE;
g_array_append_val(team_movements, new_move);
}
free_gchar_array(&dest_sids);
free_gchar_array(&dest_sids);
}
}
@ -650,38 +644,38 @@ league_get_team_movements_prom_games(const League *league, const PromGames *prom
gint i, j;
TeamMove new_move;
GPtrArray *dest_sids = (up) ?
misc_separate_strings(prom_games->dest_sid) :
misc_separate_strings(prom_games->loser_sid);
misc_separate_strings(prom_games->dest_sid) :
misc_separate_strings(prom_games->loser_sid);
GArray *dest_idcs = NULL;
gint dest_idcs_int[dest_sids->len];
gint dest_idcs_order[dest_sids->len];
gint start_idx = 0,
end_idx = prom_games->number_of_advance;
end_idx = prom_games->number_of_advance;
gint prom_type = PROM_REL_PROMOTION;
if(!up)
{
start_idx = prom_games->number_of_advance;
end_idx = prom_games_teams->len;
prom_type = PROM_REL_RELEGATION;
start_idx = prom_games->number_of_advance;
end_idx = prom_games_teams->len;
prom_type = PROM_REL_RELEGATION;
}
for(i=0;i<dest_sids->len;i++)
dest_idcs_int[i] =
league_index_from_sid((gchar*)g_ptr_array_index(dest_sids, i));
dest_idcs_int[i] =
league_index_from_sid((gchar*)g_ptr_array_index(dest_sids, i));
for(i=start_idx;i<end_idx;i++)
{
dest_idcs = g_array_new(FALSE, FALSE, sizeof(gint));
math_generate_permutation(dest_idcs_order, 0, dest_sids->len - 1);
for(j=0;j<dest_sids->len;j++)
g_array_append_val(dest_idcs, dest_idcs_int[dest_idcs_order[j]]);
{
dest_idcs = g_array_new(FALSE, FALSE, sizeof(gint));
math_generate_permutation(dest_idcs_order, 0, dest_sids->len - 1);
for(j=0;j<dest_sids->len;j++)
g_array_append_val(dest_idcs, dest_idcs_int[dest_idcs_order[j]]);
new_move.tm = *((Team*)g_ptr_array_index(prom_games_teams, i));
new_move.prom_rel_type = prom_type;
new_move.dest_idcs = dest_idcs;
new_move.dest_assigned = FALSE;
g_array_append_val(team_movements, new_move);
new_move.tm = *((Team*)g_ptr_array_index(prom_games_teams, i));
new_move.prom_rel_type = prom_type;
new_move.dest_idcs = dest_idcs;
new_move.dest_assigned = FALSE;
g_array_append_val(team_movements, new_move);
}
free_gchar_array(&dest_sids);
@ -717,13 +711,13 @@ league_get_team_movements(League *league, GArray *team_movements)
if(g_array_index(league->prom_rel.prom_games, PromGames, i).loser_sid != NULL)
league_get_team_movements_prom_games(league, &g_array_index(league->prom_rel.prom_games, PromGames, i),
team_movements, prom_games_teams, FALSE);
g_ptr_array_free(prom_games_teams, TRUE);
}
}
g_array_sort_with_data(league->teams, team_compare_func,
GINT_TO_POINTER(TEAM_COMPARE_LEAGUE_RANK + 100));
GINT_TO_POINTER(TEAM_COMPARE_LEAGUE_RANK + 100));
}
/** Find out whether there are unassigned TeamMoves in the array. */
@ -737,8 +731,8 @@ query_league_team_movements_unassigned(const GArray *team_movements)
gint i;
for(i=0;i<team_movements->len;i++)
if(!g_array_index(team_movements, TeamMove, i).dest_assigned)
return TRUE;
if(!g_array_index(team_movements, TeamMove, i).dest_assigned)
return TRUE;
return FALSE;
}
@ -755,9 +749,9 @@ query_league_team_movements_unassigned_single(const GArray *team_movements)
gint i;
for(i=0;i<team_movements->len;i++)
if(!g_array_index(team_movements, TeamMove, i).dest_assigned &&
g_array_index(team_movements, TeamMove, i).dest_idcs->len == 1)
return TRUE;
if(!g_array_index(team_movements, TeamMove, i).dest_assigned &&
g_array_index(team_movements, TeamMove, i).dest_idcs->len == 1)
return TRUE;
return FALSE;
}
@ -765,7 +759,7 @@ query_league_team_movements_unassigned_single(const GArray *team_movements)
/** Print out the movements to the console (debug function). */
void
league_team_movements_print(const GArray *team_movements,
const gint *league_size, const gint *league_cur_size)
const gint *league_size, const gint *league_cur_size)
{
#ifdef DEBUG
printf("league_team_movements_print\n");
@ -777,43 +771,43 @@ league_team_movements_print(const GArray *team_movements,
g_print("%-25s Dest \t\t Possible\n\n", "Team");
for(i=0;i<team_movements->len;i++)
{
tmove = &g_array_index(team_movements, TeamMove, i);
if(tmove->dest_assigned)
g_print("%-25s (%d) %s \t\t", tmove->tm.name,
league_from_clid(tmove->tm.clid)->layer,
lig(g_array_index(tmove->dest_idcs, gint, 0)).name);
else
g_print("%-25s (%d) UNASSIGNED \t\t", tmove->tm.name,
league_from_clid(tmove->tm.clid)->layer);
for(j=0;j<tmove->dest_idcs->len;j++)
g_print("%d ", g_array_index(tmove->dest_idcs, gint, j));
g_print("\n");
tmove = &g_array_index(team_movements, TeamMove, i);
if(tmove->dest_assigned)
g_print("%-25s (%d) %s \t\t", tmove->tm.name,
league_from_clid(tmove->tm.clid)->layer,
lig(g_array_index(tmove->dest_idcs, gint, 0)).name);
else
g_print("%-25s (%d) UNASSIGNED \t\t", tmove->tm.name,
league_from_clid(tmove->tm.clid)->layer);
for(j=0;j<tmove->dest_idcs->len;j++)
g_print("%d ", g_array_index(tmove->dest_idcs, gint, j));
g_print("\n");
}
g_print("%-20s Size Cursize\n", "League");
for(i=0;i<ligs->len;i++)
g_print("%-20s %d %d\n", lig(i).name, league_size[i],
league_cur_size[i]);
g_print("%-20s %d %d\n", lig(i).name, league_size[i],
league_cur_size[i]);
}
/** Compare two leagues when doing promotion/relegation. The league
that has fewer teams is preferred. */
gint
league_team_movements_compare_dest_idcs(gconstpointer a, gconstpointer b,
gpointer data)
gpointer data)
{
#ifdef DEBUG
printf("league_team_movements_compare_dest_idcs\n");
#endif
gint league_idx1 = *(gint*)a,
league_idx2 = *(gint*)b;
league_idx2 = *(gint*)b;
const gint *league_cur_size = (const gint*)data;
if(league_cur_size[league_idx1] < league_cur_size[league_idx2])
return -1;
return -1;
else if(league_cur_size[league_idx1] > league_cur_size[league_idx2])
return 1;
return 1;
return 0;
}
@ -823,7 +817,7 @@ league_team_movements_compare_dest_idcs(gconstpointer a, gconstpointer b,
the dest league is full. */
void
league_team_movements_assign_dest(GArray *team_movements, gint idx,
const gint *league_size, gint *league_cur_size)
const gint *league_size, gint *league_cur_size)
{
#ifdef DEBUG
printf("league_team_movements_assign_dest\n");
@ -833,61 +827,61 @@ league_team_movements_assign_dest(GArray *team_movements, gint idx,
TeamMove *tmove = &g_array_index(team_movements, TeamMove, idx);
if(debug > 60)
g_print("league_team_movements_assign_dest %s\n", tmove->tm.name);
g_print("league_team_movements_assign_dest %s\n", tmove->tm.name);
if(tmove->dest_idcs->len == 1)
dest_idx = g_array_index(tmove->dest_idcs, gint, 0);
dest_idx = g_array_index(tmove->dest_idcs, gint, 0);
else
{
g_array_sort_with_data(tmove->dest_idcs,
(GCompareDataFunc)league_team_movements_compare_dest_idcs,
(gpointer)league_cur_size);
dest_idx = g_array_index(tmove->dest_idcs, gint, 0);
g_array_sort_with_data(tmove->dest_idcs,
(GCompareDataFunc)league_team_movements_compare_dest_idcs,
(gpointer)league_cur_size);
dest_idx = g_array_index(tmove->dest_idcs, gint, 0);
}
league_cur_size[dest_idx]++;
if(league_cur_size[dest_idx] > league_size[dest_idx])
main_exit_program(EXIT_PROM_REL,
"league_team_movements_assign_dest: no room in league %s for team %s.",
lig(dest_idx).name, tmove->tm.name);
main_exit_program(EXIT_PROM_REL,
"league_team_movements_assign_dest: no room in league %s for team %s.",
lig(dest_idx).name, tmove->tm.name);
tmove->dest_assigned = TRUE;
if(debug > 60)
g_print("%s %d -> %d\n", tmove->tm.name,
league_from_clid(tmove->tm.clid)->layer,
league_from_clid(lig(dest_idx).id)->layer);
g_print("%s %d -> %d\n", tmove->tm.name,
league_from_clid(tmove->tm.clid)->layer,
league_from_clid(lig(dest_idx).id)->layer);
if(league_cur_size[dest_idx] == league_size[dest_idx])
for(i=0;i<team_movements->len;i++)
{
if(!g_array_index(team_movements, TeamMove, i).dest_assigned)
{
tmove = &g_array_index(team_movements, TeamMove, i);
for(j=tmove->dest_idcs->len - 1; j>= 0; j--)
if(g_array_index(tmove->dest_idcs, gint, j) == dest_idx)
g_array_remove_index(tmove->dest_idcs, j);
for(i=0;i<team_movements->len;i++)
{
if(!g_array_index(team_movements, TeamMove, i).dest_assigned)
{
tmove = &g_array_index(team_movements, TeamMove, i);
for(j=tmove->dest_idcs->len - 1; j>= 0; j--)
if(g_array_index(tmove->dest_idcs, gint, j) == dest_idx)
g_array_remove_index(tmove->dest_idcs, j);
if(tmove->dest_idcs->len == 0)
main_exit_program(EXIT_PROM_REL,
"league_team_movements_assign_dest: no destinations left for team %s.",
tmove->tm.name);
}
}
if(tmove->dest_idcs->len == 0)
main_exit_program(EXIT_PROM_REL,
"league_team_movements_assign_dest: no destinations left for team %s.",
tmove->tm.name);
}
}
if(team_is_user(&tmove->tm) != -1)
{
if(tmove->prom_rel_type == PROM_REL_PROMOTION)
user_history_add(&usr(team_is_user(&tmove->tm)),
USER_HISTORY_PROMOTED, tmove->tm.name,
lig(g_array_index(tmove->dest_idcs, gint, 0)).name,
NULL, NULL);
else
user_history_add(&usr(team_is_user(&tmove->tm)),
USER_HISTORY_RELEGATED, tmove->tm.name,
lig(g_array_index(tmove->dest_idcs, gint, 0)).name,
NULL, NULL);
if(tmove->prom_rel_type == PROM_REL_PROMOTION)
user_history_add(&usr(team_is_user(&tmove->tm)),
USER_HISTORY_PROMOTED, tmove->tm.name,
lig(g_array_index(tmove->dest_idcs, gint, 0)).name,
NULL, NULL);
else
user_history_add(&usr(team_is_user(&tmove->tm)),
USER_HISTORY_RELEGATED, tmove->tm.name,
lig(g_array_index(tmove->dest_idcs, gint, 0)).name,
NULL, NULL);
}
}
@ -899,7 +893,7 @@ league_team_movements_assign_dest(GArray *team_movements, gint idx,
account the assigned team moves). */
void
league_team_movements_prune(GArray *team_movements, const gint *league_size,
gint *league_cur_size)
gint *league_cur_size)
{
#ifdef DEBUG
printf("league_team_movements_prune\n");
@ -908,15 +902,15 @@ league_team_movements_prune(GArray *team_movements, const gint *league_size,
gint i;
if(debug > 60)
g_print("league_team_movements_prune\n");
g_print("league_team_movements_prune\n");
while(query_league_team_movements_unassigned_single(team_movements))
{
for(i=0;i<team_movements->len;i++)
if(!g_array_index(team_movements, TeamMove, i).dest_assigned &&
g_array_index(team_movements, TeamMove, i).dest_idcs->len == 1)
league_team_movements_assign_dest(team_movements, i,
league_size, league_cur_size);
for(i=0;i<team_movements->len;i++)
if(!g_array_index(team_movements, TeamMove, i).dest_assigned &&
g_array_index(team_movements, TeamMove, i).dest_idcs->len == 1)
league_team_movements_assign_dest(team_movements, i,
league_size, league_cur_size);
}
}
@ -935,25 +929,25 @@ league_team_movements_destinations(GArray *team_movements, const gint *league_si
gint league_cur_size[ligs->len];
if(debug > 60)
g_print("league_team_movements_destinations\n");
g_print("league_team_movements_destinations\n");
for(i=0;i<ligs->len;i++)
league_cur_size[i] = lig(i).teams->len;
league_cur_size[i] = lig(i).teams->len;
if(debug > 65)
league_team_movements_print(team_movements, league_size, league_cur_size);
league_team_movements_print(team_movements, league_size, league_cur_size);
while(query_league_team_movements_unassigned(team_movements))
{
league_team_movements_prune(team_movements, league_size, league_cur_size);
for(i=0;i<team_movements->len;i++)
if(!g_array_index(team_movements, TeamMove, i).dest_assigned)
league_team_movements_assign_dest(team_movements, i,
league_size, league_cur_size);
league_team_movements_prune(team_movements, league_size, league_cur_size);
for(i=0;i<team_movements->len;i++)
if(!g_array_index(team_movements, TeamMove, i).dest_assigned)
league_team_movements_assign_dest(team_movements, i,
league_size, league_cur_size);
}
if(debug > 65)
league_team_movements_print(team_movements, league_size, league_cur_size);
league_team_movements_print(team_movements, league_size, league_cur_size);
}
/** Find out whether there are any active leagues in the country. */
@ -967,8 +961,8 @@ query_leagues_active_in_country(void)
gint i;
for(i=0;i<ligs->len;i++)
if(query_league_active(&lig(i)))
return TRUE;
if(query_league_active(&lig(i)))
return TRUE;
return FALSE;
}
@ -986,10 +980,10 @@ query_league_cup_matchday_in_two_match_week(GArray **two_match_weeks, gint match
for(i=0;i<two_match_weeks[0]->len;i++)
{
if(g_array_index(two_match_weeks[0], gint, i) < matchday &&
matchday <= g_array_index(two_match_weeks[1], gint, i) &&
(matchday - g_array_index(two_match_weeks[0], gint, i)) % 2 == 1)
return TRUE;
if(g_array_index(two_match_weeks[0], gint, i) < matchday &&
matchday <= g_array_index(two_match_weeks[1], gint, i) &&
(matchday - g_array_index(two_match_weeks[0], gint, i)) % 2 == 1)
return TRUE;
}
return FALSE;
@ -1061,16 +1055,16 @@ GPtrArray*
league_cup_get_teams(gint clid)
{
return (clid < ID_CUP_START) ?
(GPtrArray*)league_from_clid(clid)->teams :
cup_from_clid(clid)->teams;
(GPtrArray*)league_from_clid(clid)->teams :
cup_from_clid(clid)->teams;
}
GPtrArray*
league_cup_get_properties(gint clid)
{
return (clid < ID_CUP_START) ?
league_from_clid(clid)->properties :
cup_from_clid(clid)->properties;
league_from_clid(clid)->properties :
cup_from_clid(clid)->properties;
}
/** Synchronise the number of league breaks with the number of