summaryrefslogtreecommitdiff
path: root/src/char_sql/int_storage.c
blob: 7c8c8ce4ae78938c4fb43981c50a5848ae4d25d2 (plain) (blame)
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
//
// original code from athena
// SQL conversion by Jioh L. Jung
//
#include "char.h"
#include "itemdb.h"
#include <string.h>
#include <stdlib.h>

#define STORAGE_MEMINC	16

// reset by inter_config_read()
struct storage *storage_pt=NULL;
struct guild_storage *guild_storage_pt=NULL;


// storage data -> DB conversion
int storage_tosql(int account_id,struct storage *p){
	int i;
	int eqcount=1;
	int noteqcount=1;
	struct itemtemp mapitem;
	for(i=0;i<MAX_STORAGE;i++){
		if(p->storage[i].nameid>0){
			if(itemdb_isequip(p->storage[i].nameid)==1){				
				mapitem.equip[eqcount].flag=0;
				mapitem.equip[eqcount].id = p->storage[i].id;
				mapitem.equip[eqcount].nameid=p->storage[i].nameid;
				mapitem.equip[eqcount].amount = p->storage[i].amount;
				mapitem.equip[eqcount].equip = p->storage[i].equip;
				mapitem.equip[eqcount].identify = p->storage[i].identify;
				mapitem.equip[eqcount].refine = p->storage[i].refine;
				mapitem.equip[eqcount].attribute = p->storage[i].attribute;
				mapitem.equip[eqcount].card[0] = p->storage[i].card[0];
				mapitem.equip[eqcount].card[1] = p->storage[i].card[1];
				mapitem.equip[eqcount].card[2] = p->storage[i].card[2];
				mapitem.equip[eqcount].card[3] = p->storage[i].card[3];
				eqcount++;
			}
			else if(itemdb_isequip(p->storage[i].nameid)==0){				
				mapitem.notequip[noteqcount].flag=0;
				mapitem.notequip[noteqcount].id = p->storage[i].id;
				mapitem.notequip[noteqcount].nameid=p->storage[i].nameid;
				mapitem.notequip[noteqcount].amount = p->storage[i].amount;
				mapitem.notequip[noteqcount].equip = p->storage[i].equip;
				mapitem.notequip[noteqcount].identify = p->storage[i].identify;
				mapitem.notequip[noteqcount].refine = p->storage[i].refine;
				mapitem.notequip[noteqcount].attribute = p->storage[i].attribute;
				mapitem.notequip[noteqcount].card[0] = p->storage[i].card[0];
				mapitem.notequip[noteqcount].card[1] = p->storage[i].card[1];
				mapitem.notequip[noteqcount].card[2] = p->storage[i].card[2];
				mapitem.notequip[noteqcount].card[3] = p->storage[i].card[3];
				noteqcount++;
			}
		}
	}

	memitemdata_to_sql(mapitem, eqcount, noteqcount, account_id,TABLE_STORAGE);

	//printf ("storage dump to DB - id: %d (total: %d)\n", account_id, j);
	return 0;
}

// DB -> storage data conversion
int storage_fromsql(int account_id, struct storage *p){
	int i=0;
	
	memset(p,0,sizeof(struct storage)); //clean up memory
	p->storage_amount = 0;
	p->account_id = account_id;
	
	// storage {`account_id`/`id`/`nameid`/`amount`/`equip`/`identify`/`refine`/`attribute`/`card0`/`card1`/`card2`/`card3`}
	sprintf(tmp_sql,"SELECT `id`,`nameid`,`amount`,`equip`,`identify`,`refine`,`attribute`,`card0`,`card1`,`card2`,`card3` FROM `%s` WHERE `account_id`='%d'",storage_db, account_id);
	if(mysql_query(&mysql_handle, tmp_sql) ) {
			printf("DB server Error - %s\n", mysql_error(&mysql_handle) );
	}
	sql_res = mysql_store_result(&mysql_handle) ;
	
	if (sql_res) {
		while((sql_row = mysql_fetch_row(sql_res))) {	//start to fetch
			p->storage[i].id= atoi(sql_row[0]);
			p->storage[i].nameid= atoi(sql_row[1]);
			p->storage[i].amount= atoi(sql_row[2]);
			p->storage[i].equip= atoi(sql_row[3]);
			p->storage[i].identify= atoi(sql_row[4]);
			p->storage[i].refine= atoi(sql_row[5]);
			p->storage[i].attribute= atoi(sql_row[6]);
			p->storage[i].card[0]= atoi(sql_row[7]);
			p->storage[i].card[1]= atoi(sql_row[8]);
			p->storage[i].card[2]= atoi(sql_row[9]);
			p->storage[i].card[3]= atoi(sql_row[10]);
			p->storage_amount = ++i;
		}
		mysql_free_result(sql_res);
	}
	
	printf ("storage load complete from DB - id: %d (total: %d)\n", account_id, p->storage_amount);
	return 1;
}

