1 // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
2 // For more information, see LICENCE in the main folder
4 #include "../common/mmo.h"
5 #include "../common/strlib.h"
6 #include "../common/showmsg.h"
7 #include "../common/socket.h"
8 #include "../common/utils.h"
9 #include "../common/sql.h"
16 int inter_homunculus_sql_init(void)
20 void inter_homunculus_sql_final(void)
25 static void mapif_homunculus_created(int fd, uint32 account_id, struct s_homunculus *sh, unsigned char flag)
27 WFIFOHEAD(fd, sizeof(struct s_homunculus)+9);
28 WFIFOW(fd,0) = 0x3890;
29 WFIFOW(fd,2) = sizeof(struct s_homunculus)+9;
30 WFIFOL(fd,4) = account_id;
32 memcpy(WFIFOP(fd,9),sh,sizeof(struct s_homunculus));
33 WFIFOSET(fd, WFIFOW(fd,2));
36 static void mapif_homunculus_deleted(int fd, int flag)
39 WFIFOW(fd, 0) = 0x3893;
40 WFIFOB(fd,2) = flag; //Flag 1 = success
44 static void mapif_homunculus_loaded(int fd, uint32 account_id, struct s_homunculus *hd)
46 WFIFOHEAD(fd, sizeof(struct s_homunculus)+9);
47 WFIFOW(fd,0) = 0x3891;
48 WFIFOW(fd,2) = sizeof(struct s_homunculus)+9;
49 WFIFOL(fd,4) = account_id;
52 WFIFOB(fd,8) = 1; // success
53 memcpy(WFIFOP(fd,9), hd, sizeof(struct s_homunculus));
57 WFIFOB(fd,8) = 0; // not found.
58 memset(WFIFOP(fd,9), 0, sizeof(struct s_homunculus));
60 WFIFOSET(fd, sizeof(struct s_homunculus)+9);
63 static void mapif_homunculus_saved(int fd, uint32 account_id, bool flag)
66 WFIFOW(fd,0) = 0x3892;
67 WFIFOL(fd,2) = account_id;
68 WFIFOB(fd,6) = flag; // 1:success, 0:failure
72 static void mapif_homunculus_renamed(int fd, uint32 account_id, uint32 char_id, unsigned char flag, char* name)
74 WFIFOHEAD(fd, NAME_LENGTH+12);
75 WFIFOW(fd, 0) = 0x3894;
76 WFIFOL(fd, 2) = account_id;
77 WFIFOL(fd, 6) = char_id;
79 safestrncpy(WFIFOCP(fd,11), name, NAME_LENGTH);
80 WFIFOSET(fd, NAME_LENGTH+12);
83 bool mapif_homunculus_save(struct s_homunculus* hd)
86 char esc_name[NAME_LENGTH*2+1];
88 Sql_EscapeStringLen(sql_handle, esc_name, hd->name, strnlen(hd->name, NAME_LENGTH));
92 if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` "
93 "(`char_id`, `class`,`prev_class`,`name`,`level`,`exp`,`intimacy`,`hunger`, `str`, `agi`, `vit`, `int`, `dex`, `luk`, `hp`,`max_hp`,`sp`,`max_sp`,`skill_point`, `rename_flag`, `vaporize`) "
94 "VALUES ('%d', '%d', '%d', '%s', '%d', '%u', '%u', '%d', '%d', %d, '%d', '%d', '%d', '%d', '%u', '%u', '%u', '%u', '%d', '%d', '%d')",
95 schema_config.homunculus_db, hd->char_id, hd->class_, hd->prev_class, esc_name, hd->level, hd->exp, hd->intimacy, hd->hunger, hd->str, hd->agi, hd->vit, hd->int_, hd->dex, hd->luk,
96 hd->hp, hd->max_hp, hd->sp, hd->max_sp, hd->skillpts, hd->rename_flag, hd->vaporize) )
98 Sql_ShowDebug(sql_handle);
103 hd->hom_id = (int)Sql_LastInsertId(sql_handle);
108 if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `char_id`='%d', `class`='%d',`prev_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`='%u',`max_hp`='%u',`sp`='%u',`max_sp`='%u',`skill_point`='%d', `rename_flag`='%d', `vaporize`='%d' WHERE `homun_id`='%d'",
109 schema_config.homunculus_db, hd->char_id, hd->class_, hd->prev_class, esc_name, hd->level, hd->exp, hd->intimacy, hd->hunger, hd->str, hd->agi, hd->vit, hd->int_, hd->dex, hd->luk,
110 hd->hp, hd->max_hp, hd->sp, hd->max_sp, hd->skillpts, hd->rename_flag, hd->vaporize, hd->hom_id) )
112 Sql_ShowDebug(sql_handle);
120 stmt = SqlStmt_Malloc(sql_handle);
121 if( SQL_ERROR == SqlStmt_Prepare(stmt, "REPLACE INTO `%s` (`homun_id`, `id`, `lv`) VALUES (%d, ?, ?)", schema_config.skill_homunculus_db, hd->hom_id) )
122 SqlStmt_ShowDebug(stmt);
123 for( i = 0; i < MAX_HOMUNSKILL; ++i )
125 if( hd->hskill[i].id > 0 && hd->hskill[i].lv != 0 )
127 SqlStmt_BindParam(stmt, 0, SQLDT_USHORT, &hd->hskill[i].id, 0);
128 SqlStmt_BindParam(stmt, 1, SQLDT_USHORT, &hd->hskill[i].lv, 0);
129 if( SQL_ERROR == SqlStmt_Execute(stmt) )
131 SqlStmt_ShowDebug(stmt);
147 // Load an homunculus
148 bool mapif_homunculus_load(int homun_id, struct s_homunculus* hd)
153 memset(hd, 0, sizeof(*hd));
155 if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `homun_id`,`char_id`,`class`,`prev_class`,`name`,`level`,`exp`,`intimacy`,`hunger`, `str`, `agi`, `vit`, `int`, `dex`, `luk`, `hp`,`max_hp`,`sp`,`max_sp`,`skill_point`,`rename_flag`, `vaporize` FROM `%s` WHERE `homun_id`='%u'",
156 schema_config.homunculus_db, homun_id) )
158 Sql_ShowDebug(sql_handle);
162 if( !Sql_NumRows(sql_handle) )
163 { //No homunculus found.
164 Sql_FreeResult(sql_handle);
167 if( SQL_SUCCESS != Sql_NextRow(sql_handle) )
169 Sql_ShowDebug(sql_handle);
170 Sql_FreeResult(sql_handle);
174 hd->hom_id = homun_id;
175 Sql_GetData(sql_handle, 1, &data, NULL); hd->char_id = atoi(data);
176 Sql_GetData(sql_handle, 2, &data, NULL); hd->class_ = atoi(data);
177 Sql_GetData(sql_handle, 3, &data, NULL); hd->prev_class = atoi(data);
178 Sql_GetData(sql_handle, 4, &data, &len); safestrncpy(hd->name, data, sizeof(hd->name));
179 Sql_GetData(sql_handle, 5, &data, NULL); hd->level = atoi(data);
180 Sql_GetData(sql_handle, 6, &data, NULL); hd->exp = atoi(data);
181 Sql_GetData(sql_handle, 7, &data, NULL); hd->intimacy = (unsigned int)strtoul(data, NULL, 10);
182 Sql_GetData(sql_handle, 8, &data, NULL); hd->hunger = atoi(data);
183 Sql_GetData(sql_handle, 9, &data, NULL); hd->str = atoi(data);
184 Sql_GetData(sql_handle, 10, &data, NULL); hd->agi = atoi(data);
185 Sql_GetData(sql_handle, 11, &data, NULL); hd->vit = atoi(data);
186 Sql_GetData(sql_handle, 12, &data, NULL); hd->int_ = atoi(data);
187 Sql_GetData(sql_handle, 13, &data, NULL); hd->dex = atoi(data);
188 Sql_GetData(sql_handle, 14, &data, NULL); hd->luk = atoi(data);
189 Sql_GetData(sql_handle, 15, &data, NULL); hd->hp = atoi(data);
190 Sql_GetData(sql_handle, 16, &data, NULL); hd->max_hp = atoi(data);
191 Sql_GetData(sql_handle, 17, &data, NULL); hd->sp = atoi(data);
192 Sql_GetData(sql_handle, 18, &data, NULL); hd->max_sp = atoi(data);
193 Sql_GetData(sql_handle, 19, &data, NULL); hd->skillpts = atoi(data);
194 Sql_GetData(sql_handle, 20, &data, NULL); hd->rename_flag = atoi(data);
195 Sql_GetData(sql_handle, 21, &data, NULL); hd->vaporize = atoi(data);
196 Sql_FreeResult(sql_handle);
198 hd->intimacy = umin(hd->intimacy,100000);
199 hd->hunger = cap_value(hd->hunger, 0, 100);
201 // Load Homunculus Skill
202 if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `id`,`lv` FROM `%s` WHERE `homun_id`=%d", schema_config.skill_homunculus_db, homun_id) )
204 Sql_ShowDebug(sql_handle);
207 while( SQL_SUCCESS == Sql_NextRow(sql_handle) )
211 Sql_GetData(sql_handle, 0, &data, NULL);
213 if( i < HM_SKILLBASE || i >= HM_SKILLBASE + MAX_HOMUNSKILL )
214 continue;// invalid skill id
215 i = i - HM_SKILLBASE;
216 hd->hskill[i].id = (unsigned short)atoi(data);
219 Sql_GetData(sql_handle, 1, &data, NULL);
220 hd->hskill[i].lv = (unsigned char)atoi(data);
222 Sql_FreeResult(sql_handle);
224 if( charserv_config.save_log )
225 ShowInfo("Homunculus loaded (ID: %d - %s / Class: %d / CID: %d).\n", hd->hom_id, hd->name, hd->class_, hd->char_id);
230 bool mapif_homunculus_delete(int homun_id)
232 if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `homun_id` = '%u'", schema_config.homunculus_db, homun_id)
233 || SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `homun_id` = '%u'", schema_config.skill_homunculus_db, homun_id)
235 Sql_ShowDebug(sql_handle);
238 ShowInfo("Homunculus '%d' has been deleted.\n", homun_id);
242 bool mapif_homunculus_rename(char *name)
246 // Check Authorised letters/symbols in the name of the homun
247 if( charserv_config.char_config.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(charserv_config.char_config.char_name_letters, name[i]) == NULL )
253 if( charserv_config.char_config.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(charserv_config.char_config.char_name_letters, name[i]) != NULL )
264 static void mapif_parse_homunculus_create(int fd, int len, uint32 account_id, struct s_homunculus* phd)
266 bool result = mapif_homunculus_save(phd);
267 mapif_homunculus_created(fd, account_id, phd, result);
270 static void mapif_parse_homunculus_delete(int fd, int homun_id)
272 bool result = mapif_homunculus_delete(homun_id);
273 mapif_homunculus_deleted(fd, result);
276 static void mapif_parse_homunculus_load(int fd, uint32 account_id, int homun_id)
278 struct s_homunculus hd;
279 bool result = mapif_homunculus_load(homun_id, &hd);
280 mapif_homunculus_loaded(fd, account_id, ( result ? &hd : NULL ));
283 static void mapif_parse_homunculus_save(int fd, int len, uint32 account_id, struct s_homunculus* phd)
285 bool result = mapif_homunculus_save(phd);
286 mapif_homunculus_saved(fd, account_id, result);
289 static void mapif_parse_homunculus_rename(int fd, uint32 account_id, uint32 char_id, char* name)
291 bool result = mapif_homunculus_rename(name);
292 mapif_homunculus_renamed(fd, account_id, char_id, result, name);
295 /*==========================================
297 *------------------------------------------*/
298 int inter_homunculus_parse_frommap(int fd)
300 unsigned short cmd = RFIFOW(fd,0);
304 case 0x3090: mapif_parse_homunculus_create(fd, (int)RFIFOW(fd,2), (int)RFIFOL(fd,4), (struct s_homunculus*)RFIFOP(fd,8)); break;
305 case 0x3091: mapif_parse_homunculus_load (fd, (int)RFIFOL(fd,2), (int)RFIFOL(fd,6)); break;
306 case 0x3092: mapif_parse_homunculus_save (fd, (int)RFIFOW(fd,2), (int)RFIFOL(fd,4), (struct s_homunculus*)RFIFOP(fd,8)); break;
307 case 0x3093: mapif_parse_homunculus_delete(fd, (int)RFIFOL(fd,2)); break;
308 case 0x3094: mapif_parse_homunculus_rename(fd, (int)RFIFOL(fd,2), (int)RFIFOL(fd,6), RFIFOCP(fd,10)); break;