root/src/char_sql/int_pet.c @ 21

Revision 1, 8.9 kB (checked in by jinshiro, 17 years ago)
Line 
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/socket.h"
7#include "../common/strlib.h"
8#include "../common/showmsg.h"
9#include "../common/utils.h"
10#include "../common/sql.h"
11#include "char.h"
12#include "inter.h"
13
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17
18struct s_pet *pet_pt;
19
20//---------------------------------------------------------
21int inter_pet_tosql(int pet_id, struct s_pet* p)
22{
23        //`pet` (`pet_id`, `class`,`name`,`account_id`,`char_id`,`level`,`egg_id`,`equip`,`intimate`,`hungry`,`rename_flag`,`incuvate`)
24        char esc_name[NAME_LENGTH*2+1];// escaped pet name
25
26        Sql_EscapeStringLen(sql_handle, esc_name, p->name, strnlen(p->name, NAME_LENGTH));
27        p->hungry = cap_value(p->hungry, 0, 100);
28        p->intimate = cap_value(p->intimate, 0, 1000);
29
30        if( pet_id == -1 )
31        {// New pet.
32                if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` "
33                        "(`class`,`name`,`account_id`,`char_id`,`level`,`egg_id`,`equip`,`intimate`,`hungry`,`rename_flag`,`incuvate`) "
34                        "VALUES ('%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')",
35                        pet_db, p->class_, esc_name, p->account_id, p->char_id, p->level, p->egg_id,
36                        p->equip, p->intimate, p->hungry, p->rename_flag, p->incuvate) )
37                {
38                        Sql_ShowDebug(sql_handle);
39                        return 0;
40                }
41                p->pet_id = (int)Sql_LastInsertId(sql_handle);
42        }
43        else
44        {// Update pet.
45                if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `class`='%d',`name`='%s',`account_id`='%d',`char_id`='%d',`level`='%d',`egg_id`='%d',`equip`='%d',`intimate`='%d',`hungry`='%d',`rename_flag`='%d',`incuvate`='%d' WHERE `pet_id`='%d'",
46                        pet_db, p->class_, esc_name, p->account_id, p->char_id, p->level, p->egg_id,
47                        p->equip, p->intimate, p->hungry, p->rename_flag, p->incuvate, p->pet_id) )
48                {
49                        Sql_ShowDebug(sql_handle);
50                        return 0;
51                }
52        }
53
54        if (save_log)
55                ShowInfo("Pet saved %d - %s.\n", pet_id, p->name);
56        return 1;
57}
58#ifndef TXT_SQL_CONVERT
59int inter_pet_fromsql(int pet_id, struct s_pet* p)
60{
61        char* data;
62        size_t len;
63
64#ifdef NOISY
65        ShowInfo("Loading pet (%d)...\n",pet_id);
66#endif
67        memset(p, 0, sizeof(struct s_pet));
68
69        //`pet` (`pet_id`, `class`,`name`,`account_id`,`char_id`,`level`,`egg_id`,`equip`,`intimate`,`hungry`,`rename_flag`,`incuvate`)
70
71        if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `pet_id`, `class`,`name`,`account_id`,`char_id`,`level`,`egg_id`,`equip`,`intimate`,`hungry`,`rename_flag`,`incuvate` FROM `%s` WHERE `pet_id`='%d'", pet_db, pet_id) )
72        {
73                Sql_ShowDebug(sql_handle);
74                return 0;
75        }
76
77        if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
78        {
79                p->pet_id = pet_id;
80                Sql_GetData(sql_handle,  1, &data, NULL); p->class_ = atoi(data);
81                Sql_GetData(sql_handle,  2, &data, &len); memcpy(p->name, data, min(len, NAME_LENGTH));
82                Sql_GetData(sql_handle,  3, &data, NULL); p->account_id = atoi(data);
83                Sql_GetData(sql_handle,  4, &data, NULL); p->char_id = atoi(data);
84                Sql_GetData(sql_handle,  5, &data, NULL); p->level = atoi(data);
85                Sql_GetData(sql_handle,  6, &data, NULL); p->egg_id = atoi(data);
86                Sql_GetData(sql_handle,  7, &data, NULL); p->equip = atoi(data);
87                Sql_GetData(sql_handle,  8, &data, NULL); p->intimate = atoi(data);
88                Sql_GetData(sql_handle,  9, &data, NULL); p->hungry = atoi(data);
89                Sql_GetData(sql_handle, 10, &data, NULL); p->rename_flag = atoi(data);
90                Sql_GetData(sql_handle, 11, &data, NULL); p->incuvate = atoi(data);
91
92                Sql_FreeResult(sql_handle);
93
94                p->hungry = cap_value(p->hungry, 0, 100);
95                p->intimate = cap_value(p->intimate, 0, 1000);
96
97                if( save_log )
98                        ShowInfo("Pet loaded (%d - %s).\n", pet_id, p->name);
99        }
100        return 0;
101}
102//----------------------------------------------
103
104int inter_pet_sql_init(void){
105        //memory alloc
106        pet_pt = (struct s_pet*)aCalloc(sizeof(struct s_pet), 1);
107        return 0;
108}
109void inter_pet_sql_final(void){
110        if (pet_pt) aFree(pet_pt);
111        return;
112}
113//----------------------------------
114int inter_pet_delete(int pet_id){
115        ShowInfo("delete pet request: %d...\n",pet_id);
116
117        if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `pet_id`='%d'", pet_db, pet_id) )
118                Sql_ShowDebug(sql_handle);
119        return 0;
120}
121//------------------------------------------------------
122int mapif_pet_created(int fd, int account_id, struct s_pet *p)
123{
124        WFIFOHEAD(fd, 11);
125        WFIFOW(fd, 0) =0x3880;
126        WFIFOL(fd, 2) =account_id;
127        if(p!=NULL){
128                WFIFOB(fd, 6)=0;
129                WFIFOL(fd, 7) =p->pet_id;
130                ShowInfo("int_pet: created pet %d - %s\n", p->pet_id, p->name);
131        }else{
132                WFIFOB(fd, 6)=1;
133                WFIFOL(fd, 7)=0;
134        }
135        WFIFOSET(fd, 11);
136
137        return 0;
138}
139
140int mapif_pet_info(int fd, int account_id, struct s_pet *p){
141        WFIFOHEAD(fd, sizeof(struct s_pet) + 9);
142        WFIFOW(fd, 0) =0x3881;
143        WFIFOW(fd, 2) =sizeof(struct s_pet) + 9;
144        WFIFOL(fd, 4) =account_id;
145        WFIFOB(fd, 8)=0;
146        memcpy(WFIFOP(fd, 9), p, sizeof(struct s_pet));
147        WFIFOSET(fd, WFIFOW(fd, 2));
148
149        return 0;
150}
151
152int mapif_pet_noinfo(int fd, int account_id){
153        WFIFOHEAD(fd, sizeof(struct s_pet) + 9);
154        WFIFOW(fd, 0) =0x3881;
155        WFIFOW(fd, 2) =sizeof(struct s_pet) + 9;
156        WFIFOL(fd, 4) =account_id;
157        WFIFOB(fd, 8)=1;
158        memset(WFIFOP(fd, 9), 0, sizeof(struct s_pet));
159        WFIFOSET(fd, WFIFOW(fd, 2));
160
161        return 0;
162}
163
164int mapif_save_pet_ack(int fd, int account_id, int flag){
165        WFIFOHEAD(fd, 7);
166        WFIFOW(fd, 0) =0x3882;
167        WFIFOL(fd, 2) =account_id;
168        WFIFOB(fd, 6) =flag;
169        WFIFOSET(fd, 7);
170
171        return 0;
172}
173
174int mapif_delete_pet_ack(int fd, int flag){
175        WFIFOHEAD(fd, 3);
176        WFIFOW(fd, 0) =0x3883;
177        WFIFOB(fd, 2) =flag;
178        WFIFOSET(fd, 3);
179
180        return 0;
181}
182
183int mapif_create_pet(int fd, int account_id, int char_id, short pet_class, short pet_lv, short pet_egg_id,
184        short pet_equip, short intimate, short hungry, char rename_flag, char incuvate, char *pet_name)
185{
186        memset(pet_pt, 0, sizeof(struct s_pet));
187        strncpy(pet_pt->name, pet_name, NAME_LENGTH);
188        if(incuvate == 1)
189                pet_pt->account_id = pet_pt->char_id = 0;
190        else {
191                pet_pt->account_id = account_id;
192                pet_pt->char_id = char_id;
193        }
194        pet_pt->class_ = pet_class;
195        pet_pt->level = pet_lv;
196        pet_pt->egg_id = pet_egg_id;
197        pet_pt->equip = pet_equip;
198        pet_pt->intimate = intimate;
199        pet_pt->hungry = hungry;
200        pet_pt->rename_flag = rename_flag;
201        pet_pt->incuvate = incuvate;
202
203        if(pet_pt->hungry < 0)
204                pet_pt->hungry = 0;
205        else if(pet_pt->hungry > 100)
206                pet_pt->hungry = 100;
207        if(pet_pt->intimate < 0)
208                pet_pt->intimate = 0;
209        else if(pet_pt->intimate > 1000)
210                pet_pt->intimate = 1000;
211
212        pet_pt->pet_id = -1; //Signal NEW pet.
213        if (inter_pet_tosql(pet_pt->pet_id,pet_pt))
214                mapif_pet_created(fd, account_id, pet_pt);
215        else    //Failed...
216                mapif_pet_created(fd, account_id, NULL);
217
218        return 0;
219}
220
221int mapif_load_pet(int fd, int account_id, int char_id, int pet_id){
222        memset(pet_pt, 0, sizeof(struct s_pet));
223
224        inter_pet_fromsql(pet_id, pet_pt);
225
226        if(pet_pt!=NULL) {
227                if(pet_pt->incuvate == 1) {
228                        pet_pt->account_id = pet_pt->char_id = 0;
229                        mapif_pet_info(fd, account_id, pet_pt);
230                }
231                else if(account_id == pet_pt->account_id && char_id == pet_pt->char_id)
232                        mapif_pet_info(fd, account_id, pet_pt);
233                else
234                        mapif_pet_noinfo(fd, account_id);
235        }
236        else
237                mapif_pet_noinfo(fd, account_id);
238
239        return 0;
240}
241
242int mapif_save_pet(int fd, int account_id, struct s_pet *data) {
243        //here process pet save request.
244        int len;
245        RFIFOHEAD(fd);
246        len=RFIFOW(fd, 2);
247        if(sizeof(struct s_pet)!=len-8) {
248                ShowError("inter pet: data size error %d %d\n", sizeof(struct s_pet), len-8);
249        }
250
251        else{
252                if(data->hungry < 0)
253                        data->hungry = 0;
254                else if(data->hungry > 100)
255                        data->hungry = 100;
256                if(data->intimate < 0)
257                        data->intimate = 0;
258                else if(data->intimate > 1000)
259                        data->intimate = 1000;
260                inter_pet_tosql(data->pet_id,data);
261                mapif_save_pet_ack(fd, account_id, 0);
262        }
263
264        return 0;
265}
266
267int mapif_delete_pet(int fd, int pet_id){
268        mapif_delete_pet_ack(fd, inter_pet_delete(pet_id));
269
270        return 0;
271}
272
273int mapif_parse_CreatePet(int fd){
274        RFIFOHEAD(fd);
275        mapif_create_pet(fd, RFIFOL(fd, 2), RFIFOL(fd, 6), RFIFOW(fd, 10), RFIFOW(fd, 12), RFIFOW(fd, 14), RFIFOW(fd, 16), RFIFOW(fd, 18),
276                RFIFOW(fd, 20), RFIFOB(fd, 22), RFIFOB(fd, 23), (char*)RFIFOP(fd, 24));
277        return 0;
278}
279
280int mapif_parse_LoadPet(int fd){
281        RFIFOHEAD(fd);
282        mapif_load_pet(fd, RFIFOL(fd, 2), RFIFOL(fd, 6), RFIFOL(fd, 10));
283        return 0;
284}
285
286int mapif_parse_SavePet(int fd){
287        RFIFOHEAD(fd);
288        mapif_save_pet(fd, RFIFOL(fd, 4), (struct s_pet *) RFIFOP(fd, 8));
289        return 0;
290}
291
292int mapif_parse_DeletePet(int fd){
293        RFIFOHEAD(fd);
294        mapif_delete_pet(fd, RFIFOL(fd, 2));
295        return 0;
296}
297
298int inter_pet_parse_frommap(int fd){
299        RFIFOHEAD(fd);
300        switch(RFIFOW(fd, 0)){
301        case 0x3080: mapif_parse_CreatePet(fd); break;
302        case 0x3081: mapif_parse_LoadPet(fd); break;
303        case 0x3082: mapif_parse_SavePet(fd); break;
304        case 0x3083: mapif_parse_DeletePet(fd); break;
305        default:
306                return 0;
307        }
308        return 1;
309}
310#endif //TXT_SQL_CONVERT
Note: See TracBrowser for help on using the browser.