// Save guild_storage data to sql
int guild_storage_tosql(int guild_id, struct guild_storage *p){
	int i;
	int eqcount=1;
	int noteqcount=1;
	struct itemtemp mapitem;
	for(i=0;i<MAX_GUILD_STORAGE;i++){
		if(p->storage[i].nameid>0){
			if(itemdb_isequip(p->storage[i].nameid)==1){				
				mapitem.equip[eqcount].flag=0;
				mapitem.equip[eqcount].id = p->storage[i].id;
				mapitem.equip[eqcount].nameid=p->storage[i].nameid;
				mapitem.equip[eqcount].amount = p->storage[i].amount;
				mapitem.equip[eqcount].equip = p->storage[i].equip;
				mapitem.equip[eqcount].identify = p->storage[i].identify;
				mapitem.equip[eqcount].refine = p->storage[i].refine;
				mapitem.equip[eqcount].attribute = p->storage[i].attribute;
				mapitem.equip[eqcount].card[0] = p->storage[i].card[0];
				mapitem.equip[eqcount].card[1] = p->storage[i].card[1];
				mapitem.equip[eqcount].card[2] = p->storage[i].card[2];
				mapitem.equip[eqcount].card[3] = p->storage[i].card[3];
				eqcount++;
			}
			else if(itemdb_isequip(p->storage[i].nameid)==0){				
				mapitem.notequip[noteqcount].flag=0;
				mapitem.notequip[noteqcount].id = p->storage[i].id;
				mapitem.notequip[noteqcount].nameid=p->storage[i].nameid;
				mapitem.notequip[noteqcount].amount = p->storage[i].amount;
				mapitem.notequip[noteqcount].equip = p->storage[i].equip;
				mapitem.notequip[noteqcount].identify = p->storage[i].identify;
				mapitem.notequip[noteqcount].refine = p->storage[i].refine;
				mapitem.notequip[noteqcount].attribute = p->storage[i].attribute;
				mapitem.notequip[noteqcount].card[0] = p->storage[i].card[0];
				mapitem.notequip[noteqcount].card[1] = p->storage[i].card[1];
				mapitem.notequip[noteqcount].card[2] = p->storage[i].card[2];
				mapitem.notequip[noteqcount].card[3] = p->storage[i].card[3];
				noteqcount++;
			}
		}
	}

	memitemdata_to_sql(mapitem, eqcount, noteqcount, guild_id,TABLE_GUILD_STORAGE);

	printf ("guild storage save to DB - id: %d (total: %d)\n", guild_id,i);
	return 0;
}

