int_storage.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. #ifndef TXT_SQL_CONVERT
  16. // reset by inter_config_read()
  17. struct storage *storage_pt=NULL;
  18. struct guild_storage *guild_storage_pt=NULL;
  19. #endif //TXT_SQL_CONVERT
  20. /// Save guild_storage data to sql
  21. int storage_tosql(int account_id, struct storage* p)
  22. {
  23. memitemdata_to_sql(p->storage_, MAX_STORAGE, account_id, TABLE_STORAGE);
  24. //ShowInfo ("storage save to DB - account: %d\n", account_id);
  25. return 0;
  26. }
  27. #ifndef TXT_SQL_CONVERT
  28. /// Load guild_storage data to mem
  29. int storage_fromsql(int account_id, struct storage* p)
  30. {
  31. StringBuf buf;
  32. struct item* item;
  33. char* data;
  34. int i;
  35. int j;
  36. memset(p, 0, sizeof(struct storage)); //clean up memory
  37. p->storage_amount = 0;
  38. p->account_id = account_id;
  39. // storage {`account_id`/`id`/`nameid`/`amount`/`equip`/`identify`/`refine`/`attribute`/`card0`/`card1`/`card2`/`card3`}
  40. StringBuf_Init(&buf);
  41. StringBuf_AppendStr(&buf, "SELECT `id`,`nameid`,`amount`,`equip`,`identify`,`refine`,`attribute`");
  42. for( j = 0; j < MAX_SLOTS; ++j )
  43. StringBuf_Printf(&buf, ",`card%d`", j);
  44. StringBuf_Printf(&buf, " FROM `%s` WHERE `account_id`='%d' ORDER BY `nameid`", storage_db, account_id);
  45. if( SQL_ERROR == Sql_Query(sql_handle, StringBuf_Value(&buf)) )
  46. Sql_ShowDebug(sql_handle);
  47. StringBuf_Destroy(&buf);
  48. for( i = 0; i < MAX_STORAGE && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  49. {
  50. item = &p->storage_[i];
  51. Sql_GetData(sql_handle, 0, &data, NULL); item->id = atoi(data);
  52. Sql_GetData(sql_handle, 1, &data, NULL); item->nameid = atoi(data);
  53. Sql_GetData(sql_handle, 2, &data, NULL); item->amount = atoi(data);
  54. Sql_GetData(sql_handle, 3, &data, NULL); item->equip = atoi(data);
  55. Sql_GetData(sql_handle, 4, &data, NULL); item->identify = atoi(data);
  56. Sql_GetData(sql_handle, 5, &data, NULL); item->refine = atoi(data);
  57. Sql_GetData(sql_handle, 6, &data, NULL); item->attribute = atoi(data);
  58. for( j = 0; j < MAX_SLOTS; ++j )
  59. {
  60. Sql_GetData(sql_handle, 7+j, &data, NULL); item->card[j] = atoi(data);
  61. }
  62. }
  63. p->storage_amount = i;
  64. Sql_FreeResult(sql_handle);
  65. ShowInfo("storage load complete from DB - id: %d (total: %d)\n", account_id, p->storage_amount);
  66. return 1;
  67. }
  68. #endif //TXT_SQL_CONVERT
  69. /// Save guild_storage data to sql
  70. int guild_storage_tosql(int guild_id, struct guild_storage* p)
  71. {
  72. memitemdata_to_sql(p->storage_, MAX_GUILD_STORAGE, guild_id, TABLE_GUILD_STORAGE);
  73. ShowInfo ("guild storage save to DB - guild: %d\n", guild_id);
  74. return 0;
  75. }
  76. #ifndef TXT_SQL_CONVERT
  77. /// Load guild_storage data to mem
  78. int guild_storage_fromsql(int guild_id, struct guild_storage* p)
  79. {
  80. StringBuf buf;
  81. struct item* item;
  82. char* data;
  83. int i;
  84. int j;
  85. memset(p, 0, sizeof(struct guild_storage)); //clean up memory
  86. p->storage_amount = 0;
  87. p->guild_id = guild_id;
  88. // storage {`guild_id`/`id`/`nameid`/`amount`/`equip`/`identify`/`refine`/`attribute`/`card0`/`card1`/`card2`/`card3`}
  89. StringBuf_Init(&buf);
  90. StringBuf_AppendStr(&buf, "SELECT `id`,`nameid`,`amount`,`equip`,`identify`,`refine`,`attribute`");
  91. for( j = 0; j < MAX_SLOTS; ++j )
  92. StringBuf_Printf(&buf, ",`card%d`", j);
  93. StringBuf_Printf(&buf, " FROM `%s` WHERE `guild_id`='%d' ORDER BY `nameid`", guild_storage_db, guild_id);
  94. if( SQL_ERROR == Sql_Query(sql_handle, StringBuf_Value(&buf)) )
  95. Sql_ShowDebug(sql_handle);
  96. StringBuf_Destroy(&buf);
  97. for( i = 0; i < MAX_GUILD_STORAGE && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  98. {
  99. item = &p->storage_[i];
  100. Sql_GetData(sql_handle, 0, &data, NULL); item->id = atoi(data);
  101. Sql_GetData(sql_handle, 1, &data, NULL); item->nameid = atoi(data);
  102. Sql_GetData(sql_handle, 2, &data, NULL); item->amount = atoi(data);
  103. Sql_GetData(sql_handle, 3, &data, NULL); item->equip = atoi(data);
  104. Sql_GetData(sql_handle, 4, &data, NULL); item->identify = atoi(data);
  105. Sql_GetData(sql_handle, 5, &data, NULL); item->refine = atoi(data);
  106. Sql_GetData(sql_handle, 6, &data, NULL); item->attribute = atoi(data);
  107. for( j = 0; j < MAX_SLOTS; ++j )
  108. {
  109. Sql_GetData(sql_handle, 7+j, &data, NULL); item->card[j] = atoi(data);
  110. }
  111. }
  112. p->storage_amount = i;
  113. Sql_FreeResult(sql_handle);
  114. ShowInfo("guild storage load complete from DB - id: %d (total: %d)\n", guild_id, p->storage_amount);
  115. return 0;
  116. }
  117. //---------------------------------------------------------
  118. // storage data initialize
  119. int inter_storage_sql_init(void)
  120. {
  121. //memory alloc
  122. ShowDebug("interserver storage memory initialize....(%d byte)\n",sizeof(struct storage));
  123. storage_pt = (struct storage*)aCalloc(sizeof(struct storage), 1);
  124. guild_storage_pt = (struct guild_storage*)aCalloc(sizeof(struct guild_storage), 1);
  125. return 1;
  126. }
  127. // storage data finalize
  128. void inter_storage_sql_final(void)
  129. {
  130. if (storage_pt) aFree(storage_pt);
  131. if (guild_storage_pt) aFree(guild_storage_pt);
  132. return;
  133. }
  134. // q?f[^?
  135. int inter_storage_delete(int account_id)
  136. {
  137. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `account_id`='%d'", storage_db, account_id) )
  138. Sql_ShowDebug(sql_handle);
  139. return 0;
  140. }
  141. int inter_guild_storage_delete(int guild_id)
  142. {
  143. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_id`='%d'", guild_storage_db, guild_id) )
  144. Sql_ShowDebug(sql_handle);
  145. return 0;
  146. }
  147. //---------------------------------------------------------
  148. // packet from map server
  149. // recive packet about storage data
  150. int mapif_load_storage(int fd,int account_id)
  151. {
  152. //load from DB
  153. WFIFOHEAD(fd, sizeof(struct storage)+8);
  154. storage_fromsql(account_id, storage_pt);
  155. WFIFOW(fd,0)=0x3810;
  156. WFIFOW(fd,2)=sizeof(struct storage)+8;
  157. WFIFOL(fd,4)=account_id;
  158. memcpy(WFIFOP(fd,8),storage_pt,sizeof(struct storage));
  159. WFIFOSET(fd,WFIFOW(fd,2));
  160. return 0;
  161. }
  162. // send ack to map server which is "storage data save ok."
  163. int mapif_save_storage_ack(int fd,int account_id)
  164. {
  165. WFIFOHEAD(fd, 7);
  166. WFIFOW(fd,0)=0x3811;
  167. WFIFOL(fd,2)=account_id;
  168. WFIFOB(fd,6)=0;
  169. WFIFOSET(fd,7);
  170. return 0;
  171. }
  172. int mapif_load_guild_storage(int fd,int account_id,int guild_id)
  173. {
  174. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id` FROM `%s` WHERE `guild_id`='%d'", guild_db, guild_id) )
  175. Sql_ShowDebug(sql_handle);
  176. else if( Sql_NumRows(sql_handle) > 0 )
  177. {// guild exists
  178. guild_storage_fromsql(guild_id, guild_storage_pt);
  179. WFIFOHEAD(fd, sizeof(struct guild_storage)+12);
  180. WFIFOW(fd,0) = 0x3818;
  181. WFIFOW(fd,2) = sizeof(struct guild_storage)+12;
  182. WFIFOL(fd,4) = account_id;
  183. WFIFOL(fd,8) = guild_id;
  184. memcpy(WFIFOP(fd,12), guild_storage_pt, sizeof(struct guild_storage));
  185. WFIFOSET(fd, WFIFOW(fd,2));
  186. return 0;
  187. }
  188. // guild does not exist
  189. Sql_FreeResult(sql_handle);
  190. WFIFOHEAD(fd, 12);
  191. WFIFOW(fd,0) = 0x3818;
  192. WFIFOW(fd,2) = 12;
  193. WFIFOL(fd,4) = account_id;
  194. WFIFOL(fd,8) = 0;
  195. WFIFOSET(fd, 12);
  196. return 0;
  197. }
  198. int mapif_save_guild_storage_ack(int fd,int account_id,int guild_id,int fail)
  199. {
  200. WFIFOHEAD(fd,11);
  201. WFIFOW(fd,0)=0x3819;
  202. WFIFOL(fd,2)=account_id;
  203. WFIFOL(fd,6)=guild_id;
  204. WFIFOB(fd,10)=fail;
  205. WFIFOSET(fd,11);
  206. return 0;
  207. }
  208. //---------------------------------------------------------
  209. // packet from map server
  210. // recive request about storage data
  211. int mapif_parse_LoadStorage(int fd)
  212. {
  213. RFIFOHEAD(fd);
  214. mapif_load_storage(fd,RFIFOL(fd,2));
  215. return 0;
  216. }
  217. // storage data recive and save
  218. int mapif_parse_SaveStorage(int fd)
  219. {
  220. int account_id;
  221. int len;
  222. RFIFOHEAD(fd);
  223. account_id=RFIFOL(fd,4);
  224. len=RFIFOW(fd,2);
  225. if(sizeof(struct storage)!=len-8){
  226. ShowError("inter storage: data size error %d %d\n",sizeof(struct storage),len-8);
  227. }else{
  228. memcpy(&storage_pt[0],RFIFOP(fd,8),sizeof(struct storage));
  229. storage_tosql(account_id, storage_pt);
  230. mapif_save_storage_ack(fd,account_id);
  231. }
  232. return 0;
  233. }
  234. int mapif_parse_LoadGuildStorage(int fd)
  235. {
  236. RFIFOHEAD(fd);
  237. mapif_load_guild_storage(fd,RFIFOL(fd,2),RFIFOL(fd,6));
  238. return 0;
  239. }
  240. int mapif_parse_SaveGuildStorage(int fd)
  241. {
  242. int guild_id;
  243. int len;
  244. RFIFOHEAD(fd);
  245. guild_id = RFIFOL(fd,8);
  246. len = RFIFOW(fd,2);
  247. if( sizeof(struct guild_storage) != len - 12 )
  248. {
  249. ShowError("inter storage: data size error %d != %d\n", sizeof(struct guild_storage), len - 12);
  250. }
  251. else
  252. {
  253. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id` FROM `%s` WHERE `guild_id`='%d'", guild_db, guild_id) )
  254. Sql_ShowDebug(sql_handle);
  255. else if( Sql_NumRows(sql_handle) > 0 )
  256. {// guild exists
  257. Sql_FreeResult(sql_handle);
  258. memcpy(guild_storage_pt, RFIFOP(fd,12), sizeof(struct guild_storage));
  259. guild_storage_tosql(guild_id, guild_storage_pt);
  260. mapif_save_guild_storage_ack(fd, RFIFOL(fd,4), guild_id, 0);
  261. return 0;
  262. }
  263. Sql_FreeResult(sql_handle);
  264. }
  265. mapif_save_guild_storage_ack(fd, RFIFOL(fd,4), guild_id, 1);
  266. return 0;
  267. }
  268. int inter_storage_parse_frommap(int fd)
  269. {
  270. RFIFOHEAD(fd);
  271. switch(RFIFOW(fd,0)){
  272. case 0x3010: mapif_parse_LoadStorage(fd); break;
  273. case 0x3011: mapif_parse_SaveStorage(fd); break;
  274. case 0x3018: mapif_parse_LoadGuildStorage(fd); break;
  275. case 0x3019: mapif_parse_SaveGuildStorage(fd); break;
  276. default:
  277. return 0;
  278. }
  279. return 1;
  280. }
  281. #endif //TXT_SQL_CONVERT