| 1 | /* |
| 2 | * Copyright (C) 1998 Mark Baysinger ([email protected]) |
| 3 | * Copyright (C) 1998,1999,2000 Ross Combs ([email protected]) |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version 2 |
| 8 | * of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 | */ |
| 19 | #define GAME_INTERNAL_ACCESS |
| 20 | #include "common/setup_before.h" |
| 21 | #include "game.h" |
| 22 | |
| 23 | #include <cerrno> |
| 24 | #include <cstring> |
| 25 | #include <cassert> |
| 26 | |
| 27 | #include "compat/rename.h" |
| 28 | #include "compat/strcasecmp.h" |
| 29 | #include "common/eventlog.h" |
| 30 | #include "common/addr.h" |
| 31 | #include "common/bnettime.h" |
| 32 | |
| 33 | #include "connection.h" |
| 34 | #include "channel.h" |
| 35 | #include "server.h" |
| 36 | #include "account.h" |
| 37 | #include "account_wrap.h" |
| 38 | #include "prefs.h" |
| 39 | #include "watch.h" |
| 40 | #include "realm.h" |
| 41 | #include "ladder.h" |
| 42 | #include "game_conv.h" |
| 43 | #include "common/setup_after.h" |
| 44 | |
| 45 | #ifdef WITH_LUA |
| 46 | #include "luainterface.h" |
| 47 | #endif |
| 48 | namespace pvpgn |
| 49 | { |
| 50 | |
| 51 | namespace bnetd |
| 52 | { |
| 53 | |
| 54 | DECLARE_ELIST_INIT(gamelist_head); |
| 55 | static int glist_length = 0; |
| 56 | static int totalcount = 0; |
| 57 | |
| 58 | |
| 59 | static void game_choose_host(t_game * game); |
| 60 | static void game_destroy(t_game * game); |
| 61 | static int game_report(t_game * game); |
| 62 | |
| 63 | |
| 64 | static void game_choose_host(t_game * game) |
| 65 | { |
| 66 | unsigned int i; |
| 67 | |
| 68 | if (game->count < 1) |
| 69 | { |
| 70 | eventlog(eventlog_level_error, __FUNCTION__, "game has had no connections?!"); |
| 71 | return; |
| 72 | } |
| 73 | if (!game->connections) |
| 74 | { |
| 75 | eventlog(eventlog_level_error, __FUNCTION__, "game has NULL connections array"); |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | for (i = 0; i < game->count; i++) |
| 80 | if (game->connections[i]) |
| 81 | { |
| 82 | game->owner = game->connections[i]; |
| 83 | game->addr = conn_get_game_addr(game->connections[i]); |
| 84 | game->port = conn_get_game_port(game->connections[i]); |
| 85 | return; |
| 86 | } |
| 87 | eventlog(eventlog_level_warn, __FUNCTION__, "no valid connections found"); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | extern char const * game_type_get_str(t_game_type type) |
| 92 | { |
| 93 | switch (type) |
| 94 | { |
| 95 | case game_type_none: |
| 96 | return "NONE"; |
| 97 | |
| 98 | case game_type_melee: |
| 99 | return "melee"; |
| 100 | |
| 101 | case game_type_topvbot: |
| 102 | return "top vs bottom"; |
| 103 | |
| 104 | case game_type_ffa: |
| 105 | return "free for all"; |
| 106 | |
| 107 | case game_type_oneonone: |
| 108 | return "one on one"; |
| 109 | |
| 110 | case game_type_ctf: |
| 111 | return "capture the flag"; |
| 112 | |
| 113 | case game_type_greed: |
| 114 | return "greed"; |
| 115 | |
| 116 | case game_type_slaughter: |
| 117 | return "slaughter"; |
| 118 | |
| 119 | case game_type_sdeath: |
| 120 | return "sudden death"; |
| 121 | |
| 122 | case game_type_ladder: |
| 123 | return "ladder"; |
| 124 | |
| 125 | case game_type_ironman: |
| 126 | return "ironman"; |
| 127 | |
| 128 | case game_type_mapset: |
| 129 | return "mapset"; |
| 130 | |
| 131 | case game_type_teammelee: |
| 132 | return "team melee"; |
| 133 | |
| 134 | case game_type_teamffa: |
| 135 | return "team free for all"; |
| 136 | |
| 137 | case game_type_teamctf: |
| 138 | return "team capture the flag"; |
| 139 | |
| 140 | case game_type_pgl: |
| 141 | return "PGL"; |
| 142 | |
| 143 | case game_type_diablo: |
| 144 | return "Diablo"; |
| 145 | |
| 146 | case game_type_diablo2open: |
| 147 | return "Diablo II (open)"; |
| 148 | |
| 149 | case game_type_diablo2closed: |
| 150 | return "Diablo II (closed)"; |
| 151 | |
| 152 | case game_type_all: |
| 153 | default: |
| 154 | return "UNKNOWN"; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | |
| 159 | extern char const * game_status_get_str(t_game_status status) |
| 160 | { |
| 161 | switch (status) |
| 162 | { |
| 163 | case game_status_started: |
| 164 | return "started"; |
| 165 | |
| 166 | case game_status_full: |
| 167 | return "full"; |
| 168 | |
| 169 | case game_status_open: |
| 170 | return "open"; |
| 171 | |
| 172 | case game_status_done: |
| 173 | return "done"; |
| 174 | |
| 175 | case game_status_loaded: |
| 176 | return "loaded"; |
| 177 | |
| 178 | default: |
| 179 | return "UNKNOWN"; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | |
| 184 | extern char const * game_result_get_str(t_game_result result) |
| 185 | { |
| 186 | switch (result) |
| 187 | { |
| 188 | case game_result_none: |
| 189 | return "NONE"; |
| 190 | |
| 191 | case game_result_win: |
| 192 | return "WIN"; |
| 193 | |
| 194 | case game_result_loss: |
| 195 | return "LOSS"; |
| 196 | |
| 197 | case game_result_draw: |
| 198 | return "DRAW"; |
| 199 | |
| 200 | case game_result_disconnect: |
| 201 | return "DISCONNECT"; |
| 202 | |
| 203 | case game_result_observer: |
| 204 | return "OBSERVER"; |
| 205 | |
| 206 | default: |
| 207 | return "UNKNOWN"; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | |
| 212 | extern char const * game_option_get_str(t_game_option option) |
| 213 | { |
| 214 | switch (option) |
| 215 | { |
| 216 | case game_option_melee_normal: |
| 217 | return "normal"; |
| 218 | case game_option_ffa_normal: |
| 219 | return "normal"; |
| 220 | case game_option_oneonone_normal: |
| 221 | return "normal"; |
| 222 | case game_option_ctf_normal: |
| 223 | return "normal"; |
| 224 | case game_option_greed_10000: |
| 225 | return "10000 minerals"; |
| 226 | case game_option_greed_7500: |
| 227 | return "7500 minerals"; |
| 228 | case game_option_greed_5000: |
| 229 | return "5000 minerals"; |
| 230 | case game_option_greed_2500: |
| 231 | return "2500 minerals"; |
| 232 | case game_option_slaughter_60: |
| 233 | return "60 minutes"; |
| 234 | case game_option_slaughter_45: |
| 235 | return "45 minutes"; |
| 236 | case game_option_slaughter_30: |
| 237 | return "30 minutes"; |
| 238 | case game_option_slaughter_15: |
| 239 | return "15 minutes"; |
| 240 | case game_option_sdeath_normal: |
| 241 | return "normal"; |
| 242 | case game_option_ladder_countasloss: |
| 243 | return "count as loss"; |
| 244 | case game_option_ladder_nopenalty: |
| 245 | return "no penalty"; |
| 246 | case game_option_mapset_normal: |
| 247 | return "normal"; |
| 248 | case game_option_teammelee_4: |
| 249 | return "4 teams"; |
| 250 | case game_option_teammelee_3: |
| 251 | return "3 teams"; |
| 252 | case game_option_teammelee_2: |
| 253 | return "2 teams"; |
| 254 | case game_option_teamffa_4: |
| 255 | return "4 teams"; |
| 256 | case game_option_teamffa_3: |
| 257 | return "3 teams"; |
| 258 | case game_option_teamffa_2: |
| 259 | return "2 teams"; |
| 260 | case game_option_teamctf_4: |
| 261 | return "4 teams"; |
| 262 | case game_option_teamctf_3: |
| 263 | return "3 teams"; |
| 264 | case game_option_teamctf_2: |
| 265 | return "2 teams"; |
| 266 | case game_option_topvbot_7: |
| 267 | return "7 vs all"; |
| 268 | case game_option_topvbot_6: |
| 269 | return "6 vs all"; |
| 270 | case game_option_topvbot_5: |
| 271 | return "5 vs all"; |
| 272 | case game_option_topvbot_4: |
| 273 | return "4 vs all"; |
| 274 | case game_option_topvbot_3: |
| 275 | return "3 vs all"; |
| 276 | case game_option_topvbot_2: |
| 277 | return "2 vs all"; |
| 278 | case game_option_topvbot_1: |
| 279 | return "1 vs all"; |
| 280 | |
| 281 | case game_option_none: |
| 282 | return "none"; |
| 283 | default: |
| 284 | return "UNKNOWN"; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | |
| 289 | extern char const * game_maptype_get_str(t_game_maptype maptype) |
| 290 | { |
| 291 | switch (maptype) |
| 292 | { |
| 293 | case game_maptype_selfmade: |
| 294 | return "Self-Made"; |
| 295 | case game_maptype_blizzard: |
| 296 | return "Blizzard"; |
| 297 | case game_maptype_ladder: |
| 298 | return "Ladder"; |
| 299 | case game_maptype_pgl: |
| 300 | return "PGL"; |
| 301 | case game_maptype_kbk: |
| 302 | return "KBK"; |
| 303 | case game_maptype_compusa: |
| 304 | return "CompUSA"; |
| 305 | default: |
| 306 | return "Unknown"; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | |
| 311 | extern char const * game_tileset_get_str(t_game_tileset tileset) |
| 312 | { |
| 313 | switch (tileset) |
| 314 | { |
| 315 | case game_tileset_badlands: |
| 316 | return "Badlands"; |
| 317 | case game_tileset_space: |
| 318 | return "Space"; |
| 319 | case game_tileset_installation: |
| 320 | return "Installation"; |
| 321 | case game_tileset_ashworld: |
| 322 | return "Ash World"; |
| 323 | case game_tileset_jungle: |
| 324 | return "Jungle"; |
| 325 | case game_tileset_desert: |
| 326 | return "Desert"; |
| 327 | case game_tileset_ice: |
| 328 | return "Ice"; |
| 329 | case game_tileset_twilight: |
| 330 | return "Twilight"; |
| 331 | default: |
| 332 | return "Unknown"; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | |
| 337 | extern char const * game_speed_get_str(t_game_speed speed) |
| 338 | { |
| 339 | switch (speed) |
| 340 | { |
| 341 | case game_speed_slowest: |
| 342 | return "slowest"; |
| 343 | case game_speed_slower: |
| 344 | return "slower"; |
| 345 | case game_speed_slow: |
| 346 | return "slow"; |
| 347 | case game_speed_normal: |
| 348 | return "normal"; |
| 349 | case game_speed_fast: |
| 350 | return "fast"; |
| 351 | case game_speed_faster: |
| 352 | return "faster"; |
| 353 | case game_speed_fastest: |
| 354 | return "fastest"; |
| 355 | default: |
| 356 | return "unknown"; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | |
| 361 | extern char const * game_difficulty_get_str(unsigned difficulty) |
| 362 | { |
| 363 | switch (difficulty) |
| 364 | { |
| 365 | case game_difficulty_normal: |
| 366 | return "normal"; |
| 367 | case game_difficulty_nightmare: |
| 368 | return "nightmare"; |
| 369 | case game_difficulty_hell: |
| 370 | return "hell"; |
| 371 | case game_difficulty_hardcore_normal: |
| 372 | return "hardcore normal"; |
| 373 | case game_difficulty_hardcore_nightmare: |
| 374 | return "hardcore nightmare"; |
| 375 | case game_difficulty_hardcore_hell: |
| 376 | return "hardcore hell"; |
| 377 | default: |
| 378 | return "unknown"; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | |
| 383 | extern t_game * game_create(char const * name, char const * pass, char const * info, t_game_type type, int startver, t_clienttag clienttag, unsigned long gameversion) |
| 384 | { |
| 385 | t_game * game; |
| 386 | |
| 387 | if (!name) |
| 388 | { |
| 389 | eventlog(eventlog_level_info, __FUNCTION__, "got NULL game name"); |
| 390 | return NULL; |
| 391 | } |
| 392 | if (!pass) |
| 393 | { |
| 394 | eventlog(eventlog_level_info, __FUNCTION__, "got NULL game pass"); |
| 395 | return NULL; |
| 396 | } |
| 397 | if (!info) |
| 398 | { |
| 399 | eventlog(eventlog_level_info, __FUNCTION__, "got NULL game info"); |
| 400 | return NULL; |
| 401 | } |
| 402 | |
| 403 | if (gamelist_find_game_available(name, clienttag, game_type_all)) |
| 404 | { |
| 405 | eventlog(eventlog_level_info, __FUNCTION__, "game \"{}\" not created because it already exists", name); |
| 406 | return NULL; /* already have a game by that name */ |
| 407 | } |
| 408 | |
| 409 | game = (t_game*)xmalloc(sizeof(t_game)); |
| 410 | game->name = xstrdup(name); |
| 411 | game->pass = xstrdup(pass); |
| 412 | game->info = xstrdup(info); |
| 413 | if (!(game->clienttag = clienttag)) |
| 414 | { |
| 415 | eventlog(eventlog_level_error, __FUNCTION__, "got UNKNOWN clienttag"); |
| 416 | xfree((void *)game->info); /* avoid warning */ |
| 417 | xfree((void *)game->pass); /* avoid warning */ |
| 418 | xfree((void *)game->name); /* avoid warning */ |
| 419 | xfree(game); |
| 420 | return NULL; |
| 421 | } |
| 422 | |
| 423 | game->type = type; |
| 424 | game->addr = 0; /* will be set by first player */ |
| 425 | game->port = 0; /* will be set by first player */ |
| 426 | game->version = gameversion; |
| 427 | game->startver = startver; /* start packet version */ |
| 428 | game->status = game_status_open; |
| 429 | game->realm = 0; |
| 430 | game->realmname = NULL; |
| 431 | game->id = ++totalcount; |
| 432 | game->mapname = NULL; |
| 433 | game->ref = 0; |
| 434 | game->count = 0; |
| 435 | game->owner = NULL; |
| 436 | game->connections = NULL; |
| 437 | game->players = NULL; |
| 438 | game->results = NULL; |
| 439 | game->reported_results = NULL; |
| 440 | game->report_heads = NULL; |
| 441 | game->report_bodies = NULL; |
| 442 | game->create_time = now; |
| 443 | game->start_time = (std::time_t)0; |
| 444 | game->lastaccess_time = now; |
| 445 | game->option = game_option_none; |
| 446 | game->maptype = game_maptype_none; |
| 447 | game->tileset = game_tileset_none; |
| 448 | game->speed = game_speed_none; |
| 449 | game->mapsize_x = 0; |
| 450 | game->mapsize_y = 0; |
| 451 | game->maxplayers = 0; |
| 452 | game->bad = 0; |
| 453 | game->description = NULL; |
| 454 | game->flag = std::strcmp(pass, "") ? game_flag_private : game_flag_none; |
| 455 | game->difficulty = game_difficulty_none; |
| 456 | |
| 457 | game_parse_info(game, info); |
| 458 | |
| 459 | elist_add(&gamelist_head, &game->glist_link); |
| 460 | glist_length++; |
| 461 | |
| 462 | eventlog(eventlog_level_info, __FUNCTION__, "game \"{}\" (pass \"{}\") type {}({}) startver {} created", name, pass, (unsigned short)type, game_type_get_str(game->type), startver); |
| 463 | |
| 464 | return game; |
| 465 | } |
| 466 | |
| 467 | |
| 468 | static void game_destroy(t_game * game) |
| 469 | { |
| 470 | unsigned int i; |
| 471 | |
| 472 | if (!game) |
| 473 | { |
| 474 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 475 | return; |
| 476 | } |
| 477 | |
| 478 | #ifdef WITH_LUA |
| 479 | lua_handle_game(game, NULL, luaevent_game_destroy); |
| 480 | #endif |
| 481 | |
| 482 | elist_del(&game->glist_link); |
| 483 | glist_length--; |
| 484 | |
| 485 | if (game->realmname) |
| 486 | { |
| 487 | realm_add_game_number(realmlist_find_realm(game->realmname), -1); |
| 488 | } |
| 489 | |
| 490 | eventlog(eventlog_level_debug, __FUNCTION__, "game \"{}\" (count={} ref={}) removed from list...", game_get_name(game), game->count, game->ref); |
| 491 | |
| 492 | for (i = 0; i < game->count; i++) |
| 493 | { |
| 494 | if (game->report_bodies && game->report_bodies[i]) |
| 495 | xfree((void *)game->report_bodies[i]); /* avoid warning */ |
| 496 | if (game->report_heads && game->report_heads[i]) |
| 497 | xfree((void *)game->report_heads[i]); /* avoid warning */ |
| 498 | if (game->reported_results && game->reported_results[i]) |
| 499 | xfree((void *)game->reported_results[i]); |
| 500 | } |
| 501 | if (game->realmname) |
| 502 | xfree((void *)game->realmname); /* avoid warining */ |
| 503 | if (game->report_bodies) |
| 504 | xfree((void *)game->report_bodies); /* avoid warning */ |
| 505 | if (game->report_heads) |
| 506 | xfree((void *)game->report_heads); /* avoid warning */ |
| 507 | if (game->results) |
| 508 | xfree((void *)game->results); /* avoid warning */ |
| 509 | if (game->reported_results) |
| 510 | xfree((void *)game->reported_results); |
| 511 | if (game->connections) |
| 512 | xfree((void *)game->connections); /* avoid warning */ |
| 513 | if (game->players) |
| 514 | xfree((void *)game->players); /* avoid warning */ |
| 515 | if (game->mapname) |
| 516 | xfree((void *)game->mapname); /* avoid warning */ |
| 517 | if (game->description) |
| 518 | xfree((void *)game->description); /* avoid warning */ |
| 519 | |
| 520 | xfree((void *)game->info); /* avoid warning */ |
| 521 | xfree((void *)game->pass); /* avoid warning */ |
| 522 | if (game->name) xfree((void *)game->name); /* avoid warning */ |
| 523 | xfree((void *)game); /* avoid warning */ |
| 524 | |
| 525 | eventlog(eventlog_level_info, __FUNCTION__, "game deleted"); |
| 526 | |
| 527 | return; |
| 528 | } |
| 529 | |
| 530 | static int game_evaluate_results(t_game * game) |
| 531 | { |
| 532 | unsigned int i, j; |
| 533 | unsigned int wins, losses, draws, disconnects, reports; |
| 534 | |
| 535 | if (!game) |
| 536 | { |
| 537 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 538 | return -1; |
| 539 | } |
| 540 | if (!game->results) |
| 541 | { |
| 542 | eventlog(eventlog_level_error, __FUNCTION__, "results array is NULL"); |
| 543 | return -1; |
| 544 | } |
| 545 | |
| 546 | if (!game->reported_results) |
| 547 | { |
| 548 | eventlog(eventlog_level_error, __FUNCTION__, "reported_results array is NULL"); |
| 549 | return -1; |
| 550 | } |
| 551 | |
| 552 | int discisloss = game_discisloss(game); |
| 553 | |
| 554 | for (i = 0; i < game->count; i++) |
| 555 | { |
| 556 | wins = losses = draws = disconnects = reports = 0; |
| 557 | |
| 558 | for (j = 0; j < game->count; j++) |
| 559 | { |
| 560 | if (game->reported_results[j]) |
| 561 | { |
| 562 | switch (game->reported_results[j][i]) |
| 563 | { |
| 564 | case game_result_win: |
| 565 | wins++; |
| 566 | reports++; |
| 567 | break; |
| 568 | case game_result_loss: |
| 569 | losses++; |
| 570 | reports++; |
| 571 | break; |
| 572 | case game_result_draw: |
| 573 | draws++; |
| 574 | reports++; |
| 575 | break; |
| 576 | case game_result_disconnect: |
| 577 | disconnects++; |
| 578 | reports++; |
| 579 | break; |
| 580 | default: |
| 581 | break; |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | eventlog(eventlog_level_debug, __FUNCTION__, "wins: {} losses: {} draws: {} disconnects: {}", wins, losses, draws, disconnects); |
| 586 | |
| 587 | //now decide what result we give |
| 588 | if (!(reports)) // no results at all - game canceled before starting |
| 589 | { |
| 590 | game->results[i] = game_result_none; |
| 591 | eventlog(eventlog_level_debug, __FUNCTION__, "deciding to give \"none\" to player {}", i); |
| 592 | } |
| 593 | else if ((disconnects >= draws) && (disconnects >= losses) && (disconnects >= wins)) |
| 594 | { |
| 595 | if (discisloss) |
| 596 | { |
| 597 | game->results[i] = game_result_loss; //losses are also bad... |
| 598 | eventlog(eventlog_level_debug, __FUNCTION__, "deciding to give \"loss\" to player {} (due to discisloss)", i); |
| 599 | } |
| 600 | else |
| 601 | { |
| 602 | game->results[i] = game_result_disconnect; //consider disconnects the worst case... |
| 603 | eventlog(eventlog_level_debug, __FUNCTION__, "deciding to give \"disconnect\" to player {}", i); |
| 604 | } |
| 605 | } |
| 606 | else if ((losses >= wins) && (losses >= draws)) |
| 607 | { |
| 608 | game->results[i] = game_result_loss; //losses are also bad... |
| 609 | eventlog(eventlog_level_debug, __FUNCTION__, "deciding to give \"loss\" to player {}", i); |
| 610 | } |
| 611 | else if ((draws >= wins)) |
| 612 | { |
| 613 | game->results[i] = game_result_draw; |
| 614 | eventlog(eventlog_level_debug, __FUNCTION__, "deciding to give \"draw\" to player {}", i); |
| 615 | } |
| 616 | else if (wins) |
| 617 | { |
| 618 | game->results[i] = game_result_win; |
| 619 | eventlog(eventlog_level_debug, __FUNCTION__, "deciding to give \"win\" to player {}", i); |
| 620 | } |
| 621 | } |
| 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | static int game_match_type(t_game_type type, const char *gametypes) |
| 626 | { |
| 627 | char *p, *q; |
| 628 | int res; |
| 629 | |
| 630 | if (!gametypes || !gametypes[0]) return 0; |
| 631 | |
| 632 | gametypes = p = xstrdup(gametypes); |
| 633 | res = 0; |
| 634 | do { |
| 635 | q = std::strchr(p, ','); |
| 636 | if (q) *q = '\0'; |
| 637 | if (!strcasecmp(p, "topvbot")) { |
| 638 | if (type == game_type_topvbot) { res = 1; break; } |
| 639 | } |
| 640 | else if (!strcasecmp(p, "melee")) { |
| 641 | if (type == game_type_melee) { res = 1; break; } |
| 642 | } |
| 643 | else if (!strcasecmp(p, "ffa")) { |
| 644 | if (type == game_type_ffa) { res = 1; break; } |
| 645 | } |
| 646 | else if (!strcasecmp(p, "oneonone")) { |
| 647 | if (type == game_type_oneonone) { res = 1; break; } |
| 648 | } |
| 649 | if (q) p = q + 1; |
| 650 | } while (q); |
| 651 | |
| 652 | free((void*)gametypes); |
| 653 | return res; |
| 654 | } |
| 655 | |
| 656 | static int game_sanity_check(t_game_result * results, t_account * * players, unsigned int count, unsigned int discisloss) |
| 657 | { |
| 658 | unsigned int winners = 0; |
| 659 | unsigned int losers = 0; |
| 660 | unsigned int draws = 0; |
| 661 | unsigned int discs = 0; |
| 662 | |
| 663 | for (unsigned int curr = 0; curr < count; curr++) |
| 664 | { |
| 665 | if (!players[curr]) |
| 666 | { |
| 667 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL player[{}] (of {})", curr, count); |
| 668 | return -1; |
| 669 | } |
| 670 | |
| 671 | switch (results[curr]) |
| 672 | { |
| 673 | case game_result_win: |
| 674 | winners++; |
| 675 | break; |
| 676 | case game_result_loss: |
| 677 | losers++; |
| 678 | break; |
| 679 | case game_result_draw: |
| 680 | draws++; |
| 681 | break; |
| 682 | case game_result_disconnect: |
| 683 | discs++; |
| 684 | break; |
| 685 | default: |
| 686 | eventlog(eventlog_level_error, __FUNCTION__, "bad results[{}]={}", curr, (unsigned int)results[curr]); |
| 687 | return -1; |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | if (draws > 0) |
| 692 | { |
| 693 | if (draws != count) |
| 694 | { |
| 695 | eventlog(eventlog_level_error, __FUNCTION__, "some, but not all players had a draw count={} (winners={} losers={} draws={})", count, winners, losers, draws); |
| 696 | return -1; |
| 697 | } |
| 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | if ((discisloss) && ((losers < 1) || (winners<1) || (winners>1 && (winners != losers)))) |
| 702 | { |
| 703 | eventlog(eventlog_level_info, __FUNCTION__, "missing winner or loser for count={} (winners={} losers={})", count, winners, losers); |
| 704 | return -1; |
| 705 | } |
| 706 | |
| 707 | return 0; |
| 708 | } |
| 709 | |
| 710 | static int game_report(t_game * game) |
| 711 | { |
| 712 | std::FILE * fp; |
| 713 | char * realname; |
| 714 | char * tempname; |
| 715 | unsigned int i; |
| 716 | unsigned int realcount; |
| 717 | t_ladder_info * ladder_info = NULL; |
| 718 | char clienttag_str[5]; |
| 719 | |
| 720 | if (!game) |
| 721 | { |
| 722 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 723 | return -1; |
| 724 | } |
| 725 | if (!game->clienttag) |
| 726 | { |
| 727 | eventlog(eventlog_level_error, __FUNCTION__, "got UNKNOWN clienttag"); |
| 728 | return -1; |
| 729 | } |
| 730 | if (!game->players) |
| 731 | { |
| 732 | eventlog(eventlog_level_error, __FUNCTION__, "player array is NULL"); |
| 733 | return -1; |
| 734 | } |
| 735 | if (!game->reported_results) |
| 736 | { |
| 737 | eventlog(eventlog_level_error, __FUNCTION__, "reported_results array is NULL"); |
| 738 | return -1; |
| 739 | } |
| 740 | if (!game->results) |
| 741 | { |
| 742 | eventlog(eventlog_level_error, __FUNCTION__, "results array is NULL"); |
| 743 | return -1; |
| 744 | } |
| 745 | |
| 746 | #ifdef WITH_LUA |
| 747 | lua_handle_game(game, NULL, luaevent_game_end); |
| 748 | #endif |
| 749 | |
| 750 | if (game->clienttag == CLIENTTAG_WARCRAFT3_UINT || game->clienttag == CLIENTTAG_WAR3XP_UINT) |
| 751 | // war3 game reporting is done elsewhere, so we can skip this function |
| 752 | return 0; |
| 753 | |
| 754 | if ((game->clienttag == CLIENTTAG_WCHAT_UINT) || (game->clienttag == CLIENTTAG_REDALERT_UINT) |
| 755 | || (game->clienttag == CLIENTTAG_DUNE2000_UINT) || (game->clienttag == CLIENTTAG_NOX_UINT) |
| 756 | || (game->clienttag == CLIENTTAG_NOXQUEST_UINT) || (game->clienttag == CLIENTTAG_RENEGADE_UINT) |
| 757 | || (game->clienttag == CLIENTTAG_RENGDFDS_UINT) || (game->clienttag == CLIENTTAG_EMPERORBD_UINT) |
| 758 | || (game->clienttag == CLIENTTAG_LOFLORE3_UINT) || (game->clienttag == CLIENTTAG_WWOL_UINT)) |
| 759 | // PELISH: We are not supporting ladders for all WOL clients yet |
| 760 | return 0; |
| 761 | |
| 762 | if (game->clienttag == CLIENTTAG_DIABLOSHR_UINT || |
| 763 | game->clienttag == CLIENTTAG_DIABLORTL_UINT || |
| 764 | game->clienttag == CLIENTTAG_DIABLO2ST_UINT || |
| 765 | game->clienttag == CLIENTTAG_DIABLO2DV_UINT || |
| 766 | game->clienttag == CLIENTTAG_DIABLO2XP_UINT) |
| 767 | { |
| 768 | if (prefs_get_report_diablo_games() == 1) |
| 769 | /* diablo games have transient players and no reported winners/losers */ |
| 770 | realcount = 0; |
| 771 | else |
| 772 | { |
| 773 | eventlog(eventlog_level_info, __FUNCTION__, "diablo gamereport disabled: ignoring game"); |
| 774 | return 0; |
| 775 | } |
| 776 | } |
| 777 | else |
| 778 | { |
| 779 | game_evaluate_results(game); // evaluate results from the reported results |
| 780 | /* "compact" the game; move all the real players to the top... */ |
| 781 | realcount = 0; |
| 782 | for (i = 0; i < game->count; i++) |
| 783 | { |
| 784 | if (!game->players[i]) |
| 785 | { |
| 786 | eventlog(eventlog_level_error, __FUNCTION__, "player slot {} has NULL account", i); |
| 787 | continue; |
| 788 | } |
| 789 | |
| 790 | if (game->results[i] != game_result_none) |
| 791 | { |
| 792 | game->players[realcount] = game->players[i]; |
| 793 | game->results[realcount] = game->results[i]; |
| 794 | game->report_heads[realcount] = game->report_heads[i]; |
| 795 | game->report_bodies[realcount] = game->report_bodies[i]; |
| 796 | realcount++; |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | /* then nuke duplicate players after the real players */ |
| 801 | for (i = realcount; i < game->count; i++) |
| 802 | { |
| 803 | game->players[i] = NULL; |
| 804 | game->results[i] = game_result_none; |
| 805 | game->report_heads[i] = NULL; |
| 806 | game->report_bodies[i] = NULL; |
| 807 | } |
| 808 | |
| 809 | if (realcount < 1) |
| 810 | { |
| 811 | eventlog(eventlog_level_info, __FUNCTION__, "ignoring game"); |
| 812 | return -1; |
| 813 | } |
| 814 | |
| 815 | if (game_sanity_check(game->results, game->players, realcount, game_is_ladder(game) && game_discisloss(game)) < 0) |
| 816 | { |
| 817 | eventlog(eventlog_level_info, __FUNCTION__, "game results ignored due to inconsistencies"); |
| 818 | game->bad = 1; |
| 819 | } |
| 820 | |
| 821 | if (((tag_check_wolv1(game->clienttag)) || (tag_check_wolv2(game->clienttag))) && (realcount == 2) |
| 822 | && (game->results[0] == game_result_disconnect) && (game->results[0] == game->results[1])) { |
| 823 | //FIXME: Find more general solution for that (we are not supporting more than 2 players now) |
| 824 | DEBUG0("WOL Both players got game_result_disconnect - ignoring game"); |
| 825 | game->bad = 1; |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | eventlog(eventlog_level_debug, __FUNCTION__, "realcount={} count={}", realcount, game->count); |
| 830 | |
| 831 | if (realcount >= 1 && !game->bad) |
| 832 | { |
| 833 | if (game_is_ladder(game)) |
| 834 | { |
| 835 | t_ladder_id id; |
| 836 | |
| 837 | if (game_get_type(game) == game_type_ironman) |
| 838 | id = ladder_id_ironman; |
| 839 | else |
| 840 | id = ladder_id_normal; |
| 841 | |
| 842 | for (i = 0; i < realcount; i++) |
| 843 | { |
| 844 | eventlog(eventlog_level_debug, __FUNCTION__, "realplayer {} result={}", i + 1, (unsigned int)game->results[i]); |
| 845 | |
| 846 | if ((tag_check_wolv1(game->clienttag)) || (tag_check_wolv2(game->clienttag))) { |
| 847 | id = ladder_id_solo; |
| 848 | ladder_init_account_wol(game->players[i], game->clienttag, id); |
| 849 | switch (game->results[i]) |
| 850 | { |
| 851 | case game_result_win: |
| 852 | account_inc_ladder_wins(game->players[i], game->clienttag, id); |
| 853 | break; |
| 854 | case game_result_loss: |
| 855 | account_inc_ladder_losses(game->players[i], game->clienttag, id); |
| 856 | break; |
| 857 | case game_result_disconnect: |
| 858 | account_inc_ladder_disconnects(game->players[i], game->clienttag, id); |
| 859 | break; |
| 860 | default: |
| 861 | eventlog(eventlog_level_error, __FUNCTION__, "bad ladder game realplayer results[{}] = {}", i, game->results[i]); |
| 862 | account_inc_ladder_disconnects(game->players[i], game->clienttag, id); |
| 863 | } |
| 864 | } |
| 865 | else { |
| 866 | ladder_init_account(game->players[i], game->clienttag, id); |
| 867 | switch (game->results[i]) |
| 868 | { |
| 869 | case game_result_win: |
| 870 | account_inc_ladder_wins(game->players[i], game->clienttag, id); |
| 871 | account_set_ladder_last_result(game->players[i], game->clienttag, id, game_result_get_str(game_result_win)); |
| 872 | break; |
| 873 | case game_result_loss: |
| 874 | account_inc_ladder_losses(game->players[i], game->clienttag, id); |
| 875 | account_set_ladder_last_result(game->players[i], game->clienttag, id, game_result_get_str(game_result_loss)); |
| 876 | break; |
| 877 | case game_result_draw: |
| 878 | account_inc_ladder_draws(game->players[i], game->clienttag, id); |
| 879 | account_set_ladder_last_result(game->players[i], game->clienttag, id, game_result_get_str(game_result_draw)); |
| 880 | break; |
| 881 | case game_result_disconnect: |
| 882 | account_inc_ladder_disconnects(game->players[i], game->clienttag, id); |
| 883 | account_set_ladder_last_result(game->players[i], game->clienttag, id, game_result_get_str(game_result_disconnect)); |
| 884 | break; |
| 885 | default: |
| 886 | eventlog(eventlog_level_error, __FUNCTION__, "bad ladder game realplayer results[{}] = {}", i, game->results[i]); |
| 887 | account_inc_ladder_disconnects(game->players[i], game->clienttag, id); |
| 888 | account_set_ladder_last_result(game->players[i], game->clienttag, id, game_result_get_str(game_result_disconnect)); |
| 889 | } |
| 890 | account_set_ladder_last_time(game->players[i], game->clienttag, id, bnettime()); |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | if ((tag_check_wolv1(game->clienttag)) || (tag_check_wolv2(game->clienttag))) { |
| 895 | id = ladder_id_solo; |
| 896 | ladder_update_wol(game->clienttag, id, game->players, game->results); |
| 897 | } |
| 898 | else { |
| 899 | ladder_info = (t_ladder_info*)xmalloc(sizeof(t_ladder_info)*realcount); |
| 900 | if (ladder_update(game->clienttag, id, |
| 901 | realcount, game->players, game->results, ladder_info) < 0) |
| 902 | { |
| 903 | eventlog(eventlog_level_info, __FUNCTION__, "unable to update ladder stats"); |
| 904 | xfree(ladder_info); |
| 905 | ladder_info = NULL; |
| 906 | } |
| 907 | } |
| 908 | } |
| 909 | else |
| 910 | { |
| 911 | if (!(tag_check_wolv1(game->clienttag)) || !(tag_check_wolv2(game->clienttag))) { |
| 912 | for (i = 0; i < realcount; i++) |
| 913 | { |
| 914 | switch (game->results[i]) |
| 915 | { |
| 916 | case game_result_win: |
| 917 | account_inc_normal_wins(game->players[i], game->clienttag); |
| 918 | account_set_normal_last_result(game->players[i], game->clienttag, game_result_get_str(game_result_win)); |
| 919 | break; |
| 920 | case game_result_loss: |
| 921 | account_inc_normal_losses(game->players[i], game->clienttag); |
| 922 | account_set_normal_last_result(game->players[i], game->clienttag, game_result_get_str(game_result_loss)); |
| 923 | break; |
| 924 | case game_result_draw: |
| 925 | account_inc_normal_draws(game->players[i], game->clienttag); |
| 926 | account_set_normal_last_result(game->players[i], game->clienttag, game_result_get_str(game_result_draw)); |
| 927 | break; |
| 928 | case game_result_disconnect: |
| 929 | account_inc_normal_disconnects(game->players[i], game->clienttag); |
| 930 | account_set_normal_last_result(game->players[i], game->clienttag, game_result_get_str(game_result_disconnect)); |
| 931 | break; |
| 932 | default: |
| 933 | eventlog(eventlog_level_error, __FUNCTION__, "bad normal game realplayer results[{}] = {}", i, game->results[i]); |
| 934 | account_inc_normal_disconnects(game->players[i], game->clienttag); |
| 935 | account_set_normal_last_result(game->players[i], game->clienttag, game_result_get_str(game_result_disconnect)); |
| 936 | } |
| 937 | account_set_normal_last_time(game->players[i], game->clienttag, bnettime()); |
| 938 | } |
| 939 | } |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | if (game_get_type(game) != game_type_ladder && prefs_get_report_all_games() != 1) |
| 944 | { |
| 945 | eventlog(eventlog_level_debug, __FUNCTION__, "not reporting normal games"); |
| 946 | |
| 947 | if (ladder_info) |
| 948 | { |
| 949 | xfree(ladder_info); |
| 950 | } |
| 951 | |
| 952 | return 0; |
| 953 | } |
| 954 | |
| 955 | { |
| 956 | struct std::tm * tmval; |
| 957 | char dstr[64]; |
| 958 | |
| 959 | if (!(tmval = std::localtime(&now))) |
| 960 | dstr[0] = '\0'; |
| 961 | else |
| 962 | std::sprintf(dstr, "%04d%02d%02d%02d%02d%02d", |
| 963 | 1900 + tmval->tm_year, |
| 964 | tmval->tm_mon + 1, |
| 965 | tmval->tm_mday, |
| 966 | tmval->tm_hour, |
| 967 | tmval->tm_min, |
| 968 | tmval->tm_sec); |
| 969 | |
| 970 | tempname = (char*)xmalloc(std::strlen(prefs_get_reportdir()) + 1 + 1 + 5 + 1 + 2 + 1 + std::strlen(dstr) + 1 + 6 + 1); |
| 971 | std::sprintf(tempname, "%s/_bnetd-gr_%s_%06u", prefs_get_reportdir(), dstr, game->id); |
| 972 | realname = (char*)xmalloc(std::strlen(prefs_get_reportdir()) + 1 + 2 + 1 + std::strlen(dstr) + 1 + 6 + 1); |
| 973 | std::sprintf(realname, "%s/gr_%s_%06u", prefs_get_reportdir(), dstr, game->id); |
| 974 | } |
| 975 | |
| 976 | if (!(fp = std::fopen(tempname, "w"))) |
| 977 | { |
| 978 | eventlog(eventlog_level_error, __FUNCTION__, "could not open report file \"{}\" for writing (std::fopen: {})", tempname, std::strerror(errno)); |
| 979 | if (ladder_info) |
| 980 | xfree(ladder_info); |
| 981 | xfree(realname); |
| 982 | xfree(tempname); |
| 983 | return -1; |
| 984 | } |
| 985 | |
| 986 | if (game->bad) |
| 987 | std::fprintf(fp, "[ game results ignored due to inconsistencies ]\n\n"); |
| 988 | std::fprintf(fp, "name=\"%s\" id=" GAMEID_FORMATF "\n", |
| 989 | game_get_name(game), |
| 990 | game->id); |
| 991 | std::fprintf(fp, "clienttag=%4s type=\"%s\" option=\"%s\"\n", |
| 992 | tag_uint_to_str(clienttag_str, game->clienttag), |
| 993 | game_type_get_str(game->type), |
| 994 | game_option_get_str(game->option)); |
| 995 | { |
| 996 | struct std::tm * gametime; |
| 997 | char timetemp[GAME_TIME_MAXLEN]; |
| 998 | |
| 999 | if (!(gametime = std::localtime(&game->create_time))) |
| 1000 | std::strcpy(timetemp, "?"); |
| 1001 | else |
| 1002 | std::strftime(timetemp, sizeof(timetemp), GAME_TIME_FORMAT, gametime); |
| 1003 | std::fprintf(fp, "created=\"%s\" ", timetemp); |
| 1004 | |
| 1005 | if (!(gametime = std::localtime(&game->start_time))) |
| 1006 | std::strcpy(timetemp, "?"); |
| 1007 | else |
| 1008 | std::strftime(timetemp, sizeof(timetemp), GAME_TIME_FORMAT, gametime); |
| 1009 | std::fprintf(fp, "started=\"%s\" ", timetemp); |
| 1010 | |
| 1011 | if (!(gametime = std::localtime(&now))) |
| 1012 | std::strcpy(timetemp, "?"); |
| 1013 | else |
| 1014 | std::strftime(timetemp, sizeof(timetemp), GAME_TIME_FORMAT, gametime); |
| 1015 | std::fprintf(fp, "ended=\"%s\"\n", timetemp); |
| 1016 | } |
| 1017 | { |
| 1018 | char const * mapname; |
| 1019 | |
| 1020 | if (!(mapname = game_get_mapname(game))) |
| 1021 | mapname = "?"; |
| 1022 | |
| 1023 | std::fprintf(fp, "mapfile=\"%s\" mapauth=\"%s\" mapsize=%ux%u tileset=\"%s\"\n", |
| 1024 | mapname, |
| 1025 | game_maptype_get_str(game_get_maptype(game)), |
| 1026 | game_get_mapsize_x(game), game_get_mapsize_y(game), |
| 1027 | game_tileset_get_str(game_get_tileset(game))); |
| 1028 | } |
| 1029 | std::fprintf(fp, "joins=%u maxplayers=%u\n", |
| 1030 | game_get_count(game), |
| 1031 | game_get_maxplayers(game)); |
| 1032 | |
| 1033 | if (!prefs_get_hide_addr()) |
| 1034 | std::fprintf(fp, "host=%s\n", addr_num_to_addr_str(game_get_addr(game), game_get_port(game))); |
| 1035 | |
| 1036 | std::fprintf(fp, "\n\n"); |
| 1037 | |
| 1038 | if (game->clienttag == CLIENTTAG_DIABLORTL_UINT) |
| 1039 | for (i = 0; i < game->count; i++) |
| 1040 | std::fprintf(fp, "%-16s JOINED\n", account_get_name(game->players[i])); |
| 1041 | else |
| 1042 | if (ladder_info) |
| 1043 | for (i = 0; i < realcount; i++) |
| 1044 | std::fprintf(fp, "%-16s %-8s rating=%u [#%05u] prob=%4.1f%% K=%2u adj=%+d\n", |
| 1045 | account_get_name(game->players[i]), |
| 1046 | game_result_get_str(game->results[i]), |
| 1047 | ladder_info[i].oldrating, |
| 1048 | ladder_info[i].oldrank, |
| 1049 | ladder_info[i].prob*100.0, |
| 1050 | ladder_info[i].k, |
| 1051 | ladder_info[i].adj); |
| 1052 | else |
| 1053 | for (i = 0; i < realcount; i++) |
| 1054 | std::fprintf(fp, "%-16s %-8s\n", |
| 1055 | account_get_name(game->players[i]), |
| 1056 | game_result_get_str(game->results[i])); |
| 1057 | std::fprintf(fp, "\n\n"); |
| 1058 | |
| 1059 | if (ladder_info) |
| 1060 | xfree(ladder_info); |
| 1061 | |
| 1062 | for (i = 0; i < realcount; i++) |
| 1063 | { |
| 1064 | if (game->report_heads[i]) |
| 1065 | std::fprintf(fp, "%s\n", game->report_heads[i]); |
| 1066 | else |
| 1067 | std::fprintf(fp, "[ game report header not available for player %u (\"%s\") ]\n", i + 1, account_get_name(game->players[i])); |
| 1068 | if (game->report_bodies[i]) |
| 1069 | std::fprintf(fp, "%s\n", game->report_bodies[i]); |
| 1070 | else |
| 1071 | std::fprintf(fp, "[ game report body not available for player %u (\"%s\") ]\n\n", i + 1, account_get_name(game->players[i])); |
| 1072 | } |
| 1073 | std::fprintf(fp, "\n\n"); |
| 1074 | |
| 1075 | if (game->clienttag == CLIENTTAG_STARCRAFT_UINT || |
| 1076 | game->clienttag == CLIENTTAG_SHAREWARE_UINT || |
| 1077 | game->clienttag == CLIENTTAG_BROODWARS_UINT || |
| 1078 | game->clienttag == CLIENTTAG_WARCIIBNE_UINT) |
| 1079 | { |
| 1080 | for (i = 0; i < realcount; i++) |
| 1081 | std::fprintf(fp, "%s's normal record is now %u/%u/%u (%u draws)\n", |
| 1082 | account_get_name(game->players[i]), |
| 1083 | account_get_normal_wins(game->players[i], game->clienttag), |
| 1084 | account_get_normal_losses(game->players[i], game->clienttag), |
| 1085 | account_get_normal_disconnects(game->players[i], game->clienttag), |
| 1086 | account_get_normal_draws(game->players[i], game->clienttag)); |
| 1087 | } |
| 1088 | if (game->clienttag == CLIENTTAG_STARCRAFT_UINT || |
| 1089 | game->clienttag == CLIENTTAG_BROODWARS_UINT || |
| 1090 | game->clienttag == CLIENTTAG_WARCIIBNE_UINT) |
| 1091 | { |
| 1092 | std::fprintf(fp, "\n"); |
| 1093 | for (i = 0; i < realcount; i++) |
| 1094 | std::fprintf(fp, "%s's standard ladder record is now %u/%u/%u (rating %u [#%05d]) (%u draws)\n", |
| 1095 | account_get_name(game->players[i]), |
| 1096 | account_get_ladder_wins(game->players[i], game->clienttag, ladder_id_normal), |
| 1097 | account_get_ladder_losses(game->players[i], game->clienttag, ladder_id_normal), |
| 1098 | account_get_ladder_disconnects(game->players[i], game->clienttag, ladder_id_normal), |
| 1099 | account_get_ladder_rating(game->players[i], game->clienttag, ladder_id_normal), |
| 1100 | account_get_ladder_rank(game->players[i], game->clienttag, ladder_id_normal), |
| 1101 | account_get_ladder_draws(game->players[i], game->clienttag, ladder_id_normal)); |
| 1102 | } |
| 1103 | if (game->clienttag == CLIENTTAG_WARCIIBNE_UINT) |
| 1104 | { |
| 1105 | std::fprintf(fp, "\n"); |
| 1106 | for (i = 0; i < realcount; i++) |
| 1107 | std::fprintf(fp, "%s's ironman ladder record is now %u/%u/%u (rating %u [#%05d]) (%u draws)\n", |
| 1108 | account_get_name(game->players[i]), |
| 1109 | account_get_ladder_wins(game->players[i], game->clienttag, ladder_id_ironman), |
| 1110 | account_get_ladder_losses(game->players[i], game->clienttag, ladder_id_ironman), |
| 1111 | account_get_ladder_disconnects(game->players[i], game->clienttag, ladder_id_ironman), |
| 1112 | account_get_ladder_rating(game->players[i], game->clienttag, ladder_id_ironman), |
| 1113 | account_get_ladder_rank(game->players[i], game->clienttag, ladder_id_ironman), |
| 1114 | account_get_ladder_draws(game->players[i], game->clienttag, ladder_id_ironman)); |
| 1115 | } |
| 1116 | |
| 1117 | std::fprintf(fp, "\nThis game lasted %lu minutes (elapsed).\n", ((unsigned long int)std::difftime(now, game->start_time)) / 60); |
| 1118 | |
| 1119 | #ifdef WITH_LUA |
| 1120 | lua_handle_game(game, NULL, luaevent_game_report); |
| 1121 | #endif |
| 1122 | |
| 1123 | if (std::fclose(fp) < 0) |
| 1124 | { |
| 1125 | eventlog(eventlog_level_error, __FUNCTION__, "could not close report file \"{}\" after writing (std::fclose: {})", tempname, std::strerror(errno)); |
| 1126 | xfree(realname); |
| 1127 | xfree(tempname); |
| 1128 | return -1; |
| 1129 | } |
| 1130 | |
| 1131 | if (p_rename(tempname, realname) < 0) |
| 1132 | { |
| 1133 | eventlog(eventlog_level_error, __FUNCTION__, "could not std::rename report file to \"{}\" (std::rename: {})", realname, std::strerror(errno)); |
| 1134 | xfree(realname); |
| 1135 | xfree(tempname); |
| 1136 | return -1; |
| 1137 | } |
| 1138 | |
| 1139 | eventlog(eventlog_level_debug, __FUNCTION__, "game report saved as \"{}\"", realname); |
| 1140 | xfree(realname); |
| 1141 | xfree(tempname); |
| 1142 | return 0; |
| 1143 | } |
| 1144 | |
| 1145 | |
| 1146 | extern unsigned int game_get_id(t_game const * game) |
| 1147 | { |
| 1148 | if (!game) |
| 1149 | { |
| 1150 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1151 | return 0; |
| 1152 | } |
| 1153 | return game->id; |
| 1154 | } |
| 1155 | |
| 1156 | |
| 1157 | extern char const * game_get_name(t_game const * game) |
| 1158 | { |
| 1159 | if (!game) |
| 1160 | { |
| 1161 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1162 | return NULL; |
| 1163 | } |
| 1164 | return game->name ? game->name : "BNet"; |
| 1165 | } |
| 1166 | |
| 1167 | |
| 1168 | extern t_game_type game_get_type(t_game const * game) |
| 1169 | { |
| 1170 | if (!game) |
| 1171 | { |
| 1172 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1173 | return game_type_none; |
| 1174 | } |
| 1175 | return game->type; |
| 1176 | } |
| 1177 | |
| 1178 | |
| 1179 | extern t_game_maptype game_get_maptype(t_game const * game) |
| 1180 | { |
| 1181 | if (!game) |
| 1182 | { |
| 1183 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1184 | return game_maptype_none; |
| 1185 | } |
| 1186 | return game->maptype; |
| 1187 | } |
| 1188 | |
| 1189 | |
| 1190 | extern int game_set_maptype(t_game * game, t_game_maptype maptype) |
| 1191 | { |
| 1192 | if (!game) |
| 1193 | { |
| 1194 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1195 | return -1; |
| 1196 | } |
| 1197 | game->maptype = maptype; |
| 1198 | return 0; |
| 1199 | } |
| 1200 | |
| 1201 | |
| 1202 | extern t_game_tileset game_get_tileset(t_game const * game) |
| 1203 | { |
| 1204 | if (!game) |
| 1205 | { |
| 1206 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1207 | return game_tileset_none; |
| 1208 | } |
| 1209 | return game->tileset; |
| 1210 | } |
| 1211 | |
| 1212 | |
| 1213 | extern int game_set_tileset(t_game * game, t_game_tileset tileset) |
| 1214 | { |
| 1215 | if (!game) |
| 1216 | { |
| 1217 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1218 | return -1; |
| 1219 | } |
| 1220 | game->tileset = tileset; |
| 1221 | return 0; |
| 1222 | } |
| 1223 | |
| 1224 | |
| 1225 | extern t_game_speed game_get_speed(t_game const * game) |
| 1226 | { |
| 1227 | if (!game) |
| 1228 | { |
| 1229 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1230 | return game_speed_none; |
| 1231 | } |
| 1232 | return game->speed; |
| 1233 | } |
| 1234 | |
| 1235 | |
| 1236 | extern int game_set_speed(t_game * game, t_game_speed speed) |
| 1237 | { |
| 1238 | if (!game) |
| 1239 | { |
| 1240 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1241 | return -1; |
| 1242 | } |
| 1243 | game->speed = speed; |
| 1244 | return 0; |
| 1245 | } |
| 1246 | |
| 1247 | |
| 1248 | extern unsigned int game_get_mapsize_x(t_game const * game) |
| 1249 | { |
| 1250 | if (!game) |
| 1251 | { |
| 1252 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1253 | return 0; |
| 1254 | } |
| 1255 | return game->mapsize_x; |
| 1256 | } |
| 1257 | |
| 1258 | |
| 1259 | extern int game_set_mapsize_x(t_game * game, unsigned int x) |
| 1260 | { |
| 1261 | if (!game) |
| 1262 | { |
| 1263 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1264 | return -1; |
| 1265 | } |
| 1266 | game->mapsize_x = x; |
| 1267 | return 0; |
| 1268 | } |
| 1269 | |
| 1270 | |
| 1271 | extern unsigned int game_get_mapsize_y(t_game const * game) |
| 1272 | { |
| 1273 | if (!game) |
| 1274 | { |
| 1275 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1276 | return 0; |
| 1277 | } |
| 1278 | return game->mapsize_y; |
| 1279 | } |
| 1280 | |
| 1281 | |
| 1282 | extern int game_set_mapsize_y(t_game * game, unsigned int y) |
| 1283 | { |
| 1284 | if (!game) |
| 1285 | { |
| 1286 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1287 | return -1; |
| 1288 | } |
| 1289 | game->mapsize_y = y; |
| 1290 | return 0; |
| 1291 | } |
| 1292 | |
| 1293 | |
| 1294 | extern unsigned int game_get_maxplayers(t_game const * game) |
| 1295 | { |
| 1296 | if (!game) |
| 1297 | { |
| 1298 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1299 | return 0; |
| 1300 | } |
| 1301 | return game->maxplayers; |
| 1302 | } |
| 1303 | |
| 1304 | |
| 1305 | extern int game_set_maxplayers(t_game * game, unsigned int maxplayers) |
| 1306 | { |
| 1307 | if (!game) |
| 1308 | { |
| 1309 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1310 | return -1; |
| 1311 | } |
| 1312 | game->maxplayers = maxplayers; |
| 1313 | return 0; |
| 1314 | } |
| 1315 | |
| 1316 | |
| 1317 | extern unsigned int game_get_difficulty(t_game const * game) |
| 1318 | { |
| 1319 | if (!game) |
| 1320 | { |
| 1321 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1322 | return 0; |
| 1323 | } |
| 1324 | return game->difficulty; |
| 1325 | } |
| 1326 | |
| 1327 | |
| 1328 | extern int game_set_difficulty(t_game * game, unsigned int difficulty) |
| 1329 | { |
| 1330 | if (!game) |
| 1331 | { |
| 1332 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1333 | return -1; |
| 1334 | } |
| 1335 | game->difficulty = difficulty; |
| 1336 | return 0; |
| 1337 | } |
| 1338 | |
| 1339 | |
| 1340 | extern char const * game_get_description(t_game const * game) |
| 1341 | { |
| 1342 | if (!game) |
| 1343 | { |
| 1344 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1345 | return NULL; |
| 1346 | } |
| 1347 | return game->description; |
| 1348 | } |
| 1349 | |
| 1350 | |
| 1351 | extern int game_set_description(t_game * game, char const * description) |
| 1352 | { |
| 1353 | if (!game) |
| 1354 | { |
| 1355 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1356 | return -1; |
| 1357 | } |
| 1358 | if (!description) |
| 1359 | { |
| 1360 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL description"); |
| 1361 | return -1; |
| 1362 | } |
| 1363 | |
| 1364 | if (game->description != NULL) xfree((void *)game->description); |
| 1365 | game->description = xstrdup(description); |
| 1366 | |
| 1367 | return 0; |
| 1368 | } |
| 1369 | |
| 1370 | |
| 1371 | extern char const * game_get_pass(t_game const * game) |
| 1372 | { |
| 1373 | if (!game) |
| 1374 | { |
| 1375 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1376 | return NULL; |
| 1377 | } |
| 1378 | return game->pass; |
| 1379 | } |
| 1380 | |
| 1381 | |
| 1382 | extern char const * game_get_info(t_game const * game) |
| 1383 | { |
| 1384 | if (!game) |
| 1385 | { |
| 1386 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1387 | return NULL; |
| 1388 | } |
| 1389 | return game->info; |
| 1390 | } |
| 1391 | |
| 1392 | |
| 1393 | extern int game_get_startver(t_game const * game) |
| 1394 | { |
| 1395 | if (!game) |
| 1396 | { |
| 1397 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1398 | return 0; |
| 1399 | } |
| 1400 | return game->startver; |
| 1401 | } |
| 1402 | |
| 1403 | |
| 1404 | extern unsigned long game_get_version(t_game const * game) |
| 1405 | { |
| 1406 | if (!game) |
| 1407 | { |
| 1408 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1409 | return 0; |
| 1410 | } |
| 1411 | return game->version; |
| 1412 | } |
| 1413 | |
| 1414 | |
| 1415 | extern unsigned int game_get_ref(t_game const * game) |
| 1416 | { |
| 1417 | if (!game) |
| 1418 | { |
| 1419 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1420 | return 0; |
| 1421 | } |
| 1422 | return game->ref; |
| 1423 | } |
| 1424 | |
| 1425 | |
| 1426 | extern unsigned int game_get_count(t_game const * game) |
| 1427 | { |
| 1428 | if (!game) |
| 1429 | { |
| 1430 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1431 | return 0; |
| 1432 | } |
| 1433 | return game->count; |
| 1434 | } |
| 1435 | |
| 1436 | |
| 1437 | extern void game_set_status(t_game * game, t_game_status status) |
| 1438 | { |
| 1439 | if (!game) { |
| 1440 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1441 | return; |
| 1442 | } |
| 1443 | // [quetzal] 20020829 - this should prevent invalid status changes |
| 1444 | // its like started game cant become open and so on |
| 1445 | if (game->status == game_status_started && |
| 1446 | (status == game_status_open || status == game_status_full || status == game_status_loaded)) { |
| 1447 | eventlog(eventlog_level_error, "game_set_status", |
| 1448 | "attempting to set status '{}' ({}) to started game", game_status_get_str(status), status); |
| 1449 | return; |
| 1450 | } |
| 1451 | |
| 1452 | if (game->status == game_status_done && status != game_status_done) { |
| 1453 | eventlog(eventlog_level_error, "game_set_status", |
| 1454 | "attempting to set status '{}' ({}) to done game", game_status_get_str(status), status); |
| 1455 | return; |
| 1456 | } |
| 1457 | |
| 1458 | if (status == game_status_started && game->start_time == (std::time_t)0) |
| 1459 | game->start_time = now; |
| 1460 | game->status = status; |
| 1461 | |
| 1462 | #ifdef WITH_LUA |
| 1463 | lua_handle_game(game, NULL, luaevent_game_changestatus); |
| 1464 | #endif |
| 1465 | } |
| 1466 | |
| 1467 | |
| 1468 | extern t_game_status game_get_status(t_game const * game) |
| 1469 | { |
| 1470 | if (!game) |
| 1471 | { |
| 1472 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1473 | return game_status_started; |
| 1474 | } |
| 1475 | return game->status; |
| 1476 | } |
| 1477 | |
| 1478 | |
| 1479 | extern unsigned int game_get_addr(t_game const * game) |
| 1480 | { |
| 1481 | if (!game) |
| 1482 | { |
| 1483 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1484 | return 0; |
| 1485 | } |
| 1486 | |
| 1487 | return game->addr; /* host byte order */ |
| 1488 | } |
| 1489 | |
| 1490 | |
| 1491 | extern unsigned short game_get_port(t_game const * game) |
| 1492 | { |
| 1493 | if (!game) |
| 1494 | { |
| 1495 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1496 | return 0; |
| 1497 | } |
| 1498 | |
| 1499 | return game->port; /* host byte order */ |
| 1500 | } |
| 1501 | |
| 1502 | |
| 1503 | extern unsigned int game_get_latency(t_game const * game) |
| 1504 | { |
| 1505 | if (!game) |
| 1506 | { |
| 1507 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1508 | return 0; |
| 1509 | } |
| 1510 | if (game->ref < 1) |
| 1511 | { |
| 1512 | eventlog(eventlog_level_error, __FUNCTION__, "game \"{}\" has no players", game_get_name(game)); |
| 1513 | return 0; |
| 1514 | } |
| 1515 | if (!game->players) |
| 1516 | { |
| 1517 | eventlog(eventlog_level_error, __FUNCTION__, "game \"{}\" has NULL players array (ref={})", game_get_name(game), game->ref); |
| 1518 | return 0; |
| 1519 | } |
| 1520 | if (!game->players[0]) |
| 1521 | { |
| 1522 | eventlog(eventlog_level_error, __FUNCTION__, "game \"{}\" has NULL players[0] entry (ref={})", game_get_name(game), game->ref); |
| 1523 | return 0; |
| 1524 | } |
| 1525 | |
| 1526 | return 0; /* conn_get_latency(game->players[0]); */ |
| 1527 | } |
| 1528 | |
| 1529 | extern t_connection * game_get_player_conn(t_game const * game, unsigned int i) |
| 1530 | { |
| 1531 | if (!game) |
| 1532 | { |
| 1533 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1534 | return NULL; |
| 1535 | } |
| 1536 | if (game->ref < 1) |
| 1537 | { |
| 1538 | eventlog(eventlog_level_error, __FUNCTION__, "game \"{}\" has no players", game_get_name(game)); |
| 1539 | return NULL; |
| 1540 | } |
| 1541 | if (!game->players) |
| 1542 | { |
| 1543 | eventlog(eventlog_level_error, __FUNCTION__, "game \"{}\" has NULL player array (ref={})", game_get_name(game), game->ref); |
| 1544 | return NULL; |
| 1545 | } |
| 1546 | if (!game->players[i]) |
| 1547 | { |
| 1548 | eventlog(eventlog_level_error, __FUNCTION__, "game \"{}\" has NULL players[i] entry (ref={})", game_get_name(game), game->ref); |
| 1549 | return NULL; |
| 1550 | } |
| 1551 | return game->connections[i]; |
| 1552 | } |
| 1553 | |
| 1554 | extern t_clienttag game_get_clienttag(t_game const * game) |
| 1555 | { |
| 1556 | if (!game) |
| 1557 | { |
| 1558 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1559 | return 0; |
| 1560 | } |
| 1561 | return game->clienttag; |
| 1562 | } |
| 1563 | |
| 1564 | |
| 1565 | extern int game_add_player(t_game * game, char const * pass, int startver, t_connection * c) |
| 1566 | { |
| 1567 | t_connection * * tempc; |
| 1568 | t_account * * tempp; |
| 1569 | t_game_result * tempr; |
| 1570 | t_game_result ** temprr; |
| 1571 | char const * * temprh; |
| 1572 | char const * * temprb; |
| 1573 | unsigned int i = 0; |
| 1574 | |
| 1575 | if (!game) |
| 1576 | { |
| 1577 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1578 | return -1; |
| 1579 | } |
| 1580 | if (!pass) |
| 1581 | { |
| 1582 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL password"); |
| 1583 | return -1; |
| 1584 | } |
| 1585 | if (startver != STARTVER_UNKNOWN && startver != STARTVER_GW1 && startver != STARTVER_GW3 && startver != STARTVER_GW4 && startver != STARTVER_REALM1) |
| 1586 | { |
| 1587 | eventlog(eventlog_level_error, __FUNCTION__, "got bad game startver {}", startver); |
| 1588 | return -1; |
| 1589 | } |
| 1590 | if (!c) |
| 1591 | { |
| 1592 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection"); |
| 1593 | return -1; |
| 1594 | } |
| 1595 | if (game->type == game_type_ladder && (account_get_normal_wins(conn_get_account(c), conn_get_clienttag(c)) < 10 && conn_get_wol(c) == 0)) |
| 1596 | /* if () ... */ |
| 1597 | { |
| 1598 | eventlog(eventlog_level_error, __FUNCTION__, "can not join ladder game without 10 normal wins"); |
| 1599 | return -1; |
| 1600 | } |
| 1601 | |
| 1602 | { |
| 1603 | t_clienttag gt; |
| 1604 | |
| 1605 | if (!(gt = game_get_clienttag(game))) |
| 1606 | { |
| 1607 | eventlog(eventlog_level_error, __FUNCTION__, "could not get clienttag for game"); |
| 1608 | return -1; |
| 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | if (game->pass[0] != '\0' && strcasecmp(game->pass, pass) != 0) |
| 1613 | { |
| 1614 | eventlog(eventlog_level_debug, __FUNCTION__, "game \"{}\" password mismatch \"{}\"!=\"{}\"", game_get_name(game), game->pass, pass); |
| 1615 | return -1; |
| 1616 | } |
| 1617 | |
| 1618 | if (game->connections && (game->count > 0)) |
| 1619 | { |
| 1620 | for (i = 0; i < game->count; i++) |
| 1621 | { |
| 1622 | if (game->connections[i] == NULL) |
| 1623 | { |
| 1624 | game->connections[i] = c; |
| 1625 | game->players[i] = conn_get_account(c); |
| 1626 | game->results[i] = game_result_none; |
| 1627 | game->reported_results[i] = NULL; |
| 1628 | game->report_heads[i] = NULL; |
| 1629 | game->report_bodies[i] = NULL; |
| 1630 | |
| 1631 | game->ref++; |
| 1632 | game->lastaccess_time = now; |
| 1633 | break; |
| 1634 | } |
| 1635 | } |
| 1636 | |
| 1637 | } |
| 1638 | |
| 1639 | // first player will be added here |
| 1640 | if ((i == game->count) || (game->count == 0)) |
| 1641 | { |
| 1642 | |
| 1643 | if (!game->connections) /* some std::realloc()s are broken */ |
| 1644 | tempc = (t_connection**)xmalloc((game->count + 1)*sizeof(t_connection *)); |
| 1645 | else |
| 1646 | tempc = (t_connection**)xrealloc(game->connections, (game->count + 1)*sizeof(t_connection *)); |
| 1647 | game->connections = tempc; |
| 1648 | if (!game->players) /* some std::realloc()s are broken */ |
| 1649 | tempp = (t_account**)xmalloc((game->count + 1)*sizeof(t_account *)); |
| 1650 | else |
| 1651 | tempp = (t_account**)xrealloc(game->players, (game->count + 1)*sizeof(t_account *)); |
| 1652 | game->players = tempp; |
| 1653 | |
| 1654 | if (!game->results) /* some std::realloc()s are broken */ |
| 1655 | tempr = (t_game_result*)xmalloc((game->count + 1)*sizeof(t_game_result)); |
| 1656 | else |
| 1657 | tempr = (t_game_result*)xrealloc(game->results, (game->count + 1)*sizeof(t_game_result)); |
| 1658 | game->results = tempr; |
| 1659 | |
| 1660 | if (!game->reported_results) |
| 1661 | temprr = (t_game_result**)xmalloc((game->count + 1)*sizeof(t_game_result *)); |
| 1662 | else |
| 1663 | temprr = (t_game_result**)xrealloc(game->reported_results, (game->count + 1)*sizeof(t_game_result *)); |
| 1664 | game->reported_results = temprr; |
| 1665 | |
| 1666 | if (!game->report_heads) /* some xrealloc()s are broken */ |
| 1667 | temprh = (const char**)xmalloc((game->count + 1)*sizeof(char const *)); |
| 1668 | else |
| 1669 | temprh = (const char**)xrealloc((void *)game->report_heads, (game->count + 1)*sizeof(char const *)); /* avoid compiler warning */ |
| 1670 | game->report_heads = temprh; |
| 1671 | |
| 1672 | if (!game->report_bodies) /* some xrealloc()s are broken */ |
| 1673 | temprb = (const char**)xmalloc((game->count + 1)*sizeof(char const *)); |
| 1674 | else |
| 1675 | temprb = (const char**)xrealloc((void *)game->report_bodies, (game->count + 1)*sizeof(char const *)); /* avoid compiler warning */ |
| 1676 | game->report_bodies = temprb; |
| 1677 | |
| 1678 | game->connections[game->count] = c; |
| 1679 | game->players[game->count] = conn_get_account(c); |
| 1680 | game->results[game->count] = game_result_none; |
| 1681 | game->reported_results[game->count] = NULL; |
| 1682 | game->report_heads[game->count] = NULL; |
| 1683 | game->report_bodies[game->count] = NULL; |
| 1684 | |
| 1685 | game->count++; |
| 1686 | game->ref++; |
| 1687 | game->lastaccess_time = now; |
| 1688 | |
| 1689 | } // end of "if ((i == game->count) || (game->count == 0))" |
| 1690 | |
| 1691 | if (game->startver != startver && startver != STARTVER_UNKNOWN) /* with join startver ALWAYS unknown [KWS] */ |
| 1692 | eventlog(eventlog_level_error, __FUNCTION__, "player \"{}\" client \"{}\" startver {} joining game startver {} (count={} ref={})", account_get_name(conn_get_account(c)), clienttag_uint_to_str(conn_get_clienttag(c)), startver, game->startver, game->count, game->ref); |
| 1693 | |
| 1694 | game_choose_host(game); |
| 1695 | |
| 1696 | return 0; |
| 1697 | } |
| 1698 | |
| 1699 | extern int game_del_player(t_game * game, t_connection * c) |
| 1700 | { |
| 1701 | char const * tname; |
| 1702 | unsigned int i; |
| 1703 | t_account * account; |
| 1704 | |
| 1705 | if (!game) |
| 1706 | { |
| 1707 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1708 | return -1; |
| 1709 | } |
| 1710 | if (!c) |
| 1711 | { |
| 1712 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection"); |
| 1713 | return -1; |
| 1714 | } |
| 1715 | if (!game->players) |
| 1716 | { |
| 1717 | eventlog(eventlog_level_error, __FUNCTION__, "player array is NULL"); |
| 1718 | return -1; |
| 1719 | } |
| 1720 | if (!game->reported_results) |
| 1721 | { |
| 1722 | eventlog(eventlog_level_error, __FUNCTION__, "reported results array is NULL"); |
| 1723 | return -1; |
| 1724 | } |
| 1725 | account = conn_get_account(c); |
| 1726 | |
| 1727 | if (conn_get_leavegamewhisper_ack(c) == 0) |
| 1728 | { |
| 1729 | watchlist->dispatch(conn_get_account(c), NULL, conn_get_clienttag(c), Watch::ET_leavegame); |
| 1730 | conn_set_leavegamewhisper_ack(c, 1); //1 = already whispered. We reset this each std::time user joins a channel |
| 1731 | } |
| 1732 | |
| 1733 | eventlog(eventlog_level_debug, __FUNCTION__, "game \"{}\" has ref={}, count={}; trying to remove player \"{}\"", game_get_name(game), game->ref, game->count, account_get_name(account)); |
| 1734 | |
| 1735 | for (i = 0; i < game->count; i++) |
| 1736 | if (game->players[i] == account && game->connections[i]) |
| 1737 | { |
| 1738 | eventlog(eventlog_level_debug, __FUNCTION__, "removing player #{} \"{}\" from \"{}\", {} players left", i, (tname = account_get_name(account)), game_get_name(game), game->ref - 1); |
| 1739 | game->connections[i] = NULL; |
| 1740 | if (!(game->reported_results[i])) |
| 1741 | eventlog(eventlog_level_debug, __FUNCTION__, "player \"{}\" left without reporting (valid) results", tname); |
| 1742 | |
| 1743 | eventlog(eventlog_level_debug, __FUNCTION__, "player deleted... (ref={})", game->ref); |
| 1744 | |
| 1745 | if (game->ref < 2) |
| 1746 | { |
| 1747 | eventlog(eventlog_level_debug, __FUNCTION__, "no more players, reporting game"); |
| 1748 | game_report(game); |
| 1749 | eventlog(eventlog_level_debug, __FUNCTION__, "no more players, destroying game"); |
| 1750 | game_destroy(game); |
| 1751 | return 0; |
| 1752 | } |
| 1753 | |
| 1754 | game->ref--; |
| 1755 | game->lastaccess_time = now; |
| 1756 | |
| 1757 | game_choose_host(game); |
| 1758 | |
| 1759 | #ifdef WITH_LUA |
| 1760 | lua_handle_game(game, c, luaevent_game_userleft); |
| 1761 | #endif |
| 1762 | return 0; |
| 1763 | } |
| 1764 | |
| 1765 | eventlog(eventlog_level_error, __FUNCTION__, "player \"{}\" was not in the game", account_get_name(account)); |
| 1766 | return -1; |
| 1767 | } |
| 1768 | |
| 1769 | extern t_account * game_get_player(t_game * game, unsigned int i) |
| 1770 | { |
| 1771 | if (!game) |
| 1772 | { |
| 1773 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1774 | return NULL; |
| 1775 | } |
| 1776 | |
| 1777 | if (!(i < game->count)) |
| 1778 | { |
| 1779 | eventlog(eventlog_level_error, __FUNCTION__, "requested illegal player id {}", i); |
| 1780 | return NULL; |
| 1781 | } |
| 1782 | |
| 1783 | return game->players[i]; |
| 1784 | } |
| 1785 | |
| 1786 | extern int game_set_report(t_game * game, t_account * account, char const * rephead, char const * repbody) |
| 1787 | { |
| 1788 | unsigned int pos; |
| 1789 | |
| 1790 | if (!game) |
| 1791 | { |
| 1792 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1793 | return -1; |
| 1794 | } |
| 1795 | if (!account) |
| 1796 | { |
| 1797 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL account"); |
| 1798 | return -1; |
| 1799 | } |
| 1800 | if (!game->players) |
| 1801 | { |
| 1802 | eventlog(eventlog_level_error, __FUNCTION__, "player array is NULL"); |
| 1803 | return -1; |
| 1804 | } |
| 1805 | if (!game->report_heads) |
| 1806 | { |
| 1807 | eventlog(eventlog_level_error, __FUNCTION__, "report_heads array is NULL"); |
| 1808 | return -1; |
| 1809 | } |
| 1810 | if (!game->report_bodies) |
| 1811 | { |
| 1812 | eventlog(eventlog_level_error, __FUNCTION__, "report_bodies array is NULL"); |
| 1813 | return -1; |
| 1814 | } |
| 1815 | if (!rephead) |
| 1816 | { |
| 1817 | eventlog(eventlog_level_error, __FUNCTION__, "report head is NULL"); |
| 1818 | return -1; |
| 1819 | } |
| 1820 | if (!repbody) |
| 1821 | { |
| 1822 | eventlog(eventlog_level_error, __FUNCTION__, "report body is NULL"); |
| 1823 | return -1; |
| 1824 | } |
| 1825 | |
| 1826 | { |
| 1827 | unsigned int i; |
| 1828 | |
| 1829 | pos = game->count; |
| 1830 | for (i = 0; i < game->count; i++) |
| 1831 | if (game->players[i] == account) |
| 1832 | pos = i; |
| 1833 | } |
| 1834 | if (pos == game->count) |
| 1835 | { |
| 1836 | eventlog(eventlog_level_error, __FUNCTION__, "could not find player \"{}\" to set result", account_get_name(account)); |
| 1837 | return -1; |
| 1838 | } |
| 1839 | |
| 1840 | game->report_heads[pos] = xstrdup(rephead); |
| 1841 | game->report_bodies[pos] = xstrdup(repbody); |
| 1842 | |
| 1843 | return 0; |
| 1844 | } |
| 1845 | |
| 1846 | extern int game_set_reported_results(t_game * game, t_account * account, t_game_result * results) |
| 1847 | { |
| 1848 | unsigned int i, j; |
| 1849 | t_game_result result; |
| 1850 | |
| 1851 | if (!game) |
| 1852 | { |
| 1853 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1854 | return -1; |
| 1855 | } |
| 1856 | |
| 1857 | if (!account) |
| 1858 | { |
| 1859 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL account"); |
| 1860 | return -1; |
| 1861 | } |
| 1862 | |
| 1863 | if (!results) |
| 1864 | { |
| 1865 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL results"); |
| 1866 | return -1; |
| 1867 | } |
| 1868 | |
| 1869 | if (!game->players) |
| 1870 | { |
| 1871 | eventlog(eventlog_level_error, __FUNCTION__, "player array is NULL"); |
| 1872 | return -1; |
| 1873 | } |
| 1874 | |
| 1875 | if (!game->reported_results) |
| 1876 | { |
| 1877 | eventlog(eventlog_level_error, __FUNCTION__, "reported_results array is NULL"); |
| 1878 | return -1; |
| 1879 | } |
| 1880 | |
| 1881 | for (i = 0; i < game->count; i++) |
| 1882 | { |
| 1883 | if ((game->players[i] == account)) break; |
| 1884 | } |
| 1885 | |
| 1886 | if (i == game->count) |
| 1887 | { |
| 1888 | eventlog(eventlog_level_error, __FUNCTION__, "could not find player \"{}\" to set reported results", account_get_name(account)); |
| 1889 | return -1; |
| 1890 | } |
| 1891 | |
| 1892 | if (game->reported_results[i]) |
| 1893 | { |
| 1894 | eventlog(eventlog_level_error, __FUNCTION__, "player \"{}\" already reported results - skipping this report", account_get_name(account)); |
| 1895 | return -1; |
| 1896 | } |
| 1897 | |
| 1898 | for (j = 0; j < game->count; j++) |
| 1899 | { |
| 1900 | result = results[j]; |
| 1901 | switch (result) |
| 1902 | { |
| 1903 | case game_result_win: |
| 1904 | case game_result_loss: |
| 1905 | case game_result_draw: |
| 1906 | case game_result_observer: |
| 1907 | case game_result_disconnect: |
| 1908 | break; |
| 1909 | case game_result_none: |
| 1910 | case game_result_playing: |
| 1911 | if (i != j) break; /* accept none/playing only from "others" */ |
| 1912 | default: /* result is invalid */ |
| 1913 | if (i != j) |
| 1914 | { |
| 1915 | eventlog(eventlog_level_error, __FUNCTION__, "ignoring bad reported result {} for player \"{}\"", (unsigned int)result, account_get_name(game->players[j])); |
| 1916 | results[i] = game_result_none; |
| 1917 | } |
| 1918 | else { |
| 1919 | eventlog(eventlog_level_error, __FUNCTION__, "got bad reported result {} for self - skipping results", (unsigned int)result); |
| 1920 | return -1; |
| 1921 | } |
| 1922 | } |
| 1923 | } |
| 1924 | |
| 1925 | game->reported_results[i] = results; |
| 1926 | |
| 1927 | return 0; |
| 1928 | } |
| 1929 | |
| 1930 | |
| 1931 | extern int game_set_self_report(t_game * game, t_account * account, t_game_result result) |
| 1932 | { |
| 1933 | unsigned int i; |
| 1934 | t_game_result * results; |
| 1935 | |
| 1936 | if (!game) |
| 1937 | { |
| 1938 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1939 | return -1; |
| 1940 | } |
| 1941 | |
| 1942 | if (!account) |
| 1943 | { |
| 1944 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL account"); |
| 1945 | return -1; |
| 1946 | } |
| 1947 | |
| 1948 | if (!game->players) |
| 1949 | { |
| 1950 | eventlog(eventlog_level_error, __FUNCTION__, "player array is NULL"); |
| 1951 | return -1; |
| 1952 | } |
| 1953 | |
| 1954 | if (!game->reported_results) |
| 1955 | { |
| 1956 | eventlog(eventlog_level_error, __FUNCTION__, "reported_results array is NULL"); |
| 1957 | return -1; |
| 1958 | } |
| 1959 | |
| 1960 | results = (t_game_result*)xmalloc(sizeof(t_game_result)*game->count); |
| 1961 | |
| 1962 | for (i = 0; i < game->count; i++) |
| 1963 | { |
| 1964 | if ((game->players[i] == account)) |
| 1965 | results[i] = result; |
| 1966 | else |
| 1967 | results[i] = game_result_none; |
| 1968 | } |
| 1969 | |
| 1970 | game_set_reported_results(game, account, results); |
| 1971 | |
| 1972 | return 0; |
| 1973 | } |
| 1974 | |
| 1975 | extern t_game_result * game_get_reported_results(t_game * game, t_account * account) |
| 1976 | { |
| 1977 | unsigned int i; |
| 1978 | |
| 1979 | if (!(game)) |
| 1980 | { |
| 1981 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 1982 | return NULL; |
| 1983 | } |
| 1984 | |
| 1985 | if (!(account)) |
| 1986 | { |
| 1987 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL account"); |
| 1988 | return NULL; |
| 1989 | } |
| 1990 | |
| 1991 | if (!(game->players)) |
| 1992 | { |
| 1993 | eventlog(eventlog_level_error, __FUNCTION__, "player array is NULL"); |
| 1994 | return NULL; |
| 1995 | } |
| 1996 | |
| 1997 | if (!(game->reported_results)) |
| 1998 | { |
| 1999 | eventlog(eventlog_level_error, __FUNCTION__, "reported_results array is NULL"); |
| 2000 | return NULL; |
| 2001 | } |
| 2002 | |
| 2003 | for (i = 0; i < game->count; i++) |
| 2004 | { |
| 2005 | if ((game->players[i] == account)) break; |
| 2006 | } |
| 2007 | |
| 2008 | if (i == game->count) |
| 2009 | { |
| 2010 | eventlog(eventlog_level_error, __FUNCTION__, "could not find player \"{}\" to set reported results", account_get_name(account)); |
| 2011 | return NULL; |
| 2012 | } |
| 2013 | |
| 2014 | if (!(game->reported_results[i])) |
| 2015 | { |
| 2016 | eventlog(eventlog_level_error, __FUNCTION__, "player \"{}\" has not reported any results", account_get_name(account)); |
| 2017 | return NULL; |
| 2018 | } |
| 2019 | |
| 2020 | return game->reported_results[i]; |
| 2021 | } |
| 2022 | |
| 2023 | |
| 2024 | extern char const * game_get_mapname(t_game const * game) |
| 2025 | { |
| 2026 | if (!game) |
| 2027 | { |
| 2028 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 2029 | return NULL; |
| 2030 | } |
| 2031 | |
| 2032 | return game->mapname; |
| 2033 | } |
| 2034 | |
| 2035 | |
| 2036 | extern int game_set_mapname(t_game * game, char const * mapname) |
| 2037 | { |
| 2038 | if (!game) |
| 2039 | { |
| 2040 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 2041 | return -1; |
| 2042 | } |
| 2043 | if (!mapname) |
| 2044 | { |
| 2045 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL mapname"); |
| 2046 | return -1; |
| 2047 | } |
| 2048 | |
| 2049 | if (game->mapname != NULL) xfree((void *)game->mapname); |
| 2050 | |
| 2051 | game->mapname = xstrdup(mapname); |
| 2052 | |
| 2053 | return 0; |
| 2054 | } |
| 2055 | |
| 2056 | |
| 2057 | extern t_connection * game_get_owner(t_game const * game) |
| 2058 | { |
| 2059 | if (!game) |
| 2060 | { |
| 2061 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 2062 | return NULL; |
| 2063 | } |
| 2064 | return game->owner; |
| 2065 | } |
| 2066 | |
| 2067 | |
| 2068 | extern std::time_t game_get_create_time(t_game const * game) |
| 2069 | { |
| 2070 | if (!game) |
| 2071 | { |
| 2072 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 2073 | return (std::time_t)0; |
| 2074 | } |
| 2075 | |
| 2076 | return game->create_time; |
| 2077 | } |
| 2078 | |
| 2079 | |
| 2080 | extern std::time_t game_get_start_time(t_game const * game) |
| 2081 | { |
| 2082 | if (!game) |
| 2083 | { |
| 2084 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 2085 | return (std::time_t)0; |
| 2086 | } |
| 2087 | |
| 2088 | return game->start_time; |
| 2089 | } |
| 2090 | |
| 2091 | |
| 2092 | extern int game_set_option(t_game * game, t_game_option option) |
| 2093 | { |
| 2094 | if (!game) |
| 2095 | { |
| 2096 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 2097 | return -1; |
| 2098 | } |
| 2099 | |
| 2100 | game->option = option; |
| 2101 | return 0; |
| 2102 | } |
| 2103 | |
| 2104 | |
| 2105 | extern t_game_option game_get_option(t_game const * game) |
| 2106 | { |
| 2107 | if (!game) |
| 2108 | { |
| 2109 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 2110 | return game_option_none; |
| 2111 | } |
| 2112 | |
| 2113 | return game->option; |
| 2114 | } |
| 2115 | |
| 2116 | |
| 2117 | extern int gamelist_create(void) |
| 2118 | { |
| 2119 | elist_init(&gamelist_head); |
| 2120 | glist_length = 0; |
| 2121 | return 0; |
| 2122 | } |
| 2123 | |
| 2124 | |
| 2125 | extern int gamelist_destroy(void) |
| 2126 | { |
| 2127 | /* FIXME: if called with active games, games are not freed */ |
| 2128 | elist_init(&gamelist_head); |
| 2129 | glist_length = 0; |
| 2130 | |
| 2131 | return 0; |
| 2132 | } |
| 2133 | |
| 2134 | extern t_elist * gamelist(void) |
| 2135 | { |
| 2136 | return &gamelist_head; |
| 2137 | } |
| 2138 | |
| 2139 | extern int gamelist_get_length(void) |
| 2140 | { |
| 2141 | return glist_length; |
| 2142 | } |
| 2143 | |
| 2144 | |
| 2145 | extern t_game * gamelist_find_game(char const * name, t_clienttag ctag, t_game_type type) |
| 2146 | { |
| 2147 | t_elist *curr; |
| 2148 | t_game *game; |
| 2149 | |
| 2150 | elist_for_each(curr, &gamelist_head) |
| 2151 | { |
| 2152 | game = elist_entry(curr, t_game, glist_link); |
| 2153 | if ((type == game_type_all || game->type == type) |
| 2154 | && ctag == game->clienttag |
| 2155 | && game->name |
| 2156 | && !strcasecmp(name, game->name)) return game; |
| 2157 | } |
| 2158 | |
| 2159 | return NULL; |
| 2160 | } |
| 2161 | |
| 2162 | |
| 2163 | extern t_game * gamelist_find_game_available(char const * name, t_clienttag ctag, t_game_type type) |
| 2164 | { |
| 2165 | t_elist *curr; |
| 2166 | t_game *game; |
| 2167 | t_game_status status; |
| 2168 | |
| 2169 | elist_for_each(curr, &gamelist_head) |
| 2170 | { |
| 2171 | game = elist_entry(curr, t_game, glist_link); |
| 2172 | status = game->status; |
| 2173 | |
| 2174 | if ((type == game_type_all || game->type == type) && (ctag == game->clienttag) && (game->name) |
| 2175 | && (!strcasecmp(name, game->name)) && (game->status != game_status_started) && |
| 2176 | (game->status != game_status_done)) |
| 2177 | return game; |
| 2178 | } |
| 2179 | |
| 2180 | return NULL; |
| 2181 | } |
| 2182 | |
| 2183 | extern t_game * gamelist_find_game_byid(unsigned int id) |
| 2184 | { |
| 2185 | t_elist *curr; |
| 2186 | t_game *game; |
| 2187 | |
| 2188 | elist_for_each(curr, &gamelist_head) |
| 2189 | { |
| 2190 | game = elist_entry(curr, t_game, glist_link); |
| 2191 | if (game->id == id) |
| 2192 | return game; |
| 2193 | } |
| 2194 | |
| 2195 | return NULL; |
| 2196 | } |
| 2197 | |
| 2198 | |
| 2199 | extern void gamelist_traverse(t_glist_func cb, void *data, t_gamelist_source_type gamelist_source) |
| 2200 | { |
| 2201 | t_elist *curr; |
| 2202 | |
| 2203 | #ifdef WITH_LUA |
| 2204 | t_game *game; |
| 2205 | |
| 2206 | if (gamelist_source == gamelist_source_joinbutton) |
| 2207 | { |
| 2208 | struct glist_cbdata *cbdata = (struct glist_cbdata*)data; |
| 2209 | // get gamelist from Lua script: pair(gameid=gamename) |
| 2210 | std::vector<t_game*> gamelist = lua_handle_game_list(cbdata->c); |
| 2211 | if (gamelist.size() > 0) |
| 2212 | { |
| 2213 | // display games to a user according to the given order |
| 2214 | for (std::vector<t_game*>::size_type i = 0; i != gamelist.size(); i++) |
| 2215 | { |
| 2216 | if (game = gamelist_find_game_byid(gamelist[i]->id)) |
| 2217 | { |
| 2218 | if (gamelist[i]->name) |
| 2219 | game->name = xstrdup(gamelist[i]->name); // override game name |
| 2220 | cb(game, data); // display game item |
| 2221 | } |
| 2222 | } |
| 2223 | // break next execution (override of display below gamelist) |
| 2224 | return; |
| 2225 | } |
| 2226 | } |
| 2227 | #endif |
| 2228 | elist_for_each(curr, &gamelist_head) |
| 2229 | { |
| 2230 | if (cb(elist_entry(curr, t_game, glist_link), data) < 0) return; |
| 2231 | } |
| 2232 | } |
| 2233 | |
| 2234 | |
| 2235 | extern int gamelist_total_games(void) |
| 2236 | { |
| 2237 | return totalcount; |
| 2238 | } |
| 2239 | |
| 2240 | extern int game_set_realm(t_game * game, unsigned int realm) |
| 2241 | { |
| 2242 | if (!game) |
| 2243 | { |
| 2244 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 2245 | return -1; |
| 2246 | } |
| 2247 | game->realm = realm; |
| 2248 | return 0; |
| 2249 | } |
| 2250 | |
| 2251 | extern unsigned int game_get_realm(t_game const * game) |
| 2252 | { |
| 2253 | if (!game) |
| 2254 | { |
| 2255 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 2256 | return 0; |
| 2257 | } |
| 2258 | return game->realm; |
| 2259 | } |
| 2260 | |
| 2261 | extern int game_set_realmname(t_game * game, char const * realmname) |
| 2262 | { |
| 2263 | char const * temp; |
| 2264 | |
| 2265 | if (!game) |
| 2266 | { |
| 2267 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 2268 | return -1; |
| 2269 | } |
| 2270 | |
| 2271 | if (realmname) |
| 2272 | temp = xstrdup(realmname); |
| 2273 | else |
| 2274 | temp = NULL; |
| 2275 | |
| 2276 | if (game->realmname) |
| 2277 | xfree((void *)game->realmname); /* avoid warning */ |
| 2278 | game->realmname = temp; |
| 2279 | return 0; |
| 2280 | } |
| 2281 | |
| 2282 | extern char const * game_get_realmname(t_game const * game) |
| 2283 | { |
| 2284 | if (!game) |
| 2285 | { |
| 2286 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 2287 | return NULL; |
| 2288 | } |
| 2289 | return game->realmname; |
| 2290 | } |
| 2291 | |
| 2292 | |
| 2293 | extern void gamelist_check_voidgame(void) |
| 2294 | { |
| 2295 | t_elist *curr, *save; |
| 2296 | t_game *game; |
| 2297 | |
| 2298 | elist_for_each_safe(curr, &gamelist_head, save) |
| 2299 | { |
| 2300 | game = elist_entry(curr, t_game, glist_link); |
| 2301 | if (!game->realm) |
| 2302 | continue; |
| 2303 | if (game->ref >= 1) |
| 2304 | continue; |
| 2305 | if ((now - game->lastaccess_time) > MAX_GAME_EMPTY_TIME) |
| 2306 | game_destroy(game); |
| 2307 | } |
| 2308 | } |
| 2309 | |
| 2310 | extern void game_set_flag(t_game * game, t_game_flag flag) |
| 2311 | { |
| 2312 | if (!game) |
| 2313 | { |
| 2314 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 2315 | return; |
| 2316 | } |
| 2317 | game->flag = flag; |
| 2318 | } |
| 2319 | |
| 2320 | |
| 2321 | extern t_game_flag game_get_flag(t_game const * game) |
| 2322 | { |
| 2323 | if (!game) |
| 2324 | { |
| 2325 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 2326 | return game_flag_none; |
| 2327 | } |
| 2328 | return game->flag; |
| 2329 | } |
| 2330 | |
| 2331 | extern int game_get_count_by_clienttag(t_clienttag ct) |
| 2332 | { |
| 2333 | t_game *game; |
| 2334 | t_elist *curr; |
| 2335 | int clienttaggames = 0; |
| 2336 | |
| 2337 | if (!ct) { |
| 2338 | eventlog(eventlog_level_error, __FUNCTION__, "got UNKNOWN clienttag"); |
| 2339 | return 0; |
| 2340 | } |
| 2341 | |
| 2342 | /* Get number of games for client tag specific */ |
| 2343 | elist_for_each(curr, &gamelist_head) |
| 2344 | { |
| 2345 | game = elist_entry(curr, t_game, glist_link); |
| 2346 | if (game_get_clienttag(game) == ct) |
| 2347 | clienttaggames++; |
| 2348 | } |
| 2349 | |
| 2350 | return clienttaggames; |
| 2351 | } |
| 2352 | |
| 2353 | static int game_match_name(const char *name, const char *prefix) |
| 2354 | { |
| 2355 | /* the easy cases */ |
| 2356 | if (!name || !*name) return 1; |
| 2357 | if (!prefix || !*prefix) return 1; |
| 2358 | |
| 2359 | if (!std::strncmp(name, prefix, std::strlen(prefix))) return 1; |
| 2360 | |
| 2361 | return 0; |
| 2362 | } |
| 2363 | |
| 2364 | extern int game_is_ladder(t_game *game) |
| 2365 | { |
| 2366 | assert(game); |
| 2367 | |
| 2368 | /* all normal ladder games are still counted as ladder games */ |
| 2369 | if (game->type == game_type_ladder || |
| 2370 | game->type == game_type_ironman) return 1; |
| 2371 | |
| 2372 | /* addition game types are also checked against gamename prefix if set */ |
| 2373 | if (game_match_type(game_get_type(game), prefs_get_ladder_games()) && |
| 2374 | game_match_name(game_get_name(game), prefs_get_ladder_prefix())) return 1; |
| 2375 | |
| 2376 | return 0; |
| 2377 | } |
| 2378 | |
| 2379 | extern int game_discisloss(t_game *game) |
| 2380 | { |
| 2381 | assert(game); |
| 2382 | |
| 2383 | if (prefs_get_discisloss()) |
| 2384 | return 1; |
| 2385 | |
| 2386 | /* all normal ladder games provide discisloss option themselves */ |
| 2387 | if (game->type == game_type_ladder || |
| 2388 | game->type == game_type_ironman) return game->option == game_option_ladder_countasloss; |
| 2389 | |
| 2390 | /* additional game types that are consideres ladder are always considered discasloss */ |
| 2391 | if (game_match_type(game_get_type(game), prefs_get_ladder_games()) && |
| 2392 | game_match_name(game_get_name(game), prefs_get_ladder_prefix())) return 1; |
| 2393 | |
| 2394 | /* all other games are handled as usual */ |
| 2395 | return game->option == game_option_ladder_countasloss; |
| 2396 | } |
| 2397 | |
| 2398 | extern int game_set_channel(t_game * game, t_channel * channel) |
| 2399 | { |
| 2400 | if (!game) { |
| 2401 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 2402 | return -1; |
| 2403 | } |
| 2404 | |
| 2405 | if (!channel) { |
| 2406 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL channel"); |
| 2407 | return -1; |
| 2408 | } |
| 2409 | |
| 2410 | game->channel = channel; |
| 2411 | return 0; |
| 2412 | } |
| 2413 | |
| 2414 | extern t_channel * game_get_channel(t_game * game) |
| 2415 | { |
| 2416 | if (!game) { |
| 2417 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL game"); |
| 2418 | return NULL; |
| 2419 | } |
| 2420 | |
| 2421 | return game->channel; |
| 2422 | |
| 2423 | } |
| 2424 | |
| 2425 | } |
| 2426 | |
| 2427 | } |