-
Notifications
You must be signed in to change notification settings - Fork 756
/
char.c
6608 lines (5779 loc) · 224 KB
/
char.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
/**
* This file is part of Hercules.
* http://herc.ws - http://github.com/HerculesWS/Hercules
*
* Copyright (C) 2012-2018 Hercules Dev Team
* Copyright (C) Athena Dev Teams
*
* Hercules is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define HERCULES_CORE
#include "config/core.h" // CONSOLE_INPUT
#include "char.h"
#include "char/HPMchar.h"
#include "char/geoip.h"
#include "char/int_auction.h"
#include "char/int_clan.h"
#include "char/int_elemental.h"
#include "char/int_guild.h"
#include "char/int_homun.h"
#include "char/int_mail.h"
#include "char/int_mercenary.h"
#include "char/int_party.h"
#include "char/int_pet.h"
#include "char/int_quest.h"
#include "char/int_rodex.h"
#include "char/int_storage.h"
#include "char/inter.h"
#include "char/loginif.h"
#include "char/mapif.h"
#include "char/pincode.h"
#include "common/HPM.h"
#include "common/cbasetypes.h"
#include "common/conf.h"
#include "common/console.h"
#include "common/core.h"
#include "common/db.h"
#include "common/memmgr.h"
#include "common/mapindex.h"
#include "common/mmo.h"
#include "common/nullpo.h"
#include "common/showmsg.h"
#include "common/socket.h"
#include "common/strlib.h"
#include "common/sql.h"
#include "common/timer.h"
#include "common/utils.h"
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h> // stat()
#if MAX_MAP_SERVERS > 1
# ifdef _MSC_VER
# pragma message("WARNING: your settings allow more than one map server to connect, this is deprecated dangerous feature USE IT AT YOUR OWN RISK")
# else
# warning your settings allow more than one map server to connect, this is deprecated dangerous feature USE IT AT YOUR OWN RISK
# endif
#endif
// private declarations
char char_db[256] = "char";
char scdata_db[256] = "sc_data";
char cart_db[256] = "cart_inventory";
char inventory_db[256] = "inventory";
char charlog_db[256] = "charlog";
char storage_db[256] = "storage";
char interlog_db[256] = "interlog";
char skill_db[256] = "skill";
char memo_db[256] = "memo";
char guild_db[256] = "guild";
char guild_alliance_db[256] = "guild_alliance";
char guild_castle_db[256] = "guild_castle";
char guild_expulsion_db[256] = "guild_expulsion";
char guild_member_db[256] = "guild_member";
char guild_position_db[256] = "guild_position";
char guild_skill_db[256] = "guild_skill";
char guild_storage_db[256] = "guild_storage";
char party_db[256] = "party";
char pet_db[256] = "pet";
char mail_db[256] = "mail"; // MAIL SYSTEM
char auction_db[256] = "auction"; // Auctions System
char friend_db[256] = "friends";
char hotkey_db[256] = "hotkey";
char quest_db[256] = "quest";
char rodex_db[256] = "rodex_mail";
char rodex_item_db[256] = "rodex_items";
char homunculus_db[256] = "homunculus";
char skill_homunculus_db[256] = "skill_homunculus";
char mercenary_db[256] = "mercenary";
char mercenary_owner_db[256] = "mercenary_owner";
char ragsrvinfo_db[256] = "ragsrvinfo";
char elemental_db[256] = "elemental";
char account_data_db[256] = "account_data";
char acc_reg_num_db[32] = "acc_reg_num_db";
char acc_reg_str_db[32] = "acc_reg_str_db";
char char_reg_str_db[32] = "char_reg_str_db";
char char_reg_num_db[32] = "char_reg_num_db";
struct char_interface char_s;
struct char_interface *chr;
char db_path[1024] = "db";
char wisp_server_name[NAME_LENGTH] = "Server";
char login_ip_str[128];
uint32 login_ip = 0;
uint16 login_port = 6900;
char char_ip_str[128];
char bind_ip_str[128];
uint32 bind_ip = INADDR_ANY;
int char_maintenance_min_group_id = 0;
bool enable_char_creation = true; ///< Whether to allow character creation.
bool name_ignoring_case = false; // Allow or not identical name for characters but with a different case by [Yor]
int char_name_option = 0; // Option to know which letters/symbols are authorized in the name of a character (0: all, 1: only those in char_name_letters, 2: all EXCEPT those in char_name_letters) by [Yor]
char unknown_char_name[NAME_LENGTH] = "Unknown"; // Name to use when the requested name cannot be determined
#define TRIM_CHARS "\255\xA0\032\t\x0A\x0D " //The following characters are trimmed regardless because they cause confusion and problems on the servers. [Skotlex]
char char_name_letters[1024] = ""; // list of letters/symbols allowed (or not) in a character name. by [Yor]
int char_del_level = 0; ///< From which level you can delete character [Lupus]
int char_del_delay = 86400;
bool char_aegis_delete = false; ///< Verify if char is in guild/party or char and reacts as Aegis does (disallow deletion), @see chr->delete2_req.
int max_connect_user = -1;
int gm_allow_group = -1;
int autosave_interval = DEFAULT_AUTOSAVE_INTERVAL;
int start_zeny = 0;
/// Start items for new characters
struct start_item_s {
int id;
int amount;
int loc;
bool stackable;
};
VECTOR_DECL(struct start_item_s) start_items;
int guild_exp_rate = 100;
//Custom limits for the fame lists. [Skotlex]
int fame_list_size_chemist = MAX_FAME_LIST;
int fame_list_size_smith = MAX_FAME_LIST;
int fame_list_size_taekwon = MAX_FAME_LIST;
// Char-server-side stored fame lists [DracoRPG]
struct fame_list smith_fame_list[MAX_FAME_LIST];
struct fame_list chemist_fame_list[MAX_FAME_LIST];
struct fame_list taekwon_fame_list[MAX_FAME_LIST];
// Initial position (it's possible to set it in conf file)
#ifdef RENEWAL
struct point start_point = { 0, 97, 90 };
#else
struct point start_point = { 0, 53, 111 };
#endif
unsigned short skillid2idx[MAX_SKILL_ID];
//-----------------------------------------------------
// Auth database
//-----------------------------------------------------
#define AUTH_TIMEOUT 30000
static struct DBMap *auth_db; // int account_id -> struct char_auth_node*
//-----------------------------------------------------
// Online User Database
//-----------------------------------------------------
/**
* @see DBCreateData
*/
static struct DBData char_create_online_char_data(union DBKey key, va_list args)
{
struct online_char_data* character;
CREATE(character, struct online_char_data, 1);
character->account_id = key.i;
character->char_id = -1;
character->server = -1;
character->pincode_enable = -1;
character->fd = -1;
character->waiting_disconnect = INVALID_TIMER;
return DB->ptr2data(character);
}
void char_set_account_online(int account_id)
{
WFIFOHEAD(chr->login_fd,6);
WFIFOW(chr->login_fd,0) = 0x272b;
WFIFOL(chr->login_fd,2) = account_id;
WFIFOSET(chr->login_fd,6);
}
void char_set_account_offline(int account_id)
{
WFIFOHEAD(chr->login_fd,6);
WFIFOW(chr->login_fd,0) = 0x272c;
WFIFOL(chr->login_fd,2) = account_id;
WFIFOSET(chr->login_fd,6);
}
void char_set_char_charselect(int account_id)
{
struct online_char_data* character;
character = (struct online_char_data*)idb_ensure(chr->online_char_db, account_id, chr->create_online_char_data);
if( character->server > -1 )
if( chr->server[character->server].users > 0 ) // Prevent this value from going negative.
chr->server[character->server].users--;
character->char_id = -1;
character->server = -1;
if(character->pincode_enable == -1)
character->pincode_enable = pincode->charselect + pincode->enabled;
if(character->waiting_disconnect != INVALID_TIMER) {
timer->delete(character->waiting_disconnect, chr->waiting_disconnect);
character->waiting_disconnect = INVALID_TIMER;
}
if (chr->login_fd > 0 && !sockt->session[chr->login_fd]->flag.eof)
chr->set_account_online(account_id);
}
void char_set_char_online(int map_id, int char_id, int account_id)
{
struct online_char_data* character;
struct mmo_charstatus *cp;
//Update DB
if( SQL_ERROR == SQL->Query(inter->sql_handle, "UPDATE `%s` SET `online`='1' WHERE `char_id`='%d' LIMIT 1", char_db, char_id) )
Sql_ShowDebug(inter->sql_handle);
//Check to see for online conflicts
character = (struct online_char_data*)idb_ensure(chr->online_char_db, account_id, chr->create_online_char_data);
if( character->char_id != -1 && character->server > -1 && character->server != map_id )
{
ShowNotice("chr->set_char_online: Character %d:%d marked in map server %d, but map server %d claims to have (%d:%d) online!\n",
character->account_id, character->char_id, character->server, map_id, account_id, char_id);
mapif->disconnectplayer(chr->server[character->server].fd, character->account_id, character->char_id, 2); // 2: Already connected to server
}
//Update state data
character->char_id = char_id;
character->server = map_id;
if( character->server > -1 )
chr->server[character->server].users++;
//Get rid of disconnect timer
if(character->waiting_disconnect != INVALID_TIMER) {
timer->delete(character->waiting_disconnect, chr->waiting_disconnect);
character->waiting_disconnect = INVALID_TIMER;
}
//Set char online in guild cache. If char is in memory, use the guild id on it, otherwise seek it.
cp = (struct mmo_charstatus*)idb_get(chr->char_db_,char_id);
inter_guild->CharOnline(char_id, cp?cp->guild_id:-1);
//Notify login server
if (chr->login_fd > 0 && !sockt->session[chr->login_fd]->flag.eof)
chr->set_account_online(account_id);
}
void char_set_char_offline(int char_id, int account_id)
{
struct online_char_data* character;
if ( char_id == -1 )
{
if( SQL_ERROR == SQL->Query(inter->sql_handle, "UPDATE `%s` SET `online`='0' WHERE `account_id`='%d'", char_db, account_id) )
Sql_ShowDebug(inter->sql_handle);
}
else
{
struct mmo_charstatus* cp = (struct mmo_charstatus*) idb_get(chr->char_db_,char_id);
inter_guild->CharOffline(char_id, cp?cp->guild_id:-1);
if (cp)
idb_remove(chr->char_db_,char_id);
if( SQL_ERROR == SQL->Query(inter->sql_handle, "UPDATE `%s` SET `online`='0' WHERE `char_id`='%d' LIMIT 1", char_db, char_id) )
Sql_ShowDebug(inter->sql_handle);
}
if ((character = (struct online_char_data*)idb_get(chr->online_char_db, account_id)) != NULL) {
//We don't free yet to avoid aCalloc/aFree spamming during char change. [Skotlex]
if( character->server > -1 )
if( chr->server[character->server].users > 0 ) // Prevent this value from going negative.
chr->server[character->server].users--;
if(character->waiting_disconnect != INVALID_TIMER){
timer->delete(character->waiting_disconnect, chr->waiting_disconnect);
character->waiting_disconnect = INVALID_TIMER;
}
if(character->char_id == char_id)
{
character->char_id = -1;
character->server = -1;
character->pincode_enable = -1;
}
//FIXME? Why Kevin free'd the online information when the char was effectively in the map-server?
}
//Remove char if 1- Set all offline, or 2- character is no longer connected to char-server.
if (chr->login_fd > 0 && !sockt->session[chr->login_fd]->flag.eof && (char_id == -1 || character == NULL || character->fd == -1))
chr->set_account_offline(account_id);
}
/**
* @see DBApply
*/
static int char_db_setoffline(union DBKey key, struct DBData *data, va_list ap)
{
struct online_char_data* character = (struct online_char_data*)DB->data2ptr(data);
int server_id = va_arg(ap, int);
nullpo_ret(character);
if (server_id == -1) {
character->char_id = -1;
character->server = -1;
if(character->waiting_disconnect != INVALID_TIMER){
timer->delete(character->waiting_disconnect, chr->waiting_disconnect);
character->waiting_disconnect = INVALID_TIMER;
}
} else if (character->server == server_id)
character->server = -2; //In some map server that we aren't connected to.
return 0;
}
/**
* @see DBApply
*/
static int char_db_kickoffline(union DBKey key, struct DBData *data, va_list ap)
{
struct online_char_data* character = (struct online_char_data*)DB->data2ptr(data);
int server_id = va_arg(ap, int);
nullpo_ret(character);
if (server_id > -1 && character->server != server_id)
return 0;
//Kick out any connected characters, and set them offline as appropriate.
if (character->server > -1 && character->server < MAX_MAP_SERVERS)
mapif->disconnectplayer(chr->server[character->server].fd, character->account_id, character->char_id, 1); // 1: Server closed
else if (character->waiting_disconnect == INVALID_TIMER)
chr->set_char_offline(character->char_id, character->account_id);
else
return 0; // fail
return 1;
}
void char_set_login_all_offline(void)
{
//Tell login-server to also mark all our characters as offline.
WFIFOHEAD(chr->login_fd,2);
WFIFOW(chr->login_fd,0) = 0x2737;
WFIFOSET(chr->login_fd,2);
}
void char_set_all_offline(int id)
{
if (id < 0)
ShowNotice("Sending all users offline.\n");
else
ShowNotice("Sending users of map-server %d offline.\n",id);
chr->online_char_db->foreach(chr->online_char_db,chr->db_kickoffline,id);
if (id >= 0 || chr->login_fd <= 0 || sockt->session[chr->login_fd]->flag.eof)
return;
chr->set_login_all_offline();
}
void char_set_all_offline_sql(void)
{
//Set all players to 'OFFLINE'
if( SQL_ERROR == SQL->Query(inter->sql_handle, "UPDATE `%s` SET `online` = '0'", char_db) )
Sql_ShowDebug(inter->sql_handle);
if( SQL_ERROR == SQL->Query(inter->sql_handle, "UPDATE `%s` SET `online` = '0'", guild_member_db) )
Sql_ShowDebug(inter->sql_handle);
if( SQL_ERROR == SQL->Query(inter->sql_handle, "UPDATE `%s` SET `connect_member` = '0'", guild_db) )
Sql_ShowDebug(inter->sql_handle);
}
/**
* @see DBCreateData
*/
static struct DBData char_create_charstatus(union DBKey key, va_list args)
{
struct mmo_charstatus *cp;
cp = (struct mmo_charstatus *) aCalloc(1,sizeof(struct mmo_charstatus));
cp->char_id = key.i;
return DB->ptr2data(cp);
}
int char_mmo_char_tosql(int char_id, struct mmo_charstatus* p)
{
int i = 0;
int count = 0;
int diff = 0;
char save_status[128]; //For displaying save information. [Skotlex]
struct mmo_charstatus *cp;
int errors = 0; //If there are any errors while saving, "cp" will not be updated at the end.
StringBuf buf;
nullpo_ret(p);
if (char_id != p->char_id) return 0;
cp = idb_ensure(chr->char_db_, char_id, chr->create_charstatus);
StrBuf->Init(&buf);
memset(save_status, 0, sizeof(save_status));
//map inventory data
if( memcmp(p->inventory, cp->inventory, sizeof(p->inventory)) ) {
if (!chr->memitemdata_to_sql(p->inventory, p->char_id, TABLE_INVENTORY))
strcat(save_status, " inventory");
else
errors++;
}
//map cart data
if( memcmp(p->cart, cp->cart, sizeof(p->cart)) ) {
if (!chr->memitemdata_to_sql(p->cart, p->char_id, TABLE_CART))
strcat(save_status, " cart");
else
errors++;
}
if (
(p->base_exp != cp->base_exp) || (p->base_level != cp->base_level) ||
(p->job_level != cp->job_level) || (p->job_exp != cp->job_exp) ||
(p->zeny != cp->zeny) ||
(p->last_point.map != cp->last_point.map) ||
(p->last_point.x != cp->last_point.x) || (p->last_point.y != cp->last_point.y) ||
(p->max_hp != cp->max_hp) || (p->hp != cp->hp) ||
(p->max_sp != cp->max_sp) || (p->sp != cp->sp) ||
(p->status_point != cp->status_point) || (p->skill_point != cp->skill_point) ||
(p->str != cp->str) || (p->agi != cp->agi) || (p->vit != cp->vit) ||
(p->int_ != cp->int_) || (p->dex != cp->dex) || (p->luk != cp->luk) ||
(p->option != cp->option) ||
(p->party_id != cp->party_id) || (p->guild_id != cp->guild_id) ||
(p->pet_id != cp->pet_id) || (p->look.weapon != cp->look.weapon) || (p->hom_id != cp->hom_id) ||
(p->ele_id != cp->ele_id) || (p->look.shield != cp->look.shield) || (p->look.head_top != cp->look.head_top) ||
(p->look.head_mid != cp->look.head_mid) || (p->look.head_bottom != cp->look.head_bottom) || (p->delete_date != cp->delete_date) ||
(p->rename != cp->rename) || (p->slotchange != cp->slotchange) || (p->look.robe != cp->look.robe) ||
(p->show_equip != cp->show_equip) || (p->allow_party != cp->allow_party) || (p->font != cp->font) ||
(p->uniqueitem_counter != cp->uniqueitem_counter) || (p->hotkey_rowshift != cp->hotkey_rowshift) ||
(p->clan_id != cp->clan_id) || (p->last_login != cp->last_login) || (p->attendance_count != cp->attendance_count) ||
(p->attendance_timer != cp->attendance_timer)
) {
//Save status
unsigned int opt = 0;
if( p->allow_party )
opt |= OPT_ALLOW_PARTY;
if( p->show_equip )
opt |= OPT_SHOW_EQUIP;
if( SQL_ERROR == SQL->Query(inter->sql_handle, "UPDATE `%s` SET `base_level`='%d', `job_level`='%d',"
"`base_exp`='%"PRIu64"', `job_exp`='%"PRIu64"', `zeny`='%d',"
"`max_hp`='%d',`hp`='%d',`max_sp`='%d',`sp`='%d',`status_point`='%d',`skill_point`='%d',"
"`str`='%d',`agi`='%d',`vit`='%d',`int`='%d',`dex`='%d',`luk`='%d',"
"`option`='%u',`party_id`='%d',`guild_id`='%d',`pet_id`='%d',`homun_id`='%d',`elemental_id`='%d',"
"`weapon`='%d',`shield`='%d',`head_top`='%d',`head_mid`='%d',`head_bottom`='%d',"
"`last_map`='%s',`last_x`='%d',`last_y`='%d',`save_map`='%s',`save_x`='%d',`save_y`='%d', `rename`='%d',"
"`delete_date`='%lu',`robe`='%d',`slotchange`='%d', `char_opt`='%u', `font`='%u', `uniqueitem_counter` ='%u',"
"`hotkey_rowshift`='%d',`clan_id`='%d',`last_login`='%"PRId64"',`attendance_count`='%d',`attendance_timer`='%"PRId64"'"
" WHERE `account_id`='%d' AND `char_id` = '%d'",
char_db, p->base_level, p->job_level,
p->base_exp, p->job_exp, p->zeny,
p->max_hp, p->hp, p->max_sp, p->sp, p->status_point, p->skill_point,
p->str, p->agi, p->vit, p->int_, p->dex, p->luk,
p->option, p->party_id, p->guild_id, p->pet_id, p->hom_id, p->ele_id,
p->look.weapon, p->look.shield, p->look.head_top, p->look.head_mid, p->look.head_bottom,
mapindex_id2name(p->last_point.map), p->last_point.x, p->last_point.y,
mapindex_id2name(p->save_point.map), p->save_point.x, p->save_point.y, p->rename,
(unsigned long)p->delete_date, // FIXME: platform-dependent size
p->look.robe,p->slotchange,opt,p->font,p->uniqueitem_counter,
p->hotkey_rowshift,p->clan_id,p->last_login, p->attendance_count, p->attendance_timer,
p->account_id, p->char_id) )
{
Sql_ShowDebug(inter->sql_handle);
errors++;
} else
strcat(save_status, " status");
}
if( p->bank_vault != cp->bank_vault || p->mod_exp != cp->mod_exp || p->mod_drop != cp->mod_drop || p->mod_death != cp->mod_death ) {
if( SQL_ERROR == SQL->Query(inter->sql_handle, "REPLACE INTO `%s` (`account_id`,`bank_vault`,`base_exp`,`base_drop`,`base_death`) VALUES ('%d','%d','%d','%d','%d')",account_data_db,p->account_id,p->bank_vault,p->mod_exp,p->mod_drop,p->mod_death) ) {
Sql_ShowDebug(inter->sql_handle);
errors++;
} else
strcat(save_status, " accdata");
}
//Values that will seldom change (to speed up saving)
if (
(p->hair != cp->hair) || (p->hair_color != cp->hair_color) ||
(p->clothes_color != cp->clothes_color) || (p->body != cp->body) ||
(p->class != cp->class) ||
(p->partner_id != cp->partner_id) || (p->father != cp->father) ||
(p->mother != cp->mother) || (p->child != cp->child) ||
(p->karma != cp->karma) || (p->manner != cp->manner) ||
(p->fame != cp->fame)
)
{
if( SQL_ERROR == SQL->Query(inter->sql_handle, "UPDATE `%s` SET `class`='%d',"
"`hair`='%d', `hair_color`='%d', `clothes_color`='%d', `body`='%d',"
"`partner_id`='%d', `father`='%d', `mother`='%d', `child`='%d',"
"`karma`='%d', `manner`='%d', `fame`='%d'"
" WHERE `account_id`='%d' AND `char_id` = '%d'",
char_db, p->class,
p->hair, p->hair_color, p->clothes_color, p->body,
p->partner_id, p->father, p->mother, p->child,
p->karma, p->manner, p->fame,
p->account_id, p->char_id) )
{
Sql_ShowDebug(inter->sql_handle);
errors++;
} else
strcat(save_status, " status2");
}
/* Mercenary Owner */
if( (p->mer_id != cp->mer_id) ||
(p->arch_calls != cp->arch_calls) || (p->arch_faith != cp->arch_faith) ||
(p->spear_calls != cp->spear_calls) || (p->spear_faith != cp->spear_faith) ||
(p->sword_calls != cp->sword_calls) || (p->sword_faith != cp->sword_faith) )
{
if (inter_mercenary->owner_tosql(char_id, p))
strcat(save_status, " mercenary");
else
errors++;
}
//memo points
if( memcmp(p->memo_point, cp->memo_point, sizeof(p->memo_point)) )
{
char esc_mapname[NAME_LENGTH*2+1];
//`memo` (`memo_id`,`char_id`,`map`,`x`,`y`)
if( SQL_ERROR == SQL->Query(inter->sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", memo_db, p->char_id) )
{
Sql_ShowDebug(inter->sql_handle);
errors++;
}
//insert here.
StrBuf->Clear(&buf);
StrBuf->Printf(&buf, "INSERT INTO `%s`(`char_id`,`map`,`x`,`y`) VALUES ", memo_db);
for( i = 0, count = 0; i < MAX_MEMOPOINTS; ++i )
{
if( p->memo_point[i].map )
{
if( count )
StrBuf->AppendStr(&buf, ",");
SQL->EscapeString(inter->sql_handle, esc_mapname, mapindex_id2name(p->memo_point[i].map));
StrBuf->Printf(&buf, "('%d', '%s', '%d', '%d')", char_id, esc_mapname, p->memo_point[i].x, p->memo_point[i].y);
++count;
}
}
if( count )
{
if( SQL_ERROR == SQL->QueryStr(inter->sql_handle, StrBuf->Value(&buf)) )
{
Sql_ShowDebug(inter->sql_handle);
errors++;
}
}
strcat(save_status, " memo");
}
//skills
if( memcmp(p->skill, cp->skill, sizeof(p->skill)) ) {
//`skill` (`char_id`, `id`, `lv`)
if( SQL_ERROR == SQL->Query(inter->sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", skill_db, p->char_id) ) {
Sql_ShowDebug(inter->sql_handle);
errors++;
}
StrBuf->Clear(&buf);
StrBuf->Printf(&buf, "INSERT INTO `%s`(`char_id`,`id`,`lv`,`flag`) VALUES ", skill_db);
//insert here.
for (i = 0, count = 0; i < MAX_SKILL_DB; ++i) {
if( p->skill[i].id != 0 && p->skill[i].flag != SKILL_FLAG_TEMPORARY ) {
if( p->skill[i].lv == 0 && ( p->skill[i].flag == SKILL_FLAG_PERM_GRANTED || p->skill[i].flag == SKILL_FLAG_PERMANENT ) )
continue;
if( p->skill[i].flag != SKILL_FLAG_PERMANENT && p->skill[i].flag != SKILL_FLAG_PERM_GRANTED && (p->skill[i].flag - SKILL_FLAG_REPLACED_LV_0) == 0 )
continue;
if( count )
StrBuf->AppendStr(&buf, ",");
StrBuf->Printf(&buf, "('%d','%d','%d','%d')", char_id, p->skill[i].id,
( (p->skill[i].flag == SKILL_FLAG_PERMANENT || p->skill[i].flag == SKILL_FLAG_PERM_GRANTED) ? p->skill[i].lv : p->skill[i].flag - SKILL_FLAG_REPLACED_LV_0),
p->skill[i].flag == SKILL_FLAG_PERM_GRANTED ? p->skill[i].flag : 0);/* other flags do not need to be saved */
++count;
}
}
if( count )
{
if( SQL_ERROR == SQL->QueryStr(inter->sql_handle, StrBuf->Value(&buf)) )
{
Sql_ShowDebug(inter->sql_handle);
errors++;
}
}
strcat(save_status, " skills");
}
diff = 0;
for(i = 0; i < MAX_FRIENDS; i++){
if(p->friends[i].char_id != cp->friends[i].char_id ||
p->friends[i].account_id != cp->friends[i].account_id){
diff = 1;
break;
}
}
if(diff == 1) {
//Save friends
if( SQL_ERROR == SQL->Query(inter->sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", friend_db, char_id) )
{
Sql_ShowDebug(inter->sql_handle);
errors++;
}
StrBuf->Clear(&buf);
StrBuf->Printf(&buf, "INSERT INTO `%s` (`char_id`, `friend_account`, `friend_id`) VALUES ", friend_db);
for( i = 0, count = 0; i < MAX_FRIENDS; ++i )
{
if( p->friends[i].char_id > 0 )
{
if( count )
StrBuf->AppendStr(&buf, ",");
StrBuf->Printf(&buf, "('%d','%d','%d')", char_id, p->friends[i].account_id, p->friends[i].char_id);
count++;
}
}
if( count )
{
if( SQL_ERROR == SQL->QueryStr(inter->sql_handle, StrBuf->Value(&buf)) )
{
Sql_ShowDebug(inter->sql_handle);
errors++;
}
}
strcat(save_status, " friends");
}
#ifdef HOTKEY_SAVING
// hotkeys
StrBuf->Clear(&buf);
StrBuf->Printf(&buf, "REPLACE INTO `%s` (`char_id`, `hotkey`, `type`, `itemskill_id`, `skill_lvl`) VALUES ", hotkey_db);
diff = 0;
for(i = 0; i < ARRAYLENGTH(p->hotkeys); i++){
if(memcmp(&p->hotkeys[i], &cp->hotkeys[i], sizeof(struct hotkey)))
{
if( diff )
StrBuf->AppendStr(&buf, ",");// not the first hotkey
StrBuf->Printf(&buf, "('%d','%u','%u','%u','%u')", char_id, (unsigned int)i, (unsigned int)p->hotkeys[i].type, p->hotkeys[i].id , (unsigned int)p->hotkeys[i].lv);
diff = 1;
}
}
if(diff) {
if( SQL_ERROR == SQL->QueryStr(inter->sql_handle, StrBuf->Value(&buf)) )
{
Sql_ShowDebug(inter->sql_handle);
errors++;
} else
strcat(save_status, " hotkeys");
}
#endif
StrBuf->Destroy(&buf);
if (chr->show_save_log && save_status[0] != '\0')
ShowInfo("Saved char %d - %s:%s.\n", char_id, p->name, save_status);
if (!errors)
memcpy(cp, p, sizeof(struct mmo_charstatus));
return 0;
}
/**
* Gets a player object's item data from an sql table. [Smokexyz/Hercules]
* @param[in|out] items reference to the item list of a character/account/guild.
* @param[in] max Max amount of items to be pulled into the list.
* @param[in] guid Unique ID of the player object (account_id, char_id, guild_id).
* @param[in] table Table to be used for the transaction.
* @return -1 on failure or number of items added to the list if successful.
*/
int char_getitemdata_from_sql(struct item *items, int max, int guid, enum inventory_table_type table)
{
int i = 0;
struct SqlStmt *stmt = NULL;
const char *tablename = NULL;
const char *selectoption = NULL;
bool has_favorite = false;
StringBuf buf;
struct item item = { 0 }; // temp storage variable
nullpo_retr(-1, items);
Assert_retr(-1, guid > 0);
Assert_retr(-1, max > 0);
// Initialize the array.
memset(items, 0x0, sizeof(struct item) * max);
switch (table) {
case TABLE_INVENTORY:
tablename = inventory_db;
selectoption = "char_id";
has_favorite = true;
break;
case TABLE_CART:
tablename = cart_db;
selectoption = "char_id";
break;
case TABLE_GUILD_STORAGE:
tablename = guild_storage_db;
selectoption = "guild_id";
break;
default:
ShowError("char_getitemdata_from_sql: Invalid table type %d!\n", (int) table);
Assert_retr(-1, table);
return -1;
}
StrBuf->Init(&buf);
StrBuf->AppendStr(&buf, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `expire_time`, `bound`, `unique_id`");
for(i = 0; i < MAX_SLOTS; i++)
StrBuf->Printf(&buf, ", `card%d`", i);
for(i = 0; i < MAX_ITEM_OPTIONS; i++)
StrBuf->Printf(&buf, ", `opt_idx%d`, `opt_val%d`", i, i);
if (has_favorite)
StrBuf->AppendStr(&buf, ", `favorite`");
StrBuf->Printf(&buf, " FROM `%s` WHERE `%s`=?", tablename, selectoption);
stmt = SQL->StmtMalloc(inter->sql_handle);
if (SQL_ERROR == SQL->StmtPrepareStr(stmt, StrBuf->Value(&buf))
|| SQL_ERROR == SQL->StmtBindParam(stmt, 0, SQLDT_INT, &guid, sizeof guid)
|| SQL_ERROR == SQL->StmtExecute(stmt)) {
SqlStmt_ShowDebug(stmt);
SQL->StmtFree(stmt);
StrBuf->Destroy(&buf);
return -1;
}
if (SQL_ERROR == SQL->StmtBindColumn(stmt, 0, SQLDT_INT, &item.id, sizeof item.id, NULL, NULL)
|| SQL_ERROR == SQL->StmtBindColumn(stmt, 1, SQLDT_SHORT, &item.nameid, sizeof item.nameid, NULL, NULL)
|| SQL_ERROR == SQL->StmtBindColumn(stmt, 2, SQLDT_SHORT, &item.amount, sizeof item.amount, NULL, NULL)
|| SQL_ERROR == SQL->StmtBindColumn(stmt, 3, SQLDT_UINT, &item.equip, sizeof item.equip, NULL, NULL)
|| SQL_ERROR == SQL->StmtBindColumn(stmt, 4, SQLDT_CHAR, &item.identify, sizeof item.identify, NULL, NULL)
|| SQL_ERROR == SQL->StmtBindColumn(stmt, 5, SQLDT_CHAR, &item.refine, sizeof item.refine, NULL, NULL)
|| SQL_ERROR == SQL->StmtBindColumn(stmt, 6, SQLDT_CHAR, &item.attribute, sizeof item.attribute, NULL, NULL)
|| SQL_ERROR == SQL->StmtBindColumn(stmt, 7, SQLDT_UINT, &item.expire_time, sizeof item.expire_time, NULL, NULL)
|| SQL_ERROR == SQL->StmtBindColumn(stmt, 8, SQLDT_UCHAR, &item.bound, sizeof item.bound, NULL, NULL)
|| SQL_ERROR == SQL->StmtBindColumn(stmt, 9, SQLDT_UINT64, &item.unique_id, sizeof item.unique_id, NULL, NULL)
) {
SqlStmt_ShowDebug(stmt);
}
for (i = 0; i < MAX_SLOTS; i++) {
if (SQL_ERROR == SQL->StmtBindColumn(stmt, 10 + i, SQLDT_SHORT, &item.card[i], sizeof item.card[i], NULL, NULL))
SqlStmt_ShowDebug(stmt);
}
for (i = 0; i < MAX_ITEM_OPTIONS; i++) {
if (SQL_ERROR == SQL->StmtBindColumn(stmt, 10 + MAX_SLOTS + i * 2, SQLDT_INT16, &item.option[i].index, sizeof item.option[i].index, NULL, NULL)
|| SQL_ERROR == SQL->StmtBindColumn(stmt, 11 + MAX_SLOTS + i * 2, SQLDT_INT16, &item.option[i].value, sizeof item.option[i].index, NULL, NULL))
SqlStmt_ShowDebug(stmt);
}
if (has_favorite) {
if (SQL_ERROR == SQL->StmtBindColumn(stmt, 10 + MAX_SLOTS + MAX_ITEM_OPTIONS * 2, SQLDT_CHAR, &item.favorite, sizeof item.favorite, NULL, NULL))
SqlStmt_ShowDebug(stmt);
}
if (SQL->StmtNumRows(stmt) > 0 ) {
i = 0;
while (SQL_SUCCESS == SQL->StmtNextRow(stmt) && i < max) {
items[i++] = item;
}
}
SQL->StmtFree(stmt);
StrBuf->Destroy(&buf);
return i;
}
/**
* Saves an array of 'item' entries into the specified table. [Smokexyz/Hercules]
* @param[in] items The items array.
* @param[in] guid The character/account/guild ID (depending on table).
* @param[in] tableswitch The type of table (@see enum inventory_table_type).
* @retval -1 in case of failure, or number of changes made within the table.
*/
int char_memitemdata_to_sql(const struct item *p_items, int guid, enum inventory_table_type table)
{
StringBuf buf;
int i = 0, j = 0;
const char *tablename = NULL;
const char *selectoption = NULL;
bool has_favorite = false;
struct item *cp_items = NULL; // temp item storage variable
bool *matched_p = NULL;
int total_updates = 0, total_deletes = 0, total_inserts = 0, total_changes = 0;
int item_count = 0, db_max = 0;
nullpo_ret(p_items);
switch (table) {
case TABLE_INVENTORY:
tablename = inventory_db;
selectoption = "char_id";
has_favorite = true;
item_count = MAX_INVENTORY;
break;
case TABLE_CART:
tablename = cart_db;
selectoption = "char_id";
item_count = MAX_CART;
break;
case TABLE_GUILD_STORAGE:
tablename = guild_storage_db;
selectoption = "guild_id";
item_count = MAX_GUILD_STORAGE;
break;
default:
ShowError("Invalid table type %d!\n", (int) table);
Assert_retr(-1, table);
return -1;
}
cp_items = aCalloc(item_count, sizeof(struct item));
matched_p = aCalloc(item_count, sizeof(bool));
StrBuf->Init(&buf);
/**
* If the storage table is not empty, check for items and replace or delete where needed.
*/
if ((db_max = chr->getitemdata_from_sql(cp_items, item_count, guid, table)) > 0) {
int *deletes = aCalloc(db_max, sizeof(struct item));
for (i = 0; i < db_max; i++) {
struct item *cp_it = &cp_items[i];
ARR_FIND(0, item_count, j,
matched_p[j] != true
&& p_items[j].nameid != 0
&& cp_it->nameid == p_items[j].nameid
&& cp_it->unique_id == p_items[j].unique_id
&& memcmp(p_items[j].card, cp_it->card, sizeof(short) * MAX_SLOTS) == 0
&& memcmp(p_items[j].option, cp_it->option, 5 * MAX_ITEM_OPTIONS) == 0);
if (j < item_count) { // Item found.
matched_p[j] = true; // Mark the item as matched.
// If the amount has changed, set for replacement with current item properties.
if (memcmp(cp_it, &p_items[j], sizeof(struct item)) != 0) {
int k = 0;
if (total_updates == 0) {
StrBuf->Clear(&buf);
StrBuf->Printf(&buf, "REPLACE INTO `%s` (`id`, `%s`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`", tablename, selectoption);
for (k = 0; k < MAX_SLOTS; k++)
StrBuf->Printf(&buf, ", `card%d`", k);
for (k = 0; k < MAX_ITEM_OPTIONS; k++)
StrBuf->Printf(&buf, ", `opt_idx%d`, `opt_val%d`", k, k);
StrBuf->AppendStr(&buf, ", `expire_time`, `bound`, `unique_id`");
if (has_favorite)
StrBuf->AppendStr(&buf, ", `favorite`");
StrBuf->AppendStr(&buf, ") VALUES ");
}
StrBuf->Printf(&buf, "%s('%d', '%d', '%d', '%d', '%u', '%d', '%d', '%d'",
total_updates > 0 ? ", " : "", cp_it->id, guid, p_items[j].nameid, p_items[j].amount, p_items[j].equip, p_items[j].identify, p_items[j].refine, p_items[j].attribute);
for (k = 0; k < MAX_SLOTS; k++)
StrBuf->Printf(&buf, ", '%d'", p_items[j].card[k]);
for (k = 0; k < MAX_ITEM_OPTIONS; ++k)
StrBuf->Printf(&buf, ", '%d', '%d'", p_items[j].option[k].index, p_items[j].option[k].value);
StrBuf->Printf(&buf, ", '%u', '%d', '%"PRIu64"'", p_items[j].expire_time, p_items[j].bound, p_items[j].unique_id);
if (has_favorite)
StrBuf->Printf(&buf, ", %d", p_items[j].favorite);
StrBuf->AppendStr(&buf, ")");
total_updates++;
}
} else { // Doesn't exist in the table, set for deletion.
deletes[total_deletes++] = cp_it->id;
}
}
if (total_updates > 0 && SQL_ERROR == SQL->QueryStr(inter->sql_handle, StrBuf->Value(&buf)))
Sql_ShowDebug(inter->sql_handle);
/**
* Handle deletions, if any.
*/
if (total_deletes > 0) {
StrBuf->Clear(&buf);
StrBuf->Printf(&buf, "DELETE FROM `%s` WHERE `id` IN (", tablename);
for (i = 0; i < total_deletes; i++)
StrBuf->Printf(&buf, "%s'%d'", i == 0 ? "" : ", ", deletes[i]);
StrBuf->AppendStr(&buf, ");");
if (SQL_ERROR == SQL->QueryStr(inter->sql_handle, StrBuf->Value(&buf)))
Sql_ShowDebug(inter->sql_handle);
}
aFree(deletes);
}
/**
* Check for new items and add if required.
*/
for (i = 0; i < item_count; i++) {
const struct item *p_it = &p_items[i];
if (matched_p[i] || p_it->nameid == 0)
continue;
if (total_inserts == 0) {
StrBuf->Clear(&buf);
StrBuf->Printf(&buf, "INSERT INTO `%s` (`%s`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `expire_time`, `bound`, `unique_id`", tablename, selectoption);
for (j = 0; j < MAX_SLOTS; ++j)
StrBuf->Printf(&buf, ", `card%d`", j);
for (j = 0; j < MAX_ITEM_OPTIONS; ++j)
StrBuf->Printf(&buf, ", `opt_idx%d`, `opt_val%d`", j, j);
if (has_favorite)
StrBuf->AppendStr(&buf, ", `favorite`");
StrBuf->AppendStr(&buf, ") VALUES ");
}
StrBuf->Printf(&buf, "%s('%d', '%d', '%d', '%u', '%d', '%d', '%d', '%u', '%d', '%"PRIu64"'",
total_inserts > 0 ? ", " : "", guid, p_it->nameid, p_it->amount, p_it->equip, p_it->identify, p_it->refine,
p_it->attribute, p_it->expire_time, p_it->bound, p_it->unique_id);
for (j = 0; j < MAX_SLOTS; ++j)
StrBuf->Printf(&buf, ", '%d'", p_it->card[j]);
for (j = 0; j < MAX_ITEM_OPTIONS; ++j)
StrBuf->Printf(&buf, ", '%d', '%d'", p_it->option[j].index, p_it->option[j].value);
if (has_favorite)
StrBuf->Printf(&buf, ", '%d'", p_it->favorite);
StrBuf->AppendStr(&buf, ")");
total_inserts++;
}
if (total_inserts > 0 && SQL_ERROR == SQL->QueryStr(inter->sql_handle, StrBuf->Value(&buf)))
Sql_ShowDebug(inter->sql_handle);
StrBuf->Destroy(&buf);
aFree(cp_items);
aFree(matched_p);
ShowInfo("%s save complete - guid: %d (replace: %d, insert: %d, delete: %d)\n", tablename, guid, total_updates, total_inserts, total_deletes);
return total_changes;
}
/**
* Returns the correct gender ID for the given character and enum value.
*
* If the per-character sex is defined but not supported by the current packetver, the database entries are corrected.
*
* @param sd Character data, if available.
* @param p Character status.
* @param sex Character sex (database enum)
*
* @retval SEX_MALE if the per-character sex is male