int_pet.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "int_pet.hpp"
  4. #include <cstdlib>
  5. #include <cstring>
  6. #include <common/malloc.hpp>
  7. #include <common/mmo.hpp>
  8. #include <common/showmsg.hpp>
  9. #include <common/socket.hpp>
  10. #include <common/sql.hpp>
  11. #include <common/strlib.hpp>
  12. #include <common/utils.hpp>
  13. #include "char.hpp"
  14. #include "inter.hpp"
  15. struct s_pet *pet_pt;
  16. int32 mapif_load_pet(int32 fd, uint32 account_id, uint32 char_id, int32 pet_id);
  17. //---------------------------------------------------------
  18. int32 inter_pet_tosql(int32 pet_id, struct s_pet* p)
  19. {
  20. //`pet` (`pet_id`, `class`,`name`,`account_id`,`char_id`,`level`,`egg_id`,`equip`,`intimate`,`hungry`,`rename_flag`,`incubate`,`autofeed`)
  21. char esc_name[NAME_LENGTH*2+1];// escaped pet name
  22. Sql_EscapeStringLen(sql_handle, esc_name, p->name, strnlen(p->name, NAME_LENGTH));
  23. p->hungry = cap_value(p->hungry, 0, 100);
  24. p->intimate = cap_value(p->intimate, 0, 1000);
  25. if( pet_id == -1 )
  26. {// New pet.
  27. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` "
  28. "(`class`,`name`,`account_id`,`char_id`,`level`,`egg_id`,`equip`,`intimate`,`hungry`,`rename_flag`,`incubate`,`autofeed`) "
  29. "VALUES ('%d', '%s', '%d', '%d', '%d', '%u', '%u', '%d', '%d', '%d', '%d', '%d')",
  30. schema_config.pet_db, p->class_, esc_name, p->account_id, p->char_id, p->level, p->egg_id,
  31. p->equip, p->intimate, p->hungry, p->rename_flag, p->incubate, p->autofeed) )
  32. {
  33. Sql_ShowDebug(sql_handle);
  34. return 0;
  35. }
  36. p->pet_id = (int32)Sql_LastInsertId(sql_handle);
  37. }
  38. else
  39. {// Update pet.
  40. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `class`='%d',`name`='%s',`account_id`='%d',`char_id`='%d',`level`='%d',`egg_id`='%u',`equip`='%u',`intimate`='%d',`hungry`='%d',`rename_flag`='%d',`incubate`='%d',`autofeed`='%d' WHERE `pet_id`='%d'",
  41. schema_config.pet_db, p->class_, esc_name, p->account_id, p->char_id, p->level, p->egg_id,
  42. p->equip, p->intimate, p->hungry, p->rename_flag, p->incubate, p->autofeed, p->pet_id) )
  43. {
  44. Sql_ShowDebug(sql_handle);
  45. return 0;
  46. }
  47. }
  48. if (charserv_config.save_log)
  49. ShowInfo("Pet saved %d - %s.\n", pet_id, p->name);
  50. return 1;
  51. }
  52. int32 inter_pet_fromsql(int32 pet_id, struct s_pet* p)
  53. {
  54. char* data;
  55. size_t len;
  56. #ifdef NOISY
  57. ShowInfo("Loading pet (%d)...\n",pet_id);
  58. #endif
  59. memset(p, 0, sizeof(struct s_pet));
  60. //`pet` (`pet_id`, `class`,`name`,`account_id`,`char_id`,`level`,`egg_id`,`equip`,`intimate`,`hungry`,`rename_flag`,`incubate`,`autofeed`)
  61. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `pet_id`, `class`,`name`,`account_id`,`char_id`,`level`,`egg_id`,`equip`,`intimate`,`hungry`,`rename_flag`,`incubate`,`autofeed` FROM `%s` WHERE `pet_id`='%d'", schema_config.pet_db, pet_id) )
  62. {
  63. Sql_ShowDebug(sql_handle);
  64. return 0;
  65. }
  66. if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  67. {
  68. p->pet_id = pet_id;
  69. Sql_GetData(sql_handle, 1, &data, nullptr); p->class_ = atoi(data);
  70. Sql_GetData(sql_handle, 2, &data, &len); memcpy(p->name, data, zmin(len, NAME_LENGTH));
  71. Sql_GetData(sql_handle, 3, &data, nullptr); p->account_id = atoi(data);
  72. Sql_GetData(sql_handle, 4, &data, nullptr); p->char_id = atoi(data);
  73. Sql_GetData(sql_handle, 5, &data, nullptr); p->level = atoi(data);
  74. Sql_GetData(sql_handle, 6, &data, nullptr); p->egg_id = strtoul(data, nullptr, 10);
  75. Sql_GetData(sql_handle, 7, &data, nullptr); p->equip = strtoul(data, nullptr, 10);
  76. Sql_GetData(sql_handle, 8, &data, nullptr); p->intimate = atoi(data);
  77. Sql_GetData(sql_handle, 9, &data, nullptr); p->hungry = atoi(data);
  78. Sql_GetData(sql_handle, 10, &data, nullptr); p->rename_flag = atoi(data);
  79. Sql_GetData(sql_handle, 11, &data, nullptr); p->incubate = atoi(data);
  80. Sql_GetData(sql_handle, 12, &data, nullptr); p->autofeed = atoi(data) != 0;
  81. Sql_FreeResult(sql_handle);
  82. p->hungry = cap_value(p->hungry, 0, 100);
  83. p->intimate = cap_value(p->intimate, 0, 1000);
  84. if( charserv_config.save_log )
  85. ShowInfo("Pet loaded (%d - %s).\n", pet_id, p->name);
  86. }
  87. return 0;
  88. }
  89. //----------------------------------------------
  90. int32 inter_pet_sql_init(void){
  91. //memory alloc
  92. pet_pt = (struct s_pet*)aCalloc(sizeof(struct s_pet), 1);
  93. return 0;
  94. }
  95. void inter_pet_sql_final(void){
  96. if (pet_pt) aFree(pet_pt);
  97. return;
  98. }
  99. //----------------------------------
  100. int32 inter_pet_delete(int32 pet_id){
  101. ShowInfo("delete pet request: %d...\n",pet_id);
  102. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `pet_id`='%d'", schema_config.pet_db, pet_id) )
  103. Sql_ShowDebug(sql_handle);
  104. return 0;
  105. }
  106. //------------------------------------------------------
  107. int32 mapif_pet_created(int32 fd, uint32 account_id, struct s_pet *p)
  108. {
  109. WFIFOHEAD(fd, 12);
  110. WFIFOW(fd, 0) = 0x3880;
  111. WFIFOL(fd, 2) = account_id;
  112. if(p!=nullptr){
  113. WFIFOW(fd, 6) = p->class_;
  114. WFIFOL(fd, 8) = p->pet_id;
  115. ShowInfo("int_pet: created pet %d - %s\n", p->pet_id, p->name);
  116. }else{
  117. WFIFOB(fd, 6) = 0;
  118. WFIFOL(fd, 8) = 0;
  119. }
  120. WFIFOSET(fd, 12);
  121. return 0;
  122. }
  123. int32 mapif_pet_info(int32 fd, uint32 account_id, struct s_pet *p){
  124. WFIFOHEAD(fd, sizeof(struct s_pet) + 9);
  125. WFIFOW(fd, 0) =0x3881;
  126. WFIFOW(fd, 2) =sizeof(struct s_pet) + 9;
  127. WFIFOL(fd, 4) =account_id;
  128. WFIFOB(fd, 8)=0;
  129. memcpy(WFIFOP(fd, 9), p, sizeof(struct s_pet));
  130. WFIFOSET(fd, WFIFOW(fd, 2));
  131. return 0;
  132. }
  133. int32 mapif_pet_noinfo(int32 fd, uint32 account_id){
  134. WFIFOHEAD(fd, sizeof(struct s_pet) + 9);
  135. WFIFOW(fd, 0) =0x3881;
  136. WFIFOW(fd, 2) =sizeof(struct s_pet) + 9;
  137. WFIFOL(fd, 4) =account_id;
  138. WFIFOB(fd, 8)=1;
  139. memset(WFIFOP(fd, 9), 0, sizeof(struct s_pet));
  140. WFIFOSET(fd, WFIFOW(fd, 2));
  141. return 0;
  142. }
  143. int32 mapif_save_pet_ack(int32 fd, uint32 account_id, int32 flag){
  144. WFIFOHEAD(fd, 7);
  145. WFIFOW(fd, 0) =0x3882;
  146. WFIFOL(fd, 2) =account_id;
  147. WFIFOB(fd, 6) =flag;
  148. WFIFOSET(fd, 7);
  149. return 0;
  150. }
  151. int32 mapif_delete_pet_ack(int32 fd, int32 flag){
  152. WFIFOHEAD(fd, 3);
  153. WFIFOW(fd, 0) =0x3883;
  154. WFIFOB(fd, 2) =flag;
  155. WFIFOSET(fd, 3);
  156. return 0;
  157. }
  158. int32 mapif_create_pet(int32 fd, uint32 account_id, uint32 char_id, short pet_class, short pet_lv, t_itemid pet_egg_id, t_itemid pet_equip, short intimate, short hungry, char rename_flag, char incubate, char *pet_name)
  159. {
  160. memset(pet_pt, 0, sizeof(struct s_pet));
  161. safestrncpy(pet_pt->name, pet_name, NAME_LENGTH);
  162. if(incubate == 1)
  163. pet_pt->account_id = pet_pt->char_id = 0;
  164. else {
  165. pet_pt->account_id = account_id;
  166. pet_pt->char_id = char_id;
  167. }
  168. pet_pt->class_ = pet_class;
  169. pet_pt->level = pet_lv;
  170. pet_pt->egg_id = pet_egg_id;
  171. pet_pt->equip = pet_equip;
  172. pet_pt->intimate = intimate;
  173. pet_pt->hungry = hungry;
  174. pet_pt->rename_flag = rename_flag;
  175. pet_pt->incubate = incubate;
  176. pet_pt->autofeed = 0;
  177. if(pet_pt->hungry < 0)
  178. pet_pt->hungry = 0;
  179. else if(pet_pt->hungry > 100)
  180. pet_pt->hungry = 100;
  181. if(pet_pt->intimate < 0)
  182. pet_pt->intimate = 0;
  183. else if(pet_pt->intimate > 1000)
  184. pet_pt->intimate = 1000;
  185. pet_pt->pet_id = -1; //Signal NEW pet.
  186. if (inter_pet_tosql(pet_pt->pet_id,pet_pt)){
  187. if( pet_pt->incubate ){
  188. mapif_pet_created(fd, account_id, pet_pt);
  189. }else{
  190. mapif_load_pet(fd, account_id, char_id, pet_pt->pet_id);
  191. }
  192. }else //Failed...
  193. mapif_pet_created(fd, account_id, nullptr);
  194. return 0;
  195. }
  196. int32 mapif_load_pet(int32 fd, uint32 account_id, uint32 char_id, int32 pet_id){
  197. memset(pet_pt, 0, sizeof(struct s_pet));
  198. inter_pet_fromsql(pet_id, pet_pt);
  199. if(pet_pt!=nullptr) {
  200. if(pet_pt->incubate == 1) {
  201. pet_pt->account_id = pet_pt->char_id = 0;
  202. mapif_pet_info(fd, account_id, pet_pt);
  203. }
  204. else if(account_id == pet_pt->account_id && char_id == pet_pt->char_id)
  205. mapif_pet_info(fd, account_id, pet_pt);
  206. else
  207. mapif_pet_noinfo(fd, account_id);
  208. }
  209. else
  210. mapif_pet_noinfo(fd, account_id);
  211. return 0;
  212. }
  213. int32 mapif_save_pet(int32 fd, uint32 account_id, struct s_pet *data) {
  214. //here process pet save request.
  215. int32 len;
  216. len=RFIFOW(fd, 2);
  217. if(sizeof(struct s_pet)!=len-8) {
  218. ShowError("inter pet: data size error %" PRIuPTR " %d\n", sizeof(struct s_pet), len-8);
  219. }
  220. else{
  221. if(data->hungry < 0)
  222. data->hungry = 0;
  223. else if(data->hungry > 100)
  224. data->hungry = 100;
  225. if(data->intimate < 0)
  226. data->intimate = 0;
  227. else if(data->intimate > 1000)
  228. data->intimate = 1000;
  229. inter_pet_tosql(data->pet_id,data);
  230. mapif_save_pet_ack(fd, account_id, 0);
  231. }
  232. return 0;
  233. }
  234. int32 mapif_delete_pet(int32 fd, int32 pet_id){
  235. mapif_delete_pet_ack(fd, inter_pet_delete(pet_id));
  236. return 0;
  237. }
  238. int32 mapif_parse_CreatePet(int32 fd){
  239. mapif_create_pet(fd, RFIFOL(fd, 2), RFIFOL(fd, 6), RFIFOW(fd, 10), RFIFOW(fd, 12), RFIFOL(fd, 14), RFIFOL(fd, 18), RFIFOW(fd, 22),
  240. RFIFOW(fd, 24), RFIFOB(fd, 26), RFIFOB(fd, 27), RFIFOCP(fd, 28));
  241. return 0;
  242. }
  243. int32 mapif_parse_LoadPet(int32 fd){
  244. mapif_load_pet(fd, RFIFOL(fd, 2), RFIFOL(fd, 6), RFIFOL(fd, 10));
  245. return 0;
  246. }
  247. int32 mapif_parse_SavePet(int32 fd){
  248. mapif_save_pet(fd, RFIFOL(fd, 4), (struct s_pet *) RFIFOP(fd, 8));
  249. return 0;
  250. }
  251. int32 mapif_parse_DeletePet(int32 fd){
  252. mapif_delete_pet(fd, RFIFOL(fd, 2));
  253. return 0;
  254. }
  255. int32 inter_pet_parse_frommap(int32 fd){
  256. switch(RFIFOW(fd, 0)){
  257. case 0x3080: mapif_parse_CreatePet(fd); break;
  258. case 0x3081: mapif_parse_LoadPet(fd); break;
  259. case 0x3082: mapif_parse_SavePet(fd); break;
  260. case 0x3083: mapif_parse_DeletePet(fd); break;
  261. default:
  262. return 0;
  263. }
  264. return 1;
  265. }