int_homun.cpp 10 KB

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