int_mercenary.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "int_mercenary.hpp"
  4. #include <cstdlib>
  5. #include <cstring>
  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 "char.hpp"
  12. #include "inter.hpp"
  13. bool mercenary_owner_fromsql(uint32 char_id, struct mmo_charstatus *status)
  14. {
  15. char* data;
  16. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `merc_id`, `arch_calls`, `arch_faith`, `spear_calls`, `spear_faith`, `sword_calls`, `sword_faith` FROM `%s` WHERE `char_id` = '%d'", schema_config.mercenary_owner_db, char_id) )
  17. {
  18. Sql_ShowDebug(sql_handle);
  19. return false;
  20. }
  21. if( SQL_SUCCESS != Sql_NextRow(sql_handle) )
  22. {
  23. Sql_FreeResult(sql_handle);
  24. return false;
  25. }
  26. Sql_GetData(sql_handle, 0, &data, nullptr); status->mer_id = atoi(data);
  27. Sql_GetData(sql_handle, 1, &data, nullptr); status->arch_calls = atoi(data);
  28. Sql_GetData(sql_handle, 2, &data, nullptr); status->arch_faith = atoi(data);
  29. Sql_GetData(sql_handle, 3, &data, nullptr); status->spear_calls = atoi(data);
  30. Sql_GetData(sql_handle, 4, &data, nullptr); status->spear_faith = atoi(data);
  31. Sql_GetData(sql_handle, 5, &data, nullptr); status->sword_calls = atoi(data);
  32. Sql_GetData(sql_handle, 6, &data, nullptr); status->sword_faith = atoi(data);
  33. Sql_FreeResult(sql_handle);
  34. return true;
  35. }
  36. bool mercenary_owner_tosql(uint32 char_id, struct mmo_charstatus *status)
  37. {
  38. if( SQL_ERROR == Sql_Query(sql_handle, "REPLACE INTO `%s` (`char_id`, `merc_id`, `arch_calls`, `arch_faith`, `spear_calls`, `spear_faith`, `sword_calls`, `sword_faith`) VALUES ('%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')",
  39. schema_config.mercenary_owner_db, char_id, status->mer_id, status->arch_calls, status->arch_faith, status->spear_calls, status->spear_faith, status->sword_calls, status->sword_faith) )
  40. {
  41. Sql_ShowDebug(sql_handle);
  42. return false;
  43. }
  44. return true;
  45. }
  46. bool mercenary_owner_delete(uint32 char_id)
  47. {
  48. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id` = '%d'", schema_config.mercenary_owner_db, char_id) )
  49. Sql_ShowDebug(sql_handle);
  50. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id` = '%d'", schema_config.mercenary_db, char_id) )
  51. Sql_ShowDebug(sql_handle);
  52. return true;
  53. }
  54. bool mapif_mercenary_save(struct s_mercenary* merc)
  55. {
  56. bool flag = true;
  57. if( merc->mercenary_id == 0 )
  58. { // Create new DB entry
  59. if( SQL_ERROR == Sql_Query(sql_handle,
  60. "INSERT INTO `%s` (`char_id`,`class`,`hp`,`sp`,`kill_counter`,`life_time`) VALUES ('%d','%d','%u','%u','%u','%" PRtf "')",
  61. schema_config.mercenary_db, merc->char_id, merc->class_, merc->hp, merc->sp, merc->kill_count, merc->life_time) )
  62. {
  63. Sql_ShowDebug(sql_handle);
  64. flag = false;
  65. }
  66. else
  67. merc->mercenary_id = (int32)Sql_LastInsertId(sql_handle);
  68. }
  69. else if( SQL_ERROR == Sql_Query(sql_handle,
  70. "UPDATE `%s` SET `char_id` = '%d', `class` = '%d', `hp` = '%u', `sp` = '%u', `kill_counter` = '%u', `life_time` = '%" PRtf "' WHERE `mer_id` = '%d'",
  71. schema_config.mercenary_db, merc->char_id, merc->class_, merc->hp, merc->sp, merc->kill_count, merc->life_time, merc->mercenary_id) )
  72. { // Update DB entry
  73. Sql_ShowDebug(sql_handle);
  74. flag = false;
  75. }
  76. return flag;
  77. }
  78. bool mapif_mercenary_load(int32 merc_id, uint32 char_id, struct s_mercenary *merc)
  79. {
  80. char* data;
  81. memset(merc, 0, sizeof(struct s_mercenary));
  82. merc->mercenary_id = merc_id;
  83. merc->char_id = char_id;
  84. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `class`, `hp`, `sp`, `kill_counter`, `life_time` FROM `%s` WHERE `mer_id` = '%d' AND `char_id` = '%d'", schema_config.mercenary_db, merc_id, char_id) )
  85. {
  86. Sql_ShowDebug(sql_handle);
  87. return false;
  88. }
  89. if( SQL_SUCCESS != Sql_NextRow(sql_handle) )
  90. {
  91. Sql_FreeResult(sql_handle);
  92. return false;
  93. }
  94. Sql_GetData(sql_handle, 0, &data, nullptr); merc->class_ = atoi(data);
  95. Sql_GetData(sql_handle, 1, &data, nullptr); merc->hp = atoi(data);
  96. Sql_GetData(sql_handle, 2, &data, nullptr); merc->sp = atoi(data);
  97. Sql_GetData(sql_handle, 3, &data, nullptr); merc->kill_count = atoi(data);
  98. Sql_GetData(sql_handle, 4, &data, nullptr); merc->life_time = strtoll( data, nullptr, 10 );
  99. Sql_FreeResult(sql_handle);
  100. if( charserv_config.save_log )
  101. ShowInfo("Mercenary loaded (ID: %d / Class: %d / CID: %d).\n", merc->mercenary_id, merc->class_, merc->char_id);
  102. return true;
  103. }
  104. bool mapif_mercenary_delete(int32 merc_id)
  105. {
  106. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `mer_id` = '%d'", schema_config.mercenary_db, merc_id) )
  107. {
  108. Sql_ShowDebug(sql_handle);
  109. return false;
  110. }
  111. return true;
  112. }
  113. void mapif_mercenary_send(int32 fd, struct s_mercenary *merc, unsigned char flag)
  114. {
  115. int32 size = sizeof(struct s_mercenary) + 5;
  116. WFIFOHEAD(fd,size);
  117. WFIFOW(fd,0) = 0x3870;
  118. WFIFOW(fd,2) = size;
  119. WFIFOB(fd,4) = flag;
  120. memcpy(WFIFOP(fd,5),merc,sizeof(struct s_mercenary));
  121. WFIFOSET(fd,size);
  122. }
  123. void mapif_parse_mercenary_create(int32 fd, struct s_mercenary* merc)
  124. {
  125. bool result = mapif_mercenary_save(merc);
  126. mapif_mercenary_send(fd, merc, result);
  127. }
  128. void mapif_parse_mercenary_load(int32 fd, int32 merc_id, uint32 char_id)
  129. {
  130. struct s_mercenary merc;
  131. bool result = mapif_mercenary_load(merc_id, char_id, &merc);
  132. mapif_mercenary_send(fd, &merc, result);
  133. }
  134. void mapif_mercenary_deleted(int32 fd, unsigned char flag)
  135. {
  136. WFIFOHEAD(fd,3);
  137. WFIFOW(fd,0) = 0x3871;
  138. WFIFOB(fd,2) = flag;
  139. WFIFOSET(fd,3);
  140. }
  141. void mapif_parse_mercenary_delete(int32 fd, int32 merc_id)
  142. {
  143. bool result = mapif_mercenary_delete(merc_id);
  144. mapif_mercenary_deleted(fd, result);
  145. }
  146. void mapif_mercenary_saved(int32 fd, unsigned char flag)
  147. {
  148. WFIFOHEAD(fd,3);
  149. WFIFOW(fd,0) = 0x3872;
  150. WFIFOB(fd,2) = flag;
  151. WFIFOSET(fd,3);
  152. }
  153. void mapif_parse_mercenary_save(int32 fd, struct s_mercenary* merc)
  154. {
  155. bool result = mapif_mercenary_save(merc);
  156. mapif_mercenary_saved(fd, result);
  157. }
  158. int32 inter_mercenary_sql_init(void)
  159. {
  160. return 0;
  161. }
  162. void inter_mercenary_sql_final(void)
  163. {
  164. return;
  165. }
  166. /*==========================================
  167. * Inter Packets
  168. *------------------------------------------*/
  169. int32 inter_mercenary_parse_frommap(int32 fd)
  170. {
  171. unsigned short cmd = RFIFOW(fd,0);
  172. switch( cmd )
  173. {
  174. case 0x3070: mapif_parse_mercenary_create(fd, (struct s_mercenary*)RFIFOP(fd,4)); break;
  175. case 0x3071: mapif_parse_mercenary_load(fd, (int32)RFIFOL(fd,2), (int32)RFIFOL(fd,6)); break;
  176. case 0x3072: mapif_parse_mercenary_delete(fd, (int32)RFIFOL(fd,2)); break;
  177. case 0x3073: mapif_parse_mercenary_save(fd, (struct s_mercenary*)RFIFOP(fd,4)); break;
  178. default:
  179. return 0;
  180. }
  181. return 1;
  182. }