| 1 | /* |
| 2 | * Copyright (C) 1999 Rob Crittenden ([email protected]) |
| 3 | * Copyright (C) 1999,2000 Ross Combs ([email protected]) |
| 4 | * Copyright (C) 1999,2000 D.Moreaux ([email protected]) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version 2 |
| 9 | * of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 | */ |
| 20 | #include "common/setup_before.h" |
| 21 | #define LADDER_INTERNAL_ACCESS |
| 22 | #include "ladder.h" |
| 23 | |
| 24 | #include <cstdio> |
| 25 | #include <cerrno> |
| 26 | #include <cstring> |
| 27 | #include <fstream> |
| 28 | #include <iostream> |
| 29 | #include <sstream> |
| 30 | #include <string> |
| 31 | #include <list> |
| 32 | #include <map> |
| 33 | #include <cmath> |
| 34 | #include <algorithm> |
| 35 | #include <vector> |
| 36 | |
| 37 | #ifdef HAVE_SYS_STAT_H |
| 38 | # include <sys/stat.h> |
| 39 | #endif |
| 40 | |
| 41 | #include "compat/strerror.h" |
| 42 | #include "common/tag.h" |
| 43 | #include "common/eventlog.h" |
| 44 | #include "common/list.h" |
| 45 | #include "common/hashtable.h" |
| 46 | #include "common/bnettime.h" |
| 47 | #include "common/bnet_protocol.h" |
| 48 | |
| 49 | #include "account.h" |
| 50 | #include "account_wrap.h" |
| 51 | #include "prefs.h" |
| 52 | #include "ladder_calc.h" |
| 53 | #include "team.h" |
| 54 | #include "common/setup_after.h" |
| 55 | |
| 56 | #define MaxRankKeptInLadder 1000 |
| 57 | |
| 58 | namespace pvpgn |
| 59 | { |
| 60 | |
| 61 | namespace bnetd |
| 62 | { |
| 63 | |
| 64 | /* for War3 XP computations */ |
| 65 | static t_xpcalc_entry * xpcalc; |
| 66 | static t_xplevel_entry * xplevels; |
| 67 | int w3_xpcalc_maxleveldiff; |
| 68 | |
| 69 | const std::vector<std::string> ladder_id_str = { "0", "1", "", "3", "", "solo", "team", "ffa" }; |
| 70 | const std::vector<std::string> bin_ladder_id_str = { "", "", "", "I", "", "SOLO", "TEAM", "FFA", "AT" }; |
| 71 | const std::vector<std::string> bin_ladder_sort_str = { "R", "W", "G", "" }; |
| 72 | const std::vector<std::string> bin_ladder_time_str = { "A", "C", "" }; |
| 73 | |
| 74 | Ladders ladders; |
| 75 | |
| 76 | |
| 77 | /* |
| 78 | * Prepare an account for first ladder play if necessary. |
| 79 | */ |
| 80 | extern int ladder_init_account(t_account * account, t_clienttag clienttag, t_ladder_id id) |
| 81 | { |
| 82 | unsigned int uid, rating; |
| 83 | |
| 84 | if (!account) |
| 85 | { |
| 86 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL account"); |
| 87 | return -1; |
| 88 | } |
| 89 | if (!clienttag) |
| 90 | { |
| 91 | eventlog(eventlog_level_error, __FUNCTION__, "got bad clienttag"); |
| 92 | return -1; |
| 93 | } |
| 94 | |
| 95 | if (account_get_ladder_rating(account, clienttag, id) == 0) |
| 96 | { |
| 97 | if (account_get_ladder_wins(account, clienttag, id) + |
| 98 | account_get_ladder_losses(account, clienttag, id) > 0) /* no ladder games so far... */ |
| 99 | { |
| 100 | eventlog(eventlog_level_warn, __FUNCTION__, "account for \"{}\" ({}) has {} wins and {} losses but has zero rating", account_get_name(account), clienttag_uint_to_str(clienttag), account_get_ladder_wins(account, clienttag, id), account_get_ladder_losses(account, clienttag, id)); |
| 101 | return -1; |
| 102 | } |
| 103 | account_adjust_ladder_rating(account, clienttag, id, prefs_get_ladder_init_rating()); |
| 104 | |
| 105 | uid = account_get_uid(account); |
| 106 | rating = account_get_ladder_rating(account, clienttag, id); |
| 107 | |
| 108 | LadderList* ladderlist_cr = ladders.getLadderList(LadderKey(id, clienttag, ladder_sort_highestrated, ladder_time_current)); |
| 109 | LadderList* ladderlist_cw = ladders.getLadderList(LadderKey(id, clienttag, ladder_sort_mostwins, ladder_time_current)); |
| 110 | LadderList* ladderlist_cg = ladders.getLadderList(LadderKey(id, clienttag, ladder_sort_mostgames, ladder_time_current)); |
| 111 | |
| 112 | LadderReferencedObject reference(account); |
| 113 | |
| 114 | ladderlist_cr->updateEntry(uid, rating, 0, 0, reference); |
| 115 | ladderlist_cg->updateEntry(uid, 0, rating, 0, reference); |
| 116 | ladderlist_cw->updateEntry(uid, 0, rating, 0, reference); |
| 117 | |
| 118 | INFO2("initialized account for \"{}\" for \"{}\" ladder", account_get_name(account), clienttag_uint_to_str(clienttag)); |
| 119 | } |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | extern int ladder_init_account_wol(t_account * account, t_clienttag clienttag, t_ladder_id id) |
| 125 | { |
| 126 | unsigned int uid; |
| 127 | |
| 128 | if (!account) |
| 129 | { |
| 130 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL account"); |
| 131 | return -1; |
| 132 | } |
| 133 | if (!clienttag) |
| 134 | { |
| 135 | eventlog(eventlog_level_error, __FUNCTION__, "got bad clienttag"); |
| 136 | return -1; |
| 137 | } |
| 138 | |
| 139 | if ((account_get_ladder_points(account, clienttag, id) == 0) && (account_get_ladder_wins(account, clienttag, id) == 0) |
| 140 | && (account_get_ladder_losses(account, clienttag, id) == 0)) |
| 141 | { |
| 142 | uid = account_get_uid(account); |
| 143 | |
| 144 | LadderList* ladderlist = ladders.getLadderList(LadderKey(id, clienttag, ladder_sort_default, ladder_time_default)); |
| 145 | |
| 146 | LadderReferencedObject reference(account); |
| 147 | |
| 148 | ladderlist->updateEntry(uid, 0, 0, 0, reference); |
| 149 | |
| 150 | INFO2("initialized WOL account for \"{}\" for \"{}\" ladder", account_get_name(account), clienttag_uint_to_str(clienttag)); |
| 151 | } |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * Update player ratings, rankings, etc due to game results. |
| 158 | */ |
| 159 | extern int ladder_update(t_clienttag clienttag, t_ladder_id id, unsigned int count, t_account * * players, t_game_result * results, t_ladder_info * info) |
| 160 | { |
| 161 | unsigned int curr; |
| 162 | unsigned int ratio = 0; |
| 163 | int uid; |
| 164 | |
| 165 | if (count<1 || count>8) |
| 166 | { |
| 167 | eventlog(eventlog_level_error, __FUNCTION__, "got invalid player count {}", count); |
| 168 | return -1; |
| 169 | } |
| 170 | if (!players) |
| 171 | { |
| 172 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL players"); |
| 173 | return -1; |
| 174 | } |
| 175 | if (!results) |
| 176 | { |
| 177 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL results"); |
| 178 | return -1; |
| 179 | } |
| 180 | if (!clienttag) |
| 181 | { |
| 182 | eventlog(eventlog_level_error, __FUNCTION__, "got bad clienttag"); |
| 183 | return -1; |
| 184 | } |
| 185 | if (!info) |
| 186 | { |
| 187 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL info"); |
| 188 | return -1; |
| 189 | } |
| 190 | |
| 191 | if (ladder_calc_info(clienttag, id, count, players, results, info) < 0) |
| 192 | { |
| 193 | eventlog(eventlog_level_error, __FUNCTION__, "unable to calculate info from game results"); |
| 194 | return -1; |
| 195 | } |
| 196 | |
| 197 | for (curr = 0; curr < count; curr++) |
| 198 | { |
| 199 | unsigned int wins, games; |
| 200 | t_account * account; |
| 201 | |
| 202 | account = players[curr]; |
| 203 | uid = account_get_uid(account); |
| 204 | wins = account_get_ladder_wins(account, clienttag, id); |
| 205 | games = wins + account_get_ladder_losses(account, clienttag, id) + |
| 206 | account_get_ladder_disconnects(account, clienttag, id) + |
| 207 | account_get_ladder_draws(account, clienttag, id); |
| 208 | |
| 209 | //reasonable close to 1000 ;-) |
| 210 | ratio = (wins << 10) / games; |
| 211 | |
| 212 | account_adjust_ladder_rating(account, clienttag, id, info[curr].adj); |
| 213 | unsigned int rating = account_get_ladder_rating(account, clienttag, id); |
| 214 | |
| 215 | LadderReferencedObject reference(account); |
| 216 | |
| 217 | LadderList* ladderlist = ladders.getLadderList(LadderKey(id, clienttag, ladder_sort_highestrated, ladder_time_current)); |
| 218 | ladderlist->updateEntry(uid, rating, wins, ratio, reference); |
| 219 | |
| 220 | ladderlist = ladders.getLadderList(LadderKey(id, clienttag, ladder_sort_mostgames, ladder_time_current)); |
| 221 | ladderlist->updateEntry(uid, games, rating, ratio, reference); |
| 222 | |
| 223 | ladderlist = ladders.getLadderList(LadderKey(id, clienttag, ladder_sort_mostwins, ladder_time_current)); |
| 224 | ladderlist->updateEntry(uid, wins, rating, ratio, reference); |
| 225 | } |
| 226 | |
| 227 | ladders.update(); |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | static int _cb_count_points(int * points_win, int * points_loss, int pl1_points, int pl2_points) |
| 232 | { |
| 233 | *points_win = static_cast<int>(64 * (1 - 1 / (powf(10, static_cast<float>(pl2_points - pl1_points) / 400) + 1)) + 0.5); |
| 234 | *points_loss = std::min(64 - *points_win, pl1_points / 10); |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | extern int ladder_update_wol(t_clienttag clienttag, t_ladder_id id, t_account * * players, t_game_result * results) |
| 240 | { |
| 241 | unsigned int wins, losses, points; |
| 242 | int curr; |
| 243 | int uid; |
| 244 | |
| 245 | if (!players) { |
| 246 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL players"); |
| 247 | return -1; |
| 248 | } |
| 249 | |
| 250 | if (!results) { |
| 251 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL results"); |
| 252 | return -1; |
| 253 | } |
| 254 | |
| 255 | if (!clienttag) { |
| 256 | eventlog(eventlog_level_error, __FUNCTION__, "got bad clienttag"); |
| 257 | return -1; |
| 258 | } |
| 259 | |
| 260 | if (((results[0]) && (results[1])) && ((results[0] == results[1]) && (results[0] == game_result_disconnect))) { |
| 261 | DEBUG0("Both players got game_result_disconnect - points counting terminated"); |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | int pl1_points = account_get_ladder_points(players[0], clienttag, id); |
| 266 | int pl2_points = account_get_ladder_points(players[1], clienttag, id); |
| 267 | |
| 268 | for (curr = 0; curr < 2; curr++) { |
| 269 | t_account * account = players[curr]; |
| 270 | int mypoints = account_get_ladder_points(players[curr], clienttag, id); |
| 271 | int points_win = 0; |
| 272 | int points_loss = 0; |
| 273 | uid = account_get_uid(account); |
| 274 | |
| 275 | if (results[curr] == game_result_win) { |
| 276 | _cb_count_points(&points_win, &points_loss, mypoints, (mypoints == pl1_points ? pl2_points : pl1_points)); |
| 277 | account_set_ladder_points(players[curr], clienttag, id, mypoints + points_win); |
| 278 | DEBUG3("Player {} WIN, had {} points and now have {} points", account_get_name(account), mypoints, mypoints + points_win); |
| 279 | } |
| 280 | else { |
| 281 | _cb_count_points(&points_win, &points_loss, mypoints, (mypoints == pl1_points ? pl2_points : pl1_points)); |
| 282 | account_set_ladder_points(players[curr], clienttag, id, mypoints - points_loss); |
| 283 | DEBUG3("Player {} LOSS, had {} points and now have {} points", account_get_name(account), mypoints, mypoints - points_loss); |
| 284 | } |
| 285 | |
| 286 | LadderReferencedObject reference(account); |
| 287 | wins = account_get_ladder_wins(account, clienttag, id); |
| 288 | losses = account_get_ladder_losses(account, clienttag, id); |
| 289 | points = account_get_ladder_points(account, clienttag, id); |
| 290 | |
| 291 | LadderList* ladderlist = ladders.getLadderList(LadderKey(id, clienttag, ladder_sort_default, ladder_time_default)); |
| 292 | ladderlist->updateEntry(uid, points, wins, 0, reference); |
| 293 | } |
| 294 | |
| 295 | ladders.update(); |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | extern int ladder_check_map(char const * mapname, t_game_maptype maptype, t_clienttag clienttag) |
| 300 | { |
| 301 | if (!mapname) |
| 302 | { |
| 303 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL mapname"); |
| 304 | return -1; |
| 305 | } |
| 306 | if (!clienttag) |
| 307 | { |
| 308 | eventlog(eventlog_level_error, __FUNCTION__, "got bad clienttag"); |
| 309 | return -1; |
| 310 | } |
| 311 | |
| 312 | eventlog(eventlog_level_debug, __FUNCTION__, "checking mapname \"{}\" maptype={}", mapname, (int)maptype); |
| 313 | if (maptype == game_maptype_ladder) /* FIXME: what about Ironman? */ |
| 314 | return 1; |
| 315 | |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | /* ********************************************************************************************************************* |
| 320 | * handling of bnxpcalc and bnxplevel stuff (old code) |
| 321 | * *********************************************************************************************************************/ |
| 322 | |
| 323 | extern int ladder_createxptable(const char *xplevelfile, const char *xpcalcfile) |
| 324 | { |
| 325 | std::FILE *fd1, *fd2; |
| 326 | char buffer[256]; |
| 327 | char *p; |
| 328 | t_xpcalc_entry * newxpcalc; |
| 329 | int len, i, j; |
| 330 | int level, startxp, neededxp, mingames; |
| 331 | float lossfactor; |
| 332 | int minlevel, leveldiff, higher_xpgained, higher_xplost, lower_xpgained, lower_xplost = 10; |
| 333 | |
| 334 | if (xplevelfile == NULL || xpcalcfile == NULL) { |
| 335 | eventlog(eventlog_level_error, "ladder_createxptable", "got NULL filename(s)"); |
| 336 | return -1; |
| 337 | } |
| 338 | |
| 339 | /* first lets open files */ |
| 340 | if ((fd1 = std::fopen(xplevelfile, "rt")) == NULL) { |
| 341 | eventlog(eventlog_level_error, "ladder_createxptable", "could not open XP level file : \"{}\"", xplevelfile); |
| 342 | return -1; |
| 343 | } |
| 344 | |
| 345 | if ((fd2 = std::fopen(xpcalcfile, "rt")) == NULL) { |
| 346 | eventlog(eventlog_level_error, "ladder_createxptable", "could not open XP calc file : \"{}\"", xpcalcfile); |
| 347 | std::fclose(fd1); |
| 348 | return -1; |
| 349 | } |
| 350 | |
| 351 | /* then lets allocate mem for all the arrays */ |
| 352 | xpcalc = (t_xpcalc_entry*)xmalloc(sizeof(t_xpcalc_entry)* W3_XPCALC_MAXLEVEL); //presume the maximal leveldiff is level number |
| 353 | |
| 354 | w3_xpcalc_maxleveldiff = -1; |
| 355 | std::memset(xpcalc, 0, sizeof(t_xpcalc_entry)* W3_XPCALC_MAXLEVEL); |
| 356 | xplevels = (t_xplevel_entry*)xmalloc(sizeof(t_xplevel_entry)* W3_XPCALC_MAXLEVEL); |
| 357 | std::memset(xplevels, 0, sizeof(t_xplevel_entry)* W3_XPCALC_MAXLEVEL); |
| 358 | |
| 359 | /* finally, lets read from the files */ |
| 360 | |
| 361 | while (std::fgets(buffer, 256, fd1)) { |
| 362 | len = std::strlen(buffer); |
| 363 | if (len < 2) continue; |
| 364 | if (buffer[len - 1] == '\n') buffer[len - 1] = '\0'; |
| 365 | |
| 366 | /* support comments */ |
| 367 | for (p = buffer; *p && *p != '#'; p++); |
| 368 | if (*p == '#') *p = '\0'; |
| 369 | |
| 370 | if (std::sscanf(buffer, "%d %d %d %f %d", &level, &startxp, &neededxp, &lossfactor, &mingames) != 5) |
| 371 | continue; |
| 372 | |
| 373 | if (level < 1 || level > W3_XPCALC_MAXLEVEL) { /* invalid level */ |
| 374 | eventlog(eventlog_level_error, "ladder_createxptable", "read INVALID player level : {}", level); |
| 375 | continue; |
| 376 | } |
| 377 | |
| 378 | level--; /* the index in a C array starts from 0 */ |
| 379 | xplevels[level].startxp = startxp; |
| 380 | xplevels[level].neededxp = neededxp; |
| 381 | xplevels[level].lossfactor = (int)(lossfactor * 100); /* we store the loss factor as % */ |
| 382 | xplevels[level].mingames = mingames; |
| 383 | eventlog(eventlog_level_trace, "ladder_createxptable", "inserting level XP info (level: {}, startxp: {} neededxp: {} lossfactor: {} mingames: {})", level + 1, xplevels[level].startxp, xplevels[level].neededxp, xplevels[level].lossfactor, xplevels[level].mingames); |
| 384 | } |
| 385 | std::fclose(fd1); |
| 386 | |
| 387 | while (std::fgets(buffer, 256, fd2)) { |
| 388 | len = std::strlen(buffer); |
| 389 | if (len < 2) continue; |
| 390 | if (buffer[len - 1] == '\n') buffer[len - 1] = '\0'; |
| 391 | |
| 392 | /* support comments */ |
| 393 | for (p = buffer; *p && *p != '#'; p++); |
| 394 | if (*p == '#') *p = '\0'; |
| 395 | |
| 396 | if (std::sscanf(buffer, " %d %d %d %d %d %d ", &minlevel, &leveldiff, &higher_xpgained, &higher_xplost, &lower_xpgained, &lower_xplost) != 6) |
| 397 | continue; |
| 398 | |
| 399 | eventlog(eventlog_level_trace, "ladder_createxptable", "parsed xpcalc leveldiff : {}", leveldiff); |
| 400 | |
| 401 | if (leveldiff <0) { |
| 402 | eventlog(eventlog_level_error, "ladder_createxptable", "got invalid level diff : {}", leveldiff); |
| 403 | continue; |
| 404 | } |
| 405 | |
| 406 | if (leveldiff>(w3_xpcalc_maxleveldiff + 1)) { |
| 407 | eventlog(eventlog_level_error, __FUNCTION__, "expected entry for leveldiff={} but found {}", w3_xpcalc_maxleveldiff + 1, leveldiff); |
| 408 | continue; |
| 409 | } |
| 410 | |
| 411 | w3_xpcalc_maxleveldiff = leveldiff; |
| 412 | |
| 413 | xpcalc[leveldiff].higher_winxp = higher_xpgained; |
| 414 | xpcalc[leveldiff].higher_lossxp = higher_xplost; |
| 415 | xpcalc[leveldiff].lower_winxp = lower_xpgained; |
| 416 | xpcalc[leveldiff].lower_lossxp = lower_xplost; |
| 417 | } |
| 418 | std::fclose(fd2); |
| 419 | |
| 420 | newxpcalc = (t_xpcalc_entry*)xrealloc(xpcalc, sizeof(t_xpcalc_entry)* (w3_xpcalc_maxleveldiff + 1)); |
| 421 | xpcalc = newxpcalc; |
| 422 | |
| 423 | /* OK, now we need to test couse if the user forgot to put some values |
| 424 | * lots of profiles could get screwed up |
| 425 | */ |
| 426 | |
| 427 | if (w3_xpcalc_maxleveldiff < 0) |
| 428 | { |
| 429 | eventlog(eventlog_level_error, __FUNCTION__, "found no valid entries for WAR3 xp calculation"); |
| 430 | ladder_destroyxptable(); |
| 431 | return -1; |
| 432 | } |
| 433 | |
| 434 | eventlog(eventlog_level_info, __FUNCTION__, "set war3 xpcalc maxleveldiff to {}", w3_xpcalc_maxleveldiff); |
| 435 | |
| 436 | for (j = 0; j <= w3_xpcalc_maxleveldiff; j++) |
| 437 | if (xpcalc[j].higher_winxp == 0 || xpcalc[j].higher_lossxp == 0 || |
| 438 | xpcalc[j].lower_winxp == 0 || xpcalc[j].lower_lossxp == 0) { |
| 439 | eventlog(eventlog_level_error, __FUNCTION__, "I found 0 for a win/loss XP, please check your config file"); |
| 440 | ladder_destroyxptable(); |
| 441 | return -1; |
| 442 | } |
| 443 | |
| 444 | for (i = 0; i<W3_XPCALC_MAXLEVEL; i++) |
| 445 | if ((i > 0 && xplevels[i].neededxp == 0) || xplevels[i].lossfactor == 0 |
| 446 | || (i > 0 && (xplevels[i].startxp <= xplevels[i - 1].startxp || xplevels[i].neededxp < xplevels[i - 1].neededxp))) { |
| 447 | eventlog(eventlog_level_error, __FUNCTION__, "I found 0 for a level XP, please check your config file (level: {} neededxp: {} lossfactor: {})", i + 1, xplevels[i].neededxp, xplevels[i].lossfactor); |
| 448 | ladder_destroyxptable(); |
| 449 | return -1; |
| 450 | } |
| 451 | |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | extern void ladder_destroyxptable() |
| 456 | { |
| 457 | if (xpcalc != NULL) xfree(xpcalc); |
| 458 | if (xplevels != NULL) xfree(xplevels); |
| 459 | } |
| 460 | |
| 461 | extern int war3_get_maxleveldiff() |
| 462 | { |
| 463 | return w3_xpcalc_maxleveldiff; |
| 464 | } |
| 465 | |
| 466 | |
| 467 | extern int ladder_war3_xpdiff(unsigned int winnerlevel, unsigned int looserlevel, int *winxpdiff, int *loosxpdiff) |
| 468 | { |
| 469 | int diff, absdiff; |
| 470 | |
| 471 | diff = winnerlevel - looserlevel; |
| 472 | absdiff = (diff < 0) ? (-diff) : diff; |
| 473 | |
| 474 | if (absdiff > w3_xpcalc_maxleveldiff) { |
| 475 | eventlog(eventlog_level_error, "ladder_war3_xpdiff", "got invalid level difference : {}", absdiff); |
| 476 | return -1; |
| 477 | } |
| 478 | |
| 479 | if (winnerlevel > W3_XPCALC_MAXLEVEL || looserlevel > W3_XPCALC_MAXLEVEL || winnerlevel < 1 || looserlevel < 1) { |
| 480 | eventlog(eventlog_level_error, "ladder_war3_xpdiff", "got invalid account levels (win: {} loss: {})", winnerlevel, looserlevel); |
| 481 | return -1; |
| 482 | } |
| 483 | |
| 484 | if (winxpdiff == NULL || loosxpdiff == NULL) { |
| 485 | eventlog(eventlog_level_error, "ladder_war3_xpdiff", "got NULL winxpdiff, loosxpdiff"); |
| 486 | return -1; |
| 487 | } |
| 488 | /* we return the xp difference for the winner and the looser |
| 489 | * we compute that from the xp charts also applying the loss factor for |
| 490 | * lower level profiles |
| 491 | * FIXME: ?! loss factor doesnt keep the sum of xp won/lost constant |
| 492 | * DON'T CARE, cause current win/loss values aren't symetrical any more |
| 493 | */ |
| 494 | if (diff >= 0) { |
| 495 | *winxpdiff = xpcalc[absdiff].higher_winxp; |
| 496 | *loosxpdiff = -(xpcalc[absdiff].lower_lossxp * xplevels[looserlevel - 1].lossfactor) / 100; |
| 497 | } |
| 498 | else { |
| 499 | *winxpdiff = xpcalc[absdiff].lower_winxp; |
| 500 | *loosxpdiff = -(xpcalc[absdiff].higher_lossxp * xplevels[looserlevel - 1].lossfactor) / 100; |
| 501 | } |
| 502 | |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | extern int ladder_war3_updatelevel(unsigned int oldlevel, int xp) |
| 507 | { |
| 508 | int i, mylevel; |
| 509 | |
| 510 | if (oldlevel < 1 || oldlevel > W3_XPCALC_MAXLEVEL) { |
| 511 | eventlog(eventlog_level_error, "ladder_war3_updatelevel", "got invalid level: {}", oldlevel); |
| 512 | return oldlevel; |
| 513 | } |
| 514 | |
| 515 | if (xp <= 0) return 1; |
| 516 | |
| 517 | mylevel = oldlevel; |
| 518 | |
| 519 | for (i = mylevel; i < W3_XPCALC_MAXLEVEL; i++) |
| 520 | if (xplevels[i].startxp > xp) { mylevel = i; break; } |
| 521 | |
| 522 | for (i = mylevel - 1; i > 0; i--) |
| 523 | if (xplevels[i - 1].startxp < xp) { mylevel = i + 1; break; } |
| 524 | |
| 525 | return mylevel; |
| 526 | } |
| 527 | |
| 528 | extern int ladder_war3_get_min_xp(unsigned int Level) |
| 529 | { |
| 530 | if (Level < 1 || Level > W3_XPCALC_MAXLEVEL) |
| 531 | { |
| 532 | eventlog(eventlog_level_error, __FUNCTION__, "got invalid Level {}", Level); |
| 533 | return -1; |
| 534 | } |
| 535 | return xplevels[Level - 1].startxp; |
| 536 | } |
| 537 | |
| 538 | |
| 539 | /* ********************************************************************************************************************* |
| 540 | * start of new ladder codes |
| 541 | * *********************************************************************************************************************/ |
| 542 | |
| 543 | |
| 544 | LadderKey::LadderKey(t_ladder_id ladderId_, t_clienttag clienttag_, t_ladder_sort ladderSort_, t_ladder_time ladderTime_) |
| 545 | :ladderId(ladderId_), clienttag(clienttag_), ladderSort(ladderSort_), ladderTime(ladderTime_) |
| 546 | { |
| 547 | } |
| 548 | |
| 549 | |
| 550 | LadderKey::~LadderKey() throw () |
| 551 | {} |
| 552 | |
| 553 | |
| 554 | bool |
| 555 | LadderKey::operator== (const LadderKey& right) const |
| 556 | { |
| 557 | return ((ladderId == right.ladderId) && |
| 558 | (clienttag == right.clienttag) && |
| 559 | (ladderSort == right.ladderSort) && |
| 560 | (ladderTime == right.ladderTime)); |
| 561 | } |
| 562 | |
| 563 | |
| 564 | bool |
| 565 | LadderKey::operator< (const LadderKey& right) const |
| 566 | { |
| 567 | if (ladderId != right.ladderId) |
| 568 | return (ladderId < right.ladderId); |
| 569 | else if (clienttag != right.clienttag) |
| 570 | return (clienttag < right.clienttag); |
| 571 | else if (ladderSort != right.ladderSort) |
| 572 | return ladderSort < right.ladderSort; |
| 573 | else |
| 574 | return (ladderTime < right.ladderTime); |
| 575 | } |
| 576 | |
| 577 | |
| 578 | t_ladder_id |
| 579 | LadderKey::getLadderId() const |
| 580 | { |
| 581 | return ladderId; |
| 582 | } |
| 583 | |
| 584 | |
| 585 | t_clienttag |
| 586 | LadderKey::getClienttag() const |
| 587 | { |
| 588 | return clienttag; |
| 589 | } |
| 590 | |
| 591 | |
| 592 | t_ladder_sort |
| 593 | LadderKey::getLadderSort() const |
| 594 | { |
| 595 | return ladderSort; |
| 596 | } |
| 597 | |
| 598 | |
| 599 | t_ladder_time |
| 600 | LadderKey::getLadderTime() const |
| 601 | { |
| 602 | return ladderTime; |
| 603 | } |
| 604 | |
| 605 | LadderKey |
| 606 | LadderKey::getOpposingKey() const |
| 607 | { |
| 608 | if (ladderTime == ladder_time_active) |
| 609 | return LadderKey(ladderId, clienttag, ladderSort, ladder_time_current); |
| 610 | else if (ladderTime == ladder_time_current) |
| 611 | return LadderKey(ladderId, clienttag, ladderSort, ladder_time_active); |
| 612 | else |
| 613 | return LadderKey(ladderId, clienttag, ladderSort, ladderTime);; |
| 614 | } |
| 615 | |
| 616 | |
| 617 | LadderReferencedObject::LadderReferencedObject(t_account *account_) |
| 618 | :referenceType(referenceTypeAccount), account(account_), team(NULL) |
| 619 | { |
| 620 | } |
| 621 | |
| 622 | |
| 623 | LadderReferencedObject::LadderReferencedObject(t_team *team_) |
| 624 | : referenceType(referenceTypeTeam), account(NULL), team(team_) |
| 625 | { |
| 626 | } |
| 627 | |
| 628 | |
| 629 | LadderReferencedObject::~LadderReferencedObject() throw() |
| 630 | { |
| 631 | } |
| 632 | |
| 633 | |
| 634 | bool |
| 635 | LadderReferencedObject::getData(const LadderKey& ladderKey_, unsigned int& uid_, unsigned int& primary_, unsigned int& secondary_, unsigned int& tertiary_) const |
| 636 | { |
| 637 | // returns false in case of failures - and also when no need to add this referencedObject to ladder |
| 638 | t_clienttag clienttag = ladderKey_.getClienttag(); |
| 639 | t_ladder_id ladderId = ladderKey_.getLadderId(); |
| 640 | |
| 641 | if (referenceType == referenceTypeAccount) |
| 642 | { |
| 643 | uid_ = account_get_uid(account); |
| 644 | if (clienttag == CLIENTTAG_WARCRAFT3_UINT || clienttag == CLIENTTAG_WAR3XP_UINT) { |
| 645 | if (!(primary_ = account_get_ladder_level(account, clienttag, ladderId))) |
| 646 | return false; |
| 647 | secondary_ = account_get_ladder_xp(account, clienttag, ladderId); |
| 648 | tertiary_ = 0; |
| 649 | return true; |
| 650 | } |
| 651 | else if (tag_check_wolv1(clienttag) || tag_check_wolv2(clienttag)) { |
| 652 | if (!(primary_ = account_get_ladder_points(account, clienttag, ladderId))) |
| 653 | return false; |
| 654 | secondary_ = account_get_ladder_wins(account, clienttag, ladderId); |
| 655 | tertiary_ = 0; |
| 656 | return true; |
| 657 | } |
| 658 | else{ |
| 659 | t_ladder_sort ladderSort = ladderKey_.getLadderSort(); |
| 660 | unsigned int rating, wins, games; |
| 661 | // current ladders |
| 662 | if (ladderKey_.getLadderTime() == ladder_time_current) |
| 663 | { |
| 664 | rating = account_get_ladder_rating(account, clienttag, ladderId); |
| 665 | if (!rating) return false; |
| 666 | wins = account_get_ladder_wins(account, clienttag, ladderId); |
| 667 | games = wins + |
| 668 | account_get_ladder_losses(account, clienttag, ladderId) + |
| 669 | account_get_ladder_disconnects(account, clienttag, ladderId) + |
| 670 | account_get_ladder_draws(account, clienttag, ladderId); |
| 671 | } |
| 672 | else{ // active ladders |
| 673 | rating = account_get_ladder_active_rating(account, clienttag, ladderId); |
| 674 | if (!rating) return false; |
| 675 | wins = account_get_ladder_active_wins(account, clienttag, ladderId); |
| 676 | games = wins + |
| 677 | account_get_ladder_active_losses(account, clienttag, ladderId) + |
| 678 | account_get_ladder_active_disconnects(account, clienttag, ladderId) + |
| 679 | account_get_ladder_active_draws(account, clienttag, ladderId); |
| 680 | } |
| 681 | if (games == 0) |
| 682 | return false; |
| 683 | |
| 684 | unsigned int ratio = (wins << 10) / games; |
| 685 | switch (ladderSort) |
| 686 | { |
| 687 | case ladder_sort_highestrated: |
| 688 | primary_ = rating; |
| 689 | secondary_ = wins; |
| 690 | tertiary_ = ratio; |
| 691 | return true; |
| 692 | case ladder_sort_mostwins: |
| 693 | primary_ = wins; |
| 694 | secondary_ = rating; |
| 695 | tertiary_ = ratio; |
| 696 | return true; |
| 697 | case ladder_sort_mostgames: |
| 698 | primary_ = games; |
| 699 | secondary_ = rating; |
| 700 | tertiary_ = ratio; |
| 701 | return true; |
| 702 | default: |
| 703 | return false; |
| 704 | } |
| 705 | } |
| 706 | } |
| 707 | else if (referenceType == referenceTypeTeam) |
| 708 | { |
| 709 | uid_ = team_get_teamid(team); |
| 710 | if (!(primary_ = team_get_level(team))) |
| 711 | return false; |
| 712 | secondary_ = team_get_xp(team); |
| 713 | tertiary_ = 0; |
| 714 | return true; |
| 715 | } |
| 716 | |
| 717 | return false; |
| 718 | } |
| 719 | |
| 720 | unsigned int |
| 721 | LadderReferencedObject::getRank(const LadderKey& ladderKey_) const |
| 722 | { |
| 723 | t_clienttag clienttag = ladderKey_.getClienttag(); |
| 724 | t_ladder_id ladderId = ladderKey_.getLadderId(); |
| 725 | t_ladder_time ladderTime = ladderKey_.getLadderTime(); |
| 726 | t_ladder_sort ladderSort = ladderKey_.getLadderSort(); |
| 727 | |
| 728 | if (referenceType == referenceTypeAccount) |
| 729 | { |
| 730 | if (ladderSort == ladder_sort_default || ladderSort == ladder_sort_highestrated) |
| 731 | { |
| 732 | if (ladderTime == ladder_time_active) |
| 733 | { |
| 734 | return account_get_ladder_active_rank(account, clienttag, ladderId); |
| 735 | } |
| 736 | else{ |
| 737 | return account_get_ladder_rank(account, clienttag, ladderId); |
| 738 | } |
| 739 | } |
| 740 | else{ |
| 741 | // the account rank is only determined by highestrated/default ladder sort |
| 742 | return 0; |
| 743 | } |
| 744 | } |
| 745 | else if (referenceType == referenceTypeTeam) |
| 746 | { |
| 747 | return team_get_rank(team); |
| 748 | } |
| 749 | return 0; |
| 750 | } |
| 751 | |
| 752 | bool |
| 753 | LadderReferencedObject::setRank(const LadderKey& ladderKey_, unsigned int rank_) const |
| 754 | { |
| 755 | t_clienttag clienttag = ladderKey_.getClienttag(); |
| 756 | t_ladder_id ladderId = ladderKey_.getLadderId(); |
| 757 | t_ladder_time ladderTime = ladderKey_.getLadderTime(); |
| 758 | t_ladder_sort ladderSort = ladderKey_.getLadderSort(); |
| 759 | |
| 760 | if (referenceType == referenceTypeAccount) |
| 761 | { |
| 762 | if (ladderSort == ladder_sort_default || ladderSort == ladder_sort_highestrated) |
| 763 | { |
| 764 | if (ladderTime == ladder_time_active) |
| 765 | { |
| 766 | account_set_ladder_active_rank(account, clienttag, ladderId, rank_); |
| 767 | } |
| 768 | else{ |
| 769 | account_set_ladder_rank(account, clienttag, ladderId, rank_); |
| 770 | } |
| 771 | } |
| 772 | else{ |
| 773 | // the account rank is only determined by highestrated/default ladder sort |
| 774 | return false; |
| 775 | } |
| 776 | } |
| 777 | else if (referenceType == referenceTypeTeam) |
| 778 | { |
| 779 | team_set_rank(team, rank_); |
| 780 | } |
| 781 | return true; |
| 782 | } |
| 783 | |
| 784 | |
| 785 | void |
| 786 | LadderReferencedObject::activate(const LadderKey& ladderKey_) const |
| 787 | { |
| 788 | if (referenceType == referenceTypeAccount) |
| 789 | { |
| 790 | if (ladderKey_.getLadderSort() == ladder_sort_highestrated) |
| 791 | { |
| 792 | |
| 793 | t_clienttag clienttag = ladderKey_.getClienttag(); |
| 794 | t_ladder_id ladderId = ladderKey_.getLadderId(); |
| 795 | char const * timestr; |
| 796 | t_bnettime bt; |
| 797 | |
| 798 | account_set_ladder_active_wins(account, clienttag, ladderId, |
| 799 | account_get_ladder_wins(account, clienttag, ladderId)); |
| 800 | account_set_ladder_active_losses(account, clienttag, ladderId, |
| 801 | account_get_ladder_losses(account, clienttag, ladderId)); |
| 802 | account_set_ladder_active_draws(account, clienttag, ladderId, |
| 803 | account_get_ladder_draws(account, clienttag, ladderId)); |
| 804 | account_set_ladder_active_disconnects(account, clienttag, ladderId, |
| 805 | account_get_ladder_disconnects(account, clienttag, ladderId)); |
| 806 | account_set_ladder_active_rating(account, clienttag, ladderId, |
| 807 | account_get_ladder_rating(account, clienttag, ladderId)); |
| 808 | account_set_ladder_active_rank(account, clienttag, ladderId, |
| 809 | account_get_ladder_rank(account, clienttag, ladderId)); |
| 810 | if (!(timestr = account_get_ladder_last_time(account, clienttag, ladderId))) |
| 811 | timestr = BNETD_LADDER_DEFAULT_TIME; |
| 812 | bnettime_set_str(&bt, timestr); |
| 813 | account_set_ladder_active_last_time(account, clienttag, ladderId, bt); |
| 814 | } |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | |
| 819 | const t_referenceType |
| 820 | LadderReferencedObject::getReferenceType() const |
| 821 | { |
| 822 | return referenceType; |
| 823 | } |
| 824 | |
| 825 | |
| 826 | t_account * |
| 827 | LadderReferencedObject::getAccount() const |
| 828 | { |
| 829 | if (referenceType == referenceTypeAccount) |
| 830 | return account; |
| 831 | else |
| 832 | return NULL; |
| 833 | } |
| 834 | |
| 835 | t_team * |
| 836 | LadderReferencedObject::getTeam() const |
| 837 | { |
| 838 | if (referenceType == referenceTypeTeam) |
| 839 | return team; |
| 840 | else |
| 841 | return NULL; |
| 842 | } |
| 843 | |
| 844 | |
| 845 | LadderEntry::LadderEntry(unsigned int uid_, unsigned int primary_, unsigned int secondary_, unsigned int tertiary_, LadderReferencedObject referencedObject_) |
| 846 | :uid(uid_), primary(primary_), secondary(secondary_), tertiary(tertiary_), rank(0), referencedObject(referencedObject_) |
| 847 | { |
| 848 | } |
| 849 | |
| 850 | |
| 851 | LadderEntry::~LadderEntry() throw() |
| 852 | { |
| 853 | } |
| 854 | |
| 855 | |
| 856 | unsigned int |
| 857 | LadderEntry::getUid() const |
| 858 | { |
| 859 | return uid; |
| 860 | } |
| 861 | |
| 862 | |
| 863 | unsigned int |
| 864 | LadderEntry::getPrimary() const |
| 865 | { |
| 866 | return primary; |
| 867 | } |
| 868 | |
| 869 | |
| 870 | unsigned int |
| 871 | LadderEntry::getSecondary() const |
| 872 | { |
| 873 | return secondary; |
| 874 | } |
| 875 | |
| 876 | |
| 877 | unsigned int |
| 878 | LadderEntry::getTertiary() const |
| 879 | { |
| 880 | return tertiary; |
| 881 | } |
| 882 | |
| 883 | |
| 884 | unsigned int |
| 885 | LadderEntry::getRank() const |
| 886 | { |
| 887 | return rank; |
| 888 | } |
| 889 | |
| 890 | |
| 891 | const LadderReferencedObject& |
| 892 | LadderEntry::getReferencedObject() const |
| 893 | { |
| 894 | return referencedObject; |
| 895 | } |
| 896 | |
| 897 | |
| 898 | bool |
| 899 | LadderEntry::setRank(unsigned int rank_, const LadderKey& ladderKey_) |
| 900 | { |
| 901 | if (referencedObject.getRank(ladderKey_) != rank_) |
| 902 | { |
| 903 | rank = rank_; |
| 904 | return referencedObject.setRank(ladderKey_, rank_); |
| 905 | } |
| 906 | else{ |
| 907 | return false; |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | |
| 912 | void |
| 913 | LadderEntry::update(unsigned int primary_, unsigned int secondary_, unsigned int tertiary_) |
| 914 | { |
| 915 | primary = primary_; |
| 916 | secondary = secondary_; |
| 917 | tertiary = tertiary_; |
| 918 | } |
| 919 | |
| 920 | std::string |
| 921 | LadderEntry::status() const |
| 922 | { |
| 923 | std::ostringstream result; |
| 924 | result << uid << "," << primary << "," << secondary << "," << tertiary; |
| 925 | |
| 926 | return result.str(); |
| 927 | } |
| 928 | |
| 929 | bool |
| 930 | LadderEntry::operator== (const LadderEntry& right) const |
| 931 | { |
| 932 | return ((uid == right.uid) && |
| 933 | (primary == right.primary) && |
| 934 | (secondary == right.secondary) && |
| 935 | (tertiary == right.tertiary)); |
| 936 | } |
| 937 | |
| 938 | |
| 939 | bool |
| 940 | LadderEntry::operator< (const LadderEntry& right) const |
| 941 | { |
| 942 | if (primary != right.primary) |
| 943 | return (primary>right.primary); |
| 944 | else if (secondary != right.secondary) |
| 945 | return (secondary > right.secondary); |
| 946 | else if (tertiary != right.tertiary) |
| 947 | return (tertiary > right.tertiary); |
| 948 | else |
| 949 | return (uid > right.uid); |
| 950 | } |
| 951 | |
| 952 | |
| 953 | |
| 954 | LadderList::LadderList(LadderKey ladderKey_, t_referenceType referenceType_) |
| 955 | :ladderKey(ladderKey_), dirty(true), saved(false), referenceType(referenceType_) |
| 956 | { |
| 957 | ladderFilename = clienttag_uint_to_str(ladderKey_.getClienttag()); |
| 958 | ladderFilename += "_"; |
| 959 | ladderFilename += bin_ladder_time_str.at(static_cast<size_t>(ladderKey_.getLadderTime())); |
| 960 | ladderFilename += bin_ladder_sort_str.at(static_cast<size_t>(ladderKey_.getLadderSort())); |
| 961 | ladderFilename += bin_ladder_id_str.at(static_cast<size_t>(ladderKey_.getLadderId())); |
| 962 | ladderFilename += "_LADDER"; |
| 963 | } |
| 964 | |
| 965 | |
| 966 | LadderList::~LadderList() throw () |
| 967 | { |
| 968 | } |
| 969 | |
| 970 | |
| 971 | bool |
| 972 | LadderList::load() |
| 973 | { |
| 974 | return loadBinary(); |
| 975 | } |
| 976 | |
| 977 | |
| 978 | bool |
| 979 | LadderList::save() |
| 980 | { |
| 981 | sortAndUpdate(); |
| 982 | return saveBinary(); |
| 983 | } |
| 984 | |
| 985 | |
| 986 | void |
| 987 | LadderList::sortAndUpdate() |
| 988 | { |
| 989 | if (!(dirty)) |
| 990 | return; |
| 991 | |
| 992 | unsigned int rank = 1; |
| 993 | unsigned int changed = 0; |
| 994 | LList::iterator endMarker = ladder.end(); |
| 995 | |
| 996 | sort(ladder.begin(), ladder.end()); |
| 997 | |
| 998 | for (LList::iterator lit(ladder.begin()); lit != ladder.end(); lit++, rank++) |
| 999 | { |
| 1000 | if (rank <= MaxRankKeptInLadder) |
| 1001 | { |
| 1002 | if (lit->getRank() != rank) |
| 1003 | { |
| 1004 | if (lit->setRank(rank, ladderKey)) |
| 1005 | changed++; |
| 1006 | } |
| 1007 | } |
| 1008 | else |
| 1009 | { |
| 1010 | lit->setRank(0, ladderKey); |
| 1011 | if (endMarker == ladder.end()) |
| 1012 | endMarker = lit; |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | if (endMarker != ladder.end()) |
| 1017 | ladder.erase(endMarker, ladder.end()); |
| 1018 | |
| 1019 | if ((changed)) |
| 1020 | eventlog(eventlog_level_trace, __FUNCTION__, "adjusted rank for {} accounts", changed); |
| 1021 | saved = false; |
| 1022 | dirty = false; |
| 1023 | } |
| 1024 | |
| 1025 | |
| 1026 | const unsigned int magick = 0xdeadc0de; |
| 1027 | |
| 1028 | |
| 1029 | inline void |
| 1030 | LadderList::readdata(std::ifstream &fp, unsigned int &data) |
| 1031 | { |
| 1032 | fp.read((char *)(&data), sizeof(unsigned int)); |
| 1033 | } |
| 1034 | |
| 1035 | |
| 1036 | inline void |
| 1037 | LadderList::readdata(std::ifstream &fp, unsigned int data[], unsigned int membercount) |
| 1038 | { |
| 1039 | fp.read((char *)(data), sizeof(*data)*membercount); |
| 1040 | } |
| 1041 | |
| 1042 | |
| 1043 | bool |
| 1044 | LadderList::loadBinary() |
| 1045 | { |
| 1046 | std::string filename = prefs_get_ladderdir(); |
| 1047 | filename += "/"; |
| 1048 | filename += ladderFilename; |
| 1049 | |
| 1050 | std::ifstream fp(filename.c_str(), std::ios::in | std::ios::binary); |
| 1051 | |
| 1052 | if (!(fp)) |
| 1053 | { |
| 1054 | eventlog(eventlog_level_info, __FUNCTION__, "could not open ladder file \"{}\" - maybe ladder still empty (std::ifstream: {})", filename.c_str(), std::strerror(errno)); |
| 1055 | return false; |
| 1056 | } |
| 1057 | |
| 1058 | unsigned int checksum = 0; |
| 1059 | readdata(fp, checksum); |
| 1060 | |
| 1061 | if (checksum != magick) |
| 1062 | { |
| 1063 | eventlog(eventlog_level_error, __FUNCTION__, "{} not starting with magick", ladderFilename.c_str()); |
| 1064 | return false; |
| 1065 | } |
| 1066 | |
| 1067 | struct stat sfile; |
| 1068 | if (stat(filename.c_str(), &sfile) < 0) |
| 1069 | { |
| 1070 | eventlog(eventlog_level_error, __FUNCTION__, "failed to retrieve size of {}", ladderFilename.c_str()); |
| 1071 | return false; |
| 1072 | } |
| 1073 | |
| 1074 | unsigned int filesize = sfile.st_size; |
| 1075 | if (filesize%sizeof(unsigned int) != 0) |
| 1076 | { |
| 1077 | eventlog(eventlog_level_error, __FUNCTION__, "{} has unexpected size of {} bytes (not multiple of sizeof(unsigned int)", ladderFilename.c_str(), filesize); |
| 1078 | return false; |
| 1079 | } |
| 1080 | |
| 1081 | unsigned int noe = (filesize) / sizeof(unsigned int)-2; |
| 1082 | |
| 1083 | if (noe % 4 != 0) |
| 1084 | { |
| 1085 | eventlog(eventlog_level_error, __FUNCTION__, "{} has unexpected count of entries ({}) ", ladderFilename.c_str(), noe); |
| 1086 | return false; |
| 1087 | } |
| 1088 | |
| 1089 | checksum = 0; |
| 1090 | unsigned int values[4]; |
| 1091 | unsigned int uid, primary, secondary, tertiary; |
| 1092 | |
| 1093 | while (noe >= 4) |
| 1094 | { |
| 1095 | readdata(fp, values, 4); |
| 1096 | noe -= 4; |
| 1097 | |
| 1098 | //handle differently dependant on ladderKey->ladderId |
| 1099 | if (t_account* account = accountlist_find_account_by_uid(values[0])) |
| 1100 | { |
| 1101 | uid = values[0]; |
| 1102 | primary = values[2]; |
| 1103 | secondary = values[1]; |
| 1104 | tertiary = values[3]; |
| 1105 | LadderReferencedObject reference(account); |
| 1106 | addEntry(uid, primary, secondary, tertiary, reference); |
| 1107 | } |
| 1108 | else{ |
| 1109 | eventlog(eventlog_level_debug, __FUNCTION__, "no known entry for uid {}", values[0]); |
| 1110 | } |
| 1111 | for (int count = 0; count < 4; count++) checksum += values[count]; |
| 1112 | } |
| 1113 | |
| 1114 | unsigned int filechecksum = 0; |
| 1115 | readdata(fp, filechecksum); |
| 1116 | |
| 1117 | if (filechecksum != checksum) |
| 1118 | { |
| 1119 | eventlog(eventlog_level_error, __FUNCTION__, "{} has invalid checksum... fall back to old loading mode", ladderFilename.c_str()); |
| 1120 | ladder.clear(); |
| 1121 | return false; |
| 1122 | } |
| 1123 | |
| 1124 | eventlog(eventlog_level_info, __FUNCTION__, "successfully loaded {}", filename.c_str()); |
| 1125 | return true; |
| 1126 | } |
| 1127 | |
| 1128 | |
| 1129 | inline void |
| 1130 | LadderList::writedata(std::ofstream &fp, unsigned int &data) |
| 1131 | { |
| 1132 | fp.write((char *)(&data), sizeof(unsigned int)); |
| 1133 | } |
| 1134 | |
| 1135 | |
| 1136 | inline void |
| 1137 | LadderList::writedata(std::ofstream &fp, const unsigned int &data) |
| 1138 | { |
| 1139 | fp.write((char *)(&data), sizeof(unsigned int)); |
| 1140 | } |
| 1141 | |
| 1142 | |
| 1143 | inline void |
| 1144 | LadderList::writedata(std::ofstream &fp, unsigned int data[], unsigned int membercount) |
| 1145 | { |
| 1146 | fp.write((char *)(data), sizeof(*data)*membercount); |
| 1147 | } |
| 1148 | |
| 1149 | |
| 1150 | bool |
| 1151 | LadderList::saveBinary() |
| 1152 | { |
| 1153 | |
| 1154 | if (saved) |
| 1155 | return true; |
| 1156 | |
| 1157 | std::string filename = prefs_get_ladderdir(); |
| 1158 | filename += "/"; |
| 1159 | filename += ladderFilename; |
| 1160 | |
| 1161 | std::ofstream fp(filename.c_str(), std::ios::out | std::ios::binary); |
| 1162 | |
| 1163 | if (!(fp)) |
| 1164 | { |
| 1165 | eventlog(eventlog_level_error, __FUNCTION__, "could not open file \"{}\" for writing (std::ofstream: {})", filename.c_str(), std::strerror(errno)); |
| 1166 | return false; |
| 1167 | } |
| 1168 | |
| 1169 | writedata(fp, magick); //write the magick int as header |
| 1170 | |
| 1171 | unsigned int checksum = 0; |
| 1172 | unsigned int results[4]; |
| 1173 | |
| 1174 | for (LList::const_iterator lit(ladder.begin()); lit != ladder.end(); lit++) |
| 1175 | { |
| 1176 | results[0] = lit->getUid(); |
| 1177 | results[1] = lit->getSecondary(); |
| 1178 | results[2] = lit->getPrimary(); |
| 1179 | results[3] = 0; |
| 1180 | writedata(fp, results, 4); |
| 1181 | |
| 1182 | //calculate a checksum over saved data |
| 1183 | for (int count = 0; count < 4; count++) checksum += results[count]; |
| 1184 | } |
| 1185 | |
| 1186 | writedata(fp, checksum); // add checksum at the en |
| 1187 | |
| 1188 | saved = true; |
| 1189 | eventlog(eventlog_level_info, __FUNCTION__, "successfully saved {}", filename.c_str()); |
| 1190 | return true; |
| 1191 | } |
| 1192 | |
| 1193 | |
| 1194 | void |
| 1195 | LadderList::addEntry(unsigned int uid_, unsigned int primary_, unsigned int secondary_, unsigned int tertiary_, const LadderReferencedObject& referencedObject_) |
| 1196 | { |
| 1197 | if (referenceType != referencedObject_.getReferenceType()) |
| 1198 | { |
| 1199 | eventlog(eventlog_level_error, __FUNCTION__, "referenceType of LadderList and referencedObject do mismatch"); |
| 1200 | return; |
| 1201 | } |
| 1202 | |
| 1203 | LadderEntry entry(uid_, primary_, secondary_, tertiary_, referencedObject_); |
| 1204 | ladder.push_back(entry); |
| 1205 | dirty = true; |
| 1206 | } |
| 1207 | |
| 1208 | |
| 1209 | void |
| 1210 | LadderList::updateEntry(unsigned int uid_, unsigned int primary_, unsigned int secondary_, unsigned int tertiary_, const LadderReferencedObject& referencedObject_) |
| 1211 | { |
| 1212 | if (referenceType != referencedObject_.getReferenceType()) |
| 1213 | { |
| 1214 | eventlog(eventlog_level_error, __FUNCTION__, "referenceType of LadderList and referencedObject do mismatch"); |
| 1215 | return; |
| 1216 | } |
| 1217 | |
| 1218 | LList::iterator lit(ladder.begin()); |
| 1219 | for (; lit != ladder.end() && lit->getUid() != uid_; lit++); |
| 1220 | |
| 1221 | if (lit == ladder.end()) |
| 1222 | { |
| 1223 | LadderEntry entry(uid_, primary_, secondary_, tertiary_, referencedObject_); |
| 1224 | ladder.push_back(entry); |
| 1225 | } |
| 1226 | else{ |
| 1227 | lit->update(primary_, secondary_, tertiary_); |
| 1228 | } |
| 1229 | dirty = true; |
| 1230 | } |
| 1231 | |
| 1232 | |
| 1233 | bool |
| 1234 | LadderList::delEntry(unsigned int uid_) |
| 1235 | { |
| 1236 | LList::iterator lit(ladder.begin()); |
| 1237 | for (; lit != ladder.end() && lit->getUid() != uid_; lit++); |
| 1238 | |
| 1239 | if (lit == ladder.end()) |
| 1240 | return false; //account not on ladder |
| 1241 | else{ |
| 1242 | |
| 1243 | lit->setRank(0, ladderKey); |
| 1244 | ladder.erase(lit); |
| 1245 | dirty = true; |
| 1246 | return true; |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | |
| 1251 | const LadderReferencedObject* |
| 1252 | LadderList::getReferencedObject(unsigned int rank_) const |
| 1253 | { |
| 1254 | if (rank_ > ladder.size()) |
| 1255 | return 0; |
| 1256 | else |
| 1257 | return &ladder[rank_ - 1].getReferencedObject(); |
| 1258 | } |
| 1259 | |
| 1260 | |
| 1261 | unsigned int |
| 1262 | LadderList::getRank(unsigned int uid_) const |
| 1263 | { |
| 1264 | unsigned int rank; |
| 1265 | LList::const_iterator lit(ladder.begin()); |
| 1266 | for (rank = 1; lit != ladder.end() && lit->getUid() != uid_; lit++); |
| 1267 | |
| 1268 | if (lit == ladder.end()) |
| 1269 | return 0; |
| 1270 | else |
| 1271 | return lit->getRank(); |
| 1272 | } |
| 1273 | |
| 1274 | |
| 1275 | const LadderKey& |
| 1276 | LadderList::getLadderKey() const |
| 1277 | { |
| 1278 | return ladderKey; |
| 1279 | } |
| 1280 | |
| 1281 | |
| 1282 | const t_referenceType |
| 1283 | LadderList::getReferenceType() const |
| 1284 | { |
| 1285 | |
| 1286 | return referenceType; |
| 1287 | } |
| 1288 | |
| 1289 | |
| 1290 | void |
| 1291 | LadderList::activateFrom(const LadderList * currentLadder_) |
| 1292 | { |
| 1293 | const LList* currentList = ¤tLadder_->ladder; |
| 1294 | for (LList::const_iterator lit(currentList->begin()); lit != currentList->end(); lit++) |
| 1295 | { |
| 1296 | const LadderReferencedObject& referencedObject = lit->getReferencedObject(); |
| 1297 | updateEntry(lit->getUid(), lit->getPrimary(), lit->getSecondary(), lit->getTertiary(), referencedObject); |
| 1298 | referencedObject.activate(ladderKey); |
| 1299 | } |
| 1300 | return; |
| 1301 | } |
| 1302 | |
| 1303 | |
| 1304 | void |
| 1305 | LadderList::writeStatusfile() const |
| 1306 | { |
| 1307 | std::string filename; |
| 1308 | |
| 1309 | filename = prefs_get_outputdir(); |
| 1310 | filename += "/"; |
| 1311 | filename += ladderFilename; |
| 1312 | |
| 1313 | std::ofstream fp(filename.c_str()); |
| 1314 | |
| 1315 | if (!(fp)) |
| 1316 | { |
| 1317 | eventlog(eventlog_level_error, __FUNCTION__, "could not open file \"{}\" for writing (std::ofstream: {})", filename.c_str(), std::strerror(errno)); |
| 1318 | return; |
| 1319 | } |
| 1320 | |
| 1321 | unsigned int rank = 1; |
| 1322 | for (LList::const_iterator lit(ladder.begin()); lit != ladder.end(); lit++, rank++) |
| 1323 | { |
| 1324 | fp << rank << "," << lit->status() << "\n"; |
| 1325 | } |
| 1326 | |
| 1327 | } |
| 1328 | |
| 1329 | |
| 1330 | Ladders::Ladders() |
| 1331 | { |
| 1332 | // WAR3 ladders |
| 1333 | LadderKey WAR3_solo(ladder_id_solo, CLIENTTAG_WARCRAFT3_UINT, ladder_sort_default, ladder_time_default); |
| 1334 | ladderMap.insert(std::make_pair(WAR3_solo, LadderList(WAR3_solo, referenceTypeAccount))); |
| 1335 | |
| 1336 | LadderKey WAR3_team(ladder_id_team, CLIENTTAG_WARCRAFT3_UINT, ladder_sort_default, ladder_time_default); |
| 1337 | ladderMap.insert(std::make_pair(WAR3_team, LadderList(WAR3_team, referenceTypeAccount))); |
| 1338 | |
| 1339 | LadderKey WAR3_ffa(ladder_id_ffa, CLIENTTAG_WARCRAFT3_UINT, ladder_sort_default, ladder_time_default); |
| 1340 | ladderMap.insert(std::make_pair(WAR3_ffa, LadderList(WAR3_ffa, referenceTypeAccount))); |
| 1341 | |
| 1342 | LadderKey WAR3_ateam(ladder_id_ateam, CLIENTTAG_WARCRAFT3_UINT, ladder_sort_default, ladder_time_default); |
| 1343 | ladderMap.insert(std::make_pair(WAR3_ateam, LadderList(WAR3_ateam, referenceTypeTeam))); |
| 1344 | |
| 1345 | |
| 1346 | //W3XP ladders |
| 1347 | LadderKey W3XP_solo(ladder_id_solo, CLIENTTAG_WAR3XP_UINT, ladder_sort_default, ladder_time_default); |
| 1348 | ladderMap.insert(std::make_pair(W3XP_solo, LadderList(W3XP_solo, referenceTypeAccount))); |
| 1349 | |
| 1350 | LadderKey W3XP_team(ladder_id_team, CLIENTTAG_WAR3XP_UINT, ladder_sort_default, ladder_time_default); |
| 1351 | ladderMap.insert(std::make_pair(W3XP_team, LadderList(W3XP_team, referenceTypeAccount))); |
| 1352 | |
| 1353 | LadderKey W3XP_ffa(ladder_id_ffa, CLIENTTAG_WAR3XP_UINT, ladder_sort_default, ladder_time_default); |
| 1354 | ladderMap.insert(std::make_pair(W3XP_ffa, LadderList(W3XP_ffa, referenceTypeAccount))); |
| 1355 | |
| 1356 | LadderKey W3XP_ateam(ladder_id_ateam, CLIENTTAG_WAR3XP_UINT, ladder_sort_default, ladder_time_default); |
| 1357 | ladderMap.insert(std::make_pair(W3XP_ateam, LadderList(W3XP_ateam, referenceTypeTeam))); |
| 1358 | |
| 1359 | //STAR ladders |
| 1360 | LadderKey STAR_ar(ladder_id_normal, CLIENTTAG_STARCRAFT_UINT, ladder_sort_highestrated, ladder_time_active); |
| 1361 | ladderMap.insert(std::make_pair(STAR_ar, LadderList(STAR_ar, referenceTypeAccount))); |
| 1362 | |
| 1363 | LadderKey STAR_aw(ladder_id_normal, CLIENTTAG_STARCRAFT_UINT, ladder_sort_mostwins, ladder_time_active); |
| 1364 | ladderMap.insert(std::make_pair(STAR_aw, LadderList(STAR_aw, referenceTypeAccount))); |
| 1365 | |
| 1366 | LadderKey STAR_ag(ladder_id_normal, CLIENTTAG_STARCRAFT_UINT, ladder_sort_mostgames, ladder_time_active); |
| 1367 | ladderMap.insert(std::make_pair(STAR_ag, LadderList(STAR_ag, referenceTypeAccount))); |
| 1368 | |
| 1369 | LadderKey STAR_cr(ladder_id_normal, CLIENTTAG_STARCRAFT_UINT, ladder_sort_highestrated, ladder_time_current); |
| 1370 | ladderMap.insert(std::make_pair(STAR_cr, LadderList(STAR_cr, referenceTypeAccount))); |
| 1371 | |
| 1372 | LadderKey STAR_cw(ladder_id_normal, CLIENTTAG_STARCRAFT_UINT, ladder_sort_mostwins, ladder_time_current); |
| 1373 | ladderMap.insert(std::make_pair(STAR_cw, LadderList(STAR_cw, referenceTypeAccount))); |
| 1374 | |
| 1375 | LadderKey STAR_cg(ladder_id_normal, CLIENTTAG_STARCRAFT_UINT, ladder_sort_mostgames, ladder_time_current); |
| 1376 | ladderMap.insert(std::make_pair(STAR_cg, LadderList(STAR_cg, referenceTypeAccount))); |
| 1377 | |
| 1378 | //SEXP ladders |
| 1379 | LadderKey SEXP_ar(ladder_id_normal, CLIENTTAG_BROODWARS_UINT, ladder_sort_highestrated, ladder_time_active); |
| 1380 | ladderMap.insert(std::make_pair(SEXP_ar, LadderList(SEXP_ar, referenceTypeAccount))); |
| 1381 | |
| 1382 | LadderKey SEXP_aw(ladder_id_normal, CLIENTTAG_BROODWARS_UINT, ladder_sort_mostwins, ladder_time_active); |
| 1383 | ladderMap.insert(std::make_pair(SEXP_aw, LadderList(SEXP_aw, referenceTypeAccount))); |
| 1384 | |
| 1385 | LadderKey SEXP_ag(ladder_id_normal, CLIENTTAG_BROODWARS_UINT, ladder_sort_mostgames, ladder_time_active); |
| 1386 | ladderMap.insert(std::make_pair(SEXP_ag, LadderList(SEXP_ag, referenceTypeAccount))); |
| 1387 | |
| 1388 | LadderKey SEXP_cr(ladder_id_normal, CLIENTTAG_BROODWARS_UINT, ladder_sort_highestrated, ladder_time_current); |
| 1389 | ladderMap.insert(std::make_pair(SEXP_cr, LadderList(SEXP_cr, referenceTypeAccount))); |
| 1390 | |
| 1391 | LadderKey SEXP_cw(ladder_id_normal, CLIENTTAG_BROODWARS_UINT, ladder_sort_mostwins, ladder_time_current); |
| 1392 | ladderMap.insert(std::make_pair(SEXP_cw, LadderList(SEXP_cw, referenceTypeAccount))); |
| 1393 | |
| 1394 | LadderKey SEXP_cg(ladder_id_normal, CLIENTTAG_BROODWARS_UINT, ladder_sort_mostgames, ladder_time_current); |
| 1395 | ladderMap.insert(std::make_pair(SEXP_cg, LadderList(SEXP_cg, referenceTypeAccount))); |
| 1396 | |
| 1397 | //W2BN ladders |
| 1398 | LadderKey W2BN_ar(ladder_id_normal, CLIENTTAG_WARCIIBNE_UINT, ladder_sort_highestrated, ladder_time_active); |
| 1399 | ladderMap.insert(std::make_pair(W2BN_ar, LadderList(W2BN_ar, referenceTypeAccount))); |
| 1400 | |
| 1401 | LadderKey W2BN_aw(ladder_id_normal, CLIENTTAG_WARCIIBNE_UINT, ladder_sort_mostwins, ladder_time_active); |
| 1402 | ladderMap.insert(std::make_pair(W2BN_aw, LadderList(W2BN_aw, referenceTypeAccount))); |
| 1403 | |
| 1404 | LadderKey W2BN_ag(ladder_id_normal, CLIENTTAG_WARCIIBNE_UINT, ladder_sort_mostgames, ladder_time_active); |
| 1405 | ladderMap.insert(std::make_pair(W2BN_ag, LadderList(W2BN_ag, referenceTypeAccount))); |
| 1406 | |
| 1407 | LadderKey W2BN_cr(ladder_id_normal, CLIENTTAG_WARCIIBNE_UINT, ladder_sort_highestrated, ladder_time_current); |
| 1408 | ladderMap.insert(std::make_pair(W2BN_cr, LadderList(W2BN_cr, referenceTypeAccount))); |
| 1409 | |
| 1410 | LadderKey W2BN_cw(ladder_id_normal, CLIENTTAG_WARCIIBNE_UINT, ladder_sort_mostwins, ladder_time_current); |
| 1411 | ladderMap.insert(std::make_pair(W2BN_cw, LadderList(W2BN_cw, referenceTypeAccount))); |
| 1412 | |
| 1413 | LadderKey W2BN_cg(ladder_id_normal, CLIENTTAG_WARCIIBNE_UINT, ladder_sort_mostgames, ladder_time_current); |
| 1414 | ladderMap.insert(std::make_pair(W2BN_cg, LadderList(W2BN_cg, referenceTypeAccount))); |
| 1415 | |
| 1416 | LadderKey W2BN_ari(ladder_id_ironman, CLIENTTAG_WARCIIBNE_UINT, ladder_sort_highestrated, ladder_time_active); |
| 1417 | ladderMap.insert(std::make_pair(W2BN_ari, LadderList(W2BN_ari, referenceTypeAccount))); |
| 1418 | |
| 1419 | LadderKey W2BN_awi(ladder_id_ironman, CLIENTTAG_WARCIIBNE_UINT, ladder_sort_mostwins, ladder_time_active); |
| 1420 | ladderMap.insert(std::make_pair(W2BN_awi, LadderList(W2BN_awi, referenceTypeAccount))); |
| 1421 | |
| 1422 | LadderKey W2BN_agi(ladder_id_ironman, CLIENTTAG_WARCIIBNE_UINT, ladder_sort_mostgames, ladder_time_active); |
| 1423 | ladderMap.insert(std::make_pair(W2BN_agi, LadderList(W2BN_agi, referenceTypeAccount))); |
| 1424 | |
| 1425 | LadderKey W2BN_cri(ladder_id_ironman, CLIENTTAG_WARCIIBNE_UINT, ladder_sort_highestrated, ladder_time_current); |
| 1426 | ladderMap.insert(std::make_pair(W2BN_cri, LadderList(W2BN_cri, referenceTypeAccount))); |
| 1427 | |
| 1428 | LadderKey W2BN_cwi(ladder_id_ironman, CLIENTTAG_WARCIIBNE_UINT, ladder_sort_mostwins, ladder_time_current); |
| 1429 | ladderMap.insert(std::make_pair(W2BN_cwi, LadderList(W2BN_cwi, referenceTypeAccount))); |
| 1430 | |
| 1431 | LadderKey W2BN_cgi(ladder_id_ironman, CLIENTTAG_WARCIIBNE_UINT, ladder_sort_mostgames, ladder_time_current); |
| 1432 | ladderMap.insert(std::make_pair(W2BN_cgi, LadderList(W2BN_cgi, referenceTypeAccount))); |
| 1433 | |
| 1434 | //TSUN ladders |
| 1435 | LadderKey TSUN_solo(ladder_id_solo, CLIENTTAG_TIBERNSUN_UINT, ladder_sort_default, ladder_time_default); |
| 1436 | ladderMap.insert(std::make_pair(TSUN_solo, LadderList(TSUN_solo, referenceTypeAccount))); |
| 1437 | |
| 1438 | //TSXP ladders |
| 1439 | LadderKey TSXP_solo(ladder_id_solo, CLIENTTAG_TIBSUNXP_UINT, ladder_sort_default, ladder_time_default); |
| 1440 | ladderMap.insert(std::make_pair(TSXP_solo, LadderList(TSXP_solo, referenceTypeAccount))); |
| 1441 | |
| 1442 | //RAL2 ladders |
| 1443 | LadderKey RAL2_solo(ladder_id_solo, CLIENTTAG_REDALERT2_UINT, ladder_sort_default, ladder_time_default); |
| 1444 | ladderMap.insert(std::make_pair(RAL2_solo, LadderList(RAL2_solo, referenceTypeAccount))); |
| 1445 | |
| 1446 | //YURI ladders |
| 1447 | LadderKey YURI_solo(ladder_id_solo, CLIENTTAG_YURISREV_UINT, ladder_sort_default, ladder_time_default); |
| 1448 | ladderMap.insert(std::make_pair(YURI_solo, LadderList(YURI_solo, referenceTypeAccount))); |
| 1449 | |
| 1450 | } |
| 1451 | |
| 1452 | Ladders::~Ladders() throw () |
| 1453 | { |
| 1454 | } |
| 1455 | |
| 1456 | |
| 1457 | LadderList* |
| 1458 | Ladders::getLadderList(const LadderKey& ladderKey_) |
| 1459 | { |
| 1460 | KeyLadderMap::iterator kit(ladderMap.begin()); |
| 1461 | for (; kit != ladderMap.end() && (!(ladderKey_ == (*kit).first)); kit++); |
| 1462 | |
| 1463 | if (kit == ladderMap.end()) |
| 1464 | { |
| 1465 | eventlog(eventlog_level_error, __FUNCTION__, "found no matching ladderlist"); |
| 1466 | return NULL; |
| 1467 | } |
| 1468 | else{ |
| 1469 | return &(*kit).second; |
| 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | |
| 1474 | void |
| 1475 | Ladders::load() |
| 1476 | { |
| 1477 | std::list<LadderList*> laddersToRebuild; |
| 1478 | for (KeyLadderMap::iterator kit(ladderMap.begin()); kit != ladderMap.end(); kit++) |
| 1479 | { |
| 1480 | if (!kit->second.load()) |
| 1481 | { |
| 1482 | //loading failed, add to rebuild list |
| 1483 | laddersToRebuild.push_back(&kit->second); |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | if (!laddersToRebuild.empty()) |
| 1488 | rebuild(laddersToRebuild); |
| 1489 | |
| 1490 | update(); |
| 1491 | save(); |
| 1492 | } |
| 1493 | |
| 1494 | void |
| 1495 | Ladders::rebuild(std::list<LadderList*>& laddersToRebuild) |
| 1496 | { |
| 1497 | t_entry * curr; |
| 1498 | t_account * account; |
| 1499 | unsigned int uid, primary, secondary, tertiary; |
| 1500 | |
| 1501 | eventlog(eventlog_level_debug, __FUNCTION__, "start rebuilding ladders"); |
| 1502 | |
| 1503 | if (accountlist_load_all(ST_FORCE)) { |
| 1504 | eventlog(eventlog_level_error, __FUNCTION__, "error loading all accounts"); |
| 1505 | return; |
| 1506 | } |
| 1507 | |
| 1508 | HASHTABLE_TRAVERSE(accountlist(), curr) |
| 1509 | { |
| 1510 | if ((account = ((t_account *)entry_get_data(curr)))) |
| 1511 | { |
| 1512 | |
| 1513 | LadderReferencedObject referencedObject(account); |
| 1514 | for (std::list<LadderList*>::iterator lit(laddersToRebuild.begin()); lit != laddersToRebuild.end(); lit++) |
| 1515 | { |
| 1516 | // only do handle referenceTypeAccount ladders here |
| 1517 | if ((*lit)->getReferenceType() != referenceTypeAccount) |
| 1518 | { |
| 1519 | continue; |
| 1520 | } |
| 1521 | |
| 1522 | if (referencedObject.getData((*lit)->getLadderKey(), uid, primary, secondary, tertiary)) |
| 1523 | { |
| 1524 | (*lit)->addEntry(uid, primary, secondary, tertiary, referencedObject); |
| 1525 | } |
| 1526 | } |
| 1527 | } |
| 1528 | } |
| 1529 | |
| 1530 | // now we would need to traverse teamlist, too. |
| 1531 | // how about comletly moving this code into team? |
| 1532 | |
| 1533 | eventlog(eventlog_level_debug, __FUNCTION__, "done rebuilding ladders"); |
| 1534 | |
| 1535 | } |
| 1536 | |
| 1537 | void |
| 1538 | Ladders::update() |
| 1539 | { |
| 1540 | for (KeyLadderMap::iterator kit(ladderMap.begin()); kit != ladderMap.end(); kit++) |
| 1541 | { |
| 1542 | kit->second.sortAndUpdate(); |
| 1543 | } |
| 1544 | } |
| 1545 | |
| 1546 | |
| 1547 | void |
| 1548 | Ladders::activate() |
| 1549 | { |
| 1550 | for (KeyLadderMap::iterator kit(ladderMap.begin()); kit != ladderMap.end(); kit++) |
| 1551 | { |
| 1552 | if (kit->second.getLadderKey().getLadderTime() == ladder_time_active) |
| 1553 | { |
| 1554 | LadderList* activeLadder = &kit->second; |
| 1555 | LadderList* currentLadder = getLadderList(activeLadder->getLadderKey().getOpposingKey()); |
| 1556 | |
| 1557 | if (currentLadder == NULL){ |
| 1558 | eventlog(eventlog_level_error, __FUNCTION__, "found active ladder, but no matching current ladder"); |
| 1559 | continue; |
| 1560 | } |
| 1561 | activeLadder->activateFrom(currentLadder); |
| 1562 | } |
| 1563 | } |
| 1564 | update(); |
| 1565 | } |
| 1566 | |
| 1567 | |
| 1568 | void |
| 1569 | Ladders::save() |
| 1570 | { |
| 1571 | for (KeyLadderMap::iterator kit(ladderMap.begin()); kit != ladderMap.end(); kit++) |
| 1572 | { |
| 1573 | kit->second.save(); |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | |
| 1578 | void |
| 1579 | Ladders::status() const |
| 1580 | { |
| 1581 | for (KeyLadderMap::const_iterator kit(ladderMap.begin()); kit != ladderMap.end(); kit++) |
| 1582 | { |
| 1583 | kit->second.writeStatusfile(); |
| 1584 | } |
| 1585 | } |
| 1586 | |
| 1587 | |
| 1588 | } |
| 1589 | |
| 1590 | } |
| 1591 |