| 1 | /* |
| 2 | * Copyright (C) 2004 CreepLord ([email protected]) |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #include "common/setup_before.h" |
| 20 | #include "anongame.h" |
| 21 | |
| 22 | #include <cstdint> |
| 23 | #include <cstdlib> |
| 24 | #include <cstring> |
| 25 | |
| 26 | #include "compat/strdup.h" |
| 27 | #include "common/packet.h" |
| 28 | #include "common/eventlog.h" |
| 29 | #include "common/tag.h" |
| 30 | #include "common/queue.h" |
| 31 | #include "common/bn_type.h" |
| 32 | #include "common/list.h" |
| 33 | #include "common/addr.h" |
| 34 | #include "common/xalloc.h" |
| 35 | #include "common/trans.h" |
| 36 | |
| 37 | #include "team.h" |
| 38 | #include "account.h" |
| 39 | #include "account_wrap.h" |
| 40 | #include "connection.h" |
| 41 | #include "prefs.h" |
| 42 | #include "versioncheck.h" |
| 43 | #include "tournament.h" |
| 44 | #include "timer.h" |
| 45 | #include "ladder.h" |
| 46 | #include "server.h" |
| 47 | #include "anongame_maplists.h" |
| 48 | #include "anongame_gameresult.h" |
| 49 | #include "common/setup_after.h" |
| 50 | |
| 51 | #define MAX_LEVEL 100 |
| 52 | |
| 53 | namespace pvpgn |
| 54 | { |
| 55 | |
| 56 | namespace bnetd |
| 57 | { |
| 58 | |
| 59 | /* [quetzal] 20020827 - this one get modified by anongame_queue player when there're enough |
| 60 | * players and map has been chosen based on their preferences. otherwise its NULL |
| 61 | */ |
| 62 | static const char *mapname = NULL; |
| 63 | |
| 64 | static int players[ANONGAME_TYPES] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 65 | static t_connection *player[ANONGAME_TYPES][ANONGAME_MAX_GAMECOUNT]; |
| 66 | |
| 67 | /* [quetzal] 20020815 - queue to hold matching players */ |
| 68 | static t_list *matchlists[ANONGAME_TYPES][MAX_LEVEL]; |
| 69 | |
| 70 | long average_anongame_search_time = 30; |
| 71 | unsigned int anongame_search_count = 0; |
| 72 | |
| 73 | /**********************************************************************************/ |
| 74 | static t_connection *_connlist_find_connection_by_uid(int uid); |
| 75 | |
| 76 | static int _anongame_gametype_to_queue(int type, int gametype); |
| 77 | static int _anongame_level_by_queue(t_connection * c, int queue); |
| 78 | static const char * _get_map_from_prefs(int queue, std::uint32_t cur_prefs, t_clienttag clienttag); |
| 79 | static unsigned int _anongame_get_gametype_tab(int queue); |
| 80 | |
| 81 | static int _anongame_totalplayers(int queue); |
| 82 | static int _anongame_totalteams(int queue); |
| 83 | |
| 84 | static int _handle_anongame_search(t_connection * c, t_packet const *packet); |
| 85 | static int _anongame_queue(t_connection * c, int queue, std::uint32_t map_prefs); |
| 86 | static int _anongame_compare_level(void const *a, void const *b); |
| 87 | static int _anongame_order_queue(int queue); |
| 88 | static int _anongame_match(t_connection * c, int queue); |
| 89 | static int _anongame_search_found(int queue); |
| 90 | /**********************************************************************************/ |
| 91 | |
| 92 | static t_connection *_connlist_find_connection_by_uid(int uid) |
| 93 | { |
| 94 | return connlist_find_connection_by_account(accountlist_find_account_by_uid(uid)); |
| 95 | } |
| 96 | |
| 97 | /**********/ |
| 98 | |
| 99 | static char const *_anongame_queue_to_string(int queue) |
| 100 | { |
| 101 | switch (queue) { |
| 102 | case ANONGAME_TYPE_1V1: |
| 103 | return "PG 1v1"; |
| 104 | case ANONGAME_TYPE_2V2: |
| 105 | return "PG 2v2"; |
| 106 | case ANONGAME_TYPE_3V3: |
| 107 | return "PG 3v3"; |
| 108 | case ANONGAME_TYPE_4V4: |
| 109 | return "PG 4v4"; |
| 110 | case ANONGAME_TYPE_SMALL_FFA: |
| 111 | return "PG SFFA"; |
| 112 | case ANONGAME_TYPE_AT_2V2: |
| 113 | return "AT 2v2"; |
| 114 | case ANONGAME_TYPE_TEAM_FFA: |
| 115 | return "AT TFFA"; |
| 116 | case ANONGAME_TYPE_AT_3V3: |
| 117 | return "AT 3v3"; |
| 118 | case ANONGAME_TYPE_AT_4V4: |
| 119 | return "AT 4v4"; |
| 120 | case ANONGAME_TYPE_TY: |
| 121 | return "TOURNEY"; |
| 122 | case ANONGAME_TYPE_5V5: |
| 123 | return "PG 5v5"; |
| 124 | case ANONGAME_TYPE_6V6: |
| 125 | return "PG 6v6"; |
| 126 | case ANONGAME_TYPE_2V2V2: |
| 127 | return "PG 2v2v2"; |
| 128 | case ANONGAME_TYPE_3V3V3: |
| 129 | return "PG 3v3v3"; |
| 130 | case ANONGAME_TYPE_4V4V4: |
| 131 | return "PG 4v4v4"; |
| 132 | case ANONGAME_TYPE_2V2V2V2: |
| 133 | return "PG 2v2v2v2"; |
| 134 | case ANONGAME_TYPE_3V3V3V3: |
| 135 | return "PG 3v3v3v3"; |
| 136 | case ANONGAME_TYPE_AT_2V2V2: |
| 137 | return "AT 2v2v2"; |
| 138 | default: |
| 139 | eventlog(eventlog_level_error, __FUNCTION__, "invalid queue number {}", queue); |
| 140 | return "error"; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | static int _anongame_gametype_to_queue(int type, int gametype) |
| 145 | { |
| 146 | switch (type) { |
| 147 | case 0: /* PG */ |
| 148 | switch (gametype) { |
| 149 | case 0: |
| 150 | return ANONGAME_TYPE_1V1; |
| 151 | case 1: |
| 152 | return ANONGAME_TYPE_2V2; |
| 153 | case 2: |
| 154 | return ANONGAME_TYPE_3V3; |
| 155 | case 3: |
| 156 | return ANONGAME_TYPE_4V4; |
| 157 | case 4: |
| 158 | return ANONGAME_TYPE_SMALL_FFA; |
| 159 | case 5: |
| 160 | return ANONGAME_TYPE_5V5; |
| 161 | case 6: |
| 162 | return ANONGAME_TYPE_6V6; |
| 163 | case 7: |
| 164 | return ANONGAME_TYPE_2V2V2; |
| 165 | case 8: |
| 166 | return ANONGAME_TYPE_3V3V3; |
| 167 | case 9: |
| 168 | return ANONGAME_TYPE_4V4V4; |
| 169 | case 10: |
| 170 | return ANONGAME_TYPE_2V2V2V2; |
| 171 | case 11: |
| 172 | return ANONGAME_TYPE_3V3V3V3; |
| 173 | default: |
| 174 | eventlog(eventlog_level_error, __FUNCTION__, "invalid PG game type: {}", gametype); |
| 175 | return -1; |
| 176 | } |
| 177 | case 1: /* AT */ |
| 178 | switch (gametype) { |
| 179 | case 0: |
| 180 | return ANONGAME_TYPE_AT_2V2; |
| 181 | case 2: |
| 182 | return ANONGAME_TYPE_AT_3V3; |
| 183 | case 3: |
| 184 | return ANONGAME_TYPE_AT_4V4; |
| 185 | case 4: |
| 186 | return ANONGAME_TYPE_AT_2V2V2; |
| 187 | default: |
| 188 | eventlog(eventlog_level_error, __FUNCTION__, "invalid AT game type: {}", gametype); |
| 189 | return -1; |
| 190 | } |
| 191 | case 2: /* TY */ |
| 192 | return ANONGAME_TYPE_TY; |
| 193 | default: |
| 194 | eventlog(eventlog_level_error, __FUNCTION__, "invalid type: {}", type); |
| 195 | return -1; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | static int _anongame_level_by_queue(t_connection * c, int queue) |
| 200 | { |
| 201 | t_clienttag ct = conn_get_clienttag(c); |
| 202 | |
| 203 | switch (queue) { |
| 204 | case ANONGAME_TYPE_1V1: |
| 205 | return account_get_ladder_level(conn_get_account(c), ct, ladder_id_solo); |
| 206 | case ANONGAME_TYPE_2V2: |
| 207 | case ANONGAME_TYPE_3V3: |
| 208 | case ANONGAME_TYPE_4V4: |
| 209 | case ANONGAME_TYPE_5V5: |
| 210 | case ANONGAME_TYPE_6V6: |
| 211 | case ANONGAME_TYPE_2V2V2: |
| 212 | case ANONGAME_TYPE_3V3V3: |
| 213 | case ANONGAME_TYPE_4V4V4: |
| 214 | case ANONGAME_TYPE_2V2V2V2: |
| 215 | case ANONGAME_TYPE_3V3V3V3: |
| 216 | return account_get_ladder_level(conn_get_account(c), ct, ladder_id_ffa); |
| 217 | case ANONGAME_TYPE_SMALL_FFA: |
| 218 | case ANONGAME_TYPE_TEAM_FFA: |
| 219 | return account_get_ladder_level(conn_get_account(c), ct, ladder_id_ffa); |
| 220 | case ANONGAME_TYPE_AT_2V2: |
| 221 | case ANONGAME_TYPE_AT_3V3: |
| 222 | case ANONGAME_TYPE_AT_4V4: |
| 223 | case ANONGAME_TYPE_AT_2V2V2: |
| 224 | return 0; |
| 225 | case ANONGAME_TYPE_TY: /* set to ((wins * 3) + ties - losses) ie. prelim score */ |
| 226 | return tournament_get_player_score(conn_get_account(c)); |
| 227 | default: |
| 228 | eventlog(eventlog_level_error, __FUNCTION__, "unknown queue: {}", queue); |
| 229 | return -1; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | static const char * _get_map_from_prefs(int queue, std::uint32_t cur_prefs, t_clienttag clienttag) |
| 234 | { |
| 235 | int i, j = 0; |
| 236 | const char *default_map, *selected; |
| 237 | char *res_maps[32]; |
| 238 | char clienttag_str[5]; |
| 239 | |
| 240 | switch (clienttag) { |
| 241 | case CLIENTTAG_WARCRAFT3_UINT: |
| 242 | default_map = "Maps\\(8)PlainsOfSnow.w3m"; |
| 243 | break; |
| 244 | case CLIENTTAG_WAR3XP_UINT: |
| 245 | default_map = "Maps\\(8)PlainsOfSnow.w3m"; |
| 246 | break; |
| 247 | default: |
| 248 | ERROR1("invalid clienttag: {}", tag_uint_to_str(clienttag_str, clienttag)); |
| 249 | return "Maps\\(8)PlainsOfSnow.w3m"; |
| 250 | } |
| 251 | |
| 252 | for (i = 0; i < 32; i++) |
| 253 | res_maps[i] = NULL; |
| 254 | |
| 255 | for (i = 0; i < 32; i++) { |
| 256 | if (cur_prefs & 1) |
| 257 | res_maps[j++] = maplists_get_map(queue, clienttag, i + 1); |
| 258 | cur_prefs >>= 1; |
| 259 | } |
| 260 | |
| 261 | i = std::rand() % j; |
| 262 | if (res_maps[i]) |
| 263 | selected = res_maps[i]; |
| 264 | else |
| 265 | selected = default_map; |
| 266 | |
| 267 | eventlog(eventlog_level_trace, __FUNCTION__, "got map {} from prefs", selected); |
| 268 | return selected; |
| 269 | } |
| 270 | |
| 271 | static unsigned int _anongame_get_gametype_tab(int queue) |
| 272 | { |
| 273 | /* dizzy: this changed in 1.05 */ |
| 274 | switch (queue) { |
| 275 | case ANONGAME_TYPE_1V1: |
| 276 | return SERVER_ANONGAME_SOLO_STR; |
| 277 | case ANONGAME_TYPE_2V2: |
| 278 | case ANONGAME_TYPE_3V3: |
| 279 | case ANONGAME_TYPE_4V4: |
| 280 | case ANONGAME_TYPE_5V5: |
| 281 | case ANONGAME_TYPE_6V6: |
| 282 | case ANONGAME_TYPE_2V2V2: |
| 283 | case ANONGAME_TYPE_3V3V3: |
| 284 | case ANONGAME_TYPE_4V4V4: |
| 285 | case ANONGAME_TYPE_2V2V2V2: |
| 286 | case ANONGAME_TYPE_3V3V3V3: |
| 287 | return SERVER_ANONGAME_TEAM_STR; |
| 288 | case ANONGAME_TYPE_SMALL_FFA: |
| 289 | return SERVER_ANONGAME_SFFA_STR; |
| 290 | case ANONGAME_TYPE_TEAM_FFA: |
| 291 | return 0; /* Team FFA is no longer supported */ |
| 292 | case ANONGAME_TYPE_AT_2V2: |
| 293 | return SERVER_ANONGAME_AT2v2_STR; |
| 294 | case ANONGAME_TYPE_AT_3V3: |
| 295 | return SERVER_ANONGAME_AT3v3_STR; |
| 296 | case ANONGAME_TYPE_AT_4V4: |
| 297 | return SERVER_ANONGAME_AT4v4_STR; |
| 298 | case ANONGAME_TYPE_AT_2V2V2: |
| 299 | return SERVER_ANONGAME_AT2v2_STR; /* fixme */ |
| 300 | case ANONGAME_TYPE_TY: |
| 301 | return SERVER_ANONGAME_TY_STR; |
| 302 | default: |
| 303 | eventlog(eventlog_level_error, __FUNCTION__, "invalid queue ({})", queue); |
| 304 | return 0; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | static int _anongame_totalplayers(int queue) |
| 309 | { |
| 310 | switch (queue) { |
| 311 | case ANONGAME_TYPE_1V1: |
| 312 | return 2; |
| 313 | case ANONGAME_TYPE_2V2: |
| 314 | case ANONGAME_TYPE_AT_2V2: |
| 315 | case ANONGAME_TYPE_SMALL_FFA: /* fixme: total players not always 4 */ |
| 316 | return 4; |
| 317 | case ANONGAME_TYPE_3V3: |
| 318 | case ANONGAME_TYPE_AT_3V3: |
| 319 | case ANONGAME_TYPE_2V2V2: |
| 320 | case ANONGAME_TYPE_AT_2V2V2: |
| 321 | return 6; |
| 322 | case ANONGAME_TYPE_4V4: |
| 323 | case ANONGAME_TYPE_AT_4V4: |
| 324 | case ANONGAME_TYPE_TEAM_FFA: |
| 325 | case ANONGAME_TYPE_2V2V2V2: |
| 326 | return 8; |
| 327 | case ANONGAME_TYPE_3V3V3: |
| 328 | return 9; |
| 329 | case ANONGAME_TYPE_5V5: |
| 330 | return 10; |
| 331 | case ANONGAME_TYPE_6V6: |
| 332 | case ANONGAME_TYPE_4V4V4: |
| 333 | case ANONGAME_TYPE_3V3V3V3: |
| 334 | return 12; |
| 335 | case ANONGAME_TYPE_TY: |
| 336 | return tournament_get_totalplayers(); |
| 337 | default: |
| 338 | eventlog(eventlog_level_error, __FUNCTION__, "unknown queue: {}", queue); |
| 339 | return 0; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | static int _anongame_totalteams(int queue) |
| 344 | { |
| 345 | /* dont forget to change this if you make some game type with more teams */ |
| 346 | #define ANONGAME_MAX_TEAMS 4 |
| 347 | switch (queue) { |
| 348 | case ANONGAME_TYPE_1V1: |
| 349 | case ANONGAME_TYPE_SMALL_FFA: |
| 350 | return 0; |
| 351 | case ANONGAME_TYPE_2V2: |
| 352 | case ANONGAME_TYPE_3V3: |
| 353 | case ANONGAME_TYPE_4V4: |
| 354 | case ANONGAME_TYPE_5V5: |
| 355 | case ANONGAME_TYPE_6V6: |
| 356 | case ANONGAME_TYPE_AT_2V2: |
| 357 | case ANONGAME_TYPE_AT_3V3: |
| 358 | case ANONGAME_TYPE_AT_4V4: |
| 359 | return 2; |
| 360 | case ANONGAME_TYPE_2V2V2: |
| 361 | case ANONGAME_TYPE_3V3V3: |
| 362 | case ANONGAME_TYPE_4V4V4: |
| 363 | case ANONGAME_TYPE_AT_2V2V2: |
| 364 | return 3; |
| 365 | case ANONGAME_TYPE_TEAM_FFA: /* not even used */ |
| 366 | case ANONGAME_TYPE_2V2V2V2: |
| 367 | case ANONGAME_TYPE_3V3V3V3: |
| 368 | return 4; |
| 369 | case ANONGAME_TYPE_TY: |
| 370 | return 2; /* fixme: does not support 2v2v2 - tournament_get_totalteams() */ |
| 371 | default: |
| 372 | eventlog(eventlog_level_error, __FUNCTION__, "unknown queue: {}", queue); |
| 373 | return 0; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | /**********/ |
| 378 | static int _handle_anongame_search(t_connection * c, t_packet const *packet) |
| 379 | { |
| 380 | int i, j, temp, set = 1; |
| 381 | t_packet *rpacket; |
| 382 | t_connection *tc[6]; |
| 383 | t_anongame *a, *ta; |
| 384 | std::uint8_t teamsize = 0; |
| 385 | std::uint8_t option = bn_byte_get(packet->u.client_findanongame.option); |
| 386 | |
| 387 | if (!(a = conn_get_anongame(c))) { |
| 388 | if (!(a = conn_create_anongame(c))) { |
| 389 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] conn_create_anongame failed", conn_get_socket(c)); |
| 390 | return -1; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | conn_set_anongame_search_starttime(c, now); |
| 395 | |
| 396 | switch (option) { |
| 397 | case CLIENT_FINDANONGAME_AT_INVITER_SEARCH: |
| 398 | a->count = bn_int_get(packet->u.client_findanongame_at_inv.count); |
| 399 | a->id = bn_int_get(packet->u.client_findanongame_at_inv.id); |
| 400 | a->tid = bn_int_get(packet->u.client_findanongame_at_inv.tid); |
| 401 | a->race = bn_int_get(packet->u.client_findanongame_at_inv.race); |
| 402 | a->map_prefs = bn_int_get(packet->u.client_findanongame_at_inv.map_prefs); |
| 403 | a->type = bn_byte_get(packet->u.client_findanongame_at_inv.type); |
| 404 | a->gametype = bn_byte_get(packet->u.client_findanongame_at_inv.gametype); |
| 405 | teamsize = bn_byte_get(packet->u.client_findanongame_at_inv.teamsize); |
| 406 | break; |
| 407 | case CLIENT_FINDANONGAME_AT_SEARCH: |
| 408 | a->count = bn_int_get(packet->u.client_findanongame_at.count); |
| 409 | a->id = bn_int_get(packet->u.client_findanongame_at.id); |
| 410 | a->tid = bn_int_get(packet->u.client_findanongame_at.tid); |
| 411 | a->race = bn_int_get(packet->u.client_findanongame_at.race); |
| 412 | teamsize = bn_byte_get(packet->u.client_findanongame_at.teamsize); |
| 413 | break; |
| 414 | case CLIENT_FINDANONGAME_SEARCH: |
| 415 | a->count = bn_int_get(packet->u.client_findanongame.count); |
| 416 | a->id = bn_int_get(packet->u.client_findanongame.id); |
| 417 | a->race = bn_int_get(packet->u.client_findanongame.race); |
| 418 | a->map_prefs = bn_int_get(packet->u.client_findanongame.map_prefs); |
| 419 | a->type = bn_byte_get(packet->u.client_findanongame.type); |
| 420 | a->gametype = bn_byte_get(packet->u.client_findanongame.gametype); |
| 421 | break; |
| 422 | default: |
| 423 | eventlog(eventlog_level_error, __FUNCTION__, "invalid search option ({})", option); |
| 424 | return -1; |
| 425 | } |
| 426 | |
| 427 | if (option != CLIENT_FINDANONGAME_AT_SEARCH) |
| 428 | if ((a->queue = _anongame_gametype_to_queue(a->type, a->gametype)) < 0) { |
| 429 | eventlog(eventlog_level_error, __FUNCTION__, "invalid queue: {}", a->queue); |
| 430 | return -1; |
| 431 | } |
| 432 | |
| 433 | account_set_w3pgrace(conn_get_account(c), conn_get_clienttag(c), a->race); |
| 434 | |
| 435 | /* send search reply to client */ |
| 436 | if (!(rpacket = packet_create(packet_class_bnet))) |
| 437 | return -1; |
| 438 | packet_set_size(rpacket, sizeof(t_server_anongame_search_reply)); |
| 439 | packet_set_type(rpacket, SERVER_ANONGAME_SEARCH_REPLY); |
| 440 | bn_byte_set(&rpacket->u.server_anongame_search_reply.option, SERVER_FINDANONGAME_SEARCH); |
| 441 | bn_int_set(&rpacket->u.server_anongame_search_reply.count, a->count); |
| 442 | bn_int_set(&rpacket->u.server_anongame_search_reply.reply, 0); |
| 443 | temp = (int)average_anongame_search_time; |
| 444 | packet_append_data(rpacket, &temp, 2); |
| 445 | conn_push_outqueue(c, rpacket); |
| 446 | packet_del_ref(rpacket); |
| 447 | /* end search reply */ |
| 448 | |
| 449 | switch (option) { |
| 450 | case CLIENT_FINDANONGAME_AT_INVITER_SEARCH: |
| 451 | for (i = 0; i < teamsize; i++) { /* assign player conns to tc[] array */ |
| 452 | if (!(tc[i] = _connlist_find_connection_by_uid(bn_int_get(packet->u.client_findanongame_at_inv.info[i])))) { |
| 453 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] got NULL connection", conn_get_socket(tc[i])); |
| 454 | return -1; |
| 455 | } |
| 456 | } |
| 457 | for (i = 0; i < teamsize; i++) { /* assign info from inviter to other team players */ |
| 458 | if (!(ta = conn_get_anongame(tc[i]))) { |
| 459 | if (!(ta = conn_create_anongame(tc[i]))) { |
| 460 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] conn_create_anongame failed", conn_get_socket(tc[i])); |
| 461 | return -1; |
| 462 | } |
| 463 | } |
| 464 | for (j = 0; j < teamsize; j++) /* add each players conn to each anongame struct */ |
| 465 | ta->tc[j] = tc[j]; |
| 466 | |
| 467 | ta->type = a->type; |
| 468 | ta->gametype = a->gametype; |
| 469 | ta->queue = a->queue; |
| 470 | ta->map_prefs = a->map_prefs; |
| 471 | |
| 472 | if (ta->tid != a->tid) |
| 473 | set = 0; |
| 474 | } |
| 475 | if (!set) /* check if search packet has been recieved from each team member */ |
| 476 | return 0; |
| 477 | break; |
| 478 | case CLIENT_FINDANONGAME_AT_SEARCH: |
| 479 | for (i = 0; i < teamsize; i++) { /* assign player conns to tc[] array */ |
| 480 | if (!(tc[i] = _connlist_find_connection_by_uid(bn_int_get(packet->u.client_findanongame_at.info[i])))) { |
| 481 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] got NULL connection", conn_get_socket(tc[i])); |
| 482 | return -1; |
| 483 | } |
| 484 | } |
| 485 | for (i = 0; i < teamsize; i++) { /* check if search packet has been recieved from each team member */ |
| 486 | if (!(ta = conn_get_anongame(tc[i]))) |
| 487 | return 0; |
| 488 | if (ta->tid != a->tid) |
| 489 | return 0; |
| 490 | } |
| 491 | break; |
| 492 | case CLIENT_FINDANONGAME_SEARCH: |
| 493 | tc[0] = c; |
| 494 | a->tc[0] = c; |
| 495 | break; |
| 496 | default: |
| 497 | eventlog(eventlog_level_error, __FUNCTION__, "invalid search option ({})", option); |
| 498 | return -1; |
| 499 | } |
| 500 | |
| 501 | if (_anongame_queue(tc[0], a->queue, a->map_prefs) < 0) { |
| 502 | eventlog(eventlog_level_error, __FUNCTION__, "queue failed"); |
| 503 | return -1; |
| 504 | } |
| 505 | |
| 506 | _anongame_match(c, a->queue); |
| 507 | |
| 508 | /* if enough players are queued send found packet */ |
| 509 | if (players[a->queue] == _anongame_totalplayers(a->queue)) |
| 510 | if (_anongame_search_found(a->queue) < 0) |
| 511 | return -1; |
| 512 | |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | static int _anongame_queue(t_connection * c, int queue, std::uint32_t map_prefs) |
| 517 | { |
| 518 | int level; |
| 519 | t_matchdata *md; |
| 520 | |
| 521 | if (!c) { |
| 522 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection"); |
| 523 | } |
| 524 | |
| 525 | if (queue >= ANONGAME_TYPES) { |
| 526 | eventlog(eventlog_level_error, __FUNCTION__, "unknown queue: {}", queue); |
| 527 | return -1; |
| 528 | } |
| 529 | |
| 530 | level = _anongame_level_by_queue(c, queue); |
| 531 | |
| 532 | if (!matchlists[queue][level]) |
| 533 | matchlists[queue][level] = list_create(); |
| 534 | |
| 535 | md = (t_matchdata*)xmalloc(sizeof(t_matchdata)); |
| 536 | md->c = c; |
| 537 | md->map_prefs = map_prefs; |
| 538 | md->versiontag = conn_get_versioncheck(c) ? conn_get_versioncheck(c)->get_version_tag().c_str() : nullptr; |
| 539 | |
| 540 | list_append_data(matchlists[queue][level], md); |
| 541 | |
| 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | static int _anongame_compare_level(void const *a, void const *b) |
| 546 | { |
| 547 | t_connection *ca = *(t_connection * const *)a; |
| 548 | t_connection *cb = *(t_connection * const *)b; |
| 549 | |
| 550 | int level_a = _anongame_level_by_queue(ca, anongame_get_queue(conn_get_anongame(ca))); |
| 551 | int level_b = _anongame_level_by_queue(cb, anongame_get_queue(conn_get_anongame(cb))); |
| 552 | |
| 553 | return (level_a > level_b) ? -1 : ((level_a < level_b) ? 1 : 0); |
| 554 | } |
| 555 | |
| 556 | static int _anongame_order_queue(int queue) |
| 557 | { |
| 558 | if (_anongame_totalteams(queue) != 0 && !anongame_arranged(queue)) { /* no need to reorder 1v1, sffa, or AT queues */ |
| 559 | int i, j; |
| 560 | t_connection *temp; |
| 561 | int level[ANONGAME_MAX_TEAMS]; |
| 562 | int teams = _anongame_totalteams(queue); /* number of teams */ |
| 563 | int ppt = (teams > 0) ? (players[queue] / teams) : 0; /* players per team */ |
| 564 | |
| 565 | for (i = 0; i < ANONGAME_MAX_TEAMS; i++) |
| 566 | level[i] = 0; |
| 567 | |
| 568 | for (i = 0; i < ppt - 1; i++) { /* loop through the number of players per team */ |
| 569 | for (j = 0; j < teams; j++) { |
| 570 | level[j] = level[j] + _anongame_level_by_queue(player[queue][i * ppt + j], queue); |
| 571 | } |
| 572 | |
| 573 | if (teams == 2) { |
| 574 | /* 1 >= 2 */ |
| 575 | if (level[i * teams] >= level[i * teams + 1]) { |
| 576 | temp = player[queue][(i + 1) * teams]; |
| 577 | player[queue][(i + 1) * teams] = player[queue][(i + 1) * teams + 1]; |
| 578 | player[queue][(i + 1) * teams + 1] = temp; |
| 579 | } |
| 580 | /* 2 >= 1 */ |
| 581 | else if (level[i * teams + 1] >= level[i * teams]) { |
| 582 | ; /* nothing to do */ |
| 583 | } |
| 584 | } |
| 585 | /* end 2 teams */ |
| 586 | else if (teams == 3) { |
| 587 | /* 1 >= 2 >= 3 */ |
| 588 | if (level[i * 3] >= level[i * 3 + 1] && level[i * 3 + 1] >= level[i * 3 + 2]) { |
| 589 | temp = player[queue][(i + 1) * 3]; |
| 590 | player[queue][(i + 1) * 3] = player[queue][(i + 1) * 3 + 2]; |
| 591 | player[queue][(i + 1) * 3 + 2] = temp; |
| 592 | } |
| 593 | /* 1 >= 3 >= 2 */ |
| 594 | else if (level[i * 3] >= level[i * 3 + 2] && level[i * 3 + 2] >= level[i * 3 + 1]) { |
| 595 | temp = player[queue][(i + 1) * 3]; |
| 596 | player[queue][(i + 1) * 3] = player[queue][(i + 1) * 3 + 2]; |
| 597 | player[queue][(i + 1) * 3 + 2] = player[queue][(i + 1) * 3 + 1]; |
| 598 | player[queue][(i + 1) * 3 + 1] = temp; |
| 599 | } |
| 600 | /* 2 >= 1 >= 3 */ |
| 601 | else if (level[i * 3 + 1] >= level[i * 3] && level[i * 3] >= level[i * 3 + 2]) { |
| 602 | temp = player[queue][(i + 1) * 3]; |
| 603 | player[queue][(i + 1) * 3] = player[queue][(i + 1) * 3 + 1]; |
| 604 | player[queue][(i + 1) * 3 + 1] = player[queue][(i + 1) * 3 + 2]; |
| 605 | player[queue][(i + 1) * 3 + 2] = temp; |
| 606 | } |
| 607 | /* 2 >= 3 >= 1 */ |
| 608 | else if (level[i * 3 + 1] >= level[i * 3 + 2] && level[i * 3 + 2] >= level[i * 3]) { |
| 609 | temp = player[queue][(i + 1) * 3 + 1]; |
| 610 | player[queue][(i + 1) * 3 + 1] = player[queue][(i + 1) * 3 + 2]; |
| 611 | player[queue][(i + 1) * 3 + 2] = temp; |
| 612 | } |
| 613 | /* 3 >= 1 >= 2 */ |
| 614 | else if (level[i * 3 + 2] >= level[i * 3] && level[i * 3] >= level[i * 3 + 1]) { |
| 615 | temp = player[queue][(i + 1) * 3]; |
| 616 | player[queue][(i + 1) * 3] = player[queue][(i + 1) * 3 + 1]; |
| 617 | player[queue][(i + 1) * 3 + 1] = temp; |
| 618 | } |
| 619 | /* 3 >= 2 >= 1 */ |
| 620 | else if (level[i * 3 + 2] >= level[i * 3 + 1] && level[i * 3 + 1] >= level[i * 3]) { |
| 621 | ; /* nothing to do */ |
| 622 | } |
| 623 | } |
| 624 | /* end 3 teams */ |
| 625 | else if (teams == 4) { |
| 626 | /* 1234 */ |
| 627 | if (level[i * 4] >= level[i * 4 + 1] && level[i * 4 + 1] >= level[i * 4 + 2] && level[i * 4 + 2] >= level[i * 4 + 3]) { |
| 628 | temp = player[queue][(i + 1) * 4]; |
| 629 | player[queue][(i + 1) * 4] = player[queue][(i + 1) * 4 + 3]; |
| 630 | player[queue][(i + 1) * 4 + 3] = temp; |
| 631 | temp = player[queue][(i + 1) * 4 + 1]; |
| 632 | player[queue][(i + 1) * 4 + 1] = player[queue][(i + 1) * 4 + 2]; |
| 633 | player[queue][(i + 1) * 4 + 2] = temp; |
| 634 | } |
| 635 | /* 1243 */ |
| 636 | else if (level[i * 4] >= level[i * 4 + 1] && level[i * 4 + 1] >= level[i * 4 + 2] && level[i * 4 + 3] >= level[i * 4 + 2]) { |
| 637 | temp = player[queue][(i + 1) * 4]; |
| 638 | player[queue][(i + 1) * 4] = player[queue][(i + 1) * 4 + 3]; |
| 639 | player[queue][(i + 1) * 4 + 3] = player[queue][(i + 1) * 4 + 1]; |
| 640 | player[queue][(i + 1) * 4 + 1] = player[queue][(i + 1) * 4 + 2]; |
| 641 | player[queue][(i + 1) * 4 + 2] = temp; |
| 642 | } |
| 643 | /* 1324 */ |
| 644 | else if (level[i * 4] >= level[i * 4 + 2] && level[i * 4 + 2] >= level[i * 4 + 1] && level[i * 4 + 1] >= level[i * 4 + 3]) { |
| 645 | temp = player[queue][(i + 1) * 4]; |
| 646 | player[queue][(i + 1) * 4] = player[queue][(i + 1) * 4 + 3]; |
| 647 | player[queue][(i + 1) * 4 + 3] = temp; |
| 648 | } |
| 649 | /* 1342 */ |
| 650 | else if (level[i * 4] >= level[i * 4 + 2] && level[i * 4 + 2] >= level[i * 4 + 3] && level[i * 4 + 3] >= level[i * 4 + 1]) { |
| 651 | temp = player[queue][(i + 1) * 4]; |
| 652 | player[queue][(i + 1) * 4] = player[queue][(i + 1) * 4 + 3]; |
| 653 | player[queue][(i + 1) * 4 + 3] = player[queue][(i + 1) * 4 + 1]; |
| 654 | player[queue][(i + 1) * 4 + 1] = temp; |
| 655 | } |
| 656 | /* 1423 */ |
| 657 | else if (level[i * 4] >= level[i * 4 + 3] && level[i * 4 + 3] >= level[i * 4 + 1] && level[i * 4 + 1] >= level[i * 4 + 2]) { |
| 658 | temp = player[queue][(i + 1) * 4]; |
| 659 | player[queue][(i + 1) * 4] = player[queue][(i + 1) * 4 + 3]; |
| 660 | player[queue][(i + 1) * 4 + 3] = player[queue][(i + 1) * 4 + 2]; |
| 661 | player[queue][(i + 1) * 4 + 2] = temp; |
| 662 | } |
| 663 | /* 1432 */ |
| 664 | else if (level[i * 4] >= level[i * 4 + 3] && level[i * 4 + 3] >= level[i * 4 + 2] && level[i * 4 + 2] >= level[i * 4 + 1]) { |
| 665 | temp = player[queue][(i + 1) * 4]; |
| 666 | player[queue][(i + 1) * 4] = player[queue][(i + 1) * 4 + 3]; |
| 667 | player[queue][(i + 1) * 4 + 3] = player[queue][(i + 1) * 4 + 2]; |
| 668 | player[queue][(i + 1) * 4 + 2] = player[queue][(i + 1) * 4 + 1]; |
| 669 | player[queue][(i + 1) * 4 + 1] = temp; |
| 670 | } |
| 671 | /* 2134 */ |
| 672 | else if (level[i * 4 + 1] >= level[i * 4] && level[i * 4] >= level[i * 4 + 2] && level[i * 4 + 2] >= level[i * 4 + 3]) { |
| 673 | temp = player[queue][(i + 1) * 4]; |
| 674 | player[queue][(i + 1) * 4] = player[queue][(i + 1) * 4 + 2]; |
| 675 | player[queue][(i + 1) * 4 + 2] = player[queue][(i + 1) * 4 + 1]; |
| 676 | player[queue][(i + 1) * 4 + 1] = player[queue][(i + 1) * 4 + 3]; |
| 677 | player[queue][(i + 1) * 4 + 3] = temp; |
| 678 | } |
| 679 | /* 2143 */ |
| 680 | else if (level[i * 4 + 1] >= level[i * 4] && level[i * 4] >= level[i * 4 + 3] && level[i * 4 + 3] >= level[i * 4 + 2]) { |
| 681 | temp = player[queue][(i + 1) * 4]; |
| 682 | player[queue][(i + 1) * 4] = player[queue][(i + 1) * 4 + 2]; |
| 683 | player[queue][(i + 1) * 4 + 2] = temp; |
| 684 | temp = player[queue][(i + 1) * 4 + 1]; |
| 685 | player[queue][(i + 1) * 4 + 1] = player[queue][(i + 1) * 4 + 3]; |
| 686 | player[queue][(i + 1) * 4 + 3] = temp; |
| 687 | } |
| 688 | /* 2314 */ |
| 689 | else if (level[i * 4 + 1] >= level[i * 4 + 2] && level[i * 4 + 2] >= level[i * 4] && level[i * 4] >= level[i * 4 + 3]) { |
| 690 | temp = player[queue][(i + 1) * 4]; |
| 691 | player[queue][(i + 1) * 4] = player[queue][(i + 1) * 4 + 3]; |
| 692 | player[queue][(i + 1) * 4 + 3] = player[queue][(i + 1) * 4 + 1]; |
| 693 | player[queue][(i + 1) * 4 + 1] = temp; |
| 694 | } |
| 695 | /* 2341 */ |
| 696 | else if (level[i * 4 + 1] >= level[i * 4 + 2] && level[i * 4 + 2] >= level[i * 4 + 3] && level[i * 4 + 3] >= level[i * 4]) { |
| 697 | temp = player[queue][(i + 1) * 4 + 1]; |
| 698 | player[queue][(i + 1) * 4 + 1] = player[queue][(i + 1) * 4 + 2]; |
| 699 | player[queue][(i + 1) * 4 + 2] = player[queue][(i + 1) * 4 + 3]; |
| 700 | player[queue][(i + 1) * 4 + 3] = temp; |
| 701 | } |
| 702 | /* 2413 */ |
| 703 | else if (level[i * 4 + 1] >= level[i * 4 + 3] && level[i * 4 + 3] >= level[i * 4] && level[i * 4] >= level[i * 4 + 2]) { |
| 704 | temp = player[queue][(i + 1) * 4]; |
| 705 | player[queue][(i + 1) * 4] = player[queue][(i + 1) * 4 + 1]; |
| 706 | player[queue][(i + 1) * 4 + 1] = player[queue][(i + 1) * 4 + 3]; |
| 707 | player[queue][(i + 1) * 4 + 3] = player[queue][(i + 1) * 4 + 2]; |
| 708 | player[queue][(i + 1) * 4 + 2] = temp; |
| 709 | } |
| 710 | /* 2431 */ |
| 711 | else if (level[i * 4 + 1] >= level[i * 4 + 3] && level[i * 4 + 3] >= level[i * 4 + 2] && level[i * 4 + 2] >= level[i * 4]) { |
| 712 | temp = player[queue][(i + 1) * 4 + 1]; |
| 713 | player[queue][(i + 1) * 4 + 1] = player[queue][(i + 1) * 4 + 3]; |
| 714 | player[queue][(i + 1) * 4 + 3] = player[queue][(i + 1) * 4 + 2]; |
| 715 | player[queue][(i + 1) * 4 + 2] = temp; |
| 716 | } |
| 717 | /* 3124 */ |
| 718 | else if (level[i * 4 + 2] >= level[i * 4] && level[i * 4] >= level[i * 4 + 1] && level[i * 4 + 1] >= level[i * 4 + 3]) { |
| 719 | temp = player[queue][(i + 1) * 4]; |
| 720 | player[queue][(i + 1) * 4] = player[queue][(i + 1) * 4 + 2]; |
| 721 | player[queue][(i + 1) * 4 + 2] = player[queue][(i + 1) * 4 + 3]; |
| 722 | player[queue][(i + 1) * 4 + 3] = temp; |
| 723 | } |
| 724 | /* 3142 */ |
| 725 | else if (level[i * 4 + 2] >= level[i * 4] && level[i * 4] >= level[i * 4 + 3] && level[i * 4 + 3] >= level[i * 4 + 1]) { |
| 726 | temp = player[queue][(i + 1) * 4]; |
| 727 | player[queue][(i + 1) * 4] = player[queue][(i + 1) * 4 + 2]; |
| 728 | player[queue][(i + 1) * 4 + 2] = player[queue][(i + 1) * 4 + 3]; |
| 729 | player[queue][(i + 1) * 4 + 3] = player[queue][(i + 1) * 4 + 1]; |
| 730 | player[queue][(i + 1) * 4 + 1] = temp; |
| 731 | } |
| 732 | /* 3214 */ |
| 733 | else if (level[i * 4 + 2] >= level[i * 4 + 1] && level[i * 4 + 1] >= level[i * 4] && level[i * 4] >= level[i * 4 + 3]) { |
| 734 | temp = player[queue][(i + 1) * 4]; |
| 735 | player[queue][(i + 1) * 4] = player[queue][(i + 1) * 4 + 1]; |
| 736 | player[queue][(i + 1) * 4 + 1] = player[queue][(i + 1) * 4 + 2]; |
| 737 | player[queue][(i + 1) * 4 + 2] = player[queue][(i + 1) * 4 + 3]; |
| 738 | player[queue][(i + 1) * 4 + 3] = temp; |
| 739 | } |
| 740 | /* 3241 */ |
| 741 | else if (level[i * 4 + 2] >= level[i * 4 + 1] && level[i * 4 + 1] >= level[i * 4 + 3] && level[i * 4 + 3] >= level[i * 4]) { |
| 742 | temp = player[queue][(i + 1) * 4 + 1]; |
| 743 | player[queue][(i + 1) * 4 + 1] = player[queue][(i + 1) * 4 + 2]; |
| 744 | player[queue][(i + 1) * 4 + 2] = player[queue][(i + 1) * 4 + 3]; |
| 745 | player[queue][(i + 1) * 4 + 3] = temp; |
| 746 | } |
| 747 | /* 3412 */ |
| 748 | else if (level[i * 4 + 2] >= level[i * 4 + 3] && level[i * 4 + 3] >= level[i * 4] && level[i * 4] >= level[i * 4 + 1]) { |
| 749 | temp = player[queue][(i + 1) * 4]; |
| 750 | player[queue][(i + 1) * 4] = player[queue][(i + 1) * 4 + 1]; |
| 751 | player[queue][(i + 1) * 4 + 1] = temp; |
| 752 | temp = player[queue][(i + 1) * 4 + 2]; |
| 753 | player[queue][(i + 1) * 4 + 2] = player[queue][(i + 1) * 4 + 3]; |
| 754 | player[queue][(i + 1) * 4 + 3] = temp; |
| 755 | } |
| 756 | /* 3421 */ |
| 757 | else if (level[i * 4 + 2] >= level[i * 4 + 3] && level[i * 4 + 3] >= level[i * 4 + 1] && level[i * 4 + 1] >= level[i * 4]) { |
| 758 | temp = player[queue][(i + 1) * 4 + 2]; |
| 759 | player[queue][(i + 1) * 4 + 2] = player[queue][(i + 1) * 4 + 3]; |
| 760 | player[queue][(i + 1) * 4 + 3] = temp; |
| 761 | } |
| 762 | /* 4123 */ |
| 763 | else if (level[i * 4 + 3] >= level[i * 4] && level[i * 4] >= level[i * 4 + 1] && level[i * 4 + 1] >= level[i * 4 + 2]) { |
| 764 | temp = player[queue][(i + 1) * 4]; |
| 765 | player[queue][(i + 1) * 4] = player[queue][(i + 1) * 4 + 2]; |
| 766 | player[queue][(i + 1) * 4 + 2] = temp; |
| 767 | } |
| 768 | /* 4132 */ |
| 769 | else if (level[i * 4 + 3] >= level[i * 4] && level[i * 4] >= level[i * 4 + 2] && level[i * 4 + 2] >= level[i * 4 + 1]) { |
| 770 | temp = player[queue][(i + 1) * 4]; |
| 771 | player[queue][(i + 1) * 4] = player[queue][(i + 1) * 4 + 2]; |
| 772 | player[queue][(i + 1) * 4 + 2] = player[queue][(i + 1) * 4 + 1]; |
| 773 | player[queue][(i + 1) * 4 + 1] = temp; |
| 774 | } |
| 775 | /* 4213 */ |
| 776 | else if (level[i * 4 + 3] >= level[i * 4 + 1] && level[i * 4 + 1] >= level[i * 4] && level[i * 4] >= level[i * 4 + 2]) { |
| 777 | temp = player[queue][(i + 1) * 4]; |
| 778 | player[queue][(i + 1) * 4] = player[queue][(i + 1) * 4 + 1]; |
| 779 | player[queue][(i + 1) * 4 + 1] = player[queue][(i + 1) * 4 + 2]; |
| 780 | player[queue][(i + 1) * 4 + 2] = temp; |
| 781 | } |
| 782 | /* 4231 */ |
| 783 | else if (level[i * 4 + 3] >= level[i * 4 + 1] && level[i * 4 + 1] >= level[i * 4 + 2] && level[i * 4 + 2] >= level[i * 4]) { |
| 784 | temp = player[queue][(i + 1) * 4 + 1]; |
| 785 | player[queue][(i + 1) * 4 + 1] = player[queue][(i + 1) * 4 + 2]; |
| 786 | player[queue][(i + 1) * 4 + 2] = temp; |
| 787 | } |
| 788 | /* 4312 */ |
| 789 | else if (level[i * 4 + 3] >= level[i * 4 + 2] && level[i * 4 + 2] >= level[i * 4] && level[i * 4] >= level[i * 4 + 1]) { |
| 790 | temp = player[queue][(i + 1) * 4]; |
| 791 | player[queue][(i + 1) * 4] = player[queue][(i + 1) * 4 + 1]; |
| 792 | player[queue][(i + 1) * 4 + 1] = temp; |
| 793 | } |
| 794 | /* 4321 */ |
| 795 | else if (level[i * 4 + 3] >= level[i * 4 + 2] && level[i * 4 + 2] >= level[i * 4 + 1] && level[i * 4 + 1] >= level[i * 4]) { |
| 796 | ; /* nothing to do */ |
| 797 | } |
| 798 | } /* end 4 teams */ |
| 799 | } /* end ppt loop */ |
| 800 | } /* end "if" statement */ |
| 801 | return 0; |
| 802 | } |
| 803 | |
| 804 | static int _anongame_match(t_connection * c, int queue) |
| 805 | { |
| 806 | int level = _anongame_level_by_queue(c, queue); |
| 807 | int delta = 0; |
| 808 | int i; |
| 809 | t_matchdata *md; |
| 810 | t_elem *curr; |
| 811 | int diff; |
| 812 | t_anongame *a = conn_get_anongame(c); |
| 813 | std::uint32_t cur_prefs = a->map_prefs; |
| 814 | t_connection *inv_c[ANONGAME_MAX_TEAMS]; |
| 815 | int maxlevel, minlevel; |
| 816 | int teams = 0; |
| 817 | players[queue] = 0; |
| 818 | |
| 819 | eventlog(eventlog_level_trace, __FUNCTION__, "[{}] matching started for level {} player in queue {}", conn_get_socket(c), level, queue); |
| 820 | |
| 821 | diff = war3_get_maxleveldiff(); |
| 822 | maxlevel = level + diff; |
| 823 | minlevel = (level - diff < 0) ? 0 : level - diff; |
| 824 | |
| 825 | while (abs(delta) < (diff + 1)) { |
| 826 | if ((level + delta <= maxlevel) && (level + delta >= minlevel)) { |
| 827 | eventlog(eventlog_level_trace, __FUNCTION__, "Traversing level {} players", level + delta); |
| 828 | |
| 829 | LIST_TRAVERSE(matchlists[queue][level + delta], curr) { |
| 830 | md = (t_matchdata*)elem_get_data(curr); |
| 831 | if (md->versiontag |
| 832 | && conn_get_versioncheck(c) |
| 833 | && !std::strcmp(md->versiontag, conn_get_versioncheck(c)->get_version_tag().c_str()) |
| 834 | && (cur_prefs & md->map_prefs)) |
| 835 | { |
| 836 | /* set maxlevel and minlevel to keep all players within 6 levels */ |
| 837 | maxlevel = (level + delta + diff < maxlevel) ? level + delta + diff : maxlevel; |
| 838 | minlevel = (level + delta - diff > minlevel) ? level + delta - diff : minlevel; |
| 839 | cur_prefs &= md->map_prefs; |
| 840 | |
| 841 | /* AT match */ |
| 842 | if (anongame_arranged(queue)) { |
| 843 | |
| 844 | /* set the inv_c for unqueueing later */ |
| 845 | inv_c[teams] = md->c; |
| 846 | |
| 847 | a = conn_get_anongame(md->c); |
| 848 | |
| 849 | /* add all the players on the team to player[][] */ |
| 850 | int totalplayers = _anongame_totalplayers(queue); |
| 851 | int totalteams = _anongame_totalteams(queue); |
| 852 | int totalcount = (totalteams > 0) ? totalplayers / totalteams : 0; |
| 853 | for (i = 0; i < totalcount; i++) { |
| 854 | player[queue][teams + i * _anongame_totalteams(queue)] = a->tc[i]; |
| 855 | players[queue]++; |
| 856 | } |
| 857 | teams++; |
| 858 | |
| 859 | /* check for enough players */ |
| 860 | if (players[queue] == _anongame_totalplayers(queue)) { |
| 861 | |
| 862 | /* unqueue just the single team entry */ |
| 863 | for (i = 0; i < teams; i++) |
| 864 | anongame_unqueue(inv_c[i], queue); |
| 865 | |
| 866 | mapname = _get_map_from_prefs(queue, cur_prefs, conn_get_clienttag(c)); |
| 867 | return 0; |
| 868 | } |
| 869 | |
| 870 | /* PG match */ |
| 871 | } |
| 872 | else { |
| 873 | player[queue][players[queue]++] = md->c; |
| 874 | |
| 875 | if (players[queue] == _anongame_totalplayers(queue)) { |
| 876 | /* first sort queue by level */ |
| 877 | std::qsort(player[queue], players[queue], sizeof(t_connection *), _anongame_compare_level); |
| 878 | /* next call reodering function */ |
| 879 | _anongame_order_queue(queue); |
| 880 | /* unqueue players */ |
| 881 | for (i = 0; i < players[queue]; i++) |
| 882 | anongame_unqueue(player[queue][i], queue); |
| 883 | |
| 884 | mapname = _get_map_from_prefs(queue, cur_prefs, conn_get_clienttag(c)); |
| 885 | return 0; |
| 886 | } |
| 887 | } |
| 888 | } |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | if (delta <= 0 || level - delta < 0) |
| 893 | delta = abs(delta) + 1; |
| 894 | else |
| 895 | delta = -delta; |
| 896 | |
| 897 | if (level + delta > MAX_LEVEL) |
| 898 | delta = -delta; |
| 899 | |
| 900 | if (level + delta < 0) |
| 901 | break; /* cant really happen */ |
| 902 | |
| 903 | } |
| 904 | eventlog(eventlog_level_trace, __FUNCTION__, "[{}] Matching finished, not enough players (found {})", conn_get_socket(c), players[queue]); |
| 905 | mapname = NULL; |
| 906 | return 0; |
| 907 | } |
| 908 | |
| 909 | static int w3routeip = -1; /* changed by dizzy to show the w3routeshow addr if available */ |
| 910 | static unsigned short w3routeport = BNETD_W3ROUTE_PORT; |
| 911 | |
| 912 | static int _anongame_search_found(int queue) |
| 913 | { |
| 914 | t_packet *rpacket; |
| 915 | t_anongame *a; |
| 916 | int i, j; |
| 917 | t_saf_pt2 *pt2; |
| 918 | |
| 919 | /* FIXME: maybe periodically lookup w3routeaddr to support dynamic ips? |
| 920 | * (or should dns lookup be even quick enough to do it everytime?) |
| 921 | */ |
| 922 | |
| 923 | if (w3routeip == -1) { |
| 924 | t_addr *routeraddr; |
| 925 | |
| 926 | routeraddr = addr_create_str(prefs_get_w3route_addr(), 0, BNETD_W3ROUTE_PORT); |
| 927 | |
| 928 | if (!routeraddr) { |
| 929 | eventlog(eventlog_level_error, __FUNCTION__, "error getting w3route_addr"); |
| 930 | return -1; |
| 931 | } |
| 932 | |
| 933 | w3routeip = addr_get_ip(routeraddr); |
| 934 | w3routeport = addr_get_port(routeraddr); |
| 935 | addr_destroy(routeraddr); |
| 936 | } |
| 937 | |
| 938 | t_anongameinfo* const info = anongameinfo_create(_anongame_totalplayers(queue)); |
| 939 | if (!info) |
| 940 | { |
| 941 | eventlog(eventlog_level_error, __FUNCTION__, "anongameinfo_create failed"); |
| 942 | return -1; |
| 943 | } |
| 944 | |
| 945 | /* create data to be appended to end of packet */ |
| 946 | pt2 = (t_saf_pt2*)xmalloc(sizeof(t_saf_pt2)); |
| 947 | bn_int_set(&pt2->unknown1, 0xFFFFFFFF); |
| 948 | bn_int_set(&pt2->anongame_string, _anongame_get_gametype_tab(queue)); |
| 949 | bn_byte_set(&pt2->totalplayers, _anongame_totalplayers(queue)); |
| 950 | bn_byte_set(&pt2->totalteams, _anongame_totalteams(queue)); /* 1v1 & sffa are set to zero in _anongame_totalteams() */ |
| 951 | bn_short_set(&pt2->unknown2, 0); |
| 952 | bn_byte_set(&pt2->visibility, 2); /* visibility. 0x01 - dark 0x02 - default */ |
| 953 | bn_byte_set(&pt2->unknown3, 2); |
| 954 | |
| 955 | /* send found packet to each of the players */ |
| 956 | for (i = 0; i < players[queue]; i++) |
| 957 | { |
| 958 | if (!(a = conn_get_anongame(player[queue][i]))) |
| 959 | { |
| 960 | eventlog(eventlog_level_error, __FUNCTION__, "no anongame struct for queued player"); |
| 961 | xfree(pt2); |
| 962 | anongameinfo_destroy(info); |
| 963 | return -1; |
| 964 | } |
| 965 | |
| 966 | a->info = info; |
| 967 | a->playernum = i + 1; |
| 968 | |
| 969 | for (j = 0; j < players[queue]; j++) { |
| 970 | a->info->player[j] = player[queue][j]; |
| 971 | a->info->account[j] = conn_get_account(player[queue][j]); |
| 972 | } |
| 973 | |
| 974 | if (!(rpacket = packet_create(packet_class_bnet))) { |
| 975 | xfree(pt2); |
| 976 | anongameinfo_destroy(info); |
| 977 | return -1; |
| 978 | } |
| 979 | |
| 980 | packet_set_size(rpacket, sizeof(t_server_anongame_found)); |
| 981 | packet_set_type(rpacket, SERVER_ANONGAME_FOUND); |
| 982 | bn_byte_set(&rpacket->u.server_anongame_found.option, 1); |
| 983 | bn_int_set(&rpacket->u.server_anongame_found.count, a->count); |
| 984 | bn_int_set(&rpacket->u.server_anongame_found.unknown1, 0); |
| 985 | { /* trans support */ |
| 986 | unsigned int w3ip = w3routeip; |
| 987 | unsigned short w3port = w3routeport; |
| 988 | |
| 989 | trans_net(conn_get_addr(player[queue][i]), &w3ip, &w3port); |
| 990 | |
| 991 | /* if ip to send is 0.0.0.0 (which will not work anyway) try |
| 992 | * to guess the reachable IP of pvpgn by using the local |
| 993 | * endpoing address of the bnet class connection */ |
| 994 | if (!w3ip) |
| 995 | w3ip = conn_get_real_local_addr(player[queue][i]); |
| 996 | |
| 997 | bn_int_nset(&rpacket->u.server_anongame_found.ip, w3ip); |
| 998 | bn_short_set(&rpacket->u.server_anongame_found.port, w3port); |
| 999 | } |
| 1000 | bn_byte_set(&rpacket->u.server_anongame_found.unknown2, i + 1); |
| 1001 | bn_byte_set(&rpacket->u.server_anongame_found.unknown3, queue); |
| 1002 | bn_short_set(&rpacket->u.server_anongame_found.unknown4, 0); |
| 1003 | bn_int_set(&rpacket->u.server_anongame_found.id, 0xdeadbeef); |
| 1004 | bn_byte_set(&rpacket->u.server_anongame_found.unknown5, 6); |
| 1005 | bn_byte_set(&rpacket->u.server_anongame_found.type, a->type); |
| 1006 | bn_byte_set(&rpacket->u.server_anongame_found.gametype, a->gametype); |
| 1007 | packet_append_string(rpacket, mapname); |
| 1008 | packet_append_data(rpacket, pt2, sizeof(t_saf_pt2)); |
| 1009 | conn_push_outqueue(player[queue][i], rpacket); |
| 1010 | packet_del_ref(rpacket); |
| 1011 | } |
| 1012 | |
| 1013 | /* clear queue */ |
| 1014 | players[queue] = 0; |
| 1015 | xfree(pt2); |
| 1016 | |
| 1017 | return 0; |
| 1018 | } |
| 1019 | |
| 1020 | /**********************************************************************************/ |
| 1021 | /* external functions */ |
| 1022 | /**********************************************************************************/ |
| 1023 | extern int anongame_matchlists_create() |
| 1024 | { |
| 1025 | int i, j; |
| 1026 | |
| 1027 | for (i = 0; i < ANONGAME_TYPES; i++) { |
| 1028 | for (j = 0; j < MAX_LEVEL; j++) { |
| 1029 | matchlists[i][j] = NULL; |
| 1030 | } |
| 1031 | } |
| 1032 | return 0; |
| 1033 | } |
| 1034 | |
| 1035 | extern int anongame_matchlists_destroy() |
| 1036 | { |
| 1037 | int i, j; |
| 1038 | for (i = 0; i < ANONGAME_TYPES; i++) { |
| 1039 | for (j = 0; j < MAX_LEVEL; j++) { |
| 1040 | if (matchlists[i][j]) { |
| 1041 | list_destroy(matchlists[i][j]); |
| 1042 | } |
| 1043 | } |
| 1044 | } |
| 1045 | return 0; |
| 1046 | } |
| 1047 | |
| 1048 | /**********/ |
| 1049 | extern int handle_anongame_search(t_connection * c, t_packet const *packet) |
| 1050 | { |
| 1051 | return _handle_anongame_search(c, packet); |
| 1052 | } |
| 1053 | |
| 1054 | extern int anongame_unqueue(t_connection * c, int queue) |
| 1055 | { |
| 1056 | int i; |
| 1057 | t_elem *curr; |
| 1058 | t_matchdata *md; |
| 1059 | |
| 1060 | if (queue < 0) { |
| 1061 | eventlog(eventlog_level_error, __FUNCTION__, "got negative queue id ({})", queue); |
| 1062 | return -1; |
| 1063 | } |
| 1064 | |
| 1065 | if (queue >= ANONGAME_TYPES) { |
| 1066 | eventlog(eventlog_level_error, __FUNCTION__, "unknown queue: {}", queue); |
| 1067 | return -1; |
| 1068 | } |
| 1069 | |
| 1070 | if (conn_get_anongame_search_starttime(c) != ((std::time_t) 0)) { |
| 1071 | average_anongame_search_time *= anongame_search_count; |
| 1072 | average_anongame_search_time += (long)std::difftime(std::time(NULL), conn_get_anongame_search_starttime(c)); |
| 1073 | anongame_search_count++; |
| 1074 | average_anongame_search_time /= anongame_search_count; |
| 1075 | if (anongame_search_count > 20000) |
| 1076 | anongame_search_count = anongame_search_count / 2; /* to prevent an overflow of the average time */ |
| 1077 | conn_set_anongame_search_starttime(c, ((std::time_t) 0)); |
| 1078 | } |
| 1079 | |
| 1080 | for (i = 0; i < MAX_LEVEL; i++) { |
| 1081 | if (matchlists[queue][i] == NULL) |
| 1082 | continue; |
| 1083 | |
| 1084 | LIST_TRAVERSE(matchlists[queue][i], curr) { |
| 1085 | md = (t_matchdata*)elem_get_data(curr); |
| 1086 | if (md->c == c) { |
| 1087 | eventlog(eventlog_level_trace, __FUNCTION__, "unqueued player [{}] level {}", conn_get_socket(c), i); |
| 1088 | list_remove_elem(matchlists[queue][i], &curr); |
| 1089 | xfree(md); |
| 1090 | return 0; |
| 1091 | } |
| 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | /* Output error to std::log for PG queues, AT players are queued with single |
| 1096 | * entry. Because anongame_unqueue() is called for each player, only the first |
| 1097 | * time called will the team be removed, the rest are therefore not an error. |
| 1098 | * [Omega] |
| 1099 | */ |
| 1100 | if (anongame_arranged(queue) == 0) { |
| 1101 | eventlog(eventlog_level_trace, __FUNCTION__, "[{}] player not found in \"{}\" queue", conn_get_socket(c), _anongame_queue_to_string(queue)); |
| 1102 | return -1; |
| 1103 | } |
| 1104 | |
| 1105 | return 0; |
| 1106 | } |
| 1107 | |
| 1108 | /**********/ |
| 1109 | extern char anongame_arranged(int queue) |
| 1110 | { |
| 1111 | switch (queue) { |
| 1112 | case ANONGAME_TYPE_AT_2V2: |
| 1113 | case ANONGAME_TYPE_AT_3V3: |
| 1114 | case ANONGAME_TYPE_AT_4V4: |
| 1115 | case ANONGAME_TYPE_AT_2V2V2: |
| 1116 | return 1; |
| 1117 | case ANONGAME_TYPE_TY: |
| 1118 | return tournament_is_arranged(); |
| 1119 | default: |
| 1120 | return 0; |
| 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | extern int anongame_evaluate_results(t_anongame * anongame) |
| 1125 | { |
| 1126 | int i, j, number; |
| 1127 | int wins[ANONGAME_MAX_GAMECOUNT]; |
| 1128 | int losses[ANONGAME_MAX_GAMECOUNT]; |
| 1129 | int result; |
| 1130 | t_anongame_gameresult *results; |
| 1131 | t_anongameinfo *anoninfo = anongame->info; |
| 1132 | |
| 1133 | for (i = 0; i < ANONGAME_MAX_GAMECOUNT; i++) { |
| 1134 | wins[i] = 0; |
| 1135 | losses[i] = 0; |
| 1136 | } |
| 1137 | |
| 1138 | for (i = 0; i < anongame_get_totalplayers(anongame); i++) { |
| 1139 | if ((results = anoninfo->results[i])) { |
| 1140 | for (j = 0; j < gameresult_get_number_of_results(results); j++) { |
| 1141 | number = gameresult_get_player_number(results, j) - 1; |
| 1142 | result = gameresult_get_player_result(results, j); |
| 1143 | |
| 1144 | if ((result == W3_GAMERESULT_WIN)) |
| 1145 | wins[number]++; |
| 1146 | if ((result == W3_GAMERESULT_LOSS)) |
| 1147 | losses[number]++; |
| 1148 | } |
| 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | for (i = 0; i < anongame_get_totalplayers(anongame); i++) { |
| 1153 | if ((wins[i] > losses[i])) { |
| 1154 | if ((anoninfo->result[i] != W3_GAMERESULT_WIN)) { |
| 1155 | eventlog(eventlog_level_trace, __FUNCTION__, "player {} reported DISC/LOSS for self, but others agree on WIN", i + 1); |
| 1156 | anoninfo->result[i] = W3_GAMERESULT_WIN; |
| 1157 | } |
| 1158 | } |
| 1159 | else { |
| 1160 | if ((anoninfo->result[i] != W3_GAMERESULT_LOSS)) { |
| 1161 | eventlog(eventlog_level_trace, __FUNCTION__, "player {} reported DISC/WIN for self, but others agree on LOSS", i + 1); |
| 1162 | anoninfo->result[i] = W3_GAMERESULT_LOSS; |
| 1163 | } |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | return 0; |
| 1168 | |
| 1169 | } |
| 1170 | |
| 1171 | extern int anongame_stats(t_connection * c) |
| 1172 | { |
| 1173 | int i; |
| 1174 | int wins = 0, losses = 0, discs = 0; |
| 1175 | t_connection *gamec = conn_get_routeconn(c); |
| 1176 | t_anongame *a = conn_get_anongame(gamec); |
| 1177 | int tp = anongame_get_totalplayers(a); |
| 1178 | int oppon_level[ANONGAME_MAX_GAMECOUNT]; |
| 1179 | std::uint8_t gametype = a->queue; |
| 1180 | std::uint8_t plnum = a->playernum; |
| 1181 | t_clienttag ct = conn_get_clienttag(c); |
| 1182 | int tt = _anongame_totalteams(gametype); |
| 1183 | |
| 1184 | /* do nothing till all other players have w3route conn closed */ |
| 1185 | for (i = 0; i < tp; i++) |
| 1186 | if (i + 1 != plnum && a->info->player[i]) |
| 1187 | if (conn_get_routeconn(a->info->player[i])) |
| 1188 | return 0; |
| 1189 | |
| 1190 | anongame_evaluate_results(a); |
| 1191 | |
| 1192 | /* count wins, losses, discs */ |
| 1193 | for (i = 0; i < tp; i++) { |
| 1194 | if (a->info->result[i] == W3_GAMERESULT_WIN) |
| 1195 | wins++; |
| 1196 | else if (a->info->result[i] == W3_GAMERESULT_LOSS) |
| 1197 | losses++; |
| 1198 | else |
| 1199 | discs++; |
| 1200 | } |
| 1201 | |
| 1202 | /* do some sanity checking (hack prevention) */ |
| 1203 | switch (gametype) { |
| 1204 | case ANONGAME_TYPE_SMALL_FFA: |
| 1205 | if (wins != 1) { |
| 1206 | eventlog(eventlog_level_info, __FUNCTION__, "bogus game result: wins != 1 in small ffa game"); |
| 1207 | return -1; |
| 1208 | } |
| 1209 | break; |
| 1210 | case ANONGAME_TYPE_TEAM_FFA: |
| 1211 | if (!discs && wins != 2) { |
| 1212 | eventlog(eventlog_level_info, __FUNCTION__, "bogus game result: wins != 2 in team ffa game"); |
| 1213 | return -1; |
| 1214 | } |
| 1215 | break; |
| 1216 | default: |
| 1217 | if (!discs && wins > losses) { |
| 1218 | eventlog(eventlog_level_info, __FUNCTION__, "bogus game result: wins > losses"); |
| 1219 | return -1; |
| 1220 | } |
| 1221 | break; |
| 1222 | } |
| 1223 | |
| 1224 | /* prevent users from getting loss if server is shutdown (does not prevent errors from crash) - [Omega] */ |
| 1225 | /* also discard games with no winners at all (i.e. games where game host disc'ed and so all players do) */ |
| 1226 | if (!wins) |
| 1227 | return -1; |
| 1228 | |
| 1229 | /* according to zap, order of players in anongame is: |
| 1230 | * for PG: t1_p1, t2_p1, t1_p2, t2_p2, ... |
| 1231 | * for AT: t1_p1, t1_p2, ..., t2_p1, t2_p2, ... |
| 1232 | * |
| 1233 | * (Not True.. follows same order as PG) |
| 1234 | * 4v4 = t1_p1, t2_p1, t1_p2, t2_p2, t1_p3, t2_p3, t1_p4, t2_p4 |
| 1235 | * 3v3v3 = t1_p1, t2_p1, t3_p1, t1_p2, t2_p2, t3_p2, t1_p3, t2_p3, t3_p3 |
| 1236 | * 2v2v2v2 = t1_p1, t2_p1, t3_p1, t4_p1, t1_p2, t2_p2, t3_p2, t4_p2 |
| 1237 | */ |
| 1238 | |
| 1239 | /* opponent level calculation has to be done here, because later on, the level of other players |
| 1240 | * may already be modified |
| 1241 | */ |
| 1242 | for (i = 0; i < tp; i++) { |
| 1243 | int j, k, l; |
| 1244 | t_account *oacc; |
| 1245 | oppon_level[i] = 0; |
| 1246 | switch (gametype) { |
| 1247 | case ANONGAME_TYPE_TY: |
| 1248 | /* FIXME-TY: ADD TOURNAMENT STATS RECORDING (this part not required?) */ |
| 1249 | break; |
| 1250 | case ANONGAME_TYPE_1V1: |
| 1251 | oppon_level[i] = account_get_ladder_level(a->info->account[(i + 1) % tp], ct, ladder_id_solo); |
| 1252 | break; |
| 1253 | case ANONGAME_TYPE_SMALL_FFA: |
| 1254 | /* oppon_level = average level of all other players */ |
| 1255 | for (j = 0; j < tp; j++) |
| 1256 | if (i != j) |
| 1257 | oppon_level[i] += account_get_ladder_level(a->info->account[j], ct, ladder_id_ffa); |
| 1258 | oppon_level[i] /= (tp - 1); |
| 1259 | break; |
| 1260 | case ANONGAME_TYPE_AT_2V2: |
| 1261 | case ANONGAME_TYPE_AT_3V3: |
| 1262 | case ANONGAME_TYPE_AT_4V4: |
| 1263 | oacc = a->info->account[(i + 1) % tp]; |
| 1264 | oppon_level[i] = team_get_level(account_find_team_by_teamid(oacc, account_get_currentatteam(oacc))); |
| 1265 | break; |
| 1266 | case ANONGAME_TYPE_AT_2V2V2: |
| 1267 | oacc = a->info->account[(i + 1) % tp]; |
| 1268 | oppon_level[i] = team_get_level(account_find_team_by_teamid(oacc, account_get_currentatteam(oacc))); |
| 1269 | oacc = a->info->account[(i + 2) % tp]; |
| 1270 | oppon_level[i] = team_get_level(account_find_team_by_teamid(oacc, account_get_currentatteam(oacc))); |
| 1271 | oppon_level[i] /= 2; |
| 1272 | break; |
| 1273 | default: |
| 1274 | /* oppon_level = average level of all opponents |
| 1275 | * this should work for all PG team games |
| 1276 | * [Omega] */ |
| 1277 | k = i + 1; |
| 1278 | int _count = (tt > 0) ? tp / tt : 0; |
| 1279 | for (j = 0; j < _count; j++) { |
| 1280 | for (l = 0; l < (tt - 1); l++) { |
| 1281 | oppon_level[i] += account_get_ladder_level(a->info->account[k % tp], ct, ladder_id_team); |
| 1282 | k++; |
| 1283 | } |
| 1284 | k++; |
| 1285 | } |
| 1286 | oppon_level[i] /= (_count * (tt - 1)); |
| 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | for (i = 0; i < tp; i++) { |
| 1291 | t_account *acc; |
| 1292 | t_team *team; |
| 1293 | unsigned int currteam; |
| 1294 | int result = a->info->result[i]; |
| 1295 | |
| 1296 | if (result == -1) |
| 1297 | result = W3_GAMERESULT_LOSS; |
| 1298 | |
| 1299 | acc = a->info->account[i]; |
| 1300 | |
| 1301 | switch (gametype) { |
| 1302 | case ANONGAME_TYPE_TY: |
| 1303 | if (result == W3_GAMERESULT_WIN) |
| 1304 | tournament_add_stat(acc, 1); |
| 1305 | if (result == W3_GAMERESULT_LOSS) |
| 1306 | tournament_add_stat(acc, 2); |
| 1307 | /* FIXME-TY: how to do ties? */ |
| 1308 | break; |
| 1309 | case ANONGAME_TYPE_AT_2V2: |
| 1310 | case ANONGAME_TYPE_AT_3V3: |
| 1311 | case ANONGAME_TYPE_AT_4V4: |
| 1312 | case ANONGAME_TYPE_AT_2V2V2: |
| 1313 | |
| 1314 | if ((currteam = account_get_currentatteam(acc))) { |
| 1315 | team = account_find_team_by_teamid(acc, currteam); |
| 1316 | team_set_saveladderstats(team, gametype, result, oppon_level[i], ct); |
| 1317 | } |
| 1318 | |
| 1319 | break; |
| 1320 | case ANONGAME_TYPE_1V1: |
| 1321 | case ANONGAME_TYPE_2V2: |
| 1322 | case ANONGAME_TYPE_3V3: |
| 1323 | case ANONGAME_TYPE_4V4: |
| 1324 | case ANONGAME_TYPE_SMALL_FFA: |
| 1325 | case ANONGAME_TYPE_5V5: |
| 1326 | case ANONGAME_TYPE_6V6: |
| 1327 | case ANONGAME_TYPE_2V2V2: |
| 1328 | case ANONGAME_TYPE_3V3V3: |
| 1329 | case ANONGAME_TYPE_4V4V4: |
| 1330 | case ANONGAME_TYPE_2V2V2V2: |
| 1331 | case ANONGAME_TYPE_3V3V3V3: |
| 1332 | if (result == W3_GAMERESULT_WIN) |
| 1333 | account_set_saveladderstats(acc, gametype, game_result_win, oppon_level[i], ct); |
| 1334 | if (result == W3_GAMERESULT_LOSS) |
| 1335 | account_set_saveladderstats(acc, gametype, game_result_loss, oppon_level[i], ct); |
| 1336 | break; |
| 1337 | default: |
| 1338 | break; |
| 1339 | } |
| 1340 | } |
| 1341 | /* aaron: now update war3 ladders */ |
| 1342 | ladders.update(); |
| 1343 | return 1; |
| 1344 | } |
| 1345 | |
| 1346 | /**********/ |
| 1347 | extern t_anongameinfo *anongameinfo_create(int totalplayers) |
| 1348 | { |
| 1349 | t_anongameinfo *temp; |
| 1350 | int i; |
| 1351 | |
| 1352 | temp = (t_anongameinfo*)xmalloc(sizeof(t_anongameinfo)); |
| 1353 | |
| 1354 | temp->totalplayers = temp->currentplayers = totalplayers; |
| 1355 | for (i = 0; i < ANONGAME_MAX_GAMECOUNT; i++) { |
| 1356 | temp->player[i] = NULL; |
| 1357 | temp->account[i] = NULL; |
| 1358 | temp->result[i] = -1; /* consider DISC default */ |
| 1359 | temp->results[i] = NULL; |
| 1360 | } |
| 1361 | |
| 1362 | return temp; |
| 1363 | } |
| 1364 | |
| 1365 | extern void anongameinfo_destroy(t_anongameinfo * i) |
| 1366 | { |
| 1367 | int j; |
| 1368 | |
| 1369 | if (!i) { |
| 1370 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL anongameinfo"); |
| 1371 | return; |
| 1372 | } |
| 1373 | for (j = 0; j < ANONGAME_MAX_GAMECOUNT; j++) |
| 1374 | if (i->results[j]) |
| 1375 | gameresult_destroy(i->results[j]); |
| 1376 | xfree(i); |
| 1377 | } |
| 1378 | |
| 1379 | /**********/ |
| 1380 | extern t_anongameinfo *anongame_get_info(t_anongame * a) |
| 1381 | { |
| 1382 | if (!a) { |
| 1383 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL anongame"); |
| 1384 | return NULL; |
| 1385 | } |
| 1386 | |
| 1387 | return a->info; |
| 1388 | } |
| 1389 | |
| 1390 | extern int anongame_get_currentplayers(t_anongame * a) |
| 1391 | { |
| 1392 | if (!a) { |
| 1393 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL anongame"); |
| 1394 | return 0; |
| 1395 | } |
| 1396 | if (!a->info) { |
| 1397 | eventlog(eventlog_level_error, __FUNCTION__, "NULL anongameinfo"); |
| 1398 | return 0; |
| 1399 | } |
| 1400 | |
| 1401 | return a->info->currentplayers; |
| 1402 | } |
| 1403 | |
| 1404 | extern int anongame_get_totalplayers(t_anongame * a) |
| 1405 | { |
| 1406 | if (!a) { |
| 1407 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL anongame"); |
| 1408 | return 0; |
| 1409 | } |
| 1410 | if (!a->info) { |
| 1411 | eventlog(eventlog_level_error, __FUNCTION__, "NULL anongameinfo"); |
| 1412 | return 0; |
| 1413 | } |
| 1414 | |
| 1415 | return a->info->totalplayers; |
| 1416 | } |
| 1417 | |
| 1418 | extern t_connection *anongame_get_player(t_anongame * a, int plnum) |
| 1419 | { |
| 1420 | if (!a) { |
| 1421 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL anongame"); |
| 1422 | return NULL; |
| 1423 | } |
| 1424 | if (!a->info) { |
| 1425 | eventlog(eventlog_level_error, __FUNCTION__, "NULL anongameinfo"); |
| 1426 | return NULL; |
| 1427 | } |
| 1428 | |
| 1429 | if (plnum < 0 || plnum > 7 || plnum >= a->info->totalplayers) { |
| 1430 | eventlog(eventlog_level_error, __FUNCTION__, "invalid plnum: {}", plnum); |
| 1431 | return NULL; |
| 1432 | } |
| 1433 | |
| 1434 | return a->info->player[plnum]; |
| 1435 | } |
| 1436 | |
| 1437 | extern int anongame_get_count(t_anongame * a) |
| 1438 | { |
| 1439 | if (!a) { |
| 1440 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL anongame"); |
| 1441 | return 0; |
| 1442 | } |
| 1443 | return a->count; |
| 1444 | } |
| 1445 | |
| 1446 | extern std::uint32_t anongame_get_id(t_anongame * a) |
| 1447 | { |
| 1448 | if (!a) { |
| 1449 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL anongame"); |
| 1450 | return 0; |
| 1451 | } |
| 1452 | return a->id; |
| 1453 | } |
| 1454 | |
| 1455 | extern t_connection *anongame_get_tc(t_anongame * a, int tpnumber) |
| 1456 | { |
| 1457 | if (!a) { |
| 1458 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL anongame"); |
| 1459 | return 0; |
| 1460 | } |
| 1461 | return a->tc[tpnumber]; |
| 1462 | } |
| 1463 | |
| 1464 | extern std::uint32_t anongame_get_race(t_anongame * a) |
| 1465 | { |
| 1466 | if (!a) { |
| 1467 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL anongame"); |
| 1468 | return 0; |
| 1469 | } |
| 1470 | return a->race; |
| 1471 | } |
| 1472 | |
| 1473 | extern std::uint32_t anongame_get_handle(t_anongame * a) |
| 1474 | { |
| 1475 | if (!a) { |
| 1476 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL anongame"); |
| 1477 | return 0; |
| 1478 | } |
| 1479 | return a->handle; |
| 1480 | } |
| 1481 | |
| 1482 | extern unsigned int anongame_get_addr(t_anongame * a) |
| 1483 | { |
| 1484 | if (!a) { |
| 1485 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL anongame"); |
| 1486 | return 0; |
| 1487 | } |
| 1488 | return a->addr; |
| 1489 | } |
| 1490 | |
| 1491 | extern char anongame_get_loaded(t_anongame * a) |
| 1492 | { |
| 1493 | if (!a) { |
| 1494 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL anongame"); |
| 1495 | return 0; |
| 1496 | } |
| 1497 | return a->loaded; |
| 1498 | } |
| 1499 | |
| 1500 | extern char anongame_get_joined(t_anongame * a) |
| 1501 | { |
| 1502 | if (!a) { |
| 1503 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL anongame"); |
| 1504 | return 0; |
| 1505 | } |
| 1506 | return a->joined; |
| 1507 | } |
| 1508 | |
| 1509 | extern std::uint8_t anongame_get_playernum(t_anongame * a) |
| 1510 | { |
| 1511 | if (!a) { |
| 1512 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL anongame"); |
| 1513 | return 0; |
| 1514 | } |
| 1515 | return a->playernum; |
| 1516 | } |
| 1517 | |
| 1518 | extern std::uint8_t anongame_get_queue(t_anongame * a) |
| 1519 | { |
| 1520 | if (!a) { |
| 1521 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL anongame"); |
| 1522 | return 0; |
| 1523 | } |
| 1524 | return a->queue; |
| 1525 | } |
| 1526 | |
| 1527 | /**********/ |
| 1528 | extern void anongame_set_result(t_anongame * a, int result) |
| 1529 | { |
| 1530 | if (!a) { |
| 1531 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL anongame"); |
| 1532 | return; |
| 1533 | } |
| 1534 | if (!a->info) { |
| 1535 | eventlog(eventlog_level_error, __FUNCTION__, "NULL anongameinfo"); |
| 1536 | return; |
| 1537 | } |
| 1538 | |
| 1539 | if (a->playernum < 1 || a->playernum > ANONGAME_MAX_GAMECOUNT) { |
| 1540 | eventlog(eventlog_level_error, __FUNCTION__, "invalid playernum: {}", a->playernum); |
| 1541 | return; |
| 1542 | } |
| 1543 | |
| 1544 | a->info->result[a->playernum - 1] = result; |
| 1545 | } |
| 1546 | |
| 1547 | extern void anongame_set_gameresults(t_anongame * a, t_anongame_gameresult * results) |
| 1548 | { |
| 1549 | if (!a) { |
| 1550 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL anongame"); |
| 1551 | return; |
| 1552 | } |
| 1553 | if (!a->info) { |
| 1554 | eventlog(eventlog_level_error, __FUNCTION__, "NULL anongameinfo"); |
| 1555 | return; |
| 1556 | } |
| 1557 | |
| 1558 | if (a->playernum < 1 || a->playernum > ANONGAME_MAX_GAMECOUNT) { |
| 1559 | eventlog(eventlog_level_error, __FUNCTION__, "invalid playernum: {}", a->playernum); |
| 1560 | return; |
| 1561 | } |
| 1562 | |
| 1563 | a->info->results[a->playernum - 1] = results; |
| 1564 | } |
| 1565 | |
| 1566 | extern void anongame_set_handle(t_anongame * a, std::uint32_t h) |
| 1567 | { |
| 1568 | if (!a) { |
| 1569 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL anongame"); |
| 1570 | return; |
| 1571 | } |
| 1572 | |
| 1573 | a->handle = h; |
| 1574 | } |
| 1575 | |
| 1576 | extern void anongame_set_addr(t_anongame * a, unsigned int addr) |
| 1577 | { |
| 1578 | if (!a) { |
| 1579 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL anongame"); |
| 1580 | return; |
| 1581 | } |
| 1582 | |
| 1583 | a->addr = addr; |
| 1584 | } |
| 1585 | |
| 1586 | extern void anongame_set_loaded(t_anongame * a, char loaded) |
| 1587 | { |
| 1588 | if (!a) { |
| 1589 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL anongame"); |
| 1590 | return; |
| 1591 | } |
| 1592 | |
| 1593 | a->loaded = loaded; |
| 1594 | } |
| 1595 | |
| 1596 | extern void anongame_set_joined(t_anongame * a, char joined) |
| 1597 | { |
| 1598 | if (!a) { |
| 1599 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL anongame"); |
| 1600 | return; |
| 1601 | } |
| 1602 | |
| 1603 | a->joined = joined; |
| 1604 | } |
| 1605 | |
| 1606 | /**********/ |
| 1607 | /* move to own .c/.h file for handling w3route connections */ |
| 1608 | extern int handle_w3route_packet(t_connection * c, t_packet const *const packet) |
| 1609 | { |
| 1610 | /* [smith] 20030427 fixed Big-Endian/Little-Endian conversion (Solaris bug) then |
| 1611 | * use packet_append_data for append platform dependent data types - like |
| 1612 | * "int", cos this code was broken for BE platforms. it's rewriten in platform |
| 1613 | * independent style whis usege bn_int and other bn_* like datatypes and |
| 1614 | * fuctions for wor with datatypes - bn_int_set(), what provide right |
| 1615 | * byteorder, not depended on LE/BE |
| 1616 | * fixed broken htonl() conversion for BE platforms - change it to |
| 1617 | * bn_int_nset(). i hope it's worked on intel too %) */ |
| 1618 | |
| 1619 | t_packet *rpacket; |
| 1620 | t_connection *gamec; |
| 1621 | char const *username; |
| 1622 | t_anongame *a = NULL; |
| 1623 | std::uint8_t gametype, plnum; |
| 1624 | int tp, i; |
| 1625 | |
| 1626 | if (!c) { |
| 1627 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] got NULL connection", conn_get_socket(c)); |
| 1628 | return -1; |
| 1629 | } |
| 1630 | if (!packet) { |
| 1631 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] got NULL packet", conn_get_socket(c)); |
| 1632 | return -1; |
| 1633 | } |
| 1634 | if (packet_get_class(packet) != packet_class_w3route) { |
| 1635 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad packet (class {})", conn_get_socket(c), packet_get_class(packet)); |
| 1636 | return -1; |
| 1637 | } |
| 1638 | if (conn_get_state(c) != conn_state_connected) { |
| 1639 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] not connected", conn_get_socket(c)); |
| 1640 | return -1; |
| 1641 | } |
| 1642 | |
| 1643 | /* init route connection */ |
| 1644 | if (packet_get_type(packet) == CLIENT_W3ROUTE_REQ) { |
| 1645 | t_connection *oldc; |
| 1646 | |
| 1647 | eventlog(eventlog_level_trace, __FUNCTION__, "[{}] sizeof t_client_w3route_req {}", conn_get_socket(c), sizeof(t_client_w3route_req)); |
| 1648 | username = packet_get_str_const(packet, sizeof(t_client_w3route_req), MAX_USERNAME_LEN); |
| 1649 | eventlog(eventlog_level_info, __FUNCTION__, "[{}] got username '{}'", conn_get_socket(c), username); |
| 1650 | gamec = connlist_find_connection_by_accountname(username); |
| 1651 | |
| 1652 | if (!gamec) { |
| 1653 | eventlog(eventlog_level_info, __FUNCTION__, "[{}] no game connection found for this w3route connection; closing", conn_get_socket(c)); |
| 1654 | conn_set_state(c, conn_state_destroy); |
| 1655 | return 0; |
| 1656 | } |
| 1657 | |
| 1658 | if (!(a = conn_get_anongame(gamec))) { |
| 1659 | eventlog(eventlog_level_info, __FUNCTION__, "[{}] no anongame struct for game connection", conn_get_socket(c)); |
| 1660 | conn_set_state(c, conn_state_destroy); |
| 1661 | return 0; |
| 1662 | } |
| 1663 | |
| 1664 | if (bn_int_get((unsigned char const *)packet->u.data + sizeof(t_client_w3route_req)+std::strlen(username) + 2) != anongame_get_id(a)) { |
| 1665 | eventlog(eventlog_level_info, __FUNCTION__, "[{}] client sent wrong id for user '{}', closing connection", conn_get_socket(c), username); |
| 1666 | conn_set_state(c, conn_state_destroy); |
| 1667 | return 0; |
| 1668 | } |
| 1669 | |
| 1670 | oldc = conn_get_routeconn(gamec); |
| 1671 | if (oldc) { |
| 1672 | conn_set_routeconn(oldc, NULL); |
| 1673 | conn_set_state(oldc, conn_state_destroy); |
| 1674 | } |
| 1675 | |
| 1676 | if (conn_set_routeconn(c, gamec) < 0 || conn_set_routeconn(gamec, c) < 0) { |
| 1677 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] conn_set_routeconn failed", conn_get_socket(c)); |
| 1678 | return -1; |
| 1679 | } |
| 1680 | |
| 1681 | /* set clienttag for w3route connections; we can do conn_get_clienttag() on them */ |
| 1682 | conn_set_clienttag(c, conn_get_clienttag(gamec)); |
| 1683 | |
| 1684 | anongame_set_addr(a, bn_int_get((unsigned char const *)packet->u.data + sizeof(t_client_w3route_req)+std::strlen(username) + 2 + 12)); |
| 1685 | anongame_set_joined(a, 0); |
| 1686 | anongame_set_loaded(a, 0); |
| 1687 | anongame_set_result(a, -1); |
| 1688 | anongame_set_gameresults(a, NULL); |
| 1689 | |
| 1690 | anongame_set_handle(a, bn_int_get(packet->u.client_w3route_req.handle)); |
| 1691 | |
| 1692 | if (!(rpacket = packet_create(packet_class_w3route))) { |
| 1693 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] packet_create failed", conn_get_socket(c)); |
| 1694 | return -1; |
| 1695 | } |
| 1696 | |
| 1697 | packet_set_size(rpacket, sizeof(t_server_w3route_ack)); |
| 1698 | packet_set_type(rpacket, SERVER_W3ROUTE_ACK); |
| 1699 | bn_byte_set(&rpacket->u.server_w3route_ack.unknown1, 7); |
| 1700 | bn_short_set(&rpacket->u.server_w3route_ack.unknown2, 0); |
| 1701 | bn_int_set(&rpacket->u.server_w3route_ack.unknown3, SERVER_W3ROUTE_ACK_UNKNOWN3); |
| 1702 | |
| 1703 | bn_short_set(&rpacket->u.server_w3route_ack.unknown4, 0xcccc); |
| 1704 | bn_byte_set(&rpacket->u.server_w3route_ack.playernum, anongame_get_playernum(a)); |
| 1705 | bn_short_set(&rpacket->u.server_w3route_ack.unknown5, 0x0002); |
| 1706 | bn_short_set(&rpacket->u.server_w3route_ack.port, conn_get_port(c)); |
| 1707 | bn_int_nset(&rpacket->u.server_w3route_ack.ip, conn_get_addr(c)); |
| 1708 | bn_int_set(&rpacket->u.server_w3route_ack.unknown7, 0); |
| 1709 | bn_int_set(&rpacket->u.server_w3route_ack.unknown8, 0); |
| 1710 | conn_push_outqueue(c, rpacket); |
| 1711 | packet_del_ref(rpacket); |
| 1712 | |
| 1713 | return 0; |
| 1714 | } |
| 1715 | else { |
| 1716 | gamec = conn_get_routeconn(c); |
| 1717 | if (gamec) |
| 1718 | a = conn_get_anongame(gamec); |
| 1719 | } |
| 1720 | |
| 1721 | if (!gamec) { |
| 1722 | eventlog(eventlog_level_info, __FUNCTION__, "[{}] no game connection found for this w3route connection", conn_get_socket(c)); |
| 1723 | return 0; |
| 1724 | } |
| 1725 | |
| 1726 | if (!a) { |
| 1727 | eventlog(eventlog_level_info, __FUNCTION__, "[{}] no anongame struct found for this w3route connection", conn_get_socket(c)); |
| 1728 | return 0; |
| 1729 | } |
| 1730 | |
| 1731 | gametype = anongame_get_queue(a); |
| 1732 | plnum = anongame_get_playernum(a); |
| 1733 | tp = anongame_get_totalplayers(a); |
| 1734 | |
| 1735 | /* handle these packets _before_ checking for routeconn of other players */ |
| 1736 | switch (packet_get_type(packet)) { |
| 1737 | case CLIENT_W3ROUTE_ECHOREPLY: |
| 1738 | return 0; |
| 1739 | case CLIENT_W3ROUTE_CONNECTED: |
| 1740 | return 0; |
| 1741 | case CLIENT_W3ROUTE_GAMERESULT: |
| 1742 | case CLIENT_W3ROUTE_GAMERESULT_W3XP: |
| 1743 | { |
| 1744 | |
| 1745 | /* insert reading of whole packet into t_gameresult */ |
| 1746 | |
| 1747 | t_anongame_gameresult *gameresult; |
| 1748 | int result; |
| 1749 | |
| 1750 | t_timer_data data; |
| 1751 | t_anongameinfo *inf = anongame_get_info(a); |
| 1752 | t_connection *ac; |
| 1753 | |
| 1754 | data.p = NULL; |
| 1755 | |
| 1756 | if (!(gameresult = anongame_gameresult_parse(packet))) |
| 1757 | result = -1; |
| 1758 | else /* own result is always stored as first result */ |
| 1759 | result = gameresult_get_player_result(gameresult, 0); |
| 1760 | |
| 1761 | eventlog(eventlog_level_trace, __FUNCTION__, "[{}] got W3ROUTE_GAMERESULT: {:08}", conn_get_socket(c), result); |
| 1762 | |
| 1763 | if (!inf) { |
| 1764 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] NULL anongameinfo", conn_get_socket(c)); |
| 1765 | return -1; |
| 1766 | } |
| 1767 | |
| 1768 | anongame_set_gameresults(a, gameresult); |
| 1769 | anongame_set_result(a, result); |
| 1770 | |
| 1771 | conn_set_state(c, conn_state_destroy); |
| 1772 | |
| 1773 | /* activate timers on open w3route connectons */ |
| 1774 | if (result == W3_GAMERESULT_WIN) { |
| 1775 | for (i = 0; i < tp; i++) { |
| 1776 | if (anongame_get_player(a, i)) { |
| 1777 | ac = conn_get_routeconn(anongame_get_player(a, i)); |
| 1778 | if (ac) { |
| 1779 | /* 300 seconds or 5 minute timer */ |
| 1780 | timerlist_add_timer(ac, now + (std::time_t) 300, conn_shutdown, data); |
| 1781 | eventlog(eventlog_level_trace, __FUNCTION__, "[{}] started timer to close w3route", conn_get_socket(ac)); |
| 1782 | } |
| 1783 | } |
| 1784 | } |
| 1785 | } |
| 1786 | |
| 1787 | return 0; |
| 1788 | } |
| 1789 | } |
| 1790 | |
| 1791 | for (i = 0; i < tp; i++) |
| 1792 | if (i + 1 != plnum && anongame_get_player(a, i)) |
| 1793 | if (!conn_get_routeconn(anongame_get_player(a, i)) || !conn_get_anongame(anongame_get_player(a, i))) { |
| 1794 | eventlog(eventlog_level_info, __FUNCTION__, "[{}] not all players have w3route connections up yet", conn_get_socket(c)); |
| 1795 | return 0; |
| 1796 | } |
| 1797 | |
| 1798 | /* handle these packets _after_ checking for routeconns of other players */ |
| 1799 | switch (packet_get_type(packet)) { |
| 1800 | case CLIENT_W3ROUTE_LOADINGDONE: |
| 1801 | eventlog(eventlog_level_trace, __FUNCTION__, "[{}] got LOADINGDONE, playernum: {}", conn_get_socket(c), plnum); |
| 1802 | |
| 1803 | anongame_set_loaded(a, 1); |
| 1804 | |
| 1805 | for (i = 0; i < tp; i++) { |
| 1806 | if (!anongame_get_player(a, i)) /* ignore disconnected players */ |
| 1807 | continue; |
| 1808 | if (!(rpacket = packet_create(packet_class_w3route))) { |
| 1809 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] packet_create failed", conn_get_socket(c)); |
| 1810 | return -1; |
| 1811 | } |
| 1812 | packet_set_size(rpacket, sizeof(t_server_w3route_loadingack)); |
| 1813 | packet_set_type(rpacket, SERVER_W3ROUTE_LOADINGACK); |
| 1814 | bn_byte_set(&rpacket->u.server_w3route_loadingack.playernum, plnum); |
| 1815 | conn_push_outqueue(conn_get_routeconn(anongame_get_player(a, i)), rpacket); |
| 1816 | packet_del_ref(rpacket); |
| 1817 | } |
| 1818 | |
| 1819 | /* have all players loaded? */ |
| 1820 | for (i = 0; i < tp; i++) |
| 1821 | if (i + 1 != plnum && anongame_get_player(a, i) && !anongame_get_loaded(conn_get_anongame(anongame_get_player(a, i)))) |
| 1822 | return 0; |
| 1823 | |
| 1824 | for (i = 0; i < tp; i++) { |
| 1825 | if (!anongame_get_player(a, i)) |
| 1826 | continue; |
| 1827 | |
| 1828 | if (!(rpacket = packet_create(packet_class_w3route))) { |
| 1829 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] packet_create failed", conn_get_socket(c)); |
| 1830 | return -1; |
| 1831 | } |
| 1832 | |
| 1833 | packet_set_size(rpacket, sizeof(t_server_w3route_ready)); |
| 1834 | packet_set_type(rpacket, SERVER_W3ROUTE_READY); |
| 1835 | bn_byte_set(&rpacket->u.server_w3route_host.unknown1, 0); |
| 1836 | conn_push_outqueue(conn_get_routeconn(anongame_get_player(a, i)), rpacket); |
| 1837 | packet_del_ref(rpacket); |
| 1838 | } |
| 1839 | |
| 1840 | break; |
| 1841 | |
| 1842 | case CLIENT_W3ROUTE_ABORT: |
| 1843 | eventlog(eventlog_level_debug, __FUNCTION__, "[{}] got W3ROUTE_ABORT", conn_get_socket(c)); |
| 1844 | break; |
| 1845 | |
| 1846 | default: |
| 1847 | eventlog(eventlog_level_trace, __FUNCTION__, "[{}] default: got packet type: {:04}", conn_get_socket(c), packet_get_type(packet)); |
| 1848 | } |
| 1849 | |
| 1850 | return 0; |
| 1851 | } |
| 1852 | |
| 1853 | extern int handle_anongame_join(t_connection * c) |
| 1854 | { |
| 1855 | t_anongame *a, *ja, *oa; |
| 1856 | t_connection *jc, *o; |
| 1857 | t_packet *rpacket; |
| 1858 | int tp, level; |
| 1859 | char gametype; |
| 1860 | t_account *acct; |
| 1861 | t_clienttag ct = conn_get_clienttag(c); |
| 1862 | |
| 1863 | static t_server_w3route_playerinfo2 pl2; |
| 1864 | static t_server_w3route_levelinfo2 li2; |
| 1865 | static t_server_w3route_playerinfo_addr pl_addr; |
| 1866 | |
| 1867 | int i, j; |
| 1868 | |
| 1869 | if (!c) { |
| 1870 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] got NULL connection", conn_get_socket(c)); |
| 1871 | return -1; |
| 1872 | } |
| 1873 | if (!(conn_get_routeconn(c))) { |
| 1874 | eventlog(eventlog_level_info, __FUNCTION__, "[{}] no route connection", conn_get_socket(c)); |
| 1875 | return -1; |
| 1876 | } |
| 1877 | if (!(a = conn_get_anongame(c))) { |
| 1878 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] no anongame struct", conn_get_socket(c)); |
| 1879 | return -1; |
| 1880 | } |
| 1881 | |
| 1882 | anongame_set_joined(a, 1); |
| 1883 | gametype = anongame_get_queue(a); |
| 1884 | tp = anongame_get_totalplayers(a); |
| 1885 | |
| 1886 | /* wait till all players have w3route conns */ |
| 1887 | for (i = 0; i < tp; i++) |
| 1888 | if (anongame_get_player(a, i) && (!conn_get_routeconn(anongame_get_player(a, i)) || !conn_get_anongame(anongame_get_player(a, i)) || !anongame_get_joined(conn_get_anongame(anongame_get_player(a, i))))) { |
| 1889 | eventlog(eventlog_level_info, __FUNCTION__, "[{}] not all players have joined game BNet yet", conn_get_socket(c)); |
| 1890 | return 0; |
| 1891 | } |
| 1892 | |
| 1893 | /* then send each player info about all others */ |
| 1894 | for (j = 0; j < tp; j++) { |
| 1895 | jc = anongame_get_player(a, j); |
| 1896 | if (!jc) /* ignore disconnected players */ |
| 1897 | continue; |
| 1898 | ja = conn_get_anongame(jc); |
| 1899 | |
| 1900 | /* send a playerinfo packet for this player to each other player */ |
| 1901 | for (i = 0; i < tp; i++) { |
| 1902 | if (i + 1 != anongame_get_playernum(ja)) { |
| 1903 | eventlog(eventlog_level_trace, __FUNCTION__, "i = {}", i); |
| 1904 | |
| 1905 | if (!(o = anongame_get_player(ja, i))) { |
| 1906 | eventlog(eventlog_level_warn, __FUNCTION__, "[{}] player {} disconnected, ignoring", conn_get_socket(c), i); |
| 1907 | continue; |
| 1908 | } |
| 1909 | |
| 1910 | if (!(rpacket = packet_create(packet_class_w3route))) { |
| 1911 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] packet_create failed", conn_get_socket(c)); |
| 1912 | return -1; |
| 1913 | } |
| 1914 | |
| 1915 | packet_set_size(rpacket, sizeof(t_server_w3route_playerinfo)); |
| 1916 | packet_set_type(rpacket, SERVER_W3ROUTE_PLAYERINFO); |
| 1917 | |
| 1918 | |
| 1919 | if (!(oa = conn_get_anongame(o))) { |
| 1920 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] no anongame struct of player {}", conn_get_socket(c), i); |
| 1921 | packet_del_ref(rpacket); |
| 1922 | return -1; |
| 1923 | } |
| 1924 | |
| 1925 | bn_int_set(&rpacket->u.server_w3route_playerinfo.handle, anongame_get_handle(oa)); |
| 1926 | bn_byte_set(&rpacket->u.server_w3route_playerinfo.playernum, anongame_get_playernum(oa)); |
| 1927 | |
| 1928 | packet_append_string(rpacket, conn_get_username(o)); |
| 1929 | |
| 1930 | /* playerinfo2 */ |
| 1931 | bn_byte_set(&pl2.unknown1, 8); |
| 1932 | bn_int_set(&pl2.id, anongame_get_id(oa)); |
| 1933 | bn_int_set(&pl2.race, anongame_get_race(oa)); |
| 1934 | packet_append_data(rpacket, &pl2, sizeof(pl2)); |
| 1935 | |
| 1936 | /* external addr */ |
| 1937 | bn_short_set(&pl_addr.unknown1, 2); |
| 1938 | { /* trans support */ |
| 1939 | unsigned short port = conn_get_game_port(o); |
| 1940 | unsigned int addr = conn_get_game_addr(o); |
| 1941 | |
| 1942 | trans_net(conn_get_game_addr(jc), &addr, &port); |
| 1943 | |
| 1944 | bn_short_nset(&pl_addr.port, port); |
| 1945 | bn_int_nset(&pl_addr.ip, addr); |
| 1946 | } |
| 1947 | bn_int_set(&pl_addr.unknown2, 0); |
| 1948 | bn_int_set(&pl_addr.unknown3, 0); |
| 1949 | packet_append_data(rpacket, &pl_addr, sizeof(pl_addr)); |
| 1950 | |
| 1951 | /* local addr */ |
| 1952 | bn_short_set(&pl_addr.unknown1, 2); |
| 1953 | bn_short_nset(&pl_addr.port, conn_get_game_port(o)); |
| 1954 | bn_int_set(&pl_addr.ip, anongame_get_addr(oa)); |
| 1955 | bn_int_set(&pl_addr.unknown2, 0); |
| 1956 | bn_int_set(&pl_addr.unknown3, 0); |
| 1957 | packet_append_data(rpacket, &pl_addr, sizeof(pl_addr)); |
| 1958 | |
| 1959 | conn_push_outqueue(conn_get_routeconn(jc), rpacket); |
| 1960 | packet_del_ref(rpacket); |
| 1961 | } |
| 1962 | } |
| 1963 | |
| 1964 | /* levelinfo */ |
| 1965 | if (!(rpacket = packet_create(packet_class_w3route))) { |
| 1966 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] packet_create failed", conn_get_socket(c)); |
| 1967 | return -1; |
| 1968 | } |
| 1969 | |
| 1970 | packet_set_size(rpacket, sizeof(t_server_w3route_levelinfo)); |
| 1971 | packet_set_type(rpacket, SERVER_W3ROUTE_LEVELINFO); |
| 1972 | bn_byte_set(&rpacket->u.server_w3route_levelinfo.numplayers, anongame_get_currentplayers(a)); |
| 1973 | |
| 1974 | for (i = 0; i < tp; i++) { |
| 1975 | if (!anongame_get_player(ja, i)) |
| 1976 | continue; |
| 1977 | |
| 1978 | bn_byte_set(&li2.plnum, i + 1); |
| 1979 | bn_byte_set(&li2.unknown1, 3); |
| 1980 | |
| 1981 | switch (gametype) { |
| 1982 | case ANONGAME_TYPE_1V1: |
| 1983 | level = account_get_ladder_level(conn_get_account(anongame_get_player(ja, i)), ct, ladder_id_solo); |
| 1984 | break; |
| 1985 | case ANONGAME_TYPE_SMALL_FFA: |
| 1986 | case ANONGAME_TYPE_TEAM_FFA: |
| 1987 | level = account_get_ladder_level(conn_get_account(anongame_get_player(ja, i)), ct, ladder_id_ffa); |
| 1988 | break; |
| 1989 | case ANONGAME_TYPE_AT_2V2: |
| 1990 | case ANONGAME_TYPE_AT_3V3: |
| 1991 | case ANONGAME_TYPE_AT_4V4: |
| 1992 | case ANONGAME_TYPE_AT_2V2V2: |
| 1993 | acct = conn_get_account(anongame_get_player(ja, i)); |
| 1994 | level = team_get_level(account_find_team_by_teamid(acct, account_get_currentatteam(acct))); |
| 1995 | break; |
| 1996 | case ANONGAME_TYPE_TY: |
| 1997 | level = 0; /* FIXME-TY: WHAT TO DO HERE */ |
| 1998 | break; |
| 1999 | default: |
| 2000 | level = account_get_ladder_level(conn_get_account(anongame_get_player(ja, i)), ct, ladder_id_team); |
| 2001 | break; |
| 2002 | } |
| 2003 | |
| 2004 | /* first anongame shows level 0 as level 1 */ |
| 2005 | bn_byte_set(&li2.level, level ? level : 1); |
| 2006 | |
| 2007 | bn_short_set(&li2.unknown2, 0); |
| 2008 | packet_append_data(rpacket, &li2, sizeof(li2)); |
| 2009 | } |
| 2010 | |
| 2011 | conn_push_outqueue(conn_get_routeconn(jc), rpacket); |
| 2012 | packet_del_ref(rpacket); |
| 2013 | |
| 2014 | /* startgame1 */ |
| 2015 | if (!(rpacket = packet_create(packet_class_w3route))) { |
| 2016 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] packet_create failed", conn_get_socket(c)); |
| 2017 | return -1; |
| 2018 | } |
| 2019 | packet_set_size(rpacket, sizeof(t_server_w3route_startgame1)); |
| 2020 | packet_set_type(rpacket, SERVER_W3ROUTE_STARTGAME1); |
| 2021 | conn_push_outqueue(conn_get_routeconn(jc), rpacket); |
| 2022 | packet_del_ref(rpacket); |
| 2023 | |
| 2024 | /* startgame2 */ |
| 2025 | if (!(rpacket = packet_create(packet_class_w3route))) { |
| 2026 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] packet_create failed", conn_get_socket(c)); |
| 2027 | return -1; |
| 2028 | } |
| 2029 | packet_set_size(rpacket, sizeof(t_server_w3route_startgame2)); |
| 2030 | packet_set_type(rpacket, SERVER_W3ROUTE_STARTGAME2); |
| 2031 | conn_push_outqueue(conn_get_routeconn(jc), rpacket); |
| 2032 | packet_del_ref(rpacket); |
| 2033 | } |
| 2034 | return 0; |
| 2035 | } |
| 2036 | |
| 2037 | extern const char * anongame_get_map_from_prefs(int queue, t_clienttag clienttag) |
| 2038 | { |
| 2039 | int i, j = 0; |
| 2040 | const char *default_map, *selected; |
| 2041 | char *res_maps[32]; |
| 2042 | char clienttag_str[5]; |
| 2043 | |
| 2044 | switch (clienttag) { |
| 2045 | case CLIENTTAG_REDALERT2_UINT: |
| 2046 | default_map = "eb2.map"; |
| 2047 | break; |
| 2048 | case CLIENTTAG_YURISREV_UINT: |
| 2049 | default_map = "xgrinder.map"; |
| 2050 | break; |
| 2051 | default: |
| 2052 | ERROR1("invalid clienttag: {}", tag_uint_to_str(clienttag_str, clienttag)); |
| 2053 | return "eb2.map"; |
| 2054 | } |
| 2055 | |
| 2056 | for (i = 0; i < 32; i++) |
| 2057 | res_maps[i] = NULL; |
| 2058 | |
| 2059 | for (i = 0; i < maplists_get_totalmaps_by_queue(clienttag, queue); i++) { |
| 2060 | res_maps[j++] = maplists_get_map(queue, clienttag, i + 1); |
| 2061 | } |
| 2062 | |
| 2063 | if (j == 0) |
| 2064 | { |
| 2065 | eventlog(eventlog_level_error, __FUNCTION__, "j == 0; returning \"eb2.map\""); |
| 2066 | return "eb2.map"; |
| 2067 | } |
| 2068 | i = std::rand() % j; |
| 2069 | if (res_maps[i]) |
| 2070 | selected = res_maps[i]; |
| 2071 | else |
| 2072 | selected = default_map; |
| 2073 | |
| 2074 | eventlog(eventlog_level_trace, __FUNCTION__, "got map {} from prefs", selected); |
| 2075 | return selected; |
| 2076 | |
| 2077 | } |
| 2078 | |
| 2079 | } |
| 2080 | |
| 2081 | } |