bygfoot/src/lg_commentary.c

690 lines
23 KiB
C
Raw Normal View History

2005-06-26 13:42:01 +02:00
#include <locale.h>
2005-07-01 22:51:46 +02:00
#include "cup.h"
2005-06-26 13:42:01 +02:00
#include "file.h"
2005-07-01 22:51:46 +02:00
#include "fixture.h"
2005-10-09 11:35:44 +02:00
#include "free.h"
2005-07-01 22:51:46 +02:00
#include "league.h"
2005-05-18 18:02:14 +02:00
#include "lg_commentary.h"
#include "live_game.h"
2005-06-17 14:57:05 +02:00
#include "main.h"
2005-07-01 22:51:46 +02:00
#include "maths.h"
2005-05-18 18:02:14 +02:00
#include "misc.h"
#include "option.h"
#include "player.h"
2005-07-01 22:51:46 +02:00
#include "team.h"
2005-05-18 18:02:14 +02:00
#include "variables.h"
2005-06-26 13:42:01 +02:00
#include "xml_lg_commentary.h"
2005-05-18 18:02:14 +02:00
2005-07-01 22:51:46 +02:00
/** The replacement strings for the live game commentary tokens. */
gchar **token_rep;
2005-07-05 20:37:26 +02:00
/** Whether there was a commentary repetition rejection. */
gboolean repetition;
2005-07-01 22:51:46 +02:00
2005-05-18 18:02:14 +02:00
/** Generate commentary for the live game event in the unit.
@param unit The unit we comment.
@param fix The fixture being calculated. */
void
2005-07-01 22:51:46 +02:00
lg_commentary_generate(const LiveGame *live_game, LiveGameUnit *unit)
2005-05-18 18:02:14 +02:00
{
gchar buf[SMALL];
2005-07-01 22:51:46 +02:00
GArray **commentaries = NULL;
2005-05-18 18:02:14 +02:00
gint i;
2005-07-01 22:51:46 +02:00
lg_commentary_set_basic_tokens(unit, live_game->fix);
lg_commentary_set_team_tokens(unit, live_game->fix);
lg_commentary_set_player_tokens(unit, live_game->fix);
lg_commentary_set_stats_tokens(&live_game->stats);
2005-06-28 04:08:56 +02:00
2005-06-26 13:42:01 +02:00
if(unit->event.type == LIVE_GAME_EVENT_STYLE_CHANGE_ALL_OUT_DEFEND ||
unit->event.type == LIVE_GAME_EVENT_STYLE_CHANGE_DEFEND ||
unit->event.type == LIVE_GAME_EVENT_STYLE_CHANGE_BALANCED ||
unit->event.type == LIVE_GAME_EVENT_STYLE_CHANGE_ATTACK ||
unit->event.type == LIVE_GAME_EVENT_STYLE_CHANGE_ALL_OUT_ATTACK)
commentaries = &lg_commentary[LIVE_GAME_EVENT_STYLE_CHANGE_ALL_OUT_DEFEND];
else if(unit->event.type == LIVE_GAME_EVENT_BOOST_CHANGE_ANTI ||
unit->event.type == LIVE_GAME_EVENT_BOOST_CHANGE_OFF ||
unit->event.type == LIVE_GAME_EVENT_BOOST_CHANGE_ON)
commentaries = &lg_commentary[LIVE_GAME_EVENT_BOOST_CHANGE_ANTI];
else
commentaries = &lg_commentary[unit->event.type];
2005-05-18 18:02:14 +02:00
2005-07-01 22:51:46 +02:00
gint order[(*commentaries)->len];
lg_commentary_get_order(*commentaries, order);
2005-05-18 18:02:14 +02:00
2005-07-05 20:37:26 +02:00
repetition = FALSE;
2005-06-26 13:42:01 +02:00
for(i=0;i<(*commentaries)->len;i++)
2005-07-01 22:51:46 +02:00
if(lg_commentary_check_commentary(&g_array_index(*commentaries, LGCommentary, order[i]), buf))
2005-05-18 18:02:14 +02:00
break;
2005-07-05 20:37:26 +02:00
if(i == (*commentaries)->len)
{
repetition = TRUE;
for(i=0;i<(*commentaries)->len;i++)
if(lg_commentary_check_commentary(&g_array_index(*commentaries, LGCommentary, order[i]), buf))
break;
2005-05-18 18:02:14 +02:00
}
2005-06-26 13:42:01 +02:00
if(i == (*commentaries)->len)
2005-05-18 18:02:14 +02:00
{
g_warning("lg_commentary_generate: didn't find fitting commentary for unit type %d \n",
unit->event.type);
strcpy(buf, "FIXME!");
}
2005-10-09 11:35:44 +02:00
unit->event.commentary = g_strdup(buf);
2005-07-05 20:37:26 +02:00
unit->event.commentary_id = (i == (*commentaries)->len) ?
-1 : g_array_index(*commentaries, LGCommentary, order[i]).id;
2005-05-18 18:02:14 +02:00
2005-06-28 04:08:56 +02:00
for(i=0;i<lg_tokens.list->len;i++)
2005-07-01 22:51:46 +02:00
if(i != option_int("string_lg_commentary_token_team_home", &lg_tokens) &&
i != option_int("string_lg_commentary_token_team_away", &lg_tokens) &&
i != option_int("string_lg_commentary_token_attendance", &lg_tokens) &&
i != option_int("string_lg_commentary_token_cup_round_name", &lg_tokens) &&
i != option_int("string_lg_commentary_token_league_cup_name", &lg_tokens) &&
i != option_int("string_lg_commentary_token_yellow_limit", &lg_tokens) &&
i != option_int("string_lg_commentary_token_team_layer0", &lg_tokens) &&
i != option_int("string_lg_commentary_token_team_layer1", &lg_tokens))
2005-10-09 11:35:44 +02:00
free_gchar_ptr(token_rep[i]);
2005-07-01 22:51:46 +02:00
}
2005-06-28 04:08:56 +02:00
2005-07-01 22:51:46 +02:00
/** Check whether the commentary conditions are fulfilled and whether
we can replace all tokens in the commentary. */
gboolean
lg_commentary_check_commentary(const LGCommentary *commentary, gchar *dest)
{
2005-07-08 11:26:00 +02:00
gchar buf[SMALL];
2005-10-09 11:35:44 +02:00
if(strlen(commentary->text) == 0 ||
2005-07-08 11:26:00 +02:00
(commentary->condition != NULL &&
2005-10-09 11:35:44 +02:00
!lg_commentary_parse_condition(commentary->condition)) ||
2005-07-05 20:37:26 +02:00
(repetition == FALSE && query_lg_commentary_is_repetition(commentary->id)))
2005-07-01 22:51:46 +02:00
return FALSE;
2005-10-09 11:35:44 +02:00
strcpy(dest, commentary->text);
2005-07-01 22:51:46 +02:00
2005-07-08 11:26:00 +02:00
do
{
strcpy(buf, dest);
if(!lg_commentary_replace_tokens(dest))
return FALSE;
lg_commentary_replace_expressions(dest);
}
while(strcmp(buf, dest) != 0);
return TRUE;
2005-07-01 22:51:46 +02:00
}
2005-07-05 20:37:26 +02:00
/** Check whether a commentary with given id has been used in the last
few commentaries. Return TRUE if so and FALSE otherwise. */
gboolean
query_lg_commentary_is_repetition(gint id)
{
const GArray *units = ((LiveGame*)statp)->units;
gint units_min = units->len - const_int("int_lg_commentary_check_backwards") - 1;
gint i, min = MAX(units_min, 0);
for(i=units->len - 2; i>= min; i--)
if(g_array_index(units, LiveGameUnit, i).event.commentary_id == id)
return TRUE;
return FALSE;
}
/** Choose one of strings separated with '|' */
void lg_commentary_choose_random(gchar* s)
{
const gchar STR_SEP = '|';
gint i = 0;
gint count = 1;
const gchar* start;
for (i = 0; s[i]; i++)
count += (s[i] == STR_SEP);
if (count == 1)
return;
count = math_rndi(0, count-1) + 1;
start = s;
for (i = 0; s[i]; i++)
if (s[i] == STR_SEP)
{
count--;
if (count == 1)
start = s + i + 1;
else if (!count)
{
s[i] = '\0';
break;
}
}
2005-08-17 22:39:07 +02:00
strcpy(s, start);
}
2005-07-01 22:51:46 +02:00
/** Replace simple arithmetic expressions like "1 + 2"
and comparisons like "3 < 4" with the appropriate result. */
void
lg_commentary_replace_expressions(gchar *commentary_text)
{
gint i, j, last_idx = 0;
gint value = -1;
gchar buf[SMALL], buf2[SMALL];
2005-09-05 21:12:42 +02:00
if(debug > 100)
printf("lg_commentary_replace_expressions: #%s#\n",
commentary_text);
2005-08-28 10:56:36 +02:00
if(!g_strrstr(commentary_text, "["))
return;
2005-07-01 22:51:46 +02:00
strcpy(buf, commentary_text);
strcpy(commentary_text, "");
2005-09-05 21:12:42 +02:00
for(i=strlen(buf) - 1; i>=0; i--)
2005-07-01 22:51:46 +02:00
if(buf[i] == '[')
{
2005-09-05 21:12:42 +02:00
strncpy(buf2, buf, i);
buf2[i] = '\0';
2005-07-01 22:51:46 +02:00
strcat(commentary_text, buf2);
for(j=i + 1;j<strlen(buf);j++)
{
if(buf[j] == ']')
{
strncpy(buf2, buf + i + 1, j - i - 1);
buf2[j - i - 1] = '\0';
2005-08-22 21:38:54 +02:00
if (g_strrstr(buf2, "|"))
lg_commentary_choose_random(buf2);
else
{
2005-08-22 21:38:54 +02:00
if(g_strrstr(buf2, "<") ||
g_strrstr(buf2, ">") ||
g_strrstr(buf2, "=") ||
g_strrstr(buf2, " G ") ||
2005-08-28 10:56:36 +02:00
g_strrstr(buf2, " L ") ||
g_strrstr(buf2, " GE ") ||
g_strrstr(buf2, " LE "))
misc_parse(buf2, &value);
else
misc_parse_expression(buf2, &value);
sprintf(buf2, "%d", value);
}
2005-07-01 22:51:46 +02:00
strcat(commentary_text, buf2);
value = -1;
last_idx = j + 1;
2005-09-05 21:12:42 +02:00
2005-07-01 22:51:46 +02:00
break;
}
}
2005-09-05 21:12:42 +02:00
break;
2005-07-01 22:51:46 +02:00
}
if(last_idx < strlen(buf))
{
strncpy(buf2, buf + last_idx, strlen(buf) - last_idx);
buf2[strlen(buf) - last_idx] = '\0';
strcat(commentary_text, buf2);
}
2005-05-18 18:02:14 +02:00
}
/** Try to replace all special tokens in the string and write the result to dest.
2005-07-01 22:51:46 +02:00
@param commentary The commentary with tokens.
2005-05-18 18:02:14 +02:00
@param dest The destination string.
2005-07-01 22:51:46 +02:00
@return TRUE if we could replace all tokens and the commentary condition
was fulfilled, FALSE otherwise. */
2005-05-18 18:02:14 +02:00
gboolean
2005-07-01 22:51:46 +02:00
lg_commentary_replace_tokens(gchar *commentary_text)
2005-05-18 18:02:14 +02:00
{
gint i;
2005-07-01 22:51:46 +02:00
for(i=0;i<lg_tokens.list->len;i++)
{
2005-08-22 21:38:54 +02:00
if(g_strrstr(commentary_text,
2005-10-09 11:35:44 +02:00
g_array_index(lg_tokens.list, Option, i).string_value))
2005-07-01 22:51:46 +02:00
{
if(token_rep[i] == NULL)
return FALSE;
else
misc_string_replace_token(commentary_text,
2005-10-09 11:35:44 +02:00
g_array_index(lg_tokens.list, Option, i).string_value,
2005-07-01 22:51:46 +02:00
token_rep[i]);
}
}
return TRUE;
}
/** Find out whether the conditions of the commentary are fulfilled. */
gboolean
lg_commentary_parse_condition(const gchar *condition)
{
gboolean return_value = FALSE;
2005-07-08 11:26:00 +02:00
gchar buf[SMALL], buf2[SMALL];
2005-06-28 04:08:56 +02:00
2005-07-01 22:51:46 +02:00
strcpy(buf, condition);
2005-05-18 18:02:14 +02:00
2005-07-08 11:26:00 +02:00
do
{
strcpy(buf2, buf);
if(!lg_commentary_replace_tokens(buf))
return FALSE;
2005-07-02 13:06:43 +02:00
2005-07-08 11:26:00 +02:00
lg_commentary_replace_expressions(buf);
}
while(strcmp(buf2, buf) != 0);
2005-05-18 18:02:14 +02:00
2005-07-01 22:51:46 +02:00
misc_parse(buf, &return_value);
return return_value;
}
/** Write a random order of indices into the integer array
(only depending on the priority values of the commentaries).
I don't like this implementation of ordering the commentaries
according to their priority :-P can't think of a better one, though. */
void
lg_commentary_get_order(const GArray *commentaries, gint *order)
{
gint i, j, order_idx = 0;
gint priority_sum = 0, bounds[commentaries->len + 1];
bounds[0] = 0;
for(i=0;i<commentaries->len;i++)
{
priority_sum += g_array_index(commentaries, LGCommentary, i).priority;
bounds[i + 1] = priority_sum;
order[i] = -1;
}
gint permutation[priority_sum];
math_generate_permutation(permutation, 1, priority_sum);
2005-06-28 04:08:56 +02:00
2005-07-01 22:51:46 +02:00
for(i=0;i<priority_sum;i++)
2005-06-28 04:08:56 +02:00
{
2005-07-01 22:51:46 +02:00
if(order_idx == commentaries->len)
break;
for(j=0;j<commentaries->len;j++)
if(bounds[j] < permutation[i] && permutation[i] <= bounds[j + 1])
{
2005-10-13 10:52:33 +02:00
if(!query_integer_is_in_array(j, order, commentaries->len))
2005-07-01 22:51:46 +02:00
{
order[order_idx] = j;
order_idx++;
}
2005-06-28 04:08:56 +02:00
2005-07-01 22:51:46 +02:00
break;
}
2005-06-28 04:08:56 +02:00
}
2005-07-01 22:51:46 +02:00
}
2005-06-28 04:08:56 +02:00
2005-07-01 22:51:46 +02:00
/** Fill the stats tokens. */
void
lg_commentary_set_stats_tokens(const LiveGameStats *stats)
{
token_rep[option_int("string_lg_commentary_token_stat_shots0", &lg_tokens)] =
misc_int_to_char(stats->values[0][LIVE_GAME_STAT_VALUE_SHOTS]);
token_rep[option_int("string_lg_commentary_token_stat_shot_per0", &lg_tokens)] =
misc_int_to_char(stats->values[0][LIVE_GAME_STAT_VALUE_SHOT_PERCENTAGE]);
token_rep[option_int("string_lg_commentary_token_stat_poss0", &lg_tokens)] =
2005-07-05 20:37:26 +02:00
misc_int_to_char((gint)rint(100 * ((gfloat)stats->values[0][LIVE_GAME_STAT_VALUE_POSSESSION] /
((gfloat)stats->values[0][LIVE_GAME_STAT_VALUE_POSSESSION] +
(gfloat)stats->values[1][LIVE_GAME_STAT_VALUE_POSSESSION]))));
2005-07-01 22:51:46 +02:00
token_rep[option_int("string_lg_commentary_token_stat_pen0", &lg_tokens)] =
misc_int_to_char(stats->values[0][LIVE_GAME_STAT_VALUE_PENALTIES]);
token_rep[option_int("string_lg_commentary_token_stat_fouls0", &lg_tokens)] =
misc_int_to_char(stats->values[0][LIVE_GAME_STAT_VALUE_FOULS]);
token_rep[option_int("string_lg_commentary_token_stat_yellows0", &lg_tokens)] =
misc_int_to_char(stats->values[0][LIVE_GAME_STAT_VALUE_CARDS]);
token_rep[option_int("string_lg_commentary_token_stat_reds0", &lg_tokens)] =
misc_int_to_char(stats->values[0][LIVE_GAME_STAT_VALUE_REDS]);
token_rep[option_int("string_lg_commentary_token_stat_injs0", &lg_tokens)] =
misc_int_to_char(stats->values[0][LIVE_GAME_STAT_VALUE_INJURIES]);
2005-06-28 04:08:56 +02:00
2005-07-01 22:51:46 +02:00
token_rep[option_int("string_lg_commentary_token_stat_shots1", &lg_tokens)] =
misc_int_to_char(stats->values[1][LIVE_GAME_STAT_VALUE_SHOTS]);
token_rep[option_int("string_lg_commentary_token_stat_shot_per1", &lg_tokens)] =
misc_int_to_char(stats->values[1][LIVE_GAME_STAT_VALUE_SHOT_PERCENTAGE]);
token_rep[option_int("string_lg_commentary_token_stat_poss1", &lg_tokens)] =
2005-07-05 20:37:26 +02:00
misc_int_to_char((gint)rint(100 * ((gfloat)stats->values[1][LIVE_GAME_STAT_VALUE_POSSESSION] /
((gfloat)stats->values[0][LIVE_GAME_STAT_VALUE_POSSESSION] +
(gfloat)stats->values[1][LIVE_GAME_STAT_VALUE_POSSESSION]))));
2005-07-01 22:51:46 +02:00
token_rep[option_int("string_lg_commentary_token_stat_pen1", &lg_tokens)] =
misc_int_to_char(stats->values[1][LIVE_GAME_STAT_VALUE_PENALTIES]);
token_rep[option_int("string_lg_commentary_token_stat_fouls1", &lg_tokens)] =
misc_int_to_char(stats->values[1][LIVE_GAME_STAT_VALUE_FOULS]);
token_rep[option_int("string_lg_commentary_token_stat_yellows1", &lg_tokens)] =
misc_int_to_char(stats->values[1][LIVE_GAME_STAT_VALUE_CARDS]);
token_rep[option_int("string_lg_commentary_token_stat_reds1", &lg_tokens)] =
misc_int_to_char(stats->values[1][LIVE_GAME_STAT_VALUE_REDS]);
token_rep[option_int("string_lg_commentary_token_stat_injs1", &lg_tokens)] =
misc_int_to_char(stats->values[1][LIVE_GAME_STAT_VALUE_INJURIES]);
2005-05-18 18:02:14 +02:00
}
2005-07-01 22:51:46 +02:00
/** Fill the tokens that contain general information. */
2005-05-18 18:02:14 +02:00
void
2005-07-01 22:51:46 +02:00
lg_commentary_set_basic_tokens(const LiveGameUnit *unit, const Fixture *fix)
2005-05-18 18:02:14 +02:00
{
gchar buf[SMALL];
2005-07-01 22:51:46 +02:00
gint tmp_int = 1, current_min = live_game_unit_get_minute(unit);
gint avskill0 = (gint)rint(team_get_average_skill(fix->teams[0], TRUE)),
avskill1 = (gint)rint(team_get_average_skill(fix->teams[1], TRUE));
sprintf(buf, "%d : %d", unit->result[0], unit->result[1]);
token_rep[option_int("string_lg_commentary_token_result", &lg_tokens)] =
g_strdup(buf);
2005-05-18 18:02:14 +02:00
2005-07-01 22:51:46 +02:00
token_rep[option_int("string_lg_commentary_token_time", &lg_tokens)] =
misc_int_to_char(unit->time);
token_rep[option_int("string_lg_commentary_token_minute", &lg_tokens)] =
misc_int_to_char(live_game_unit_get_minute(unit));
if(unit->time == LIVE_GAME_UNIT_TIME_EXTRA_TIME)
tmp_int = 120 - current_min;
else if(unit->time == LIVE_GAME_UNIT_TIME_SECOND_HALF)
tmp_int = 90 - current_min;
else if(unit->time == LIVE_GAME_UNIT_TIME_FIRST_HALF)
tmp_int = 45 - current_min;
if(unit->time != LIVE_GAME_UNIT_TIME_PENALTIES && tmp_int > 0)
token_rep[option_int("string_lg_commentary_token_minute_remaining", &lg_tokens)] =
misc_int_to_char(tmp_int);
if(query_fixture_is_draw(fix))
tmp_int = 120 - current_min;
else
tmp_int = 90 - current_min;
if(tmp_int > 0)
token_rep[option_int("string_lg_commentary_token_minute_total", &lg_tokens)] =
misc_int_to_char(tmp_int);
token_rep[option_int("string_lg_commentary_token_goals0", &lg_tokens)] =
misc_int_to_char(unit->result[0]);
token_rep[option_int("string_lg_commentary_token_goals1", &lg_tokens)] =
misc_int_to_char(unit->result[1]);
token_rep[option_int("string_lg_commentary_token_goal_diff", &lg_tokens)] =
misc_int_to_char(ABS(unit->result[0] - unit->result[1]));
token_rep[option_int("string_lg_commentary_token_extra", &lg_tokens)] =
lg_commentary_get_extra_data(unit, fix);
token_rep[option_int("string_lg_commentary_token_possession", &lg_tokens)] =
misc_int_to_char(unit->possession);
token_rep[option_int("string_lg_commentary_token_no_possession", &lg_tokens)] =
misc_int_to_char(!unit->possession);
token_rep[option_int("string_lg_commentary_token_team_avskill0", &lg_tokens)] =
misc_int_to_char(avskill0);
token_rep[option_int("string_lg_commentary_token_team_avskill1", &lg_tokens)] =
misc_int_to_char(avskill1);
token_rep[option_int("string_lg_commentary_token_team_avskilldiff", &lg_tokens)] =
misc_int_to_char(ABS(avskill0 - avskill1));
}
/** Set the tokens containing team data. */
void
lg_commentary_set_team_tokens(const LiveGameUnit *unit, const Fixture *fix)
{
if(unit->result[0] != unit->result[1])
2005-06-28 04:08:56 +02:00
{
2005-07-01 22:51:46 +02:00
token_rep[option_int("string_lg_commentary_token_team_losing", &lg_tokens)] =
2005-10-09 11:35:44 +02:00
g_strdup(fix->teams[(unit->result[0] > unit->result[1])]->name);
2005-07-01 22:51:46 +02:00
token_rep[option_int("string_lg_commentary_token_team_winning", &lg_tokens)] =
2005-10-09 11:35:44 +02:00
g_strdup(fix->teams[(unit->result[0] < unit->result[1])]->name);
2005-07-03 12:56:37 +02:00
token_rep[option_int("string_lg_commentary_token_team_losingn", &lg_tokens)] =
misc_int_to_char((unit->result[0] > unit->result[1]));
token_rep[option_int("string_lg_commentary_token_team_winningn", &lg_tokens)] =
misc_int_to_char((unit->result[0] < unit->result[1]));
2005-06-28 04:08:56 +02:00
}
2005-07-01 22:51:46 +02:00
if(unit->event.team != -1)
token_rep[option_int("string_lg_commentary_token_team", &lg_tokens)] =
2005-07-03 12:56:37 +02:00
misc_int_to_char(unit->event.team);
2005-05-18 18:02:14 +02:00
}
2005-07-01 22:51:46 +02:00
/** Set the player tokens. */
void
lg_commentary_set_player_tokens(const LiveGameUnit *unit, const Fixture *fix)
2005-05-18 18:02:14 +02:00
{
2005-07-01 22:51:46 +02:00
Player *pl1 = NULL,
*pl2 = NULL;
if(unit->event.type == LIVE_GAME_EVENT_GENERAL ||
unit->event.type == LIVE_GAME_EVENT_SCORING_CHANCE ||
unit->event.type == LIVE_GAME_EVENT_HEADER ||
unit->event.type == LIVE_GAME_EVENT_PENALTY ||
unit->event.type == LIVE_GAME_EVENT_FREE_KICK)
2005-05-18 18:02:14 +02:00
{
2005-07-01 22:51:46 +02:00
pl1 = player_of_id_team(fix->teams[unit->possession],
unit->event.player);
if(unit->event.player2 != -1)
pl2 = player_of_id_team(fix->teams[unit->possession],
unit->event.player2);
2005-05-18 18:02:14 +02:00
}
2005-07-01 22:51:46 +02:00
else if(unit->event.type == LIVE_GAME_EVENT_SEND_OFF ||
unit->event.type == LIVE_GAME_EVENT_INJURY ||
unit->event.type == LIVE_GAME_EVENT_TEMP_INJURY ||
unit->event.type == LIVE_GAME_EVENT_SUBSTITUTION)
{
pl1 = player_of_id_team(fix->teams[unit->event.team],
unit->event.player);
if(unit->event.player2 != -1)
pl2 = player_of_id_team(fix->teams[unit->event.team],
unit->event.player2);
2005-05-18 18:02:14 +02:00
}
2005-07-01 22:51:46 +02:00
else if(unit->event.type == LIVE_GAME_EVENT_LOST_POSSESSION ||
unit->event.type == LIVE_GAME_EVENT_GOAL ||
2005-06-26 13:42:01 +02:00
unit->event.type == LIVE_GAME_EVENT_MISS ||
2005-05-18 18:02:14 +02:00
unit->event.type == LIVE_GAME_EVENT_SAVE ||
unit->event.type == LIVE_GAME_EVENT_POST ||
unit->event.type == LIVE_GAME_EVENT_CROSS_BAR)
{
2005-07-01 22:51:46 +02:00
pl1 = player_of_id_team(fix->teams[unit->possession],
unit->event.player);
pl2 = player_of_id_team(fix->teams[!unit->possession],
unit->event.player2);
2005-05-18 18:02:14 +02:00
}
else if(unit->event.type == LIVE_GAME_EVENT_OWN_GOAL)
2005-07-01 22:51:46 +02:00
pl1 = player_of_id_team(fix->teams[!unit->possession],
unit->event.player);
2005-05-18 18:02:14 +02:00
else if(unit->event.type == LIVE_GAME_EVENT_FOUL ||
unit->event.type == LIVE_GAME_EVENT_FOUL_RED ||
unit->event.type == LIVE_GAME_EVENT_FOUL_RED_INJURY ||
unit->event.type == LIVE_GAME_EVENT_FOUL_YELLOW)
{
2005-07-01 22:51:46 +02:00
pl1 = player_of_id_team(fix->teams[!unit->event.team],
unit->event.player);
pl2 = player_of_id_team(fix->teams[unit->event.team],
unit->event.player2);
2005-05-18 18:02:14 +02:00
}
2005-07-01 22:51:46 +02:00
if(pl1 != NULL)
2005-06-17 14:57:05 +02:00
{
2005-07-01 22:51:46 +02:00
token_rep[option_int("string_lg_commentary_token_player0", &lg_tokens)] =
2005-10-09 11:35:44 +02:00
player_get_last_name(pl1->name);
2005-07-01 22:51:46 +02:00
token_rep[option_int("string_lg_commentary_token_player_goals0", &lg_tokens)] =
misc_int_to_char(player_games_goals_get(pl1, fix->clid, PLAYER_VALUE_GOALS));
token_rep[option_int("string_lg_commentary_token_player_goals_all0", &lg_tokens)] =
misc_int_to_char(player_all_games_goals(pl1, PLAYER_VALUE_GOALS));
2005-06-17 14:57:05 +02:00
}
2005-05-18 18:02:14 +02:00
2005-07-01 22:51:46 +02:00
if(pl2 != NULL)
{
token_rep[option_int("string_lg_commentary_token_player1", &lg_tokens)] =
2005-10-09 11:35:44 +02:00
player_get_last_name(pl2->name);
2005-07-01 22:51:46 +02:00
token_rep[option_int("string_lg_commentary_token_player_goals1", &lg_tokens)] =
misc_int_to_char(player_games_goals_get(pl2, fix->clid, PLAYER_VALUE_GOALS));
token_rep[option_int("string_lg_commentary_token_player_goals_all1", &lg_tokens)] =
misc_int_to_char(player_all_games_goals(pl2, PLAYER_VALUE_GOALS));
2005-09-09 21:09:33 +02:00
token_rep[option_int("string_lg_commentary_token_player_yellows", &lg_tokens)] =
misc_int_to_char(player_card_get(pl2, fix->clid, PLAYER_VALUE_CARD_YELLOW));
2005-07-01 22:51:46 +02:00
}
2005-05-18 18:02:14 +02:00
}
/** Return some extra data depending on the unit type. */
gchar*
lg_commentary_get_extra_data(const LiveGameUnit *unit, const Fixture *fix)
{
gchar buf[SMALL];
switch(unit->event.type)
{
default:
return NULL;
break;
case LIVE_GAME_EVENT_STRUCTURE_CHANGE:
sprintf(buf, "%d",
2005-05-19 20:13:14 +02:00
fix->teams[unit->event.team]->structure);
2005-05-18 18:02:14 +02:00
break;
case LIVE_GAME_EVENT_STYLE_CHANGE_ALL_OUT_DEFEND:
2005-06-18 16:45:29 +02:00
strcpy(buf, _("ALL OUT DEFEND"));
2005-05-18 18:02:14 +02:00
break;
case LIVE_GAME_EVENT_STYLE_CHANGE_DEFEND:
2005-06-18 16:45:29 +02:00
strcpy(buf, _("DEFEND"));
2005-05-18 18:02:14 +02:00
break;
case LIVE_GAME_EVENT_STYLE_CHANGE_BALANCED:
2005-06-18 16:45:29 +02:00
strcpy(buf, _("BALANCED"));
2005-05-18 18:02:14 +02:00
break;
case LIVE_GAME_EVENT_STYLE_CHANGE_ATTACK:
2005-06-18 16:45:29 +02:00
strcpy(buf, _("ATTACK"));
2005-05-18 18:02:14 +02:00
break;
case LIVE_GAME_EVENT_STYLE_CHANGE_ALL_OUT_ATTACK:
2005-06-18 16:45:29 +02:00
strcpy(buf, _("ALL OUT ATTACK"));
2005-05-18 18:02:14 +02:00
break;
case LIVE_GAME_EVENT_BOOST_CHANGE_ANTI:
2005-06-18 16:45:29 +02:00
strcpy(buf, _("ANTI"));
2005-05-18 18:02:14 +02:00
break;
case LIVE_GAME_EVENT_BOOST_CHANGE_OFF:
2005-06-18 16:45:29 +02:00
strcpy(buf, _("OFF"));
2005-05-18 18:02:14 +02:00
break;
case LIVE_GAME_EVENT_BOOST_CHANGE_ON:
2005-06-18 16:45:29 +02:00
strcpy(buf, _("ON"));
2005-05-18 18:02:14 +02:00
break;
}
return g_strdup(buf);
}
2005-06-26 13:42:01 +02:00
2005-07-05 20:37:26 +02:00
/** Allocate memory for the token array and fill some tokens
at the beginning of the live game that don't change later. */
void
lg_commentary_initialize(const Fixture *fix)
{
gint i;
gchar buf[SMALL];
token_rep = (gchar**)g_malloc(sizeof(gchar*) * lg_tokens.list->len);
for(i=0;i<lg_tokens.list->len;i++)
token_rep[i] = NULL;
token_rep[option_int("string_lg_commentary_token_team_home", &lg_tokens)] =
2005-10-09 11:35:44 +02:00
g_strdup(fix->teams[0]->name);
2005-07-05 20:37:26 +02:00
token_rep[option_int("string_lg_commentary_token_team_away", &lg_tokens)] =
2005-10-09 11:35:44 +02:00
g_strdup(fix->teams[1]->name);
2005-07-05 20:37:26 +02:00
if(fix->teams[0]->clid < ID_CUP_START)
token_rep[option_int("string_lg_commentary_token_team_layer0", &lg_tokens)] =
misc_int_to_char(league_from_clid(fix->teams[0]->clid)->layer);
if(fix->teams[1]->clid < ID_CUP_START)
token_rep[option_int("string_lg_commentary_token_team_layer1", &lg_tokens)] =
misc_int_to_char(league_from_clid(fix->teams[1]->clid)->layer);
token_rep[option_int("string_lg_commentary_token_league_cup_name", &lg_tokens)] =
g_strdup(league_cup_get_name_string(fix->clid));
if(fix->clid >= ID_CUP_START)
{
cup_get_round_name(cup_from_clid(fix->clid), fix->round, buf);
token_rep[option_int("string_lg_commentary_token_cup_round_name", &lg_tokens)] =
g_strdup(buf);
}
misc_print_grouped_int(fix->attendance, buf);
token_rep[option_int("string_lg_commentary_token_attendance", &lg_tokens)] =
g_strdup(buf);
token_rep[option_int("string_lg_commentary_token_yellow_limit", &lg_tokens)] =
misc_int_to_char(league_cup_get_yellow_red(fix->clid));
}
/** Free the memory occupied by the tokens array and the permanent tokens. */
void
lg_commentary_post_match(void)
{
g_free(token_rep[option_int("string_lg_commentary_token_team_home", &lg_tokens)]);
g_free(token_rep[option_int("string_lg_commentary_token_team_away", &lg_tokens)]);
g_free(token_rep[option_int("string_lg_commentary_token_attendance", &lg_tokens)]);
g_free(token_rep[option_int("string_lg_commentary_token_league_cup_name", &lg_tokens)]);
g_free(token_rep[option_int("string_lg_commentary_token_cup_round_name", &lg_tokens)]);
g_free(token_rep[option_int("string_lg_commentary_token_yellow_limit", &lg_tokens)]);
g_free(token_rep[option_int("string_lg_commentary_token_team_layer0", &lg_tokens)]);
g_free(token_rep[option_int("string_lg_commentary_token_team_layer1", &lg_tokens)]);
g_free(token_rep);
}
2005-06-26 13:42:01 +02:00
/** Load the appropriate commentary file by evaluating
the language option. */
void
lg_commentary_load_commentary_file_from_option(void)
{
gchar buf[SMALL], commentary_file[SMALL];
2005-07-17 13:55:33 +02:00
gchar *cur_locale = NULL;
2005-06-26 13:42:01 +02:00
2005-07-17 13:55:33 +02:00
#ifdef G_OS_UNIX
cur_locale = setlocale(LC_MESSAGES, NULL);
#endif
if(strcmp(opt_str("string_opt_language_code"), "C") == 0)
strcpy(buf, "en");
else if(strcmp(opt_str("string_opt_language_code"), "") == 0 && cur_locale != NULL)
2005-06-26 13:42:01 +02:00
{
strncpy(buf, cur_locale, 2);
buf[2] = '\0';
}
else
2005-07-19 17:55:34 +02:00
strcpy(buf, opt_str("string_opt_language_code"));
2005-06-26 13:42:01 +02:00
sprintf(commentary_file, "lg_commentary_%s.xml", buf);
lg_commentary_load_commentary_file(commentary_file, FALSE);
}
/** Load the commentary file given. Abort if not found and abort=TRUE,
load default english file otherwise. */
void
lg_commentary_load_commentary_file(const gchar *commentary_file, gboolean abort)
{
gchar *file_name = file_find_support_file(commentary_file, FALSE);
gchar buf[SMALL];
if(file_name != NULL)
{
xml_lg_commentary_read(file_name);
g_free(file_name);
}
else
{
if(abort)
{
sprintf(buf, "lg_commentary_load_commentary_file: file %s not found \n",
file_name);
main_exit_program(EXIT_FILE_NOT_FOUND, buf);
}
else
lg_commentary_load_commentary_file("lg_commentary_en.xml", TRUE);
}
}