2005-10-20 17:45:00 +02:00
/*
2005-11-26 17:52:51 +01:00
user . 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 .
*/
2005-10-27 23:10:15 +02:00
# include "bet.h"
2005-04-16 09:46:04 +02:00
# include "cup.h"
2005-08-20 21:39:36 +02:00
# include "file.h"
2005-06-20 14:46:57 +02:00
# include "finance.h"
2005-03-13 16:46:15 +01:00
# include "fixture.h"
2005-03-03 13:46:48 +01:00
# include "free.h"
2005-03-18 23:03:23 +01:00
# include "game_gui.h"
2005-04-16 09:46:04 +02:00
# include "league.h"
2005-04-04 12:36:04 +02:00
# include "live_game.h"
2005-06-15 21:13:35 +02:00
# include "main.h"
2005-01-09 21:21:22 +01:00
# include "maths.h"
2005-03-20 21:49:55 +01:00
# include "misc.h"
2005-06-20 14:46:57 +02:00
# include "name.h"
2005-03-03 13:46:48 +01:00
# include "option.h"
2005-01-09 21:21:22 +01:00
# include "player.h"
2005-06-20 14:46:57 +02:00
# include "support.h"
2005-03-27 19:59:57 +02:00
# include "team.h"
2005-03-20 21:49:55 +01:00
# include "transfer.h"
2005-04-07 18:44:33 +02:00
# include "treeview.h"
2005-01-09 21:21:22 +01:00
# include "user.h"
2005-03-20 21:49:55 +01:00
# include "window.h"
2005-08-20 21:39:36 +02:00
# include "xml_mmatches.h"
2005-07-08 11:26:00 +02:00
# include "youth_academy.h"
2005-01-09 21:21:22 +01:00
2005-03-03 13:46:48 +01:00
/** Create a new user with default values. */
User
user_new ( void )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " user_new \n " ) ;
# endif
2005-03-03 13:46:48 +01:00
User new ;
2005-10-09 11:35:44 +02:00
new . name = g_strdup ( " NONAME " ) ;
2005-03-03 13:46:48 +01:00
new . tm = NULL ;
2005-04-13 15:01:59 +02:00
new . team_id = - 1 ;
2005-04-04 12:36:04 +02:00
live_game_reset ( & new . live_game , NULL , FALSE ) ;
2005-03-10 21:59:39 +01:00
2005-03-18 23:03:23 +01:00
new . events = g_array_new ( FALSE , FALSE , sizeof ( Event ) ) ;
2005-04-16 09:46:04 +02:00
new . history = g_array_new ( FALSE , FALSE , sizeof ( UserHistory ) ) ;
2005-03-24 08:42:24 +01:00
new . options . list = NULL ;
new . options . datalist = NULL ;
2005-03-17 21:26:01 +01:00
2005-06-20 14:46:57 +02:00
new . sponsor . name = g_string_new ( " " ) ;
new . sponsor . contract = new . sponsor . benefit = - 1 ;
2005-09-21 19:42:41 +02:00
new . youth_academy . players = g_array_new ( FALSE , FALSE , sizeof ( Player ) ) ;
2005-07-14 18:05:10 +02:00
new . youth_academy . pos_pref = PLAYER_POS_ANY ;
new . youth_academy . coach = QUALITY_AVERAGE ;
2005-07-11 09:14:18 +02:00
2005-10-09 11:35:44 +02:00
new . mmatches_file = NULL ;
2005-08-20 21:39:36 +02:00
new . mmatches = g_array_new ( FALSE , FALSE , sizeof ( MemMatch ) ) ;
2005-10-27 23:10:15 +02:00
new . bets [ 0 ] = g_array_new ( FALSE , FALSE , sizeof ( BetUser ) ) ;
new . bets [ 1 ] = g_array_new ( FALSE , FALSE , sizeof ( BetUser ) ) ;
2005-03-03 13:46:48 +01:00
return new ;
}
/** Move a user's team to top or bottom league
at the beginning of a new game and set up the team .
@ param user The user we set up the team for . */
2005-01-09 21:21:22 +01:00
void
2005-03-03 13:46:48 +01:00
user_set_up_team_new_game ( User * user )
2005-01-09 21:21:22 +01:00
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " user_set_up_team_new_game \n " ) ;
# endif
2005-01-09 21:21:22 +01:00
gchar buf [ SMALL ] ;
2005-06-11 18:54:59 +02:00
gint rndom ;
2005-01-09 21:21:22 +01:00
2007-09-01 13:28:09 +02:00
/* If the user chose to start in a different league than
his team is originally in , the league is stored in the scout
variable . */
2005-06-11 18:54:59 +02:00
if ( user - > scout = = - 1 )
2005-05-02 17:14:41 +02:00
{
2007-09-01 13:28:09 +02:00
user_set_up_team ( user , TRUE ) ;
2005-12-01 12:50:54 +01:00
user_history_add ( user , USER_HISTORY_START_GAME ,
user - > tm - > name ,
league_cup_get_name_string ( user - > tm - > clid ) , NULL , NULL ) ;
2005-05-02 17:14:41 +02:00
}
2005-01-09 21:21:22 +01:00
else
{
2005-06-11 18:54:59 +02:00
rndom = math_rndi ( 0 , lig ( user - > scout ) . teams - > len - 1 ) ;
while ( team_is_user ( & g_array_index ( lig ( user - > scout ) . teams , Team , rndom ) ) ! = - 1 )
rndom = math_rndi ( 0 , lig ( user - > scout ) . teams - > len - 1 ) ;
2005-10-09 11:35:44 +02:00
sprintf ( buf , " %s " , g_array_index ( lig ( user - > scout ) . teams , Team , rndom ) . name ) ;
misc_string_assign ( & g_array_index ( lig ( user - > scout ) . teams , Team , rndom ) . name ,
user - > tm - > name ) ;
misc_string_assign ( & user - > tm - > name , buf ) ;
2005-01-09 21:21:22 +01:00
2005-06-11 18:54:59 +02:00
user - > tm = & g_array_index ( lig ( user - > scout ) . teams , Team , rndom ) ;
user - > team_id = g_array_index ( lig ( user - > scout ) . teams , Team , rndom ) . id ;
2005-01-09 21:21:22 +01:00
2005-12-01 12:50:54 +01:00
user_history_add ( user , USER_HISTORY_START_GAME ,
user - > tm - > name ,
league_cup_get_name_string ( user - > tm - > clid ) , NULL , NULL ) ;
2005-05-02 17:14:41 +02:00
2007-09-01 13:28:09 +02:00
user_set_up_team ( user , TRUE ) ;
2005-01-09 21:21:22 +01:00
}
}
2005-03-03 13:46:48 +01:00
/** Set up finances, remove some players etc. for a new user team.
2005-09-19 23:13:36 +02:00
@ par am user The user whose team we set up . */
2005-01-09 21:21:22 +01:00
void
2007-09-01 13:28:09 +02:00
user_set_up_team ( User * user , gboolean remove_players )
2005-01-09 21:21:22 +01:00
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " user_set_up_team \n " ) ;
# endif
2005-01-09 21:21:22 +01:00
gint i , j ;
2007-09-01 13:28:09 +02:00
if ( remove_players )
{
for ( i = PLAYER_POS_DEFENDER ; i < = PLAYER_POS_FORWARD ; i + + )
for ( j = user - > tm - > players - > len - 1 ; j > 10 ; j - - )
if ( g_array_index ( user - > tm - > players , Player , j ) . pos = = i & &
g_array_index ( user - > tm - > players , Player , j ) . recovery ! = 1 )
{
player_remove_from_team ( user - > tm , j ) ;
break ;
}
}
2005-01-09 21:21:22 +01:00
2005-06-21 15:33:16 +02:00
for ( i = 0 ; i < user - > tm - > players - > len ; i + + )
g_array_index ( user - > tm - > players , Player , i ) . recovery = 0 ;
2005-03-03 13:46:48 +01:00
user - > scout = user - > physio = QUALITY_AVERAGE ;
2005-09-19 23:13:36 +02:00
if ( sett_int ( " int_opt_disable_transfers " ) )
user - > physio = QUALITY_GOOD ;
2005-01-09 21:21:22 +01:00
2005-04-16 09:46:04 +02:00
user - > tm - > style = 0 ;
2005-03-03 13:46:48 +01:00
user_set_up_finances ( user ) ;
2005-03-18 23:03:23 +01:00
user_set_up_counters ( user ) ;
2005-06-20 14:59:03 +02:00
2005-09-19 23:13:36 +02:00
if ( ! sett_int ( " int_opt_disable_ya " ) )
2005-09-21 19:42:41 +02:00
youth_academy_new ( user ) ;
2005-07-08 11:26:00 +02:00
2005-09-19 23:13:36 +02:00
user - > counters [ COUNT_USER_NEW_SPONSOR ] =
( sett_int ( " int_opt_disable_finances " ) ) ? - 5 : 1 ;
2006-12-10 13:43:29 +01:00
option_set_int ( " int_opt_user_penalty_shooter " ,
& user - > options , - 1 ) ;
2005-01-09 21:21:22 +01:00
}
2005-03-18 23:03:23 +01:00
/** Set the counters of the user to their initial values. */
void
user_set_up_counters ( User * user )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " user_set_up_counters \n " ) ;
# endif
2005-03-18 23:03:23 +01:00
gint i ;
for ( i = 0 ; i < COUNT_USER_END ; i + + )
user - > counters [ i ] = 0 ;
user - > counters [ COUNT_USER_LOAN ] =
user - > counters [ COUNT_USER_POSITIVE ] = - 1 ;
2008-05-03 17:03:30 +02:00
user - > counters [ COUNT_USER_TRAININGS_LEFT_SEASON ] =
const_int ( " int_training_camps_per_season " ) ;
2005-03-18 23:03:23 +01:00
}
2006-12-10 15:43:16 +01:00
2005-03-03 13:46:48 +01:00
/** Set up the user's finances when he's got a new team.
@ param user The user we set up the finances for . */
2005-01-09 21:21:22 +01:00
void
2005-03-03 13:46:48 +01:00
user_set_up_finances ( User * user )
2005-01-09 21:21:22 +01:00
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " user_set_up_finances \n " ) ;
# endif
2005-01-09 21:21:22 +01:00
gint i ;
2005-03-16 21:48:25 +01:00
for ( i = 0 ; i < MON_OUT_END ; i + + )
user - > money_out [ 0 ] [ i ] = user - > money_out [ 1 ] [ i ] = 0 ;
for ( i = 0 ; i < MON_IN_END ; i + + )
user - > money_in [ 0 ] [ i ] = user - > money_in [ 1 ] [ i ] = 0 ;
2008-11-07 09:38:55 +01:00
user - > debt =
user - > alr_start_week =
user - > alr_weekly_installment = 0 ;
2005-03-16 21:48:25 +01:00
user - > money =
2005-03-03 13:46:48 +01:00
math_round_integer ( user - > tm - > stadium . capacity *
math_rndi ( const_int ( " int_initial_money_lower " ) ,
const_int ( " int_initial_money_upper " ) ) , 2 ) ;
}
/** Remove a user from the game.
@ param idx The index of the user in the # users array .
@ param regenerate_team Whether the user ' s team has to be
regenerated . */
void
user_remove ( gint idx , gboolean regenerate_team )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " user_remove \n " ) ;
# endif
2005-03-27 19:59:57 +02:00
gint i ;
if ( regenerate_team )
{
for ( i = 0 ; i < usr ( idx ) . tm - > players - > len ; i + + )
free_player ( & g_array_index ( usr ( idx ) . tm - > players , Player , i ) ) ;
2005-05-14 17:10:25 +02:00
g_array_free ( usr ( idx ) . tm - > players , TRUE ) ;
usr ( idx ) . tm - > players = g_array_new ( FALSE , FALSE , sizeof ( Player ) ) ;
2006-12-10 15:43:16 +01:00
usr ( idx ) . tm - > luck = 1 ;
2005-12-01 12:50:54 +01:00
team_generate_players_stadium ( usr ( idx ) . tm , 0 ) ;
2005-05-14 17:10:25 +02:00
for ( i = 0 ; i < usr ( idx ) . tm - > players - > len ; i + + )
g_array_index ( usr ( idx ) . tm - > players , Player , i ) . team = usr ( idx ) . tm ;
2005-03-27 19:59:57 +02:00
}
2005-03-03 13:46:48 +01:00
free_user ( & usr ( idx ) ) ;
g_array_remove_index ( users , idx ) ;
2005-05-14 17:10:25 +02:00
cur_user = 0 ;
2005-05-15 16:42:58 +02:00
if ( window . main ! = NULL )
game_gui_show_main ( ) ;
2005-03-03 13:46:48 +01:00
}
void
user_set_player_list_attributes ( const User * user , PlayerListAttribute * attribute , gint list_number )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " user_set_player_list_attributes \n " ) ;
# endif
2005-03-03 13:46:48 +01:00
gint i , cnt = 0 ;
gchar prefix [ SMALL ] ;
sprintf ( prefix , " int_opt_user_pl%d_att " , list_number ) ;
2005-01-09 21:21:22 +01:00
2005-03-23 20:03:26 +01:00
for ( i = 0 ; i < user - > options . list - > len ; i + + )
2005-10-09 11:35:44 +02:00
if ( g_str_has_prefix ( g_array_index ( user - > options . list , Option , i ) . name , prefix ) )
2005-03-03 13:46:48 +01:00
{
2005-03-23 20:03:26 +01:00
attribute - > on_off [ cnt ] = g_array_index ( user - > options . list , Option , i ) . value ;
2005-03-03 13:46:48 +01:00
cnt + + ;
}
2005-01-09 21:21:22 +01:00
}
2005-03-13 16:46:15 +01:00
2008-11-21 00:58:21 +01:00
/** Find out whether there are user events. */
gboolean
query_user_events ( )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " query_user_events \n " ) ;
# endif
2008-11-21 00:58:21 +01:00
gint i ;
for ( i = 0 ; i < users - > len ; i + + )
if ( usr ( i ) . events - > len ! = 0 )
return TRUE ;
return FALSE ;
}
2005-03-13 16:46:15 +01:00
/** Find out whether there are user games at the specified date. */
gboolean
2005-04-07 18:44:33 +02:00
query_user_games_in_week_round ( gint week_number , gint week_round_number )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " query_user_games_in_week_round \n " ) ;
# endif
2005-04-07 18:44:33 +02:00
gint i ;
for ( i = 0 ; i < users - > len ; i + + )
if ( query_user_game_in_week_round ( i , week_number , week_round_number ) )
return TRUE ;
return FALSE ;
}
/** Find out whether the specified user has a match at the specified date. */
gboolean
query_user_game_in_week_round ( gint usr_idx , gint week_number , gint week_round_number )
2005-03-13 16:46:15 +01:00
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " query_user_game_in_week_round \n " ) ;
# endif
2005-03-13 16:46:15 +01:00
gint i , j ;
2005-05-21 11:18:25 +02:00
for ( i = 0 ; i < ligs - > len ; i + + )
for ( j = 0 ; j < lig ( i ) . fixtures - > len ; j + + )
if ( fixture_user_team_involved ( & g_array_index ( lig ( i ) . fixtures , Fixture , j ) ) = = usr_idx & &
g_array_index ( lig ( i ) . fixtures , Fixture , j ) . week_number = = week_number & &
g_array_index ( lig ( i ) . fixtures , Fixture , j ) . week_round_number = = week_round_number )
return TRUE ;
for ( i = 0 ; i < acps - > len ; i + + )
for ( j = 0 ; j < acp ( i ) - > fixtures - > len ; j + + )
if ( fixture_user_team_involved ( & g_array_index ( acp ( i ) - > fixtures , Fixture , j ) ) = = usr_idx & &
g_array_index ( acp ( i ) - > fixtures , Fixture , j ) . week_number = = week_number & &
g_array_index ( acp ( i ) - > fixtures , Fixture , j ) . week_round_number = = week_round_number )
return TRUE ;
2005-03-13 16:46:15 +01:00
return FALSE ;
}
2005-03-14 18:47:27 +01:00
2005-04-07 18:44:33 +02:00
2005-03-14 18:47:27 +01:00
/** Get the user managing the team.
@ param tm The team .
@ return A pointer to the user . */
User *
user_from_team ( const Team * tm )
{
gint i ;
for ( i = 0 ; i < users - > len ; i + + )
if ( usr ( i ) . tm = = tm )
return & usr ( i ) ;
2005-10-24 22:50:48 +02:00
main_exit_program ( EXIT_POINTER_NOT_FOUND ,
" User going with team %s not found. \n " ,
tm - > name ) ;
2005-06-15 21:13:35 +02:00
2005-03-14 18:47:27 +01:00
return NULL ;
}
2005-03-17 21:26:01 +01:00
2005-04-25 18:49:54 +02:00
/** Check the success counter of the user and offer a new job or
fire him if the counter says so . */
void
user_job_offer ( User * user )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " user_job_offer \n " ) ;
# endif
2005-04-25 18:49:54 +02:00
Team * new_team = NULL ;
if ( math_rnd ( 0 , 1 ) > const_float ( " float_user_success_counter_check " ) | |
2005-12-01 12:50:54 +01:00
ABS ( user - > counters [ COUNT_USER_SUCCESS ] ) <
( gfloat ) const_int ( " int_user_success_offer_limit " ) * 0.9 )
2005-04-25 18:49:54 +02:00
return ;
2005-12-01 12:50:54 +01:00
if ( user - > counters [ COUNT_USER_SUCCESS ] <
- ( gfloat ) const_int ( " int_user_success_offer_limit " ) * 0.9 & &
2005-04-25 18:49:54 +02:00
! user - > counters [ COUNT_USER_WARNING ] )
{
2005-07-08 11:26:00 +02:00
user_event_add ( user , EVENT_TYPE_WARNING , - 1 , - 1 , NULL ,
2005-10-09 11:35:44 +02:00
_ ( " The owners of %s are not satisfied with the recent performance of the team. There are rumours they're looking for a new manager. " ) , user - > tm - > name ) ;
2005-04-25 18:49:54 +02:00
user - > counters [ COUNT_USER_WARNING ] = 1 ;
return ;
}
if ( math_rnd ( 0 , 1 ) < const_float ( " float_user_success_base_prob " ) *
log ( ( gfloat ) ABS ( user - > counters [ COUNT_USER_SUCCESS ] ) *
const_float ( " float_user_success_prob_factor " ) ) )
{
if ( user - > counters [ COUNT_USER_SUCCESS ] < 0 )
{
new_team = team_get_new ( user - > tm , TRUE ) ;
user_event_add ( user , EVENT_TYPE_FIRE_FAILURE , - 1 , - 1 , new_team , NULL ) ;
}
else if ( option_int ( " int_opt_user_show_job_offers " , & user - > options ) )
{
new_team = team_get_new ( user - > tm , FALSE ) ;
user_event_add ( user , EVENT_TYPE_JOB_OFFER , - 1 , - 1 , new_team , NULL ) ;
}
}
}
2005-03-17 21:26:01 +01:00
/** Update the counters of the users. */
void
user_weekly_update_counters ( User * user )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " user_weekly_update_counters \n " ) ;
# endif
2005-04-24 12:17:20 +02:00
gint rank = team_get_league_rank ( user - > tm ) ;
2005-05-08 19:56:26 +02:00
gint teamslen = ( ( GArray * ) ( league_cup_get_teams ( user - > tm - > clid ) ) ) - > len ;
2005-12-01 12:50:54 +01:00
gint rank_bounds [ 2 ] =
{ ( gint ) rint ( const_float ( " float_user_success_table_bound_upper " ) *
( gfloat ) teamslen ) ,
( gint ) rint ( const_float ( " float_user_success_table_bound_lower " ) *
( gfloat ) teamslen ) } ;
2005-03-17 21:26:01 +01:00
gint * cnts = user - > counters ;
gint increase_capacity ;
gfloat increase_safety ;
2008-05-03 17:03:30 +02:00
cnts [ COUNT_USER_TRAININGS_WEEK ] = 0 ;
2005-03-17 21:26:01 +01:00
if ( cnts [ COUNT_USER_STADIUM_CAPACITY ] > 0 )
{
increase_capacity = math_rndi ( const_int ( " int_stadium_improvement_base_seats " ) -
const_int ( " int_stadium_improvement_seats_variance " ) ,
const_int ( " int_stadium_improvement_base_seats " ) +
const_int ( " int_stadium_improvement_seats_variance " ) ) ;
user - > tm - > stadium . capacity + = MIN ( cnts [ COUNT_USER_STADIUM_CAPACITY ] ,
increase_capacity ) ;
cnts [ COUNT_USER_STADIUM_CAPACITY ] =
MAX ( cnts [ COUNT_USER_STADIUM_CAPACITY ] - increase_capacity , 0 ) ;
}
if ( cnts [ COUNT_USER_STADIUM_SAFETY ] > 0 )
{
increase_safety = math_rnd ( const_float ( " float_stadium_improvement_base_safety " ) -
const_float ( " float_stadium_improvement_safety_variance " ) ,
const_float ( " float_stadium_improvement_base_safety " ) +
const_float ( " float_stadium_improvement_safety_variance " ) ) ;
user - > tm - > stadium . safety + = MIN ( ( gfloat ) cnts [ COUNT_USER_STADIUM_SAFETY ] / 100 ,
increase_safety ) ;
user - > tm - > stadium . safety = MIN ( user - > tm - > stadium . safety , 1 ) ;
cnts [ COUNT_USER_STADIUM_SAFETY ] =
MAX ( cnts [ COUNT_USER_STADIUM_SAFETY ] - ( gint ) rint ( increase_safety * 100 ) , 0 ) ;
}
2005-04-24 12:17:20 +02:00
2005-04-24 16:19:57 +02:00
if ( query_league_matches_in_week ( league_from_clid ( user - > tm - > clid ) , week ) )
2005-04-24 12:17:20 +02:00
{
2005-04-24 16:19:57 +02:00
if ( rank < rank_bounds [ 0 ] )
user - > counters [ COUNT_USER_SUCCESS ] + = ( rank_bounds [ 0 ] - rank ) ;
else if ( rank > rank_bounds [ 1 ] )
2005-04-25 21:01:11 +02:00
user - > counters [ COUNT_USER_SUCCESS ] - = ( rank - rank_bounds [ 1 ] ) ;
2005-04-24 12:17:20 +02:00
else
2005-04-24 16:19:57 +02:00
{
if ( user - > counters [ COUNT_USER_SUCCESS ] > 0 )
user - > counters [ COUNT_USER_SUCCESS ] - = const_int ( " int_user_success_mediocre_rank_change " ) ;
else
user - > counters [ COUNT_USER_SUCCESS ] + = const_int ( " int_user_success_mediocre_rank_change " ) ;
}
2005-04-24 12:17:20 +02:00
}
2005-03-18 23:03:23 +01:00
}
/** Return a default new user event. */
Event
user_event_new ( void )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " user_event_new \n " ) ;
# endif
2005-03-18 23:03:23 +01:00
Event new ;
new . user = NULL ;
new . type = - 1 ;
new . value1 = new . value2 = - 1 ;
2005-03-19 21:04:08 +01:00
new . value_pointer = NULL ;
new . value_string = NULL ;
2005-03-18 23:03:23 +01:00
return new ;
}
/** Add an event with the specified values to the event array of the user. */
void
user_event_add ( User * user , gint type , gint value1 , gint value2 ,
2005-07-08 11:26:00 +02:00
gpointer value_pointer , gchar * format , . . . )
2005-03-18 23:03:23 +01:00
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " user_event_add \n " ) ;
# endif
2005-03-18 23:03:23 +01:00
Event new = user_event_new ( ) ;
2005-07-08 11:26:00 +02:00
gchar text [ SMALL ] ;
va_list args ;
2005-03-18 23:03:23 +01:00
new . user = user ;
new . type = type ;
new . value1 = value1 ;
new . value2 = value2 ;
2005-03-19 21:04:08 +01:00
new . value_pointer = value_pointer ;
2005-03-18 23:03:23 +01:00
2005-07-08 11:26:00 +02:00
if ( format ! = NULL )
{
va_start ( args , format ) ;
g_vsprintf ( text , format , args ) ;
va_end ( args ) ;
2005-10-09 11:35:44 +02:00
new . value_string = g_strdup ( text ) ;
2005-07-08 11:26:00 +02:00
}
2005-03-21 11:16:37 +01:00
else
new . value_string = NULL ;
2005-03-18 23:03:23 +01:00
g_array_append_val ( user - > events , new ) ;
}
/** Remove an event from the user event array.
@ param user The user .
@ param idx The index of the event in the array . */
void
user_event_remove ( User * user , gint idx )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " user_event_remove \n " ) ;
# endif
2005-03-18 23:03:23 +01:00
free_event ( & g_array_index ( user - > events , Event , idx ) ) ;
g_array_remove_index ( user - > events , idx ) ;
}
/** Show the next event for the current user. */
void
user_event_show_next ( void )
2008-11-25 14:50:07 +01:00
{
# ifdef DEBUG
printf ( " user_event_show_next \n " ) ;
# endif
2005-03-18 23:03:23 +01:00
Event * event = NULL ;
2005-05-26 11:51:28 +02:00
gchar buf [ SMALL ] ,
2005-03-20 21:49:55 +01:00
buf2 [ SMALL ] , buf3 [ SMALL ] ;
2005-03-18 23:03:23 +01:00
if ( current_user . events - > len = = 0 )
return ;
2005-04-27 17:50:26 +02:00
stat4 = STATUS_SHOW_EVENT ;
2005-03-18 23:03:23 +01:00
event = & g_array_index ( current_user . events , Event , 0 ) ;
switch ( event - > type )
{
default :
g_warning ( " user_event_show_next: unknown event type %d \n " , event - > type ) ;
break ;
case EVENT_TYPE_PLAYER_LEFT :
2005-07-08 11:26:00 +02:00
game_gui_show_warning ( _ ( " %s has left your team because his contract expired. " ) ,
2005-10-09 11:35:44 +02:00
event - > value_string ) ;
2005-03-18 23:03:23 +01:00
break ;
2005-03-19 21:04:08 +01:00
case EVENT_TYPE_WARNING :
2005-10-09 11:35:44 +02:00
game_gui_show_warning ( event - > value_string ) ;
2005-03-18 23:03:23 +01:00
break ;
case EVENT_TYPE_FIRE_FINANCE :
2005-12-01 12:50:54 +01:00
game_gui_show_job_offer ( ( Team * ) event - > value_pointer , NULL ,
STATUS_JOB_OFFER_FIRE_FINANCE ) ;
2005-03-19 21:04:08 +01:00
break ;
2005-04-25 18:49:54 +02:00
case EVENT_TYPE_FIRE_FAILURE :
2005-12-01 12:50:54 +01:00
game_gui_show_job_offer ( ( Team * ) event - > value_pointer , NULL ,
STATUS_JOB_OFFER_FIRE_FAILURE ) ;
2005-04-25 18:49:54 +02:00
break ;
case EVENT_TYPE_JOB_OFFER :
2005-12-01 12:50:54 +01:00
game_gui_show_job_offer ( ( Team * ) event - > value_pointer , NULL ,
STATUS_JOB_OFFER_SUCCESS ) ;
2005-04-25 18:49:54 +02:00
break ;
2005-03-19 21:04:08 +01:00
case EVENT_TYPE_OVERDRAW :
if ( event - > value1 = = 1 )
2005-07-08 11:26:00 +02:00
game_gui_show_warning ( _ ( " You have overdrawn your bank account. The team owners give you %d weeks to get above your drawing credit limit. " ) , const_int ( " int_finance_overdraw_positive " ) ) ;
2005-03-19 21:04:08 +01:00
else
2005-07-08 11:26:00 +02:00
game_gui_show_warning ( _ ( " You have overdrawn your bank account once again. Bear in mind that after the fourth time you get fired. \n The team owners give you %d weeks to get above your drawing credit limit. " ) , const_int ( " int_finance_overdraw_positive " ) ) ;
2005-03-19 21:04:08 +01:00
break ;
2005-05-27 20:48:36 +02:00
case EVENT_TYPE_TRANSFER_OFFER_USER :
2005-07-08 11:26:00 +02:00
game_gui_show_warning ( _ ( " Have a look at the transfer list, there's an offer for %s. " ) ,
2005-10-09 11:35:44 +02:00
event - > value_string ) ;
2005-05-27 20:48:36 +02:00
break ;
case EVENT_TYPE_TRANSFER_OFFER_CPU :
2005-07-08 11:26:00 +02:00
game_gui_show_warning ( _ ( " Your offer for %s has been accepted. If you still want to buy him, go to the transfer list and left click on the player. " ) ,
2005-10-09 11:35:44 +02:00
event - > value_string ) ;
2005-05-27 20:48:36 +02:00
break ;
2005-05-26 11:51:28 +02:00
case EVENT_TYPE_TRANSFER_OFFER_REJECTED_BETTER_OFFER :
2005-07-01 22:51:46 +02:00
misc_print_grouped_int ( event - > value1 , buf2 ) ;
misc_print_grouped_int ( event - > value2 , buf3 ) ;
2005-10-09 11:35:44 +02:00
game_gui_show_warning ( _ ( " The owners of %s have rejected your offer (%s / %s) for %s. There was a better offer for the player than yours. " ) , ( ( Team * ) event - > value_pointer ) - > name , buf2 , buf3 , event - > value_string ) ;
2005-05-26 11:51:28 +02:00
break ;
case EVENT_TYPE_TRANSFER_OFFER_REJECTED_FEE_WAGE :
2005-07-01 22:51:46 +02:00
misc_print_grouped_int ( event - > value1 , buf2 ) ;
misc_print_grouped_int ( event - > value2 , buf3 ) ;
2005-10-09 11:35:44 +02:00
game_gui_show_warning ( _ ( " The owners of %s have rejected your offer (%s / %s) for %s. Neither the fee nor the wage you offered were acceptable, they say. " ) , ( ( Team * ) event - > value_pointer ) - > name , buf2 , buf3 , event - > value_string ) ;
2005-05-26 11:51:28 +02:00
break ;
case EVENT_TYPE_TRANSFER_OFFER_REJECTED_FEE :
2005-07-01 22:51:46 +02:00
misc_print_grouped_int ( event - > value1 , buf2 ) ;
misc_print_grouped_int ( event - > value2 , buf3 ) ;
2005-10-09 11:35:44 +02:00
game_gui_show_warning ( _ ( " The owners of %s have rejected your offer (%s / %s) for %s. The team owners weren't satisfied with the fee you offered. " ) , ( ( Team * ) event - > value_pointer ) - > name , buf2 , buf3 , event - > value_string ) ;
2005-05-26 11:51:28 +02:00
break ;
case EVENT_TYPE_TRANSFER_OFFER_REJECTED_WAGE :
2005-07-01 22:51:46 +02:00
misc_print_grouped_int ( event - > value1 , buf2 ) ;
misc_print_grouped_int ( event - > value2 , buf3 ) ;
2005-06-26 13:42:01 +02:00
/* A player from a team has rejected a transfer offer. */
2005-07-08 11:26:00 +02:00
game_gui_show_warning ( _ ( " %s of %s has rejected your offer (%s / %s). He wasn't satisfied with the wage you offered. " ) ,
2005-10-09 11:35:44 +02:00
event - > value_string ,
( ( Team * ) event - > value_pointer ) - > name , buf2 , buf3 ) ;
2005-03-19 21:04:08 +01:00
break ;
2008-05-02 14:26:35 +02:00
case EVENT_TYPE_TRANSFER_OFFER_REJECTED_STARS :
/* A player from a team has rejected a transfer offer. */
game_gui_show_warning ( _ ( " %s of %s has rejected your offer because your team has too many star players already. 'A player of my caliber doesn't play second fiddle,' he was quoted. " ) ,
event - > value_string ,
( ( Team * ) event - > value_pointer ) - > name ) ;
break ;
2005-03-19 21:04:08 +01:00
case EVENT_TYPE_TRANSFER_OFFER_MONEY :
2005-06-26 13:42:01 +02:00
/* Buy a player from a team. */
2005-07-08 11:26:00 +02:00
game_gui_show_warning ( _ ( " You didn't have enough money to buy %s from %s. " ) ,
2005-10-09 11:35:44 +02:00
event - > value_string , ( ( Team * ) event - > value_pointer ) - > name ) ;
2005-03-19 21:04:08 +01:00
break ;
case EVENT_TYPE_TRANSFER_OFFER_ROSTER :
2005-06-26 13:42:01 +02:00
/* Buy a player from a team. */
2005-07-08 11:26:00 +02:00
game_gui_show_warning ( _ ( " Your roster is full. You couldn't buy %s from %s. " ) ,
2005-10-09 11:35:44 +02:00
event - > value_string , ( ( Team * ) event - > value_pointer ) - > name ) ;
2005-03-19 21:04:08 +01:00
break ;
2005-04-07 18:44:33 +02:00
case EVENT_TYPE_PLAYER_CAREER_STOP :
2005-10-09 11:35:44 +02:00
sprintf ( buf , _ ( " %s's injury was so severe that he can't play football on a professional level anymore. He leaves your team. " ) , player_of_id_team ( event - > user - > tm , event - > value1 ) - > name ) ;
2005-04-07 18:44:33 +02:00
if ( event - > user - > tm - > players - > len < 12 )
{
strcat ( buf , _ ( " Fortunately he's got a cousin who can help your team out. " ) ) ;
2005-04-13 15:01:59 +02:00
player_replace_by_new ( player_of_id_team ( event - > user - > tm , event - > value1 ) , TRUE ) ;
2005-04-07 18:44:33 +02:00
}
else
player_remove_from_team ( event - > user - > tm , player_id_index ( event - > user - > tm , event - > value1 ) ) ;
treeview_show_user_player_list ( ) ;
game_gui_show_warning ( buf ) ;
break ;
2006-06-29 18:06:52 +02:00
case EVENT_TYPE_CHARITY :
game_gui_show_warning ( _ ( " The team owners are very satisfied with your financial management. Since the team's been earning a lot of money lately, they decide to donate half of it to charity. " ) ) ;
break ;
2005-03-18 23:03:23 +01:00
}
2005-03-20 21:49:55 +01:00
user_event_remove ( & current_user , 0 ) ;
2005-03-18 23:03:23 +01:00
}
/** Change the team of a user. */
void
user_change_team ( User * user , Team * tm )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " user_change_team \n " ) ;
# endif
2005-03-18 23:03:23 +01:00
gint i ;
2005-12-01 12:50:54 +01:00
gint success = user - > counters [ COUNT_USER_SUCCESS ] ;
2005-04-22 17:17:39 +02:00
2008-10-30 23:05:20 +01:00
/* Reset the ticketprice of the old team to the default value */
user - > tm - > stadium . ticket_price = const_int ( " int_team_stadium_ticket_price " ) ;
2005-03-18 23:03:23 +01:00
user - > tm = tm ;
2005-04-13 15:01:59 +02:00
user - > team_id = tm - > id ;
2005-03-17 21:26:01 +01:00
2007-09-01 13:28:09 +02:00
user_set_up_team ( user , FALSE ) ;
2005-06-20 14:59:03 +02:00
user - > counters [ COUNT_USER_NEW_SPONSOR ] = 0 ;
2005-12-01 12:50:54 +01:00
user - > counters [ COUNT_USER_SUCCESS ] = ( success < 0 ) ?
0 : ( gint ) rint ( ( gfloat ) success / 2 ) ;
2005-06-20 14:59:03 +02:00
g_string_free ( user - > sponsor . name , TRUE ) ;
user - > sponsor = user_get_sponsor ( & current_user ) ;
2005-03-18 23:03:23 +01:00
for ( i = user - > events - > len - 1 ; i > = 0 ; i - - )
2006-12-10 15:43:16 +01:00
user_event_remove ( user , i ) ;
2005-03-17 21:26:01 +01:00
}
2005-03-19 21:04:08 +01:00
2005-04-07 18:44:33 +02:00
/** Check whether one of the user teams has an unfit player
in the startup formation . */
gboolean
query_user_teams_have_unfit ( void )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " query_user_teams_have_unfit \n " ) ;
# endif
2005-04-07 18:44:33 +02:00
gint i , j ;
for ( i = 0 ; i < users - > len ; i + + )
{
if ( query_user_game_in_week_round ( i , week , week_round ) )
{
for ( j = 0 ; j < 11 ; j + + )
if ( g_array_index ( usr ( i ) . tm - > players , Player , j ) . health ! = 0 | |
2005-04-23 12:39:46 +02:00
player_is_banned ( & g_array_index ( usr ( i ) . tm - > players , Player , j ) ) > 0 )
2005-04-07 18:44:33 +02:00
return TRUE ;
}
}
return FALSE ;
}
2005-04-16 09:46:04 +02:00
/** Compare function for history sorting. */
gint
user_history_compare ( gconstpointer a , gconstpointer b )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " user_history_compare \n " ) ;
# endif
2005-04-16 09:46:04 +02:00
gint return_value = 0 ;
const UserHistory * his1 = ( const UserHistory * ) a ,
* his2 = ( const UserHistory * ) b ;
if ( his1 - > season < his2 - > season )
return_value = 1 ;
else if ( his1 - > season > his2 - > season )
return_value = - 1 ;
else if ( his1 - > week < his2 - > week )
return_value = 1 ;
else if ( his1 - > week > his2 - > week )
return_value = - 1 ;
return return_value ;
}
/** Add an element to the user history filled with the given values. */
void
2005-12-01 12:50:54 +01:00
user_history_add ( User * user , gint type , const gchar * team_name ,
const gchar * string0 , const gchar * string1 ,
const gchar * string2 )
2005-04-16 09:46:04 +02:00
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " user_history_add \n " ) ;
# endif
2005-04-16 09:46:04 +02:00
gint i ;
UserHistory new_history ;
UserHistory * his = & new_history ;
gboolean replace = FALSE ;
2005-12-01 12:50:54 +01:00
his - > team_name = his - > string [ 0 ] =
his - > string [ 1 ] = his - > string [ 2 ] = NULL ;
2005-04-16 09:46:04 +02:00
if ( type = = USER_HISTORY_WIN_FINAL | |
type = = USER_HISTORY_LOSE_FINAL | |
type = = USER_HISTORY_REACH_CUP_ROUND )
{
for ( i = 0 ; i < user - > history - > len ; i + + )
2005-12-01 12:50:54 +01:00
if ( ( g_array_index ( user - > history , UserHistory , i ) . type = =
USER_HISTORY_WIN_FINAL | |
g_array_index ( user - > history , UserHistory , i ) . type = =
USER_HISTORY_LOSE_FINAL | |
g_array_index ( user - > history , UserHistory , i ) . type = =
USER_HISTORY_REACH_CUP_ROUND ) & &
2005-04-16 09:46:04 +02:00
g_array_index ( user - > history , UserHistory , i ) . season = = season & &
2005-12-01 12:50:54 +01:00
strcmp ( g_array_index ( user - > history , UserHistory , i ) . team_name ,
team_name ) = = 0 & &
2005-12-09 16:52:57 +01:00
strcmp ( g_array_index ( user - > history , UserHistory , i ) . string [ 0 ] ,
string0 ) = = 0 )
2005-04-16 09:46:04 +02:00
{
/** Same cup round. */
2005-12-19 19:29:52 +01:00
if ( type = = USER_HISTORY_REACH_CUP_ROUND & &
string2 ! = NULL & &
strcmp ( g_array_index ( user - > history , UserHistory , i ) . string [ 2 ] ,
string2 ) = = 0 )
2005-04-16 09:46:04 +02:00
return ;
his = & g_array_index ( user - > history , UserHistory , i ) ;
replace = TRUE ;
break ;
}
}
2005-12-01 12:50:54 +01:00
if ( type = = USER_HISTORY_PROMOTED )
2005-04-25 18:49:54 +02:00
user - > counters [ COUNT_USER_SUCCESS ] + =
const_int ( " int_user_success_promotion " ) ;
else if ( type = = USER_HISTORY_RELEGATED )
user - > counters [ COUNT_USER_SUCCESS ] - =
const_int ( " int_user_success_relegation " ) ;
2005-04-24 12:17:20 +02:00
2005-04-16 09:46:04 +02:00
his - > season = season ;
his - > week = week ;
his - > type = type ;
if ( replace )
{
2005-12-01 12:50:54 +01:00
if ( string0 ! = NULL )
misc_string_assign ( & his - > string [ 0 ] , string0 ) ;
2005-12-09 16:52:57 +01:00
if ( string1 ! = NULL )
misc_string_assign ( & his - > string [ 1 ] , string1 ) ;
2005-12-01 12:50:54 +01:00
if ( string2 ! = NULL )
misc_string_assign ( & his - > string [ 2 ] , string2 ) ;
2005-04-16 09:46:04 +02:00
g_array_sort ( user - > history , ( GCompareFunc ) user_history_compare ) ;
}
else
{
2005-12-01 12:50:54 +01:00
his - > team_name = g_strdup ( team_name ) ;
if ( string0 ! = NULL )
his - > string [ 0 ] = g_strdup ( string0 ) ;
if ( string1 ! = NULL )
his - > string [ 1 ] = g_strdup ( string1 ) ;
if ( string2 ! = NULL )
his - > string [ 2 ] = g_strdup ( string2 ) ;
2005-04-16 09:46:04 +02:00
g_array_prepend_val ( user - > history , * his ) ;
}
}
/** Write the text corresponding to the history event
into the buffer . */
void
user_history_to_string ( const UserHistory * history , gchar * buf )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " user_history_to_string \n " ) ;
# endif
2005-04-16 09:46:04 +02:00
switch ( history - > type )
{
default :
g_warning ( " user_history_to_string: unknown history type %d. \n " , history - > type ) ;
strcpy ( buf , " FIXME!!! " ) ;
case USER_HISTORY_START_GAME :
2005-06-26 13:42:01 +02:00
/* Buy a team in a league. */
2005-05-24 20:31:07 +02:00
sprintf ( buf , _ ( " You start the game with %s in the %s. " ) ,
2005-12-01 12:50:54 +01:00
history - > team_name ,
history - > string [ 0 ] ) ;
2005-04-16 09:46:04 +02:00
break ;
2005-12-01 12:50:54 +01:00
case USER_HISTORY_FIRE_FINANCE :
2005-06-26 13:42:01 +02:00
/* Team fires, team in a league. */
2005-05-24 20:31:07 +02:00
sprintf ( buf , _ ( " %s fires you because of financial mismanagement. \n You find a new job with %s in the %s. " ) ,
2005-12-01 12:50:54 +01:00
history - > team_name ,
history - > string [ 0 ] ,
history - > string [ 1 ] ) ;
2005-04-16 09:46:04 +02:00
break ;
case USER_HISTORY_FIRE_FAILURE :
2005-06-26 13:42:01 +02:00
/* Team fires, team in a league. */
2005-05-24 20:31:07 +02:00
sprintf ( buf , _ ( " %s fires you because of unsuccessfulness. \n You find a new job with %s in the %s. " ) ,
2005-12-01 12:50:54 +01:00
history - > team_name ,
history - > string [ 0 ] ,
history - > string [ 1 ] ) ;
2005-04-16 09:46:04 +02:00
break ;
case USER_HISTORY_JOB_OFFER_ACCEPTED :
2005-06-26 13:42:01 +02:00
/* Team in a league. Leave team. */
2005-05-24 20:31:07 +02:00
sprintf ( buf , _ ( " %s offer you a job in the %s. \n You accept the challenge and leave %s. " ) ,
2005-12-01 12:50:54 +01:00
history - > string [ 0 ] ,
history - > string [ 1 ] ,
history - > team_name ) ;
2005-04-16 09:46:04 +02:00
break ;
case USER_HISTORY_END_SEASON :
2005-06-26 13:42:01 +02:00
/* League name. */
2005-12-01 12:50:54 +01:00
sprintf ( buf , _ ( " You finish the season in the %s on rank %s. " ) ,
history - > string [ 0 ] ,
history - > string [ 1 ] ) ;
2005-04-16 09:46:04 +02:00
break ;
case USER_HISTORY_PROMOTED :
2005-06-26 13:42:01 +02:00
/* League name. */
2005-05-24 20:31:07 +02:00
sprintf ( buf , _ ( " You get promoted to the %s. " ) ,
2005-12-01 12:50:54 +01:00
history - > string [ 0 ] ) ;
2005-04-16 09:46:04 +02:00
break ;
case USER_HISTORY_RELEGATED :
2005-06-26 13:42:01 +02:00
/* League name. */
2005-05-24 20:31:07 +02:00
sprintf ( buf , _ ( " You get relegated to the %s. " ) ,
2005-12-01 12:50:54 +01:00
history - > string [ 0 ] ) ;
2005-04-16 09:46:04 +02:00
break ;
case USER_HISTORY_WIN_FINAL :
2005-06-26 13:42:01 +02:00
/* Cup name, team name. */
2005-05-24 20:31:07 +02:00
sprintf ( buf , _ ( " You win the %s final against %s. " ) ,
2005-12-01 12:50:54 +01:00
history - > string [ 0 ] ,
history - > string [ 1 ] ) ;
2005-04-16 09:46:04 +02:00
break ;
case USER_HISTORY_LOSE_FINAL :
2005-06-26 13:42:01 +02:00
/* Cup name, team name. */
2005-05-24 20:31:07 +02:00
sprintf ( buf , _ ( " You lose in the %s final against %s. " ) ,
2005-12-01 12:50:54 +01:00
history - > string [ 0 ] ,
history - > string [ 1 ] ) ;
2005-04-16 09:46:04 +02:00
break ;
case USER_HISTORY_REACH_CUP_ROUND :
2005-06-26 13:42:01 +02:00
/* Cup round name (e.g. Last 32), number, cup name. */
2005-12-01 12:50:54 +01:00
sprintf ( buf , _ ( " You reach the %s (round %s) of the %s. " ) ,
history - > string [ 1 ] ,
history - > string [ 2 ] ,
history - > string [ 0 ] ) ;
2005-04-16 09:46:04 +02:00
break ;
2005-04-22 17:17:39 +02:00
case USER_HISTORY_CHAMPION :
2005-06-26 13:42:01 +02:00
/* League name. */
2005-05-24 20:31:07 +02:00
sprintf ( buf , _ ( " You are champion of the %s! " ) ,
2005-12-01 12:50:54 +01:00
history - > string [ 0 ] ) ;
2005-04-22 17:17:39 +02:00
break ;
2005-04-16 09:46:04 +02:00
}
}
2005-05-30 17:54:20 +02:00
2005-12-01 12:50:54 +01:00
/** Increase the user success counter when the user
is successful in a cup . */
void
user_add_cup_success ( User * user , const Cup * cup , gint round , gint type )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " user_add_cup_success \n " ) ;
# endif
2008-11-24 12:16:36 +01:00
gboolean international , national ;
international = query_league_cup_has_property ( cup - > id , " international " ) ;
national = query_league_cup_has_property ( cup - > id , " national " ) ;
2005-12-01 12:50:54 +01:00
if ( type = = USER_HISTORY_WIN_FINAL )
{
2008-11-24 12:16:36 +01:00
if ( international )
2005-12-01 12:50:54 +01:00
user - > counters [ COUNT_USER_SUCCESS ] + =
const_int ( " int_user_success_international_winner " ) ;
2008-11-24 12:16:36 +01:00
else if ( national )
2005-12-01 12:50:54 +01:00
user - > counters [ COUNT_USER_SUCCESS ] + =
const_int ( " int_user_success_national_winner " ) ;
}
else if ( type = = USER_HISTORY_LOSE_FINAL )
{
2008-11-24 12:16:36 +01:00
if ( international )
2005-12-01 12:50:54 +01:00
user - > counters [ COUNT_USER_SUCCESS ] + =
const_int ( " int_user_success_international_final " ) ;
2008-11-24 12:16:36 +01:00
else if ( international )
2005-12-01 12:50:54 +01:00
user - > counters [ COUNT_USER_SUCCESS ] + =
const_int ( " int_user_success_national_winner " ) ;
const_int ( " int_user_success_national_final " ) ;
}
else if ( type = = USER_HISTORY_REACH_CUP_ROUND )
{
if ( round = = cup_from_clid ( cup - > id ) - > rounds - > len - 2 )
{
2008-11-24 12:16:36 +01:00
if ( international )
2005-12-01 12:50:54 +01:00
user - > counters [ COUNT_USER_SUCCESS ] + =
const_int ( " int_user_success_international_semis " ) ;
2008-11-24 12:16:36 +01:00
else if ( national )
2005-12-01 12:50:54 +01:00
user - > counters [ COUNT_USER_SUCCESS ] + =
const_int ( " int_user_success_national_semis " ) ;
}
else if ( round = = cup_from_clid ( cup - > id ) - > rounds - > len - 3 )
{
2008-11-24 12:16:36 +01:00
if ( international )
2005-12-01 12:50:54 +01:00
user - > counters [ COUNT_USER_SUCCESS ] + =
const_int ( " int_user_success_international_quarter " ) ;
2008-11-24 12:16:36 +01:00
else if ( national )
2005-12-01 12:50:54 +01:00
user - > counters [ COUNT_USER_SUCCESS ] + =
const_int ( " int_user_success_national_quarter " ) ;
}
}
}
2005-05-30 17:54:20 +02:00
/** Find out whether there's a user who
didn ' t have his turn before a new week round begins . */
gboolean
query_user_no_turn ( void )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " query_user_no_turn \n " ) ;
# endif
2005-05-30 17:54:20 +02:00
gint i ;
for ( i = 0 ; i < users - > len ; i + + )
if ( ! usr ( i ) . counters [ COUNT_USER_TOOK_TURN ] )
return TRUE ;
return FALSE ;
}
2005-06-20 14:46:57 +02:00
/** Get a random sponsor for the user. */
UserSponsor
user_get_sponsor ( const User * user )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " user_get_sponsor \n " ) ;
# endif
2005-06-20 14:46:57 +02:00
UserSponsor new ;
gint suc_factor = ( current_user . counters [ COUNT_USER_SUCCESS ] < 0 ) ? - 1 : 1 ;
gchar * names [ 18 ] =
2005-06-26 13:42:01 +02:00
/* Company name. */
2005-06-20 14:46:57 +02:00
{ _ ( " Systems " ) ,
2005-06-26 13:42:01 +02:00
/* Company name. */
2005-06-20 14:46:57 +02:00
_ ( " Communications " ) ,
2005-06-26 13:42:01 +02:00
/* Company name. */
2005-06-20 14:46:57 +02:00
_ ( " Holdings " ) ,
2005-06-26 13:42:01 +02:00
/* Company name. */
2005-06-20 14:46:57 +02:00
_ ( " Industries " ) ,
2005-06-26 13:42:01 +02:00
/* Company name. */
2005-06-20 14:46:57 +02:00
_ ( " Company " ) ,
2005-06-26 13:42:01 +02:00
/* Company name. */
2005-06-20 14:46:57 +02:00
_ ( " Telecommunications " ) ,
2005-06-26 13:42:01 +02:00
/* Company name. */
2005-06-20 14:46:57 +02:00
_ ( " Labs " ) ,
2005-06-26 13:42:01 +02:00
/* Company name. */
2005-06-20 14:46:57 +02:00
_ ( " Technologies " ) ,
2005-06-26 13:42:01 +02:00
/* Company name. */
2005-06-20 14:46:57 +02:00
_ ( " Chemicals " ) ,
2005-06-26 13:42:01 +02:00
/* Company name. */
2005-06-20 14:46:57 +02:00
_ ( " Energy " ) ,
2005-06-26 13:42:01 +02:00
/* Company name. */
2005-06-20 14:46:57 +02:00
_ ( " Bank " ) ,
2005-06-26 13:42:01 +02:00
/* Company name. */
2005-06-20 14:46:57 +02:00
_ ( " Products " ) ,
2005-06-26 13:42:01 +02:00
/* Company name. */
2005-06-20 14:46:57 +02:00
_ ( " Software " ) ,
2005-06-26 13:42:01 +02:00
/* Company name. */
2005-06-20 14:46:57 +02:00
_ ( " Scientific " ) ,
2005-06-26 13:42:01 +02:00
/* Company name. */
2005-06-20 14:46:57 +02:00
_ ( " Financial " ) ,
2005-06-26 13:42:01 +02:00
/* Company name. */
2005-06-20 14:46:57 +02:00
_ ( " Petroleum " ) ,
2005-06-26 13:42:01 +02:00
/* Company name. */
2005-06-20 14:46:57 +02:00
_ ( " Restaurants " ) ,
2005-06-26 13:42:01 +02:00
/* Company name. */
2005-06-20 14:46:57 +02:00
_ ( " Data Systems " ) } ;
gchar * short_names [ 7 ] =
2005-06-26 13:42:01 +02:00
/* Company short name, leading to things like 'Marshall Data Systems Ltd.' */
2005-06-20 14:46:57 +02:00
{ _ ( " Ltd. " ) ,
2005-06-26 13:42:01 +02:00
/* Company short name. */
2005-06-20 14:46:57 +02:00
_ ( " Assoc. " ) ,
2005-06-26 13:42:01 +02:00
/* Company short name. */
2005-06-20 14:46:57 +02:00
_ ( " Co. " ) ,
2005-06-26 13:42:01 +02:00
/* Company short name. */
2005-06-20 14:46:57 +02:00
_ ( " Ent. " ) ,
2005-06-26 13:42:01 +02:00
/* Company short name. Copy the '&'. */
2005-06-20 14:46:57 +02:00
_ ( " & Co. " ) ,
2005-06-26 13:42:01 +02:00
/* Company short name. */
2005-06-20 14:46:57 +02:00
_ ( " Corp. " ) ,
2005-06-26 13:42:01 +02:00
/* Company short name. */
2005-06-20 14:46:57 +02:00
_ ( " Group " ) } ;
gchar * name_add [ 3 ] =
2005-06-26 13:42:01 +02:00
/* Company addition, leading to 'Marshall & Sons Petroleum Co.'. Copy the '&'. */
2005-06-20 14:46:57 +02:00
{ _ ( " & Sons " ) ,
2005-06-26 13:42:01 +02:00
/* Company addition. Copy the '&'. */
2005-06-20 14:46:57 +02:00
_ ( " & Daughters " ) ,
2005-06-26 13:42:01 +02:00
/* Company addition. */
2005-06-20 14:46:57 +02:00
_ ( " Bros. " ) } ;
2005-10-09 11:35:44 +02:00
new . name = g_string_new ( name_get_random_last_name ( name_get_list_from_sid ( user - > tm - > names_file ) ) ) ;
2005-06-20 14:46:57 +02:00
if ( math_rnd ( 0 , 1 ) < 0.2 )
2005-10-09 11:35:44 +02:00
g_string_append ( new . name , name_add [ math_rndi ( 0 , 2 ) ] ) ;
2005-06-20 14:46:57 +02:00
g_string_append ( new . name , names [ math_rndi ( 0 , 17 ) ] ) ;
if ( math_rnd ( 0 , 1 ) < 0.7 )
g_string_append ( new . name , short_names [ math_rndi ( 0 , 6 ) ] ) ;
new . contract = 4 * math_rndi ( 9 , 36 ) ;
new . benefit = ( gint ) rint ( ( ( ( const_float ( " float_sponsor_wage_percentage_upper " ) -
const_float ( " float_sponsor_wage_percentage_lower " ) ) /
( 2 * ( gfloat ) const_int ( " int_user_success_offer_limit " ) ) ) *
( gfloat ) current_user . counters [ COUNT_USER_SUCCESS ]
+ ( ( const_float ( " float_sponsor_wage_percentage_upper " ) -
const_float ( " float_sponsor_wage_percentage_lower " ) ) / 2 ) ) *
2005-10-20 17:45:00 +02:00
( 1 + suc_factor * ( ( gfloat ) new . contract / 52 - 1 ) *
const_float ( " float_sponsor_contract_length_factor " ) ) *
2005-06-20 14:46:57 +02:00
( finance_wage_unit ( current_user . tm ) * current_user . tm - > players - > len ) ) ;
new . benefit = math_round_integer ( new . benefit , 2 ) ;
return new ;
}
/** Show a list of sponsors for the user to choose from. */
void
user_show_sponsors ( void )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " user_show_sponsors \n " ) ;
# endif
2005-06-20 14:46:57 +02:00
gint i , num_offers ;
GArray * sponsors = g_array_new ( FALSE , FALSE , sizeof ( UserSponsor ) ) ;
UserSponsor sponsor ;
if ( ABS ( current_user . counters [ COUNT_USER_SUCCESS ] ) > =
const_int ( " int_user_success_offer_limit " ) )
num_offers = ( current_user . counters [ COUNT_USER_SUCCESS ] < 0 ) ?
const_int ( " int_sponsor_offers_lower " ) :
const_int ( " int_sponsor_offers_upper " ) ;
else
num_offers = ( gint ) rint ( ( ( gfloat ) ( const_int ( " int_sponsor_offers_upper " ) -
const_int ( " int_sponsor_offers_lower " ) ) /
( 2 * ( gfloat ) const_int ( " int_user_success_offer_limit " ) ) ) *
( gfloat ) current_user . counters [ COUNT_USER_SUCCESS ]
+ ( ( gfloat ) ( const_int ( " int_sponsor_offers_upper " ) -
const_int ( " int_sponsor_offers_lower " ) ) / 2 ) + 1 ) ;
num_offers + = math_rndi ( - 1 , 1 ) ;
if ( num_offers < = 0 )
num_offers = 1 ;
for ( i = 0 ; i < num_offers ; i + + )
{
sponsor = user_get_sponsor ( & current_user ) ;
g_array_append_val ( sponsors , sponsor ) ;
}
window_create ( WINDOW_SPONSORS ) ;
treeview_show_sponsors ( sponsors ) ;
for ( i = 0 ; i < num_offers ; i + + )
g_string_free ( g_array_index ( sponsors , UserSponsor , i ) . name , TRUE ) ;
g_array_free ( sponsors , TRUE ) ;
}
/** The current sponsor offers to continue. */
void
user_show_sponsor_continue ( void )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " user_show_sponsor_continue \n " ) ;
# endif
2005-06-20 14:46:57 +02:00
GArray * sponsors = g_array_new ( FALSE , FALSE , sizeof ( UserSponsor ) ) ;
UserSponsor sponsor ;
2005-06-25 16:09:37 +02:00
gchar buf [ SMALL ] ;
2005-06-20 14:46:57 +02:00
sponsor = user_get_sponsor ( & current_user ) ;
g_string_printf ( sponsor . name , current_user . sponsor . name - > str ) ;
g_array_append_val ( sponsors , sponsor ) ;
window_create ( WINDOW_SPONSORS ) ;
2005-06-25 16:09:37 +02:00
sprintf ( buf , _ ( " Your current sponsor is satisfied with your results and would like to renew the contract. Currently they're paying you %d a week. " ) , current_user . sponsor . benefit ) ;
gtk_label_set_text ( GTK_LABEL ( lookup_widget ( window . sponsors , " label_sponsors " ) ) , buf ) ;
2005-06-20 14:46:57 +02:00
treeview_show_sponsors ( sponsors ) ;
g_string_free ( g_array_index ( sponsors , UserSponsor , 0 ) . name , TRUE ) ;
g_array_free ( sponsors , TRUE ) ;
stat1 = STATUS_SPONSOR_CONTINUE ;
}
2005-08-20 21:39:36 +02:00
/** Load a MM file. */
void
2005-08-21 21:58:51 +02:00
user_mm_load_file ( const gchar * filename , GArray * mmatches )
2005-08-20 21:39:36 +02:00
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " user_mm_load_file \n " ) ;
# endif
2007-03-12 21:22:58 +01:00
GString * prefix = g_string_new ( " " ) ;
gchar filename_local [ SMALL ] ,
2005-08-20 21:39:36 +02:00
matches_file [ SMALL ] ;
2005-08-21 21:58:51 +02:00
GArray * mm_array = ( mmatches = = NULL ) ?
current_user . mmatches : mmatches ;
2005-08-20 21:39:36 +02:00
strcpy ( filename_local , filename ) ;
2007-03-12 21:22:58 +01:00
g_string_append_len ( prefix , filename_local , strlen ( filename_local ) - 8 ) ;
//prefix[strlen(filename_local) - 8] = '\0';
2005-08-20 21:39:36 +02:00
2007-03-12 21:22:58 +01:00
sprintf ( matches_file , " %s___mmatches " , prefix - > str ) ;
2005-08-20 21:39:36 +02:00
file_decompress ( filename_local ) ;
2005-08-21 21:58:51 +02:00
if ( mmatches = = NULL )
free_mmatches ( & mm_array , TRUE ) ;
xml_mmatches_read ( matches_file , mm_array ) ;
2005-08-20 21:39:36 +02:00
2007-03-12 21:22:58 +01:00
g_string_append ( prefix , " ___* " ) ;
2005-08-20 21:39:36 +02:00
file_remove_files ( prefix ) ;
2005-08-21 21:58:51 +02:00
if ( mmatches = = NULL )
2005-10-09 11:35:44 +02:00
misc_string_assign ( & current_user . mmatches_file , filename_local ) ;
2007-03-12 21:22:58 +01:00
g_string_free ( prefix , TRUE ) ;
2005-08-20 21:39:36 +02:00
}
/** Add the last match to the MM file.
@ param load_file Whether to load the matches from the file first .
@ param save_file Whether to save the MM array
to file afterwards . */
void
user_mm_add_last_match ( gboolean load_file , gboolean save_file )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " user_mm_add_last_match \n " ) ;
# endif
2005-08-20 21:39:36 +02:00
MemMatch new ;
const Fixture * fix = current_user . live_game . fix ;
gchar buf [ SMALL ] ;
if ( fix - > clid < ID_CUP_START )
new . competition_name = g_string_new ( league_cup_get_name_string ( fix - > clid ) ) ;
else
{
cup_round_name ( fix , buf ) ;
new . competition_name = g_string_new ( " " ) ;
g_string_printf ( new . competition_name , " %s %s " ,
league_cup_get_name_string ( fix - > clid ) , buf ) ;
}
2005-10-09 11:35:44 +02:00
new . country_name = g_strdup ( country . name ) ;
2005-08-20 21:39:36 +02:00
new . neutral = ! ( fix - > home_advantage ) ;
new . user_team = ( fix - > team_ids [ 0 ] ! = current_user . team_id ) ;
new . lg = current_user . live_game ;
/** This will tell the free function NOT
to free the user live game . */
current_user . live_game . started_game = - 1 ;
2005-08-21 21:58:51 +02:00
current_user . live_game . units =
g_array_new ( FALSE , FALSE , sizeof ( LiveGameUnit ) ) ;
2005-08-20 21:39:36 +02:00
if ( load_file )
2005-10-09 11:35:44 +02:00
user_mm_load_file ( current_user . mmatches_file , NULL ) ;
2005-08-20 21:39:36 +02:00
g_array_append_val ( current_user . mmatches , new ) ;
2005-08-28 10:56:36 +02:00
game_gui_print_message ( _ ( " Memorable match added. " ) ) ;
2005-08-20 21:39:36 +02:00
if ( save_file )
2005-10-09 11:35:44 +02:00
user_mm_save_file ( current_user . mmatches_file ,
2005-08-21 21:58:51 +02:00
current_user . mmatches ) ;
2005-08-20 21:39:36 +02:00
}
/** Save an MM file. */
void
2005-08-21 21:58:51 +02:00
user_mm_save_file ( const gchar * filename , const GArray * mmatches )
2005-08-20 21:39:36 +02:00
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " user_mm_save_file \n " ) ;
# endif
2005-08-20 21:39:36 +02:00
gchar prefix [ SMALL ] ;
2005-08-21 21:58:51 +02:00
strncpy ( prefix , filename , strlen ( filename ) - 8 ) ;
prefix [ strlen ( filename ) - 8 ] = ' \0 ' ;
2005-08-20 21:39:36 +02:00
strcat ( prefix , " ___ " ) ;
2005-08-21 21:58:51 +02:00
xml_mmatches_write ( prefix , mmatches ) ;
2005-08-20 21:39:36 +02:00
2005-08-21 21:58:51 +02:00
file_compress_files ( filename , prefix ) ;
2005-08-20 21:39:36 +02:00
}
/** Set an appropriate filename for the memorable
matches file . */
void
2005-08-21 21:58:51 +02:00
user_mm_set_filename ( const gchar * filename , gchar * dest )
2005-08-20 21:39:36 +02:00
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " user_mm_set_filename \n " ) ;
# endif
2005-08-20 21:39:36 +02:00
gchar buf [ SMALL ] ;
if ( g_str_has_suffix ( filename , " .bmm.zip " ) )
{
2005-08-21 21:58:51 +02:00
if ( dest = = NULL )
2005-10-09 11:35:44 +02:00
misc_string_assign ( & current_user . mmatches_file , filename ) ;
2005-08-21 21:58:51 +02:00
else
strcpy ( dest , filename ) ;
2005-08-20 21:39:36 +02:00
return ;
}
strcpy ( buf , filename ) ;
while ( g_str_has_suffix ( buf , " .bmm " ) | |
g_str_has_suffix ( buf , " .zip " ) )
buf [ strlen ( buf ) - 4 ] = ' \0 ' ;
2005-08-21 21:58:51 +02:00
if ( dest = = NULL )
2005-10-09 11:35:44 +02:00
{
strcat ( buf , " .bmm.zip " ) ;
misc_string_assign ( & current_user . mmatches_file , buf ) ;
}
2005-08-21 21:58:51 +02:00
else
sprintf ( dest , " %s.bmm.zip " , buf ) ;
}
/** Import an MM file into the user's MMs. */
void
user_mm_import_file ( const gchar * filename )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " user_mm_import_file \n " ) ;
# endif
2005-08-21 21:58:51 +02:00
GArray * mm_array = g_array_new ( FALSE , FALSE , sizeof ( MemMatch ) ) ;
gint i ;
user_mm_load_file ( filename , mm_array ) ;
for ( i = 0 ; i < mm_array - > len ; i + + )
g_array_append_val ( current_user . mmatches , g_array_index ( mm_array , MemMatch , i ) ) ;
g_array_free ( mm_array , TRUE ) ;
}
/** Export a match to a file. */
void
user_mm_export_file ( const gchar * filename )
{
2008-11-25 14:50:07 +01:00
# ifdef DEBUG
printf ( " user_mm_export_file \n " ) ;
# endif
2005-08-21 21:58:51 +02:00
gchar buf [ SMALL ] ;
GArray * mmatches = g_array_new ( FALSE , FALSE , sizeof ( MemMatch ) ) ;
user_mm_set_filename ( filename , buf ) ;
g_array_append_val ( mmatches , g_array_index ( current_user . mmatches , MemMatch , stat4 ) ) ;
user_mm_save_file ( buf , mmatches ) ;
g_array_free ( mmatches , TRUE ) ;
2005-08-20 21:39:36 +02:00
}