bygfoot/src/league.c

272 lines
5.8 KiB
C
Raw Normal View History

2005-03-14 18:47:27 +01:00
#include "cup.h"
2004-12-23 13:58:39 +01:00
#include "league.h"
2005-04-04 12:36:04 +02:00
#include "table.h"
#include "team.h"
#include "variables.h"
2004-12-23 13:58:39 +01:00
/**
Create a new league with some default values.
@return The league we created.
@see League
*/
League
league_new(void)
{
League new;
new.name = g_string_new("");
new.sid = g_string_new("");
2004-12-23 13:58:39 +01:00
new.short_name = g_string_new("");
new.symbol = g_string_new("");
new.id = league_new_id();
new.average_skill = -1;
new.prom_rel.prom_games_dest_sid = g_string_new("");
2004-12-23 13:58:39 +01:00
new.prom_rel.elements = g_array_new(FALSE, FALSE, sizeof(PromRelElement));
new.teams = g_array_new(FALSE, FALSE, sizeof(Team));
2005-01-09 21:21:22 +01:00
new.fixtures = g_array_new(FALSE, FALSE, sizeof(Fixture));
2004-12-23 13:58:39 +01:00
2005-04-04 12:36:04 +02:00
new.table = table_new();
2005-01-09 21:21:22 +01:00
new.table.clid = new.id;
2004-12-23 13:58:39 +01:00
new.first_week = new.week_gap = 1;
new.yellow_red = 1000;
return new;
}
/**
Create a new PromRelElement with default values.
@return The PromRelElement we created.
@see PromRelElement
*/
PromRelElement
prom_rel_element_new(void)
{
PromRelElement new;
new.ranks[0] = new.ranks[1] = 0;
new.dest_sid = g_string_new("");
new.type = PROM_REL_NONE;
2004-12-23 13:58:39 +01:00
return new;
}
/** Return a new numerical id for a league.
We browse through the existing leagues and take the
first free nid.
*/
gint
league_new_id(void)
{
gint i, j;
if(ligs == NULL)
return ID_LEAGUE_START;
for(i=ID_LEAGUE_START;i<ID_LEAGUE_START+1000;i++)
{
for(j=0;j<ligs->len;j++)
if(lig(j).id == i)
break;
if(j == ligs->len)
return i;
}
g_warning("league_new_id: didn't find a free numerical id.");
return -1;
}
2005-01-09 21:21:22 +01:00
/** Get the array index of the given league or cup id.
@param clid The id of the league or cup.
@return The index in the leagues or cups array. */
gint
league_cup_get_index_from_clid(gint clid)
{
gint i;
gint index = -1;
if(clid < ID_CUP_START)
{
for(i=0;i<ligs->len;i++)
if(lig(i).id == clid)
{
index = i;
break;
}
}
else
for(i=0;i<cps->len;i++)
if(cp(i).id == clid)
{
index = i;
break;
}
if(index == -1)
2005-04-04 12:36:04 +02:00
g_warning("league_cup_get_index_from_clid: couldn't find league or cup with id %d\n", clid);
2005-01-09 21:21:22 +01:00
return index;
}
/** Return the league pointer belonging to the id.
@param clid The id we look for.
@return The league pointer or NULL if failed. */
League*
league_from_clid(gint clid)
{
gint i;
for(i=0;i<ligs->len;i++)
if(lig(i).id == clid)
return &lig(i);
g_warning("league_from_clid: didn't find league with id %d\n", clid);
return NULL;
}
2005-03-14 18:47:27 +01:00
/** Return the id of the next league (or cup if we are
at the end of the leagues array).
@param clid The id of the current league or cup. */
gint
league_cup_get_next_clid(gint clid)
{
gint i, return_value = -1;
if(clid < ID_CUP_START)
{
for(i=0;i<ligs->len;i++)
if(lig(i).id == clid)
break;
if(i != ligs->len - 1)
return_value = lig(i + 1).id;
else if(cps->len > 0)
return_value = cp(0).id;
else
2005-04-06 13:14:34 +02:00
return_value = lig(0).id;
2005-03-14 18:47:27 +01:00
}
else
{
for(i=0;i<cps->len;i++)
if(cp(i).id == clid)
break;
if(i != cps->len - 1)
return_value = cp(i + 1).id;
else
return_value = lig(0).id;
}
return return_value;
}
/** Return the id of the previous league or cup.
@param clid The id of the current league or cup. */
gint
league_cup_get_previous_clid(gint clid)
{
gint i, return_value = -1;
if(clid < ID_CUP_START)
{
for(i=ligs->len - 1;i>=0;i--)
if(lig(i).id == clid)
break;
if(i != 0)
return_value = lig(i - 1).id;
else if(cps->len > 0)
return_value = cp(cps->len - 1).id;
else
2005-04-06 13:14:34 +02:00
return_value = lig(ligs->len - 1).id;
2005-03-14 18:47:27 +01:00
}
else
{
for(i=cps->len - 1;i>=0;i--)
if(cp(i).id == clid)
break;
if(i != 0)
return_value = cp(i - 1).id;
else
return_value = lig(ligs->len - 1).id;
}
return return_value;
}
/** Return the fixture coming after the specified week and round.
@param league The league the fixtures of which we examine.
@return A fixture pointer or NULL. */
Fixture*
league_cup_get_next_fixture(gint clid, gint week_number, gint week_round_number)
{
gint i;
2005-03-27 19:59:57 +02:00
GArray *fixtures = league_cup_get_fixtures(clid);
2005-03-14 18:47:27 +01:00
for(i=0;i<fixtures->len;i++)
if(g_array_index(fixtures, Fixture, i).week_number > week_number ||
(g_array_index(fixtures, Fixture, i).week_number == week_number &&
g_array_index(fixtures, Fixture, i).week_round_number > week_round_number))
return &g_array_index(fixtures, Fixture, i);
return NULL;
}
/** Return the fixture coming just before the specified week and round.
@param league The league the fixtures of which we examine.
@return A fixture pointer or NULL. */
Fixture*
league_cup_get_previous_fixture(gint clid, gint week_number, gint week_round_number)
{
gint i;
2005-03-27 19:59:57 +02:00
GArray *fixtures = league_cup_get_fixtures(clid);
2005-03-14 18:47:27 +01:00
for(i=fixtures->len - 1;i>=0;i--)
if(g_array_index(fixtures, Fixture, i).week_number < week_number ||
(g_array_index(fixtures, Fixture, i).week_number == week_number &&
g_array_index(fixtures, Fixture, i).week_round_number < week_round_number))
return &g_array_index(fixtures, Fixture, i);
return NULL;
}
/** Return the number of league in the leagues array. */
gint
league_get_index(gint clid)
{
gint i;
for(i=0;i<ligs->len;i++)
if(lig(i).id == clid)
return i;
g_warning("league_get_index: reached end of leagues array; clid is %d\n", clid);
return -1;
}
2005-03-16 21:48:25 +01:00
/** Return the average stadium capacity of cpu teams
in the specified league or cup. */
gint
league_cup_average_capacity(gint clid)
{
gint i;
gfloat sum = 0;
2005-03-27 19:59:57 +02:00
const GArray *teams = league_cup_get_teams(clid);
2005-03-16 21:48:25 +01:00
for(i=0;i<teams->len;i++)
if(team_is_user(&g_array_index(teams, Team, i)) == -1)
sum += g_array_index(teams, Team, i).stadium.capacity;
return sum / (gfloat)teams->len;
}