PageRenderTime 27ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/src/char/int_auction.c

https://gitlab.com/evol/hercules
C | 533 lines | 414 code | 94 blank | 25 comment | 102 complexity | a742539aa7e0b126543556bf39cd870c MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.0
  1. /**
  2. * This file is part of Hercules.
  3. * http://herc.ws - http://github.com/HerculesWS/Hercules
  4. *
  5. * Copyright (C) 2012-2015 Hercules Dev Team
  6. * Copyright (C) Athena Dev Teams
  7. *
  8. * Hercules is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #define HERCULES_CORE
  22. #include "int_auction.h"
  23. #include "char/char.h"
  24. #include "char/int_mail.h"
  25. #include "char/inter.h"
  26. #include "char/mapif.h"
  27. #include "common/cbasetypes.h"
  28. #include "common/db.h"
  29. #include "common/memmgr.h"
  30. #include "common/mmo.h"
  31. #include "common/nullpo.h"
  32. #include "common/showmsg.h"
  33. #include "common/socket.h"
  34. #include "common/sql.h"
  35. #include "common/strlib.h"
  36. #include "common/timer.h"
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. struct inter_auction_interface inter_auction_s;
  40. struct inter_auction_interface *inter_auction;
  41. static int inter_auction_count(int char_id, bool buy)
  42. {
  43. int i = 0;
  44. struct auction_data *auction;
  45. DBIterator *iter = db_iterator(inter_auction->db);
  46. for( auction = dbi_first(iter); dbi_exists(iter); auction = dbi_next(iter) )
  47. {
  48. if ((buy && auction->buyer_id == char_id) || (!buy && auction->seller_id == char_id))
  49. i++;
  50. }
  51. dbi_destroy(iter);
  52. return i;
  53. }
  54. void inter_auction_save(struct auction_data *auction)
  55. {
  56. int j;
  57. StringBuf buf;
  58. SqlStmt* stmt;
  59. if( !auction )
  60. return;
  61. StrBuf->Init(&buf);
  62. StrBuf->Printf(&buf, "UPDATE `%s` SET `seller_id` = '%d', `seller_name` = ?, `buyer_id` = '%d', `buyer_name` = ?, `price` = '%d', `buynow` = '%d', `hours` = '%d', `timestamp` = '%lu', `nameid` = '%d', `item_name` = ?, `type` = '%d', `refine` = '%d', `attribute` = '%d'",
  63. auction_db, auction->seller_id, auction->buyer_id, auction->price, auction->buynow, auction->hours, (unsigned long)auction->timestamp, auction->item.nameid, auction->type, auction->item.refine, auction->item.attribute);
  64. for( j = 0; j < MAX_SLOTS; j++ )
  65. StrBuf->Printf(&buf, ", `card%d` = '%d'", j, auction->item.card[j]);
  66. StrBuf->Printf(&buf, " WHERE `auction_id` = '%d'", auction->auction_id);
  67. stmt = SQL->StmtMalloc(inter->sql_handle);
  68. if( SQL_SUCCESS != SQL->StmtPrepareStr(stmt, StrBuf->Value(&buf))
  69. || SQL_SUCCESS != SQL->StmtBindParam(stmt, 0, SQLDT_STRING, auction->seller_name, strnlen(auction->seller_name, NAME_LENGTH))
  70. || SQL_SUCCESS != SQL->StmtBindParam(stmt, 1, SQLDT_STRING, auction->buyer_name, strnlen(auction->buyer_name, NAME_LENGTH))
  71. || SQL_SUCCESS != SQL->StmtBindParam(stmt, 2, SQLDT_STRING, auction->item_name, strnlen(auction->item_name, ITEM_NAME_LENGTH))
  72. || SQL_SUCCESS != SQL->StmtExecute(stmt) )
  73. {
  74. SqlStmt_ShowDebug(stmt);
  75. }
  76. SQL->StmtFree(stmt);
  77. StrBuf->Destroy(&buf);
  78. }
  79. unsigned int inter_auction_create(struct auction_data *auction)
  80. {
  81. int j;
  82. StringBuf buf;
  83. SqlStmt* stmt;
  84. if( !auction )
  85. return false;
  86. auction->timestamp = time(NULL) + (auction->hours * 3600);
  87. StrBuf->Init(&buf);
  88. StrBuf->Printf(&buf, "INSERT INTO `%s` (`seller_id`,`seller_name`,`buyer_id`,`buyer_name`,`price`,`buynow`,`hours`,`timestamp`,`nameid`,`item_name`,`type`,`refine`,`attribute`,`unique_id`", auction_db);
  89. for( j = 0; j < MAX_SLOTS; j++ )
  90. StrBuf->Printf(&buf, ",`card%d`", j);
  91. StrBuf->Printf(&buf, ") VALUES ('%d',?,'%d',?,'%d','%d','%d','%lu','%d',?,'%d','%d','%d','%"PRIu64"'",
  92. auction->seller_id, auction->buyer_id, auction->price, auction->buynow, auction->hours, (unsigned long)auction->timestamp, auction->item.nameid, auction->type, auction->item.refine, auction->item.attribute, auction->item.unique_id);
  93. for( j = 0; j < MAX_SLOTS; j++ )
  94. StrBuf->Printf(&buf, ",'%d'", auction->item.card[j]);
  95. StrBuf->AppendStr(&buf, ")");
  96. stmt = SQL->StmtMalloc(inter->sql_handle);
  97. if( SQL_SUCCESS != SQL->StmtPrepareStr(stmt, StrBuf->Value(&buf))
  98. || SQL_SUCCESS != SQL->StmtBindParam(stmt, 0, SQLDT_STRING, auction->seller_name, strnlen(auction->seller_name, NAME_LENGTH))
  99. || SQL_SUCCESS != SQL->StmtBindParam(stmt, 1, SQLDT_STRING, auction->buyer_name, strnlen(auction->buyer_name, NAME_LENGTH))
  100. || SQL_SUCCESS != SQL->StmtBindParam(stmt, 2, SQLDT_STRING, auction->item_name, strnlen(auction->item_name, ITEM_NAME_LENGTH))
  101. || SQL_SUCCESS != SQL->StmtExecute(stmt) )
  102. {
  103. SqlStmt_ShowDebug(stmt);
  104. auction->auction_id = 0;
  105. }
  106. else
  107. {
  108. struct auction_data *auction_;
  109. int64 tick = (int64)auction->hours * 3600000;
  110. auction->item.amount = 1;
  111. auction->item.identify = 1;
  112. auction->item.expire_time = 0;
  113. auction->auction_id = (unsigned int)SQL->StmtLastInsertId(stmt);
  114. auction->auction_end_timer = timer->add( timer->gettick() + tick , inter_auction->end_timer, auction->auction_id, 0);
  115. ShowInfo("New Auction %u | time left %"PRId64" ms | By %s.\n", auction->auction_id, tick, auction->seller_name);
  116. CREATE(auction_, struct auction_data, 1);
  117. memcpy(auction_, auction, sizeof(struct auction_data));
  118. idb_put(inter_auction->db, auction_->auction_id, auction_);
  119. }
  120. SQL->StmtFree(stmt);
  121. StrBuf->Destroy(&buf);
  122. return auction->auction_id;
  123. }
  124. void mapif_auction_message(int char_id, unsigned char result)
  125. {
  126. unsigned char buf[74];
  127. WBUFW(buf,0) = 0x3854;
  128. WBUFL(buf,2) = char_id;
  129. WBUFL(buf,6) = result;
  130. mapif->sendall(buf,7);
  131. }
  132. static int inter_auction_end_timer(int tid, int64 tick, int id, intptr_t data) {
  133. struct auction_data *auction;
  134. if( (auction = (struct auction_data *)idb_get(inter_auction->db, id)) != NULL )
  135. {
  136. if( auction->buyer_id )
  137. {
  138. inter_mail->sendmail(0, "Auction Manager", auction->buyer_id, auction->buyer_name, "Auction", "Thanks, you won the auction!.", 0, &auction->item);
  139. mapif->auction_message(auction->buyer_id, 6); // You have won the auction
  140. inter_mail->sendmail(0, "Auction Manager", auction->seller_id, auction->seller_name, "Auction", "Payment for your auction!.", auction->price, NULL);
  141. }
  142. else
  143. inter_mail->sendmail(0, "Auction Manager", auction->seller_id, auction->seller_name, "Auction", "No buyers have been found for your auction.", 0, &auction->item);
  144. ShowInfo("Auction End: id %u.\n", auction->auction_id);
  145. auction->auction_end_timer = INVALID_TIMER;
  146. inter_auction->delete_(auction);
  147. }
  148. return 0;
  149. }
  150. void inter_auction_delete(struct auction_data *auction)
  151. {
  152. unsigned int auction_id;
  153. nullpo_retv(auction);
  154. auction_id = auction->auction_id;
  155. if( SQL_ERROR == SQL->Query(inter->sql_handle, "DELETE FROM `%s` WHERE `auction_id` = '%d'", auction_db, auction_id) )
  156. Sql_ShowDebug(inter->sql_handle);
  157. if( auction->auction_end_timer != INVALID_TIMER )
  158. timer->delete(auction->auction_end_timer, inter_auction->end_timer);
  159. idb_remove(inter_auction->db, auction_id);
  160. }
  161. void inter_auctions_fromsql(void)
  162. {
  163. int i;
  164. struct auction_data *auction;
  165. char *data;
  166. StringBuf buf;
  167. int64 tick = timer->gettick(), endtick;
  168. time_t now = time(NULL);
  169. StrBuf->Init(&buf);
  170. StrBuf->AppendStr(&buf, "SELECT `auction_id`,`seller_id`,`seller_name`,`buyer_id`,`buyer_name`,"
  171. "`price`,`buynow`,`hours`,`timestamp`,`nameid`,`item_name`,`type`,`refine`,`attribute`,`unique_id`");
  172. for( i = 0; i < MAX_SLOTS; i++ )
  173. StrBuf->Printf(&buf, ",`card%d`", i);
  174. StrBuf->Printf(&buf, " FROM `%s` ORDER BY `auction_id` DESC", auction_db);
  175. if (SQL_ERROR == SQL->QueryStr(inter->sql_handle, StrBuf->Value(&buf)))
  176. Sql_ShowDebug(inter->sql_handle);
  177. StrBuf->Destroy(&buf);
  178. while (SQL_SUCCESS == SQL->NextRow(inter->sql_handle)) {
  179. struct item *item;
  180. CREATE(auction, struct auction_data, 1);
  181. SQL->GetData(inter->sql_handle, 0, &data, NULL); auction->auction_id = atoi(data);
  182. SQL->GetData(inter->sql_handle, 1, &data, NULL); auction->seller_id = atoi(data);
  183. SQL->GetData(inter->sql_handle, 2, &data, NULL); safestrncpy(auction->seller_name, data, NAME_LENGTH);
  184. SQL->GetData(inter->sql_handle, 3, &data, NULL); auction->buyer_id = atoi(data);
  185. SQL->GetData(inter->sql_handle, 4, &data, NULL); safestrncpy(auction->buyer_name, data, NAME_LENGTH);
  186. SQL->GetData(inter->sql_handle, 5, &data, NULL); auction->price = atoi(data);
  187. SQL->GetData(inter->sql_handle, 6, &data, NULL); auction->buynow = atoi(data);
  188. SQL->GetData(inter->sql_handle, 7, &data, NULL); auction->hours = atoi(data);
  189. SQL->GetData(inter->sql_handle, 8, &data, NULL); auction->timestamp = atoi(data);
  190. item = &auction->item;
  191. SQL->GetData(inter->sql_handle, 9, &data, NULL); item->nameid = atoi(data);
  192. SQL->GetData(inter->sql_handle,10, &data, NULL); safestrncpy(auction->item_name, data, ITEM_NAME_LENGTH);
  193. SQL->GetData(inter->sql_handle,11, &data, NULL); auction->type = atoi(data);
  194. SQL->GetData(inter->sql_handle,12, &data, NULL); item->refine = atoi(data);
  195. SQL->GetData(inter->sql_handle,13, &data, NULL); item->attribute = atoi(data);
  196. SQL->GetData(inter->sql_handle,14, &data, NULL); item->unique_id = strtoull(data, NULL, 10);
  197. item->identify = 1;
  198. item->amount = 1;
  199. item->expire_time = 0;
  200. for( i = 0; i < MAX_SLOTS; i++ )
  201. {
  202. SQL->GetData(inter->sql_handle, 15 + i, &data, NULL);
  203. item->card[i] = atoi(data);
  204. }
  205. if( auction->timestamp > now )
  206. endtick = ((int64)(auction->timestamp - now) * 1000) + tick;
  207. else
  208. endtick = tick + 10000; // 10 seconds to process ended auctions
  209. auction->auction_end_timer = timer->add(endtick, inter_auction->end_timer, auction->auction_id, 0);
  210. idb_put(inter_auction->db, auction->auction_id, auction);
  211. }
  212. SQL->FreeResult(inter->sql_handle);
  213. }
  214. void mapif_auction_sendlist(int fd, int char_id, short count, short pages, unsigned char *buf)
  215. {
  216. int len = (sizeof(struct auction_data) * count) + 12;
  217. nullpo_retv(buf);
  218. WFIFOHEAD(fd, len);
  219. WFIFOW(fd,0) = 0x3850;
  220. WFIFOW(fd,2) = len;
  221. WFIFOL(fd,4) = char_id;
  222. WFIFOW(fd,8) = count;
  223. WFIFOW(fd,10) = pages;
  224. memcpy(WFIFOP(fd,12), buf, len - 12);
  225. WFIFOSET(fd,len);
  226. }
  227. void mapif_parse_auction_requestlist(int fd)
  228. {
  229. char searchtext[NAME_LENGTH];
  230. int char_id = RFIFOL(fd,4), len = sizeof(struct auction_data);
  231. int price = RFIFOL(fd,10);
  232. short type = RFIFOW(fd,8), page = max(1,RFIFOW(fd,14));
  233. unsigned char buf[5 * sizeof(struct auction_data)];
  234. DBIterator *iter = db_iterator(inter_auction->db);
  235. struct auction_data *auction;
  236. short i = 0, j = 0, pages = 1;
  237. memcpy(searchtext, RFIFOP(fd,16), NAME_LENGTH);
  238. for( auction = dbi_first(iter); dbi_exists(iter); auction = dbi_next(iter) )
  239. {
  240. if( (type == 0 && auction->type != IT_ARMOR && auction->type != IT_PETARMOR) ||
  241. (type == 1 && auction->type != IT_WEAPON) ||
  242. (type == 2 && auction->type != IT_CARD) ||
  243. (type == 3 && auction->type != IT_ETC) ||
  244. (type == 4 && !strstr(auction->item_name, searchtext)) ||
  245. (type == 5 && auction->price > price) ||
  246. (type == 6 && auction->seller_id != char_id) ||
  247. (type == 7 && auction->buyer_id != char_id) )
  248. continue;
  249. i++;
  250. if( i > 5 )
  251. { // Counting Pages of Total Results (5 Results per Page)
  252. pages++;
  253. i = 1; // First Result of This Page
  254. }
  255. if( page != pages )
  256. continue; // This is not the requested Page
  257. memcpy(WBUFP(buf, j * len), auction, len);
  258. j++; // Found Results
  259. }
  260. dbi_destroy(iter);
  261. mapif->auction_sendlist(fd, char_id, j, pages, buf);
  262. }
  263. void mapif_auction_register(int fd, struct auction_data *auction)
  264. {
  265. int len = sizeof(struct auction_data) + 4;
  266. nullpo_retv(auction);
  267. WFIFOHEAD(fd,len);
  268. WFIFOW(fd,0) = 0x3851;
  269. WFIFOW(fd,2) = len;
  270. memcpy(WFIFOP(fd,4), auction, sizeof(struct auction_data));
  271. WFIFOSET(fd,len);
  272. }
  273. void mapif_parse_auction_register(int fd)
  274. {
  275. struct auction_data auction;
  276. if( RFIFOW(fd,2) != sizeof(struct auction_data) + 4 )
  277. return;
  278. memcpy(&auction, RFIFOP(fd,4), sizeof(struct auction_data));
  279. if( inter_auction->count(auction.seller_id, false) < 5 )
  280. auction.auction_id = inter_auction->create(&auction);
  281. mapif->auction_register(fd, &auction);
  282. }
  283. void mapif_auction_cancel(int fd, int char_id, unsigned char result)
  284. {
  285. WFIFOHEAD(fd,7);
  286. WFIFOW(fd,0) = 0x3852;
  287. WFIFOL(fd,2) = char_id;
  288. WFIFOB(fd,6) = result;
  289. WFIFOSET(fd,7);
  290. }
  291. void mapif_parse_auction_cancel(int fd)
  292. {
  293. int char_id = RFIFOL(fd,2), auction_id = RFIFOL(fd,6);
  294. struct auction_data *auction;
  295. if( (auction = (struct auction_data *)idb_get(inter_auction->db, auction_id)) == NULL )
  296. {
  297. mapif->auction_cancel(fd, char_id, 1); // Bid Number is Incorrect
  298. return;
  299. }
  300. if( auction->seller_id != char_id )
  301. {
  302. mapif->auction_cancel(fd, char_id, 2); // You cannot end the auction
  303. return;
  304. }
  305. if( auction->buyer_id > 0 )
  306. {
  307. mapif->auction_cancel(fd, char_id, 3); // An auction with at least one bidder cannot be canceled
  308. return;
  309. }
  310. inter_mail->sendmail(0, "Auction Manager", auction->seller_id, auction->seller_name, "Auction", "Auction canceled.", 0, &auction->item);
  311. inter_auction->delete_(auction);
  312. mapif->auction_cancel(fd, char_id, 0); // The auction has been canceled
  313. }
  314. void mapif_auction_close(int fd, int char_id, unsigned char result)
  315. {
  316. WFIFOHEAD(fd,7);
  317. WFIFOW(fd,0) = 0x3853;
  318. WFIFOL(fd,2) = char_id;
  319. WFIFOB(fd,6) = result;
  320. WFIFOSET(fd,7);
  321. }
  322. void mapif_parse_auction_close(int fd)
  323. {
  324. int char_id = RFIFOL(fd,2), auction_id = RFIFOL(fd,6);
  325. struct auction_data *auction;
  326. if( (auction = (struct auction_data *)idb_get(inter_auction->db, auction_id)) == NULL )
  327. {
  328. mapif->auction_close(fd, char_id, 2); // Bid Number is Incorrect
  329. return;
  330. }
  331. if( auction->seller_id != char_id )
  332. {
  333. mapif->auction_close(fd, char_id, 1); // You cannot end the auction
  334. return;
  335. }
  336. if( auction->buyer_id == 0 )
  337. {
  338. mapif->auction_close(fd, char_id, 1); // You cannot end the auction
  339. return;
  340. }
  341. // Send Money to Seller
  342. inter_mail->sendmail(0, "Auction Manager", auction->seller_id, auction->seller_name, "Auction", "Auction closed.", auction->price, NULL);
  343. // Send Item to Buyer
  344. inter_mail->sendmail(0, "Auction Manager", auction->buyer_id, auction->buyer_name, "Auction", "Auction winner.", 0, &auction->item);
  345. mapif->auction_message(auction->buyer_id, 6); // You have won the auction
  346. inter_auction->delete_(auction);
  347. mapif->auction_close(fd, char_id, 0); // You have ended the auction
  348. }
  349. void mapif_auction_bid(int fd, int char_id, int bid, unsigned char result)
  350. {
  351. WFIFOHEAD(fd,11);
  352. WFIFOW(fd,0) = 0x3855;
  353. WFIFOL(fd,2) = char_id;
  354. WFIFOL(fd,6) = bid; // To Return Zeny
  355. WFIFOB(fd,10) = result;
  356. WFIFOSET(fd,11);
  357. }
  358. void mapif_parse_auction_bid(int fd)
  359. {
  360. int char_id = RFIFOL(fd,4), bid = RFIFOL(fd,12);
  361. unsigned int auction_id = RFIFOL(fd,8);
  362. struct auction_data *auction;
  363. if( (auction = (struct auction_data *)idb_get(inter_auction->db, auction_id)) == NULL || auction->price >= bid || auction->seller_id == char_id )
  364. {
  365. mapif->auction_bid(fd, char_id, bid, 0); // You have failed to bid in the auction
  366. return;
  367. }
  368. if( inter_auction->count(char_id, true) > 4 && bid < auction->buynow && auction->buyer_id != char_id )
  369. {
  370. mapif->auction_bid(fd, char_id, bid, 9); // You cannot place more than 5 bids at a time
  371. return;
  372. }
  373. if( auction->buyer_id > 0 )
  374. { // Send Money back to the previous Buyer
  375. if( auction->buyer_id != char_id )
  376. {
  377. inter_mail->sendmail(0, "Auction Manager", auction->buyer_id, auction->buyer_name, "Auction", "Someone has placed a higher bid.", auction->price, NULL);
  378. mapif->auction_message(auction->buyer_id, 7); // You have failed to win the auction
  379. }
  380. else
  381. inter_mail->sendmail(0, "Auction Manager", auction->buyer_id, auction->buyer_name, "Auction", "You have placed a higher bid.", auction->price, NULL);
  382. }
  383. auction->buyer_id = char_id;
  384. safestrncpy(auction->buyer_name, (char*)RFIFOP(fd,16), NAME_LENGTH);
  385. auction->price = bid;
  386. if( bid >= auction->buynow )
  387. { // Automatic won the auction
  388. mapif->auction_bid(fd, char_id, bid - auction->buynow, 1); // You have successfully bid in the auction
  389. inter_mail->sendmail(0, "Auction Manager", auction->buyer_id, auction->buyer_name, "Auction", "You have won the auction.", 0, &auction->item);
  390. mapif->auction_message(char_id, 6); // You have won the auction
  391. inter_mail->sendmail(0, "Auction Manager", auction->seller_id, auction->seller_name, "Auction", "Payment for your auction!.", auction->buynow, NULL);
  392. inter_auction->delete_(auction);
  393. return;
  394. }
  395. inter_auction->save(auction);
  396. mapif->auction_bid(fd, char_id, 0, 1); // You have successfully bid in the auction
  397. }
  398. /*==========================================
  399. * Packets From Map Server
  400. *------------------------------------------*/
  401. int inter_auction_parse_frommap(int fd)
  402. {
  403. switch(RFIFOW(fd,0))
  404. {
  405. case 0x3050: mapif->parse_auction_requestlist(fd); break;
  406. case 0x3051: mapif->parse_auction_register(fd); break;
  407. case 0x3052: mapif->parse_auction_cancel(fd); break;
  408. case 0x3053: mapif->parse_auction_close(fd); break;
  409. case 0x3055: mapif->parse_auction_bid(fd); break;
  410. default:
  411. return 0;
  412. }
  413. return 1;
  414. }
  415. int inter_auction_sql_init(void)
  416. {
  417. inter_auction->db = idb_alloc(DB_OPT_RELEASE_DATA);
  418. inter_auction->fromsql();
  419. return 0;
  420. }
  421. void inter_auction_sql_final(void)
  422. {
  423. inter_auction->db->destroy(inter_auction->db,NULL);
  424. return;
  425. }
  426. void inter_auction_defaults(void)
  427. {
  428. inter_auction = &inter_auction_s;
  429. inter_auction->db = NULL; // int auction_id -> struct auction_data*
  430. inter_auction->count = inter_auction_count;
  431. inter_auction->save = inter_auction_save;
  432. inter_auction->create = inter_auction_create;
  433. inter_auction->end_timer = inter_auction_end_timer;
  434. inter_auction->delete_ = inter_auction_delete;
  435. inter_auction->fromsql = inter_auctions_fromsql;
  436. inter_auction->parse_frommap = inter_auction_parse_frommap;
  437. inter_auction->sql_init = inter_auction_sql_init;
  438. inter_auction->sql_final = inter_auction_sql_final;
  439. }