int_storage.c 7.1 KB

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