// Load guild_storage data to mem
int guild_storage_fromsql(int guild_id, struct guild_storage *p){
	int i=0;
	struct guild_storage *gs=guild_storage_pt;
	p=gs;

	memset(p,0,sizeof(struct guild_storage)); //clean up memory
	p->storage_amount = 0;
	p->guild_id = guild_id;

	// storage {`guild_id`/`id`/`nameid`/`amount`/`equip`/`identify`/`refine`/`attribute`/`card0`/`card1`/`card2`/`card3`}
	sprintf(tmp_sql,"SELECT `id`,`nameid`,`amount`,`equip`,`identify`,`refine`,`attribute`,`card0`,`card1`,`card2`,`card3` FROM `%s` WHERE `guild_id`='%d'",guild_storage_db, guild_id);
	if(mysql_query(&mysql_handle, tmp_sql) ) {
		printf("DB server Error - %s\n", mysql_error(&mysql_handle) );
	}
	sql_res = mysql_store_result(&mysql_handle) ;
	
	if (sql_res) {
		while((sql_row = mysql_fetch_row(sql_res))) {	//start to fetch
			p->storage[i].id= atoi(sql_row[0]);
			p->storage[i].nameid= atoi(sql_row[1]);
			p->storage[i].amount= atoi(sql_row[2]);
			p->storage[i].equip= atoi(sql_row[3]);
			p->storage[i].identify= atoi(sql_row[4]);
			p->storage[i].refine= atoi(sql_row[5]);
			p->storage[i].attribute= atoi(sql_row[6]);
			p->storage[i].card[0]= atoi(sql_row[7]);
			p->storage[i].card[1]= atoi(sql_row[8]);
			p->storage[i].card[2]= atoi(sql_row[9]);
			p->storage[i].card[3]= atoi(sql_row[10]);
			p->storage_amount = ++i;
		}
		mysql_free_result(sql_res);
	}
	printf ("guild storage load complete from DB - id: %d (total: %d)\n", guild_id, p->storage_amount);
	return 0;
}

//---------------------------------------------------------
// storage data initialize
int inter_storage_sql_init(){
	
	//memory alloc
	printf("interserver storage memory initialize....(%d byte)\n",sizeof(struct storage));
	storage_pt=calloc(sizeof(struct storage), 1);
	guild_storage_pt=calloc(sizeof(struct guild_storage), 1);
	memset(storage_pt,0,sizeof(struct storage));
	memset(guild_storage_pt,0,sizeof(struct guild_storage));
	
	return 1;
}
// �q�Ƀf�[�^�폜
int inter_storage_delete(int account_id)
{
		sprintf(tmp_sql, "DELETE FROM `%s` WHERE `account_id`='%d'",storage_db, account_id);
	if(mysql_query(&mysql_handle, tmp_sql) ) {
		printf("DB server Error (delete `storage`)- %s\n", mysql_error(&mysql_handle) );
	}
	return 0;
}
int inter_guild_storage_delete(int guild_id)
{
	sprintf(tmp_sql, "DELETE FROM `%s` WHERE `guild_id`='%d'",guild_storage_db, guild_id);
	if(mysql_query(&mysql_handle, tmp_sql) ) {
		printf("DB server Error (delete `guild_storage`)- %s\n", mysql_error(&mysql_handle) );
	}
	return 0;
}

//---------------------------------------------------------
// packet from map server

// recive packet about storage data
int mapif_load_storage(int fd,int account_id){
	//load from DB
	storage_fromsql(account_id, storage_pt);
	WFIFOW(fd,0)=0x3810;
	WFIFOW(fd,2)=sizeof(struct storage)+8;
	WFIFOL(fd,4)=account_id;
	memcpy(WFIFOP(fd,8),storage_pt,sizeof(struct storage));
	WFIFOSET(fd,WFIFOW(fd,2));
	return 0;
}
// send ack to map server which is "storage data save ok."
int mapif_save_storage_ack(int fd,int account_id){
	WFIFOW(fd,0)=0x3811;
	WFIFOL(fd,2)=account_id;
	WFIFOB(fd,6)=0;
	WFIFOSET(fd,7);
	return 0;
}

