root/src/char_sql/int_storage.c @ 1

Revision 1, 9.4 kB (checked in by jinshiro, 17 years ago)
RevLine 
[1]1// Copyright (c) Athena Dev Teams - Licensed under GNU GPL
2// For more information, see LICENCE in the main folder
3
4#include "../common/mmo.h"
5#include "../common/malloc.h"
6#include "../common/showmsg.h"
7#include "../common/socket.h"
8#include "../common/strlib.h" // StringBuf
9#include "../common/sql.h"
10#include "char.h"
11#include "inter.h"
12
13#include <stdio.h>
14#include <string.h>
15#include <stdlib.h>
16
17
18#define STORAGE_MEMINC  16
19
20#ifndef TXT_SQL_CONVERT
21// reset by inter_config_read()
22struct storage *storage_pt=NULL;
23struct guild_storage *guild_storage_pt=NULL;
24#endif //TXT_SQL_CONVERT
25
26/// Save guild_storage data to sql
27int storage_tosql(int account_id, struct storage* p)
28{
29        memitemdata_to_sql(p->storage_, MAX_STORAGE, account_id, TABLE_STORAGE);
30        //ShowInfo ("storage save to DB - account: %d\n", account_id);
31        return 0;
32}
33
34#ifndef TXT_SQL_CONVERT
35/// Load guild_storage data to mem
36int storage_fromsql(int account_id, struct storage* p)
37{
38        StringBuf buf;
39        struct item* item;
40        char* data;
41        int i;
42        int j;
43
44        memset(p, 0, sizeof(struct storage)); //clean up memory
45        p->storage_amount = 0;
46        p->account_id = account_id;
47
48        // storage {`account_id`/`id`/`nameid`/`amount`/`equip`/`identify`/`refine`/`attribute`/`card0`/`card1`/`card2`/`card3`}
49        StringBuf_Init(&buf);
50        StringBuf_AppendStr(&buf, "SELECT `id`,`nameid`,`amount`,`equip`,`identify`,`refine`,`attribute`");
51        for( j = 0; j < MAX_SLOTS; ++j )
52                StringBuf_Printf(&buf, ",`card%d`", j);
53        StringBuf_Printf(&buf, " FROM `%s` WHERE `account_id`='%d' ORDER BY `nameid`", storage_db, account_id);
54
55        if( SQL_ERROR == Sql_Query(sql_handle, StringBuf_Value(&buf)) )
56                Sql_ShowDebug(sql_handle);
57
58        StringBuf_Destroy(&buf);
59
60        for( i = 0; i < MAX_STORAGE && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
61        {
62                item = &p->storage_[i];
63                Sql_GetData(sql_handle, 0, &data, NULL); item->id = atoi(data);
64                Sql_GetData(sql_handle, 1, &data, NULL); item->nameid = atoi(data);
65                Sql_GetData(sql_handle, 2, &data, NULL); item->amount = atoi(data);
66                Sql_GetData(sql_handle, 3, &data, NULL); item->equip = atoi(data);
67                Sql_GetData(sql_handle, 4, &data, NULL); item->identify = atoi(data);
68                Sql_GetData(sql_handle, 5, &data, NULL); item->refine = atoi(data);
69                Sql_GetData(sql_handle, 6, &data, NULL); item->attribute = atoi(data);
70                for( j = 0; j < MAX_SLOTS; ++j )
71                {
72                        Sql_GetData(sql_handle, 7+j, &data, NULL); item->card[j] = atoi(data);
73                }
74        }
75        p->storage_amount = i;
76        Sql_FreeResult(sql_handle);
77
78        ShowInfo("storage load complete from DB - id: %d (total: %d)\n", account_id, p->storage_amount);
79        return 1;
80}
81#endif //TXT_SQL_CONVERT
82
83/// Save guild_storage data to sql
84int guild_storage_tosql(int guild_id, struct guild_storage* p)
85{
86        memitemdata_to_sql(p->storage_, MAX_GUILD_STORAGE, guild_id, TABLE_GUILD_STORAGE);
87        ShowInfo ("guild storage save to DB - guild: %d\n", guild_id);
88        return 0;
89}
90
91#ifndef TXT_SQL_CONVERT
92/// Load guild_storage data to mem
93int guild_storage_fromsql(int guild_id, struct guild_storage* p)
94{
95        StringBuf buf;
96        struct item* item;
97        char* data;
98        int i;
99        int j;
100
101        memset(p, 0, sizeof(struct guild_storage)); //clean up memory
102        p->storage_amount = 0;
103        p->guild_id = guild_id;
104
105        // storage {`guild_id`/`id`/`nameid`/`amount`/`equip`/`identify`/`refine`/`attribute`/`card0`/`card1`/`card2`/`card3`}
106        StringBuf_Init(&buf);
107        StringBuf_AppendStr(&buf, "SELECT `id`,`nameid`,`amount`,`equip`,`identify`,`refine`,`attribute`");
108        for( j = 0; j < MAX_SLOTS; ++j )
109                StringBuf_Printf(&buf, ",`card%d`", j);
110        StringBuf_Printf(&buf, " FROM `%s` WHERE `guild_id`='%d' ORDER BY `nameid`", guild_storage_db, guild_id);
111
112        if( SQL_ERROR == Sql_Query(sql_handle, StringBuf_Value(&buf)) )
113                Sql_ShowDebug(sql_handle);
114
115        StringBuf_Destroy(&buf);
116
117        for( i = 0; i < MAX_GUILD_STORAGE && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
118        {
119                item = &p->storage_[i];
120                Sql_GetData(sql_handle, 0, &data, NULL); item->id = atoi(data);
121                Sql_GetData(sql_handle, 1, &data, NULL); item->nameid = atoi(data);
122                Sql_GetData(sql_handle, 2, &data, NULL); item->amount = atoi(data);
123                Sql_GetData(sql_handle, 3, &data, NULL); item->equip = atoi(data);
124                Sql_GetData(sql_handle, 4, &data, NULL); item->identify = atoi(data);
125                Sql_GetData(sql_handle, 5, &data, NULL); item->refine = atoi(data);
126                Sql_GetData(sql_handle, 6, &data, NULL); item->attribute = atoi(data);
127                for( j = 0; j < MAX_SLOTS; ++j )
128                {
129                        Sql_GetData(sql_handle, 7+j, &data, NULL); item->card[j] = atoi(data);
130                }
131        }
132        p->storage_amount = i;
133        Sql_FreeResult(sql_handle);
134
135        ShowInfo("guild storage load complete from DB - id: %d (total: %d)\n", guild_id, p->storage_amount);
136        return 0;
137}
138
139//---------------------------------------------------------
140// storage data initialize
141int inter_storage_sql_init(void)
142{
143
144        //memory alloc
145        ShowDebug("interserver storage memory initialize....(%d byte)\n",sizeof(struct storage));
146        storage_pt = (struct storage*)aCalloc(sizeof(struct storage), 1);
147        guild_storage_pt = (struct guild_storage*)aCalloc(sizeof(struct guild_storage), 1);
148
149        return 1;
150}
151// storage data finalize
152void inter_storage_sql_final(void)
153{
154        if (storage_pt) aFree(storage_pt);
155        if (guild_storage_pt) aFree(guild_storage_pt);   
156        return;
157}
158// q?f[^?
159int inter_storage_delete(int account_id)
160{
161        if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `account_id`='%d'", storage_db, account_id) )
162                Sql_ShowDebug(sql_handle);
163        return 0;
164}
165int inter_guild_storage_delete(int guild_id)
166{
167        if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_id`='%d'", guild_storage_db, guild_id) )
168                Sql_ShowDebug(sql_handle);
169        return 0;
170}
171
172//---------------------------------------------------------
173// packet from map server
174
175// recive packet about storage data
176int mapif_load_storage(int fd,int account_id)
177{
178        //load from DB
179        WFIFOHEAD(fd, sizeof(struct storage)+8);
180        storage_fromsql(account_id, storage_pt);
181        WFIFOW(fd,0)=0x3810;
182        WFIFOW(fd,2)=sizeof(struct storage)+8;
183        WFIFOL(fd,4)=account_id;
184        memcpy(WFIFOP(fd,8),storage_pt,sizeof(struct storage));
185        WFIFOSET(fd,WFIFOW(fd,2));
186        return 0;
187}
188// send ack to map server which is "storage data save ok."
189int mapif_save_storage_ack(int fd,int account_id)
190{
191        WFIFOHEAD(fd, 7);
192        WFIFOW(fd,0)=0x3811;
193        WFIFOL(fd,2)=account_id;
194        WFIFOB(fd,6)=0;
195        WFIFOSET(fd,7);
196        return 0;
197}
198
199int mapif_load_guild_storage(int fd,int account_id,int guild_id)
200{
201        if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id` FROM `%s` WHERE `guild_id`='%d'", guild_db, guild_id) )
202                Sql_ShowDebug(sql_handle);
203        else if( Sql_NumRows(sql_handle) > 0 )
204        {// guild exists
205                guild_storage_fromsql(guild_id, guild_storage_pt);
206
207                WFIFOHEAD(fd, sizeof(struct guild_storage)+12);
208                WFIFOW(fd,0) = 0x3818;
209                WFIFOW(fd,2) = sizeof(struct guild_storage)+12;
210                WFIFOL(fd,4) = account_id;
211                WFIFOL(fd,8) = guild_id;
212                memcpy(WFIFOP(fd,12), guild_storage_pt, sizeof(struct guild_storage));
213                WFIFOSET(fd, WFIFOW(fd,2));
214                return 0;
215        }
216        // guild does not exist
217        Sql_FreeResult(sql_handle);
218        WFIFOHEAD(fd, 12);
219        WFIFOW(fd,0) = 0x3818;
220        WFIFOW(fd,2) = 12;
221        WFIFOL(fd,4) = account_id;
222        WFIFOL(fd,8) = 0;
223        WFIFOSET(fd, 12);
224        return 0;
225}
226int mapif_save_guild_storage_ack(int fd,int account_id,int guild_id,int fail)
227{
228        WFIFOHEAD(fd,11);
229        WFIFOW(fd,0)=0x3819;
230        WFIFOL(fd,2)=account_id;
231        WFIFOL(fd,6)=guild_id;
232        WFIFOB(fd,10)=fail;
233        WFIFOSET(fd,11);
234        return 0;
235}
236
237//---------------------------------------------------------
238// packet from map server
239
240// recive request about storage data
241int mapif_parse_LoadStorage(int fd)
242{
243        RFIFOHEAD(fd);
244        mapif_load_storage(fd,RFIFOL(fd,2));
245        return 0;
246}
247// storage data recive and save
248int mapif_parse_SaveStorage(int fd)
249{
250        int account_id;
251        int len;
252        RFIFOHEAD(fd);
253        account_id=RFIFOL(fd,4);
254        len=RFIFOW(fd,2);
255        if(sizeof(struct storage)!=len-8){
256                ShowError("inter storage: data size error %d %d\n",sizeof(struct storage),len-8);
257        }else{
258                memcpy(&storage_pt[0],RFIFOP(fd,8),sizeof(struct storage));
259                storage_tosql(account_id, storage_pt);
260                mapif_save_storage_ack(fd,account_id);
261        }
262        return 0;
263}
264
265int mapif_parse_LoadGuildStorage(int fd)
266{
267        RFIFOHEAD(fd);
268        mapif_load_guild_storage(fd,RFIFOL(fd,2),RFIFOL(fd,6));
269        return 0;
270}
271
272int mapif_parse_SaveGuildStorage(int fd)
273{
274        int guild_id;
275        int len;
276
277        RFIFOHEAD(fd);
278        guild_id = RFIFOL(fd,8);
279        len = RFIFOW(fd,2);
280
281        if( sizeof(struct guild_storage) != len - 12 )
282        {
283                ShowError("inter storage: data size error %d != %d\n", sizeof(struct guild_storage), len - 12);
284        }
285        else
286        {
287                if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id` FROM `%s` WHERE `guild_id`='%d'", guild_db, guild_id) )
288                        Sql_ShowDebug(sql_handle);
289                else if( Sql_NumRows(sql_handle) > 0 )
290                {// guild exists
291                        Sql_FreeResult(sql_handle);
292                        memcpy(guild_storage_pt, RFIFOP(fd,12), sizeof(struct guild_storage));
293                        guild_storage_tosql(guild_id, guild_storage_pt);
294                        mapif_save_guild_storage_ack(fd, RFIFOL(fd,4), guild_id, 0);
295                        return 0;
296                }
297                Sql_FreeResult(sql_handle);
298        }
299        mapif_save_guild_storage_ack(fd, RFIFOL(fd,4), guild_id, 1);
300        return 0;
301}
302
303
304int inter_storage_parse_frommap(int fd)
305{
306        RFIFOHEAD(fd);
307        switch(RFIFOW(fd,0)){
308        case 0x3010: mapif_parse_LoadStorage(fd); break;
309        case 0x3011: mapif_parse_SaveStorage(fd); break;
310        case 0x3018: mapif_parse_LoadGuildStorage(fd); break;
311        case 0x3019: mapif_parse_SaveGuildStorage(fd); break;
312        default:
313                return 0;
314        }
315        return 1;
316}
317#endif //TXT_SQL_CONVERT
Note: See TracBrowser for help on using the browser.