1 /*
2  * Copyright (C) 1998,1999  Ross Combs (rocombs@cs.nmsu.edu)
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 #define CHARACTER_INTERNAL_ACCESS
19 #include "common/setup_before.h"
20 #include <stdio.h>
21 #ifdef HAVE_STDDEF_H
22 # include <stddef.h>
23 #else
24 # ifndef NULL
25 #  define NULL ((void *)0)
26 # endif
27 #endif
28 #ifdef STDC_HEADERS
29 # include <stdlib.h>
30 #endif
31 #ifdef HAVE_STRING_H
32 # include <string.h>
33 #else
34 # ifdef HAVE_STRINGS_H
35 #  include <strings.h>
36 # endif
37 #endif
38 #include "common/list.h"
39 #include "common/eventlog.h"
40 #include "common/util.h"
41 #include "common/bnettime.h"
42 #include "ladder.h"
43 #include "account.h"
44 #include "account_wrap.h"
45 #include "character.h"
46 #include "connection.h"
47 #include "compat/strcasecmp.h"
48 #include "compat/strncasecmp.h"
49 #include "common/bnet_protocol.h"
50 #include "common/tag.h"
51 #include "command.h"
52 #include "prefs.h"
53 #include "friends.h"
54 #include "clan.h"
55 #include "anongame_infos.h"
56 #include "team.h"
57 #include "common/setup_after.h"
58 
59 static unsigned int char_icon_to_uint(char * icon);
60 
account_get_numattr_real(t_account * account,char const * key,char const * fn,unsigned int ln)61 extern unsigned int account_get_numattr_real(t_account * account, char const * key, char const * fn, unsigned int ln)
62 {
63     char const * temp;
64     unsigned int val;
65 
66     if (!account)
67     {
68 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL account (from %s:%u)",fn,ln);
69 	return 0;
70     }
71     if (!key)
72     {
73 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL key (from %s:%u)",fn,ln);
74 	return 0;
75     }
76 
77     if (!(temp = account_get_strattr(account,key)))
78 	return 0;
79 
80     if (str_to_uint(temp,&val)<0)
81     {
82 	eventlog(eventlog_level_error,__FUNCTION__,"not a numeric string \"%s\" for key \"%s\"",temp,key);
83 	return 0;
84     }
85 
86     return val;
87 }
88 
89 
account_set_numattr(t_account * account,char const * key,unsigned int val)90 extern int account_set_numattr(t_account * account, char const * key, unsigned int val)
91 {
92     char temp[32]; /* should be more than enough room */
93 
94     if (!account)
95     {
96 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL account");
97 	return -1;
98     }
99     if (!key)
100     {
101 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL key");
102 	return -1;
103     }
104 
105     sprintf(temp,"%u",val);
106     return account_set_strattr(account,key,temp);
107 }
108 
109 
account_get_boolattr_real(t_account * account,char const * key,char const * fn,unsigned int ln)110 extern int account_get_boolattr_real(t_account * account, char const * key, char const * fn, unsigned int ln)
111 {
112     char const * temp;
113 
114     if (!account)
115     {
116 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL account (from %s:%u)",fn,ln);
117 	return -1;
118     }
119     if (!key)
120     {
121 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL key (from %s:%u)",fn,ln);
122 	return -1;
123     }
124 
125     if (!(temp = account_get_strattr(account,key)))
126 	return -1;
127 
128     switch (str_get_bool(temp))
129     {
130     case 1:
131 	return 1;
132     case 0:
133 	return 0;
134     default:
135 	eventlog(eventlog_level_error,__FUNCTION__,"bad boolean value \"%s\" for key \"%s\"",temp,key);
136 	return -1;
137     }
138 }
139 
140 
account_set_boolattr(t_account * account,char const * key,int val)141 extern int account_set_boolattr(t_account * account, char const * key, int val)
142 {
143     if (!account)
144     {
145 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL account");
146 	return -1;
147     }
148     if (!key)
149     {
150 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL key");
151 	return -1;
152     }
153 
154     return account_set_strattr(account,key,val?"true":"false");
155 }
156 
157 
158 /****************************************************************/
159 
160 
account_get_pass(t_account * account)161 extern char const * account_get_pass(t_account * account)
162 {
163     return account_get_strattr(account,"BNET\\acct\\passhash1");
164 }
165 
account_set_pass(t_account * account,char const * passhash1)166 extern int account_set_pass(t_account * account, char const * passhash1)
167 {
168     return account_set_strattr(account,"BNET\\acct\\passhash1",passhash1);
169 }
170 
171 
172 /****************************************************************/
173 
174 
account_get_auth_admin(t_account * account,char const * channelname)175 extern int account_get_auth_admin(t_account * account, char const * channelname)
176 {
177     char temp[256];
178 
179     if (!channelname)
180 	return account_get_boolattr(account, "BNET\\auth\\admin");
181 
182     sprintf(temp,"BNET\\auth\\admin\\%.100s",channelname);
183     return account_get_boolattr(account, temp);
184 }
185 
186 
account_set_auth_admin(t_account * account,char const * channelname,int val)187 extern int account_set_auth_admin(t_account * account, char const * channelname, int val)
188 {
189     char temp[256];
190 
191     if (!channelname)
192 	return account_set_boolattr(account, "BNET\\auth\\admin", val);
193 
194     sprintf(temp,"BNET\\auth\\admin\\%.100s",channelname);
195     return account_set_boolattr(account, temp, val);
196 }
197 
198 
account_get_auth_announce(t_account * account)199 extern int account_get_auth_announce(t_account * account)
200 {
201     return account_get_boolattr(account,"BNET\\auth\\announce");
202 }
203 
204 
account_get_auth_botlogin(t_account * account)205 extern int account_get_auth_botlogin(t_account * account)
206 {
207     return account_get_boolattr(account,"BNET\\auth\\botlogin");
208 }
209 
210 
account_get_auth_bnetlogin(t_account * account)211 extern int account_get_auth_bnetlogin(t_account * account)
212 {
213     return account_get_boolattr(account,"BNET\\auth\\normallogin");
214 }
215 
216 
account_get_auth_operator(t_account * account,char const * channelname)217 extern int account_get_auth_operator(t_account * account, char const * channelname)
218 {
219     char temp[256];
220 
221     if (!channelname)
222 	return account_get_boolattr(account,"BNET\\auth\\operator");
223 
224     sprintf(temp,"BNET\\auth\\operator\\%.100s",channelname);
225     return account_get_boolattr(account,temp);
226 }
227 
account_set_auth_operator(t_account * account,char const * channelname,int val)228 extern int account_set_auth_operator(t_account * account, char const * channelname, int val)
229 {
230     char temp[256];
231 
232     if (!channelname)
233 	return account_set_boolattr(account, "BNET\\auth\\operator", val);
234 
235     sprintf(temp,"BNET\\auth\\operator\\%.100s",channelname);
236     return account_set_boolattr(account, temp, val);
237 }
238 
account_get_auth_voice(t_account * account,char const * channelname)239 extern int account_get_auth_voice(t_account * account, char const * channelname)
240 {
241     char temp[256];
242 
243     sprintf(temp,"BNET\\auth\\voice\\%.100s",channelname);
244     return account_get_boolattr(account,temp);
245 }
246 
account_set_auth_voice(t_account * account,char const * channelname,int val)247 extern int account_set_auth_voice(t_account * account, char const * channelname, int val)
248 {
249     char temp[256];
250 
251     sprintf(temp,"BNET\\auth\\voice\\%.100s",channelname);
252     return account_set_boolattr(account, temp, val);
253 }
254 
account_get_auth_changepass(t_account * account)255 extern int account_get_auth_changepass(t_account * account)
256 {
257     return account_get_boolattr(account,"BNET\\auth\\changepass");
258 }
259 
260 
account_get_auth_changeprofile(t_account * account)261 extern int account_get_auth_changeprofile(t_account * account)
262 {
263     return account_get_boolattr(account,"BNET\\auth\\changeprofile");
264 }
265 
266 
account_get_auth_createnormalgame(t_account * account)267 extern int account_get_auth_createnormalgame(t_account * account)
268 {
269     return account_get_boolattr(account,"BNET\\auth\\createnormalgame");
270 }
271 
272 
account_get_auth_joinnormalgame(t_account * account)273 extern int account_get_auth_joinnormalgame(t_account * account)
274 {
275     return account_get_boolattr(account,"BNET\\auth\\joinnormalgame");
276 }
277 
278 
account_get_auth_createladdergame(t_account * account)279 extern int account_get_auth_createladdergame(t_account * account)
280 {
281     return account_get_boolattr(account,"BNET\\auth\\createladdergame");
282 }
283 
284 
account_get_auth_joinladdergame(t_account * account)285 extern int account_get_auth_joinladdergame(t_account * account)
286 {
287     return account_get_boolattr(account,"BNET\\auth\\joinladdergame");
288 }
289 
290 
account_get_auth_lock(t_account * account)291 extern int account_get_auth_lock(t_account * account)
292 {
293     return account_get_boolattr(account,"BNET\\auth\\lockk");
294 }
295 
296 
account_set_auth_lock(t_account * account,int val)297 extern int account_set_auth_lock(t_account * account, int val)
298 {
299     return account_set_boolattr(account,"BNET\\auth\\lockk",val);
300 }
301 
302 
303 
304 
305 /****************************************************************/
306 
307 
account_get_sex(t_account * account)308 extern char const * account_get_sex(t_account * account)
309 {
310     char const * temp;
311 
312     if (!account)
313     {
314 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL account");
315 	return NULL;
316     }
317 
318     if (!(temp = account_get_strattr(account,"profile\\sex")))
319 	return "";
320     return temp;
321 }
322 
323 
account_get_age(t_account * account)324 extern char const * account_get_age(t_account * account)
325 {
326     char const * temp;
327 
328     if (!account)
329     {
330 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL account");
331 	return NULL;
332     }
333 
334     if (!(temp = account_get_strattr(account,"profile\\age")))
335 	return "";
336     return temp;
337 }
338 
339 
account_get_loc(t_account * account)340 extern char const * account_get_loc(t_account * account)
341 {
342     char const * temp;
343 
344     if (!account)
345     {
346 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL account");
347 	return NULL;
348     }
349 
350     if (!(temp = account_get_strattr(account,"profile\\location")))
351 	return "";
352     return temp;
353 }
354 
355 
account_get_desc(t_account * account)356 extern char const * account_get_desc(t_account * account)
357 {
358     char const * temp;
359 
360     if (!account)
361     {
362 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL account");
363 	return NULL;
364     }
365 
366     if (!(temp = account_get_strattr(account,"profile\\description")))
367 	return "";
368     return temp;
369 }
370 
371 
372 /****************************************************************/
373 
374 
account_get_ll_time(t_account * account)375 extern unsigned int account_get_ll_time(t_account * account)
376 {
377     return account_get_numattr(account,"BNET\\acct\\lastlogin_time");
378 }
379 
380 
account_set_ll_time(t_account * account,unsigned int t)381 extern int account_set_ll_time(t_account * account, unsigned int t)
382 {
383     return account_set_numattr(account,"BNET\\acct\\lastlogin_time",t);
384 }
385 
386 
387 
account_get_ll_clienttag(t_account * account)388 extern t_clienttag account_get_ll_clienttag(t_account * account)
389 {
390     char const * clienttag;
391     t_clienttag clienttag_uint;
392 
393     clienttag = account_get_strattr(account,"BNET\\acct\\lastlogin_clienttag");
394     clienttag_uint= tag_str_to_uint(clienttag);
395 
396     return clienttag_uint;
397 }
398 
account_set_ll_clienttag(t_account * account,t_clienttag clienttag)399 extern int account_set_ll_clienttag(t_account * account, t_clienttag clienttag)
400 {
401     char clienttag_str[5];
402 
403     return account_set_strattr(account,"BNET\\acct\\lastlogin_clienttag",tag_uint_to_str(clienttag_str,clienttag));
404 }
405 
406 
account_get_ll_user(t_account * account)407 extern char const * account_get_ll_user(t_account * account)
408 {
409     return account_get_strattr(account,"BNET\\acct\\lastlogin_user");
410 }
411 
412 
account_set_ll_user(t_account * account,char const * user)413 extern int account_set_ll_user(t_account * account, char const * user)
414 {
415     return account_set_strattr(account,"BNET\\acct\\lastlogin_user",user);
416 }
417 
418 
account_get_ll_owner(t_account * account)419 extern char const * account_get_ll_owner(t_account * account)
420 {
421     return account_get_strattr(account,"BNET\\acct\\lastlogin_owner");
422 }
423 
424 
account_set_ll_owner(t_account * account,char const * owner)425 extern int account_set_ll_owner(t_account * account, char const * owner)
426 {
427     return account_set_strattr(account,"BNET\\acct\\lastlogin_owner",owner);
428 }
429 
430 
account_get_ll_ip(t_account * account)431 extern char const * account_get_ll_ip(t_account * account)
432 {
433     return account_get_strattr(account,"BNET\\acct\\lastlogin_ip");
434 }
435 
436 
account_set_ll_ip(t_account * account,char const * ip)437 extern int account_set_ll_ip(t_account * account, char const * ip)
438 {
439     return account_set_strattr(account,"BNET\\acct\\lastlogin_ip",ip);
440 }
441 
442 /****************************************************************/
443 
444 
account_get_normal_wins(t_account * account,t_clienttag clienttag)445 extern unsigned int account_get_normal_wins(t_account * account, t_clienttag clienttag)
446 {
447     char key[256];
448     char clienttag_str[5];
449 
450     if (!clienttag)
451     {
452 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
453 	return 0;
454     }
455     sprintf(key,"Record\\%s\\0\\wins",tag_uint_to_str(clienttag_str,clienttag));
456     return account_get_numattr(account,key);
457 }
458 
459 
account_inc_normal_wins(t_account * account,t_clienttag clienttag)460 extern int account_inc_normal_wins(t_account * account, t_clienttag clienttag)
461 {
462     char key[256];
463     char clienttag_str[5];
464 
465     if (!clienttag)
466     {
467 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
468 	return -1;
469     }
470     sprintf(key,"Record\\%s\\0\\wins",tag_uint_to_str(clienttag_str,clienttag));
471     return account_set_numattr(account,key,account_get_normal_wins(account,clienttag)+1);
472 }
473 
474 
account_set_normal_wins(t_account * account,t_clienttag clienttag,unsigned wins)475 extern int account_set_normal_wins(t_account * account, t_clienttag clienttag, unsigned wins)
476 {
477     char key[256];
478     char clienttag_str[5];
479 
480     if (!clienttag) {
481 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
482 	return -1;
483     }
484 
485     sprintf(key,"Record\\%s\\0\\wins",tag_uint_to_str(clienttag_str,clienttag));
486     return account_set_numattr(account,key,wins);
487 }
488 
489 
account_get_normal_losses(t_account * account,t_clienttag clienttag)490 extern unsigned int account_get_normal_losses(t_account * account, t_clienttag clienttag)
491 {
492     char key[256];
493     char clienttag_str[5];
494 
495     if (!clienttag)
496     {
497 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
498 	return 0;
499     }
500     sprintf(key,"Record\\%s\\0\\losses",tag_uint_to_str(clienttag_str,clienttag));
501     return account_get_numattr(account,key);
502 }
503 
504 
account_inc_normal_losses(t_account * account,t_clienttag clienttag)505 extern int account_inc_normal_losses(t_account * account, t_clienttag clienttag)
506 {
507     char key[256];
508     char clienttag_str[5];
509 
510     if (!clienttag)
511     {
512 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
513 	return -1;
514     }
515     sprintf(key,"Record\\%s\\0\\losses",tag_uint_to_str(clienttag_str,clienttag));
516     return account_set_numattr(account,key,account_get_normal_losses(account,clienttag)+1);
517 }
518 
519 
account_set_normal_losses(t_account * account,t_clienttag clienttag,unsigned losses)520 extern int account_set_normal_losses(t_account * account, t_clienttag clienttag,unsigned losses)
521 {
522     char key[256];
523     char clienttag_str[5];
524 
525     if (!clienttag) {
526 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
527 	return -1;
528     }
529     sprintf(key,"Record\\%s\\0\\losses",tag_uint_to_str(clienttag_str,clienttag));
530     return account_set_numattr(account,key,losses);
531 }
532 
533 
account_get_normal_draws(t_account * account,t_clienttag clienttag)534 extern unsigned int account_get_normal_draws(t_account * account, t_clienttag clienttag)
535 {
536     char key[256];
537     char clienttag_str[5];
538 
539     if (!clienttag)
540     {
541 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
542 	return 0;
543     }
544     sprintf(key,"Record\\%s\\0\\draws",tag_uint_to_str(clienttag_str,clienttag));
545     return account_get_numattr(account,key);
546 }
547 
548 
account_inc_normal_draws(t_account * account,t_clienttag clienttag)549 extern int account_inc_normal_draws(t_account * account, t_clienttag clienttag)
550 {
551     char key[256];
552     char clienttag_str[5];
553 
554     if (!clienttag)
555     {
556 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
557 	return -1;
558     }
559     sprintf(key,"Record\\%s\\0\\draws",tag_uint_to_str(clienttag_str,clienttag));
560     return account_set_numattr(account,key,account_get_normal_draws(account,clienttag)+1);
561 }
562 
563 
account_get_normal_disconnects(t_account * account,t_clienttag clienttag)564 extern unsigned int account_get_normal_disconnects(t_account * account, t_clienttag clienttag)
565 {
566     char key[256];
567     char clienttag_str[5];
568 
569     if (!clienttag)
570     {
571 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
572 	return 0;
573     }
574     sprintf(key,"Record\\%s\\0\\disconnects",tag_uint_to_str(clienttag_str,clienttag));
575     return account_get_numattr(account,key);
576 }
577 
578 
account_inc_normal_disconnects(t_account * account,t_clienttag clienttag)579 extern int account_inc_normal_disconnects(t_account * account, t_clienttag clienttag)
580 {
581     char key[256];
582     char clienttag_str[5];
583 
584     if (!clienttag)
585     {
586 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
587 	return -1;
588     }
589     sprintf(key,"Record\\%s\\0\\disconnects",tag_uint_to_str(clienttag_str,clienttag));
590     return account_set_numattr(account,key,account_get_normal_disconnects(account,clienttag)+1);
591 }
592 
593 
account_set_normal_disconnects(t_account * account,t_clienttag clienttag,unsigned discs)594 extern int account_set_normal_disconnects(t_account * account, t_clienttag clienttag,unsigned discs)
595 {
596     char key[256];
597     char clienttag_str[5];
598 
599     if (!clienttag) {
600 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
601 	return -1;
602     }
603     sprintf(key,"Record\\%s\\0\\disconnects",tag_uint_to_str(clienttag_str,clienttag));
604     return account_set_numattr(account,key,discs);
605 }
606 
607 
account_set_normal_last_time(t_account * account,t_clienttag clienttag,t_bnettime t)608 extern int account_set_normal_last_time(t_account * account, t_clienttag clienttag, t_bnettime t)
609 {
610     char key[256];
611     char clienttag_str[5];
612 
613     if (!clienttag)
614     {
615 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
616 	return -1;
617     }
618     sprintf(key,"Record\\%s\\0\\last game",tag_uint_to_str(clienttag_str,clienttag));
619     return account_set_strattr(account,key,bnettime_get_str(t));
620 }
621 
622 
account_set_normal_last_result(t_account * account,t_clienttag clienttag,char const * result)623 extern int account_set_normal_last_result(t_account * account, t_clienttag clienttag, char const * result)
624 {
625     char key[256];
626     char clienttag_str[5];
627 
628     if (!clienttag)
629     {
630 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
631 	return -1;
632     }
633     sprintf(key,"Record\\%s\\0\\last game result",tag_uint_to_str(clienttag_str,clienttag));
634     return account_set_strattr(account,key,result);
635 }
636 
637 
638 /****************************************************************/
639 
640 
account_get_ladder_active_wins(t_account * account,t_clienttag clienttag,t_ladder_id id)641 extern unsigned int account_get_ladder_active_wins(t_account * account, t_clienttag clienttag, t_ladder_id id)
642 {
643     char key[256];
644     char clienttag_str[5];
645 
646     if (!clienttag)
647     {
648 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
649 	return 0;
650     }
651     sprintf(key,"Record\\%s\\%d\\active wins",tag_uint_to_str(clienttag_str,clienttag),(int)id);
652     return account_get_numattr(account,key);
653 }
654 
655 
account_set_ladder_active_wins(t_account * account,t_clienttag clienttag,t_ladder_id id,unsigned int wins)656 extern int account_set_ladder_active_wins(t_account * account, t_clienttag clienttag, t_ladder_id id, unsigned int wins)
657 {
658     char key[256];
659     char clienttag_str[5];
660 
661     if (!clienttag)
662     {
663 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
664 	return -1;
665     }
666     sprintf(key,"Record\\%s\\%d\\active wins",tag_uint_to_str(clienttag_str,clienttag),(int)id);
667     return account_set_numattr(account,key,wins);
668 }
669 
670 
account_get_ladder_active_losses(t_account * account,t_clienttag clienttag,t_ladder_id id)671 extern unsigned int account_get_ladder_active_losses(t_account * account, t_clienttag clienttag, t_ladder_id id)
672 {
673     char key[256];
674     char clienttag_str[5];
675 
676     if (!clienttag)
677     {
678 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
679 	return 0;
680     }
681     sprintf(key,"Record\\%s\\%d\\active losses",tag_uint_to_str(clienttag_str,clienttag),(int)id);
682     return account_get_numattr(account,key);
683 }
684 
685 
account_set_ladder_active_losses(t_account * account,t_clienttag clienttag,t_ladder_id id,unsigned int losses)686 extern int account_set_ladder_active_losses(t_account * account, t_clienttag clienttag, t_ladder_id id, unsigned int losses)
687 {
688     char key[256];
689     char clienttag_str[5];
690 
691     if (!clienttag)
692     {
693 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
694 	return -1;
695     }
696     sprintf(key,"Record\\%s\\%d\\active losses",tag_uint_to_str(clienttag_str,clienttag),(int)id);
697     return account_set_numattr(account,key,losses);
698 }
699 
700 
account_get_ladder_active_draws(t_account * account,t_clienttag clienttag,t_ladder_id id)701 extern unsigned int account_get_ladder_active_draws(t_account * account, t_clienttag clienttag, t_ladder_id id)
702 {
703     char key[256];
704     char clienttag_str[5];
705 
706     if (!clienttag)
707     {
708 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
709 	return 0;
710     }
711     sprintf(key,"Record\\%s\\%d\\active draws",tag_uint_to_str(clienttag_str,clienttag),(int)id);
712     return account_get_numattr(account,key);
713 }
714 
715 
account_set_ladder_active_draws(t_account * account,t_clienttag clienttag,t_ladder_id id,unsigned int draws)716 extern int account_set_ladder_active_draws(t_account * account, t_clienttag clienttag, t_ladder_id id, unsigned int draws)
717 {
718     char key[256];
719     char clienttag_str[5];
720 
721     if (!clienttag)
722     {
723 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
724 	return -1;
725     }
726     sprintf(key,"Record\\%s\\%d\\active draws",tag_uint_to_str(clienttag_str,clienttag),(int)id);
727     return account_set_numattr(account,key,draws);
728 }
729 
730 
account_get_ladder_active_disconnects(t_account * account,t_clienttag clienttag,t_ladder_id id)731 extern unsigned int account_get_ladder_active_disconnects(t_account * account, t_clienttag clienttag, t_ladder_id id)
732 {
733     char key[256];
734     char clienttag_str[5];
735 
736     if (!clienttag)
737     {
738 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
739 	return 0;
740     }
741     sprintf(key,"Record\\%s\\%d\\active disconnects",tag_uint_to_str(clienttag_str,clienttag),(int)id);
742     return account_get_numattr(account,key);
743 }
744 
745 
account_set_ladder_active_disconnects(t_account * account,t_clienttag clienttag,t_ladder_id id,unsigned int disconnects)746 extern int account_set_ladder_active_disconnects(t_account * account, t_clienttag clienttag, t_ladder_id id, unsigned int disconnects)
747 {
748     char key[256];
749     char clienttag_str[5];
750 
751     if (!clienttag)
752     {
753 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
754 	return -1;
755     }
756     sprintf(key,"Record\\%s\\%d\\active disconnects",tag_uint_to_str(clienttag_str,clienttag),(int)id);
757     return account_set_numattr(account,key,disconnects);
758 }
759 
760 
account_get_ladder_active_rating(t_account * account,t_clienttag clienttag,t_ladder_id id)761 extern unsigned int account_get_ladder_active_rating(t_account * account, t_clienttag clienttag, t_ladder_id id)
762 {
763     char key[256];
764     char clienttag_str[5];
765 
766     if (!clienttag)
767     {
768 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
769 	return 0;
770     }
771     sprintf(key,"Record\\%s\\%d\\active rating",tag_uint_to_str(clienttag_str,clienttag),(int)id);
772     return account_get_numattr(account,key);
773 }
774 
775 
account_set_ladder_active_rating(t_account * account,t_clienttag clienttag,t_ladder_id id,unsigned int rating)776 extern int account_set_ladder_active_rating(t_account * account, t_clienttag clienttag, t_ladder_id id, unsigned int rating)
777 {
778     char key[256];
779     char clienttag_str[5];
780 
781     if (!clienttag)
782     {
783 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
784 	return -1;
785     }
786     sprintf(key,"Record\\%s\\%d\\active rating",tag_uint_to_str(clienttag_str,clienttag),(int)id);
787     return account_set_numattr(account,key,rating);
788 }
789 
790 
account_get_ladder_active_rank(t_account * account,t_clienttag clienttag,t_ladder_id id)791 extern int account_get_ladder_active_rank(t_account * account, t_clienttag clienttag, t_ladder_id id)
792 {
793     char key[256];
794     char clienttag_str[5];
795 
796     if (!clienttag)
797     {
798 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
799 	return 0;
800     }
801     sprintf(key,"Record\\%s\\%d\\active rank",tag_uint_to_str(clienttag_str,clienttag),(int)id);
802     return account_get_numattr(account,key);
803 }
804 
805 
account_set_ladder_active_rank(t_account * account,t_clienttag clienttag,t_ladder_id id,unsigned int rank)806 extern int account_set_ladder_active_rank(t_account * account, t_clienttag clienttag, t_ladder_id id, unsigned int rank)
807 {
808     char key[256];
809     char clienttag_str[5];
810 
811     if (!clienttag)
812     {
813 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
814 	return -1;
815     }
816     sprintf(key,"Record\\%s\\%d\\active rank",tag_uint_to_str(clienttag_str,clienttag),(int)id);
817     return account_set_numattr(account,key,rank);
818 }
819 
820 
account_get_ladder_active_last_time(t_account * account,t_clienttag clienttag,t_ladder_id id)821 extern char const * account_get_ladder_active_last_time(t_account * account, t_clienttag clienttag, t_ladder_id id)
822 {
823     char key[256];
824     char clienttag_str[5];
825 
826     if (!clienttag)
827     {
828 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
829 	return NULL;
830     }
831     sprintf(key,"Record\\%s\\%d\\active last game",tag_uint_to_str(clienttag_str,clienttag),(int)id);
832     return account_get_strattr(account,key);
833 }
834 
835 
account_set_ladder_active_last_time(t_account * account,t_clienttag clienttag,t_ladder_id id,t_bnettime t)836 extern int account_set_ladder_active_last_time(t_account * account, t_clienttag clienttag, t_ladder_id id, t_bnettime t)
837 {
838     char key[256];
839     char clienttag_str[5];
840 
841     if (!clienttag)
842     {
843 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
844 	return -1;
845     }
846     sprintf(key,"Record\\%s\\%d\\active last game",tag_uint_to_str(clienttag_str,clienttag),(int)id);
847     return account_set_strattr(account,key,bnettime_get_str(t));
848 }
849 
850 /****************************************************************/
851 
852 
account_get_ladder_wins(t_account * account,t_clienttag clienttag,t_ladder_id id)853 extern unsigned int account_get_ladder_wins(t_account * account, t_clienttag clienttag, t_ladder_id id)
854 {
855     char key[256];
856     char clienttag_str[5];
857 
858     if (!clienttag)
859     {
860 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
861 	return 0;
862     }
863     sprintf(key,"Record\\%s\\%s\\wins",tag_uint_to_str(clienttag_str,clienttag),ladder_id_str[(int)id]);
864     return account_get_numattr(account,key);
865 }
866 
867 
account_inc_ladder_wins(t_account * account,t_clienttag clienttag,t_ladder_id id)868 extern int account_inc_ladder_wins(t_account * account, t_clienttag clienttag, t_ladder_id id)
869 {
870     char key[256];
871     char clienttag_str[5];
872 
873     if (!clienttag)
874     {
875 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
876 	return -1;
877     }
878     sprintf(key,"Record\\%s\\%s\\wins",tag_uint_to_str(clienttag_str,clienttag),ladder_id_str[(int)id]);
879     return account_set_numattr(account,key,account_get_ladder_wins(account,clienttag,id)+1);
880 }
881 
882 
account_set_ladder_wins(t_account * account,t_clienttag clienttag,t_ladder_id id,unsigned wins)883 extern int account_set_ladder_wins(t_account * account, t_clienttag clienttag, t_ladder_id id,unsigned wins)
884 {
885     char key[256];
886     char clienttag_str[5];
887 
888     if (!clienttag) {
889 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
890 	return -1;
891     }
892     sprintf(key,"Record\\%s\\%s\\wins",tag_uint_to_str(clienttag_str,clienttag),ladder_id_str[(int)id]);
893     return account_set_numattr(account,key,wins);
894 }
895 
896 
account_get_ladder_losses(t_account * account,t_clienttag clienttag,t_ladder_id id)897 extern unsigned int account_get_ladder_losses(t_account * account, t_clienttag clienttag, t_ladder_id id)
898 {
899     char key[256];
900     char clienttag_str[5];
901 
902     if (!clienttag)
903     {
904 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
905 	return 0;
906     }
907     sprintf(key,"Record\\%s\\%s\\losses",tag_uint_to_str(clienttag_str,clienttag),ladder_id_str[(int)id]);
908     return account_get_numattr(account,key);
909 }
910 
911 
account_inc_ladder_losses(t_account * account,t_clienttag clienttag,t_ladder_id id)912 extern int account_inc_ladder_losses(t_account * account, t_clienttag clienttag, t_ladder_id id)
913 {
914     char key[256];
915     char clienttag_str[5];
916 
917     if (!clienttag)
918     {
919        eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
920        return -1;
921     }
922     sprintf(key,"Record\\%s\\%s\\losses",tag_uint_to_str(clienttag_str,clienttag),ladder_id_str[(int)id]);
923     return account_set_numattr(account,key,account_get_ladder_losses(account,clienttag,id)+1);
924 }
925 
926 
account_set_ladder_losses(t_account * account,t_clienttag clienttag,t_ladder_id id,unsigned losses)927 extern int account_set_ladder_losses(t_account * account, t_clienttag clienttag, t_ladder_id id,unsigned losses)
928 {
929     char key[256];
930     char clienttag_str[5];
931 
932     if (!clienttag) {
933        eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
934        return -1;
935     }
936     sprintf(key,"Record\\%s\\%s\\losses",tag_uint_to_str(clienttag_str,clienttag),ladder_id_str[(int)id]);
937     return account_set_numattr(account,key,losses);
938 }
939 
940 
account_get_ladder_draws(t_account * account,t_clienttag clienttag,t_ladder_id id)941 extern unsigned int account_get_ladder_draws(t_account * account, t_clienttag clienttag, t_ladder_id id)
942 {
943     char key[256];
944     char clienttag_str[5];
945 
946     if (!clienttag)
947     {
948 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
949 	return 0;
950     }
951     sprintf(key,"Record\\%s\\%d\\draws",tag_uint_to_str(clienttag_str,clienttag),(int)id);
952     return account_get_numattr(account,key);
953 }
954 
955 
account_inc_ladder_draws(t_account * account,t_clienttag clienttag,t_ladder_id id)956 extern int account_inc_ladder_draws(t_account * account, t_clienttag clienttag, t_ladder_id id)
957 {
958     char key[256];
959     char clienttag_str[5];
960 
961     if (!clienttag)
962     {
963        eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
964        return -1;
965     }
966     sprintf(key,"Record\\%s\\%d\\draws",tag_uint_to_str(clienttag_str,clienttag),(int)id);
967     return account_set_numattr(account,key,account_get_ladder_draws(account,clienttag,id)+1);
968 }
969 
970 
account_get_ladder_disconnects(t_account * account,t_clienttag clienttag,t_ladder_id id)971 extern unsigned int account_get_ladder_disconnects(t_account * account, t_clienttag clienttag, t_ladder_id id)
972 {
973     char key[256];
974     char clienttag_str[5];
975 
976     if (!clienttag)
977     {
978 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
979 	return 0;
980     }
981     sprintf(key,"Record\\%s\\%d\\disconnects",tag_uint_to_str(clienttag_str,clienttag),(int)id);
982     return account_get_numattr(account,key);
983 }
984 
985 
account_inc_ladder_disconnects(t_account * account,t_clienttag clienttag,t_ladder_id id)986 extern int account_inc_ladder_disconnects(t_account * account, t_clienttag clienttag, t_ladder_id id)
987 {
988     char key[256];
989     char clienttag_str[5];
990 
991     if (!clienttag)
992     {
993        eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
994        return -1;
995     }
996     sprintf(key,"Record\\%s\\%d\\disconnects",tag_uint_to_str(clienttag_str,clienttag),(int)id);
997     return account_set_numattr(account,key,account_get_ladder_disconnects(account,clienttag,id)+1);
998 }
999 
1000 
account_set_ladder_disconnects(t_account * account,t_clienttag clienttag,t_ladder_id id,unsigned discs)1001 extern int account_set_ladder_disconnects(t_account * account, t_clienttag clienttag, t_ladder_id id,unsigned discs)
1002 {
1003     char key[256];
1004     char clienttag_str[5];
1005 
1006     if (!clienttag) {
1007        eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
1008        return -1;
1009     }
1010     sprintf(key,"Record\\%s\\%d\\disconnects",tag_uint_to_str(clienttag_str,clienttag),(int)id);
1011     return account_set_numattr(account,key,discs);
1012 }
1013 
1014 
account_get_ladder_rating(t_account * account,t_clienttag clienttag,t_ladder_id id)1015 extern unsigned int account_get_ladder_rating(t_account * account, t_clienttag clienttag, t_ladder_id id)
1016 {
1017     char key[256];
1018     char clienttag_str[5];
1019 
1020     if (!clienttag)
1021     {
1022 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
1023 	return 0;
1024     }
1025     sprintf(key,"Record\\%s\\%d\\rating",tag_uint_to_str(clienttag_str,clienttag),(int)id);
1026     return account_get_numattr(account,key);
1027 }
1028 
1029 
account_set_ladder_rating(t_account * account,t_clienttag clienttag,t_ladder_id id,unsigned rating)1030 extern int account_set_ladder_rating(t_account * account, t_clienttag clienttag, t_ladder_id id,unsigned rating)
1031 {
1032     char key[256];
1033     char clienttag_str[5];
1034 
1035     if (!clienttag)
1036     {
1037 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
1038 	return 0;
1039     }
1040     sprintf(key,"Record\\%s\\%d\\rating",tag_uint_to_str(clienttag_str,clienttag),(int)id);
1041     return account_set_numattr(account,key,rating);
1042 }
1043 
1044 
account_adjust_ladder_rating(t_account * account,t_clienttag clienttag,t_ladder_id id,int delta)1045 extern int account_adjust_ladder_rating(t_account * account, t_clienttag clienttag, t_ladder_id id, int delta)
1046 {
1047     char         key[256];
1048     char clienttag_str[5];
1049     unsigned int oldrating;
1050     unsigned int newrating;
1051     int          retval=0;
1052 
1053     if (!clienttag)
1054     {
1055        eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
1056        return -1;
1057     }
1058     sprintf(key,"Record\\%s\\%d\\rating",tag_uint_to_str(clienttag_str,clienttag),(int)id);
1059     /* don't allow rating to go below 1 */
1060     oldrating = account_get_ladder_rating(account,clienttag,id);
1061     if (delta<0 && oldrating<=(unsigned int)-delta)
1062         newrating = 1;
1063     else
1064         newrating = oldrating+delta;
1065     if (account_set_numattr(account,key,newrating)<0)
1066 	retval = -1;
1067 
1068     if (newrating>account_get_ladder_high_rating(account,clienttag,id))
1069     {
1070 	sprintf(key,"Record\\%s\\%d\\high rating",tag_uint_to_str(clienttag_str,clienttag),(int)id);
1071 	if (account_set_numattr(account,key,newrating)<0)
1072 	    retval = -1;
1073     }
1074 
1075     return retval;
1076 }
1077 
1078 
1079 
account_get_ladder_rank(t_account * account,t_clienttag clienttag,t_ladder_id id)1080 extern int account_get_ladder_rank(t_account * account, t_clienttag clienttag, t_ladder_id id)
1081 {
1082     char key[256];
1083     char clienttag_str[5];
1084 
1085     if (!clienttag)
1086     {
1087 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
1088 	return 0;
1089     }
1090     sprintf(key,"Record\\%s\\%s\\rank",tag_uint_to_str(clienttag_str,clienttag),ladder_id_str[(int)id]);
1091     return account_get_numattr(account,key);
1092 }
1093 
1094 
account_set_ladder_rank(t_account * account,t_clienttag clienttag,t_ladder_id id,unsigned int rank)1095 extern int account_set_ladder_rank(t_account * account, t_clienttag clienttag, t_ladder_id id, unsigned int rank)
1096 {
1097     char         key[256];
1098     char clienttag_str[5];
1099     unsigned int oldrank;
1100     int          retval=0;
1101 
1102     if (!clienttag)
1103     {
1104        eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
1105        return -1;
1106     }
1107     // if (rank==0)
1108     //    eventlog(eventlog_level_warn,__FUNCTION__,"setting rank to zero?");
1109     sprintf(key,"Record\\%s\\%s\\rank",tag_uint_to_str(clienttag_str,clienttag),ladder_id_str[(int)id]);
1110     if (account_set_numattr(account,key,rank)<0)
1111 	retval = -1;
1112 
1113     oldrank = account_get_ladder_high_rank(account,clienttag,id);
1114     if (oldrank==0 || rank<oldrank)
1115     {
1116 	sprintf(key,"Record\\%s\\%s\\high rank",tag_uint_to_str(clienttag_str,clienttag),ladder_id_str[(int)id]);
1117 	if (account_set_numattr(account,key,rank)<0)
1118 	    retval = -1;
1119     }
1120     return retval;
1121 }
1122 
account_get_ladder_high_rating(t_account * account,t_clienttag clienttag,t_ladder_id id)1123 extern unsigned int account_get_ladder_high_rating(t_account * account, t_clienttag clienttag, t_ladder_id id)
1124 {
1125     char key[256];
1126     char clienttag_str[5];
1127 
1128     if (!clienttag)
1129     {
1130 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
1131 	return 0;
1132     }
1133     sprintf(key,"Record\\%s\\%d\\high rating",tag_uint_to_str(clienttag_str,clienttag),(int)id);
1134     return account_get_numattr(account,key);
1135 }
1136 
1137 
account_get_ladder_high_rank(t_account * account,t_clienttag clienttag,t_ladder_id id)1138 extern unsigned int account_get_ladder_high_rank(t_account * account, t_clienttag clienttag, t_ladder_id id)
1139 {
1140     char key[256];
1141     char clienttag_str[5];
1142 
1143     if (!clienttag)
1144     {
1145 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
1146 	return 0;
1147     }
1148     sprintf(key,"Record\\%s\\%d\\high rank",tag_uint_to_str(clienttag_str,clienttag),(int)id);
1149     return account_get_numattr(account,key);
1150 }
1151 
1152 
account_set_ladder_last_time(t_account * account,t_clienttag clienttag,t_ladder_id id,t_bnettime t)1153 extern int account_set_ladder_last_time(t_account * account, t_clienttag clienttag, t_ladder_id id, t_bnettime t)
1154 {
1155     char key[256];
1156     char clienttag_str[5];
1157 
1158     if (!clienttag)
1159     {
1160 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
1161 	return -1;
1162     }
1163     sprintf(key,"Record\\%s\\%d\\last game",tag_uint_to_str(clienttag_str,clienttag),(int)id);
1164     return account_set_strattr(account,key,bnettime_get_str(t));
1165 }
1166 
1167 
account_get_ladder_last_time(t_account * account,t_clienttag clienttag,t_ladder_id id)1168 extern char const * account_get_ladder_last_time(t_account * account, t_clienttag clienttag, t_ladder_id id)
1169 {
1170     char key[256];
1171     char clienttag_str[5];
1172 
1173     if (!clienttag)
1174     {
1175 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
1176 	return NULL;
1177     }
1178     sprintf(key,"Record\\%s\\%d\\last game",tag_uint_to_str(clienttag_str,clienttag),(int)id);
1179     return account_get_strattr(account,key);
1180 }
1181 
1182 
account_set_ladder_last_result(t_account * account,t_clienttag clienttag,t_ladder_id id,char const * result)1183 extern int account_set_ladder_last_result(t_account * account, t_clienttag clienttag, t_ladder_id id, char const * result)
1184 {
1185     char key[256];
1186     char clienttag_str[5];
1187 
1188     if (!clienttag)
1189     {
1190 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
1191 	return -1;
1192     }
1193     sprintf(key,"Record\\%s\\%d\\last game result",tag_uint_to_str(clienttag_str,clienttag),(int)id);
1194     return account_set_strattr(account,key,result);
1195 }
1196 
1197 
1198 /****************************************************************/
1199 
1200 
account_get_normal_level(t_account * account,t_clienttag clienttag)1201 extern unsigned int account_get_normal_level(t_account * account, t_clienttag clienttag)
1202 {
1203     char key[256];
1204     char clienttag_str[5];
1205 
1206     if (!clienttag)
1207     {
1208 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
1209 	return 0;
1210     }
1211     sprintf(key,"Record\\%s\\0\\level",tag_uint_to_str(clienttag_str,clienttag));
1212     return account_get_numattr(account,key);
1213 }
1214 
1215 
account_set_normal_level(t_account * account,t_clienttag clienttag,unsigned int level)1216 extern int account_set_normal_level(t_account * account, t_clienttag clienttag, unsigned int level)
1217 {
1218     char key[256];
1219     char clienttag_str[5];
1220 
1221     if (!clienttag)
1222     {
1223 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
1224 	return -1;
1225     }
1226     sprintf(key,"Record\\%s\\0\\level",tag_uint_to_str(clienttag_str,clienttag));
1227     return account_set_numattr(account,key,level);
1228 }
1229 
1230 
account_get_normal_class(t_account * account,t_clienttag clienttag)1231 extern unsigned int account_get_normal_class(t_account * account, t_clienttag clienttag)
1232 {
1233     char key[256];
1234     char clienttag_str[5];
1235 
1236     if (!clienttag)
1237     {
1238 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
1239 	return 0;
1240     }
1241     sprintf(key,"Record\\%s\\0\\class",tag_uint_to_str(clienttag_str,clienttag));
1242     return account_get_numattr(account,key);
1243 }
1244 
1245 
account_set_normal_class(t_account * account,t_clienttag clienttag,unsigned int class)1246 extern int account_set_normal_class(t_account * account, t_clienttag clienttag, unsigned int class)
1247 {
1248     char key[256];
1249     char clienttag_str[5];
1250 
1251     if (!clienttag)
1252     {
1253 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
1254 	return -1;
1255     }
1256     sprintf(key,"Record\\%s\\0\\class",tag_uint_to_str(clienttag_str,clienttag));
1257     return account_set_numattr(account,key,class);
1258 }
1259 
1260 
account_get_normal_diablo_kills(t_account * account,t_clienttag clienttag)1261 extern unsigned int account_get_normal_diablo_kills(t_account * account, t_clienttag clienttag)
1262 {
1263     char key[256];
1264     char clienttag_str[5];
1265 
1266     if (!clienttag)
1267     {
1268 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
1269 	return 0;
1270     }
1271     sprintf(key,"Record\\%s\\0\\diablo kills",tag_uint_to_str(clienttag_str,clienttag));
1272     return account_get_numattr(account,key);
1273 }
1274 
1275 
account_set_normal_diablo_kills(t_account * account,t_clienttag clienttag,unsigned int diablo_kills)1276 extern int account_set_normal_diablo_kills(t_account * account, t_clienttag clienttag, unsigned int diablo_kills)
1277 {
1278     char key[256];
1279     char clienttag_str[5];
1280 
1281     if (!clienttag)
1282     {
1283 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
1284 	return -1;
1285     }
1286     sprintf(key,"Record\\%s\\0\\diablo kills",tag_uint_to_str(clienttag_str,clienttag));
1287     return account_set_numattr(account,key,diablo_kills);
1288 }
1289 
1290 
account_get_normal_strength(t_account * account,t_clienttag clienttag)1291 extern unsigned int account_get_normal_strength(t_account * account, t_clienttag clienttag)
1292 {
1293     char key[256];
1294     char clienttag_str[5];
1295 
1296     if (!clienttag)
1297     {
1298 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
1299 	return 0;
1300     }
1301     sprintf(key,"Record\\%s\\0\\strength",tag_uint_to_str(clienttag_str,clienttag));
1302     return account_get_numattr(account,key);
1303 }
1304 
1305 
account_set_normal_strength(t_account * account,t_clienttag clienttag,unsigned int strength)1306 extern int account_set_normal_strength(t_account * account, t_clienttag clienttag, unsigned int strength)
1307 {
1308     char key[256];
1309     char clienttag_str[5];
1310 
1311     if (!clienttag)
1312     {
1313 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
1314 	return -1;
1315     }
1316     sprintf(key,"Record\\%s\\0\\strength",tag_uint_to_str(clienttag_str,clienttag));
1317     return account_set_numattr(account,key,strength);
1318 }
1319 
1320 
account_get_normal_magic(t_account * account,t_clienttag clienttag)1321 extern unsigned int account_get_normal_magic(t_account * account, t_clienttag clienttag)
1322 {
1323     char key[256];
1324     char clienttag_str[5];
1325 
1326     if (!clienttag)
1327     {
1328 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
1329 	return 0;
1330     }
1331     sprintf(key,"Record\\%s\\0\\magic",tag_uint_to_str(clienttag_str,clienttag));
1332     return account_get_numattr(account,key);
1333 }
1334 
1335 
account_set_normal_magic(t_account * account,t_clienttag clienttag,unsigned int magic)1336 extern int account_set_normal_magic(t_account * account, t_clienttag clienttag, unsigned int magic)
1337 {
1338     char key[256];
1339     char clienttag_str[5];
1340 
1341     if (!clienttag)
1342     {
1343 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
1344 	return -1;
1345     }
1346     sprintf(key,"Record\\%s\\0\\magic",tag_uint_to_str(clienttag_str,clienttag));
1347     return account_set_numattr(account,key,magic);
1348 }
1349 
1350 
account_get_normal_dexterity(t_account * account,t_clienttag clienttag)1351 extern unsigned int account_get_normal_dexterity(t_account * account, t_clienttag clienttag)
1352 {
1353     char key[256];
1354     char clienttag_str[5];
1355 
1356     if (!clienttag)
1357     {
1358 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
1359 	return 0;
1360     }
1361     sprintf(key,"Record\\%s\\0\\dexterity",tag_uint_to_str(clienttag_str,clienttag));
1362     return account_get_numattr(account,key);
1363 }
1364 
1365 
account_set_normal_dexterity(t_account * account,t_clienttag clienttag,unsigned int dexterity)1366 extern int account_set_normal_dexterity(t_account * account, t_clienttag clienttag, unsigned int dexterity)
1367 {
1368     char key[256];
1369     char clienttag_str[5];
1370 
1371     if (!clienttag)
1372     {
1373 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
1374 	return -1;
1375     }
1376     sprintf(key,"Record\\%s\\0\\dexterity",tag_uint_to_str(clienttag_str,clienttag));
1377     return account_set_numattr(account,key,dexterity);
1378 }
1379 
1380 
account_get_normal_vitality(t_account * account,t_clienttag clienttag)1381 extern unsigned int account_get_normal_vitality(t_account * account, t_clienttag clienttag)
1382 {
1383     char key[256];
1384     char clienttag_str[5];
1385 
1386     if (!clienttag)
1387     {
1388 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
1389 	return 0;
1390     }
1391     sprintf(key,"Record\\%s\\0\\vitality",tag_uint_to_str(clienttag_str,clienttag));
1392     return account_get_numattr(account,key);
1393 }
1394 
1395 
account_set_normal_vitality(t_account * account,t_clienttag clienttag,unsigned int vitality)1396 extern int account_set_normal_vitality(t_account * account, t_clienttag clienttag, unsigned int vitality)
1397 {
1398     char key[256];
1399     char clienttag_str[5];
1400 
1401     if (!clienttag)
1402     {
1403 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
1404 	return -1;
1405     }
1406     sprintf(key,"Record\\%s\\0\\vitality",tag_uint_to_str(clienttag_str,clienttag));
1407     return account_set_numattr(account,key,vitality);
1408 }
1409 
1410 
account_get_normal_gold(t_account * account,t_clienttag clienttag)1411 extern unsigned int account_get_normal_gold(t_account * account, t_clienttag clienttag)
1412 {
1413     char key[256];
1414     char clienttag_str[5];
1415 
1416     if (!clienttag)
1417     {
1418 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
1419 	return 0;
1420     }
1421     sprintf(key,"Record\\%s\\0\\gold",tag_uint_to_str(clienttag_str,clienttag));
1422     return account_get_numattr(account,key);
1423 }
1424 
1425 
account_set_normal_gold(t_account * account,t_clienttag clienttag,unsigned int gold)1426 extern int account_set_normal_gold(t_account * account, t_clienttag clienttag, unsigned int gold)
1427 {
1428     char key[256];
1429     char clienttag_str[5];
1430 
1431     if (!clienttag)
1432     {
1433 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
1434 	return -1;
1435     }
1436     sprintf(key,"Record\\%s\\0\\gold",tag_uint_to_str(clienttag_str,clienttag));
1437     return account_set_numattr(account,key,gold);
1438 }
1439 
1440 
1441 /****************************************************************/
1442 
1443 
account_check_closed_character(t_account * account,t_clienttag clienttag,char const * realmname,char const * charname)1444 extern int account_check_closed_character(t_account * account, t_clienttag clienttag, char const * realmname, char const * charname)
1445 {
1446     char const * charlist = account_get_closed_characterlist (account, clienttag, realmname);
1447     char         tempname[32];
1448 
1449     if (charlist == NULL)
1450     {
1451         eventlog(eventlog_level_debug,__FUNCTION__,"no characters in Realm %s",realmname);
1452     }
1453     else
1454     {
1455         char const * start;
1456 	char const * next_char;
1457 	int    list_len;
1458 	int    name_len;
1459 	int    i;
1460 
1461 	eventlog(eventlog_level_debug,__FUNCTION__,"got characterlist \"%s\" for Realm %s",charlist,realmname);
1462 
1463 	list_len  = strlen(charlist);
1464 	start     = charlist;
1465 	next_char = start;
1466 	for (i = 0; i < list_len; i++, next_char++)
1467 	{
1468 	    if (',' == *next_char)
1469 	    {
1470 	        name_len = next_char - start;
1471 
1472 	        strncpy(tempname, start, name_len);
1473 		tempname[name_len] = '\0';
1474 
1475 	        eventlog(eventlog_level_debug,__FUNCTION__,"found character \"%s\"",tempname);
1476 
1477 		if (strcmp(tempname, charname) == 0)
1478 		    return 1;
1479 
1480 		start = next_char + 1;
1481 	    }
1482 	}
1483 
1484 	name_len = next_char - start;
1485 
1486 	strncpy(tempname, start, name_len);
1487 	tempname[name_len] = '\0';
1488 
1489 	eventlog(eventlog_level_debug,__FUNCTION__,"found tail character \"%s\"",tempname);
1490 
1491 	if (strcmp(tempname, charname) == 0)
1492 	    return 1;
1493     }
1494 
1495     return 0;
1496 }
1497 
1498 
account_get_closed_characterlist(t_account * account,t_clienttag clienttag,char const * realmname)1499 extern char const * account_get_closed_characterlist(t_account * account, t_clienttag clienttag, char const * realmname)
1500 {
1501     char realmkey[256];
1502     char clienttag_str[5];
1503 
1504     if (!clienttag)
1505     {
1506 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
1507 	return NULL;
1508     }
1509 
1510     if (!realmname)
1511     {
1512         eventlog(eventlog_level_error,__FUNCTION__,"got NULL realmname");
1513 	return NULL;
1514     }
1515 
1516     if (!account)
1517     {
1518         eventlog(eventlog_level_error,__FUNCTION__,"got NULL account");
1519 	return NULL;
1520     }
1521 
1522     sprintf(realmkey,"BNET\\CharacterList\\%s\\%s\\0",tag_uint_to_str(clienttag_str,clienttag),realmname);
1523     eventlog(eventlog_level_debug,__FUNCTION__,"looking for '%s'",realmkey);
1524 
1525     return account_get_strattr(account, realmkey);
1526 }
1527 
1528 
account_set_closed_characterlist(t_account * account,t_clienttag clienttag,char const * charlist)1529 extern int account_set_closed_characterlist(t_account * account, t_clienttag clienttag, char const * charlist)
1530 {
1531     char key[256];
1532     char clienttag_str[5];
1533 
1534     if (!clienttag)
1535     {
1536 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
1537 	return -1;
1538     }
1539 
1540     eventlog(eventlog_level_debug,__FUNCTION__ ,"clienttag='%s', charlist='%s'",tag_uint_to_str(clienttag_str,clienttag),charlist);
1541 
1542     sprintf(key,"BNET\\Characters\\%s\\0",tag_uint_to_str(clienttag_str,clienttag));
1543     return account_set_strattr(account,key,charlist);
1544 }
1545 
account_add_closed_character(t_account * account,t_clienttag clienttag,t_character * ch)1546 extern int account_add_closed_character(t_account * account, t_clienttag clienttag, t_character * ch)
1547 {
1548     char key[256];
1549     char clienttag_str[5];
1550     char hex_buffer[356];
1551     char chars_in_realm[256];
1552     char const * old_list;
1553 
1554     if (!clienttag)
1555     {
1556 	eventlog(eventlog_level_error,__FUNCTION__,"got bad clienttag");
1557 	return -1;
1558     }
1559 
1560     if (!ch)
1561     {
1562 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL character");
1563 	return -1;
1564     }
1565 
1566     eventlog(eventlog_level_debug,__FUNCTION__,"clienttag=\"%s\", realm=\"%s\", name=\"%s\"",tag_uint_to_str(clienttag_str,clienttag),ch->realmname,ch->name);
1567 
1568     sprintf(key,"BNET\\CharacterList\\%s\\%s\\0",tag_uint_to_str(clienttag_str,clienttag),ch->realmname);
1569     old_list = account_get_strattr(account, key);
1570     if (old_list)
1571     {
1572         sprintf(chars_in_realm, "%s,%s", old_list, ch->name);
1573     }
1574     else
1575     {
1576         sprintf(chars_in_realm, "%s", ch->name);
1577     }
1578 
1579     eventlog(eventlog_level_debug,__FUNCTION__,"new character list for realm \"%s\" is \"%s\"", ch->realmname, chars_in_realm);
1580     account_set_strattr(account, key, chars_in_realm);
1581 
1582     sprintf(key,"BNET\\Characters\\%s\\%s\\%s\\0",tag_uint_to_str(clienttag_str,clienttag),ch->realmname,ch->name);
1583     str_to_hex(hex_buffer, ch->data, ch->datalen);
1584     account_set_strattr(account,key,hex_buffer);
1585 
1586     /*
1587     eventlog(eventlog_level_debug,__FUNCTION__,"key \"%s\"", key);
1588     eventlog(eventlog_level_debug,__FUNCTION__,"value \"%s\"", hex_buffer);
1589     */
1590 
1591     return 0;
1592 }
1593 
account_set_friend(t_account * account,int friendnum,unsigned int frienduid)1594 extern int account_set_friend( t_account * account, int friendnum, unsigned int frienduid )
1595 {
1596 	char key[256];
1597 	if ( frienduid == 0 || friendnum < 0 || friendnum >= prefs_get_max_friends())
1598 	{
1599 		return -1;
1600 	}
1601 	sprintf(key, "friend\\%d\\uid", friendnum);
1602 
1603 	return account_set_numattr( account, key, frienduid);
1604 }
1605 
account_get_friend(t_account * account,int friendnum)1606 extern unsigned int account_get_friend( t_account * account, int friendnum)
1607 {
1608     char key[256];
1609     int tmp;
1610     char const * name;
1611     t_account * acct;
1612 
1613     if (friendnum < 0 || friendnum >= prefs_get_max_friends()) {
1614 	// bogus name (user himself) instead of NULL, otherwise clients might crash
1615 	eventlog(eventlog_level_error, __FUNCTION__, "invalid friendnum %d (max: %d)", friendnum, prefs_get_max_friends());
1616 	return 0;
1617     }
1618 
1619     sprintf(key, "friend\\%d\\uid", friendnum);
1620     tmp = account_get_numattr(account, key);
1621     if(!tmp) {
1622         // ok, looks like we have a problem. Maybe friends still stored in old format?
1623 
1624         sprintf(key,"friend\\%d\\name",friendnum);
1625         name = account_get_strattr(account,key);
1626 
1627         if (name)
1628         {
1629     	    if ((acct = accountlist_find_account(name)) != NULL)
1630             {
1631         	tmp = account_get_uid(acct);
1632                 account_set_friend(account,friendnum,tmp);
1633                 account_set_strattr(account,key,NULL); //remove old username-based friend now
1634 
1635                 return tmp;
1636 	    }
1637             account_set_strattr(account,key,NULL); //remove old username-based friend now
1638 	    eventlog(eventlog_level_warn, __FUNCTION__, "unexistant friend name ('%s') in old storage format", name);
1639 	    return 0;
1640         }
1641 
1642 	eventlog(eventlog_level_error, __FUNCTION__, "could not find friend (friendno: %d of '%s')", friendnum, account_get_name(account));
1643 	return 0;
1644     }
1645 
1646     return tmp;
1647 }
1648 
account_set_friendcount(t_account * account,int count)1649 static int account_set_friendcount( t_account * account, int count)
1650 {
1651 	if (count < 0 || count > prefs_get_max_friends())
1652 	{
1653 		return -1;
1654 	}
1655 
1656 	return account_set_numattr( account, "friend\\count", count);
1657 }
1658 
account_get_friendcount(t_account * account)1659 extern int account_get_friendcount( t_account * account )
1660 {
1661     return account_get_numattr( account, "friend\\count" );
1662 }
1663 
account_add_friend(t_account * my_acc,t_account * facc)1664 extern int account_add_friend( t_account * my_acc, t_account * facc)
1665 {
1666     unsigned my_uid;
1667     unsigned fuid;
1668     int nf;
1669     t_list *flist;
1670 
1671     if (my_acc == NULL || facc == NULL) {
1672 	eventlog(eventlog_level_error, __FUNCTION__, "got NULL account");
1673 	return -1;
1674     }
1675 
1676     my_uid = account_get_uid(my_acc);
1677     fuid = account_get_uid(facc);
1678 
1679     if (my_acc == facc) return -2;
1680 
1681     nf = account_get_friendcount(my_acc);
1682     if (nf >= prefs_get_max_friends()) return -3;
1683 
1684     flist = account_get_friends(my_acc);
1685     if (flist == NULL) return -1;
1686     if (friendlist_find_account(flist, facc) != NULL) return -4;
1687 
1688     account_set_friend(my_acc, nf, fuid);
1689     account_set_friendcount(my_acc, nf + 1);
1690     if (account_check_mutual(facc, my_uid) == 0)
1691 	friendlist_add_account(flist, facc, 1);
1692     else
1693 	friendlist_add_account(flist, facc, 0);
1694 
1695     return 0;
1696 }
1697 
account_remove_friend(t_account * account,int friendnum)1698 extern int account_remove_friend( t_account * account, int friendnum )
1699 {
1700     unsigned i;
1701     int n = account_get_friendcount(account);
1702 
1703     if (account == NULL) {
1704 	eventlog(eventlog_level_error, __FUNCTION__, "got NULL account");
1705 	return -1;
1706     }
1707 
1708     if (friendnum < 0 || friendnum >= n) {
1709 	eventlog(eventlog_level_error, __FUNCTION__, "got invalid friendnum (friendnum: %d max: %d)", friendnum, n);
1710 	return -1;
1711     }
1712 
1713     for(i = friendnum ; i < n - 1; i++)
1714 	account_set_friend(account, i, account_get_friend(account, i + 1));
1715 
1716     account_set_friend(account, n-1, 0); /* FIXME: should delete the attribute */
1717     account_set_friendcount(account, n-1);
1718 
1719     return 0;
1720 }
1721 
account_remove_friend2(t_account * account,const char * friend)1722 extern int account_remove_friend2( t_account * account, const char * friend)
1723 {
1724     t_list *flist;
1725     t_friend *fr;
1726     unsigned i, uid;
1727     int n;
1728 
1729     if (account == NULL) {
1730 	eventlog(eventlog_level_error, __FUNCTION__, "got NULL account");
1731 	return -1;
1732     }
1733 
1734     if (friend == NULL) {
1735 	eventlog(eventlog_level_error, __FUNCTION__, "got NULL friend username");
1736 	return -1;
1737     }
1738 
1739     if ((flist = account_get_friends(account)) == NULL)
1740 	return -1;
1741 
1742     if ((fr = friendlist_find_username(flist, friend)) == NULL) return -2;
1743 
1744     n = account_get_friendcount(account);
1745     uid = account_get_uid(friend_get_account(fr));
1746     for (i = 0; i < n; i++)
1747 	if (account_get_friend(account, i) == uid) {
1748     	    t_list * fflist;
1749     	    t_friend * ffr;
1750     	    t_account * facc;
1751 
1752 	    account_remove_friend(account, i);
1753     	    if((facc = friend_get_account(fr)) &&
1754 	       (fflist = account_get_friends(facc)) &&
1755 	       (ffr = friendlist_find_account(fflist, account)))
1756         	    friend_set_mutual(ffr, 0);
1757 	    friendlist_remove_friend(flist, fr);
1758 	    return i;
1759 	}
1760 
1761     return -2;
1762 }
1763 
1764 // Some Extra Commands for REAL admins to promote other users to Admins
1765 // And Moderators of channels
account_set_admin(t_account * account)1766 extern int account_set_admin( t_account * account )
1767 {
1768 	return account_set_strattr( account, "BNET\\auth\\admin", "true");
1769 }
account_set_demoteadmin(t_account * account)1770 extern int account_set_demoteadmin( t_account * account )
1771 {
1772 	return account_set_strattr( account, "BNET\\auth\\admin", "false" );
1773 }
1774 
account_get_command_groups(t_account * account)1775 extern unsigned int account_get_command_groups(t_account * account)
1776 {
1777     return account_get_numattr(account,"BNET\\auth\\command_groups");
1778 }
account_set_command_groups(t_account * account,unsigned int groups)1779 extern int account_set_command_groups(t_account * account, unsigned int groups)
1780 {
1781     return account_set_numattr(account,"BNET\\auth\\command_groups",groups);
1782 }
1783 
1784 // WAR3 Play Game & Profile Funcs
1785 
race_get_str(unsigned int race)1786 extern char const * race_get_str(unsigned int race)
1787 {
1788 	switch(race) {
1789 	case W3_RACE_ORCS:
1790 	    return "orcs";
1791 	case W3_RACE_HUMANS:
1792 	    return "humans";
1793 	case W3_RACE_UNDEAD:
1794 	case W3_ICON_UNDEAD:
1795 	    return "undead";
1796 	case W3_RACE_NIGHTELVES:
1797 	    return "nightelves";
1798 	case W3_RACE_RANDOM:
1799 	case W3_ICON_RANDOM:
1800 	    return "random";
1801 	case W3_RACE_DEMONS:
1802 	case W3_ICON_DEMONS:
1803 	    return "demons";
1804 	default:
1805 	    eventlog(eventlog_level_warn,__FUNCTION__,"unknown race: %x", race);
1806 	    return NULL;
1807 	}
1808 }
1809 
account_inc_racewins(t_account * account,unsigned int intrace,t_clienttag clienttag)1810 extern int account_inc_racewins( t_account * account, unsigned int intrace, t_clienttag clienttag)
1811 {
1812 	char table[256];
1813 	char clienttag_str[5];
1814 	unsigned int wins;
1815 	char const * race = race_get_str(intrace);
1816 
1817 	if(!race)
1818 	    return -1;
1819 
1820 	sprintf(table,"Record\\%s\\%s\\wins",tag_uint_to_str(clienttag_str,clienttag), race);
1821 	wins = account_get_numattr(account,table);
1822 	wins++;
1823 
1824 	return account_set_numattr(account,table,wins);
1825 }
1826 
account_get_racewins(t_account * account,unsigned int intrace,t_clienttag clienttag)1827 extern int account_get_racewins( t_account * account, unsigned int intrace, t_clienttag clienttag)
1828 {
1829 	char table[256];
1830 	char clienttag_str[5];
1831 	char const *race = race_get_str(intrace);
1832 
1833 	if(!race)
1834 	    return 0;
1835 
1836 	sprintf(table,"Record\\%s\\%s\\wins",tag_uint_to_str(clienttag_str,clienttag), race);
1837 	return account_get_numattr(account,table);
1838 }
1839 
account_inc_racelosses(t_account * account,unsigned int intrace,t_clienttag clienttag)1840 extern int account_inc_racelosses( t_account * account, unsigned int intrace, t_clienttag clienttag)
1841 {
1842 	char table[256];
1843 	char clienttag_str[5];
1844 	unsigned int losses;
1845 	char const *race = race_get_str(intrace);
1846 
1847 	if(!race)
1848 	    return -1;
1849 
1850 	sprintf(table,"Record\\%s\\%s\\losses",tag_uint_to_str(clienttag_str,clienttag),race);
1851 
1852 	losses=account_get_numattr(account,table);
1853 
1854 	losses++;
1855 
1856 	return account_set_numattr(account,table,losses);
1857 
1858 }
1859 
account_get_racelosses(t_account * account,unsigned int intrace,t_clienttag clienttag)1860 extern int account_get_racelosses( t_account * account, unsigned int intrace, t_clienttag clienttag)
1861 {
1862 	char table[256];
1863 	char clienttag_str[5];
1864 	char const *race = race_get_str(intrace);
1865 
1866 	if(!race)
1867 	    return 0;
1868 
1869 	sprintf(table,"Record\\%s\\%s\\losses",tag_uint_to_str(clienttag_str,clienttag),race);
1870 
1871 	return account_get_numattr(account,table);
1872 
1873 }
1874 
account_update_xp(t_account * account,t_clienttag clienttag,t_game_result gameresult,unsigned int opponlevel,int * xp_diff,t_ladder_id id)1875 extern int account_update_xp(t_account * account, t_clienttag clienttag, t_game_result gameresult, unsigned int opponlevel, int * xp_diff,t_ladder_id id)
1876 {
1877   int xp;
1878   int mylevel;
1879   int xpdiff = 0, placeholder;
1880 
1881   xp = account_get_ladder_xp(account, clienttag,id); //get current xp
1882   if (xp < 0) {
1883     eventlog(eventlog_level_error, __FUNCTION__, "got negative XP");
1884     return -1;
1885   }
1886 
1887   mylevel = account_get_ladder_level(account,clienttag,id); //get accounts level
1888   if (mylevel > W3_XPCALC_MAXLEVEL) {
1889     eventlog(eventlog_level_error, __FUNCTION__, "got invalid level: %d", mylevel);
1890     return -1;
1891   }
1892 
1893   if(mylevel<=0) //if level is 0 then set it to 1
1894     mylevel=1;
1895 
1896   if (opponlevel < 1) opponlevel = 1;
1897 
1898   switch (gameresult)
1899     {
1900     case game_result_win:
1901       ladder_war3_xpdiff(mylevel, opponlevel, &xpdiff, &placeholder); break;
1902     case game_result_loss:
1903       ladder_war3_xpdiff(opponlevel, mylevel, &placeholder, &xpdiff); break;
1904     default:
1905       eventlog(eventlog_level_error, __FUNCTION__, "got invalid game result: %d", gameresult);
1906       return -1;
1907     }
1908 
1909   *xp_diff = xpdiff;
1910   xp += xpdiff;
1911   if (xp < 0) xp = 0;
1912 
1913   return account_set_ladder_xp(account,clienttag,id,xp);
1914 }
1915 
account_get_ladder_xp(t_account * account,t_clienttag clienttag,t_ladder_id id)1916 extern int account_get_ladder_xp(t_account * account, t_clienttag clienttag, t_ladder_id id)
1917 {
1918     char key[256];
1919     char clienttag_str[5];
1920 
1921     sprintf(key,"Record\\%s\\%s\\xp",tag_uint_to_str(clienttag_str,clienttag),ladder_id_str[(int)id]);
1922     return account_get_numattr(account,key);
1923 }
1924 
account_set_ladder_xp(t_account * account,t_clienttag clienttag,t_ladder_id id,unsigned int xp)1925 extern int account_set_ladder_xp(t_account * account, t_clienttag clienttag, t_ladder_id id, unsigned int xp)
1926 {
1927     char         key[256];
1928     char clienttag_str[5];
1929 
1930     sprintf(key,"Record\\%s\\%s\\xp",tag_uint_to_str(clienttag_str,clienttag),ladder_id_str[(int)id]);
1931     return account_set_numattr(account,key,xp);
1932 }
1933 
account_get_ladder_level(t_account * account,t_clienttag clienttag,t_ladder_id id)1934 extern int account_get_ladder_level(t_account * account, t_clienttag clienttag, t_ladder_id id)
1935 {
1936     char key[256];
1937     char clienttag_str[5];
1938 
1939     sprintf(key,"Record\\%s\\%s\\level",tag_uint_to_str(clienttag_str,clienttag),ladder_id_str[(int)id]);
1940     return account_get_numattr(account,key);
1941 }
1942 
account_set_ladder_level(t_account * account,t_clienttag clienttag,t_ladder_id id,unsigned int level)1943 extern int account_set_ladder_level(t_account * account, t_clienttag clienttag, t_ladder_id id, unsigned int level)
1944 {
1945     char         key[256];
1946     char clienttag_str[5];
1947 
1948     sprintf(key,"Record\\%s\\%s\\level",tag_uint_to_str(clienttag_str,clienttag),ladder_id_str[(int)id]);
1949     return account_set_numattr(account,key,level);
1950 }
1951 
1952 
account_adjust_ladder_level(t_account * account,t_clienttag clienttag,t_ladder_id id)1953 extern int account_adjust_ladder_level(t_account * account, t_clienttag clienttag,t_ladder_id id)
1954 {
1955   int xp,mylevel;
1956 
1957   xp = account_get_ladder_xp(account, clienttag,id);
1958   if (xp < 0) xp = 0;
1959 
1960   mylevel = account_get_ladder_level(account,clienttag,id);
1961   if (mylevel < 1) mylevel = 1;
1962 
1963   if (mylevel > W3_XPCALC_MAXLEVEL) {
1964     eventlog(eventlog_level_error, __FUNCTION__, "got invalid level: %d", mylevel);
1965     return -1;
1966   }
1967 
1968   mylevel = ladder_war3_updatelevel(mylevel, xp);
1969 
1970   return account_set_ladder_level(account,clienttag,id,mylevel);
1971 }
1972 
1973 
1974 //Other funcs used in profiles and PG saving
account_get_raceicon(t_account * account,char * raceicon,unsigned int * raceiconnumber,unsigned int * wins,t_clienttag clienttag)1975 extern void account_get_raceicon(t_account * account, char * raceicon, unsigned int * raceiconnumber, unsigned int * wins, t_clienttag clienttag) //Based of wins for each race, Race with most wins, gets shown in chat channel
1976 {
1977 	unsigned int humans;
1978 	unsigned int orcs;
1979 	unsigned int undead;
1980 	unsigned int nightelf;
1981 	unsigned int random;
1982 	unsigned int i;
1983 
1984 	random = account_get_racewins(account,W3_RACE_RANDOM,clienttag);
1985 	humans = account_get_racewins(account,W3_RACE_HUMANS,clienttag);
1986 	orcs = account_get_racewins(account,W3_RACE_ORCS,clienttag);
1987 	undead = account_get_racewins(account,W3_RACE_UNDEAD,clienttag);
1988 	nightelf = account_get_racewins(account,W3_RACE_NIGHTELVES,clienttag);
1989 	if(orcs>=humans && orcs>=undead && orcs>=nightelf && orcs>=random) {
1990 	  *raceicon = 'O';
1991 	  *wins = orcs;
1992 	}
1993 	else if(humans>=orcs && humans>=undead && humans>=nightelf && humans>=random) {
1994 	    *raceicon = 'H';
1995 	    *wins = humans;
1996 	}
1997 	else if(nightelf>=humans && nightelf>=orcs && nightelf>=undead && nightelf>=random) {
1998 	    *raceicon = 'N';
1999 	    *wins = nightelf;
2000 	}
2001 	else if(undead>=humans && undead>=orcs && undead>=nightelf && undead>=random) {
2002 	    *raceicon = 'U';
2003 	    *wins = undead;
2004 	}
2005 	else {
2006 	    *raceicon = 'R';
2007 	    *wins = random;
2008 	}
2009 	i = 1;
2010 	while((signed)*wins >= anongame_infos_get_ICON_REQ(i,clienttag) && anongame_infos_get_ICON_REQ(i,clienttag) > 0) i++;
2011 	*raceiconnumber = i ;
2012 }
2013 
account_get_profile_calcs(t_account * account,int xp,unsigned int Level)2014 extern int account_get_profile_calcs(t_account * account, int xp, unsigned int Level)
2015 {
2016         int xp_min;
2017 	int xp_max;
2018 	unsigned int i;
2019 	int  t;
2020 	unsigned int startlvl;
2021 
2022 	if (Level==1) startlvl = 1;
2023 	else startlvl = Level-1;
2024 	for (i = startlvl; i < W3_XPCALC_MAXLEVEL; i++) {
2025 		xp_min = ladder_war3_get_min_xp(i);
2026 		xp_max = ladder_war3_get_min_xp(i+1);
2027 		if ((xp >= xp_min) && (xp < xp_max)) {
2028 			t = (int)((((double)xp - (double)xp_min)
2029 					/ ((double)xp_max - (double)xp_min)) * 128);
2030 			if (i < Level) {
2031 				return 128 + t;
2032 			} else {
2033 				return t;
2034 			}
2035 		}
2036 	}
2037 	return 0;
2038 }
2039 
account_set_saveladderstats(t_account * account,unsigned int gametype,t_game_result result,unsigned int opponlevel,t_clienttag clienttag)2040 extern int account_set_saveladderstats(t_account * account,unsigned int gametype, t_game_result result, unsigned int opponlevel, t_clienttag clienttag)
2041 {
2042 	unsigned int intrace;
2043         int xpdiff,uid,level;
2044 	t_ladder_id id;
2045 	t_ladder * ladder;
2046 
2047 	if(!account) {
2048 		eventlog(eventlog_level_error, __FUNCTION__, "got NULL account");
2049 		return -1;
2050 	}
2051 
2052 
2053 	//added for better tracking down of problems with gameresults
2054 	eventlog(eventlog_level_trace,__FUNCTION__,"parsing game result for player: %s result: %s",account_get_name(account),(result==game_result_win)?"WIN":"LOSS");
2055 
2056 	intrace = account_get_w3pgrace(account, clienttag);
2057 	uid = account_get_uid(account);
2058 
2059 	switch (gametype)
2060 	{
2061 	  case ANONGAME_TYPE_1V1: //1v1
2062 	  {
2063 	  	id = ladder_id_solo;
2064 		ladder = solo_ladder(clienttag);
2065 		break;
2066 	  }
2067 	  case ANONGAME_TYPE_2V2:
2068 	  case ANONGAME_TYPE_3V3:
2069 	  case ANONGAME_TYPE_4V4:
2070 	  case ANONGAME_TYPE_5V5:
2071 	  case ANONGAME_TYPE_6V6:
2072 	  case ANONGAME_TYPE_2V2V2:
2073 	  case ANONGAME_TYPE_3V3V3:
2074 	  case ANONGAME_TYPE_4V4V4:
2075 	  case ANONGAME_TYPE_2V2V2V2:
2076 	  case ANONGAME_TYPE_3V3V3V3:
2077 	  {
2078 	  	id = ladder_id_team;
2079 		ladder = team_ladder(clienttag);
2080 		break;
2081 	  }
2082 
2083 	  case ANONGAME_TYPE_SMALL_FFA:
2084 	  {
2085 	  	id = ladder_id_ffa;
2086 		ladder = ffa_ladder(clienttag);
2087                 break;
2088 	  }
2089 	  default:
2090 		eventlog(eventlog_level_error,__FUNCTION__,"Invalid Gametype? %u",gametype);
2091 		return -1;
2092 	}
2093 
2094 	if(result == game_result_win) //win
2095 	{
2096 		account_inc_ladder_wins(account, clienttag,id);
2097 		account_inc_racewins(account, intrace, clienttag);
2098 	}
2099 	if(result == game_result_loss) //loss
2100 	{
2101 		account_inc_ladder_losses(account, clienttag,id);
2102 		account_inc_racelosses(account,intrace, clienttag);
2103 	}
2104 
2105 	account_update_xp(account,clienttag,result,opponlevel,&xpdiff,id);
2106 	account_adjust_ladder_level(account,clienttag,id);
2107 	level = account_get_ladder_level(account,clienttag,id);
2108 	if (war3_ladder_update(ladder,uid,xpdiff,level,account,0)!=0)
2109 	  war3_ladder_add(ladder,uid,account_get_ladder_xp(account,clienttag,id),level,account,0,clienttag);
2110 
2111 	return 0;
2112 }
2113 
account_set_w3pgrace(t_account * account,t_clienttag clienttag,unsigned int race)2114 extern int account_set_w3pgrace( t_account * account, t_clienttag clienttag, unsigned int race)
2115 {
2116   char key[256];
2117   char clienttag_str[5];
2118 
2119   sprintf(key,"Record\\%s\\w3pgrace",tag_uint_to_str(clienttag_str,clienttag));
2120 
2121   return account_set_numattr( account, key, race);
2122 }
2123 
account_get_w3pgrace(t_account * account,t_clienttag clienttag)2124 extern int account_get_w3pgrace( t_account * account, t_clienttag clienttag )
2125 {
2126   char key[256];
2127   char clienttag_str[5];
2128 
2129   sprintf(key,"Record\\%s\\w3pgrace",tag_uint_to_str(clienttag_str,clienttag));
2130 
2131   return account_get_numattr( account, key);
2132 }
2133 // Arranged Team Functions
account_set_currentatteam(t_account * account,unsigned int teamcount)2134 extern int account_set_currentatteam(t_account * account, unsigned int teamcount)
2135 {
2136 	return account_set_numattr(account,"BNET\\current_at_team",teamcount);
2137 }
2138 
account_get_currentatteam(t_account * account)2139 extern int account_get_currentatteam(t_account * account)
2140 {
2141 	return account_get_numattr(account,"BNET\\current_at_team");
2142 }
2143 
account_get_highestladderlevel(t_account * account,t_clienttag clienttag)2144 extern int account_get_highestladderlevel(t_account * account,t_clienttag clienttag)
2145 {
2146 	t_elem * curr;
2147 	t_team * team;
2148 
2149 	unsigned int sololevel = account_get_ladder_level(account,clienttag,ladder_id_solo);
2150 	unsigned int teamlevel = account_get_ladder_level(account,clienttag,ladder_id_team);
2151 	unsigned int ffalevel  = account_get_ladder_level(account,clienttag,ladder_id_ffa);
2152 	unsigned int atlevel = 0;
2153 	unsigned int t;
2154 
2155 	if (account_get_teams(account))
2156 	{
2157 		LIST_TRAVERSE(account_get_teams(account),curr)
2158 		{
2159 			if (!(team = elem_get_data(curr)))
2160 			{
2161 				eventlog(eventlog_level_error,__FUNCTION__,"found NULL entry in list");
2162 				continue;
2163 			}
2164 		if ((team_get_clienttag(team)!=clienttag))
2165 			continue;
2166 
2167 		if ((t = team_get_level(team)) > atlevel)
2168 			atlevel = t;
2169 		}
2170 	}
2171 
2172 	eventlog(eventlog_level_debug,__FUNCTION__,"Checking for highest level in Solo,Team,FFA,AT Ladder Stats");
2173 	eventlog(eventlog_level_debug,__FUNCTION__,"Solo Level: %d, Team Level %d, FFA Level %d, Highest AT Team Level: %d",sololevel,teamlevel,ffalevel,atlevel);
2174 
2175 	if(sololevel >= teamlevel && sololevel >= atlevel && sololevel >= ffalevel)
2176 		return sololevel;
2177 	if(teamlevel >= sololevel && teamlevel >= atlevel && teamlevel >= ffalevel)
2178         	return teamlevel;
2179 	if(atlevel >= sololevel && atlevel >= teamlevel && atlevel >= ffalevel)
2180         	return atlevel;
2181 	return ffalevel;
2182 
2183     // we should never get here
2184 
2185 	return -1;
2186 }
2187 
2188 //BlacKDicK 04/20/2003
account_set_user_icon(t_account * account,t_clienttag clienttag,char const * usericon)2189 extern int account_set_user_icon( t_account * account, t_clienttag clienttag,char const * usericon)
2190 {
2191   char key[256];
2192   char clienttag_str[5];
2193 
2194   sprintf(key,"Record\\%s\\userselected_icon",tag_uint_to_str(clienttag_str,clienttag));
2195   if (usericon)
2196     return account_set_strattr(account,key,usericon);
2197   else
2198     return account_set_strattr(account,key,"NULL");
2199 }
2200 
account_get_user_icon(t_account * account,t_clienttag clienttag)2201 extern char const * account_get_user_icon( t_account * account, t_clienttag clienttag )
2202 {
2203   char key[256];
2204   char const * retval;
2205   char clienttag_str[5];
2206 
2207   sprintf(key,"Record\\%s\\userselected_icon",tag_uint_to_str(clienttag_str,clienttag));
2208   retval = account_get_strattr(account,key);
2209 
2210   if ((retval) && ((strcmp(retval,"NULL")!=0)))
2211     return retval;
2212   else
2213     return NULL;
2214 }
2215 
2216 // Ramdom - Nothing, Grean Dragon Whelp, Azure Dragon (Blue Dragon), Red Dragon, Deathwing, Nothing
2217 // Humans - Peasant, Footman, Knight, Archmage, Medivh, Nothing
2218 // Orcs - Peon, Grunt, Tauren, Far Seer, Thrall, Nothing
2219 // Undead - Acolyle, Ghoul, Abomination, Lich, Tichondrius, Nothing
2220 // Night Elves - Wisp, Archer, Druid of the Claw, Priestess of the Moon, Furion Stormrage, Nothing
2221 // Demons - Nothing, ???(wich unit is nfgn), Infernal, Doom Guard, Pit Lord/Manaroth, Archimonde
2222 // ADDED TFT ICON BY DJP 07/16/2003
2223 static char * profile_code[12][6] = {
2224 	    {NULL  , "ngrd", "nadr", "nrdr", "nbwm", NULL  },
2225 	    {"hpea", "hfoo", "hkni", "Hamg", "nmed", NULL  },
2226 	    {"opeo", "ogru", "otau", "Ofar", "Othr", NULL  },
2227 	    {"uaco", "ugho", "uabo", "Ulic", "Utic", NULL  },
2228 	    {"ewsp", "earc", "edoc", "Emoo", "Efur", NULL  },
2229 	    {NULL  , "nfng", "ninf", "nbal", "Nplh", "Uwar"}, /* not used by RoC */
2230 	    {NULL  , "nmyr", "nnsw", "nhyc", "Hvsh", "Eevm"},
2231 	    {"hpea", "hrif", "hsor", "hspt", "Hblm", "Hjai"},
2232 	    {"opeo", "ohun", "oshm", "ospw", "Oshd", "Orex"},
2233 	    {"uaco", "ucry", "uban", "uobs", "Ucrl", "Usyl"},
2234 	    {"ewsp", "esen", "edot", "edry", "Ekee", "Ewrd"},
2235 	    {NULL  , "nfgu", "ninf", "nbal", "Nplh", "Uwar"}
2236 	};
2237 
account_get_icon_profile(t_account * account,t_clienttag clienttag)2238 extern unsigned int account_get_icon_profile(t_account * account, t_clienttag clienttag)
2239 {
2240 	unsigned int humans	= account_get_racewins(account,W3_RACE_HUMANS,clienttag);		//  1;
2241 	unsigned int orcs	= account_get_racewins(account,W3_RACE_ORCS,clienttag); 		        //  2;
2242 	unsigned int nightelf	= account_get_racewins(account,W3_RACE_NIGHTELVES,clienttag);	        //  4;
2243 	unsigned int undead	= account_get_racewins(account,W3_RACE_UNDEAD,clienttag);		//  8;
2244 	unsigned int random	= account_get_racewins(account,W3_RACE_RANDOM,clienttag);		// 32;
2245 	unsigned int race; 	     // 0 = Humans, 1 = Orcs, 2 = Night Elves, 3 = Undead, 4 = Ramdom
2246 	unsigned int level	= 0; // 0 = under 25, 1 = 25 to 249, 2 = 250 to 499, 3 = 500 to 1499, 4 = 1500 or more (wins)
2247 	unsigned int wins;
2248 	int number_ctag		= 0;
2249 
2250 	/* moved the check for orcs in the first place so people with 0 wins get peon */
2251         if(orcs>=humans && orcs>=undead && orcs>=nightelf && orcs>=random) {
2252             wins = orcs;
2253             race = 2;
2254         }
2255         else if(humans>=orcs && humans>=undead && humans>=nightelf && humans>=random) {
2256 	    wins = humans;
2257             race = 1;
2258         }
2259         else if(nightelf>=humans && nightelf>=orcs && nightelf>=undead && nightelf>=random) {
2260             wins = nightelf;
2261             race = 4;
2262         }
2263         else if(undead>=humans && undead>=orcs && undead>=nightelf && undead>=random) {
2264             wins = undead;
2265             race = 3;
2266         }
2267         else {
2268             wins = random;
2269             race = 0;
2270         }
2271 
2272         while(wins >= anongame_infos_get_ICON_REQ(level+1,clienttag) && anongame_infos_get_ICON_REQ(level+1,clienttag) > 0) level++;
2273 	if (clienttag == CLIENTTAG_WAR3XP_UINT)
2274 	  number_ctag = 6;
2275 
2276         eventlog(eventlog_level_info,__FUNCTION__,"race -> %u; level -> %u; wins -> %u; profileicon -> %s", race, level, wins, profile_code[race+number_ctag][level]);
2277 
2278 	return char_icon_to_uint(profile_code[race+number_ctag][level]);
2279 }
2280 
account_icon_to_profile_icon(char const * icon,t_account * account,t_clienttag ctag)2281 extern unsigned int account_icon_to_profile_icon(char const * icon,t_account * account, t_clienttag ctag)
2282 {
2283 	char tmp_icon[4];
2284 	char * result;
2285 	int number_ctag=0;
2286 
2287 	if (icon==NULL) return account_get_icon_profile(account,ctag);
2288 	if (sizeof(icon)>=4){
2289 		strncpy(tmp_icon,icon,4);
2290 		tmp_icon[0]=tmp_icon[0]-48;
2291 		if (ctag==CLIENTTAG_WAR3XP_UINT) {
2292 			number_ctag = 6;
2293 		}
2294 		if (tmp_icon[0]>=1) {
2295 			if (tmp_icon[1]=='R'){
2296 				result = profile_code[0+number_ctag][tmp_icon[0]-1];
2297 			}else if (tmp_icon[1]=='H'){
2298 				result = profile_code[1+number_ctag][tmp_icon[0]-1];
2299 			}else if (tmp_icon[1]=='O'){
2300 				result = profile_code[2+number_ctag][tmp_icon[0]-1];
2301 			}else if (tmp_icon[1]=='U'){
2302 				result = profile_code[3+number_ctag][tmp_icon[0]-1];
2303 			}else if (tmp_icon[1]=='N'){
2304 				result = profile_code[4+number_ctag][tmp_icon[0]-1];
2305 			}else if (tmp_icon[1]=='D'){
2306 				result = profile_code[5+number_ctag][tmp_icon[0]-1];
2307 			}else{
2308 				eventlog(eventlog_level_warn,__FUNCTION__,"got unrecognized race on [%s] icon ",icon);
2309 				result = profile_code[2][0];} /* "opeo" */
2310 			}else{
2311 				eventlog(eventlog_level_warn,__FUNCTION__,"got race_level<1 on [%s] icon ",icon);
2312 				result = NULL;
2313 			}
2314 	}else{
2315 	    eventlog(eventlog_level_error,__FUNCTION__,"got invalid icon lenght [%s] icon ",icon);
2316 	    result = NULL;
2317 	}
2318 	//eventlog(eventlog_level_debug,__FUNCTION__,"from [%4.4s] icon returned [0x%X]",icon,char_icon_to_uint(result));
2319 	return char_icon_to_uint(result);
2320 }
2321 
account_is_operator_or_admin(t_account * account,char const * channel)2322 extern int account_is_operator_or_admin(t_account * account, char const * channel)
2323 {
2324    if ((account_get_auth_operator(account,channel)==1) || (account_get_auth_operator(account,NULL)==1) ||
2325        (account_get_auth_admin(account,channel)==1) || (account_get_auth_admin(account,NULL)==1) )
2326       return 1;
2327    else
2328       return 0;
2329 
2330 }
2331 
char_icon_to_uint(char * icon)2332 static unsigned int char_icon_to_uint(char * icon)
2333 {
2334     unsigned int value;
2335 
2336     if (!icon) return 0;
2337     if (strlen(icon)!=4) return 0;
2338 
2339     value  = ((unsigned int)icon[0])<<24;
2340     value |= ((unsigned int)icon[1])<<16;
2341     value |= ((unsigned int)icon[2])<< 8;
2342     value |= ((unsigned int)icon[3])    ;
2343 
2344     return value;
2345 }
2346 
account_get_email(t_account * account)2347 extern char const * account_get_email(t_account * account)
2348 {
2349 	if(!account)
2350 	{
2351 		eventlog(eventlog_level_error,__FUNCTION__,"Unable to set account flag to TRUE");
2352 		return NULL;
2353 	}
2354 
2355     return account_get_strattr(account,"BNET\\acct\\email");
2356 }
2357 
account_set_email(t_account * account,char const * email)2358 extern int account_set_email(t_account * account, char const * email)
2359 {
2360 	if(!account)
2361 	{
2362 		eventlog(eventlog_level_error,__FUNCTION__,"Unable to set account flag to TRUE");
2363 		return -1;
2364 	}
2365 
2366     return account_set_strattr(account,"BNET\\acct\\email", email);
2367 }
2368 
2369 /**
2370 *  Westwood Online Extensions
2371 */
account_get_wol_apgar(t_account * account)2372 extern char const * account_get_wol_apgar(t_account * account)
2373 {
2374     return account_get_strattr(account,"WOL\\auth\\apgar");
2375 }
2376 
account_set_wol_apgar(t_account * account,char const * apgar)2377 extern int account_set_wol_apgar(t_account * account, char const * apgar)
2378 {
2379     eventlog(eventlog_level_debug,__FUNCTION__,"[** WOL **] WOL\\auth\\apgar = %s",apgar);
2380     return account_set_strattr(account,"WOL\\auth\\apgar",apgar);
2381 }
2382 
2383