int_storage.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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/showmsg.h"
  6. #include "../common/socket.h"
  7. #include "../common/strlib.h" // StringBuf
  8. #include "../common/sql.h"
  9. #include "char.h"
  10. #include "inter.h"
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #define STORAGE_MEMINC 16
  15. /// Save guild_storage data to sql
  16. int storage_tosql(int account_id, struct storage_data* p)
  17. {
  18. memitemdata_to_sql(p->items, MAX_STORAGE, account_id, TABLE_STORAGE);
  19. return 0;
  20. }
  21. #ifndef TXT_SQL_CONVERT
  22. /// Load guild_storage data to mem
  23. int storage_fromsql(int account_id, struct storage_data* p)
  24. {
  25. StringBuf buf;
  26. struct item* item;
  27. char* data;
  28. int i;
  29. int j;
  30. memset(p, 0, sizeof(struct storage_data)); //clean up memory
  31. p->storage_amount = 0;
  32. // storage {`account_id`/`id`/`nameid`/`amount`/`equip`/`identify`/`refine`/`attribute`/`card0`/`card1`/`card2`/`card3`}
  33. StringBuf_Init(&buf);
  34. StringBuf_AppendStr(&buf, "SELECT `id`,`nameid`,`amount`,`equip`,`identify`,`refine`,`attribute`");
  35. for( j = 0; j < MAX_SLOTS; ++j )
  36. StringBuf_Printf(&buf, ",`card%d`", j);
  37. StringBuf_Printf(&buf, " FROM `%s` WHERE `account_id`='%d' ORDER BY `nameid`", storage_db, account_id);
  38. if( SQL_ERROR == Sql_Query(sql_handle, StringBuf_Value(&buf)) )
  39. Sql_ShowDebug(sql_handle);
  40. StringBuf_Destroy(&buf);
  41. for( i = 0; i < MAX_STORAGE && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  42. {
  43. item = &p->items[i];
  44. Sql_GetData(sql_handle, 0, &data, NULL); item->id = atoi(data);
  45. Sql_GetData(sql_handle, 1, &data, NULL); item->nameid = atoi(data);
  46. Sql_GetData(sql_handle, 2, &data, NULL); item->amount = atoi(data);
  47. Sql_GetData(sql_handle, 3, &data, NULL); item->equip = atoi(data);
  48. Sql_GetData(sql_handle, 4, &data, NULL); item->identify = atoi(data);
  49. Sql_GetData(sql_handle, 5, &data, NULL); item->refine = atoi(data);
  50. Sql_GetData(sql_handle, 6, &data, NULL); item->attribute = atoi(data);
  51. for( j = 0; j < MAX_SLOTS; ++j )
  52. {
  53. Sql_GetData(sql_handle, 7+j, &data, NULL); item->card[j] = atoi(data);
  54. }
  55. }
  56. p->storage_amount = i;
  57. Sql_FreeResult(sql_handle);
  58. ShowInfo("storage load complete from DB - id: %d (total: %d)\n", account_id, p->storage_amount);
  59. return 1;
  60. }
  61. #endif //TXT_SQL_CONVERT
  62. /// Save guild_storage data to sql
  63. int guild_storage_tosql(int guild_id, struct guild_storage* p)
  64. {
  65. memitemdata_to_sql(p->storage_, MAX_GUILD_STORAGE, guild_id, TABLE_GUILD_STORAGE);
  66. ShowInfo ("guild storage save to DB - guild: %d\n", guild_id);
  67. return 0;
  68. }
  69. #ifndef TXT_SQL_CONVERT
  70. /// Load guild_storage data to mem
  71. int guild_storage_fromsql(int guild_id, struct guild_storage* p)
  72. {
  73. StringBuf buf;
  74. struct item* item;
  75. char* data;
  76. int i;
  77. int j;
  78. memset(p, 0, sizeof(struct guild_storage)); //clean up memory
  79. p->storage_amount = 0;
  80. p->guild_id = guild_id;
  81. // storage {`guild_id`/`id`/`nameid`/`amount`/`equip`/`identify`/`refine`/`attribute`/`card0`/`card1`/`card2`/`card3`}
  82. StringBuf_Init(&buf);
  83. StringBuf_AppendStr(&buf, "SELECT `id`,`nameid`,`amount`,`equip`,`identify`,`refine`,`attribute`");
  84. for( j = 0; j < MAX_SLOTS; ++j )
  85. StringBuf_Printf(&buf, ",`card%d`", j);
  86. StringBuf_Printf(&buf, " FROM `%s` WHERE `guild_id`='%d' ORDER BY `nameid`", guild_storage_db, guild_id);
  87. if( SQL_ERROR == Sql_Query(sql_handle, StringBuf_Value(&buf)) )
  88. Sql_ShowDebug(sql_handle);
  89. StringBuf_Destroy(&buf);
  90. for( i = 0; i < MAX_GUILD_STORAGE && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  91. {
  92. item = &p->storage_[i];
  93. Sql_GetData(sql_handle, 0, &data, NULL); item->id = atoi(data);
  94. Sql_GetData(sql_handle, 1, &data, NULL); item->nameid = atoi(data);
  95. Sql_GetData(sql_handle, 2, &data, NULL); item->amount = atoi(data);
  96. Sql_GetData(sql_handle, 3, &data, NULL); item->equip = atoi(data);
  97. Sql_GetData(sql_handle, 4, &data, NULL); item->identify = atoi(data);
  98. Sql_GetData(sql_handle, 5, &data, NULL); item->refine = atoi(data);
  99. Sql_GetData(sql_handle, 6, &data, NULL); item->attribute = atoi(data);
  100. for( j = 0; j < MAX_SLOTS; ++j )
  101. {
  102. Sql_GetData(sql_handle, 7+j, &data, NULL); item->card[j] = atoi(data);
  103. }
  104. }
  105. p->storage_amount = i;
  106. Sql_FreeResult(sql_handle);
  107. ShowInfo("guild storage load complete from DB - id: %d (total: %d)\n", guild_id, p->storage_amount);
  108. return 0;
  109. }
  110. //---------------------------------------------------------
  111. // storage data initialize
  112. int inter_storage_sql_init(void)
  113. {
  114. return 1;
  115. }
  116. // storage data finalize
  117. void inter_storage_sql_final(void)
  118. {
  119. return;
  120. }
  121. // q?f[^?
  122. int inter_storage_delete(int account_id)
  123. {
  124. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `account_id`='%d'", storage_db, account_id) )
  125. Sql_ShowDebug(sql_handle);
  126. return 0;
  127. }
  128. int inter_guild_storage_delete(int guild_id)
  129. {
  130. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_id`='%d'", guild_storage_db, guild_id) )
  131. Sql_ShowDebug(sql_handle);
  132. return 0;
  133. }
  134. //---------------------------------------------------------
  135. // packet from map server
  136. int mapif_load_guild_storage(int fd,int account_id,int guild_id)
  137. {
  138. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id` FROM `%s` WHERE `guild_id`='%d'", guild_db, guild_id) )
  139. Sql_ShowDebug(sql_handle);
  140. else if( Sql_NumRows(sql_handle) > 0 )
  141. {// guild exists
  142. WFIFOHEAD(fd, sizeof(struct guild_storage)+12);
  143. WFIFOW(fd,0) = 0x3818;
  144. WFIFOW(fd,2) = sizeof(struct guild_storage)+12;
  145. WFIFOL(fd,4) = account_id;
  146. WFIFOL(fd,8) = guild_id;
  147. guild_storage_fromsql(guild_id, (struct guild_storage*)WFIFOP(fd,12));
  148. WFIFOSET(fd, WFIFOW(fd,2));
  149. return 0;
  150. }
  151. // guild does not exist
  152. Sql_FreeResult(sql_handle);
  153. WFIFOHEAD(fd, 12);
  154. WFIFOW(fd,0) = 0x3818;
  155. WFIFOW(fd,2) = 12;
  156. WFIFOL(fd,4) = account_id;
  157. WFIFOL(fd,8) = 0;
  158. WFIFOSET(fd, 12);
  159. return 0;
  160. }
  161. int mapif_save_guild_storage_ack(int fd,int account_id,int guild_id,int fail)
  162. {
  163. WFIFOHEAD(fd,11);
  164. WFIFOW(fd,0)=0x3819;
  165. WFIFOL(fd,2)=account_id;
  166. WFIFOL(fd,6)=guild_id;
  167. WFIFOB(fd,10)=fail;
  168. WFIFOSET(fd,11);
  169. return 0;
  170. }
  171. //---------------------------------------------------------
  172. // packet from map server
  173. int mapif_parse_LoadGuildStorage(int fd)
  174. {
  175. RFIFOHEAD(fd);
  176. mapif_load_guild_storage(fd,RFIFOL(fd,2),RFIFOL(fd,6));
  177. return 0;
  178. }
  179. int mapif_parse_SaveGuildStorage(int fd)
  180. {
  181. int guild_id;
  182. int len;
  183. RFIFOHEAD(fd);
  184. guild_id = RFIFOL(fd,8);
  185. len = RFIFOW(fd,2);
  186. if( sizeof(struct guild_storage) != len - 12 )
  187. {
  188. ShowError("inter storage: data size error %d != %d\n", sizeof(struct guild_storage), len - 12);
  189. }
  190. else
  191. {
  192. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id` FROM `%s` WHERE `guild_id`='%d'", guild_db, guild_id) )
  193. Sql_ShowDebug(sql_handle);
  194. else if( Sql_NumRows(sql_handle) > 0 )
  195. {// guild exists
  196. Sql_FreeResult(sql_handle);
  197. guild_storage_tosql(guild_id, (struct guild_storage*)RFIFOP(fd,12));
  198. mapif_save_guild_storage_ack(fd, RFIFOL(fd,4), guild_id, 0);
  199. return 0;
  200. }
  201. Sql_FreeResult(sql_handle);
  202. }
  203. mapif_save_guild_storage_ack(fd, RFIFOL(fd,4), guild_id, 1);
  204. return 0;
  205. }
  206. int inter_storage_parse_frommap(int fd)
  207. {
  208. RFIFOHEAD(fd);
  209. switch(RFIFOW(fd,0)){
  210. case 0x3018: mapif_parse_LoadGuildStorage(fd); break;
  211. case 0x3019: mapif_parse_SaveGuildStorage(fd); break;
  212. default:
  213. return 0;
  214. }
  215. return 1;
  216. }
  217. #endif //TXT_SQL_CONVERT