/
int_guild.c
1860 lines (1612 loc) · 55.4 KB
/
int_guild.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
// Copyright (c) Hercules Dev Team, licensed under GNU GPL.
// See the LICENSE file
// Portions Copyright (c) Athena Dev Teams
#include "../common/cbasetypes.h"
#include "../common/mmo.h"
#include "../common/malloc.h"
#include "../common/socket.h"
#include "../common/db.h"
#include "../common/showmsg.h"
#include "../common/strlib.h"
#include "../common/timer.h"
#include "char.h"
#include "inter.h"
#include "int_guild.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define GS_MEMBER_UNMODIFIED 0x00
#define GS_MEMBER_MODIFIED 0x01
#define GS_MEMBER_NEW 0x02
#define GS_POSITION_UNMODIFIED 0x00
#define GS_POSITION_MODIFIED 0x01
// LSB = 0 => Alliance, LSB = 1 => Opposition
#define GUILD_ALLIANCE_TYPE_MASK 0x01
#define GUILD_ALLIANCE_REMOVE 0x08
static const char dataToHex[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
//Guild cache
static DBMap* guild_db_; // int guild_id -> struct guild*
static DBMap *castle_db;
static unsigned int guild_exp[MAX_GUILDLEVEL];
int mapif_parse_GuildLeave(int fd,int guild_id,int account_id,int char_id,int flag,const char *mes);
int mapif_guild_broken(int guild_id,int flag);
static bool guild_check_empty(struct guild *g);
int guild_calcinfo(struct guild *g);
int mapif_guild_basicinfochanged(int guild_id,int type,const void *data,int len);
int mapif_guild_info(int fd,struct guild *g);
int inter_guild_tosql(struct guild *g,int flag);
static int guild_save_timer(int tid, int64 tick, int id, intptr_t data) {
static int last_id = 0; //To know in which guild we were.
int state = 0; //0: Have not reached last guild. 1: Reached last guild, ready for save. 2: Some guild saved, don't do further saving.
DBIterator *iter = db_iterator(guild_db_);
DBKey key;
struct guild* g;
if( last_id == 0 ) //Save the first guild in the list.
state = 1;
for( g = DB->data2ptr(iter->first(iter, &key)); dbi_exists(iter); g = DB->data2ptr(iter->next(iter, &key)) )
{
if( state == 0 && g->guild_id == last_id )
state++; //Save next guild in the list.
else
if( state == 1 && g->save_flag&GS_MASK )
{
inter_guild_tosql(g, g->save_flag&GS_MASK);
g->save_flag &= ~GS_MASK;
//Some guild saved.
last_id = g->guild_id;
state++;
}
if( g->save_flag == GS_REMOVE )
{// Nothing to save, guild is ready for removal.
if (save_log)
ShowInfo("Guild Unloaded (%d - %s)\n", g->guild_id, g->name);
db_remove(guild_db_, key);
}
}
dbi_destroy(iter);
if( state != 2 ) //Reached the end of the guild db without saving.
last_id = 0; //Reset guild saved, return to beginning.
state = guild_db_->size(guild_db_);
if( state < 1 ) state = 1; //Calculate the time slot for the next save.
timer->add(tick + autosave_interval/state, guild_save_timer, 0, 0);
return 0;
}
int inter_guild_removemember_tosql(int account_id, int char_id)
{
if( SQL_ERROR == SQL->Query(sql_handle, "DELETE from `%s` where `account_id` = '%d' and `char_id` = '%d'", guild_member_db, account_id, char_id) )
Sql_ShowDebug(sql_handle);
if( SQL_ERROR == SQL->Query(sql_handle, "UPDATE `%s` SET `guild_id` = '0' WHERE `char_id` = '%d'", char_db, char_id) )
Sql_ShowDebug(sql_handle);
return 0;
}
// Save guild into sql
int inter_guild_tosql(struct guild *g,int flag)
{
// Table guild (GS_BASIC_MASK)
// GS_EMBLEM `emblem_len`,`emblem_id`,`emblem_data`
// GS_CONNECT `connect_member`,`average_lv`
// GS_MES `mes1`,`mes2`
// GS_LEVEL `guild_lv`,`max_member`,`exp`,`next_exp`,`skill_point`
// GS_BASIC `name`,`master`,`char_id`
// GS_MEMBER `guild_member` (`guild_id`,`account_id`,`char_id`,`hair`,`hair_color`,`gender`,`class`,`lv`,`exp`,`exp_payper`,`online`,`position`,`name`)
// GS_POSITION `guild_position` (`guild_id`,`position`,`name`,`mode`,`exp_mode`)
// GS_ALLIANCE `guild_alliance` (`guild_id`,`opposition`,`alliance_id`,`name`)
// GS_EXPULSION `guild_expulsion` (`guild_id`,`account_id`,`name`,`mes`)
// GS_SKILL `guild_skill` (`guild_id`,`id`,`lv`)
// temporary storage for str convertion. They must be twice the size of the
// original string to ensure no overflows will occur. [Skotlex]
char t_info[256];
char esc_name[NAME_LENGTH*2+1];
char esc_master[NAME_LENGTH*2+1];
char new_guild = 0;
int i=0;
if (g->guild_id<=0 && g->guild_id != -1) return 0;
#ifdef NOISY
ShowInfo("Save guild request ("CL_BOLD"%d"CL_RESET" - flag 0x%x).",g->guild_id, flag);
#endif
SQL->EscapeStringLen(sql_handle, esc_name, g->name, strnlen(g->name, NAME_LENGTH));
SQL->EscapeStringLen(sql_handle, esc_master, g->master, strnlen(g->master, NAME_LENGTH));
*t_info = '\0';
// Insert a new guild the guild
if (flag&GS_BASIC && g->guild_id == -1)
{
strcat(t_info, " guild_create");
// Create a new guild
if( SQL_ERROR == SQL->Query(sql_handle, "INSERT INTO `%s` "
"(`name`,`master`,`guild_lv`,`max_member`,`average_lv`,`char_id`) "
"VALUES ('%s', '%s', '%d', '%d', '%d', '%d')",
guild_db, esc_name, esc_master, g->guild_lv, g->max_member, g->average_lv, g->member[0].char_id) )
{
Sql_ShowDebug(sql_handle);
if (g->guild_id == -1)
return 0; //Failed to create guild!
}
else
{
g->guild_id = (int)SQL->LastInsertId(sql_handle);
new_guild = 1;
}
}
// If we need an update on an existing guild or more update on the new guild
if (((flag & GS_BASIC_MASK) && !new_guild) || ((flag & (GS_BASIC_MASK & ~GS_BASIC)) && new_guild))
{
StringBuf buf;
bool add_comma = false;
StrBuf->Init(&buf);
StrBuf->Printf(&buf, "UPDATE `%s` SET ", guild_db);
if (flag & GS_EMBLEM)
{
char emblem_data[sizeof(g->emblem_data)*2+1];
char* pData = emblem_data;
strcat(t_info, " emblem");
// Convert emblem_data to hex
//TODO: why not use binary directly? [ultramage]
for(i=0; i<g->emblem_len; i++){
*pData++ = dataToHex[(g->emblem_data[i] >> 4) & 0x0F];
*pData++ = dataToHex[g->emblem_data[i] & 0x0F];
}
*pData = 0;
StrBuf->Printf(&buf, "`emblem_len`=%d, `emblem_id`=%d, `emblem_data`='%s'", g->emblem_len, g->emblem_id, emblem_data);
add_comma = true;
}
if (flag & GS_BASIC)
{
strcat(t_info, " basic");
if( add_comma )
StrBuf->AppendStr(&buf, ", ");
else
add_comma = true;
StrBuf->Printf(&buf, "`name`='%s', `master`='%s', `char_id`=%d", esc_name, esc_master, g->member[0].char_id);
}
if (flag & GS_CONNECT)
{
strcat(t_info, " connect");
if( add_comma )
StrBuf->AppendStr(&buf, ", ");
else
add_comma = true;
StrBuf->Printf(&buf, "`connect_member`=%d, `average_lv`=%d", g->connect_member, g->average_lv);
}
if (flag & GS_MES)
{
char esc_mes1[sizeof(g->mes1)*2+1];
char esc_mes2[sizeof(g->mes2)*2+1];
strcat(t_info, " mes");
if( add_comma )
StrBuf->AppendStr(&buf, ", ");
else
add_comma = true;
SQL->EscapeStringLen(sql_handle, esc_mes1, g->mes1, strnlen(g->mes1, sizeof(g->mes1)));
SQL->EscapeStringLen(sql_handle, esc_mes2, g->mes2, strnlen(g->mes2, sizeof(g->mes2)));
StrBuf->Printf(&buf, "`mes1`='%s', `mes2`='%s'", esc_mes1, esc_mes2);
}
if (flag & GS_LEVEL)
{
strcat(t_info, " level");
if( add_comma )
StrBuf->AppendStr(&buf, ", ");
//else //last condition using add_coma setting
// add_comma = true;
StrBuf->Printf(&buf, "`guild_lv`=%d, `skill_point`=%d, `exp`=%"PRIu64", `next_exp`=%u, `max_member`=%d", g->guild_lv, g->skill_point, g->exp, g->next_exp, g->max_member);
}
StrBuf->Printf(&buf, " WHERE `guild_id`=%d", g->guild_id);
if( SQL_ERROR == SQL->Query(sql_handle, "%s", StrBuf->Value(&buf)) )
Sql_ShowDebug(sql_handle);
StrBuf->Destroy(&buf);
}
if (flag&GS_MEMBER)
{
struct guild_member *m;
strcat(t_info, " members");
// Update only needed players
for(i=0;i<g->max_member;i++){
m = &g->member[i];
if (!m->modified)
continue;
if(m->account_id) {
//Since nothing references guild member table as foreign keys, it's safe to use REPLACE INTO
SQL->EscapeStringLen(sql_handle, esc_name, m->name, strnlen(m->name, NAME_LENGTH));
if( SQL_ERROR == SQL->Query(sql_handle, "REPLACE INTO `%s` (`guild_id`,`account_id`,`char_id`,`hair`,`hair_color`,`gender`,`class`,`lv`,`exp`,`exp_payper`,`online`,`position`,`name`) "
"VALUES ('%d','%d','%d','%d','%d','%d','%d','%d','%"PRIu64"','%d','%d','%d','%s')",
guild_member_db, g->guild_id, m->account_id, m->char_id,
m->hair, m->hair_color, m->gender,
m->class_, m->lv, m->exp, m->exp_payper, m->online, m->position, esc_name) )
Sql_ShowDebug(sql_handle);
if (m->modified&GS_MEMBER_NEW || new_guild == 1)
{
if( SQL_ERROR == SQL->Query(sql_handle, "UPDATE `%s` SET `guild_id` = '%d' WHERE `char_id` = '%d'",
char_db, g->guild_id, m->char_id) )
Sql_ShowDebug(sql_handle);
}
m->modified = GS_MEMBER_UNMODIFIED;
}
}
}
if (flag&GS_POSITION){
strcat(t_info, " positions");
//printf("- Insert guild %d to guild_position\n",g->guild_id);
for(i=0;i<MAX_GUILDPOSITION;i++){
struct guild_position *p = &g->position[i];
if (!p->modified)
continue;
SQL->EscapeStringLen(sql_handle, esc_name, p->name, strnlen(p->name, NAME_LENGTH));
if( SQL_ERROR == SQL->Query(sql_handle, "REPLACE INTO `%s` (`guild_id`,`position`,`name`,`mode`,`exp_mode`) VALUES ('%d','%d','%s','%d','%d')",
guild_position_db, g->guild_id, i, esc_name, p->mode, p->exp_mode) )
Sql_ShowDebug(sql_handle);
p->modified = GS_POSITION_UNMODIFIED;
}
}
if (flag&GS_ALLIANCE)
{
// Delete current alliances
// NOTE: no need to do it on both sides since both guilds in memory had
// their info changed, not to mention this would also mess up oppositions!
// [Skotlex]
//if( SQL_ERROR == SQL->Query(sql_handle, "DELETE FROM `%s` WHERE `guild_id`='%d' OR `alliance_id`='%d'", guild_alliance_db, g->guild_id, g->guild_id) )
if( SQL_ERROR == SQL->Query(sql_handle, "DELETE FROM `%s` WHERE `guild_id`='%d'", guild_alliance_db, g->guild_id) )
{
Sql_ShowDebug(sql_handle);
}
else
{
//printf("- Insert guild %d to guild_alliance\n",g->guild_id);
for(i=0;i<MAX_GUILDALLIANCE;i++)
{
struct guild_alliance *a=&g->alliance[i];
if(a->guild_id>0)
{
SQL->EscapeStringLen(sql_handle, esc_name, a->name, strnlen(a->name, NAME_LENGTH));
if( SQL_ERROR == SQL->Query(sql_handle, "REPLACE INTO `%s` (`guild_id`,`opposition`,`alliance_id`,`name`) "
"VALUES ('%d','%d','%d','%s')",
guild_alliance_db, g->guild_id, a->opposition, a->guild_id, esc_name) )
Sql_ShowDebug(sql_handle);
}
}
}
}
if (flag&GS_EXPULSION){
strcat(t_info, " expulsions");
//printf("- Insert guild %d to guild_expulsion\n",g->guild_id);
for(i=0;i<MAX_GUILDEXPULSION;i++){
struct guild_expulsion *e=&g->expulsion[i];
if(e->account_id>0){
char esc_mes[sizeof(e->mes)*2+1];
SQL->EscapeStringLen(sql_handle, esc_name, e->name, strnlen(e->name, NAME_LENGTH));
SQL->EscapeStringLen(sql_handle, esc_mes, e->mes, strnlen(e->mes, sizeof(e->mes)));
if( SQL_ERROR == SQL->Query(sql_handle, "REPLACE INTO `%s` (`guild_id`,`account_id`,`name`,`mes`) "
"VALUES ('%d','%d','%s','%s')", guild_expulsion_db, g->guild_id, e->account_id, esc_name, esc_mes) )
Sql_ShowDebug(sql_handle);
}
}
}
if (flag&GS_SKILL){
strcat(t_info, " skills");
//printf("- Insert guild %d to guild_skill\n",g->guild_id);
for(i=0;i<MAX_GUILDSKILL;i++){
if (g->skill[i].id>0 && g->skill[i].lv>0){
if( SQL_ERROR == SQL->Query(sql_handle, "REPLACE INTO `%s` (`guild_id`,`id`,`lv`) VALUES ('%d','%d','%d')",
guild_skill_db, g->guild_id, g->skill[i].id, g->skill[i].lv) )
Sql_ShowDebug(sql_handle);
}
}
}
if (save_log)
ShowInfo("Saved guild (%d - %s):%s\n",g->guild_id,g->name,t_info);
return 1;
}
// Read guild from sql
struct guild * inter_guild_fromsql(int guild_id)
{
struct guild *g;
char* data;
size_t len;
char* p;
int i;
if( guild_id <= 0 )
return NULL;
g = (struct guild*)idb_get(guild_db_, guild_id);
if( g )
return g;
#ifdef NOISY
ShowInfo("Guild load request (%d)...\n", guild_id);
#endif
if( SQL_ERROR == SQL->Query(sql_handle, "SELECT g.`name`,c.`name`,g.`guild_lv`,g.`connect_member`,g.`max_member`,g.`average_lv`,g.`exp`,g.`next_exp`,g.`skill_point`,g.`mes1`,g.`mes2`,g.`emblem_len`,g.`emblem_id`,g.`emblem_data` "
"FROM `%s` g LEFT JOIN `%s` c ON c.`char_id` = g.`char_id` WHERE g.`guild_id`='%d'", guild_db, char_db, guild_id) )
{
Sql_ShowDebug(sql_handle);
return NULL;
}
if( SQL_SUCCESS != SQL->NextRow(sql_handle) )
return NULL;// Guild does not exists.
CREATE(g, struct guild, 1);
g->guild_id = guild_id;
SQL->GetData(sql_handle, 0, &data, &len); memcpy(g->name, data, min(len, NAME_LENGTH));
SQL->GetData(sql_handle, 1, &data, &len); memcpy(g->master, data, min(len, NAME_LENGTH));
SQL->GetData(sql_handle, 2, &data, NULL); g->guild_lv = atoi(data);
SQL->GetData(sql_handle, 3, &data, NULL); g->connect_member = atoi(data);
SQL->GetData(sql_handle, 4, &data, NULL); g->max_member = atoi(data);
if( g->max_member > MAX_GUILD )
{ // Fix reduction of MAX_GUILD [PoW]
ShowWarning("Guild %d:%s specifies higher capacity (%d) than MAX_GUILD (%d)\n", guild_id, g->name, g->max_member, MAX_GUILD);
g->max_member = MAX_GUILD;
}
SQL->GetData(sql_handle, 5, &data, NULL); g->average_lv = atoi(data);
SQL->GetData(sql_handle, 6, &data, NULL); g->exp = strtoull(data, NULL, 10);
SQL->GetData(sql_handle, 7, &data, NULL); g->next_exp = (unsigned int)strtoul(data, NULL, 10);
SQL->GetData(sql_handle, 8, &data, NULL); g->skill_point = atoi(data);
SQL->GetData(sql_handle, 9, &data, &len); memcpy(g->mes1, data, min(len, sizeof(g->mes1)));
SQL->GetData(sql_handle, 10, &data, &len); memcpy(g->mes2, data, min(len, sizeof(g->mes2)));
SQL->GetData(sql_handle, 11, &data, &len); g->emblem_len = atoi(data);
SQL->GetData(sql_handle, 12, &data, &len); g->emblem_id = atoi(data);
SQL->GetData(sql_handle, 13, &data, &len);
// convert emblem data from hexadecimal to binary
//TODO: why not store it in the db as binary directly? [ultramage]
for( i = 0, p = g->emblem_data; i < g->emblem_len; ++i, ++p )
{
if( *data >= '0' && *data <= '9' )
*p = *data - '0';
else if( *data >= 'a' && *data <= 'f' )
*p = *data - 'a' + 10;
else if( *data >= 'A' && *data <= 'F' )
*p = *data - 'A' + 10;
*p <<= 4;
++data;
if( *data >= '0' && *data <= '9' )
*p |= *data - '0';
else if( *data >= 'a' && *data <= 'f' )
*p |= *data - 'a' + 10;
else if( *data >= 'A' && *data <= 'F' )
*p |= *data - 'A' + 10;
++data;
}
// load guild member info
if( SQL_ERROR == SQL->Query(sql_handle, "SELECT `account_id`,`char_id`,`hair`,`hair_color`,`gender`,`class`,`lv`,`exp`,`exp_payper`,`online`,`position`,`name` "
"FROM `%s` WHERE `guild_id`='%d' ORDER BY `position`", guild_member_db, guild_id) )
{
Sql_ShowDebug(sql_handle);
aFree(g);
return NULL;
}
for( i = 0; i < g->max_member && SQL_SUCCESS == SQL->NextRow(sql_handle); ++i )
{
struct guild_member* m = &g->member[i];
SQL->GetData(sql_handle, 0, &data, NULL); m->account_id = atoi(data);
SQL->GetData(sql_handle, 1, &data, NULL); m->char_id = atoi(data);
SQL->GetData(sql_handle, 2, &data, NULL); m->hair = atoi(data);
SQL->GetData(sql_handle, 3, &data, NULL); m->hair_color = atoi(data);
SQL->GetData(sql_handle, 4, &data, NULL); m->gender = atoi(data);
SQL->GetData(sql_handle, 5, &data, NULL); m->class_ = atoi(data);
SQL->GetData(sql_handle, 6, &data, NULL); m->lv = atoi(data);
SQL->GetData(sql_handle, 7, &data, NULL); m->exp = strtoull(data, NULL, 10);
SQL->GetData(sql_handle, 8, &data, NULL); m->exp_payper = (unsigned int)atoi(data);
SQL->GetData(sql_handle, 9, &data, NULL); m->online = atoi(data);
SQL->GetData(sql_handle, 10, &data, NULL); m->position = atoi(data);
if( m->position >= MAX_GUILDPOSITION ) // Fix reduction of MAX_GUILDPOSITION [PoW]
m->position = MAX_GUILDPOSITION - 1;
SQL->GetData(sql_handle, 11, &data, &len); memcpy(m->name, data, min(len, NAME_LENGTH));
m->modified = GS_MEMBER_UNMODIFIED;
}
//printf("- Read guild_position %d from sql \n",guild_id);
if( SQL_ERROR == SQL->Query(sql_handle, "SELECT `position`,`name`,`mode`,`exp_mode` FROM `%s` WHERE `guild_id`='%d'", guild_position_db, guild_id) )
{
Sql_ShowDebug(sql_handle);
aFree(g);
return NULL;
}
while( SQL_SUCCESS == SQL->NextRow(sql_handle) )
{
int position;
struct guild_position *pos;
SQL->GetData(sql_handle, 0, &data, NULL); position = atoi(data);
if( position < 0 || position >= MAX_GUILDPOSITION )
continue;// invalid position
pos = &g->position[position];
SQL->GetData(sql_handle, 1, &data, &len); memcpy(pos->name, data, min(len, NAME_LENGTH));
SQL->GetData(sql_handle, 2, &data, NULL); pos->mode = atoi(data);
SQL->GetData(sql_handle, 3, &data, NULL); pos->exp_mode = atoi(data);
pos->modified = GS_POSITION_UNMODIFIED;
}
//printf("- Read guild_alliance %d from sql \n",guild_id);
if( SQL_ERROR == SQL->Query(sql_handle, "SELECT `opposition`,`alliance_id`,`name` FROM `%s` WHERE `guild_id`='%d'", guild_alliance_db, guild_id) )
{
Sql_ShowDebug(sql_handle);
aFree(g);
return NULL;
}
for( i = 0; i < MAX_GUILDALLIANCE && SQL_SUCCESS == SQL->NextRow(sql_handle); ++i )
{
struct guild_alliance* a = &g->alliance[i];
SQL->GetData(sql_handle, 0, &data, NULL); a->opposition = atoi(data);
SQL->GetData(sql_handle, 1, &data, NULL); a->guild_id = atoi(data);
SQL->GetData(sql_handle, 2, &data, &len); memcpy(a->name, data, min(len, NAME_LENGTH));
}
//printf("- Read guild_expulsion %d from sql \n",guild_id);
if( SQL_ERROR == SQL->Query(sql_handle, "SELECT `account_id`,`name`,`mes` FROM `%s` WHERE `guild_id`='%d'", guild_expulsion_db, guild_id) )
{
Sql_ShowDebug(sql_handle);
aFree(g);
return NULL;
}
for( i = 0; i < MAX_GUILDEXPULSION && SQL_SUCCESS == SQL->NextRow(sql_handle); ++i )
{
struct guild_expulsion *e = &g->expulsion[i];
SQL->GetData(sql_handle, 0, &data, NULL); e->account_id = atoi(data);
SQL->GetData(sql_handle, 1, &data, &len); memcpy(e->name, data, min(len, NAME_LENGTH));
SQL->GetData(sql_handle, 2, &data, &len); memcpy(e->mes, data, min(len, sizeof(e->mes)));
}
//printf("- Read guild_skill %d from sql \n",guild_id);
if( SQL_ERROR == SQL->Query(sql_handle, "SELECT `id`,`lv` FROM `%s` WHERE `guild_id`='%d' ORDER BY `id`", guild_skill_db, guild_id) )
{
Sql_ShowDebug(sql_handle);
aFree(g);
return NULL;
}
for(i = 0; i < MAX_GUILDSKILL; i++)
{ //Skill IDs must always be initialized. [Skotlex]
g->skill[i].id = i + GD_SKILLBASE;
}
while( SQL_SUCCESS == SQL->NextRow(sql_handle) )
{
int id;
SQL->GetData(sql_handle, 0, &data, NULL); id = atoi(data) - GD_SKILLBASE;
if( id < 0 || id >= MAX_GUILDSKILL )
continue;// invalid guild skill
SQL->GetData(sql_handle, 1, &data, NULL); g->skill[id].lv = atoi(data);
}
SQL->FreeResult(sql_handle);
idb_put(guild_db_, guild_id, g); //Add to cache
g->save_flag |= GS_REMOVE; //But set it to be removed, in case it is not needed for long.
if (save_log)
ShowInfo("Guild loaded (%d - %s)\n", guild_id, g->name);
return g;
}
// `guild_castle` (`castle_id`, `guild_id`, `economy`, `defense`, `triggerE`, `triggerD`, `nextTime`, `payTime`, `createTime`, `visibleC`, `visibleG0`, `visibleG1`, `visibleG2`, `visibleG3`, `visibleG4`, `visibleG5`, `visibleG6`, `visibleG7`)
int inter_guildcastle_tosql(struct guild_castle *gc)
{
StringBuf buf;
int i;
StrBuf->Init(&buf);
StrBuf->Printf(&buf, "REPLACE INTO `%s` SET `castle_id`='%d', `guild_id`='%d', `economy`='%d', `defense`='%d', "
"`triggerE`='%d', `triggerD`='%d', `nextTime`='%d', `payTime`='%d', `createTime`='%d', `visibleC`='%d'",
guild_castle_db, gc->castle_id, gc->guild_id, gc->economy, gc->defense,
gc->triggerE, gc->triggerD, gc->nextTime, gc->payTime, gc->createTime, gc->visibleC);
for (i = 0; i < MAX_GUARDIANS; ++i)
StrBuf->Printf(&buf, ", `visibleG%d`='%d'", i, gc->guardian[i].visible);
if (SQL_ERROR == SQL->Query(sql_handle, StrBuf->Value(&buf)))
Sql_ShowDebug(sql_handle);
else if(save_log)
ShowInfo("Saved guild castle (%d)\n", gc->castle_id);
StrBuf->Destroy(&buf);
return 0;
}
// Read guild_castle from SQL
static struct guild_castle* inter_guildcastle_fromsql(int castle_id)
{
char *data;
int i;
StringBuf buf;
struct guild_castle *gc = idb_get(castle_db, castle_id);
if (gc != NULL)
return gc;
StrBuf->Init(&buf);
StrBuf->AppendStr(&buf, "SELECT `castle_id`, `guild_id`, `economy`, `defense`, `triggerE`, "
"`triggerD`, `nextTime`, `payTime`, `createTime`, `visibleC`");
for (i = 0; i < MAX_GUARDIANS; ++i)
StrBuf->Printf(&buf, ", `visibleG%d`", i);
StrBuf->Printf(&buf, " FROM `%s` WHERE `castle_id`='%d'", guild_castle_db, castle_id);
if (SQL_ERROR == SQL->Query(sql_handle, StrBuf->Value(&buf))) {
Sql_ShowDebug(sql_handle);
StrBuf->Destroy(&buf);
return NULL;
}
StrBuf->Destroy(&buf);
CREATE(gc, struct guild_castle, 1);
gc->castle_id = castle_id;
if (SQL_SUCCESS == SQL->NextRow(sql_handle)) {
SQL->GetData(sql_handle, 1, &data, NULL); gc->guild_id = atoi(data);
SQL->GetData(sql_handle, 2, &data, NULL); gc->economy = atoi(data);
SQL->GetData(sql_handle, 3, &data, NULL); gc->defense = atoi(data);
SQL->GetData(sql_handle, 4, &data, NULL); gc->triggerE = atoi(data);
SQL->GetData(sql_handle, 5, &data, NULL); gc->triggerD = atoi(data);
SQL->GetData(sql_handle, 6, &data, NULL); gc->nextTime = atoi(data);
SQL->GetData(sql_handle, 7, &data, NULL); gc->payTime = atoi(data);
SQL->GetData(sql_handle, 8, &data, NULL); gc->createTime = atoi(data);
SQL->GetData(sql_handle, 9, &data, NULL); gc->visibleC = atoi(data);
for (i = 10; i < 10+MAX_GUARDIANS; i++) {
SQL->GetData(sql_handle, i, &data, NULL); gc->guardian[i-10].visible = atoi(data);
}
}
SQL->FreeResult(sql_handle);
idb_put(castle_db, castle_id, gc);
if (save_log)
ShowInfo("Loaded guild castle (%d - guild %d)\n", castle_id, gc->guild_id);
return gc;
}
// Read exp_guild.txt
static bool exp_guild_parse_row(char* split[], int column, int current) {
int64 exp = strtoll(split[0], NULL, 10);
if (exp < 0 || exp >= UINT_MAX) {
ShowError("exp_guild: Invalid exp %"PRId64" (valid range: 0 - %u) at line %d\n", exp, UINT_MAX, current);
return false;
}
guild_exp[current] = (unsigned int)exp;
return true;
}
int inter_guild_CharOnline(int char_id, int guild_id)
{
struct guild *g;
int i;
if (guild_id == -1) {
//Get guild_id from the database
if( SQL_ERROR == SQL->Query(sql_handle, "SELECT guild_id FROM `%s` WHERE char_id='%d'", char_db, char_id) )
{
Sql_ShowDebug(sql_handle);
return 0;
}
if( SQL_SUCCESS == SQL->NextRow(sql_handle) )
{
char* data;
SQL->GetData(sql_handle, 0, &data, NULL);
guild_id = atoi(data);
}
else
{
guild_id = 0;
}
SQL->FreeResult(sql_handle);
}
if (guild_id == 0)
return 0; //No guild...
g = inter_guild_fromsql(guild_id);
if(!g) {
ShowError("Character %d's guild %d not found!\n", char_id, guild_id);
return 0;
}
//Member has logged in before saving, tell saver not to delete
if(g->save_flag & GS_REMOVE)
g->save_flag &= ~GS_REMOVE;
//Set member online
ARR_FIND( 0, g->max_member, i, g->member[i].char_id == char_id );
if( i < g->max_member )
{
g->member[i].online = 1;
g->member[i].modified = GS_MEMBER_MODIFIED;
}
return 1;
}
int inter_guild_CharOffline(int char_id, int guild_id)
{
struct guild *g=NULL;
int online_count, i;
if (guild_id == -1)
{
//Get guild_id from the database
if( SQL_ERROR == SQL->Query(sql_handle, "SELECT guild_id FROM `%s` WHERE char_id='%d'", char_db, char_id) )
{
Sql_ShowDebug(sql_handle);
return 0;
}
if( SQL_SUCCESS == SQL->NextRow(sql_handle) )
{
char* data;
SQL->GetData(sql_handle, 0, &data, NULL);
guild_id = atoi(data);
}
else
{
guild_id = 0;
}
SQL->FreeResult(sql_handle);
}
if (guild_id == 0)
return 0; //No guild...
//Character has a guild, set character offline and check if they were the only member online
g = inter_guild_fromsql(guild_id);
if (g == NULL) //Guild not found?
return 0;
//Set member offline
ARR_FIND( 0, g->max_member, i, g->member[i].char_id == char_id );
if( i < g->max_member )
{
g->member[i].online = 0;
g->member[i].modified = GS_MEMBER_MODIFIED;
}
online_count = 0;
for( i = 0; i < g->max_member; i++ )
if( g->member[i].online )
online_count++;
// Remove guild from memory if no players online
if( online_count == 0 )
g->save_flag |= GS_REMOVE;
return 1;
}
// Initialize guild sql
int inter_guild_sql_init(void)
{
//Initialize the guild cache
guild_db_= idb_alloc(DB_OPT_RELEASE_DATA);
castle_db = idb_alloc(DB_OPT_RELEASE_DATA);
//Read exp file
sv->readdb("db", DBPATH"exp_guild.txt", ',', 1, 1, MAX_GUILDLEVEL, exp_guild_parse_row);
timer->add_func_list(guild_save_timer, "guild_save_timer");
timer->add(timer->gettick() + 10000, guild_save_timer, 0, 0);
return 0;
}
/**
* @see DBApply
*/
static int guild_db_final(DBKey key, DBData *data, va_list ap)
{
struct guild *g = DB->data2ptr(data);
if (g->save_flag&GS_MASK) {
inter_guild_tosql(g, g->save_flag&GS_MASK);
return 1;
}
return 0;
}
void inter_guild_sql_final(void)
{
guild_db_->destroy(guild_db_, guild_db_final);
db_destroy(castle_db);
return;
}
// Get guild_id by its name. Returns 0 if not found, -1 on error.
int search_guildname(char *str)
{
int guild_id;
char esc_name[NAME_LENGTH*2+1];
SQL->EscapeStringLen(sql_handle, esc_name, str, safestrnlen(str, NAME_LENGTH));
//Lookup guilds with the same name
if( SQL_ERROR == SQL->Query(sql_handle, "SELECT guild_id FROM `%s` WHERE name='%s'", guild_db, esc_name) )
{
Sql_ShowDebug(sql_handle);
return -1;
}
if( SQL_SUCCESS == SQL->NextRow(sql_handle) )
{
char* data;
SQL->GetData(sql_handle, 0, &data, NULL);
guild_id = atoi(data);
}
else
{
guild_id = 0;
}
SQL->FreeResult(sql_handle);
return guild_id;
}
// Check if guild is empty
static bool guild_check_empty(struct guild *g)
{
int i;
ARR_FIND( 0, g->max_member, i, g->member[i].account_id > 0 );
if( i < g->max_member)
return false; // not empty
//Let the calling function handle the guild removal in case they need
//to do something else with it before freeing the data. [Skotlex]
return true;
}
unsigned int guild_nextexp(int level) {
if (level == 0)
return 1;
if (level <= 0 || level >= MAX_GUILDLEVEL)
return 0;
return guild_exp[level-1];
}
int guild_checkskill(struct guild *g,int id)
{
int idx = id - GD_SKILLBASE;
if(idx < 0 || idx >= MAX_GUILDSKILL)
return 0;
return g->skill[idx].lv;
}
int guild_calcinfo(struct guild *g)
{
int i,c;
unsigned int nextexp;
struct guild before = *g; // Save guild current values
if(g->guild_lv<=0)
g->guild_lv = 1;
nextexp = guild_nextexp(g->guild_lv);
// Consume guild exp and increase guild level
while(g->exp >= nextexp && nextexp > 0) { // nextexp would be 0 if g->guild_lv was >= MAX_GUILDLEVEL
g->exp-=nextexp;
g->guild_lv++;
g->skill_point++;
nextexp = guild_nextexp(g->guild_lv);
}
// Save next exp step
g->next_exp = nextexp;
// Set the max number of members, Guild Extention skill - currently adds 6 to max per skill lv.
g->max_member = 16 + guild_checkskill(g, GD_EXTENSION) * 6;
if(g->max_member > MAX_GUILD)
{
ShowError("Guild %d:%s has capacity for too many guild members (%d), max supported is %d\n", g->guild_id, g->name, g->max_member, MAX_GUILD);
g->max_member = MAX_GUILD;
}
// Compute the guild average level level
g->average_lv=0;
g->connect_member=0;
for(i=c=0;i<g->max_member;i++)
{
if(g->member[i].account_id>0)
{
if (g->member[i].lv >= 0)
{
g->average_lv+=g->member[i].lv;
c++;
}
else
{
ShowWarning("Guild %d:%s, member %d:%s has an invalid level %d\n", g->guild_id, g->name, g->member[i].char_id, g->member[i].name, g->member[i].lv);
}
if(g->member[i].online)
g->connect_member++;
}
}
if(c)
g->average_lv /= c;
// Check if guild stats has change
if(g->max_member != before.max_member || g->guild_lv != before.guild_lv || g->skill_point != before.skill_point )
{
g->save_flag |= GS_LEVEL;
mapif_guild_info(-1,g);
return 1;
}
return 0;
}
//-------------------------------------------------------------------
// Packet sent to map server
int mapif_guild_created(int fd,int account_id,struct guild *g)
{
WFIFOHEAD(fd, 10);
WFIFOW(fd,0)=0x3830;
WFIFOL(fd,2)=account_id;
if(g != NULL)
{
WFIFOL(fd,6)=g->guild_id;
ShowInfo("int_guild: Guild created (%d - %s)\n",g->guild_id,g->name);
} else
WFIFOL(fd,6)=0;
WFIFOSET(fd,10);
return 0;
}
// Guild not found
int mapif_guild_noinfo(int fd,int guild_id)
{
unsigned char buf[12];
WBUFW(buf,0)=0x3831;
WBUFW(buf,2)=8;
WBUFL(buf,4)=guild_id;
ShowWarning("int_guild: info not found %d\n",guild_id);
if(fd<0)
mapif_sendall(buf,8);
else
mapif_send(fd,buf,8);
return 0;
}
// Send guild info
int mapif_guild_info(int fd,struct guild *g)
{
unsigned char buf[8+sizeof(struct guild)];
WBUFW(buf,0)=0x3831;
WBUFW(buf,2)=4+sizeof(struct guild);
memcpy(buf+4,g,sizeof(struct guild));
if(fd<0)
mapif_sendall(buf,WBUFW(buf,2));
else
mapif_send(fd,buf,WBUFW(buf,2));
return 0;
}
// ACK member add
int mapif_guild_memberadded(int fd,int guild_id,int account_id,int char_id,int flag)
{
WFIFOHEAD(fd, 15);
WFIFOW(fd,0)=0x3832;
WFIFOL(fd,2)=guild_id;
WFIFOL(fd,6)=account_id;
WFIFOL(fd,10)=char_id;
WFIFOB(fd,14)=flag;
WFIFOSET(fd,15);
return 0;
}
// ACK member leave
int mapif_guild_withdraw(int guild_id,int account_id,int char_id,int flag, const char *name, const char *mes)
{
unsigned char buf[55+NAME_LENGTH];
WBUFW(buf, 0)=0x3834;
WBUFL(buf, 2)=guild_id;
WBUFL(buf, 6)=account_id;
WBUFL(buf,10)=char_id;
WBUFB(buf,14)=flag;
memcpy(WBUFP(buf,15),mes,40);
memcpy(WBUFP(buf,55),name,NAME_LENGTH);
mapif_sendall(buf,55+NAME_LENGTH);
ShowInfo("int_guild: guild withdraw (%d - %d: %s - %s)\n",guild_id,account_id,name,mes);
return 0;
}
// Send short member's info
int mapif_guild_memberinfoshort(struct guild *g,int idx)
{
unsigned char buf[19];
WBUFW(buf, 0)=0x3835;
WBUFL(buf, 2)=g->guild_id;
WBUFL(buf, 6)=g->member[idx].account_id;
WBUFL(buf,10)=g->member[idx].char_id;
WBUFB(buf,14)=(unsigned char)g->member[idx].online;
WBUFW(buf,15)=g->member[idx].lv;
WBUFW(buf,17)=g->member[idx].class_;
mapif_sendall(buf,19);
return 0;
}
// Send guild broken
int mapif_guild_broken(int guild_id,int flag)
{
unsigned char buf[7];
WBUFW(buf,0)=0x3836;
WBUFL(buf,2)=guild_id;
WBUFB(buf,6)=flag;
mapif_sendall(buf,7);
ShowInfo("int_guild: Guild broken (%d)\n",guild_id);
return 0;
}
// Send guild message
int mapif_guild_message(int guild_id,int account_id,char *mes,int len, int sfd)
{
unsigned char buf[512];
if (len > 500)
len = 500;
WBUFW(buf,0)=0x3837;
WBUFW(buf,2)=len+12;
WBUFL(buf,4)=guild_id;
WBUFL(buf,8)=account_id;
memcpy(WBUFP(buf,12),mes,len);
mapif_sendallwos(sfd, buf,len+12);
return 0;
}
// Send basic info
int mapif_guild_basicinfochanged(int guild_id,int type,const void *data,int len)
{