| 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or |
| 3 | * modify it under the terms of the GNU General Public License |
| 4 | * as published by the Free Software Foundation; either version 2 |
| 5 | * of the License, or (at your option) any later version. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | * You should have received a copy of the GNU General Public License |
| 13 | * along with this program; if not, write to the Free Software |
| 14 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 15 | */ |
| 16 | #include "common/setup_before.h" |
| 17 | #include "tournament.h" |
| 18 | |
| 19 | #include <cstdio> |
| 20 | #include <cerrno> |
| 21 | #include <cstring> |
| 22 | #include <ctime> |
| 23 | #include <cstdlib> |
| 24 | |
| 25 | #include "common/list.h" |
| 26 | #include "common/eventlog.h" |
| 27 | #include "common/xalloc.h" |
| 28 | #include "common/util.h" |
| 29 | #include "common/tag.h" |
| 30 | /* |
| 31 | #include "common/packet.h" |
| 32 | |
| 33 | */ |
| 34 | #include "anongame_maplists.h" |
| 35 | #include "account.h" |
| 36 | #include "common/setup_after.h" |
| 37 | |
| 38 | namespace pvpgn |
| 39 | { |
| 40 | |
| 41 | namespace bnetd |
| 42 | { |
| 43 | |
| 44 | static t_tournament_info * tournament_info = NULL; |
| 45 | static t_list * tournament_head = NULL; |
| 46 | |
| 47 | static int tournamentlist_create(void); |
| 48 | static int _gamelist_destroy(void); |
| 49 | static t_tournament_user * tournament_get_user(t_account * account); |
| 50 | //static int tournament_get_in_game_status(t_account * account); |
| 51 | |
| 52 | /*****/ |
| 53 | static int tournamentlist_create(void) |
| 54 | { |
| 55 | tournament_head = list_create(); |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | static int _gamelist_destroy(void) |
| 60 | { |
| 61 | t_elem * curr; |
| 62 | t_tournament_user * user; |
| 63 | |
| 64 | if (tournament_head) { |
| 65 | LIST_TRAVERSE(tournament_head, curr) |
| 66 | { |
| 67 | if (!(user = (t_tournament_user*)elem_get_data(curr))) { |
| 68 | eventlog(eventlog_level_error, __FUNCTION__, "tournament list contains NULL item"); |
| 69 | continue; |
| 70 | } |
| 71 | |
| 72 | if (list_remove_elem(tournament_head, &curr) < 0) |
| 73 | eventlog(eventlog_level_error, __FUNCTION__, "could not remove item from list"); |
| 74 | |
| 75 | if (user->name) |
| 76 | xfree((void *)user->name); /* avoid warning */ |
| 77 | |
| 78 | xfree(user); |
| 79 | |
| 80 | } |
| 81 | |
| 82 | if (list_destroy(tournament_head) < 0) |
| 83 | return -1; |
| 84 | tournament_head = NULL; |
| 85 | } |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | /*****/ |
| 90 | extern int tournament_check_client(t_clienttag clienttag) |
| 91 | { |
| 92 | if (clienttag == CLIENTTAG_WAR3XP_UINT && tournament_info->game_client == 2) |
| 93 | return 1; |
| 94 | if (clienttag == CLIENTTAG_WARCRAFT3_UINT && tournament_info->game_client == 1) |
| 95 | return 1; |
| 96 | |
| 97 | return -1; |
| 98 | } |
| 99 | |
| 100 | extern int tournament_signup_user(t_account * account) |
| 101 | { |
| 102 | t_tournament_user * user; |
| 103 | |
| 104 | if (!(account)) |
| 105 | return -1; |
| 106 | |
| 107 | if ((user = tournament_get_user(account))) { |
| 108 | eventlog(eventlog_level_info, __FUNCTION__, "user \"{}\" already signed up in tournament", account_get_name(account)); |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | user = (t_tournament_user*)xmalloc(sizeof(t_tournament_user)); |
| 113 | user->name = xstrdup(account_get_name(account)); |
| 114 | user->wins = 0; |
| 115 | user->losses = 0; |
| 116 | user->ties = 0; |
| 117 | user->in_game = 0; |
| 118 | user->in_finals = 0; |
| 119 | |
| 120 | list_prepend_data(tournament_head, user); |
| 121 | |
| 122 | eventlog(eventlog_level_info, __FUNCTION__, "added user \"{}\" to tournament", account_get_name(account)); |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static t_tournament_user * tournament_get_user(t_account * account) |
| 127 | { |
| 128 | t_elem const * curr; |
| 129 | t_tournament_user * user; |
| 130 | |
| 131 | if (tournament_head) |
| 132 | LIST_TRAVERSE(tournament_head, curr) |
| 133 | { |
| 134 | user = (t_tournament_user*)elem_get_data(curr); |
| 135 | if (std::strcmp(user->name, account_get_name(account)) == 0) |
| 136 | return user; |
| 137 | } |
| 138 | |
| 139 | return NULL; |
| 140 | } |
| 141 | |
| 142 | extern int tournament_user_signed_up(t_account * account) |
| 143 | { |
| 144 | if (!(tournament_get_user(account))) |
| 145 | return -1; |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | |
| 151 | extern int tournament_add_stat(t_account * account, int stat) |
| 152 | { |
| 153 | t_tournament_user * user; |
| 154 | |
| 155 | if (!(user = tournament_get_user(account))) |
| 156 | return -1; |
| 157 | |
| 158 | if (stat == 1) |
| 159 | user->wins++; |
| 160 | if (stat == 2) |
| 161 | user->losses++; |
| 162 | if (stat == 3) |
| 163 | user->ties++; |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | extern int tournament_get_stat(t_account * account, int stat) |
| 169 | { |
| 170 | t_tournament_user * user; |
| 171 | |
| 172 | if (!(user = tournament_get_user(account))) |
| 173 | return 0; |
| 174 | |
| 175 | if (stat == 1) |
| 176 | return user->wins; |
| 177 | if (stat == 2) |
| 178 | return user->losses; |
| 179 | if (stat == 3) |
| 180 | return user->ties; |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | extern int tournament_get_player_score(t_account * account) |
| 186 | { |
| 187 | t_tournament_user * user; |
| 188 | int score; |
| 189 | |
| 190 | if (!(user = tournament_get_user(account))) |
| 191 | return 0; |
| 192 | |
| 193 | score = user->wins * 3 + user->ties - user->losses; |
| 194 | |
| 195 | if (score < 0) |
| 196 | return 0; |
| 197 | |
| 198 | return score; |
| 199 | } |
| 200 | |
| 201 | extern int tournament_set_in_game_status(t_account * account, int status) |
| 202 | { |
| 203 | t_tournament_user * user; |
| 204 | |
| 205 | if (!(user = tournament_get_user(account))) |
| 206 | return -1; |
| 207 | |
| 208 | user->in_game = status; |
| 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | /* |
| 213 | static int tournament_get_in_game_status(t_account * account) |
| 214 | { |
| 215 | t_tournament_user * user; |
| 216 | |
| 217 | if (!(user = tournament_get_user(account))) |
| 218 | return 0; |
| 219 | |
| 220 | return user->in_game; |
| 221 | } |
| 222 | */ |
| 223 | extern int tournament_get_in_finals_status(t_account * account) |
| 224 | { |
| 225 | t_tournament_user * user; |
| 226 | |
| 227 | if (!(user = tournament_get_user(account))) |
| 228 | return 0; |
| 229 | |
| 230 | return user->in_finals; |
| 231 | } |
| 232 | |
| 233 | extern int tournament_get_game_in_progress(void) |
| 234 | { |
| 235 | t_elem const * curr; |
| 236 | t_tournament_user * user; |
| 237 | |
| 238 | if (tournament_head) |
| 239 | LIST_TRAVERSE_CONST(tournament_head, curr) |
| 240 | { |
| 241 | user = (t_tournament_user*)elem_get_data(curr); |
| 242 | if (user->in_game == 1) |
| 243 | return 1; |
| 244 | } |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | extern int tournament_is_arranged(void) |
| 250 | { |
| 251 | if (tournament_info->game_selection == 2) |
| 252 | return 1; |
| 253 | else |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | extern int tournament_get_totalplayers(void) |
| 258 | { |
| 259 | return tournament_info->game_type * 2; |
| 260 | } |
| 261 | |
| 262 | void tournament_check_date(unsigned int *mon, unsigned int *mday, unsigned int *year, unsigned int *hour, unsigned int *min, unsigned int *sec, char const * caller) |
| 263 | { |
| 264 | if (*mon > 12) |
| 265 | { |
| 266 | eventlog(eventlog_level_error, __FUNCTION__, "got invalid month ({}) in {}", *mon, caller); |
| 267 | *mon = 12; |
| 268 | } |
| 269 | if (*mday > 31) |
| 270 | { |
| 271 | eventlog(eventlog_level_error, __FUNCTION__, "got invalid mday ({}) in {}", *mday, caller); |
| 272 | *mday = 31; |
| 273 | } |
| 274 | if (*hour > 23) |
| 275 | { |
| 276 | eventlog(eventlog_level_error, __FUNCTION__, "got invalid hour ({}) from {}", *hour, caller); |
| 277 | *hour = 23; |
| 278 | } |
| 279 | if (*min > 59) |
| 280 | { |
| 281 | eventlog(eventlog_level_error, __FUNCTION__, "got invalid min ({}) from {}", *min, caller); |
| 282 | *min = 59; |
| 283 | } |
| 284 | if (*sec > 59) |
| 285 | { |
| 286 | eventlog(eventlog_level_error, __FUNCTION__, "got invalid sec ({}) from {}", *sec, caller); |
| 287 | *sec = 59; |
| 288 | } |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | /*****/ |
| 293 | extern int tournament_init(char const * filename) |
| 294 | { |
| 295 | std::FILE * fp; |
| 296 | unsigned int line, pos, mon, day, year, hour, min, sec; |
| 297 | char *buff, *temp, *pointer, *value; |
| 298 | char format[30]; |
| 299 | char *variable; |
| 300 | char *sponsor = NULL; |
| 301 | char *have_sponsor = NULL; |
| 302 | char *have_icon = NULL; |
| 303 | struct std::tm * timestamp = (struct std::tm*)xmalloc(sizeof(struct std::tm)); |
| 304 | |
| 305 | std::sprintf(format, "%%02u/%%02u/%%04u %%02u:%%02u:%%02u"); |
| 306 | |
| 307 | tournament_info = (t_tournament_info*)xmalloc(sizeof(t_tournament_info)); |
| 308 | tournament_info->start_preliminary = 0; |
| 309 | tournament_info->end_signup = 0; |
| 310 | tournament_info->end_preliminary = 0; |
| 311 | tournament_info->start_round_1 = 0; |
| 312 | tournament_info->start_round_2 = 0; |
| 313 | tournament_info->start_round_3 = 0; |
| 314 | tournament_info->start_round_4 = 0; |
| 315 | tournament_info->tournament_end = 0; |
| 316 | tournament_info->game_selection = 1; /* Default to PG */ |
| 317 | tournament_info->game_type = 1; /* Default to 1v1 */ |
| 318 | tournament_info->game_client = 2; /* Default to FT */ |
| 319 | tournament_info->races = 0x3F; /* Default to all races */ |
| 320 | tournament_info->format = xstrdup(""); |
| 321 | tournament_info->sponsor = xstrdup(""); |
| 322 | tournament_info->thumbs_down = 0; |
| 323 | |
| 324 | anongame_tournament_maplists_destroy(); |
| 325 | |
| 326 | if (!filename) { |
| 327 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL filename"); |
| 328 | xfree((void *)timestamp); |
| 329 | return -1; |
| 330 | } |
| 331 | |
| 332 | if (!(fp = std::fopen(filename, "r"))) { |
| 333 | eventlog(eventlog_level_error, __FUNCTION__, "could not open file \"{}\" for reading (std::fopen: {})", filename, std::strerror(errno)); |
| 334 | xfree((void *)timestamp); |
| 335 | return -1; |
| 336 | } |
| 337 | |
| 338 | for (line = 1; (buff = file_get_line(fp)); line++) { |
| 339 | for (pos = 0; buff[pos] == '\t' || buff[pos] == ' '; pos++); |
| 340 | if (buff[pos] == '\0' || buff[pos] == '#') { |
| 341 | continue; |
| 342 | } |
| 343 | if ((temp = std::strrchr(buff, '#'))) { |
| 344 | unsigned int len; |
| 345 | unsigned int endpos; |
| 346 | |
| 347 | *temp = '\0'; |
| 348 | len = std::strlen(buff) + 1; |
| 349 | for (endpos = len - 1; buff[endpos] == '\t' || buff[endpos] == ' '; endpos--); |
| 350 | buff[endpos + 1] = '\0'; |
| 351 | } |
| 352 | |
| 353 | if (std::strcmp(buff, "[MAPS]") == 0) { |
| 354 | char *clienttag, *mapname, *mname; |
| 355 | t_clienttag ctag; |
| 356 | |
| 357 | for (; (buff = file_get_line(fp));) { |
| 358 | for (pos = 0; buff[pos] == '\t' || buff[pos] == ' '; pos++); |
| 359 | if (buff[pos] == '\0' || buff[pos] == '#') { |
| 360 | continue; |
| 361 | } |
| 362 | if ((temp = std::strrchr(buff, '#'))) { |
| 363 | unsigned int len; |
| 364 | unsigned int endpos; |
| 365 | |
| 366 | *temp = '\0'; |
| 367 | len = std::strlen(buff) + 1; |
| 368 | for (endpos = len - 1; buff[endpos] == '\t' || buff[endpos] == ' '; endpos--); |
| 369 | buff[endpos + 1] = '\0'; |
| 370 | } |
| 371 | /* FIXME: use next_token() */ |
| 372 | if (!(clienttag = std::strtok(buff, " \t"))) { /* std::strtok modifies the string it is passed */ |
| 373 | continue; |
| 374 | } |
| 375 | if (std::strlen(clienttag) != 4) { |
| 376 | continue; |
| 377 | } |
| 378 | if (std::strcmp(buff, "[ENDMAPS]") == 0) { |
| 379 | break; |
| 380 | } |
| 381 | if (!(mapname = std::strtok(NULL, " \t"))) { |
| 382 | continue; |
| 383 | } |
| 384 | if (!tag_check_client((ctag = tag_case_str_to_uint(clienttag)))) { |
| 385 | continue; |
| 386 | } |
| 387 | mname = xstrdup(mapname); |
| 388 | |
| 389 | anongame_add_tournament_map(ctag, mname); |
| 390 | eventlog(eventlog_level_trace, __FUNCTION__, "added tournament map \"{}\" for {}", mname, clienttag); |
| 391 | xfree(mname); |
| 392 | } |
| 393 | } |
| 394 | else { |
| 395 | variable = buff; |
| 396 | pointer = std::strchr(variable, '='); |
| 397 | for (pointer--; pointer[0] == ' ' || pointer[0] == '\t'; pointer--); |
| 398 | pointer[1] = '\0'; |
| 399 | pointer++; |
| 400 | pointer++; |
| 401 | pointer = std::strchr(pointer, '='); |
| 402 | pointer++; |
| 403 | |
| 404 | if (std::strcmp(variable, "start_preliminary") == 0) { |
| 405 | pointer = std::strchr(pointer, '\"'); |
| 406 | pointer++; |
| 407 | value = pointer; |
| 408 | pointer = std::strchr(pointer, '\"'); |
| 409 | pointer[0] = '\0'; |
| 410 | |
| 411 | std::sscanf(value, format, &mon, &day, &year, &hour, &min, &sec); |
| 412 | |
| 413 | tournament_check_date(&mon, &day, &year, &hour, &min, &sec, variable); |
| 414 | |
| 415 | timestamp->tm_mon = mon - 1; |
| 416 | timestamp->tm_mday = day; |
| 417 | timestamp->tm_year = year - 1900; |
| 418 | timestamp->tm_hour = hour; |
| 419 | timestamp->tm_min = min; |
| 420 | timestamp->tm_sec = sec; |
| 421 | timestamp->tm_isdst = -1; |
| 422 | |
| 423 | tournament_info->start_preliminary = std::mktime(timestamp); |
| 424 | } |
| 425 | else if (std::strcmp(variable, "end_signup") == 0) { |
| 426 | pointer = std::strchr(pointer, '\"'); |
| 427 | pointer++; |
| 428 | value = pointer; |
| 429 | pointer = std::strchr(pointer, '\"'); |
| 430 | pointer[0] = '\0'; |
| 431 | |
| 432 | std::sscanf(value, format, &mon, &day, &year, &hour, &min, &sec); |
| 433 | |
| 434 | tournament_check_date(&mon, &day, &year, &hour, &min, &sec, variable); |
| 435 | |
| 436 | timestamp->tm_mon = mon - 1; |
| 437 | timestamp->tm_mday = day; |
| 438 | timestamp->tm_year = year - 1900; |
| 439 | timestamp->tm_hour = hour; |
| 440 | timestamp->tm_min = min; |
| 441 | timestamp->tm_sec = sec; |
| 442 | timestamp->tm_isdst = -1; |
| 443 | |
| 444 | tournament_info->end_signup = std::mktime(timestamp); |
| 445 | } |
| 446 | else if (std::strcmp(variable, "end_preliminary") == 0) { |
| 447 | pointer = std::strchr(pointer, '\"'); |
| 448 | pointer++; |
| 449 | value = pointer; |
| 450 | pointer = std::strchr(pointer, '\"'); |
| 451 | pointer[0] = '\0'; |
| 452 | |
| 453 | std::sscanf(value, format, &mon, &day, &year, &hour, &min, &sec); |
| 454 | |
| 455 | tournament_check_date(&mon, &day, &year, &hour, &min, &sec, variable); |
| 456 | |
| 457 | timestamp->tm_mon = mon - 1; |
| 458 | timestamp->tm_mday = day; |
| 459 | timestamp->tm_year = year - 1900; |
| 460 | timestamp->tm_hour = hour; |
| 461 | timestamp->tm_min = min; |
| 462 | timestamp->tm_sec = sec; |
| 463 | timestamp->tm_isdst = -1; |
| 464 | |
| 465 | tournament_info->end_preliminary = std::mktime(timestamp); |
| 466 | } |
| 467 | else if (std::strcmp(variable, "start_round_1") == 0) { |
| 468 | pointer = std::strchr(pointer, '\"'); |
| 469 | pointer++; |
| 470 | value = pointer; |
| 471 | pointer = std::strchr(pointer, '\"'); |
| 472 | pointer[0] = '\0'; |
| 473 | |
| 474 | std::sscanf(value, format, &mon, &day, &year, &hour, &min, &sec); |
| 475 | |
| 476 | tournament_check_date(&mon, &day, &year, &hour, &min, &sec, variable); |
| 477 | |
| 478 | timestamp->tm_mon = mon - 1; |
| 479 | timestamp->tm_mday = day; |
| 480 | timestamp->tm_year = year - 1900; |
| 481 | timestamp->tm_hour = hour; |
| 482 | timestamp->tm_min = min; |
| 483 | timestamp->tm_sec = sec; |
| 484 | timestamp->tm_isdst = -1; |
| 485 | |
| 486 | tournament_info->start_round_1 = std::mktime(timestamp); |
| 487 | } |
| 488 | else if (std::strcmp(variable, "start_round_2") == 0) { |
| 489 | pointer = std::strchr(pointer, '\"'); |
| 490 | pointer++; |
| 491 | value = pointer; |
| 492 | pointer = std::strchr(pointer, '\"'); |
| 493 | pointer[0] = '\0'; |
| 494 | |
| 495 | std::sscanf(value, format, &mon, &day, &year, &hour, &min, &sec); |
| 496 | |
| 497 | tournament_check_date(&mon, &day, &year, &hour, &min, &sec, variable); |
| 498 | |
| 499 | timestamp->tm_mon = mon - 1; |
| 500 | timestamp->tm_mday = day; |
| 501 | timestamp->tm_year = year - 1900; |
| 502 | timestamp->tm_hour = hour; |
| 503 | timestamp->tm_min = min; |
| 504 | timestamp->tm_sec = sec; |
| 505 | timestamp->tm_isdst = -1; |
| 506 | |
| 507 | tournament_info->start_round_2 = std::mktime(timestamp); |
| 508 | } |
| 509 | else if (std::strcmp(variable, "start_round_3") == 0) { |
| 510 | pointer = std::strchr(pointer, '\"'); |
| 511 | pointer++; |
| 512 | value = pointer; |
| 513 | pointer = std::strchr(pointer, '\"'); |
| 514 | pointer[0] = '\0'; |
| 515 | |
| 516 | std::sscanf(value, format, &mon, &day, &year, &hour, &min, &sec); |
| 517 | |
| 518 | tournament_check_date(&mon, &day, &year, &hour, &min, &sec, variable); |
| 519 | |
| 520 | timestamp->tm_mon = mon - 1; |
| 521 | timestamp->tm_mday = day; |
| 522 | timestamp->tm_year = year - 1900; |
| 523 | timestamp->tm_hour = hour; |
| 524 | timestamp->tm_min = min; |
| 525 | timestamp->tm_sec = sec; |
| 526 | timestamp->tm_isdst = -1; |
| 527 | |
| 528 | tournament_info->start_round_3 = std::mktime(timestamp); |
| 529 | } |
| 530 | else if (std::strcmp(variable, "start_round_4") == 0) { |
| 531 | pointer = std::strchr(pointer, '\"'); |
| 532 | pointer++; |
| 533 | value = pointer; |
| 534 | pointer = std::strchr(pointer, '\"'); |
| 535 | pointer[0] = '\0'; |
| 536 | |
| 537 | std::sscanf(value, format, &mon, &day, &year, &hour, &min, &sec); |
| 538 | |
| 539 | tournament_check_date(&mon, &day, &year, &hour, &min, &sec, variable); |
| 540 | |
| 541 | timestamp->tm_mon = mon - 1; |
| 542 | timestamp->tm_mday = day; |
| 543 | timestamp->tm_year = year - 1900; |
| 544 | timestamp->tm_hour = hour; |
| 545 | timestamp->tm_min = min; |
| 546 | timestamp->tm_sec = sec; |
| 547 | timestamp->tm_isdst = -1; |
| 548 | |
| 549 | tournament_info->start_round_4 = std::mktime(timestamp); |
| 550 | } |
| 551 | else if (std::strcmp(variable, "tournament_end") == 0) { |
| 552 | pointer = std::strchr(pointer, '\"'); |
| 553 | pointer++; |
| 554 | value = pointer; |
| 555 | pointer = std::strchr(pointer, '\"'); |
| 556 | pointer[0] = '\0'; |
| 557 | |
| 558 | std::sscanf(value, format, &mon, &day, &year, &hour, &min, &sec); |
| 559 | |
| 560 | tournament_check_date(&mon, &day, &year, &hour, &min, &sec, variable); |
| 561 | |
| 562 | timestamp->tm_mon = mon - 1; |
| 563 | timestamp->tm_mday = day; |
| 564 | timestamp->tm_year = year - 1900; |
| 565 | timestamp->tm_hour = hour; |
| 566 | timestamp->tm_min = min; |
| 567 | timestamp->tm_sec = sec; |
| 568 | timestamp->tm_isdst = -1; |
| 569 | |
| 570 | tournament_info->tournament_end = std::mktime(timestamp); |
| 571 | } |
| 572 | else if (std::strcmp(variable, "game_selection") == 0) { |
| 573 | if (std::atoi(pointer) >= 1 && std::atoi(pointer) <= 2) |
| 574 | tournament_info->game_selection = std::atoi(pointer); |
| 575 | } |
| 576 | else if (std::strcmp(variable, "game_type") == 0) { |
| 577 | if (std::atoi(pointer) >= 1 && std::atoi(pointer) <= 4) |
| 578 | tournament_info->game_type = std::atoi(pointer); |
| 579 | } |
| 580 | else if (std::strcmp(variable, "game_client") == 0) { |
| 581 | if (std::atoi(pointer) >= 1 && std::atoi(pointer) <= 2) |
| 582 | tournament_info->game_client = std::atoi(pointer); |
| 583 | } |
| 584 | else if (std::strcmp(variable, "format") == 0) { |
| 585 | pointer = std::strchr(pointer, '\"'); |
| 586 | pointer++; |
| 587 | value = pointer; |
| 588 | pointer = std::strchr(pointer, '\"'); |
| 589 | pointer[0] = '\0'; |
| 590 | |
| 591 | if (tournament_info->format) xfree((void *)tournament_info->format); |
| 592 | tournament_info->format = xstrdup(value); |
| 593 | } |
| 594 | else if (std::strcmp(variable, "races") == 0) { |
| 595 | unsigned int intvalue = 0; |
| 596 | unsigned int i; |
| 597 | |
| 598 | pointer = std::strchr(pointer, '\"'); |
| 599 | pointer++; |
| 600 | value = pointer; |
| 601 | pointer = std::strchr(pointer, '\"'); |
| 602 | pointer[0] = '\0'; |
| 603 | |
| 604 | size_t valuelen = std::strlen(value); |
| 605 | for (i = 0; i < valuelen; i++) { |
| 606 | if (value[i] == 'H') intvalue = intvalue | 0x01; |
| 607 | if (value[i] == 'O') intvalue = intvalue | 0x02; |
| 608 | if (value[i] == 'N') intvalue = intvalue | 0x04; |
| 609 | if (value[i] == 'U') intvalue = intvalue | 0x08; |
| 610 | if (value[i] == 'R') intvalue = intvalue | 0x20; |
| 611 | } |
| 612 | |
| 613 | if (intvalue == 0 || intvalue == 0x2F) |
| 614 | intvalue = 0x3F; /* hack to make all races availiable */ |
| 615 | |
| 616 | tournament_info->races = intvalue; |
| 617 | } |
| 618 | else if (std::strcmp(variable, "sponsor") == 0) { |
| 619 | pointer = std::strchr(pointer, '\"'); |
| 620 | pointer++; |
| 621 | value = pointer; |
| 622 | pointer = std::strchr(pointer, '\"'); |
| 623 | pointer[0] = '\0'; |
| 624 | |
| 625 | have_sponsor = xstrdup(value); |
| 626 | } |
| 627 | else if (std::strcmp(variable, "icon") == 0) { |
| 628 | pointer = std::strchr(pointer, '\"'); |
| 629 | pointer++; |
| 630 | value = pointer; |
| 631 | pointer = std::strchr(pointer, '\"'); |
| 632 | pointer[0] = '\0'; |
| 633 | |
| 634 | have_icon = xstrdup(value); |
| 635 | } |
| 636 | else if (std::strcmp(variable, "thumbs_down") == 0) { |
| 637 | tournament_info->thumbs_down = std::atoi(pointer); |
| 638 | } |
| 639 | else |
| 640 | eventlog(eventlog_level_error, __FUNCTION__, "bad option \"{}\" in \"{}\"", variable, filename); |
| 641 | |
| 642 | if (have_sponsor && have_icon) { |
| 643 | sponsor = (char*)xmalloc(std::strlen(have_sponsor) + 6); |
| 644 | |
| 645 | if (std::strlen(have_icon) == 4) |
| 646 | std::sprintf(sponsor, "%c%c%c%c,%s", have_icon[3], have_icon[2], have_icon[1], have_icon[0], have_sponsor); |
| 647 | else if (std::strlen(have_icon) == 2) |
| 648 | std::sprintf(sponsor, "%c%c3W,%s", have_icon[1], have_icon[0], have_sponsor); |
| 649 | else { |
| 650 | std::sprintf(sponsor, "PX3W,%s", have_sponsor); /* default to standard FT icon */ |
| 651 | eventlog(eventlog_level_warn, __FUNCTION__, "bad icon length, using W3XP"); |
| 652 | } |
| 653 | |
| 654 | if (tournament_info->sponsor) |
| 655 | xfree((void *)tournament_info->sponsor); |
| 656 | |
| 657 | tournament_info->sponsor = xstrdup(sponsor); |
| 658 | xfree((void *)have_sponsor); |
| 659 | xfree((void *)have_icon); |
| 660 | xfree((void *)sponsor); |
| 661 | have_sponsor = NULL; |
| 662 | have_icon = NULL; |
| 663 | } |
| 664 | } |
| 665 | } |
| 666 | if (have_sponsor) xfree((void *)have_sponsor); |
| 667 | if (have_icon) xfree((void *)have_icon); |
| 668 | xfree((void *)timestamp); |
| 669 | file_get_line(NULL); // clear file_get_line buffer |
| 670 | std::fclose(fp); |
| 671 | |
| 672 | /* check if we have timestamps for all the times */ |
| 673 | /* if not disable tournament by setting "start_preliminary" to 0 */ |
| 674 | if (tournament_info->end_signup == 0 || tournament_info->end_preliminary == 0 || |
| 675 | tournament_info->start_round_1 == 0 || tournament_info->start_round_2 == 0 || |
| 676 | tournament_info->start_round_3 == 0 || tournament_info->start_round_4 == 0 || |
| 677 | tournament_info->tournament_end == 0) { |
| 678 | tournament_info->start_preliminary = 0; |
| 679 | eventlog(eventlog_level_warn, __FUNCTION__, "one or more timestamps for tournaments is not valid, tournament has been disabled"); |
| 680 | } |
| 681 | else { |
| 682 | tournamentlist_create(); |
| 683 | } |
| 684 | |
| 685 | return 0; |
| 686 | } |
| 687 | |
| 688 | extern int tournament_destroy(void) |
| 689 | { |
| 690 | if (tournament_info) |
| 691 | { |
| 692 | if (tournament_info->format) xfree((void *)tournament_info->format); |
| 693 | if (tournament_info->sponsor) xfree((void *)tournament_info->sponsor); |
| 694 | xfree((void *)tournament_info); |
| 695 | } |
| 696 | tournament_info = NULL; |
| 697 | _gamelist_destroy(); |
| 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | extern int tournament_reload(char const * filename) |
| 702 | { |
| 703 | std::time_t tm; |
| 704 | std::time(&tm); |
| 705 | if ((tm >= tournament_info->start_preliminary) && (tm <= tournament_info->tournament_end)) |
| 706 | { |
| 707 | eventlog(eventlog_level_info, __FUNCTION__, "unable to reload tournament, tournament is in process"); |
| 708 | return -1; |
| 709 | } |
| 710 | tournament_destroy(); |
| 711 | return tournament_init(filename); |
| 712 | } |
| 713 | /*****/ |
| 714 | extern unsigned int tournament_get_start_preliminary(void) |
| 715 | { |
| 716 | return tournament_info->start_preliminary; |
| 717 | } |
| 718 | |
| 719 | extern unsigned int tournament_get_end_signup(void) |
| 720 | { |
| 721 | return tournament_info->end_signup; |
| 722 | } |
| 723 | |
| 724 | extern unsigned int tournament_get_end_preliminary(void) |
| 725 | { |
| 726 | return tournament_info->end_preliminary; |
| 727 | } |
| 728 | |
| 729 | extern unsigned int tournament_get_start_round_1(void) |
| 730 | { |
| 731 | return tournament_info->start_round_1; |
| 732 | } |
| 733 | |
| 734 | extern unsigned int tournament_get_start_round_2(void) |
| 735 | { |
| 736 | return tournament_info->start_round_2; |
| 737 | } |
| 738 | |
| 739 | extern unsigned int tournament_get_start_round_3(void) |
| 740 | { |
| 741 | return tournament_info->start_round_3; |
| 742 | } |
| 743 | |
| 744 | extern unsigned int tournament_get_start_round_4(void) |
| 745 | { |
| 746 | return tournament_info->start_round_4; |
| 747 | } |
| 748 | |
| 749 | extern unsigned int tournament_get_tournament_end(void) |
| 750 | { |
| 751 | return tournament_info->tournament_end; |
| 752 | } |
| 753 | |
| 754 | extern unsigned int tournament_get_game_selection(void) |
| 755 | { |
| 756 | return tournament_info->game_selection; |
| 757 | } |
| 758 | |
| 759 | extern unsigned int tournament_get_game_type(void) |
| 760 | { |
| 761 | return tournament_info->game_type; |
| 762 | } |
| 763 | |
| 764 | extern unsigned int tournament_get_races(void) |
| 765 | { |
| 766 | return tournament_info->races; |
| 767 | } |
| 768 | |
| 769 | extern char * tournament_get_format(void) |
| 770 | { |
| 771 | return tournament_info->format; |
| 772 | } |
| 773 | |
| 774 | extern char * tournament_get_sponsor(void) |
| 775 | { |
| 776 | return tournament_info->sponsor; |
| 777 | } |
| 778 | |
| 779 | extern unsigned int tournament_get_thumbs_down(void) |
| 780 | { |
| 781 | return tournament_info->thumbs_down; |
| 782 | } |
| 783 | |
| 784 | } |
| 785 | |
| 786 | } |
| 787 | |
| 788 | /****/ |