int_homun.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "../common/mmo.h"
  4. #include "../common/malloc.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"
  10. #include "char.h"
  11. #include "inter.h"
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. struct s_homunculus *homun_pt;
  16. int inter_homunculus_sql_init(void){
  17. //memory alloc
  18. homun_pt = (struct s_homunculus*)aCalloc(sizeof(struct s_homunculus), 1);
  19. return 0;
  20. }
  21. void inter_homunculus_sql_final(void){
  22. if (homun_pt) aFree(homun_pt);
  23. return;
  24. }
  25. static int mapif_saved_homunculus(int fd, int account_id, bool flag)
  26. {
  27. WFIFOHEAD(fd, 7);
  28. WFIFOW(fd,0) = 0x3892;
  29. WFIFOL(fd,2) = account_id;
  30. WFIFOB(fd,6) = flag; // 1:success, 0:failure
  31. WFIFOSET(fd, 7);
  32. return 0;
  33. }
  34. int mapif_info_homunculus(int fd, int account_id, struct s_homunculus *hd)
  35. {
  36. WFIFOHEAD(fd, sizeof(struct s_homunculus)+9);
  37. WFIFOW(fd,0) = 0x3891;
  38. WFIFOW(fd,2) = sizeof(struct s_homunculus)+9;
  39. WFIFOL(fd,4) = account_id;
  40. WFIFOB(fd,8) = 1; // account loaded with success
  41. memcpy(WFIFOP(fd,9), hd, sizeof(struct s_homunculus));
  42. WFIFOSET(fd, sizeof(struct s_homunculus)+9);
  43. return 0;
  44. }
  45. int mapif_homunculus_deleted(int fd, int flag)
  46. {
  47. WFIFOHEAD(fd, 3);
  48. WFIFOW(fd, 0) = 0x3893;
  49. WFIFOB(fd,2) = flag; //Flag 1 = success
  50. WFIFOSET(fd, 3);
  51. return 0;
  52. }
  53. int mapif_homunculus_created(int fd, int account_id, struct s_homunculus *sh, unsigned char flag)
  54. {
  55. WFIFOHEAD(fd, sizeof(struct s_homunculus)+9);
  56. WFIFOW(fd,0) = 0x3890;
  57. WFIFOW(fd,2) = sizeof(struct s_homunculus)+9;
  58. WFIFOL(fd,4) = account_id;
  59. WFIFOB(fd,8)= flag;
  60. memcpy(WFIFOP(fd,9),sh,sizeof(struct s_homunculus));
  61. WFIFOSET(fd, WFIFOW(fd,2));
  62. return 0;
  63. }
  64. // Save/Update Homunculus Skills
  65. static bool mapif_save_homunculus_skills(struct s_homunculus *hd)
  66. {
  67. SqlStmt* stmt;
  68. int i;
  69. stmt = SqlStmt_Malloc(sql_handle);
  70. if( SQL_ERROR == SqlStmt_Prepare(stmt, "REPLACE INTO `skill_homunculus` (`homun_id`, `id`, `lv`) VALUES (%d, ?, ?)", hd->hom_id) )
  71. SqlStmt_ShowDebug(stmt);
  72. for( i = 0; i < MAX_HOMUNSKILL; ++i )
  73. {
  74. if( hd->hskill[i].id > 0 && hd->hskill[i].lv != 0 )
  75. {
  76. SqlStmt_BindParam(stmt, 0, SQLDT_USHORT, &hd->hskill[i].id, 0);
  77. SqlStmt_BindParam(stmt, 1, SQLDT_USHORT, &hd->hskill[i].lv, 0);
  78. if( SQL_ERROR == SqlStmt_Execute(stmt) )
  79. {
  80. SqlStmt_ShowDebug(stmt);
  81. SqlStmt_Free(stmt);
  82. return false;
  83. }
  84. }
  85. }
  86. SqlStmt_Free(stmt);
  87. return true;
  88. }
  89. static bool mapif_save_homunculus(int fd, int account_id, struct s_homunculus *hd)
  90. {
  91. bool flag = true;
  92. char esc_name[NAME_LENGTH*2+1];
  93. Sql_EscapeStringLen(sql_handle, esc_name, hd->name, strnlen(hd->name, NAME_LENGTH));
  94. if( hd->hom_id == 0 )
  95. {// new homunculus
  96. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `homunculus` "
  97. "(`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`) "
  98. "VALUES ('%d', '%d', '%s', '%d', '%u', '%u', '%d', '%d', %d, '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')",
  99. 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,
  100. hd->hp, hd->max_hp, hd->sp, hd->max_sp, hd->skillpts, hd->rename_flag, hd->vaporize) )
  101. {
  102. Sql_ShowDebug(sql_handle);
  103. flag = false;
  104. }
  105. else
  106. {
  107. hd->hom_id = (int)Sql_LastInsertId(sql_handle);
  108. }
  109. }
  110. else
  111. {
  112. 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'",
  113. 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,
  114. hd->hp, hd->max_hp, hd->sp, hd->max_sp, hd->skillpts, hd->rename_flag, hd->vaporize, hd->hom_id) )
  115. {
  116. Sql_ShowDebug(sql_handle);
  117. flag = false;
  118. }
  119. else
  120. {
  121. flag = mapif_save_homunculus_skills(hd);
  122. mapif_saved_homunculus(fd, account_id, flag);
  123. }
  124. }
  125. return flag;
  126. }
  127. // Load an homunculus
  128. int mapif_load_homunculus(int fd)
  129. {
  130. int i;
  131. char* data;
  132. size_t len;
  133. memset(homun_pt, 0, sizeof(struct s_homunculus));
  134. RFIFOHEAD(fd);
  135. 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'", RFIFOL(fd,6)) )
  136. {
  137. Sql_ShowDebug(sql_handle);
  138. return 0;
  139. }
  140. if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  141. {
  142. homun_pt->hom_id = RFIFOL(fd,6);
  143. Sql_GetData(sql_handle, 1, &data, NULL); homun_pt->char_id = atoi(data);
  144. Sql_GetData(sql_handle, 2, &data, NULL); homun_pt->class_ = atoi(data);
  145. Sql_GetData(sql_handle, 3, &data, &len); memcpy(homun_pt->name, data, min(len, NAME_LENGTH));
  146. Sql_GetData(sql_handle, 4, &data, NULL); homun_pt->level = atoi(data);
  147. Sql_GetData(sql_handle, 5, &data, NULL); homun_pt->exp = atoi(data);
  148. Sql_GetData(sql_handle, 6, &data, NULL); homun_pt->intimacy = (unsigned int)strtoul(data, NULL, 10);
  149. homun_pt->intimacy = cap_value(homun_pt->intimacy, 0, 100000);
  150. Sql_GetData(sql_handle, 7, &data, NULL); homun_pt->hunger = atoi(data);
  151. homun_pt->hunger = cap_value(homun_pt->hunger, 0, 100);
  152. Sql_GetData(sql_handle, 8, &data, NULL); homun_pt->str = atoi(data);
  153. Sql_GetData(sql_handle, 9, &data, NULL); homun_pt->agi = atoi(data);
  154. Sql_GetData(sql_handle, 10, &data, NULL); homun_pt->vit = atoi(data);
  155. Sql_GetData(sql_handle, 11, &data, NULL); homun_pt->int_ = atoi(data);
  156. Sql_GetData(sql_handle, 12, &data, NULL); homun_pt->dex = atoi(data);
  157. Sql_GetData(sql_handle, 13, &data, NULL); homun_pt->luk = atoi(data);
  158. Sql_GetData(sql_handle, 14, &data, NULL); homun_pt->hp = atoi(data);
  159. Sql_GetData(sql_handle, 15, &data, NULL); homun_pt->max_hp = atoi(data);
  160. Sql_GetData(sql_handle, 16, &data, NULL); homun_pt->sp = atoi(data);
  161. Sql_GetData(sql_handle, 17, &data, NULL); homun_pt->max_sp = atoi(data);
  162. Sql_GetData(sql_handle, 18, &data, NULL); homun_pt->skillpts = atoi(data);
  163. Sql_GetData(sql_handle, 19, &data, NULL); homun_pt->rename_flag = atoi(data);
  164. Sql_GetData(sql_handle, 20, &data, NULL); homun_pt->vaporize = atoi(data);
  165. Sql_FreeResult(sql_handle);
  166. }
  167. // Load Homunculus Skill
  168. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `id`,`lv` FROM `skill_homunculus` WHERE `homun_id`=%d", homun_pt->hom_id) )
  169. {
  170. Sql_ShowDebug(sql_handle);
  171. return 0;
  172. }
  173. while( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  174. {
  175. // id
  176. Sql_GetData(sql_handle, 0, &data, NULL);
  177. i = atoi(data);
  178. if( i < HM_SKILLBASE || i >= HM_SKILLBASE + MAX_HOMUNSKILL )
  179. continue;// invalid guild skill
  180. i = i - HM_SKILLBASE;
  181. homun_pt->hskill[i].id = (unsigned short)atoi(data);
  182. // lv
  183. Sql_GetData(sql_handle, 1, &data, NULL);
  184. homun_pt->hskill[i].lv = (unsigned short)atoi(data);
  185. }
  186. Sql_FreeResult(sql_handle);
  187. if( save_log )
  188. ShowInfo("Homunculus loaded (%d - %s).\n", homun_pt->hom_id, homun_pt->name);
  189. return mapif_info_homunculus(fd, RFIFOL(fd,2), homun_pt);
  190. }
  191. int inter_delete_homunculus(int hom_id)
  192. {
  193. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `homunculus` WHERE `homun_id` = '%u'", hom_id) ||
  194. SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `skill_homunculus` WHERE `homun_id` = '%u'", hom_id) )
  195. {
  196. Sql_ShowDebug(sql_handle);
  197. return 0;
  198. }
  199. return 1;
  200. }
  201. int mapif_delete_homunculus(int fd)
  202. {
  203. RFIFOHEAD(fd);
  204. mapif_homunculus_deleted(fd, inter_delete_homunculus(RFIFOL(fd,2)));
  205. return 1;
  206. }
  207. int mapif_rename_homun_ack(int fd, int account_id, int char_id, unsigned char flag, char *name){
  208. WFIFOHEAD(fd, NAME_LENGTH+12);
  209. WFIFOW(fd, 0) =0x3894;
  210. WFIFOL(fd, 2) =account_id;
  211. WFIFOL(fd, 6) =char_id;
  212. WFIFOB(fd, 10) =flag;
  213. memcpy(WFIFOP(fd, 11), name, NAME_LENGTH);
  214. WFIFOSET(fd, NAME_LENGTH+12);
  215. return 0;
  216. }
  217. int mapif_rename_homun(int fd, int account_id, int char_id, char *name){
  218. int i;
  219. // Check Authorised letters/symbols in the name of the homun
  220. if (char_name_option == 1) { // only letters/symbols in char_name_letters are authorised
  221. for (i = 0; i < NAME_LENGTH && name[i]; i++)
  222. if (strchr(char_name_letters, name[i]) == NULL) {
  223. mapif_rename_homun_ack(fd, account_id, char_id, 0, name);
  224. return 0;
  225. }
  226. } else if (char_name_option == 2) { // letters/symbols in char_name_letters are forbidden
  227. for (i = 0; i < NAME_LENGTH && name[i]; i++)
  228. if (strchr(char_name_letters, name[i]) != NULL) {
  229. mapif_rename_homun_ack(fd, account_id, char_id, 0, name);
  230. return 0;
  231. }
  232. }
  233. mapif_rename_homun_ack(fd, account_id, char_id, 1, name);
  234. return 0;
  235. }
  236. int mapif_parse_CreateHomunculus(int fd)
  237. {
  238. RFIFOHEAD(fd);
  239. memcpy(homun_pt, RFIFOP(fd,8), sizeof(struct s_homunculus));
  240. // Save in sql db
  241. if(mapif_save_homunculus(fd,RFIFOL(fd,4), homun_pt))
  242. return mapif_homunculus_created(fd, RFIFOL(fd,4), homun_pt, 1); // send homun_id
  243. return mapif_homunculus_created(fd, RFIFOL(fd,4), homun_pt, 0); // fail
  244. }
  245. int inter_homunculus_parse_frommap(int fd){
  246. RFIFOHEAD(fd);
  247. switch(RFIFOW(fd, 0)){
  248. case 0x3090: mapif_parse_CreateHomunculus(fd); break;
  249. case 0x3091: mapif_load_homunculus(fd); break;
  250. case 0x3092: mapif_save_homunculus(fd, RFIFOW(fd,4), (struct s_homunculus*) RFIFOP(fd, 8)); break;
  251. case 0x3093: mapif_delete_homunculus(fd); break; // doesn't need to be parse, very simple packet...
  252. case 0x3094: mapif_rename_homun(fd, RFIFOL(fd, 2), RFIFOL(fd, 6), RFIFOP(fd, 10)); break;
  253. default:
  254. return 0;
  255. }
  256. return 1;
  257. }