| 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 | #define ANONGAME_GAMERESULT_INTERNAL_ACCESS |
| 18 | #include "anongame_gameresult.h" |
| 19 | |
| 20 | #include <cassert> |
| 21 | |
| 22 | #include "common/eventlog.h" |
| 23 | #include "common/xalloc.h" |
| 24 | #include "common/packet.h" |
| 25 | #include "common/bn_type.h" |
| 26 | |
| 27 | |
| 28 | #include "common/setup_after.h" |
| 29 | |
| 30 | namespace pvpgn |
| 31 | { |
| 32 | |
| 33 | namespace bnetd |
| 34 | { |
| 35 | |
| 36 | extern int gameresult_destroy(t_anongame_gameresult * gameresult) |
| 37 | { |
| 38 | if (!(gameresult)) |
| 39 | { |
| 40 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL gameresult"); |
| 41 | return -1; |
| 42 | } |
| 43 | |
| 44 | if ((gameresult->players)) xfree((void *)gameresult->players); |
| 45 | if ((gameresult->heroes)) xfree((void *)gameresult->heroes); |
| 46 | xfree((void *)gameresult); |
| 47 | |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | extern t_anongame_gameresult * anongame_gameresult_parse(t_packet const * const packet) |
| 52 | { |
| 53 | t_anongame_gameresult * gameresult; |
| 54 | t_client_w3route_gameresult_player * player; |
| 55 | t_client_w3route_gameresult_part2 * part2; |
| 56 | t_client_w3route_gameresult_hero * hero; |
| 57 | t_client_w3route_gameresult_part3 * part3; |
| 58 | |
| 59 | int counter, heroes_count; |
| 60 | unsigned expectedsize; //still without hero infos |
| 61 | unsigned int offset = 0; |
| 62 | |
| 63 | int result_count = bn_byte_get(packet->u.client_w3route_gameresult.number_of_results); |
| 64 | expectedsize = sizeof(t_client_w3route_gameresult)+ |
| 65 | sizeof(t_client_w3route_gameresult_player)* result_count + |
| 66 | sizeof(t_client_w3route_gameresult_part2)+ |
| 67 | sizeof(t_client_w3route_gameresult_part3); |
| 68 | |
| 69 | if (packet_get_size(packet) < expectedsize) |
| 70 | { |
| 71 | eventlog(eventlog_level_error, __FUNCTION__, "gameresult packet is smaller than expected"); |
| 72 | return NULL; |
| 73 | } |
| 74 | |
| 75 | gameresult = (t_anongame_gameresult*)xmalloc(sizeof(t_anongame_gameresult)); |
| 76 | |
| 77 | gameresult->players = (t_anongame_player*)xmalloc(sizeof(t_anongame_player)*result_count); |
| 78 | |
| 79 | gameresult->number_of_results = result_count; |
| 80 | |
| 81 | offset += sizeof(t_client_w3route_gameresult); |
| 82 | |
| 83 | for (counter = 0; counter < result_count; counter++) |
| 84 | { |
| 85 | player = (t_client_w3route_gameresult_player *)packet_get_raw_data_const(packet, offset); |
| 86 | |
| 87 | gameresult->players[counter].number = bn_byte_get(player->number); |
| 88 | gameresult->players[counter].result = bn_int_get(player->result); |
| 89 | gameresult->players[counter].race = bn_int_get(player->race); |
| 90 | |
| 91 | offset += sizeof(t_client_w3route_gameresult_player); |
| 92 | } |
| 93 | |
| 94 | part2 = (t_client_w3route_gameresult_part2 *)packet_get_raw_data_const(packet, offset); |
| 95 | |
| 96 | gameresult->unit_score = bn_int_get(part2->unit_score); |
| 97 | gameresult->heroes_score = bn_int_get(part2->heroes_score); |
| 98 | gameresult->resource_score = bn_int_get(part2->resource_score); |
| 99 | gameresult->units_produced = bn_int_get(part2->units_produced); |
| 100 | gameresult->units_killed = bn_int_get(part2->units_killed); |
| 101 | gameresult->buildings_produced = bn_int_get(part2->buildings_produced); |
| 102 | gameresult->buildings_razed = bn_int_get(part2->buildings_razed); |
| 103 | gameresult->largest_army = bn_int_get(part2->largest_army); |
| 104 | heroes_count = bn_int_get(part2->heroes_used_count); |
| 105 | gameresult->heroes_used_count = heroes_count; |
| 106 | |
| 107 | offset += sizeof(t_client_w3route_gameresult_part2); |
| 108 | |
| 109 | if ((heroes_count)) |
| 110 | { |
| 111 | gameresult->heroes = (t_anongame_hero*)xmalloc(sizeof(t_anongame_hero)*heroes_count); |
| 112 | |
| 113 | if (packet_get_size(packet) < expectedsize + sizeof(t_client_w3route_gameresult_hero)*heroes_count) |
| 114 | { |
| 115 | eventlog(eventlog_level_error, __FUNCTION__, "gameresult packet is smaller than expected"); |
| 116 | return NULL; |
| 117 | } |
| 118 | |
| 119 | for (counter = 0; counter < heroes_count; counter++) |
| 120 | { |
| 121 | hero = (t_client_w3route_gameresult_hero *)packet_get_raw_data_const(packet, offset); |
| 122 | |
| 123 | gameresult->heroes[counter].level = bn_short_get(hero->level); |
| 124 | gameresult->heroes[counter].race_and_name = bn_int_get(hero->race_and_name); |
| 125 | gameresult->heroes[counter].hero_xp = bn_int_get(hero->hero_xp); |
| 126 | |
| 127 | offset += sizeof(t_client_w3route_gameresult_hero); |
| 128 | } |
| 129 | } |
| 130 | else |
| 131 | gameresult->heroes = NULL; |
| 132 | |
| 133 | part3 = (t_client_w3route_gameresult_part3 *)packet_get_raw_data_const(packet, offset); |
| 134 | |
| 135 | gameresult->heroes_killed = bn_int_get(part3->heroes_killed); |
| 136 | gameresult->items_obtained = bn_int_get(part3->items_obtained); |
| 137 | gameresult->mercenaries_hired = bn_int_get(part3->mercenaries_hired); |
| 138 | gameresult->total_hero_xp = bn_int_get(part3->total_hero_xp); |
| 139 | gameresult->gold_mined = bn_int_get(part3->gold_mined); |
| 140 | gameresult->lumber_harvested = bn_int_get(part3->lumber_harvested); |
| 141 | gameresult->resources_traded_given = bn_int_get(part3->resources_traded_given); |
| 142 | gameresult->resources_traded_taken = bn_int_get(part3->resources_traded_taken); |
| 143 | gameresult->tech_percentage = bn_int_get(part3->tech_percentage); |
| 144 | gameresult->gold_lost_to_upkeep = bn_int_get(part3->gold_lost_to_upkeep); |
| 145 | |
| 146 | return gameresult; |
| 147 | } |
| 148 | |
| 149 | extern char gameresult_get_number_of_results(t_anongame_gameresult * gameresult) |
| 150 | { |
| 151 | assert(gameresult); |
| 152 | |
| 153 | return gameresult->number_of_results; |
| 154 | } |
| 155 | |
| 156 | extern int gameresult_get_player_result(t_anongame_gameresult * gameresult, int player) |
| 157 | { |
| 158 | if (!(gameresult)) |
| 159 | return -1; |
| 160 | |
| 161 | if (player >= gameresult->number_of_results) |
| 162 | { |
| 163 | eventlog(eventlog_level_error, __FUNCTION__, "request for invalid player number"); |
| 164 | return -1; |
| 165 | } |
| 166 | |
| 167 | return gameresult->players[player].result; |
| 168 | } |
| 169 | |
| 170 | extern int gameresult_get_player_number(t_anongame_gameresult * gameresult, int player) |
| 171 | { |
| 172 | if (!(gameresult)) |
| 173 | return -1; |
| 174 | |
| 175 | if (player >= gameresult->number_of_results) |
| 176 | { |
| 177 | eventlog(eventlog_level_error, __FUNCTION__, "request for invalid player number"); |
| 178 | return -1; |
| 179 | } |
| 180 | |
| 181 | return gameresult->players[player].number; |
| 182 | } |
| 183 | |
| 184 | } |
| 185 | |
| 186 | } |