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