int_mercenary.c 6.3 KB

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