forked from eddoww/Astonia-3.5
/
database.c
4127 lines (3417 loc) · 111 KB
/
database.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* $Id: database.c,v 1.34 2008/03/27 08:22:35 devel Exp $ (C) 2001 D.Brockhaus
*
* Database connectivity. Note that after startup, ALL database requests are handled by a background thread.
* This is to ensure that slow responses from the database wont slow down the main server. But you need to
* remember never to use any mysql calls from the main server thread.
*
* To avoid overwriting more current data, any routine wishing to save character data must use a
* where current_area=areaID. The only exception is load_char, which uses locking.
*
* $Log: database.c,v $
* Revision 1.34 2008/03/27 08:22:35 devel
* typo
*
* Revision 1.33 2008/01/03 10:10:25 devel
* added update_fix_item() hook
*
* Revision 1.32 2007/09/21 11:00:06 devel
* dont kick player during a play session for not having entered his name
*
* Revision 1.31 2007/09/11 17:07:21 devel
* bugfix in /klog
*
* Revision 1.30 2007/09/03 08:57:46 devel
* fixed typo
* ordered player notes by date
*
* Revision 1.29 2007/08/21 22:03:47 devel
* added ban time to punishment message
*
* Revision 1.28 2007/08/15 09:13:45 devel
* removed testing hack which preserved levels
*
* Revision 1.27 2007/08/09 11:12:28 devel
* query time statistics
*
* Revision 1.26 2007/07/28 18:51:23 devel
* faster (and more expensive) globl_online handling
*
* Revision 1.25 2007/07/27 05:59:32 devel
* summary statistics
*
* Revision 1.24 2007/07/24 18:54:55 devel
* removed locked, using banned instead
* exterminate needs a reason and kicks the player off line
*
* Revision 1.23 2007/07/13 15:47:16 devel
* clog -> charlog
*
* Revision 1.22 2007/07/09 11:21:19 devel
* merc -> merc0
*
* Revision 1.21 2007/07/04 09:20:40 devel
* added /support support
* added "you cannot escape" helper
*
* Revision 1.20 2007/07/01 12:09:25 devel
* changed dlog from showing char ID to sID
*
* Revision 1.19 2007/06/28 12:12:33 devel
* added /showalts logic
* added display of punishments for player
*
* Revision 1.18 2007/06/13 15:24:25 devel
* new punishment system
*
* Revision 1.17 2007/06/13 09:27:35 devel
* removed an extra log message
*
* Revision 1.16 2007/05/26 13:17:07 devel
* *** empty log message ***
*
* Revision 1.15 2007/05/25 12:21:32 devel
* added reset of depots after crash
* added max players online limit for unpaid accounts
*
* Revision 1.14 2007/05/02 13:05:49 devel
* login removes MS from seyans
*
* Revision 1.13 2007/02/24 14:09:54 devel
* NWO first checkin, feb 24
*
* Revision 1.12 2007/01/05 08:54:25 devel
* added support for locked owner data
* added too many tries error message support
*
* Revision 1.11 2006/12/14 14:28:50 devel
* added karma log
*
* Revision 1.10 2006/12/08 10:34:46 devel
* fixed bug in iplogging
*
* Revision 1.9 2006/10/06 17:26:17 devel
* fixed bug with ipbanned msg shown when account unpaid
*
* Revision 1.8 2006/09/22 12:12:06 devel
* added iplog and ipban logic to logins
*
* Revision 1.7 2006/09/22 09:55:46 devel
* added calls to badpass_ip
*
* Revision 1.6 2006/08/19 17:27:08 devel
* changed security setup to new mysql version (4.x)
*
* Revision 1.5 2006/06/23 16:19:37 ssim
* fixed teufel PK bugs
*
* Revision 1.4 2006/04/26 16:05:44 ssim
* unknown change???
*
* Revision 1.3 2006/04/07 10:02:06 ssim
* fixed hard-coded pail_till=X for a new year
*
* Revision 1.2 2006/03/30 12:16:01 ssim
* changed some elogs to xlogs to avoid cluttering the error log file with unimportant errors
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/file.h>
#include <string.h>
#include <time.h>
#include <stdarg.h>
#include <dirent.h>
#include <signal.h>
#include <sys/time.h>
#include <mysql/mysql.h>
#include <mysql/mysqld_error.h>
#include <mysql/errmsg.h>
#include <pthread.h>
#include <ctype.h>
#include <zlib.h>
#include <math.h>
#include "server.h"
#include "log.h"
#include "create.h"
#include "player.h"
#include "sleep.h"
#include "tool.h"
#include "drdata.h"
#include "drvlib.h"
#include "timer.h"
#include "direction.h"
#include "map.h"
#include "mem.h"
#include "lookup.h"
#include "chat.h"
#include "database.h"
#include "effect.h"
#include "task.h"
#include "skill.h"
#include "talk.h"
#include "sleep.h"
#include "clan.h"
#include "libload.h"
#include "io.h"
#include "mail.h"
#include "player.h"
#include "consistency.h"
#include "btrace.h"
#include "date.h"
#include "club.h"
#include "badip.h"
#include "depot.h"
#include "note.h"
#define DT_QUERY 1
#define DT_LOAD 2
#define DT_AREA 3
#define DT_LOOKUP_ID 4
#define DT_LOOKUP_NAME 5
#define DT_PUNISH 6
#define DT_CLANLOG 7
#define DT_EXTERMINATE 8
#define DT_CHECK_TASK 9
#define DT_RENAME 10
#define DT_RESCUE 11
#define DT_STAT_UPDATE 12
#define DT_LASTSEEN 13
#define DT_LOCKNAME 14
#define DT_UNLOCKNAME 15
#define DT_CLUBS 16
#define DT_CLANS 17
#define DT_CLANMEMBERS 18
#define DT_WARN 19
#define DT_READNOTES 20
#define DT_KARMALOG 21
#define DT_PLRNOTES 22
#define DT_SHOWALTS 23
#define DT_SUMMARY 24
#define DT_UPDATEGLOBAL 25
#define MAXAREA 60
#define MAXMIRROR 27
#define SAVEBLOCKSIZE 4096
MYSQL mysql;
static void *db_thread(void *);
static int add_query(int type,char *opt1,char *opt2,int nolock);
static void load_char(char *name,char *password);
static void update_arealist(void);
void db_readnotes(int masterID,int victimID);
void db_showalts(int masterID,int victimID);
void db_plr_notes(int victimID,int sID);
void db_punish(char *who,char *desc);
void db_warn(char *who,char *desc);
void db_lookup_id(char *tID);
void db_lookup_name(char *name);
void db_karmalog(int masterID);
void db_lastseen(char *suID,char *srID);
void db_read_clanlog(char *query);
void db_exterminate(char *name,char *master);
void db_rename(char *name,char *master);
void db_lockname(char *name,char *master);
void db_unlockname(char *name,char *master);
void db_rescue_char(char *IDstring);
void db_stat_update(void);
void db_read_clubs(void);
void db_read_clans(void);
int isbanned_iplog(int ip);
void add_iplog(int ID,unsigned int ip);
void db_read_clan_membercount(int cnr);
void db_summary(char *key,int sID);
void db_update_global(void);
static pthread_t db_tid;
static pthread_mutex_t data_mutex;
static pthread_mutex_t server_mutex;
volatile static int db_thread_quit=0;
volatile int global_online,max_online;
unsigned int query_cnt=0,query_long=0,query_long_max=0;
unsigned long long query_time=0,query_long_time=0;
int summary_query_cnt=0,summary_query_hour=0;
long long summary_query_time=0,summary_query_long=0;
unsigned int save_char_cnt=0,save_area_cnt=0,exit_char_cnt=0,save_subscriber_cnt=0,save_char_mirror_cnt=0,load_char_cnt=0;
unsigned int query_stat[20];
static char mysqlpass[80];
static void makemysqlpass(void)
{
static char key1[]={117, 127, 98, 38, 118, 115, 100, 104,0};
static char key2[]={"qpc74a7v"};
static char key3[]={"bcoxsa1k"};
int n;
for (n=0; key1[n]; n++) {
mysqlpass[n]=key1[n]^key2[n]^key3[n];
//printf("%d, ",mysqlpass[n]);
}
mysqlpass[n]=0;
//printf("\n%s\n",mysqlpass);
}
static void destroymysqlpass(void)
{
bzero(mysqlpass,sizeof(mysqlpass));
}
int mysql_query_con(MYSQL *my,const char *query)
{
int err,ret,nr;
unsigned long long start,diff;
char buf[80];
//xlog("size %d, query %.80s",strlen(query),query);
//if (!demon && areaID==9) { int tmp; tmp=RANDOM(3); if (tmp) sleep(tmp); }
start=timel();
while (1) {
ret=mysql_query(my,query);
err=mysql_errno(my);
if (err>=CR_MIN_ERROR && err<=CR_MAX_ERROR) {
elog("lost connection to server: %s",mysql_error(my));
mysql_close(my);
sleep(1);
makemysqlpass();
mysql_real_connect(my,"localhost","root",mysqlpass,"mysql",0,NULL,0);
destroymysqlpass();
} else if (err==ER_NO_SUCH_TABLE) {
elog("wrong database: %s",mysql_error(my));
sprintf(buf,"use merc%d",worldID);
mysql_query(my,buf);
} else break;
}
diff=timel()-start;
query_cnt++;
query_time+=diff;
if (diff>10000000) {
query_long++;
query_long_time+=diff;
if (diff>query_long_max) query_long_max=diff;
}
nr=diff/(500*1000ull);
if (nr>=0 && nr<20) {
query_stat[nr]++;
}
summary_query_long=max(diff,summary_query_long);
summary_query_cnt++;
summary_query_time+=diff;
return ret;
}
volatile int db_store=0;
MYSQL_RES *mysql_store_result_cnt(MYSQL *my)
{
db_store++;
return mysql_store_result(my);
}
void mysql_free_result_cnt(MYSQL_RES *result)
{
db_store--;
mysql_free_result(result);
}
static int start_db_thread(void)
{
pthread_attr_t attr;
if (pthread_mutex_init(&data_mutex,NULL)) return 0;
if (pthread_mutex_init(&server_mutex,NULL)) return 0;
if (pthread_attr_init(&attr)) return 0;
if (pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_JOINABLE)) return 0;
if (pthread_create(&db_tid,&attr,db_thread,NULL)) return 0;
xlog("Created background thread for database accesses");
return 1;
}
static void wait_db_thread(void)
{
int exit_status;
xlog("Waiting for database thread to finish");
db_thread_quit=1;
pthread_join(db_tid, (void **) &exit_status);
xlog("Database thread finished with exit code %d",exit_status);
if (query_cnt==0) query_cnt=1; // sanity check, should never happen...
xlog("%d queries, %.2fs (%.2fms/q), %d long queries. Average long query: %.2fms, longest query: %.2fms.",
query_cnt,query_time/1000000.0,(double)query_time/query_cnt/1000.0,
query_long,
query_long ? query_long_time/query_long/1000.0 : 0.0,
query_long_max/1000.0);
xlog("save_char_cnt=%d, save_area_cnt=%d, exit_char_cnt=%d, save_subscriber_cnt=%d, save_char_mirror_cnt=%d, load_char_cnt=%d",
save_char_cnt,save_area_cnt,exit_char_cnt,save_subscriber_cnt,save_char_mirror_cnt,load_char_cnt);
}
/*void *my_mysql_malloc(unsigned int size)
{
return xmalloc(size,IM_MYSQL);
}
void my_mysql_free(void *ptr)
{
return xfree(ptr);
}
void *my_mysql_realloc(void *ptr,unsigned int size)
{
return xrealloc(ptr,size,IM_MYSQL);
}
void mysql_set_malloc_proc(void* (*new_malloc_proc)(size_t));
void mysql_set_free_proc(void (*new_free_proc)(void*));
void mysql_set_realloc_proc(void* (*new_realloc_proc)(void *,size_t));*/
// start connection to database and initialise database if needed
int init_database(void)
{
char buf[1024];
//mysql_set_malloc_proc(my_mysql_malloc);
//mysql_set_free_proc(my_mysql_free);
//mysql_set_realloc_proc(my_mysql_realloc);
// init database client
if (!mysql_init(&mysql)) {
elog("Failed to initialise database: Error: %s",mysql_error(&mysql));
return 0;
}
// try to login as root with our password
makemysqlpass();
if (!mysql_real_connect(&mysql,"localhost","root",mysqlpass,"mysql",0,NULL,0)) {
destroymysqlpass();
xlog("Connect to database failed.");
} else {
destroymysqlpass();
xlog("Login to database as root OK");
}
// set default database to merc
sprintf(buf,"use merc%d",worldID);
if (mysql_query(&mysql,buf)) {
elog("Failed to select database merc: Error: %s (%d)",mysql_error(&mysql),mysql_errno(&mysql));
} else xlog("Using existing database merc%d",worldID);
// sweep database, remove all players marked as online here. we're just starting, there shouldnt be any...
sprintf(buf,"update chars set current_area=0,current_mirror=0,spacer=45 where current_area=%d and current_mirror=%d",areaID,areaM);
if (mysql_query(&mysql,buf)) {
elog("Could not sweep database: Error: %s (%d)",mysql_error(&mysql),mysql_errno(&mysql));
return 0;
}
xlog("sweep_database: removed %lld characters",mysql_affected_rows(&mysql));
sprintf(buf,"update depot set area=-1,mirror=-1,cID=-1 where area=%d and mirror=%d",areaID,areaM);
if (mysql_query(&mysql,buf)) {
elog("Could not sweep database(2): Error: %s (%d)",mysql_error(&mysql),mysql_errno(&mysql));
return 0;
}
xlog("sweep_database: removed %lld depots",mysql_affected_rows(&mysql));
sprintf(buf,"update area set players=0 where ID=%d and mirror=%d",areaID,areaM);
if (mysql_query(&mysql,buf)) {
elog("Could not reset area online count: Error: %s (%d)",mysql_error(&mysql),mysql_errno(&mysql));
return 0;
}
// we need an up to date arealist before starting
update_arealist();
db_read_clubs();
if (multi) return start_db_thread();
else return 1;
}
#define MAXDRDATA (1024*64)
// save character cn to database
// will mark cn as logged out if area is not zero
#define COMPRESS_MAGIC 0x84736251
int compress_escape_string(MYSQL *my,unsigned char *dst,unsigned char *src,int ilen,int *xlen)
{
struct z_stream_s zs;
int ret,olen,rlen;
unsigned char buf[ilen*2];
void *my_zlib_malloc(void *dummy,unsigned int cnt,unsigned int size);
void my_zlib_free(void *dummy,void *ptr);
bzero(&zs,sizeof(zs));
zs.zalloc=my_zlib_malloc;
zs.zfree=my_zlib_free;
deflateInit(&zs,1);
zs.next_in=src;
zs.avail_in=ilen;
zs.next_out=buf+12;
zs.avail_out=ilen*2-12;
ret=deflate(&zs,Z_SYNC_FLUSH);
deflateEnd(&zs);
if (ret!=Z_OK) {
elog("compression failure #1, database");
return 0;
}
if (zs.avail_in) {
elog("compression failure #2, database");
return 0;
}
olen=(ilen*2-12)-zs.avail_out;
*(unsigned int*)(buf+0)=COMPRESS_MAGIC;
*(unsigned int*)(buf+4)=ilen;
*(unsigned int*)(buf+8)=olen;
mysql_real_escape_string(my,dst,buf,olen+12);
rlen=strlen(dst);
//xlog("ilen=%d, olen=%d, rlen=%d",ilen,olen+12,rlen);
if (xlen) *xlen=olen+12;
return rlen;
}
int uncompress_string(unsigned char *dst,unsigned char *src,int maxout)
{
struct z_stream_s zs;
int ret,size;
void *my_zlib_malloc(void *dummy,unsigned int cnt,unsigned int siz);
void my_zlib_free(void *dummy,void *ptr);
bzero(&zs,sizeof(zs));
zs.zalloc=my_zlib_malloc;
zs.zfree=my_zlib_free;
inflateInit(&zs);
zs.next_in=src+12;
zs.avail_in=*(unsigned int*)(src+8);
zs.next_out=dst;
zs.avail_out=maxout;
ret=inflate(&zs,Z_SYNC_FLUSH);
inflateEnd(&zs);
if (ret!=Z_OK) {
elog("Compression error %d\n",ret);
return 0;
}
size=maxout-zs.avail_out;
if (zs.avail_in) {
elog("HELP (%d, want=%d, got=%d)",zs.avail_in,maxout,size);
btrace("oopsie in uncompress_string()");
}
//xlog("size=%d/%d",size,maxout);
return size;
}
static void spec_log(unsigned char *src);
void save_depot(int sID,struct depot_ppd *dat,int cID,int leaving)
{
char buf[sizeof(struct depot_ppd)*2+12+1024];
char query[sizeof(struct depot_ppd)*2+12+1024+160];
int len,xlen,add,expandto,size;
if (!dat->loaded) {
xlog("not saving depot, was not loaded");
return;
}
if (dat->lastsave==dat->changes) {
if (leaving) {
sprintf(query,"update depot set area=-1,mirror=-1,cID=-1 where ID=%d and area=%d and mirror=%d and cID=%d",sID,areaID,areaM,cID);
if (!add_query(DT_QUERY,query,"save_depot",0)) elog("Failed to update (small) depot (sID=%d) on logout",sID);
}
//xlog("not saving depot, no change");
return;
}
dat->lastsave=dat->changes;
len=compress_escape_string(&mysql,buf,(unsigned char*)dat,sizeof(struct depot_ppd),&xlen);
//xlog("depot for sID=%d: %d/%d bytes",sID,xlen,sizeof(struct depot_ppd));
size=xlen+16;
expandto=((size+1024-1)/1024)*1024;
add=expandto-size;
memset(buf+len,'X',add);
buf[len+add]=0;
//xlog("add=%d,size=%d,expandto=%d",add,size,expandto);
if (leaving) sprintf(query,"update depot set data=\"%s\",area=-1,mirror=-1,cID=-1 where ID=%d and area=%d and mirror=%d and cID=%d",buf,sID,areaID,areaM,cID);
else sprintf(query,"update depot set data=\"%s\" where ID=%d and area=%d and mirror=%d and cID=%d",buf,sID,areaID,areaM,cID);
if (!add_query(DT_QUERY,query,"save_depot",0)) elog("Failed to update depot (sID=%d) on logout",sID);
}
// save character cn to database
// will mark cn as logged out if area is not zero
int save_char(int cn,int area)
{
int size,expandto,add;
unsigned char cdata[sizeof(struct character)],idata[sizeof(struct item)*(INVENTORYSIZE+1)],ddata[MAXDRDATA];
unsigned char cbuf[sizeof(cdata)*2],ibuf[sizeof(idata)*2],dbuf[sizeof(ddata)*2];
unsigned char buf[sizeof(cbuf)+sizeof(ibuf)+sizeof(dbuf)+80];
int n,in,ilen=0,dlen=0,clen=0,xilen=0,xdlen=0,xclen=0;
struct item *itmp;
struct data *dat;
//xlog("save char %s (%d, ID=%d)",ch[cn].name,cn,ch[cn].ID);
// character data
memcpy(cdata,ch+cn,sizeof(struct character));
// items
itmp=(void*)(idata);
for (n=0; n<INVENTORYSIZE; n++) {
if ((in=ch[cn].item[n])) {
*itmp=it[in]; ilen+=sizeof(struct item);
if (IDR_ISSPELL(itmp->driver)) { // make drdata contain the remaining duration
*(signed long*)(itmp->drdata)-=ticker;
*(signed long*)(itmp->drdata+4)-=ticker;
//xlog("save: remembering time for spell. time left: %ld ticks",*(unsigned long*)(itmp->drdata));
}
itmp++;
}
}
if ((in=ch[cn].citem)) {
*itmp++=it[in]; ilen+=sizeof(struct item);
}
// drdata aka ppd
for (dat=ch[cn].dat; dat; dat=dat->next) {
if (!(dat->ID&PERSISTENT_PLAYER_DATA)) continue;
if (dat->ID==DRD_DEPOT_PPD) {
save_depot(ch[cn].sID,dat->data,ch[cn].ID,area);
continue;
}
*(unsigned int*)(ddata+dlen)=dat->ID; dlen+=4;
*(unsigned int*)(ddata+dlen)=dat->size; dlen+=4;
memcpy(ddata+dlen,dat->data,dat->size); dlen+=dat->size;
//if (dat->size>1024) xlog("ppd: id=%d, size=%d",(dat->ID&65535),dat->size);
}
// pad all records in the database to a multiple of SAVEBLOCKSIZE bytes:
// calculate the size the record will have in the database (mysql sucks soo bad)
// the three blocks plus the name plus the fixed size stuff
size=0;
// empty INTs are not written:
if (!ch[cn].clan) size-=4;
if (!ch[cn].clan_rank) size-=4;
if (!ch[cn].clan_serial) size-=4;
if (!ch[cn].exp) size-=4;
// ch.current_area and ch.current_mirror are not added if set, the field spacer is used to deal with those
mysql_real_escape_string(&mysql,cbuf,cdata,sizeof(struct character)); clen=xclen=sizeof(struct character);
ilen=compress_escape_string(&mysql,ibuf,idata,ilen,&xilen);
dlen=compress_escape_string(&mysql,dbuf,ddata,dlen,&xdlen);
size+=xclen+xilen+xdlen+strlen(ch[cn].name)+74;
expandto=((size+SAVEBLOCKSIZE-1)/SAVEBLOCKSIZE)*SAVEBLOCKSIZE;
add=expandto-size;
memset(dbuf+dlen,'X',add);
dbuf[dlen+add]=0;
//xlog("size=%d, expandto=%d, add=%d",size,expandto,add);
// note the ...and current_area=areaID and current_mirror=areaM, this makes sure that we will not overwrite
// the data if the character already left our area (needed because this is a delayed write)
if (!area) { // just a data backup.
sprintf(buf,"update chars set chr='%s',item='%s',ppd='%s',class=%u,clan=%d,clan_rank=%d,clan_serial=%d,experience=%d,mirror=%d,spacer=0 where ID=%d and current_area=%d and current_mirror=%d",
cbuf,
ibuf,
dbuf,
(unsigned int)(ch[cn].flags&0x0fffffff),
ch[cn].clan,
ch[cn].clan_rank,
ch[cn].clan_serial,
max(ch[cn].exp,ch[cn].exp_used),
ch[cn].mirror,
ch[cn].ID,
areaID,
areaM);
save_char_cnt++;
} else { // logout
#ifdef CHARINFO
sprintf(buf,"update charinfo set current_area=0,logout_time=%d,class=%u,clan=%d,clan_rank=%d,clan_serial=%d,experience=%d where ID=%d",
time_now,
(unsigned int)(ch[cn].flags&0x0fffffff),
ch[cn].clan,
ch[cn].clan_rank,
ch[cn].clan_serial,
max(ch[cn].exp,ch[cn].exp_used),
ch[cn].ID);
if (!add_query(DT_QUERY,buf,"save_charinfo",0)) {
elog("Failed to update charinfo %s (ID=%d) on logout",ch[cn].name,ch[cn].ID);
}
#endif
sprintf(buf,"update chars set chr='%s',item='%s',ppd='%s',current_area=0,current_mirror=0,allowed_area=%d,logout_time=%d,class=%u,clan=%d,clan_rank=%d,clan_serial=%d,experience=%d,mirror=%d,spacer=42 where ID=%d and current_area=%d and current_mirror=%d",
cbuf,
ibuf,
dbuf,
area,
time_now,
(unsigned int)(ch[cn].flags&0x0fffffff),
ch[cn].clan,
ch[cn].clan_rank,
ch[cn].clan_serial,
max(ch[cn].exp,ch[cn].exp_used),
ch[cn].mirror,
ch[cn].ID,
areaID,
areaM);
exit_char_cnt++;
}
if (!add_query(DT_QUERY,buf,"save_char",0)) {
elog("Failed to update account %s (ID=%d) on logout",ch[cn].name,ch[cn].ID);
return 0;
}
if (area) {
add_query(DT_UPDATEGLOBAL,"","update_global",0);
}
return 1;
}
// mark char cn as not-online without saving any other data
int release_char(int cn)
{
char buf[256];
xlog("release char %s (%d, ID=%d)",ch[cn].name,cn,ch[cn].ID);
#ifdef CHARINFO
sprintf(buf,"update charinfo set current_area=0 where ID=%d",ch[cn].ID);
if (!add_query(DT_QUERY,buf,"release_charinfo",0)) {
elog("Failed to release charinfo %s (ID=%d) on logout",ch[cn].name,ch[cn].ID);
}
#endif
sprintf(buf,"update chars set current_area=0,current_mirror=0,spacer=43 where ID=%d and current_area=%d and current_mirror=%d",ch[cn].ID,areaID,areaM);
if (!add_query(DT_QUERY,buf,"release_char",0)) {
elog("Failed to release char %s (ID=%d) on logout",ch[cn].name,ch[cn].ID);
return 0;
}
return 1;
}
int release_char_nolock(int cn)
{
char buf[256];
xlog("release char %s (%d, ID=%d)",ch[cn].name,cn,ch[cn].ID);
#ifdef CHARINFO
sprintf(buf,"update charinfo set current_area=0 where ID=%d",ch[cn].ID);
if (!add_query(DT_QUERY,buf,"release_charinfo",1)) {
elog("Failed to release char %s (ID=%d) on logout",ch[cn].name,ch[cn].ID);
return 0;
}
#endif
sprintf(buf,"update chars set current_area=0,current_mirror=0,spacer=43 where ID=%d and current_area=%d and current_mirror=%d",ch[cn].ID,areaID,areaM);
if (!add_query(DT_QUERY,buf,"release_char",1)) {
elog("Failed to release char %s (ID=%d) on logout",ch[cn].name,ch[cn].ID);
return 0;
}
return 1;
}
void exit_database(void)
{
if (multi) wait_db_thread(); // wait for background thread to write back data still in buffers
else tick_login(); // write back data still in buffers
mysql_close(&mysql);
}
void area_alive(int godown)
{
char buf[256];
static int last_time=0;
static unsigned long long last_transfer=0;
unsigned long long diff;
int bps=0,tdiff;
extern int mmem_usage,last_error;
if (last_time) {
diff=sent_bytes_raw+rec_bytes_raw-last_transfer;
tdiff=time_now-last_time;
if (tdiff) bps=diff/tdiff;
}
last_time=time_now;
last_transfer=sent_bytes_raw+rec_bytes_raw;
if (!godown) {
sprintf(buf,"update area set alive_time=%d,idle=%d,players=%d,server=%u,port=%d,bps=%d,mem_usage=%d,last_error=%d where ID=%d and mirror=%d",
time_now,
server_idle,
online,
serverID,
server_port,
bps,
mmem_usage,
last_error,
areaID,
areaM);
} else {
sprintf(buf,"update area set alive_time=0,idle=0,players=0,server=0,port=0,bps=0,mem_usage=0,last_error=0 where ID=%d and mirror=%d",
areaID,
areaM);
}
if (!add_query(DT_QUERY,buf,"area_alive",0)) {
elog("Could not add query to update area_alive");
}
save_area_cnt++;
}
void call_area_load(void)
{
if (!add_query(DT_AREA,NULL,"area load",0)) {
elog("Could note add query to load area");
}
}
void call_check_task(void)
{
if (!add_query(DT_CHECK_TASK,NULL,"check task",0)) {
elog("Could note add query to check task");
}
}
void call_stat_update(void)
{
if (!add_query(DT_STAT_UPDATE,NULL,"stat update",0)) {
elog("Could note add query for stat update");
}
}
void log_dc(int cn)
{
char buf[80];
if (!(ch[cn].flags&CF_PLAYER)) {
elog("log_dc called for non-player %d (%s)",cn,ch[cn].name);
return;
}
sprintf(buf,"update chars set logout_time=%d where ID=%d",time_now,ch[cn].ID);
add_query(DT_QUERY,buf,"log_dc",0);
}
int add_task(void *task,int len)
{
char buf[len*4+256];
char data[len*4];
mysql_real_escape_string(&mysql,data,task,len);
sprintf(buf,"insert into task values(0,'%s')",data);
if (!add_query(DT_QUERY,buf,"add_task",0)) {
elog("Failed to add task");
return 0;
}
return 1;
}
int change_area(int cn,int area,int x,int y)
{
int server,port,nr;
if (!get_area(area,ch[cn].mirror,&server,&port)) return 0;
ch[cn].tmpx=x;
ch[cn].tmpy=y;
ch[cn].tmpa=area;
ch[cn].flags|=CF_AREACHANGE;
kick_char(cn);
nr=ch[cn].player;
if (nr) player_to_server(nr,server,port);
//xlog("sending %s (%d,%d) to area %d",ch[cn].name,cn,nr,area);
return 1;
}
int dlog(int cn,int in,char *format,...)
{
char tbuf[1024],pbuf[80],xbuf[1024]={""},ibuf[1024]={""};
va_list args;
unsigned long long prof;
int nr,addr;
prof=prof_start(25);
va_start(args,format);
vsnprintf(tbuf,1020,format,args);
va_end(args);
if (cn) {
if ((ch[cn].flags&CF_PLAYER) && (nr=ch[cn].player) && (addr=get_player_addr(nr))) sprintf(pbuf,",IP=%u.%u.%u.%u",(addr>>0)&255,(addr>>8)&255,(addr>>16)&255,(addr>>24)&255);
else pbuf[0]=0;
sprintf(ibuf," [name=%s,level=%d,saves=%d,hp=%d,end=%d,mana=%d,exp=%d,gold=%d,pos=%d,%d%s]",
ch[cn].name,
ch[cn].level,
ch[cn].saves,
ch[cn].hp/POWERSCALE,
ch[cn].endurance/POWERSCALE,
ch[cn].mana/POWERSCALE,
ch[cn].exp,
ch[cn].gold,
ch[cn].x,
ch[cn].y,
pbuf);
}
if (in) {
sprintf(xbuf," [name=%s, desc=%s, value=%d, sprite=%d, driver=%d, ID=%X, mods=(%d:%d, %d:%d, %d:%d, %d:%d, %d:%d), complex=%d, quality=%d]",
it[in].name,
it[in].description,
it[in].value,
it[in].sprite,
it[in].driver,
it[in].ID,
it[in].mod_index[0],it[in].mod_value[0],
it[in].mod_index[1],it[in].mod_value[1],
it[in].mod_index[2],it[in].mod_value[2],
it[in].mod_index[3],it[in].mod_value[3],
it[in].mod_index[4],it[in].mod_value[4],
it[in].complexity,
it[in].quality);
}
ilog("sID=%d: %s%s%s",cn ? ch[cn].sID : 0,tbuf,ibuf,xbuf);
prof_stop(25,prof);
return 1;
}
int add_note(int sID,int kind,char *desc,...)
{
va_list args;
char buf[1024],buf2[1024];
va_start(args,desc);
vsnprintf(buf,900,desc,args);
va_end(args);
sprintf(buf2,"insert into merc0.sub_note values(0,%d,%d,%d,'%s')",sID,kind,time_now,buf);
return add_query(DT_QUERY,buf2,"add_note",0);
}
int add_clanlog(int clan,int serial,int cID,int prio,char *format,...)
{
char buf[256],ebuf[512],qbuf[1024];
va_list args;
va_start(args,format);
vsnprintf(buf,250,format,args);
va_end(args);
mysql_real_escape_string(&mysql,ebuf,buf,strlen(buf));
sprintf(qbuf,"insert into clanlog values(0,%d,%d,%d,%d,%d,'%s')",
time_now,
clan,
serial,
cID,
prio,
ebuf);
return add_query(DT_QUERY,qbuf,"add_clanlog",0);
}
//select 278527,clan,serial,time,content from clanlog where time>=1055577172 and time<=1059173572 and clan=14 and serial=15 and cID=104665
int lookup_clanlog(int cnID,int clan,int serial,int coID,int prio,int from_time,int to_time)
{
char buf[512];//,len;
int len;
len=sprintf(buf,"select %d,clan,serial,time,content from clanlog where time>=%d and time<=%d",cnID,from_time,to_time);
if (clan) len+=sprintf(buf+len," and clan=%d",clan);
if (serial) len+=sprintf(buf+len," and serial=%d",serial);
if (coID) len+=sprintf(buf+len," and cID=%d",coID);
if (prio) len+=sprintf(buf+len," and prio<=%d",prio);
len+=sprintf(buf+len," order by time limit 51");
return add_query(DT_CLANLOG,buf,"lookup_clanlog",0);
}
int exterminate(int masterID,int victimID,char *desc)
{
char buf[80];
sprintf(buf,"%010d:%010d",masterID,victimID);
return add_query(DT_EXTERMINATE,buf,desc,0);