2005-10-20 17:45:00 +02:00
/*
2005-11-26 17:52:51 +01:00
cup . c
2005-10-20 17:45:00 +02:00
Bygfoot Football Manager - - a small and simple GTK2 - based
football management game .
http : //bygfoot.sourceforge.net
Copyright ( C ) 2005 Gyözö Both ( gyboth @ bygfoot . com )
This program is free software ; you can redistribute it and / or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation ; either version 2
of the License , or ( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program ; if not , write to the Free Software
Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 , USA .
*/
2004-12-23 21:43:43 +01:00
# include "cup.h"
2004-12-30 17:48:19 +01:00
# include "free.h"
2005-06-17 18:24:29 +02:00
# include "fixture.h"
2005-04-14 21:07:25 +02:00
# include "league.h"
2004-12-30 17:48:19 +01:00
# include "main.h"
# include "maths.h"
# include "misc.h"
2005-05-06 18:35:19 +02:00
# include "option.h"
2005-04-10 18:34:27 +02:00
# include "table.h"
2004-12-30 17:48:19 +01:00
# include "team.h"
# include "variables.h"
# include "xml_league.h"
/**
Create and return a new cup with default values .
@ return The new cup .
@ see # Cup
*/
Cup
2005-04-14 21:07:25 +02:00
cup_new ( gboolean new_id )
2004-12-30 17:48:19 +01:00
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " cup_new \n " ) ;
# endif
2004-12-30 17:48:19 +01:00
Cup new ;
2005-10-09 11:35:44 +02:00
new . name = NULL ;
new . short_name = NULL ;
new . symbol = NULL ;
new . sid = NULL ;
2004-12-30 17:48:19 +01:00
2005-04-14 21:07:25 +02:00
new . id = ( new_id ) ? cup_id_new : - 1 ;
2005-05-06 18:35:19 +02:00
new . group = - 1 ;
new . add_week = 0 ;
2005-04-14 21:07:25 +02:00
new . last_week = - 1 ;
new . week_gap = 1 ;
2005-05-07 16:19:52 +02:00
2004-12-30 17:48:19 +01:00
new . yellow_red = 1000 ;
2005-09-14 23:16:22 +02:00
new . talent_diff = 0 ;
2004-12-30 17:48:19 +01:00
new . rounds = g_array_new ( FALSE , FALSE , sizeof ( CupRound ) ) ;
2005-05-08 19:56:26 +02:00
new . teams = g_ptr_array_new ( ) ;
2005-05-06 18:35:19 +02:00
new . team_names = g_ptr_array_new ( ) ;
2005-01-09 21:21:22 +01:00
new . fixtures = g_array_new ( FALSE , FALSE , sizeof ( Fixture ) ) ;
2008-12-26 16:59:46 +01:00
new . week_breaks = g_array_new ( FALSE , FALSE , sizeof ( WeekBreak ) ) ;
2009-01-08 23:20:26 +01:00
new . skip_weeks_with = g_ptr_array_new ( ) ;
2005-05-08 19:56:26 +02:00
new . bye = g_ptr_array_new ( ) ;
2004-12-30 17:48:19 +01:00
2005-05-06 18:35:19 +02:00
new . properties = g_ptr_array_new ( ) ;
2005-01-09 21:21:22 +01:00
new . next_fixture_update_week = - 1 ;
new . next_fixture_update_week_round = - 1 ;
2004-12-30 17:48:19 +01:00
return new ;
}
/** Return a CupChooseTeam with default values. */
CupChooseTeam
cup_choose_team_new ( void )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " cup_choose_team_new \n " ) ;
# endif
# ifdef DEBUG
printf ( " cup_choose_team_new \n " ) ;
# endif
2004-12-30 17:48:19 +01:00
CupChooseTeam new ;
2005-10-09 11:35:44 +02:00
new . sid = NULL ;
2004-12-30 17:48:19 +01:00
new . number_of_teams = - 1 ;
new . start_idx = new . end_idx = - 1 ;
new . randomly = FALSE ;
2005-05-06 18:35:19 +02:00
new . generate = FALSE ;
2008-11-18 19:32:37 +01:00
new . skip_group_check = FALSE ;
2008-11-22 20:02:38 +01:00
new . from_table = 0 ;
2009-01-03 12:24:00 +01:00
new . preload = TRUE ;
2021-02-18 16:41:04 +01:00
new . optional = FALSE ;
2021-02-09 15:57:44 +01:00
new . next = NULL ;
2004-12-30 17:48:19 +01:00
return new ;
}
/** Return a CupRound with default values. */
CupRound
cup_round_new ( void )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " cup_round_new \n " ) ;
# endif
2004-12-30 17:48:19 +01:00
CupRound new ;
2009-01-04 11:53:09 +01:00
new . name = NULL ;
2005-05-08 19:56:26 +02:00
new . new_teams = 0 ;
2005-05-30 18:22:23 +02:00
new . byes = - 1 ;
2004-12-30 17:48:19 +01:00
new . home_away = TRUE ;
new . replay = 0 ;
new . neutral = FALSE ;
2005-09-27 14:18:46 +02:00
new . randomise_teams = TRUE ;
2006-06-29 18:06:52 +02:00
new . delay = 0 ;
2004-12-30 17:48:19 +01:00
new . round_robin_number_of_groups = 0 ;
2005-09-27 14:18:46 +02:00
new . round_robin_number_of_advance = 0 ;
2004-12-30 17:48:19 +01:00
new . round_robin_number_of_best_advance = 0 ;
2008-12-13 16:15:03 +01:00
new . round_robins = 2 ;
2008-12-14 11:37:10 +01:00
new . rr_breaks = g_array_new ( FALSE , FALSE , sizeof ( gint ) ) ;
2007-02-18 14:04:19 +01:00
new . two_match_weeks [ 0 ] = g_array_new ( FALSE , FALSE , sizeof ( gint ) ) ;
new . two_match_weeks [ 1 ] = g_array_new ( FALSE , FALSE , sizeof ( gint ) ) ;
new . two_match_week = FALSE ;
2005-04-22 17:17:39 +02:00
new . tables = g_array_new ( FALSE , FALSE , sizeof ( Table ) ) ;
2005-05-08 19:56:26 +02:00
new . choose_teams = g_array_new ( FALSE , FALSE , sizeof ( CupChooseTeam ) ) ;
2008-12-28 16:44:12 +01:00
new . waits = g_array_new ( FALSE , FALSE , sizeof ( CupRoundWait ) ) ;
2005-08-15 18:25:49 +02:00
new . team_ptrs = g_ptr_array_new ( ) ;
2004-12-30 17:48:19 +01:00
return new ;
}
2005-05-06 18:35:19 +02:00
/** Reset some arrays at the beginning of a new season. */
void
cup_reset ( Cup * cup )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " cup_reset \n " ) ;
# endif
2008-11-21 18:00:47 +01:00
gint i ;
2005-05-06 18:35:19 +02:00
2005-10-09 11:35:44 +02:00
free_gchar_array ( & cup - > team_names ) ;
2005-05-06 18:35:19 +02:00
cup - > team_names = g_ptr_array_new ( ) ;
2005-05-08 19:56:26 +02:00
if ( cup - > teams - > len > 0 )
{
g_ptr_array_free ( cup - > teams , TRUE ) ;
cup - > teams = g_ptr_array_new ( ) ;
}
2005-05-06 18:35:19 +02:00
g_array_free ( cup - > fixtures , TRUE ) ;
cup - > fixtures = g_array_new ( FALSE , FALSE , sizeof ( Fixture ) ) ;
free_g_ptr_array ( & cup - > bye ) ;
for ( i = 0 ; i < cup - > rounds - > len ; i + + )
{
2021-02-16 03:36:50 +01:00
CupRound * cup_round = & g_array_index ( cup - > rounds , CupRound , i ) ;
if ( cup_round - > tables - > len > 0 )
2005-05-11 22:17:24 +02:00
{
2021-02-16 03:36:50 +01:00
free_tables ( & cup_round - > tables ) ;
cup_round - > tables = g_array_new ( FALSE , FALSE , sizeof ( Table ) ) ;
2005-05-11 22:17:24 +02:00
}
2021-02-16 03:36:50 +01:00
g_ptr_array_free ( cup_round - > team_ptrs , TRUE ) ;
cup_round - > team_ptrs = g_ptr_array_new ( ) ;
2005-05-06 18:35:19 +02:00
}
}
2005-04-14 21:07:25 +02:00
/** Find out whether a choose_team definition refers to a
league in the country . If so , no teams get loaded for that
choose_team . */
gboolean
query_cup_choose_team_is_league ( const gchar * sid )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " query_cup_choose_team_is_league \n " ) ;
# endif
2005-04-14 21:07:25 +02:00
gint i ;
for ( i = 0 ; i < ligs - > len ; i + + )
2005-10-09 11:35:44 +02:00
if ( strcmp ( lig ( i ) . sid , sid ) = = 0 )
2005-04-14 21:07:25 +02:00
return TRUE ;
return FALSE ;
}
2021-02-04 00:48:28 +01:00
/** @return TRUE if the team(s) referenced by \p sid need to be
* generated .
*/
static gboolean
cup_choose_team_should_generate ( const CupChooseTeam * ct )
{
if ( g_str_has_prefix ( ct - > sid , " LEAGUE " ) | | g_str_has_prefix ( ct - > sid , " CUP " ) )
return FALSE ;
return ! country_get_league_sid ( & country , ct - > sid ) & &
! country_get_cup_sid ( & country , ct - > sid ) ;
}
2005-10-31 17:19:14 +01:00
/** Write the cup or league of the chooseteam into the appropriate pointer
and return TRUE ; return FALSE if no cup / league is found . */
2005-04-14 21:07:25 +02:00
void
cup_get_choose_team_league_cup ( const CupChooseTeam * ct ,
2005-06-08 19:43:28 +02:00
const League * * league , const Cup * * cup )
2005-04-14 21:07:25 +02:00
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " cup_get_choose_team_league_cup \n " ) ;
# endif
2005-04-14 21:07:25 +02:00
gint i , idx ;
gchar trash [ SMALL ] ;
2009-01-08 23:20:26 +01:00
gchar prefix [ SMALL ] ;
2005-04-14 21:07:25 +02:00
2005-05-06 18:35:19 +02:00
* league = NULL ;
* cup = NULL ;
2005-10-09 11:35:44 +02:00
sscanf ( ct - > sid , " %[^0-9]%d " , trash , & idx ) ;
2005-04-14 21:07:25 +02:00
2005-10-31 17:19:14 +01:00
if ( g_str_has_prefix ( ct - > sid , " LEAGUE " ) & & idx > = 0 & & idx < = ligs - > len )
2005-04-14 21:07:25 +02:00
{
* league = & lig ( idx - 1 ) ;
* cup = NULL ;
}
2005-10-31 17:19:14 +01:00
else if ( g_str_has_prefix ( ct - > sid , " CUP " ) & & idx > = 0 & & idx < = cps - > len )
2005-04-14 21:07:25 +02:00
{
* cup = & cp ( idx - 1 ) ;
* league = NULL ;
}
else
{
2009-01-08 23:20:26 +01:00
if ( g_str_has_suffix ( ct - > sid , " * " ) )
g_utf8_strncpy ( prefix , ct - > sid , g_utf8_strlen ( ct - > sid , - 1 ) - 1 ) ;
else
strcpy ( prefix , " NONAME " ) ;
2005-04-14 21:07:25 +02:00
for ( i = 0 ; i < ligs - > len ; i + + )
2005-07-05 20:37:26 +02:00
{
2009-01-08 23:20:26 +01:00
if ( strcmp ( lig ( i ) . sid , ct - > sid ) = = 0 | |
g_str_has_prefix ( lig ( i ) . sid , prefix ) )
2005-04-14 21:07:25 +02:00
{
* league = & lig ( i ) ;
* cup = NULL ;
break ;
}
2005-07-05 20:37:26 +02:00
}
2005-04-14 21:07:25 +02:00
2005-05-25 19:58:04 +02:00
for ( i = 0 ; i < cps - > len ; i + + )
2005-05-06 18:35:19 +02:00
{
2009-01-08 23:20:26 +01:00
if ( strcmp ( cp ( i ) . sid , ct - > sid ) = = 0 | |
g_str_has_prefix ( cp ( i ) . sid , prefix ) )
2005-04-14 21:07:25 +02:00
{
2005-05-25 19:58:04 +02:00
* cup = & cp ( i ) ;
2005-04-14 21:07:25 +02:00
* league = NULL ;
break ;
}
2005-05-06 18:35:19 +02:00
}
2005-04-14 21:07:25 +02:00
}
2005-07-05 20:37:26 +02:00
if ( * league = = NULL & & * cup = = NULL )
2005-10-24 22:50:48 +02:00
main_exit_program ( EXIT_CHOOSE_TEAM_ERROR ,
" cup_get_choose_team_league_cup: no league nor cup found for chooseteam %s " ,
ct - > sid ) ;
2009-01-08 23:20:26 +01:00
if ( debug > 100 )
{
if ( * league = = NULL )
printf ( " cup_get_choose_team_league_cup: sid %s cup %s \n " , ct - > sid , ( * cup ) - > name ) ;
else
printf ( " cup_get_choose_team_league_cup: sid %s league %s \n " , ct - > sid , ( * league ) - > name ) ;
}
2005-04-14 21:07:25 +02:00
}
2005-08-15 18:25:49 +02:00
/** Load the pointers to the teams participating in the
cup round . If necessary , teams are generated and stored in the teams
array of the cup round . */
void
2009-01-11 15:51:12 +01:00
cup_get_team_pointers ( Cup * cup , gint round , GPtrArray * teams_sorted , gboolean preload )
2005-05-06 18:35:19 +02:00
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " cup_get_team_pointers \n " ) ;
# endif
2009-01-03 19:01:27 +01:00
gint i , existing_teams ;
2005-05-08 19:56:26 +02:00
CupRound * cup_round = & g_array_index ( cup - > rounds , CupRound , round ) ;
2005-08-15 18:25:49 +02:00
GPtrArray * teams = cup_round - > team_ptrs ;
2005-05-06 18:35:19 +02:00
if ( debug > 60 )
2005-12-14 15:05:59 +01:00
g_print ( " cup_get_team_pointers %s round %d \n " , cup - > name , round ) ;
2005-05-06 18:35:19 +02:00
2021-02-06 20:25:45 +01:00
existing_teams = cup_round - > team_ptrs - > len ;
2009-01-03 19:01:27 +01:00
2021-02-03 19:48:42 +01:00
for ( i = 0 ; i < cup_round - > choose_teams - > len ; i + + ) {
const CupChooseTeam * choose_team =
& g_array_index ( cup_round - > choose_teams , CupChooseTeam , i ) ;
if ( choose_team - > preload = = preload )
2009-01-03 12:24:00 +01:00
{
2021-02-04 00:48:28 +01:00
if ( cup_choose_team_should_generate ( choose_team ) )
2021-02-03 19:48:42 +01:00
cup_load_choose_team_generate ( cup , cup_round , choose_team ) ;
2009-01-03 12:24:00 +01:00
else
2021-02-03 19:48:42 +01:00
cup_load_choose_team ( cup , teams , teams_sorted , choose_team ) ;
}
}
2005-08-04 14:37:50 +02:00
2021-02-06 20:25:45 +01:00
for ( i = existing_teams ; i < cup_round - > team_ptrs - > len ; i + + )
{
Team * team = g_ptr_array_index ( cup_round - > team_ptrs , i ) ;
g_ptr_array_add ( cup - > teams , team ) ;
2005-05-06 18:35:19 +02:00
}
2020-10-02 16:23:25 +02:00
country_lookup_first_team_ids ( & country ) ;
2005-05-08 19:56:26 +02:00
if ( debug > 70 )
for ( i = 0 ; i < teams - > len ; i + + )
2021-02-18 18:21:50 +01:00
g_print ( " cup_get_team_pointers: %s (%s) round %d team %d %s (clid %d) \n " ,
cup - > name , cup - > sid , round , i ,
2009-01-03 19:01:27 +01:00
( ( Team * ) g_ptr_array_index ( teams , i ) ) - > name ,
( ( Team * ) g_ptr_array_index ( teams , i ) ) - > clid ) ;
2005-05-06 18:35:19 +02:00
}
/** Get the pointers to the teams (already generated, in one of the leagues or cups)
specified in the chooseteam . Add them to the ' teams ' pointer array . */
void
2009-01-11 15:51:12 +01:00
cup_load_choose_team ( Cup * cup , GPtrArray * teams , GPtrArray * teams_sorted , const CupChooseTeam * ct )
2005-04-14 21:07:25 +02:00
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " cup_load_choose_team \n " ) ;
# endif
2008-11-18 19:32:37 +01:00
gint i ;
2005-05-24 20:31:07 +02:00
gint debug_num = teams - > len ;
2005-06-08 19:43:28 +02:00
const League * league = NULL ;
const Cup * cup_temp = NULL ;
2005-04-14 21:07:25 +02:00
2005-05-06 18:35:19 +02:00
if ( debug > 60 )
2009-04-20 09:26:04 +02:00
g_print ( " cup_load_choose_team: %s, %s, teams %d to %d, random: %d \n " , cup - > name ,
ct - > sid , ct - > start_idx , ct - > end_idx , ct - > randomly ) ;
2005-04-14 21:07:25 +02:00
2005-05-06 18:35:19 +02:00
cup_get_choose_team_league_cup ( ct , & league , & cup_temp ) ;
2005-03-13 22:10:19 +01:00
2005-05-06 18:35:19 +02:00
if ( cup_temp = = NULL )
2008-11-18 19:32:37 +01:00
cup_load_choose_team_from_league ( cup , league , teams , ct ) ;
else
2009-01-11 15:51:12 +01:00
cup_load_choose_team_from_cup ( cup , cup_temp , teams , teams_sorted , ct ) ;
2005-04-14 21:07:25 +02:00
2008-11-18 19:32:37 +01:00
if ( debug > 80 )
for ( i = debug_num ; i < teams - > len ; i + + )
g_print ( " cup_load_choose_team: %d %s \n " , i , ( ( Team * ) g_ptr_array_index ( teams , i ) ) - > name ) ;
}
2005-05-06 18:35:19 +02:00
2008-11-18 19:32:37 +01:00
void
2009-01-11 15:51:12 +01:00
cup_load_choose_team_from_cup ( Cup * cup , const Cup * cup_temp , GPtrArray * teams , GPtrArray * teams_sorted , const CupChooseTeam * ct )
2008-11-18 19:32:37 +01:00
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " cup_load_choose_team_from_cup \n " ) ;
# endif
2008-11-18 19:32:37 +01:00
gint i ;
gint start , end ;
gint number_of_teams ;
GPtrArray * cup_teams_sorted ;
2005-04-14 21:07:25 +02:00
2008-11-18 19:32:37 +01:00
number_of_teams = 0 ;
cup_teams_sorted = NULL ;
2005-04-14 21:07:25 +02:00
2008-12-28 16:44:12 +01:00
if ( season = = 1 & & week = = 1 & & cup - > add_week = = 0 )
2008-11-18 19:32:37 +01:00
{
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 ;
}
2021-02-09 15:57:44 +01:00
if ( number_of_teams ! = ct - > number_of_teams ) {
if ( ct - > next )
return cup_load_choose_team ( cup , teams , teams_sorted , ct - > next ) ;
2008-11-18 19:32:37 +01:00
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 ) ;
2021-02-09 15:57:44 +01:00
}
2005-05-06 18:35:19 +02:00
}
else
{
2009-01-11 15:51:12 +01:00
/* Self-referential cup or no? */
cup_teams_sorted = ( cup = = cup_temp ) ?
teams_sorted :
cup_get_teams_sorted ( cup_temp ) ;
2008-11-18 19:32:37 +01:00
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 + + ;
2005-05-06 18:35:19 +02:00
2008-11-18 19:32:37 +01:00
if ( number_of_teams = = ct - > number_of_teams )
break ;
}
}
2009-01-11 15:51:12 +01:00
if ( cup ! = cup_temp )
g_ptr_array_free ( cup_teams_sorted , TRUE ) ;
2008-11-18 19:32:37 +01:00
if ( ct - > number_of_teams ! = - 1 & &
2021-02-09 15:57:44 +01:00
number_of_teams ! = ct - > number_of_teams ) {
if ( ct - > next )
return cup_load_choose_team ( cup , teams , teams_sorted , ct - > next ) ;
2008-11-18 19:32:37 +01:00
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 ) ;
2021-02-09 15:57:44 +01:00
}
2008-11-18 19:32:37 +01:00
}
}
2008-12-03 22:46:27 +01:00
2008-11-18 19:32:37 +01:00
void
cup_load_choose_team_from_league ( Cup * cup , const League * league ,
GPtrArray * teams , const CupChooseTeam * ct )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " cup_load_choose_team_from_league \n " ) ;
# endif
2008-11-18 19:32:37 +01:00
gint start , end ;
gint number_of_teams ;
gint j ;
2008-11-22 20:02:38 +01:00
Table * table ;
2005-04-14 21:07:25 +02:00
2008-11-18 19:32:37 +01:00
number_of_teams = 0 ;
2008-11-26 20:27:00 +01:00
if ( league - > tables - > len = = 0 )
table = & g_array_index ( league - > tables , Table , 0 ) ;
else
table = & g_array_index ( league - > tables , Table , ct - > from_table ) ;
2005-04-14 21:07:25 +02:00
2008-11-18 19:32:37 +01:00
if ( ct - > number_of_teams = = - 1 )
{
2008-11-22 20:02:38 +01:00
for ( j = 0 ; j < table - > elements - > len ; j + + )
2008-11-18 19:32:37 +01:00
{
2020-09-22 05:04:18 +02:00
Team * team = team_of_id (
g_array_index ( table - > elements , TableElement , j ) . team_id ) ;
2020-09-22 05:06:43 +02:00
if ( team_is_reserve_team ( team ) & &
! query_league_cup_has_property ( cup - > id , " include_reserve_teams " ) )
continue ;
2020-09-22 05:04:18 +02:00
g_ptr_array_add ( teams , team ) ;
g_ptr_array_add ( cup - > team_names , g_strdup ( team - > name ) ) ;
2008-11-18 19:32:37 +01:00
}
}
else
{
start = ct - > start_idx - 1 ;
end = ct - > end_idx - start ;
gint order [ end ] ;
for ( j = 0 ; j < end ; j + + )
order [ j ] = j + start ;
if ( ct - > randomly )
math_generate_permutation ( order , start , start + end - 1 ) ;
for ( j = 0 ; j < end ; j + + )
{
2020-09-22 05:04:18 +02:00
Team * team = team_of_id ( g_array_index ( table - > elements , TableElement , order [ j ] ) . team_id ) ;
2020-09-22 05:06:43 +02:00
if ( team_is_reserve_team ( team ) & &
! query_league_cup_has_property ( cup - > id , " include_reserve_teams " ) )
continue ;
2008-11-18 19:32:37 +01:00
if ( debug > 80 )
2009-04-20 09:26:04 +02:00
g_print ( " j %d order %d team %s isinint %d numteams %d \n " ,
j , order [ j ] ,
2008-11-22 20:02:38 +01:00
team_of_id ( g_array_index ( table - > elements ,
2008-11-18 19:32:37 +01:00
TableElement , order [ j ] ) . team_id ) - > name ,
query_team_is_in_cups (
2008-11-22 20:02:38 +01:00
team_of_id ( g_array_index ( table - > elements ,
2008-11-18 19:32:37 +01:00
TableElement , order [ j ] ) . team_id ) ,
cup - > group ) ,
number_of_teams ) ;
2020-09-22 05:04:18 +02:00
if ( ct - > skip_group_check | | ! query_team_is_in_cups ( team , cup - > group ) )
2008-11-18 19:32:37 +01:00
{
2020-09-22 05:04:18 +02:00
g_ptr_array_add ( teams , team ) ;
g_ptr_array_add ( cup - > team_names , g_strdup ( team - > name ) ) ;
2008-11-18 19:32:37 +01:00
number_of_teams + + ;
if ( number_of_teams = = ct - > number_of_teams )
break ;
}
}
2005-04-14 21:07:25 +02:00
}
2005-05-24 20:31:07 +02:00
2008-11-18 19:32:37 +01:00
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 ) ;
2004-12-30 17:48:19 +01:00
}
2021-02-09 15:53:22 +01:00
/** 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
2021-02-09 15:57:44 +01:00
cup_generate_team_list ( const Cup * cup , GPtrArray * teams , gboolean league_talents )
2021-02-09 15:53:22 +01:00
{
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 ) ;
}
}
2005-05-06 18:35:19 +02:00
/** Load the teams specified in the chooseteam from a non-country league. */
2004-12-30 17:48:19 +01:00
void
2005-05-08 19:56:26 +02:00
cup_load_choose_team_generate ( Cup * cup , CupRound * cup_round , const CupChooseTeam * ct )
2004-12-30 17:48:19 +01:00
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " cup_load_choose_team_generate \n " ) ;
# endif
2005-05-06 18:35:19 +02:00
gint j , k ;
gint number_of_teams , end_idx = - 1 ;
2021-02-06 20:25:45 +01:00
GPtrArray * teams_local = NULL ;
2005-05-06 18:35:19 +02:00
GPtrArray * sids = NULL ;
2005-04-13 15:01:59 +02:00
2005-05-06 18:35:19 +02:00
if ( debug > 60 )
2021-02-18 18:21:50 +01:00
g_print ( " cup_load_choose_team_generate: %s, (%s) %s \n " , cup - > name , cup - > sid ,
2005-10-09 11:35:44 +02:00
ct - > sid ) ;
2004-12-30 17:48:19 +01:00
2021-02-06 20:25:45 +01:00
teams_local = g_ptr_array_new ( ) ;
2008-12-04 18:35:47 +01:00
sids = misc_randomise_g_pointer_array ( misc_separate_strings ( ct - > sid ) ) ;
2004-12-30 17:48:19 +01:00
2005-05-06 18:35:19 +02:00
for ( j = 0 ; j < sids - > len ; j + + )
2004-12-30 17:48:19 +01:00
{
2021-02-06 06:05:16 +01:00
const gchar * sid = g_ptr_array_index ( sids , j ) ;
2021-02-09 15:53:22 +01:00
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 ;
}
2021-02-06 06:05:16 +01:00
if ( ! query_cup_choose_team_is_league ( sid ) )
2004-12-30 17:48:19 +01:00
{
2021-02-06 20:25:45 +01:00
const League * league = bygfoot_get_league_sid ( sid ) ;
if ( ! league ) {
g_warning ( " Cup: %s choose_team_sid %s not found. " ,
cup - > sid , sid ) ;
continue ;
}
2021-02-06 06:05:16 +01:00
for ( k = 0 ; k < league - > teams - > len ; k + + )
2008-11-18 14:26:57 +01:00
{
2021-02-06 06:05:16 +01:00
Team * team = & g_array_index ( league - > teams , Team , k ) ;
2008-11-24 12:16:36 +01:00
if ( query_league_cup_has_property ( cup - > id , " league_talents " ) )
2008-11-18 14:26:57 +01:00
{
2021-02-06 06:05:16 +01:00
team - > average_talent = league - > average_talent ;
2008-11-18 14:26:57 +01:00
}
2021-02-06 20:25:45 +01:00
g_ptr_array_add ( teams_local , team ) ;
2008-11-18 14:26:57 +01:00
}
2004-12-30 17:48:19 +01:00
}
}
2005-10-05 21:59:37 +02:00
free_gchar_array ( & sids ) ;
2005-09-19 23:13:36 +02:00
/** No teams found. */
if ( teams_local - > len = = 0 )
{
2021-02-06 20:25:45 +01:00
g_ptr_array_free ( teams_local , TRUE ) ;
2005-09-19 23:13:36 +02:00
return ;
}
2005-05-06 18:35:19 +02:00
gint permutation [ teams_local - > len ] ;
for ( j = 0 ; j < teams_local - > len ; j + + )
permutation [ j ] = j ;
2004-12-30 17:48:19 +01:00
2005-05-06 18:35:19 +02:00
if ( ct - > randomly & & teams_local - > len > 0 )
{
if ( ct - > start_idx = = - 1 )
math_generate_permutation ( permutation , 0 , teams_local - > len - 1 ) ;
else
math_generate_permutation ( permutation , ct - > start_idx - 1 , ct - > end_idx - 1 ) ;
2021-02-09 15:57:44 +01:00
} else {
/* I'm doing this in the else here to avoid breaking other code
* paths , since I don ' t really understand well enough what they
* are doing .
*/
for ( j = 0 ; j < teams_local - > len ; j + + ) {
permutation [ j ] = ct - > start_idx + j - 1 ;
}
2005-05-06 18:35:19 +02:00
}
2005-01-09 21:21:22 +01:00
2005-05-06 18:35:19 +02:00
number_of_teams = 0 ;
if ( ct - > start_idx = = - 1 )
end_idx = teams_local - > len ;
else
end_idx = ct - > end_idx -
ct - > start_idx + 1 ;
2004-12-30 17:48:19 +01:00
2005-05-06 18:35:19 +02:00
if ( teams_local - > len < end_idx )
{
2005-10-24 22:50:48 +02:00
main_exit_program ( EXIT_CHOOSE_TEAM_ERROR ,
" cup_load_choose_team_generate: not enough teams (%d) in chooseteam %s in cup %s (%d are specified) \n " ,
teams_local - > len , ct - > sid ,
cup - > name , end_idx ) ;
2004-12-30 17:48:19 +01:00
}
2005-04-10 18:34:27 +02:00
2005-05-06 18:35:19 +02:00
for ( j = 0 ; j < end_idx ; j + + )
2005-04-13 15:01:59 +02:00
{
2021-02-06 20:25:45 +01:00
Team * team = g_ptr_array_index ( teams_local , permutation [ j ] ) ;
2008-11-18 19:32:37 +01:00
if ( ct - > skip_group_check | |
2021-02-06 20:25:45 +01:00
! query_team_is_in_cups ( team , cup - > group ) )
2005-04-13 15:01:59 +02:00
{
2021-02-06 20:25:45 +01:00
//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 ) ;
2005-05-06 18:35:19 +02:00
2005-04-13 15:01:59 +02:00
number_of_teams + + ;
}
2005-05-06 18:35:19 +02:00
2005-04-14 21:07:25 +02:00
if ( number_of_teams = = ct - > number_of_teams )
2005-04-13 15:01:59 +02:00
break ;
}
2008-12-28 11:24:54 +01:00
2005-05-06 18:35:19 +02:00
if ( ( ( ct - > number_of_teams ! = - 1 & & number_of_teams ! = ct - > number_of_teams ) | |
( ct - > number_of_teams = = - 1 & & number_of_teams ! = teams_local - > len ) ) & &
2021-02-09 15:57:44 +01:00
teams_local - > len > 0 ) {
/* TODO: handle number_of_teams > 0 */
g_ptr_array_free ( teams_local , TRUE ) ;
if ( ct - > next )
return cup_load_choose_team_generate ( cup , cup_round , ct - > next ) ;
2021-02-18 16:41:04 +01:00
if ( ct - > optional )
return ;
main_exit_program ( EXIT_CHOOSE_TEAM_ERROR ,
2005-10-24 22:50:48 +02:00
" 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 ) ;
2021-02-09 15:57:44 +01:00
}
2021-02-06 20:25:45 +01:00
g_ptr_array_free ( teams_local , teams_local - > len > 0 ) ;
2005-04-13 15:01:59 +02:00
}
/** Return a pointer array of teams ordered corresponding to
their success in the cup . A bit tricky because we have to
fetch the team pointers corresponding to their name because
the team pointers in the fixtures are partially invalid because
of promotion relegation . */
GPtrArray *
cup_get_teams_sorted ( const Cup * cup )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " cup_get_teams_sorted \n " ) ;
# endif
2005-04-13 15:01:59 +02:00
gint i , j ;
GPtrArray * teams = g_ptr_array_new ( ) ;
GArray * team_ids = g_array_new ( FALSE , FALSE , sizeof ( gint ) ) ;
2005-04-10 18:34:27 +02:00
2005-04-13 15:01:59 +02:00
for ( i = 0 ; i < cup - > fixtures - > len ; i + + )
for ( j = 0 ; j < 2 ; j + + )
if ( ! query_misc_integer_is_in_g_array (
g_array_index ( cup - > fixtures , Fixture , i ) . team_ids [ j ] , team_ids ) )
{
g_array_append_val ( team_ids , g_array_index ( cup - > fixtures , Fixture , i ) . team_ids [ j ] ) ;
g_ptr_array_add ( teams , team_of_id ( g_array_index ( cup - > fixtures , Fixture , i ) . team_ids [ j ] ) ) ;
}
2005-04-22 17:17:39 +02:00
g_ptr_array_sort_with_data ( teams , cup_compare_success , ( gpointer ) cup ) ;
2005-04-13 15:01:59 +02:00
g_array_free ( team_ids , TRUE ) ;
return teams ;
}
/** Compare two teams in a pointer array taking into account
their success in a cup . */
gint
cup_compare_success ( gconstpointer a , gconstpointer b , gpointer data )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " cup_compare_success \n " ) ;
# endif
2005-04-22 17:17:39 +02:00
const Cup * cup = ( const Cup * ) data ;
const CupRound * cupround = NULL ;
const GArray * fixtures = cup - > fixtures ;
2005-04-13 15:01:59 +02:00
const Team * team1 = * ( const Team * * ) a ;
const Team * team2 = * ( const Team * * ) b ;
gint round_reached1 = cup_get_round_reached ( team1 , fixtures ) ,
round_reached2 = cup_get_round_reached ( team2 , fixtures ) ;
gint return_value = 0 ;
2005-04-22 17:17:39 +02:00
if ( team1 = = team2 )
return_value = 0 ;
else if ( round_reached1 < round_reached2 )
2005-04-13 15:01:59 +02:00
return_value = 1 ;
else if ( round_reached1 > round_reached2 )
return_value = - 1 ;
else
{
2005-04-22 17:17:39 +02:00
cupround = & g_array_index ( cup - > rounds , CupRound , round_reached1 ) ;
if ( cupround - > tables - > len > 0 )
return_value =
cup_compare_success_tables ( team1 , team2 , cup , round_reached1 ) ;
2005-04-13 15:01:59 +02:00
else
2005-06-25 16:09:37 +02:00
return_value = cup_compare_success_knockout ( team1 , team2 , cup - > fixtures ) ;
}
return return_value ;
}
/** Compare two teams that reached the same knockout round. */
gint
cup_compare_success_knockout ( const Team * tm1 , const Team * tm2 , const GArray * fixtures )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " cup_compare_success_knockout \n " ) ;
# endif
2005-06-25 16:09:37 +02:00
gint i ;
gint winner1 = - 1 , winner2 = - 1 ;
gint return_value = 0 ;
for ( i = fixtures - > len - 1 ; i > = 0 ; i - - )
{
if ( winner1 = = - 1 & &
query_fixture_team_involved ( ( & g_array_index ( fixtures , Fixture , i ) ) , tm1 - > id ) )
winner1 = GPOINTER_TO_INT (
fixture_winner_of ( & g_array_index ( fixtures , Fixture , i ) , TRUE ) ) ;
if ( winner2 = = - 1 & &
query_fixture_team_involved ( ( & g_array_index ( fixtures , Fixture , i ) ) , tm2 - > id ) )
winner2 = GPOINTER_TO_INT (
fixture_winner_of ( & g_array_index ( fixtures , Fixture , i ) , TRUE ) ) ;
if ( winner1 ! = - 1 & & winner2 ! = - 1 )
break ;
2005-04-13 15:01:59 +02:00
}
2005-06-25 16:09:37 +02:00
if ( winner1 = = tm1 - > id & & winner2 ! = tm2 - > id )
return_value = - 1 ;
else if ( winner1 ! = tm1 - > id & & winner2 = = tm2 - > id )
return_value = 1 ;
2005-04-13 15:01:59 +02:00
return return_value ;
}
2005-06-25 16:09:37 +02:00
2005-04-22 17:17:39 +02:00
/** Compare two teams in cup tables. */
gint
cup_compare_success_tables ( const Team * tm1 , const Team * tm2 , const Cup * cup , gint round )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " cup_compare_success_tables \n " ) ;
# endif
2005-04-22 17:17:39 +02:00
gint i , j ;
gint return_value = 0 ;
const CupRound * cupround = & g_array_index ( cup - > rounds , CupRound , round ) ;
const TableElement * elem1 = NULL , * elem2 = NULL ;
2005-09-22 18:41:07 +02:00
if ( team_get_cup_rank ( tm1 , cupround , TRUE ) >
team_get_cup_rank ( tm2 , cupround , TRUE ) )
2005-04-22 17:17:39 +02:00
return_value = 1 ;
2005-09-22 18:41:07 +02:00
else if ( team_get_cup_rank ( tm1 , cupround , TRUE ) <
team_get_cup_rank ( tm2 , cupround , TRUE ) )
2005-04-22 17:17:39 +02:00
return_value = - 1 ;
else
{
for ( i = 0 ; i < cupround - > tables - > len ; i + + )
for ( j = 0 ; j < g_array_index ( cupround - > tables , Table , i ) . elements - > len ; j + + )
2005-04-24 12:17:20 +02:00
if ( g_array_index ( g_array_index ( cupround - > tables , Table , i ) . elements , TableElement , j ) . team_id = = tm1 - > id )
2005-04-22 17:17:39 +02:00
elem1 = & g_array_index ( g_array_index ( cupround - > tables , Table , i ) . elements , TableElement , j ) ;
2005-04-24 12:17:20 +02:00
else if ( g_array_index ( g_array_index ( cupround - > tables , Table , i ) . elements , TableElement , j ) . team_id = = tm2 - > id )
2005-04-22 17:17:39 +02:00
elem2 = & g_array_index ( g_array_index ( cupround - > tables , Table , i ) . elements , TableElement , j ) ;
return_value = table_element_compare_func ( elem1 , elem2 , GINT_TO_POINTER ( cup - > id ) ) ;
}
return return_value ;
}
2005-04-13 15:01:59 +02:00
/** Return the cup round that the team reached in the cup.
@ param fixtures The fixtures array of the cup . */
gint
cup_get_round_reached ( const Team * tm , const GArray * fixtures )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " cup_get_round_reached \n " ) ;
# endif
2005-04-13 15:01:59 +02:00
gint round = - 1 ;
gint i ;
for ( i = 0 ; i < fixtures - > len ; i + + )
if ( g_array_index ( fixtures , Fixture , i ) . team_ids [ 0 ] = = tm - > id | |
g_array_index ( fixtures , Fixture , i ) . team_ids [ 1 ] = = tm - > id )
round = MAX ( round , g_array_index ( fixtures , Fixture , i ) . round ) ;
return round ;
2004-12-30 17:48:19 +01:00
}
2005-01-09 21:21:22 +01:00
/** Calculate the week number the first matchday of the given
cup round takes place .
@ param cup The cup we examine .
@ param cup_round The index of the cup round in the cup . rounds array .
@ return A week number . */
gint
2008-12-22 12:20:16 +01:00
cup_get_first_week_of_cup_round ( Cup * cup , gint cup_round , gboolean with_delay )
2005-01-09 21:21:22 +01:00
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " cup_get_first_week_of_cup_round \n " ) ;
# endif
2005-01-09 21:21:22 +01:00
gint week_number ;
if ( cup_round = = cup - > rounds - > len - 1 )
week_number = cup - > last_week -
( cup_get_matchdays_in_cup_round ( cup , cup_round ) - 1 ) * cup - > week_gap ;
else
2008-12-22 12:20:16 +01:00
week_number = cup_get_first_week_of_cup_round ( cup , cup_round + 1 , FALSE ) -
2005-01-09 21:21:22 +01:00
cup_get_matchdays_in_cup_round ( cup , cup_round ) * cup - > week_gap ;
2008-12-22 12:20:16 +01:00
if ( with_delay )
week_number + = g_array_index ( cup - > rounds , CupRound , cup_round ) . delay ;
2006-06-29 18:06:52 +02:00
2005-04-20 19:56:31 +02:00
if ( week_number < = 0 )
2006-06-29 18:06:52 +02:00
{
2009-04-29 19:18:54 +02:00
debug_print_message ( " cup_get_first_week_of_cup_round: First week of cup %s, cup round %d is not positive (%d). Please correct the cup definition file!!! \n " ,
2006-06-29 18:06:52 +02:00
cup - > name , cup_round , week_number ) ;
if ( cup - > week_gap > 1 )
{
cup - > week_gap - - ;
2009-04-29 19:18:54 +02:00
debug_print_message ( " Lowering week gap to %d and trying again. \n " ,
2006-06-29 18:06:52 +02:00
cup - > week_gap ) ;
}
else
{
cup - > last_week + + ;
2009-04-29 19:18:54 +02:00
debug_print_message ( " Increasing last week to %d and trying again. \n " ,
2006-06-29 18:06:52 +02:00
cup - > last_week ) ;
}
2008-12-22 12:20:16 +01:00
return cup_get_first_week_of_cup_round ( cup , cup_round , with_delay ) ;
2006-06-29 18:06:52 +02:00
}
2005-10-24 22:50:48 +02:00
2005-01-09 21:21:22 +01:00
return week_number ;
}
2005-04-14 21:07:25 +02:00
/** Calculate the last week of a cup if we only know the first week. */
gint
cup_get_last_week_from_first ( const Cup * cup , gint first_week )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " cup_get_last_week_from_first \n " ) ;
# endif
2005-04-14 21:07:25 +02:00
gint i ;
gint matchdays = 0 ;
for ( i = 0 ; i < cup - > rounds - > len ; i + + )
matchdays + = cup_get_matchdays_in_cup_round ( cup , i ) ;
return first_week + ( matchdays - 1 ) * cup - > week_gap ;
}
2005-01-09 21:21:22 +01:00
/** Return the number of matchdays for a given cup round.
@ param cup The cup we examine .
@ param cup_round The index of the cup round .
@ return The number of matchdays , mostly 1 or 2. */
gint
2005-08-14 21:03:11 +02:00
cup_get_matchdays_in_cup_round ( const Cup * cup , gint round )
2005-01-09 21:21:22 +01:00
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " cup_get_matchdays_in_cup_round \n " ) ;
# endif
2007-02-18 14:04:19 +01:00
gint i ;
2005-08-14 21:03:11 +02:00
const CupRound * cup_round = & g_array_index ( cup - > rounds , CupRound , round ) ;
2005-01-09 21:21:22 +01:00
gint number_of_teams = - 1 ;
gint number_of_matchdays = - 1 ;
2007-02-18 14:04:19 +01:00
gint diff ;
2005-01-09 21:21:22 +01:00
2005-08-14 21:03:11 +02:00
if ( cup_round - > round_robin_number_of_groups > 0 )
2005-01-09 21:21:22 +01:00
{
2005-09-27 14:18:46 +02:00
number_of_teams = cup_round_get_number_of_teams ( cup , round ) ;
2006-05-13 16:29:13 +02:00
number_of_teams + = cup_round - > round_robin_number_of_groups - 1 ;
number_of_teams / = cup_round - > round_robin_number_of_groups ;
/* Now, number_of_teams = number of teams in largest group */
if ( number_of_teams % 2 = = 0 )
number_of_matchdays = number_of_teams - 1 ;
2005-09-27 14:18:46 +02:00
else
2006-05-13 16:29:13 +02:00
number_of_matchdays = number_of_teams ;
2008-12-13 16:15:03 +01:00
number_of_matchdays * = cup_round - > round_robins ;
2007-02-18 14:04:19 +01:00
for ( i = 0 ; i < cup_round - > two_match_weeks [ 0 ] - > len ; i + + )
{
diff = g_array_index ( cup_round - > two_match_weeks [ 1 ] , gint , i ) -
g_array_index ( cup_round - > two_match_weeks [ 0 ] , gint , i ) ;
number_of_matchdays - = ( ( diff + diff % 2 ) / 2 ) ;
}
2005-01-09 21:21:22 +01:00
}
2007-02-18 14:04:19 +01:00
else if ( cup_round - > home_away )
number_of_matchdays = 2 - cup_round - > two_match_week ;
2005-01-09 21:21:22 +01:00
else
number_of_matchdays = 1 ;
return number_of_matchdays ;
}
2005-08-14 21:03:11 +02:00
/** Return the number of teams playing in
the given cup round .
2005-01-09 21:21:22 +01:00
@ param cup The cup we examine .
@ param cup_round The index of the cup round .
2005-08-14 21:03:11 +02:00
@ return The number teams in the round . */
2005-01-09 21:21:22 +01:00
gint
2005-08-14 21:03:11 +02:00
cup_round_get_number_of_teams ( const Cup * cup , gint round )
2005-01-09 21:21:22 +01:00
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " cup_round_get_number_of_teams \n " ) ;
# endif
2005-05-08 19:56:26 +02:00
const CupRound * cup_round = & g_array_index ( cup - > rounds , CupRound , round ) ;
2005-06-08 19:43:28 +02:00
gint number_of_teams = 0 ;
2005-01-09 21:21:22 +01:00
2005-05-08 19:56:26 +02:00
if ( round = = 0 )
2005-06-08 19:43:28 +02:00
number_of_teams = cup_round_get_new_teams ( cup_round ) ;
2005-05-08 19:56:26 +02:00
else if ( g_array_index ( cup - > rounds , CupRound , round - 1 ) . round_robin_number_of_groups > 0 )
2005-01-09 21:21:22 +01:00
{
number_of_teams =
2005-05-08 19:56:26 +02:00
( g_array_index ( cup - > rounds , CupRound , round - 1 ) . round_robin_number_of_groups *
g_array_index ( cup - > rounds , CupRound , round - 1 ) . round_robin_number_of_advance ) +
g_array_index ( cup - > rounds , CupRound , round - 1 ) . round_robin_number_of_best_advance +
2005-08-14 21:03:11 +02:00
cup_round_get_new_teams ( cup_round ) + cup_round_get_byes ( cup , round - 1 ) ;
2005-01-09 21:21:22 +01:00
}
2005-08-14 21:03:11 +02:00
else
number_of_teams = cup_round_get_number_of_teams ( cup , round - 1 ) / 2 +
cup_round_get_new_teams ( cup_round ) + cup_round_get_byes ( cup , round - 1 ) ;
return number_of_teams ;
}
/** Get the number of byes in the given cup round. */
gint
cup_round_get_byes ( const Cup * cup , gint round )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " cup_round_get_byes \n " ) ;
# endif
2005-08-14 21:03:11 +02:00
const CupRound * cup_round = & g_array_index ( cup - > rounds , CupRound , round ) ;
gint number_of_byes = 0 , new_teams = 0 ;
if ( cup_round - > byes ! = - 1 )
number_of_byes = cup_round - > byes ;
else
{
if ( round = = 0 )
new_teams = cup_round_get_new_teams ( cup_round ) ;
else
new_teams = cup_round_get_number_of_teams ( cup , round ) ;
2005-06-08 19:43:28 +02:00
2005-09-27 14:18:46 +02:00
number_of_byes = ( cup_round - > round_robin_number_of_groups = = 0 ) ?
math_get_bye_len ( new_teams ) : 0 ;
/* if(cup_round->round_robin_number_of_groups == 0) */
/* number_of_byes = math_get_bye_len(new_teams); */
/* else */
/* { */
/* while(new_teams % cup_round->round_robin_number_of_groups != 0) */
/* { */
/* new_teams--; */
/* number_of_byes++; */
/* } */
/* } */
}
2005-06-08 19:43:28 +02:00
2005-08-14 21:03:11 +02:00
return number_of_byes ;
2005-06-08 19:43:28 +02:00
}
/** Return the number of new teams that come into the
cup in the given cup round . */
gint
cup_round_get_new_teams ( const CupRound * cup_round )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " cup_round_get_new_teams \n " ) ;
# endif
2005-06-08 19:43:28 +02:00
gint i , new_teams = 0 ;
const Cup * cup_temp = NULL ;
const League * league = NULL ;
GPtrArray * teams_sorted = NULL ;
if ( cup_round - > new_teams ! = 0 )
new_teams = cup_round - > new_teams ;
2005-01-09 21:21:22 +01:00
else
2005-06-08 19:43:28 +02:00
{
for ( i = 0 ; i < cup_round - > choose_teams - > len ; i + + )
{
2005-09-22 18:41:07 +02:00
if ( g_array_index ( cup_round - > choose_teams , CupChooseTeam , i ) .
number_of_teams ! = - 1 )
new_teams + =
g_array_index ( cup_round - > choose_teams , CupChooseTeam , i ) .
number_of_teams ;
2005-06-08 19:43:28 +02:00
else
{
2005-09-22 18:41:07 +02:00
cup_get_choose_team_league_cup (
& g_array_index ( cup_round - > choose_teams , CupChooseTeam , i ) ,
& league , & cup_temp ) ;
2005-06-08 19:43:28 +02:00
if ( cup_temp = = NULL )
new_teams + = league - > teams - > len ;
else
{
teams_sorted = cup_get_teams_sorted ( cup_temp ) ;
new_teams + = teams_sorted - > len ;
g_ptr_array_free ( teams_sorted , TRUE ) ;
}
}
}
}
2005-01-09 21:21:22 +01:00
2005-06-08 19:43:28 +02:00
return new_teams ;
2005-01-09 21:21:22 +01:00
}
2005-03-03 13:46:48 +01:00
/** Return the cup pointer belonging to the id.
@ param clid The id we look for .
@ return The cup pointer or NULL if failed . */
Cup *
cup_from_clid ( gint clid )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " cup_from_clid \n " ) ;
# endif
2005-03-03 13:46:48 +01:00
gint i ;
for ( i = 0 ; i < cps - > len ; i + + )
if ( cp ( i ) . id = = clid )
return & cp ( i ) ;
2005-10-24 22:50:48 +02:00
main_exit_program ( EXIT_POINTER_NOT_FOUND ,
" cup_from_clid: didn't find cup with id %d \n " , clid ) ;
2005-06-15 21:13:35 +02:00
2005-05-06 18:35:19 +02:00
return NULL ;
}
2005-04-14 21:07:25 +02:00
2005-05-06 18:35:19 +02:00
/** Find the cup with the given sid. */
Cup *
cup_from_sid ( const gchar * sid )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " cup_from_sid \n " ) ;
# endif
2005-05-06 18:35:19 +02:00
gint i ;
for ( i = 0 ; i < cps - > len ; i + + )
2005-10-09 11:35:44 +02:00
if ( strcmp ( cp ( i ) . sid , sid ) = = 0 )
2005-05-06 18:35:19 +02:00
return & cp ( i ) ;
2005-10-24 22:50:48 +02:00
main_exit_program ( EXIT_POINTER_NOT_FOUND ,
" cup_from_sid: didn't find cup with sid %s \n " , sid ) ;
2005-06-15 21:13:35 +02:00
2005-03-03 13:46:48 +01:00
return NULL ;
}
2005-03-14 18:47:27 +01:00
2009-01-04 11:53:09 +01:00
/** Return a standard cup round name. */
2005-04-16 09:46:04 +02:00
void
cup_get_round_name ( const Cup * cup , gint round , gchar * buf )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " cup_get_round_name \n " ) ;
# endif
2005-04-16 09:46:04 +02:00
const CupRound * cup_round =
& g_array_index ( cup - > rounds , CupRound , round ) ;
if ( cup_round - > round_robin_number_of_groups > 0 )
{
strcpy ( buf , _ ( " Round robin " ) ) ;
return ;
}
switch ( cup - > rounds - > len - round )
{
default :
2005-06-26 13:42:01 +02:00
/* A cup stage, e.g. Last 32 when there are only 32 teams left. */
2005-05-28 12:41:53 +02:00
sprintf ( buf , _ ( " Last %d " ) , ( gint ) rint ( powf ( 2 , cup - > rounds - > len - round ) ) ) ;
2005-04-16 09:46:04 +02:00
break ;
case 1 :
strcpy ( buf , _ ( " Final " ) ) ;
break ;
case 2 :
strcpy ( buf , _ ( " Semi-final " ) ) ;
break ;
case 3 :
strcpy ( buf , _ ( " Quarter-final " ) ) ;
break ;
}
}
2005-04-22 17:17:39 +02:00
/** Find out whether the cup contains tables
that can be displayed . Returns - 1 if false
and the number of the cup round with tables
otherwise . */
gint
cup_has_tables ( gint clid )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " cup_has_tables \n " ) ;
# endif
2005-04-22 17:17:39 +02:00
const Cup * cup = cup_from_clid ( clid ) ;
gint i ;
for ( i = cup - > rounds - > len - 1 ; i > = 0 ; i - - )
if ( g_array_index ( cup - > rounds , CupRound , i ) . tables - > len > 0 )
return i ;
return - 1 ;
}
2005-05-01 12:16:14 +02:00
/** Return the team that won the cup. */
Team *
cup_get_winner ( const Cup * cup )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " cup_get_winner \n " ) ;
# endif
2005-05-01 12:16:14 +02:00
GPtrArray * teams_sorted = cup_get_teams_sorted ( cup ) ;
Team * tm = ( Team * ) g_ptr_array_index ( teams_sorted , 0 ) ;
g_ptr_array_free ( teams_sorted , TRUE ) ;
return tm ;
}
2005-05-06 18:35:19 +02:00
/** Find out whether a cup with add_week 1000 should begin now. */
gboolean
query_cup_begins ( const Cup * cup )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " query_cup_begins \n " ) ;
# endif
2005-11-26 17:52:51 +01:00
gint i , j ;
2005-06-08 19:43:28 +02:00
const League * league = NULL ;
const Cup * cup_temp = NULL ;
2005-11-26 17:52:51 +01:00
const CupRound * cup_round = NULL ;
2005-05-06 18:35:19 +02:00
2005-11-26 17:52:51 +01:00
for ( j = 0 ; j < cup - > rounds - > len ; j + + )
{
cup_round = & g_array_index ( cup - > rounds , CupRound , j ) ;
2005-05-06 18:35:19 +02:00
2005-11-26 17:52:51 +01:00
for ( i = 0 ; i < cup_round - > choose_teams - > len ; i + + )
2021-02-04 00:48:28 +01:00
if ( ! cup_choose_team_should_generate ( & g_array_index ( cup_round - > choose_teams , CupChooseTeam , i ) ) )
2005-11-26 17:52:51 +01:00
{
2021-02-21 01:34:21 +01:00
const Fixture * last_fixture ;
2005-11-26 17:52:51 +01:00
cup_get_choose_team_league_cup (
& g_array_index ( cup_round - > choose_teams ,
CupChooseTeam , i ) , & league , & cup_temp ) ;
2021-02-21 01:34:21 +01:00
if ( cup_temp = = NULL & &
2008-11-24 12:16:36 +01:00
query_league_active ( league ) & &
2006-05-07 18:32:52 +02:00
g_array_index ( league - > fixtures , Fixture ,
2021-02-21 01:34:21 +01:00
league - > fixtures - > len - 1 ) . attendance = = - 1 )
return FALSE ;
/* Handle cups */
if ( ! cup_temp )
continue ;
if ( ! cup_temp - > fixtures - > len )
return FALSE ;
last_fixture = & g_array_index ( cup_temp - > fixtures , Fixture ,
cup_temp - > fixtures - > len - 1 ) ;
/* attendance == 1 means there are scheduled games that have
* not been played yet . If the last fixture is not for the
* last round of the cup that means there are still games
* that have not been scheduled yet .
*/
if ( last_fixture - > attendance = = - 1 | |
last_fixture - > round ! = cup_temp - > rounds - > len - 1 )
2005-11-26 17:52:51 +01:00
return FALSE ;
}
}
2005-05-06 18:35:19 +02:00
2005-06-03 09:45:09 +02:00
return TRUE ;
2005-05-06 18:35:19 +02:00
}
2005-05-21 11:18:25 +02:00
/** Return the number of international cups in the country. */
2005-06-08 19:43:28 +02:00
gboolean
query_cup_transfer ( void )
2005-05-21 11:18:25 +02:00
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " query_cup_transfer \n " ) ;
# endif
2005-06-08 19:43:28 +02:00
gint i ;
2005-05-21 11:18:25 +02:00
2005-06-08 19:43:28 +02:00
for ( i = 0 ; i < acps - > len ; i + + )
if ( acp ( i ) - > teams - > len > 0 )
return TRUE ;
2005-05-21 11:18:25 +02:00
2005-06-08 19:43:28 +02:00
return FALSE ;
2005-05-21 11:18:25 +02:00
}
2005-05-28 16:34:14 +02:00
/** Find out whether the cup has a highlight property
and return the highlight colour . */
gchar *
cup_get_highlight_colour ( const Cup * cup )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " cup_get_highlight_colour \n " ) ;
# endif
# ifdef DEBUG
printf ( " cup_get_highlight_colour \n " ) ;
# endif
2005-05-28 16:34:14 +02:00
gint i ;
gchar buf [ SMALL ] ;
for ( i = 0 ; i < cup - > properties - > len ; i + + )
2005-10-05 21:59:37 +02:00
if ( g_str_has_prefix ( ( gchar * ) g_ptr_array_index ( cup - > properties , i ) , " highlight " ) )
2005-05-28 16:34:14 +02:00
{
sprintf ( buf , " string_cup_%s " ,
2005-10-05 21:59:37 +02:00
( gchar * ) g_ptr_array_index ( cup - > properties , i ) ) ;
2005-05-28 16:34:14 +02:00
return const_app ( buf ) ;
}
return NULL ;
}
2008-11-23 16:06:00 +01:00
/** Check the cup fixtures for suspicious entries. */
gboolean
cup_check_fixtures ( const Cup * cup )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " cup_check_fixtures \n " ) ;
# endif
2008-11-23 16:06:00 +01:00
gint i ;
for ( i = 0 ; i < cup - > fixtures - > len ; i + + )
{
if ( g_array_index ( cup - > fixtures , Fixture , i ) . teams [ 0 ] = =
g_array_index ( cup - > fixtures , Fixture , i ) . teams [ 1 ] )
{
2008-11-24 12:16:36 +01:00
if ( ! query_league_cup_has_property ( cup - > id , " silent_on_fixture_error " ) )
2009-04-29 19:18:54 +02:00
debug_print_message ( " cup_check_fixture: bad fixture found in cup %s; cup will be disabled \n " , cup - > name ) ;
2008-11-23 16:06:00 +01:00
return FALSE ;
}
}
return TRUE ;
}
2008-12-28 16:44:12 +01:00
/** Check whether we have to wait for other cups
before we can write fixtures for the cup round . */
gboolean
cup_round_check_waits ( const CupRound * cup_round )
{
gint i , j , k ;
2009-01-08 23:20:26 +01:00
gchar prefix [ SMALL ] ;
2008-12-28 16:44:12 +01:00
for ( i = 0 ; i < cup_round - > waits - > len ; i + + )
{
2009-01-08 23:20:26 +01:00
if ( g_str_has_suffix ( g_array_index ( cup_round - > waits , CupRoundWait , i ) . cup_sid , " * " ) )
g_utf8_strncpy ( prefix , g_array_index ( cup_round - > waits , CupRoundWait , i ) . cup_sid ,
g_utf8_strlen ( g_array_index ( cup_round - > waits , CupRoundWait , i ) . cup_sid , - 1 ) - 1 ) ;
else
strcpy ( prefix , " NONAME " ) ;
2008-12-28 16:44:12 +01:00
for ( j = 0 ; j < acps - > len ; j + + )
{
2009-01-08 23:20:26 +01:00
if ( strcmp ( acp ( j ) - > sid , g_array_index ( cup_round - > waits , CupRoundWait , i ) . cup_sid ) = = 0 | |
g_str_has_prefix ( acp ( j ) - > sid , prefix ) )
2008-12-28 16:44:12 +01:00
{
/* Cup round we're waiting for isn't even reached. */
if ( g_array_index ( acp ( j ) - > fixtures , Fixture , acp ( j ) - > fixtures - > len - 1 ) . round <
g_array_index ( cup_round - > waits , CupRoundWait , i ) . cup_round )
return TRUE ;
for ( k = acp ( j ) - > fixtures - > len - 1 ; k > = 0 ; k - - )
{
/* Cup round we've been waiting for is finished,
we ' re not waiting anymore . */
if ( g_array_index ( acp ( j ) - > fixtures , Fixture , k ) . round >
g_array_index ( cup_round - > waits , CupRoundWait , i ) . cup_round )
break ;
/* Still waiting for matches to be calculated. */
if ( g_array_index ( acp ( j ) - > fixtures , Fixture , k ) . round = =
g_array_index ( cup_round - > waits , CupRoundWait , i ) . cup_round & &
g_array_index ( acp ( j ) - > fixtures , Fixture , k ) . attendance = = - 1 )
return TRUE ;
}
break ;
}
}
}
return FALSE ;
}
2009-01-11 15:51:12 +01:00
/** Find out whether the cup chooses teams from itself
( e . g . the defending champion from last season ) . */
gboolean
query_cup_self_referential ( const Cup * cup )
{
gint i , j ;
for ( i = 0 ; i < cup - > rounds - > len ; i + + )
for ( j = 0 ; j < g_array_index ( cup - > rounds , CupRound , i ) . choose_teams - > len ; j + + )
if ( strcmp ( g_array_index ( g_array_index ( cup - > rounds , CupRound , i ) . choose_teams , CupChooseTeam , j ) . sid , cup - > sid ) = = 0 )
return TRUE ;
return FALSE ;
}
/** Find out if the cup is part of the array of
cups that are displayed in the game . */
gboolean
query_cup_hidden ( const Cup * cup )
{
gint i ;
for ( i = 0 ; i < acps - > len ; i + + )
if ( acp ( i ) = = cup )
return FALSE ;
return TRUE ;
}