int mapif_load_guild_storage(int fd,int account_id,int guild_id)
{
	int guild_exist=0;
	WFIFOW(fd,0)=0x3818;
	
	// Check if guild exists, I may write a function for this later, coz I use it several times.
	//printf("- Check if guild %d exists\n",g->guild_id);
	sprintf(tmp_sql, "SELECT count(*) FROM `%s` WHERE `guild_id`='%d'",guild_db, guild_id);
	if(mysql_query(&mysql_handle, tmp_sql) ) {
		printf("DB server Error (delete `guild`)- %s\n", mysql_error(&mysql_handle) );
	}
	sql_res = mysql_store_result(&mysql_handle) ;
	if (sql_res!=NULL && mysql_num_rows(sql_res)>0) {
		sql_row = mysql_fetch_row(sql_res);
		guild_exist =  atoi (sql_row[0]);
		//printf("- Check if guild %d exists : %s\n",g->guild_id,((guild_exist==0)?"No":"Yes"));
	}
	mysql_free_result(sql_res) ; //resource free
	
	if(guild_exist==1) {
		guild_storage_fromsql(guild_id,guild_storage_pt);
		WFIFOW(fd,2)=sizeof(struct guild_storage)+12;
		WFIFOL(fd,4)=account_id;
		WFIFOL(fd,8)=guild_id;
		memcpy(WFIFOP(fd,12),guild_storage_pt,sizeof(struct guild_storage));
	}
	else {
		WFIFOW(fd,2)=12;
		WFIFOL(fd,4)=account_id;
		WFIFOL(fd,8)=0;
	}
	WFIFOSET(fd,WFIFOW(fd,2));

	return 0;
}
int mapif_save_guild_storage_ack(int fd,int account_id,int guild_id,int fail)
{
	WFIFOW(fd,0)=0x3819;
	WFIFOL(fd,2)=account_id;
	WFIFOL(fd,6)=guild_id;
	WFIFOB(fd,10)=fail;
	WFIFOSET(fd,11);
	return 0;
}

//---------------------------------------------------------
// packet from map server

// recive request about storage data
int mapif_parse_LoadStorage(int fd){
	mapif_load_storage(fd,RFIFOL(fd,2));
	return 0;
}
// storage data recive and save
int mapif_parse_SaveStorage(int fd){
	int account_id=RFIFOL(fd,4);
	int len=RFIFOW(fd,2);
	
	if(sizeof(struct storage)!=len-8){
		printf("inter storage: data size error %d %d\n",sizeof(struct storage),len-8);
	}else{
		memcpy(&storage_pt[0],RFIFOP(fd,8),sizeof(struct storage));
		storage_tosql(account_id, storage_pt);
		mapif_save_storage_ack(fd,account_id);
	}
	return 0;
}

int mapif_parse_LoadGuildStorage(int fd)
{
	mapif_load_guild_storage(fd,RFIFOL(fd,2),RFIFOL(fd,6));
	return 0;
}

int mapif_parse_SaveGuildStorage(int fd)
{
	int guild_exist=0;
	int guild_id=RFIFOL(fd,8);
	int len=RFIFOW(fd,2);
	if(sizeof(struct guild_storage)!=len-12){
		printf("inter storage: data size error %d %d\n",sizeof(struct guild_storage),len-12);
	}
	else {
		// Check if guild exists, I may write a function for this later, coz I use it several times.
		//printf("- Check if guild %d exists\n",g->guild_id);
		sprintf(tmp_sql, "SELECT count(*) FROM `%s` WHERE `guild_id`='%d'",guild_db, guild_id);
		if(mysql_query(&mysql_handle, tmp_sql) ) {
			printf("DB server Error (delete `guild`)- %s\n", mysql_error(&mysql_handle) );
		}
		sql_res = mysql_store_result(&mysql_handle) ;
		if (sql_res!=NULL && mysql_num_rows(sql_res)>0) {
			sql_row = mysql_fetch_row(sql_res);
			guild_exist =  atoi (sql_row[0]);
			//printf("- Check if guild %d exists : %s\n",g->guild_id,((guild_exist==0)?"No":"Yes"));
		}
		mysql_free_result(sql_res) ; //resource free
		
		if(guild_exist==1) {
			memcpy(guild_storage_pt,RFIFOP(fd,12),sizeof(struct guild_storage));
			guild_storage_tosql(guild_id,guild_storage_pt);
			mapif_save_guild_storage_ack(fd,RFIFOL(fd,4),guild_id,0);
		}
		else
			mapif_save_guild_storage_ack(fd,RFIFOL(fd,4),guild_id,1);
	}
	return 0;
}


int inter_storage_parse_frommap(int fd){
	switch(RFIFOW(fd,0)){
	case 0x3010: mapif_parse_LoadStorage(fd); break;
	case 0x3011: mapif_parse_SaveStorage(fd); break;
	case 0x3018: mapif_parse_LoadGuildStorage(fd); break;
	case 0x3019: mapif_parse_SaveGuildStorage(fd); break;
	default:
		return 0;
	}
	return 1;
}