| 1 |
/*
|
| 2 |
* Copyright (C) 2000,2001 Onlyer (onlyer@263.net)
|
| 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 |
#include "setup.h"
|
| 20 |
|
| 21 |
#ifdef STDC_HEADERS
|
| 22 |
# include <stdlib.h>
|
| 23 |
#else
|
| 24 |
# ifdef HAVE_MALLOC_H
|
| 25 |
# include <malloc.h>
|
| 26 |
# endif
|
| 27 |
#endif
|
| 28 |
#ifdef HAVE_STRING_H
|
| 29 |
# include <string.h>
|
| 30 |
#else
|
| 31 |
# ifdef HAVE_STRINGS_H
|
| 32 |
# include <strings.h>
|
| 33 |
# endif
|
| 34 |
# ifdef HAVE_MEMORY_H
|
| 35 |
# include <memory.h>
|
| 36 |
# endif
|
| 37 |
#endif
|
| 38 |
#include "compat/strcasecmp.h"
|
| 39 |
#include "compat/strdup.h"
|
| 40 |
#ifdef TIME_WITH_SYS_TIME
|
| 41 |
# include <time.h>
|
| 42 |
# include <sys/time.h>
|
| 43 |
#else
|
| 44 |
# ifdef HAVE_SYS_TIME_H
|
| 45 |
# include <sys/time.h>
|
| 46 |
# else
|
| 47 |
# include <time.h>
|
| 48 |
# endif
|
| 49 |
#endif
|
| 50 |
|
| 51 |
#include "bit.h"
|
| 52 |
#include "prefs.h"
|
| 53 |
#include "game.h"
|
| 54 |
#include "common/list.h"
|
| 55 |
#include "common/eventlog.h"
|
| 56 |
#include "common/xalloc.h"
|
| 57 |
#include "common/setup_after.h"
|
| 58 |
|
| 59 |
static t_list * gamelist_head=NULL;
|
| 60 |
static t_elem const * gamelist_curr_elem=NULL;
|
| 61 |
static unsigned int total_game=0;
|
| 62 |
static unsigned int game_id=0;
|
| 63 |
static t_game_charinfo * game_find_character(t_game * game, char const * charname);
|
| 64 |
|
| 65 |
extern t_list * d2cs_gamelist(void)
|
| 66 |
{
|
| 67 |
return gamelist_head;
|
| 68 |
}
|
| 69 |
|
| 70 |
extern t_elem const * gamelist_get_curr_elem(void)
|
| 71 |
{
|
| 72 |
return gamelist_curr_elem;
|
| 73 |
}
|
| 74 |
|
| 75 |
extern void gamelist_set_curr_elem(t_elem const * elem)
|
| 76 |
{
|
| 77 |
gamelist_curr_elem=elem;
|
| 78 |
return;
|
| 79 |
}
|
| 80 |
|
| 81 |
extern int d2cs_gamelist_create(void)
|
| 82 |
{
|
| 83 |
gamelist_head=list_create();
|
| 84 |
return 0;
|
| 85 |
}
|
| 86 |
|
| 87 |
extern int d2cs_gamelist_destroy(void)
|
| 88 |
{
|
| 89 |
t_game * game;
|
| 90 |
|
| 91 |
BEGIN_LIST_TRAVERSE_DATA(gamelist_head, game)
|
| 92 |
{
|
| 93 |
game_destroy(game,&curr_elem_);
|
| 94 |
}
|
| 95 |
END_LIST_TRAVERSE_DATA();
|
| 96 |
|
| 97 |
if (list_destroy(gamelist_head)<0) {
|
| 98 |
eventlog(eventlog_level_error,__FUNCTION__,"error destroy connection list");
|
| 99 |
return -1;
|
| 100 |
}
|
| 101 |
gamelist_head=NULL;
|
| 102 |
return 0;
|
| 103 |
}
|
| 104 |
|
| 105 |
extern t_game * d2cs_gamelist_find_game(char const * gamename)
|
| 106 |
{
|
| 107 |
t_game * game;
|
| 108 |
|
| 109 |
ASSERT(gamename,NULL);
|
| 110 |
BEGIN_LIST_TRAVERSE_DATA(gamelist_head,game)
|
| 111 |
{
|
| 112 |
if (!strcasecmp(game->name,gamename)) return game;
|
| 113 |
}
|
| 114 |
END_LIST_TRAVERSE_DATA()
|
| 115 |
return NULL;
|
| 116 |
}
|
| 117 |
|
| 118 |
extern t_game * gamelist_find_game_by_id(unsigned int id)
|
| 119 |
{
|
| 120 |
t_game * game;
|
| 121 |
|
| 122 |
BEGIN_LIST_TRAVERSE_DATA(gamelist_head,game)
|
| 123 |
{
|
| 124 |
if (game->id==id) return game;
|
| 125 |
}
|
| 126 |
END_LIST_TRAVERSE_DATA()
|
| 127 |
return NULL;
|
| 128 |
}
|
| 129 |
|
| 130 |
extern t_game * gamelist_find_game_by_d2gs_and_id(unsigned int d2gs_id, unsigned int d2gs_gameid)
|
| 131 |
{
|
| 132 |
t_game * game;
|
| 133 |
|
| 134 |
BEGIN_LIST_TRAVERSE_DATA(gamelist_head,game)
|
| 135 |
{
|
| 136 |
if (!game->created) continue;
|
| 137 |
if (game->d2gs_gameid!=d2gs_gameid) continue;
|
| 138 |
if (d2gs_get_id(game->d2gs) != d2gs_id) continue;
|
| 139 |
return game;
|
| 140 |
}
|
| 141 |
END_LIST_TRAVERSE_DATA()
|
| 142 |
return NULL;
|
| 143 |
}
|
| 144 |
|
| 145 |
extern t_game * gamelist_find_character(char const * charname)
|
| 146 |
{
|
| 147 |
t_game * game;
|
| 148 |
|
| 149 |
ASSERT(charname,NULL);
|
| 150 |
BEGIN_LIST_TRAVERSE_DATA(gamelist_head, game)
|
| 151 |
{
|
| 152 |
if (game_find_character(game,charname)) return game;
|
| 153 |
}
|
| 154 |
END_LIST_TRAVERSE_DATA();
|
| 155 |
return NULL;
|
| 156 |
}
|
| 157 |
|
| 158 |
extern void d2cs_gamelist_check_voidgame(void)
|
| 159 |
{
|
| 160 |
t_game * game;
|
| 161 |
time_t now;
|
| 162 |
int timeout;
|
| 163 |
|
| 164 |
timeout=prefs_get_max_game_idletime();
|
| 165 |
if (!timeout) return;
|
| 166 |
now=time(NULL);
|
| 167 |
BEGIN_LIST_TRAVERSE_DATA(gamelist_head, game)
|
| 168 |
{
|
| 169 |
if (!game->currchar) {
|
| 170 |
if ((now-game->lastaccess_time)>timeout) {
|
| 171 |
eventlog(eventlog_level_info,__FUNCTION__,"game %s is empty too long time,destroying it",game->name);
|
| 172 |
game_destroy(game,&curr_elem_);
|
| 173 |
}
|
| 174 |
}
|
| 175 |
}
|
| 176 |
END_LIST_TRAVERSE_DATA()
|
| 177 |
}
|
| 178 |
|
| 179 |
extern t_game * d2cs_game_create(char const * gamename, char const * gamepass, char const * gamedesc,
|
| 180 |
unsigned int gameflag)
|
| 181 |
{
|
| 182 |
t_game * game;
|
| 183 |
time_t now;
|
| 184 |
|
| 185 |
ASSERT(gamename,NULL);
|
| 186 |
ASSERT(gamepass,NULL);
|
| 187 |
ASSERT(gamedesc,NULL);
|
| 188 |
if (d2cs_gamelist_find_game(gamename)) {
|
| 189 |
eventlog(eventlog_level_error,__FUNCTION__,"game %s already exist",gamename);
|
| 190 |
return NULL;
|
| 191 |
}
|
| 192 |
game=xmalloc(sizeof(t_game));
|
| 193 |
game->name=xstrdup(gamename);
|
| 194 |
game->pass=xstrdup(gamepass);
|
| 195 |
game->desc=xstrdup(gamedesc);
|
| 196 |
game->charlist=list_create();
|
| 197 |
now=time(NULL);
|
| 198 |
game_id++;
|
| 199 |
if (game_id==0) game_id=1;
|
| 200 |
game->id=game_id;
|
| 201 |
game->created=0;
|
| 202 |
game->create_time=now;
|
| 203 |
game->lastaccess_time=now;
|
| 204 |
game->gameflag=gameflag;
|
| 205 |
game->charlevel=0;
|
| 206 |
game->leveldiff=0;
|
| 207 |
game->d2gs_gameid=0;
|
| 208 |
game->d2gs=NULL;
|
| 209 |
game->maxchar=MAX_CHAR_PER_GAME;
|
| 210 |
game->currchar=0;
|
| 211 |
list_prepend_data(gamelist_head,game);
|
| 212 |
total_game++;
|
| 213 |
eventlog(eventlog_level_info,__FUNCTION__,"game %s pass=%s desc=%s gameflag=0x%08X created (%d total)",gamename,gamepass,
|
| 214 |
gamedesc,gameflag,total_game);
|
| 215 |
return game;
|
| 216 |
}
|
| 217 |
|
| 218 |
extern int game_destroy(t_game * game, t_elem ** elem)
|
| 219 |
{
|
| 220 |
t_elem * curr;
|
| 221 |
t_game_charinfo * charinfo;
|
| 222 |
|
| 223 |
ASSERT(game,-1);
|
| 224 |
if (gamelist_curr_elem && (game==elem_get_data(gamelist_curr_elem))) {
|
| 225 |
gamelist_curr_elem=elem_get_next_const(gamelist_head,gamelist_curr_elem);
|
| 226 |
}
|
| 227 |
if (list_remove_data(gamelist_head,game,elem)<0) {
|
| 228 |
eventlog(eventlog_level_error,__FUNCTION__,"error remove game %s on game list",game->name);
|
| 229 |
return -1;
|
| 230 |
}
|
| 231 |
total_game--;
|
| 232 |
eventlog(eventlog_level_info,__FUNCTION__,"game %s removed from game list (%d left)",game->name,total_game);
|
| 233 |
LIST_TRAVERSE(game->charlist,curr)
|
| 234 |
{
|
| 235 |
if ((charinfo=elem_get_data(curr))) {
|
| 236 |
if (charinfo->charname) xfree((void *)charinfo->charname);
|
| 237 |
xfree(charinfo);
|
| 238 |
}
|
| 239 |
list_remove_elem(game->charlist,&curr);
|
| 240 |
}
|
| 241 |
list_destroy(game->charlist);
|
| 242 |
|
| 243 |
if (game->d2gs) {
|
| 244 |
d2gs_add_gamenum(game->d2gs,-1);
|
| 245 |
gqlist_check_creategame(d2gs_get_maxgame(game->d2gs) - d2gs_get_gamenum(game->d2gs));
|
| 246 |
}
|
| 247 |
if (game->desc) xfree((void *)game->desc);
|
| 248 |
if (game->pass) xfree((void *)game->pass);
|
| 249 |
if (game->name) xfree((void *)game->name);
|
| 250 |
xfree(game);
|
| 251 |
return 0;
|
| 252 |
}
|
| 253 |
|
| 254 |
static t_game_charinfo * game_find_character(t_game * game, char const * charname)
|
| 255 |
{
|
| 256 |
t_game_charinfo * charinfo;
|
| 257 |
|
| 258 |
ASSERT(game,NULL);
|
| 259 |
ASSERT(charname,NULL);
|
| 260 |
if (!game->charlist) {
|
| 261 |
eventlog(eventlog_level_error,__FUNCTION__,"got NULL character list in game %s",game->name);
|
| 262 |
return NULL;
|
| 263 |
}
|
| 264 |
BEGIN_LIST_TRAVERSE_DATA(game->charlist,charinfo)
|
| 265 |
{
|
| 266 |
if (!charinfo->charname) continue;
|
| 267 |
if (!strcmp_charname(charinfo->charname,charname)) return charinfo;
|
| 268 |
}
|
| 269 |
END_LIST_TRAVERSE_DATA()
|
| 270 |
return NULL;
|
| 271 |
}
|
| 272 |
|
| 273 |
extern int game_add_character(t_game * game, char const * charname, unsigned char class,
|
| 274 |
unsigned char level)
|
| 275 |
{
|
| 276 |
t_game_charinfo * charinfo;
|
| 277 |
|
| 278 |
ASSERT(game,-1);
|
| 279 |
ASSERT(charname,-1);
|
| 280 |
charinfo=game_find_character(game,charname);
|
| 281 |
if (charinfo) {
|
| 282 |
eventlog(eventlog_level_info,__FUNCTION__,"updating character %s (game %s) status", charname,game->name);
|
| 283 |
charinfo->class=class;
|
| 284 |
charinfo->level=level;
|
| 285 |
return 0;
|
| 286 |
}
|
| 287 |
charinfo=xmalloc(sizeof(t_game_charinfo));
|
| 288 |
charinfo->charname=xstrdup(charname);
|
| 289 |
charinfo->class=class;
|
| 290 |
charinfo->level=level;
|
| 291 |
list_append_data(game->charlist,charinfo);
|
| 292 |
game->currchar++;
|
| 293 |
game->lastaccess_time=time(NULL);
|
| 294 |
eventlog(eventlog_level_info,__FUNCTION__,"added character %s to game %s (%d total)",charname,game->name,game->currchar);
|
| 295 |
return 0;
|
| 296 |
}
|
| 297 |
|
| 298 |
extern int game_del_character(t_game * game, char const * charname)
|
| 299 |
{
|
| 300 |
t_game_charinfo * charinfo;
|
| 301 |
t_elem * elem;
|
| 302 |
|
| 303 |
ASSERT(game,-1);
|
| 304 |
ASSERT(charname,-1);
|
| 305 |
if (!(charinfo=game_find_character(game,charname))) {
|
| 306 |
eventlog(eventlog_level_error,__FUNCTION__,"character %s not found in game %s",charname,game->name);
|
| 307 |
return -1;
|
| 308 |
}
|
| 309 |
if (list_remove_data(game->charlist,charinfo,&elem)) {
|
| 310 |
eventlog(eventlog_level_error,__FUNCTION__,"error remove character %s from game %s",charname,game->name);
|
| 311 |
return -1;
|
| 312 |
}
|
| 313 |
if (charinfo->charname) xfree((void *)charinfo->charname);
|
| 314 |
xfree(charinfo);
|
| 315 |
game->currchar--;
|
| 316 |
game->lastaccess_time=time(NULL);
|
| 317 |
eventlog(eventlog_level_info,__FUNCTION__,"removed character %s from game %s (%d left)",charname,game->name,game->currchar);
|
| 318 |
return 0;
|
| 319 |
}
|
| 320 |
|
| 321 |
extern int game_set_d2gs_gameid(t_game * game, unsigned int d2gs_gameid)
|
| 322 |
{
|
| 323 |
ASSERT(game,-1);
|
| 324 |
game->d2gs_gameid=d2gs_gameid;
|
| 325 |
return 0;
|
| 326 |
}
|
| 327 |
|
| 328 |
extern unsigned int game_get_d2gs_gameid(t_game const * game)
|
| 329 |
{
|
| 330 |
ASSERT(game,0);
|
| 331 |
return game->d2gs_gameid;
|
| 332 |
}
|
| 333 |
|
| 334 |
extern unsigned int d2cs_game_get_id(t_game const * game)
|
| 335 |
{
|
| 336 |
ASSERT(game,0);
|
| 337 |
return game->id;
|
| 338 |
}
|
| 339 |
|
| 340 |
extern unsigned int game_get_gameflag_ladder(t_game const * game)
|
| 341 |
{
|
| 342 |
ASSERT(game,0);
|
| 343 |
return gameflag_get_ladder(game->gameflag);
|
| 344 |
}
|
| 345 |
|
| 346 |
|
| 347 |
extern int game_set_d2gs(t_game * game, t_d2gs * gs)
|
| 348 |
{
|
| 349 |
ASSERT(game,-1);
|
| 350 |
game->d2gs=gs;
|
| 351 |
return 0;
|
| 352 |
}
|
| 353 |
|
| 354 |
extern t_d2gs * game_get_d2gs(t_game const * game)
|
| 355 |
{
|
| 356 |
ASSERT(game,NULL);
|
| 357 |
return game->d2gs;
|
| 358 |
}
|
| 359 |
|
| 360 |
extern int game_set_leveldiff(t_game * game, unsigned int leveldiff)
|
| 361 |
{
|
| 362 |
ASSERT(game,-1);
|
| 363 |
game->leveldiff=leveldiff;
|
| 364 |
return 0;
|
| 365 |
}
|
| 366 |
|
| 367 |
extern int game_set_charlevel(t_game * game, unsigned int charlevel)
|
| 368 |
{
|
| 369 |
ASSERT(game,-1);
|
| 370 |
game->charlevel=charlevel;
|
| 371 |
return 0;
|
| 372 |
}
|
| 373 |
|
| 374 |
extern unsigned int game_get_charlevel(t_game const * game)
|
| 375 |
{
|
| 376 |
ASSERT(game,0);
|
| 377 |
return game->charlevel;
|
| 378 |
}
|
| 379 |
|
| 380 |
extern unsigned int game_get_leveldiff(t_game const * game)
|
| 381 |
{
|
| 382 |
ASSERT(game,0);
|
| 383 |
return game->leveldiff;
|
| 384 |
}
|
| 385 |
|
| 386 |
extern unsigned int game_get_maxlevel(t_game const * game)
|
| 387 |
{
|
| 388 |
int maxlevel;
|
| 389 |
|
| 390 |
ASSERT(game,0);
|
| 391 |
maxlevel=game->charlevel+game->leveldiff;
|
| 392 |
if (maxlevel>0xff) maxlevel=0xff;
|
| 393 |
return maxlevel;
|
| 394 |
}
|
| 395 |
|
| 396 |
extern unsigned int game_get_minlevel(t_game const * game)
|
| 397 |
{
|
| 398 |
int minlevel;
|
| 399 |
|
| 400 |
ASSERT(game,0);
|
| 401 |
minlevel=game->charlevel-game->leveldiff;
|
| 402 |
if (minlevel<0) minlevel=0;
|
| 403 |
return minlevel;
|
| 404 |
}
|
| 405 |
|
| 406 |
extern unsigned int game_get_gameflag_expansion(t_game const * game)
|
| 407 |
{
|
| 408 |
ASSERT(game,0);
|
| 409 |
return gameflag_get_expansion(game->gameflag);
|
| 410 |
}
|
| 411 |
|
| 412 |
extern unsigned int game_get_gameflag_hardcore(t_game const * game)
|
| 413 |
{
|
| 414 |
ASSERT(game,0);
|
| 415 |
return gameflag_get_hardcore(game->gameflag);
|
| 416 |
}
|
| 417 |
|
| 418 |
extern unsigned int game_get_gameflag_difficulty(t_game const * game)
|
| 419 |
{
|
| 420 |
ASSERT(game,0);
|
| 421 |
return gameflag_get_difficulty(game->gameflag);
|
| 422 |
}
|
| 423 |
|
| 424 |
extern int game_set_gameflag_ladder(t_game * game, unsigned int ladder)
|
| 425 |
{
|
| 426 |
ASSERT(game,-1);
|
| 427 |
gameflag_set_ladder(game->gameflag,ladder);
|
| 428 |
return 0;
|
| 429 |
}
|
| 430 |
|
| 431 |
extern int game_set_gameflag_expansion(t_game * game, unsigned int expansion)
|
| 432 |
{
|
| 433 |
ASSERT(game,-1);
|
| 434 |
gameflag_set_expansion(game->gameflag,expansion);
|
| 435 |
return 0;
|
| 436 |
}
|
| 437 |
|
| 438 |
extern int game_set_gameflag_hardcore(t_game * game, unsigned int hardcore)
|
| 439 |
{
|
| 440 |
ASSERT(game,-1);
|
| 441 |
gameflag_set_hardcore(game->gameflag,hardcore);
|
| 442 |
return 0;
|
| 443 |
}
|
| 444 |
|
| 445 |
extern int game_set_gameflag_difficulty(t_game * game, unsigned int difficulty)
|
| 446 |
{
|
| 447 |
ASSERT(game,-1);
|
| 448 |
gameflag_set_difficulty(game->gameflag,difficulty);
|
| 449 |
return 0;
|
| 450 |
}
|
| 451 |
|
| 452 |
extern unsigned int game_get_created(t_game const * game)
|
| 453 |
{
|
| 454 |
ASSERT(game,0);
|
| 455 |
return game->created;
|
| 456 |
}
|
| 457 |
|
| 458 |
extern int game_set_created(t_game * game, unsigned int created)
|
| 459 |
{
|
| 460 |
ASSERT(game,-1);
|
| 461 |
game->created=created;
|
| 462 |
return 0;
|
| 463 |
}
|
| 464 |
|
| 465 |
extern unsigned int game_get_maxchar(t_game const * game)
|
| 466 |
{
|
| 467 |
ASSERT(game,0);
|
| 468 |
return game->maxchar;
|
| 469 |
}
|
| 470 |
|
| 471 |
extern int game_set_maxchar(t_game * game, unsigned int maxchar)
|
| 472 |
{
|
| 473 |
ASSERT(game,-1);
|
| 474 |
game->maxchar=maxchar;
|
| 475 |
return 0;
|
| 476 |
}
|
| 477 |
|
| 478 |
extern unsigned int game_get_currchar(t_game const * game)
|
| 479 |
{
|
| 480 |
ASSERT(game,0);
|
| 481 |
return game->currchar;
|
| 482 |
}
|
| 483 |
|
| 484 |
extern char const * d2cs_game_get_name(t_game const * game)
|
| 485 |
{
|
| 486 |
ASSERT(game,NULL);
|
| 487 |
return game->name;
|
| 488 |
}
|
| 489 |
|
| 490 |
extern char const * game_get_desc(t_game const * game)
|
| 491 |
{
|
| 492 |
ASSERT(game,NULL);
|
| 493 |
return game->desc;
|
| 494 |
}
|
| 495 |
|
| 496 |
extern char const * d2cs_game_get_pass(t_game const * game)
|
| 497 |
{
|
| 498 |
ASSERT(game,NULL);
|
| 499 |
return game->pass;
|
| 500 |
}
|
| 501 |
|
| 502 |
extern unsigned int game_get_gameflag(t_game const * game)
|
| 503 |
{
|
| 504 |
ASSERT(game,0);
|
| 505 |
return game->gameflag;
|
| 506 |
}
|
| 507 |
|
| 508 |
extern int d2cs_game_get_create_time(t_game const * game)
|
| 509 |
{
|
| 510 |
ASSERT(game,-1);
|
| 511 |
return game->create_time;
|
| 512 |
}
|
| 513 |
|
| 514 |
extern int game_set_create_time(t_game * game, int create_time)
|
| 515 |
{
|
| 516 |
ASSERT(game,-1);
|
| 517 |
game->create_time=create_time;
|
| 518 |
return 0;
|
| 519 |
}
|
| 520 |
|
| 521 |
extern t_list * game_get_charlist(t_game const * game)
|
| 522 |
{
|
| 523 |
ASSERT(game,NULL);
|
| 524 |
return game->charlist;
|
| 525 |
}
|
| 526 |
|
| 527 |
extern unsigned int gamelist_get_totalgame(void)
|
| 528 |
{
|
| 529 |
return total_game;
|
| 530 |
}
|