root/src/char_sql/int_homun.c @ 10

Revision 1, 10.0 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/strlib.h"
7#include "../common/showmsg.h"
8#include "../common/socket.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
18
19int inter_homunculus_sql_init(void)
20{
21        return 0;
22}
23void inter_homunculus_sql_final(void)
24{
25        return;
26}
27
28static void mapif_homunculus_created(int fd, int account_id, struct s_homunculus *sh, unsigned char flag)
29{
30        WFIFOHEAD(fd, sizeof(struct s_homunculus)+9);
31        WFIFOW(fd,0) = 0x3890;
32        WFIFOW(fd,2) = sizeof(struct s_homunculus)+9;
33        WFIFOL(fd,4) = account_id;
34        WFIFOB(fd,8)= flag;
35        memcpy(WFIFOP(fd,9),sh,sizeof(struct s_homunculus));
36        WFIFOSET(fd, WFIFOW(fd,2));
37}
38
39static void mapif_homunculus_deleted(int fd, int flag)
40{
41        WFIFOHEAD(fd, 3);
42        WFIFOW(fd, 0) = 0x3893;
43        WFIFOB(fd,2) = flag; //Flag 1 = success
44        WFIFOSET(fd, 3);
45}
46
47static void mapif_homunculus_loaded(int fd, int account_id, struct s_homunculus *hd)
48{
49        WFIFOHEAD(fd, sizeof(struct s_homunculus)+9);
50        WFIFOW(fd,0) = 0x3891;
51        WFIFOW(fd,2) = sizeof(struct s_homunculus)+9;
52        WFIFOL(fd,4) = account_id;
53        if( hd != NULL )
54        {
55                WFIFOB(fd,8) = 1; // success
56                memcpy(WFIFOP(fd,9), hd, sizeof(struct s_homunculus));
57        }
58        else
59        {
60                WFIFOB(fd,8) = 0; // not found.
61                memset(WFIFOP(fd,9), 0, sizeof(struct s_homunculus));
62        }
63        WFIFOSET(fd, sizeof(struct s_homunculus)+9);
64}
65
66static void mapif_homunculus_saved(int fd, int account_id, bool flag)
67{
68        WFIFOHEAD(fd, 7);
69        WFIFOW(fd,0) = 0x3892;
70        WFIFOL(fd,2) = account_id;
71        WFIFOB(fd,6) = flag; // 1:success, 0:failure
72        WFIFOSET(fd, 7);
73}
74
75static void mapif_homunculus_renamed(int fd, int account_id, int char_id, unsigned char flag, char* name)
76{
77        WFIFOHEAD(fd, NAME_LENGTH+12);
78        WFIFOW(fd, 0) = 0x3894;
79        WFIFOL(fd, 2) = account_id;
80        WFIFOL(fd, 6) = char_id;
81        WFIFOB(fd,10) = flag;
82        safestrncpy((char*)WFIFOP(fd,11), name, NAME_LENGTH);
83        WFIFOSET(fd, NAME_LENGTH+12);
84}
85
86bool mapif_homunculus_save(struct s_homunculus* hd)
87{
88        bool flag = true;
89        char esc_name[NAME_LENGTH*2+1];
90
91        Sql_EscapeStringLen(sql_handle, esc_name, hd->name, strnlen(hd->name, NAME_LENGTH));
92
93        if( hd->hom_id == 0 )
94        {// new homunculus
95                if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `homunculus` "
96                        "(`char_id`, `class`,`name`,`level`,`exp`,`intimacy`,`hunger`, `str`, `agi`, `vit`, `int`, `dex`, `luk`, `hp`,`max_hp`,`sp`,`max_sp`,`skill_point`, `rename_flag`, `vaporize`) "
97                        "VALUES ('%d', '%d', '%s', '%d', '%u', '%u', '%d', '%d', %d, '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')",
98                        hd->char_id, hd->class_, esc_name, hd->level, hd->exp, hd->intimacy, hd->hunger, hd->str, hd->agi, hd->vit, hd->int_, hd->dex, hd->luk,
99                        hd->hp, hd->max_hp, hd->sp, hd->max_sp, hd->skillpts, hd->rename_flag, hd->vaporize) )
100                {
101                        Sql_ShowDebug(sql_handle);
102                        flag = false;
103                }
104                else
105                {
106                        hd->hom_id = (int)Sql_LastInsertId(sql_handle);
107                }
108        }
109        else
110        {
111                if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `homunculus` SET `char_id`='%d', `class`='%d',`name`='%s',`level`='%d',`exp`='%u',`intimacy`='%u',`hunger`='%d', `str`='%d', `agi`='%d', `vit`='%d', `int`='%d', `dex`='%d', `luk`='%d', `hp`='%d',`max_hp`='%d',`sp`='%d',`max_sp`='%d',`skill_point`='%d', `rename_flag`='%d', `vaporize`='%d' WHERE `homun_id`='%d'",
112                        hd->char_id, hd->class_, esc_name, hd->level, hd->exp, hd->intimacy, hd->hunger, hd->str, hd->agi, hd->vit, hd->int_, hd->dex, hd->luk,
113                        hd->hp, hd->max_hp, hd->sp, hd->max_sp, hd->skillpts, hd->rename_flag, hd->vaporize, hd->hom_id) )
114                {
115                        Sql_ShowDebug(sql_handle);
116                        flag = false;
117                }
118                else
119                {
120                        SqlStmt* stmt;
121                        int i;
122
123                        stmt = SqlStmt_Malloc(sql_handle);
124                        if( SQL_ERROR == SqlStmt_Prepare(stmt, "REPLACE INTO `skill_homunculus` (`homun_id`, `id`, `lv`) VALUES (%d, ?, ?)", hd->hom_id) )
125                                SqlStmt_ShowDebug(stmt);
126                        for( i = 0; i < MAX_HOMUNSKILL; ++i )
127                        {
128                                if( hd->hskill[i].id > 0 && hd->hskill[i].lv != 0 )
129                                {
130                                        SqlStmt_BindParam(stmt, 0, SQLDT_USHORT, &hd->hskill[i].id, 0);
131                                        SqlStmt_BindParam(stmt, 1, SQLDT_USHORT, &hd->hskill[i].lv, 0);
132                                        if( SQL_ERROR == SqlStmt_Execute(stmt) )
133                                        {
134                                                SqlStmt_ShowDebug(stmt);
135                                                SqlStmt_Free(stmt);
136                                                flag = false;
137                                                break;
138                                        }
139                                }
140                        }
141                        SqlStmt_Free(stmt);
142                }
143        }
144
145        return flag;
146}
147
148
149
150// Load an homunculus
151bool mapif_homunculus_load(int homun_id, struct s_homunculus* hd)
152{
153        int i;
154        char* data;
155        size_t len;
156
157        memset(hd, 0, sizeof(*hd));
158
159        if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `homun_id`,`char_id`,`class`,`name`,`level`,`exp`,`intimacy`,`hunger`, `str`, `agi`, `vit`, `int`, `dex`, `luk`, `hp`,`max_hp`,`sp`,`max_sp`,`skill_point`,`rename_flag`, `vaporize` FROM `homunculus` WHERE `homun_id`='%u'", homun_id) )
160        {
161                Sql_ShowDebug(sql_handle);
162                return false;
163        }
164
165        if( !Sql_NumRows(sql_handle) )
166        {       //No homunculus found.
167                Sql_FreeResult(sql_handle);
168                return false;
169        }
170        if( SQL_SUCCESS != Sql_NextRow(sql_handle) )
171        {
172                Sql_ShowDebug(sql_handle);
173                Sql_FreeResult(sql_handle);
174                return false;
175        }
176
177        hd->hom_id = homun_id;
178        Sql_GetData(sql_handle,  1, &data, NULL); hd->char_id = atoi(data);
179        Sql_GetData(sql_handle,  2, &data, NULL); hd->class_ = atoi(data);
180        Sql_GetData(sql_handle,  3, &data, &len); safestrncpy(hd->name, data, sizeof(hd->name));
181        Sql_GetData(sql_handle,  4, &data, NULL); hd->level = atoi(data);
182        Sql_GetData(sql_handle,  5, &data, NULL); hd->exp = atoi(data);
183        Sql_GetData(sql_handle,  6, &data, NULL); hd->intimacy = (unsigned int)strtoul(data, NULL, 10);
184        Sql_GetData(sql_handle,  7, &data, NULL); hd->hunger = atoi(data);
185        Sql_GetData(sql_handle,  8, &data, NULL); hd->str = atoi(data);
186        Sql_GetData(sql_handle,  9, &data, NULL); hd->agi = atoi(data);
187        Sql_GetData(sql_handle, 10, &data, NULL); hd->vit = atoi(data);
188        Sql_GetData(sql_handle, 11, &data, NULL); hd->int_ = atoi(data);
189        Sql_GetData(sql_handle, 12, &data, NULL); hd->dex = atoi(data);
190        Sql_GetData(sql_handle, 13, &data, NULL); hd->luk = atoi(data);
191        Sql_GetData(sql_handle, 14, &data, NULL); hd->hp = atoi(data);
192        Sql_GetData(sql_handle, 15, &data, NULL); hd->max_hp = atoi(data);
193        Sql_GetData(sql_handle, 16, &data, NULL); hd->sp = atoi(data);
194        Sql_GetData(sql_handle, 17, &data, NULL); hd->max_sp = atoi(data);
195        Sql_GetData(sql_handle, 18, &data, NULL); hd->skillpts = atoi(data);
196        Sql_GetData(sql_handle, 19, &data, NULL); hd->rename_flag = atoi(data);
197        Sql_GetData(sql_handle, 20, &data, NULL); hd->vaporize = atoi(data);
198        Sql_FreeResult(sql_handle);
199
200        hd->intimacy = cap_value(hd->intimacy, 0, 100000);
201        hd->hunger = cap_value(hd->hunger, 0, 100);
202
203        // Load Homunculus Skill
204        if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `id`,`lv` FROM `skill_homunculus` WHERE `homun_id`=%d", homun_id) )
205        {
206                Sql_ShowDebug(sql_handle);
207                return false;
208        }
209        while( SQL_SUCCESS == Sql_NextRow(sql_handle) )
210        {
211                // id
212                Sql_GetData(sql_handle, 0, &data, NULL);
213                i = atoi(data);
214                if( i < HM_SKILLBASE || i >= HM_SKILLBASE + MAX_HOMUNSKILL )
215                        continue;// invalid skill id
216                i = i - HM_SKILLBASE;
217                hd->hskill[i].id = (unsigned short)atoi(data);
218
219                // lv
220                Sql_GetData(sql_handle, 1, &data, NULL);
221                hd->hskill[i].lv = (unsigned short)atoi(data);
222        }
223        Sql_FreeResult(sql_handle);
224
225        if( save_log )
226                ShowInfo("Homunculus loaded (%d - %s).\n", hd->hom_id, hd->name);
227
228        return true;
229}
230
231bool mapif_homunculus_delete(int homun_id)
232{
233        if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `homunculus` WHERE `homun_id` = '%u'", homun_id)
234        ||      SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `skill_homunculus` WHERE `homun_id` = '%u'", homun_id)
235        ) {
236                Sql_ShowDebug(sql_handle);
237                return false;
238        }
239        return true;
240}
241
242bool mapif_homunculus_rename(char *name)
243{
244        int i;
245
246        // Check Authorised letters/symbols in the name of the homun
247        if( char_name_option == 1 )
248        {// only letters/symbols in char_name_letters are authorised
249                for( i = 0; i < NAME_LENGTH && name[i]; i++ )
250                        if( strchr(char_name_letters, name[i]) == NULL )
251                                return false;
252        } else
253        if( char_name_option == 2 )
254        {// letters/symbols in char_name_letters are forbidden
255                for( i = 0; i < NAME_LENGTH && name[i]; i++ )
256                        if( strchr(char_name_letters, name[i]) != NULL )
257                                return false;
258        }
259
260        return true;
261}
262
263
264static void mapif_parse_homunculus_create(int fd, int len, int account_id, struct s_homunculus* phd)
265{
266        bool result = mapif_homunculus_save(phd);
267        mapif_homunculus_created(fd, account_id, phd, result);
268}
269
270static void mapif_parse_homunculus_delete(int fd, int homun_id)
271{
272        bool result = mapif_homunculus_delete(homun_id);
273        mapif_homunculus_deleted(fd, result);
274}
275
276static void mapif_parse_homunculus_load(int fd, int account_id, int homun_id)
277{
278        struct s_homunculus hd;
279        bool result = mapif_homunculus_load(homun_id, &hd);
280        mapif_homunculus_loaded(fd, account_id, ( result ? &hd : NULL ));
281}
282
283static void mapif_parse_homunculus_save(int fd, int len, int account_id, struct s_homunculus* phd)
284{
285        bool result = mapif_homunculus_save(phd);
286        mapif_homunculus_saved(fd, account_id, result);
287}
288
289static void mapif_parse_homunculus_rename(int fd, int account_id, int char_id, char* name)
290{
291        bool result = mapif_homunculus_rename(name);
292        mapif_homunculus_renamed(fd, account_id, char_id, result, name);
293}
294
295
296int inter_homunculus_parse_frommap(int fd)
297{
298        unsigned short cmd = RFIFOW(fd,0);
299
300        switch( cmd )
301        {
302        case 0x3090: mapif_parse_homunculus_create(fd, (int)RFIFOW(fd,2), (int)RFIFOL(fd,4), (struct s_homunculus*)RFIFOP(fd,8)); break;
303        case 0x3091: mapif_parse_homunculus_load  (fd, (int)RFIFOL(fd,2), (int)RFIFOL(fd,6)); break;
304        case 0x3092: mapif_parse_homunculus_save  (fd, (int)RFIFOW(fd,2), (int)RFIFOL(fd,4), (struct s_homunculus*)RFIFOP(fd,8)); break;
305        case 0x3093: mapif_parse_homunculus_delete(fd, (int)RFIFOL(fd,2)); break;
306        case 0x3094: mapif_parse_homunculus_rename(fd, (int)RFIFOL(fd,2), (int)RFIFOL(fd,6), (char*)RFIFOP(fd,10)); break;
307        default:
308                return 0;
309        }
310        return 1;
311}
Note: See TracBrowser for help on using the browser.