| 1 |
/*
|
| 2 |
* This program is free software; you can redistribute it and/or
|
| 3 |
* modify it under the terms of the GNU General Public License
|
| 4 |
* as published by the Free Software Foundation; either version 2
|
| 5 |
* of the License, or (at your option) any later version.
|
| 6 |
*
|
| 7 |
* This program is distributed in the hope that it will be useful,
|
| 8 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 9 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 10 |
* GNU General Public License for more details.
|
| 11 |
*
|
| 12 |
* You should have received a copy of the GNU General Public License
|
| 13 |
* along with this program; if not, write to the Free Software
|
| 14 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
| 15 |
*/
|
| 16 |
#include "common/setup_before.h"
|
| 17 |
#ifdef STDC_HEADERS
|
| 18 |
# include <stdlib.h>
|
| 19 |
#else
|
| 20 |
# ifdef HAVE_MALLOC_H
|
| 21 |
# include <malloc.h>
|
| 22 |
# endif
|
| 23 |
#endif
|
| 24 |
#ifdef HAVE_STRING_H
|
| 25 |
# include <string.h>
|
| 26 |
#else
|
| 27 |
# ifdef HAVE_STRINGS_H
|
| 28 |
# include <strings.h>
|
| 29 |
# endif
|
| 30 |
#endif
|
| 31 |
#include "compat/strrchr.h"
|
| 32 |
#include <errno.h>
|
| 33 |
#include "compat/strerror.h"
|
| 34 |
#include "common/list.h"
|
| 35 |
#include "common/eventlog.h"
|
| 36 |
#include "common/util.h"
|
| 37 |
#include "common/packet.h"
|
| 38 |
#include "common/tag.h"
|
| 39 |
#include "common/bn_type.h"
|
| 40 |
#include "common/packet.h"
|
| 41 |
#ifdef HAVE_ASSERT_H
|
| 42 |
# include <assert.h>
|
| 43 |
#endif
|
| 44 |
#include "common/xalloc.h"
|
| 45 |
#define ANONGAME_GAMERESULT_INTERNAL_ACCESS
|
| 46 |
#include "anongame_gameresult.h"
|
| 47 |
#include "common/setup_after.h"
|
| 48 |
|
| 49 |
extern int gameresult_destroy(t_anongame_gameresult * gameresult)
|
| 50 |
{
|
| 51 |
if (!(gameresult))
|
| 52 |
{
|
| 53 |
eventlog(eventlog_level_error,__FUNCTION__,"got NULL gameresult");
|
| 54 |
return -1;
|
| 55 |
}
|
| 56 |
|
| 57 |
if ((gameresult->players)) xfree((void *)gameresult->players);
|
| 58 |
if ((gameresult->heroes)) xfree((void *)gameresult->heroes);
|
| 59 |
xfree((void *)gameresult);
|
| 60 |
|
| 61 |
return 0;
|
| 62 |
}
|
| 63 |
|
| 64 |
extern t_anongame_gameresult * anongame_gameresult_parse(t_packet const * const packet)
|
| 65 |
{
|
| 66 |
t_anongame_gameresult * gameresult;
|
| 67 |
t_client_w3route_gameresult_player * player;
|
| 68 |
t_client_w3route_gameresult_part2 * part2;
|
| 69 |
t_client_w3route_gameresult_hero * hero;
|
| 70 |
t_client_w3route_gameresult_part3 * part3;
|
| 71 |
|
| 72 |
int counter, heroes_count;
|
| 73 |
int expectedsize; //still without hero infos
|
| 74 |
unsigned int offset = 0;
|
| 75 |
|
| 76 |
int result_count = bn_byte_get(packet->u.client_w3route_gameresult.number_of_results);
|
| 77 |
expectedsize = sizeof(t_client_w3route_gameresult) +
|
| 78 |
sizeof(t_client_w3route_gameresult_player) * result_count +
|
| 79 |
sizeof(t_client_w3route_gameresult_part2) +
|
| 80 |
sizeof(t_client_w3route_gameresult_part3);
|
| 81 |
|
| 82 |
if (packet_get_size(packet) < expectedsize)
|
| 83 |
{
|
| 84 |
eventlog(eventlog_level_error,__FUNCTION__,"gameresult packet is smaller than expected");
|
| 85 |
return NULL;
|
| 86 |
}
|
| 87 |
|
| 88 |
gameresult = xmalloc(sizeof(t_anongame_gameresult));
|
| 89 |
|
| 90 |
gameresult->players = xmalloc(sizeof(t_anongame_player)*result_count);
|
| 91 |
|
| 92 |
gameresult->number_of_results = result_count;
|
| 93 |
|
| 94 |
offset+= sizeof(t_client_w3route_gameresult);
|
| 95 |
|
| 96 |
for (counter=0; counter<result_count;counter++)
|
| 97 |
{
|
| 98 |
player = (t_client_w3route_gameresult_player *)packet_get_raw_data_const(packet,offset);
|
| 99 |
|
| 100 |
gameresult->players[counter].number = bn_byte_get(player->number);
|
| 101 |
gameresult->players[counter].result = bn_int_get(player->result);
|
| 102 |
gameresult->players[counter].race = bn_int_get(player->race);
|
| 103 |
|
| 104 |
offset+= sizeof(t_client_w3route_gameresult_player);
|
| 105 |
}
|
| 106 |
|
| 107 |
part2 = (t_client_w3route_gameresult_part2 *)packet_get_raw_data_const(packet,offset);
|
| 108 |
|
| 109 |
gameresult->unit_score = bn_int_get(part2->unit_score);
|
| 110 |
gameresult->heroes_score = bn_int_get(part2->heroes_score);
|
| 111 |
gameresult->resource_score = bn_int_get(part2->resource_score);
|
| 112 |
gameresult->units_produced = bn_int_get(part2->units_produced);
|
| 113 |
gameresult->units_killed = bn_int_get(part2->units_killed);
|
| 114 |
gameresult->buildings_produced = bn_int_get(part2->buildings_produced);
|
| 115 |
gameresult->buildings_razed = bn_int_get(part2->buildings_razed);
|
| 116 |
gameresult->largest_army = bn_int_get(part2->largest_army);
|
| 117 |
heroes_count = bn_int_get(part2->heroes_used_count);
|
| 118 |
gameresult->heroes_used_count = heroes_count;
|
| 119 |
|
| 120 |
offset+= sizeof(t_client_w3route_gameresult_part2);
|
| 121 |
|
| 122 |
if ((heroes_count))
|
| 123 |
{
|
| 124 |
gameresult->heroes = xmalloc(sizeof(t_anongame_hero)*heroes_count);
|
| 125 |
|
| 126 |
if (packet_get_size(packet) < expectedsize + sizeof(t_client_w3route_gameresult_hero)*heroes_count)
|
| 127 |
{
|
| 128 |
eventlog(eventlog_level_error,__FUNCTION__,"gameresult packet is smaller than expected");
|
| 129 |
return NULL;
|
| 130 |
}
|
| 131 |
|
| 132 |
for (counter=0; counter < heroes_count; counter++)
|
| 133 |
{
|
| 134 |
hero = (t_client_w3route_gameresult_hero *)packet_get_raw_data_const(packet,offset);
|
| 135 |
|
| 136 |
gameresult->heroes[counter].level = bn_int_get(hero->level);
|
| 137 |
gameresult->heroes[counter].race_and_name = bn_int_get(hero->race_and_name);
|
| 138 |
gameresult->heroes[counter].hero_xp = bn_int_get(hero->hero_xp);
|
| 139 |
|
| 140 |
offset+= sizeof(t_client_w3route_gameresult_hero);
|
| 141 |
}
|
| 142 |
}
|
| 143 |
else
|
| 144 |
gameresult->heroes = NULL;
|
| 145 |
|
| 146 |
part3 = (t_client_w3route_gameresult_part3 *)packet_get_raw_data_const(packet,offset);
|
| 147 |
|
| 148 |
gameresult->heroes_killed = bn_int_get(part3->heroes_killed);
|
| 149 |
gameresult->items_obtained = bn_int_get(part3->items_obtained);
|
| 150 |
gameresult->mercenaries_hired = bn_int_get(part3->mercenaries_hired);
|
| 151 |
gameresult->total_hero_xp = bn_int_get(part3->total_hero_xp);
|
| 152 |
gameresult->gold_mined = bn_int_get(part3->gold_mined);
|
| 153 |
gameresult->lumber_harvested = bn_int_get(part3->lumber_harvested);
|
| 154 |
gameresult->resources_traded_given = bn_int_get(part3->resources_traded_given);
|
| 155 |
gameresult->resources_traded_taken = bn_int_get(part3->resources_traded_taken);
|
| 156 |
gameresult->tech_percentage = bn_int_get(part3->tech_percentage);
|
| 157 |
gameresult->gold_lost_to_upkeep = bn_int_get(part3->gold_lost_to_upkeep);
|
| 158 |
|
| 159 |
return gameresult;
|
| 160 |
}
|
| 161 |
|
| 162 |
extern char gameresult_get_number_of_results(t_anongame_gameresult * gameresult)
|
| 163 |
{
|
| 164 |
assert(gameresult);
|
| 165 |
|
| 166 |
return gameresult->number_of_results;
|
| 167 |
}
|
| 168 |
|
| 169 |
extern int gameresult_get_player_result(t_anongame_gameresult * gameresult, int player)
|
| 170 |
{
|
| 171 |
if (!(gameresult))
|
| 172 |
return -1;
|
| 173 |
|
| 174 |
if (player >= gameresult->number_of_results)
|
| 175 |
{
|
| 176 |
eventlog(eventlog_level_error,__FUNCTION__,"request for invalid player number");
|
| 177 |
return -1;
|
| 178 |
}
|
| 179 |
|
| 180 |
return gameresult->players[player].result;
|
| 181 |
}
|
| 182 |
|
| 183 |
extern int gameresult_get_player_number(t_anongame_gameresult * gameresult, int player)
|
| 184 |
{
|
| 185 |
if (!(gameresult))
|
| 186 |
return -1;
|
| 187 |
|
| 188 |
if (player >= gameresult->number_of_results)
|
| 189 |
{
|
| 190 |
eventlog(eventlog_level_error,__FUNCTION__,"request for invalid player number");
|
| 191 |
return -1;
|
| 192 |
}
|
| 193 |
|
| 194 |
return gameresult->players[player].number;
|
| 195 |
}
|