| 1 | /* |
| 2 | * (C) 2004 Olaf Freyer ([email protected]) |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | #include "common/setup_before.h" |
| 19 | #define TEAM_INTERNAL_ACCESS |
| 20 | #include "team.h" |
| 21 | |
| 22 | #include <cstring> |
| 23 | #include <cassert> |
| 24 | |
| 25 | #include "common/eventlog.h" |
| 26 | #include "common/list.h" |
| 27 | #include "common/bnet_protocol.h" |
| 28 | |
| 29 | #include "account.h" |
| 30 | #include "account_wrap.h" |
| 31 | #include "storage.h" |
| 32 | #include "ladder.h" |
| 33 | #include "server.h" |
| 34 | #include "common/setup_after.h" |
| 35 | |
| 36 | namespace pvpgn |
| 37 | { |
| 38 | |
| 39 | namespace bnetd |
| 40 | { |
| 41 | |
| 42 | static t_list *teamlist_head = NULL; |
| 43 | unsigned max_teamid = 0; |
| 44 | int teamlist_add_team(t_team * team); |
| 45 | |
| 46 | /* callback function for storage use */ |
| 47 | |
| 48 | static int _cb_load_teams(void *team) |
| 49 | { |
| 50 | if (teamlist_add_team((t_team*)team) < 0) |
| 51 | { |
| 52 | eventlog(eventlog_level_error, __FUNCTION__, "failed to add team to teamlist"); |
| 53 | return -1; |
| 54 | } |
| 55 | |
| 56 | if (((t_team *)team)->teamid > max_teamid) |
| 57 | max_teamid = ((t_team *)team)->teamid; |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | |
| 62 | int teamlist_add_team(t_team * team) |
| 63 | { |
| 64 | int i; |
| 65 | |
| 66 | if (!(team)) |
| 67 | { |
| 68 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL team"); |
| 69 | return -1; |
| 70 | } |
| 71 | |
| 72 | for (i = 0; i < team->size; i++) |
| 73 | { |
| 74 | if (!(team->members[i] = accountlist_find_account_by_uid(team->teammembers[i]))) |
| 75 | { |
| 76 | eventlog(eventlog_level_error, __FUNCTION__, "at least one non-existant member (id {}) in team {} - discarding team", team->teammembers[i], team->teamid); |
| 77 | //FIXME: delete team file now??? |
| 78 | return team->teamid; //we return teamid even though we have an error, we don't want unintentional overwriting |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | for (i = 0; i < team->size; i++) |
| 83 | account_add_team(team->members[i], team); |
| 84 | |
| 85 | if (!(team->teamid)) |
| 86 | team->teamid = ++max_teamid; |
| 87 | |
| 88 | list_append_data(teamlist_head, team); |
| 89 | |
| 90 | return team->teamid; |
| 91 | } |
| 92 | |
| 93 | |
| 94 | int teamlist_load(void) |
| 95 | { |
| 96 | // make sure to unload previous teamlist before loading again |
| 97 | if (teamlist_head) |
| 98 | teamlist_unload(); |
| 99 | |
| 100 | teamlist_head = list_create(); |
| 101 | |
| 102 | storage->load_teams(_cb_load_teams); |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | int teamlist_unload(void) |
| 109 | { |
| 110 | t_elem *curr; |
| 111 | t_team *team; |
| 112 | |
| 113 | if ((teamlist_head)) |
| 114 | { |
| 115 | LIST_TRAVERSE(teamlist_head, curr) |
| 116 | { |
| 117 | if (!(team = (t_team*)elem_get_data(curr))) |
| 118 | { |
| 119 | eventlog(eventlog_level_error, __FUNCTION__, "found NULL entry in list"); |
| 120 | continue; |
| 121 | } |
| 122 | xfree((void *)team); |
| 123 | list_remove_elem(teamlist_head, &curr); |
| 124 | } |
| 125 | |
| 126 | if (list_destroy(teamlist_head) < 0) |
| 127 | return -1; |
| 128 | |
| 129 | teamlist_head = NULL; |
| 130 | } |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | int teams_destroy(t_list * teams) |
| 136 | { |
| 137 | t_elem *curr; |
| 138 | t_team *team; |
| 139 | |
| 140 | if ((teams)) |
| 141 | { |
| 142 | LIST_TRAVERSE(teams, curr) |
| 143 | { |
| 144 | if (!(team = (t_team*)elem_get_data(curr))) |
| 145 | { |
| 146 | eventlog(eventlog_level_error, __FUNCTION__, "found NULL entry in list"); |
| 147 | continue; |
| 148 | } |
| 149 | list_remove_elem(teams, &curr); |
| 150 | } |
| 151 | |
| 152 | if (list_destroy(teams) < 0) |
| 153 | return -1; |
| 154 | } |
| 155 | teams = NULL; |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | t_team* create_team(t_account **accounts, t_clienttag clienttag) |
| 161 | { |
| 162 | t_team * team; |
| 163 | int i; |
| 164 | unsigned char size; |
| 165 | |
| 166 | team = (t_team*)xmalloc(sizeof(t_team)); |
| 167 | std::memset(team, 0, sizeof(t_team)); |
| 168 | size = 0; |
| 169 | |
| 170 | for (i = 0; i < MAX_TEAMSIZE; i++) |
| 171 | { |
| 172 | team->members[i] = accounts[i]; |
| 173 | if ((accounts[i])) |
| 174 | { |
| 175 | team->teammembers[i] = account_get_uid(accounts[i]); |
| 176 | size++; |
| 177 | } |
| 178 | } |
| 179 | team->size = size; |
| 180 | team->clienttag = clienttag; |
| 181 | |
| 182 | _cb_load_teams(team); |
| 183 | |
| 184 | storage->write_team(team); |
| 185 | |
| 186 | return team; |
| 187 | } |
| 188 | |
| 189 | void dispose_team(t_team * team) |
| 190 | { |
| 191 | if ((team)) |
| 192 | xfree((void *)team); |
| 193 | team = NULL; |
| 194 | } |
| 195 | |
| 196 | t_team * teamlist_find_team_by_accounts(t_account **accounts, t_clienttag clienttag) |
| 197 | { |
| 198 | return _list_find_team_by_accounts(accounts, clienttag, teamlist_head); |
| 199 | } |
| 200 | |
| 201 | t_team * _list_find_team_by_accounts(t_account **accounts, t_clienttag clienttag, t_list * teamlist) |
| 202 | { |
| 203 | t_elem *curr; |
| 204 | t_team *cteam; |
| 205 | int i, j, found; |
| 206 | unsigned char size; |
| 207 | |
| 208 | assert(accounts); |
| 209 | |
| 210 | found = 0; |
| 211 | |
| 212 | if ((teamlist)) |
| 213 | { |
| 214 | LIST_TRAVERSE(teamlist, curr) |
| 215 | { |
| 216 | if (!(cteam = (t_team*)elem_get_data(curr))) |
| 217 | { |
| 218 | eventlog(eventlog_level_error, __FUNCTION__, "found NULL entry in list"); |
| 219 | continue; |
| 220 | } |
| 221 | size = 0; |
| 222 | |
| 223 | for (i = 0; i < MAX_TEAMSIZE; i++) |
| 224 | { |
| 225 | if (!(accounts[i])) |
| 226 | break; |
| 227 | |
| 228 | size++; |
| 229 | found = 0; |
| 230 | for (j = 0; j < MAX_TEAMSIZE; j++) |
| 231 | { |
| 232 | if ((accounts[i] == cteam->members[j])) |
| 233 | { |
| 234 | found = 1; |
| 235 | break; |
| 236 | } |
| 237 | } |
| 238 | if (!(found)) break; |
| 239 | } |
| 240 | if ((found) && (clienttag == cteam->clienttag) && (size == cteam->size)) |
| 241 | return cteam; |
| 242 | } |
| 243 | |
| 244 | } |
| 245 | |
| 246 | return NULL; |
| 247 | } |
| 248 | |
| 249 | t_team * teamlist_find_team_by_uids(unsigned int * uids, t_clienttag clienttag) |
| 250 | { |
| 251 | return _list_find_team_by_uids(uids, clienttag, teamlist_head); |
| 252 | } |
| 253 | |
| 254 | t_team * _list_find_team_by_uids(unsigned int * uids, t_clienttag clienttag, t_list * teamlist) |
| 255 | { |
| 256 | t_elem *curr; |
| 257 | t_team *cteam; |
| 258 | int i, j, found; |
| 259 | unsigned char size; |
| 260 | |
| 261 | assert(uids); |
| 262 | |
| 263 | found = 0; |
| 264 | |
| 265 | if ((teamlist)) |
| 266 | { |
| 267 | LIST_TRAVERSE(teamlist, curr) |
| 268 | { |
| 269 | if (!(cteam = (t_team*)elem_get_data(curr))) |
| 270 | { |
| 271 | eventlog(eventlog_level_error, __FUNCTION__, "found NULL entry in list"); |
| 272 | continue; |
| 273 | } |
| 274 | size = 0; |
| 275 | |
| 276 | for (i = 0; i < MAX_TEAMSIZE; i++) |
| 277 | { |
| 278 | if (!(uids[i])) |
| 279 | break; |
| 280 | |
| 281 | found = 0; |
| 282 | for (j = 0; j < MAX_TEAMSIZE; j++) |
| 283 | { |
| 284 | if ((uids[i] == cteam->teammembers[j])) |
| 285 | { |
| 286 | found = 1; |
| 287 | break; |
| 288 | } |
| 289 | } |
| 290 | if (!(found)) break; |
| 291 | } |
| 292 | if ((found) && (clienttag == cteam->clienttag) && (size == cteam->size)) |
| 293 | return cteam; |
| 294 | } |
| 295 | |
| 296 | } |
| 297 | |
| 298 | return NULL; |
| 299 | } |
| 300 | |
| 301 | t_team * teamlist_find_team_by_teamid(unsigned int teamid) |
| 302 | { |
| 303 | return _list_find_team_by_teamid(teamid, teamlist_head); |
| 304 | } |
| 305 | |
| 306 | t_team* _list_find_team_by_teamid(unsigned int teamid, t_list * teamlist) |
| 307 | { |
| 308 | t_elem * curr; |
| 309 | t_team * cteam; |
| 310 | |
| 311 | assert(teamid); |
| 312 | |
| 313 | if ((teamlist)) |
| 314 | { |
| 315 | LIST_TRAVERSE(teamlist, curr) |
| 316 | { |
| 317 | if (!(cteam = (t_team*)elem_get_data(curr))) |
| 318 | { |
| 319 | eventlog(eventlog_level_error, __FUNCTION__, "found NULL entry in list"); |
| 320 | continue; |
| 321 | } |
| 322 | |
| 323 | if ((cteam->teamid == teamid)) |
| 324 | return cteam; |
| 325 | |
| 326 | } |
| 327 | } |
| 328 | return NULL; |
| 329 | } |
| 330 | |
| 331 | unsigned int team_get_teamid(t_team * team) |
| 332 | { |
| 333 | assert(team); |
| 334 | |
| 335 | return team->teamid; |
| 336 | } |
| 337 | |
| 338 | t_account * team_get_member(t_team * team, int count) |
| 339 | { |
| 340 | assert(team); |
| 341 | assert(count >= 0); |
| 342 | assert(count < MAX_TEAMSIZE); |
| 343 | |
| 344 | return team->members[count]; |
| 345 | } |
| 346 | |
| 347 | unsigned int team_get_memberuid(t_team * team, int count) |
| 348 | { |
| 349 | assert(team); |
| 350 | assert(count >= 0); |
| 351 | assert(count < MAX_TEAMSIZE); |
| 352 | |
| 353 | return team->teammembers[count]; |
| 354 | } |
| 355 | |
| 356 | |
| 357 | t_clienttag team_get_clienttag(t_team * team) |
| 358 | { |
| 359 | assert(team); |
| 360 | |
| 361 | return team->clienttag; |
| 362 | } |
| 363 | |
| 364 | unsigned char team_get_size(t_team * team) |
| 365 | { |
| 366 | assert(team); |
| 367 | |
| 368 | return team->size; |
| 369 | } |
| 370 | |
| 371 | int team_get_wins(t_team * team) |
| 372 | { |
| 373 | assert(team); |
| 374 | |
| 375 | return team->wins; |
| 376 | } |
| 377 | |
| 378 | int team_get_losses(t_team * team) |
| 379 | { |
| 380 | assert(team); |
| 381 | |
| 382 | return team->losses; |
| 383 | } |
| 384 | |
| 385 | int team_get_xp(t_team * team) |
| 386 | { |
| 387 | assert(team); |
| 388 | |
| 389 | return team->xp; |
| 390 | } |
| 391 | |
| 392 | int team_get_level(t_team * team) |
| 393 | { |
| 394 | assert(team); |
| 395 | |
| 396 | return team->level; |
| 397 | } |
| 398 | |
| 399 | int team_set_rank(t_team * team, unsigned int rank) |
| 400 | { |
| 401 | assert(team); |
| 402 | team->rank = rank; |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | int team_get_rank(t_team * team) |
| 407 | { |
| 408 | assert(team); |
| 409 | |
| 410 | return team->rank; |
| 411 | } |
| 412 | |
| 413 | time_t team_get_lastgame(t_team * team) |
| 414 | { |
| 415 | assert(team); |
| 416 | |
| 417 | return team->lastgame; |
| 418 | } |
| 419 | |
| 420 | int team_inc_wins(t_team * team) |
| 421 | { |
| 422 | assert(team); |
| 423 | |
| 424 | team->wins++; |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | int team_inc_losses(t_team * team) |
| 429 | { |
| 430 | assert(team); |
| 431 | |
| 432 | team->losses++; |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | int team_update_lastgame(t_team * team) |
| 437 | { |
| 438 | assert(team); |
| 439 | |
| 440 | team->lastgame = now; |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | extern int team_update_xp(t_team * team, int gameresult, unsigned int opponlevel, int * xp_diff) |
| 445 | { |
| 446 | int xp; |
| 447 | int mylevel; |
| 448 | int xpdiff = 0, placeholder; |
| 449 | |
| 450 | xp = team->xp; //get current xp |
| 451 | if (xp < 0) { |
| 452 | eventlog(eventlog_level_error, __FUNCTION__, "got negative XP"); |
| 453 | return -1; |
| 454 | } |
| 455 | |
| 456 | mylevel = team->level; //get teams level |
| 457 | if (mylevel > W3_XPCALC_MAXLEVEL) { |
| 458 | eventlog(eventlog_level_error, __FUNCTION__, "got invalid level: {}", mylevel); |
| 459 | return -1; |
| 460 | } |
| 461 | |
| 462 | if (mylevel <= 0) //if level is 0 then set it to 1 |
| 463 | mylevel = 1; |
| 464 | |
| 465 | if (opponlevel < 1) opponlevel = 1; |
| 466 | |
| 467 | switch (gameresult) |
| 468 | { |
| 469 | case W3_GAMERESULT_WIN: |
| 470 | ladder_war3_xpdiff(mylevel, opponlevel, &xpdiff, &placeholder); break; |
| 471 | case W3_GAMERESULT_LOSS: |
| 472 | ladder_war3_xpdiff(opponlevel, mylevel, &placeholder, &xpdiff); break; |
| 473 | default: |
| 474 | eventlog(eventlog_level_error, __FUNCTION__, "got invalid game result: {}", gameresult); |
| 475 | return -1; |
| 476 | } |
| 477 | |
| 478 | *xp_diff = xpdiff; |
| 479 | xp += xpdiff; |
| 480 | if (xp < 0) xp = 0; |
| 481 | |
| 482 | team->xp = xp; |
| 483 | |
| 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | int team_update_level(t_team * team) |
| 488 | { |
| 489 | int xp, mylevel; |
| 490 | |
| 491 | xp = team->xp; |
| 492 | if (xp < 0) xp = 0; |
| 493 | |
| 494 | mylevel = team->level; |
| 495 | if (mylevel < 1) mylevel = 1; |
| 496 | |
| 497 | if (mylevel > W3_XPCALC_MAXLEVEL) { |
| 498 | eventlog(eventlog_level_error, "account_set_sololevel", "got invalid level: {}", mylevel); |
| 499 | return -1; |
| 500 | } |
| 501 | |
| 502 | mylevel = ladder_war3_updatelevel(mylevel, xp); |
| 503 | |
| 504 | team->level = mylevel; |
| 505 | |
| 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | int team_set_saveladderstats(t_team * team, unsigned int gametype, int result, unsigned int opponlevel, t_clienttag clienttag) |
| 510 | { |
| 511 | unsigned int intrace; |
| 512 | int xpdiff; |
| 513 | int i; |
| 514 | t_account * account; |
| 515 | |
| 516 | if (!team) |
| 517 | { |
| 518 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL team"); |
| 519 | return -1; |
| 520 | } |
| 521 | |
| 522 | //added for better tracking down of problems with gameresults |
| 523 | eventlog(eventlog_level_trace, __FUNCTION__, "parsing game result for team: {} result: {}", team_get_teamid(team), (result == W3_GAMERESULT_WIN) ? "WIN" : "LOSS"); |
| 524 | |
| 525 | |
| 526 | if (result == W3_GAMERESULT_WIN) |
| 527 | { |
| 528 | team_inc_wins(team); |
| 529 | } |
| 530 | if (result == W3_GAMERESULT_LOSS) |
| 531 | { |
| 532 | team_inc_losses(team); |
| 533 | } |
| 534 | team_update_xp(team, result, opponlevel, &xpdiff); |
| 535 | team_update_level(team); |
| 536 | team_update_lastgame(team); |
| 537 | |
| 538 | LadderList* ladderList = ladders.getLadderList(LadderKey(ladder_id_ateam, clienttag, ladder_sort_default, ladder_time_default)); |
| 539 | LadderReferencedObject reference(team); |
| 540 | ladderList->updateEntry(team->teamid, team->level, team->xp, 0, reference); |
| 541 | |
| 542 | storage->write_team(team); |
| 543 | |
| 544 | |
| 545 | // now set currentatteam to 0 so we only get one report for each team |
| 546 | for (i = 0; i < MAX_TEAMSIZE; i++) |
| 547 | { |
| 548 | if ((account = team->members[i])) |
| 549 | { |
| 550 | intrace = account_get_w3pgrace(account, clienttag); |
| 551 | if (result == W3_GAMERESULT_WIN) |
| 552 | { |
| 553 | account_inc_racewins(account, intrace, clienttag); |
| 554 | } |
| 555 | if (result == W3_GAMERESULT_LOSS) |
| 556 | { |
| 557 | account_inc_racelosses(account, intrace, clienttag); |
| 558 | } |
| 559 | account_set_currentatteam(account, 0); |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | } |
| 567 | |
| 568 | } |