Debian Codegraph @

src/d2cs/game.cpp

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