vending.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "../common/nullpo.h"
  4. #include "../common/malloc.h" // aMalloc, aFree
  5. #include "../common/showmsg.h" // ShowInfo
  6. #include "../common/strlib.h"
  7. #include "../common/utils.h"
  8. #include "clif.h"
  9. #include "itemdb.h"
  10. #include "atcommand.h"
  11. #include "map.h"
  12. #include "path.h"
  13. #include "chrif.h"
  14. #include "vending.h"
  15. #include "pc.h"
  16. #include "npc.h"
  17. #include "skill.h"
  18. #include "battle.h"
  19. #include "log.h"
  20. #include <stdio.h>
  21. #include <stdlib.h> // atoi
  22. #include <string.h>
  23. /// Struct for vending entry of autotrader
  24. struct s_autotrade_entry {
  25. int cartinventory_id;
  26. uint16 amount;
  27. int price;
  28. uint16 index;
  29. };
  30. /// Struct of autotrader
  31. struct s_autotrade {
  32. int account_id;
  33. int char_id;
  34. int vendor_id;
  35. int m;
  36. uint16 x,
  37. y;
  38. unsigned char sex;
  39. char title[MESSAGE_SIZE];
  40. uint16 count;
  41. struct s_autotrade_entry **entries;
  42. struct map_session_data *sd;
  43. };
  44. static int vending_nextid = 0; ///Vending_id counter
  45. static DBMap *vending_db; ///Db holder the vender : charid -> map_session_data
  46. //Autotrader
  47. static struct s_autotrade **autotraders; ///Autotraders Storage
  48. static uint16 autotrader_count; ///Autotrader count
  49. static void do_final_vending_autotrade(void);
  50. /**
  51. * Lookup to get the vending_db outside module
  52. * @return the vending_db
  53. */
  54. DBMap * vending_getdb(){
  55. return vending_db;
  56. }
  57. /**
  58. * Create an unique vending shop id.
  59. * @return the next vending_id
  60. */
  61. static int vending_getuid(void){
  62. return ++vending_nextid;
  63. }
  64. /**
  65. * Make a player close his shop
  66. * @param sd : player session
  67. */
  68. void vending_closevending(struct map_session_data* sd)
  69. {
  70. nullpo_retv(sd);
  71. if( sd->state.vending ) {
  72. if( Sql_Query( mmysql_handle, "DELETE FROM `%s` WHERE vending_id = %d;", vending_items_db, sd->vender_id ) != SQL_SUCCESS ||
  73. Sql_Query( mmysql_handle, "DELETE FROM `%s` WHERE `id` = %d;", vendings_db, sd->vender_id ) != SQL_SUCCESS ){
  74. Sql_ShowDebug(mmysql_handle);
  75. }
  76. sd->state.vending = false;
  77. clif_closevendingboard(&sd->bl, 0);
  78. idb_remove(vending_db, sd->status.char_id);
  79. }
  80. }
  81. /**
  82. * Player request a shop's item list (a player shop)
  83. * @param sd : player requestion the list
  84. * @param id : vender account id (gid)
  85. */
  86. void vending_vendinglistreq(struct map_session_data* sd, int id)
  87. {
  88. struct map_session_data* vsd;
  89. nullpo_retv(sd);
  90. if( (vsd = map_id2sd(id)) == NULL )
  91. return;
  92. if( !vsd->state.vending )
  93. return; // not vending
  94. if (!pc_can_give_items(sd) || !pc_can_give_items(vsd)) //check if both GMs are allowed to trade
  95. { // GM is not allowed to trade
  96. clif_displaymessage(sd->fd, msg_txt(sd,246));
  97. return;
  98. }
  99. sd->vended_id = vsd->vender_id; // register vending uid
  100. clif_vendinglist(sd, id, vsd->vending);
  101. }
  102. /**
  103. * Purchase item(s) from a shop
  104. * @param sd : buyer player session
  105. * @param aid : account id of vender
  106. * @param uid : shop unique id
  107. * @param data : items data who would like to purchase \n
  108. * data := {<index>.w <amount>.w }[count]
  109. * @param count : number of different items he's trying to buy
  110. */
  111. void vending_purchasereq(struct map_session_data* sd, int aid, int uid, const uint8* data, int count)
  112. {
  113. int i, j, cursor, w, new_ = 0, blank, vend_list[MAX_VENDING];
  114. double z;
  115. struct s_vending vending[MAX_VENDING]; // against duplicate packets
  116. struct map_session_data* vsd = map_id2sd(aid);
  117. nullpo_retv(sd);
  118. if( vsd == NULL || !vsd->state.vending || vsd->bl.id == sd->bl.id )
  119. return; // invalid shop
  120. if( vsd->vender_id != uid ) { // shop has changed
  121. clif_buyvending(sd, 0, 0, 6); // store information was incorrect
  122. return;
  123. }
  124. if( !searchstore_queryremote(sd, aid) && ( sd->bl.m != vsd->bl.m || !check_distance_bl(&sd->bl, &vsd->bl, AREA_SIZE) ) )
  125. return; // shop too far away
  126. searchstore_clearremote(sd);
  127. if( count < 1 || count > MAX_VENDING || count > vsd->vend_num )
  128. return; // invalid amount of purchased items
  129. blank = pc_inventoryblank(sd); //number of free cells in the buyer's inventory
  130. // duplicate item in vending to check hacker with multiple packets
  131. memcpy(&vending, &vsd->vending, sizeof(vsd->vending)); // copy vending list
  132. // some checks
  133. z = 0.; // zeny counter
  134. w = 0; // weight counter
  135. for( i = 0; i < count; i++ ) {
  136. short amount = *(uint16*)(data + 4*i + 0);
  137. short idx = *(uint16*)(data + 4*i + 2);
  138. idx -= 2;
  139. if( amount <= 0 )
  140. return;
  141. // check of item index in the cart
  142. if( idx < 0 || idx >= MAX_CART )
  143. return;
  144. ARR_FIND( 0, vsd->vend_num, j, vsd->vending[j].index == idx );
  145. if( j == vsd->vend_num )
  146. return; //picked non-existing item
  147. else
  148. vend_list[i] = j;
  149. z += ((double)vsd->vending[j].value * (double)amount);
  150. if( z > (double)sd->status.zeny || z < 0. || z > (double)MAX_ZENY )
  151. {
  152. clif_buyvending(sd, idx, amount, 1); // you don't have enough zeny
  153. return;
  154. }
  155. if( z + (double)vsd->status.zeny > (double)MAX_ZENY && !battle_config.vending_over_max )
  156. {
  157. clif_buyvending(sd, idx, vsd->vending[j].amount, 4); // too much zeny = overflow
  158. return;
  159. }
  160. w += itemdb_weight(vsd->status.cart[idx].nameid) * amount;
  161. if( w + sd->weight > sd->max_weight ) {
  162. clif_buyvending(sd, idx, amount, 2); // you can not buy, because overweight
  163. return;
  164. }
  165. //Check to see if cart/vend info is in sync.
  166. if( vending[j].amount > vsd->status.cart[idx].amount )
  167. vending[j].amount = vsd->status.cart[idx].amount;
  168. // if they try to add packets (example: get twice or more 2 apples if marchand has only 3 apples).
  169. // here, we check cumulative amounts
  170. if( vending[j].amount < amount )
  171. {
  172. // send more quantity is not a hack (an other player can have buy items just before)
  173. clif_buyvending(sd, idx, vsd->vending[j].amount, 4); // not enough quantity
  174. return;
  175. }
  176. vending[j].amount -= amount;
  177. switch( pc_checkadditem(sd, vsd->status.cart[idx].nameid, amount) ) {
  178. case CHKADDITEM_EXIST:
  179. break; //We'd add this item to the existing one (in buyers inventory)
  180. case CHKADDITEM_NEW:
  181. new_++;
  182. if (new_ > blank)
  183. return; //Buyer has no space in his inventory
  184. break;
  185. case CHKADDITEM_OVERAMOUNT:
  186. return; //too many items
  187. }
  188. }
  189. pc_payzeny(sd, (int)z, LOG_TYPE_VENDING, vsd);
  190. if( battle_config.vending_tax )
  191. z -= z * (battle_config.vending_tax/10000.);
  192. pc_getzeny(vsd, (int)z, LOG_TYPE_VENDING, sd);
  193. for( i = 0; i < count; i++ ) {
  194. short amount = *(uint16*)(data + 4*i + 0);
  195. short idx = *(uint16*)(data + 4*i + 2);
  196. idx -= 2;
  197. // vending item
  198. pc_additem(sd, &vsd->status.cart[idx], amount, LOG_TYPE_VENDING);
  199. vsd->vending[vend_list[i]].amount -= amount;
  200. if( vsd->vending[vend_list[i]].amount ){
  201. if( Sql_Query( mmysql_handle, "UPDATE `%s` SET `amount` = %d WHERE `vending_id` = %d and `cartinventory_id` = %d", vending_items_db, vsd->vending[vend_list[i]].amount, vsd->vender_id, vsd->status.cart[idx].id ) != SQL_SUCCESS ){
  202. Sql_ShowDebug( mmysql_handle );
  203. }
  204. }else{
  205. if( Sql_Query( mmysql_handle, "DELETE FROM `%s` WHERE `vending_id` = %d and `cartinventory_id` = %d", vending_items_db, vsd->vender_id, vsd->status.cart[idx].id ) != SQL_SUCCESS ){
  206. Sql_ShowDebug( mmysql_handle );
  207. }
  208. }
  209. pc_cart_delitem(vsd, idx, amount, 0, LOG_TYPE_VENDING);
  210. clif_vendingreport(vsd, idx, amount);
  211. //print buyer's name
  212. if( battle_config.buyer_name ) {
  213. char temp[256];
  214. sprintf(temp, msg_txt(sd,265), sd->status.name);
  215. clif_disp_onlyself(vsd,temp,strlen(temp));
  216. }
  217. }
  218. // compact the vending list
  219. for( i = 0, cursor = 0; i < vsd->vend_num; i++ ) {
  220. if( vsd->vending[i].amount == 0 )
  221. continue;
  222. if( cursor != i ) { // speedup
  223. vsd->vending[cursor].index = vsd->vending[i].index;
  224. vsd->vending[cursor].amount = vsd->vending[i].amount;
  225. vsd->vending[cursor].value = vsd->vending[i].value;
  226. }
  227. cursor++;
  228. }
  229. vsd->vend_num = cursor;
  230. //Always save BOTH: buyer and customer
  231. if( save_settings&2 ) {
  232. chrif_save(sd,0);
  233. chrif_save(vsd,0);
  234. }
  235. //check for @AUTOTRADE users [durf]
  236. if( vsd->state.autotrade ) {
  237. //see if there is anything left in the shop
  238. ARR_FIND( 0, vsd->vend_num, i, vsd->vending[i].amount > 0 );
  239. if( i == vsd->vend_num ) {
  240. //Close Vending (this was automatically done by the client, we have to do it manually for autovenders) [Skotlex]
  241. vending_closevending(vsd);
  242. map_quit(vsd); //They have no reason to stay around anymore, do they?
  243. }
  244. }
  245. }
  246. /**
  247. * Player setup a new shop
  248. * @param sd : player opening the shop
  249. * @param message : shop title
  250. * @param data : itemlist data \n
  251. * data := {<index>.w <amount>.w <value>.l}[count]
  252. * @param count : number of different items
  253. */
  254. bool vending_openvending(struct map_session_data* sd, const char* message, const uint8* data, int count) {
  255. int i, j;
  256. int vending_skill_lvl;
  257. char message_sql[MESSAGE_SIZE*2];
  258. nullpo_retr(false,sd);
  259. if ( pc_isdead(sd) || !sd->state.prevend || pc_istrading(sd))
  260. return false; // can't open vendings lying dead || didn't use via the skill (wpe/hack) || can't have 2 shops at once
  261. vending_skill_lvl = pc_checkskill(sd, MC_VENDING);
  262. // skill level and cart check
  263. if( !vending_skill_lvl || !pc_iscarton(sd) ) {
  264. clif_skill_fail(sd, MC_VENDING, USESKILL_FAIL_LEVEL, 0);
  265. return false;
  266. }
  267. // check number of items in shop
  268. if( count < 1 || count > MAX_VENDING || count > 2 + vending_skill_lvl )
  269. { // invalid item count
  270. clif_skill_fail(sd, MC_VENDING, USESKILL_FAIL_LEVEL, 0);
  271. return false;
  272. }
  273. // filter out invalid items
  274. i = 0;
  275. for( j = 0; j < count; j++ ) {
  276. short index = *(uint16*)(data + 8*j + 0);
  277. short amount = *(uint16*)(data + 8*j + 2);
  278. unsigned int value = *(uint32*)(data + 8*j + 4);
  279. index -= 2; // offset adjustment (client says that the first cart position is 2)
  280. if( index < 0 || index >= MAX_CART // invalid position
  281. || pc_cartitem_amount(sd, index, amount) < 0 // invalid item or insufficient quantity
  282. //NOTE: official server does not do any of the following checks!
  283. || !sd->status.cart[index].identify // unidentified item
  284. || sd->status.cart[index].attribute == 1 // broken item
  285. || sd->status.cart[index].expire_time // It should not be in the cart but just in case
  286. || (sd->status.cart[index].bound && !pc_can_give_bounded_items(sd)) // can't trade account bound items and has no permission
  287. || !itemdb_cantrade(&sd->status.cart[index], pc_get_group_level(sd), pc_get_group_level(sd)) ) // untradeable item
  288. continue;
  289. sd->vending[i].index = index;
  290. sd->vending[i].amount = amount;
  291. sd->vending[i].value = min(value, (unsigned int)battle_config.vending_max_value);
  292. i++; // item successfully added
  293. }
  294. if( i != j )
  295. clif_displaymessage (sd->fd, msg_txt(sd,266)); //"Some of your items cannot be vended and were removed from the shop."
  296. if( i == 0 ) { // no valid item found
  297. clif_skill_fail(sd, MC_VENDING, USESKILL_FAIL_LEVEL, 0); // custom reply packet
  298. return false;
  299. }
  300. sd->state.prevend = 0;
  301. sd->state.vending = true;
  302. sd->vender_id = vending_getuid();
  303. sd->vend_num = i;
  304. safestrncpy(sd->message, message, MESSAGE_SIZE);
  305. Sql_EscapeString( mmysql_handle, message_sql, sd->message );
  306. if( Sql_Query( mmysql_handle, "INSERT INTO `%s`(`id`,`account_id`,`char_id`,`sex`,`map`,`x`,`y`,`title`,`autotrade`) VALUES( %d, %d, %d, '%c', '%s', %d, %d, '%s', %d );", vendings_db, sd->vender_id, sd->status.account_id, sd->status.char_id, sd->status.sex == 0 ? 'F' : 'M', map[sd->bl.m].name, sd->bl.x, sd->bl.y, message_sql, sd->state.autotrade ) != SQL_SUCCESS ){
  307. Sql_ShowDebug(mmysql_handle);
  308. }
  309. for( i = 0; i < count; i++ ){
  310. if( Sql_Query( mmysql_handle, "INSERT INTO `%s`(`vending_id`,`index`,`cartinventory_id`,`amount`,`price`) VALUES( %d, %d, %d, %d, %d );", vending_items_db, sd->vender_id, i, sd->status.cart[sd->vending[i].index].id, sd->vending[i].amount, sd->vending[i].value ) != SQL_SUCCESS ){
  311. Sql_ShowDebug(mmysql_handle);
  312. }
  313. }
  314. clif_openvending(sd,sd->bl.id,sd->vending);
  315. clif_showvendingboard(&sd->bl,message,0);
  316. idb_put(vending_db, sd->status.char_id, sd);
  317. return true;
  318. }
  319. /**
  320. * Checks if an item is being sold in given player's vending.
  321. * @param sd : vender session (player)
  322. * @param nameid : item id
  323. * @return 0:not selling it, 1: yes
  324. */
  325. bool vending_search(struct map_session_data* sd, unsigned short nameid) {
  326. int i;
  327. if( !sd->state.vending ) { // not vending
  328. return false;
  329. }
  330. ARR_FIND( 0, sd->vend_num, i, sd->status.cart[sd->vending[i].index].nameid == (short)nameid );
  331. if( i == sd->vend_num ) { // not found
  332. return false;
  333. }
  334. return true;
  335. }
  336. /**
  337. * Searches for all items in a vending, that match given ids, price and possible cards.
  338. * @param sd : The vender session to search into
  339. * @param s : parameter of the search (see s_search_store_search)
  340. * @return Whether or not the search should be continued.
  341. */
  342. bool vending_searchall(struct map_session_data* sd, const struct s_search_store_search* s) {
  343. int i, c, slot;
  344. unsigned int idx, cidx;
  345. struct item* it;
  346. if( !sd->state.vending ) // not vending
  347. return true;
  348. for( idx = 0; idx < s->item_count; idx++ ) {
  349. ARR_FIND( 0, sd->vend_num, i, sd->status.cart[sd->vending[i].index].nameid == (short)s->itemlist[idx] );
  350. if( i == sd->vend_num ) {// not found
  351. continue;
  352. }
  353. it = &sd->status.cart[sd->vending[i].index];
  354. if( s->min_price && s->min_price > sd->vending[i].value ) {// too low price
  355. continue;
  356. }
  357. if( s->max_price && s->max_price < sd->vending[i].value ) {// too high price
  358. continue;
  359. }
  360. if( s->card_count ) {// check cards
  361. if( itemdb_isspecial(it->card[0]) ) {// something, that is not a carded
  362. continue;
  363. }
  364. slot = itemdb_slot(it->nameid);
  365. for( c = 0; c < slot && it->card[c]; c ++ ) {
  366. ARR_FIND( 0, s->card_count, cidx, s->cardlist[cidx] == it->card[c] );
  367. if( cidx != s->card_count )
  368. {// found
  369. break;
  370. }
  371. }
  372. if( c == slot || !it->card[c] ) {// no card match
  373. continue;
  374. }
  375. }
  376. if( !searchstore_result(s->search_sd, sd->vender_id, sd->status.account_id, sd->message, it->nameid, sd->vending[i].amount, sd->vending[i].value, it->card, it->refine) )
  377. {// result set full
  378. return false;
  379. }
  380. }
  381. return true;
  382. }
  383. /** Open vending for Autotrader
  384. * @param sd Player as autotrader
  385. */
  386. void vending_reopen( struct map_session_data* sd ){
  387. // Ready to open vending for this char
  388. if ( sd && autotrader_count > 0 && autotraders){
  389. uint16 i;
  390. uint8 *data, *p;
  391. uint16 j, count;
  392. ARR_FIND(0,autotrader_count,i,autotraders[i] && autotraders[i]->char_id == sd->status.char_id);
  393. if (i >= autotrader_count) {
  394. return;
  395. }
  396. // Init vending data for autotrader
  397. CREATE(data, uint8, autotraders[i]->count * 8);
  398. for (j = 0, p = data, count = autotraders[i]->count; j < autotraders[i]->count; j++) {
  399. struct s_autotrade_entry *entry = autotraders[i]->entries[j];
  400. uint16 *index = (uint16*)(p + 0);
  401. uint16 *amount = (uint16*)(p + 2);
  402. uint32 *value = (uint32*)(p + 4);
  403. // Find item position in cart
  404. ARR_FIND(0, MAX_CART, entry->index, sd->status.cart[entry->index].id == entry->cartinventory_id);
  405. if (entry->index == MAX_CART) {
  406. count--;
  407. continue;
  408. }
  409. *index = entry->index + 2;
  410. *amount = entry->amount;
  411. *value = entry->price;
  412. p += 8;
  413. }
  414. // Set him into a hacked prevend state
  415. sd->state.prevend = 1;
  416. // Open the vending again
  417. if( vending_openvending(sd, autotraders[i]->title, data, count) ){
  418. // Set him to autotrade
  419. if (Sql_Query( mmysql_handle, "UPDATE `%s` SET `autotrade` = 1 WHERE `id` = %d;",
  420. vendings_db, sd->vender_id ) != SQL_SUCCESS )
  421. {
  422. Sql_ShowDebug( mmysql_handle );
  423. }
  424. // Make him look perfect
  425. unit_setdir(&sd->bl,battle_config.feature_autotrade_direction);
  426. if( battle_config.feature_autotrade_sit )
  427. pc_setsit(sd);
  428. ShowInfo("Loaded autotrade vending data for '"CL_WHITE"%s"CL_RESET"' with '"CL_WHITE"%d"CL_RESET"' items at "CL_WHITE"%s (%d,%d)"CL_RESET"\n",
  429. sd->status.name,count,mapindex_id2name(sd->mapindex),sd->bl.x,sd->bl.y);
  430. }else{
  431. // Failed to open the vending, set him offline
  432. ShowWarning("Failed to load autotrade vending data for '"CL_WHITE"%s"CL_RESET"' with '"CL_WHITE"%d"CL_RESET"' items\n", sd->status.name, count );
  433. map_quit( sd );
  434. }
  435. aFree(data);
  436. //If the last autotrade is loaded, clear autotraders [Cydh]
  437. if (i+1 >= autotrader_count)
  438. do_final_vending_autotrade();
  439. }
  440. }
  441. /**
  442. * Initializing autotraders from table
  443. */
  444. void do_init_vending_autotrade( void ) {
  445. if (battle_config.feature_autotrade) {
  446. uint16 i, items = 0;
  447. autotrader_count = 0;
  448. // Get autotrader from table. `map`, `x`, and `y`, aren't used here
  449. // Just read player that has data at vending_items [Cydh]
  450. if (Sql_Query(mmysql_handle,
  451. "SELECT `id`, `account_id`, `char_id`, `sex`, `title` "
  452. "FROM `%s` "
  453. "WHERE `autotrade` = 1 AND (SELECT COUNT(`vending_id`) FROM `%s` WHERE `vending_id` = `id`) > 0;",
  454. vendings_db, vending_items_db ) != SQL_SUCCESS )
  455. {
  456. Sql_ShowDebug(mmysql_handle);
  457. return;
  458. }
  459. if( (autotrader_count = (uint32)Sql_NumRows(mmysql_handle)) > 0 ){
  460. // Init autotraders
  461. CREATE(autotraders, struct s_autotrade *, autotrader_count);
  462. if (autotraders == NULL) { //This is shouldn't happen [Cydh]
  463. ShowError("Failed to initialize vending autotraders!\n");
  464. Sql_FreeResult(mmysql_handle);
  465. return;
  466. }
  467. // Init each autotrader data
  468. i = 0;
  469. while (SQL_SUCCESS == Sql_NextRow(mmysql_handle) && i < autotrader_count) {
  470. size_t len;
  471. char* data;
  472. CREATE(autotraders[i], struct s_autotrade, 1);
  473. Sql_GetData(mmysql_handle, 0, &data, NULL); autotraders[i]->vendor_id = atoi(data);
  474. Sql_GetData(mmysql_handle, 1, &data, NULL); autotraders[i]->account_id = atoi(data);
  475. Sql_GetData(mmysql_handle, 2, &data, NULL); autotraders[i]->char_id = atoi(data);
  476. Sql_GetData(mmysql_handle, 3, &data, NULL); autotraders[i]->sex = (data[0] == 'F') ? 0 : 1;
  477. Sql_GetData(mmysql_handle, 4, &data, &len); safestrncpy(autotraders[i]->title, data, min(len + 1, MESSAGE_SIZE));
  478. autotraders[i]->count = 0;
  479. // initialize player
  480. CREATE(autotraders[i]->sd, struct map_session_data, 1);
  481. pc_setnewpc(autotraders[i]->sd, autotraders[i]->account_id, autotraders[i]->char_id, 0, gettick(), autotraders[i]->sex, 0);
  482. autotraders[i]->sd->state.autotrade = 1;
  483. chrif_authreq(autotraders[i]->sd, true);
  484. i++;
  485. }
  486. Sql_FreeResult(mmysql_handle);
  487. //Init items on vending list each autotrader
  488. for (i = 0; i < autotrader_count; i++){
  489. struct s_autotrade *at = NULL;
  490. uint16 j;
  491. if (autotraders[i] == NULL)
  492. continue;
  493. at = autotraders[i];
  494. if (SQL_ERROR == Sql_Query(mmysql_handle,
  495. "SELECT `cartinventory_id`, `amount`, `price` "
  496. "FROM `%s` "
  497. "WHERE `vending_id` = %d "
  498. "ORDER BY `index` ASC;", vending_items_db, at->vendor_id ) )
  499. {
  500. Sql_ShowDebug(mmysql_handle);
  501. continue;
  502. }
  503. if (!(at->count = (uint32)Sql_NumRows(mmysql_handle))) {
  504. map_quit(at->sd);
  505. continue;
  506. }
  507. //Init the list
  508. CREATE(at->entries, struct s_autotrade_entry *,at->count);
  509. //Add the item into list
  510. j = 0;
  511. while (SQL_SUCCESS == Sql_NextRow(mmysql_handle) && j < at->count) {
  512. char* data;
  513. CREATE(at->entries[j], struct s_autotrade_entry, 1);
  514. Sql_GetData(mmysql_handle, 0, &data, NULL); at->entries[j]->cartinventory_id = atoi(data);
  515. Sql_GetData(mmysql_handle, 1, &data, NULL); at->entries[j]->amount = atoi(data);
  516. Sql_GetData(mmysql_handle, 2, &data, NULL); at->entries[j]->price = atoi(data);
  517. j++;
  518. }
  519. items += j;
  520. Sql_FreeResult(mmysql_handle);
  521. }
  522. ShowStatus("Done loading '"CL_WHITE"%d"CL_RESET"' autotraders with '"CL_WHITE"%d"CL_RESET"' items.\n", autotrader_count, items);
  523. }
  524. }
  525. // Everything is loaded fine, their entries will be reinserted once they are loaded
  526. if (Sql_Query( mmysql_handle, "DELETE FROM `%s`;", vendings_db ) != SQL_SUCCESS ||
  527. Sql_Query( mmysql_handle, "DELETE FROM `%s`;", vending_items_db ) != SQL_SUCCESS)
  528. {
  529. Sql_ShowDebug(mmysql_handle);
  530. }
  531. }
  532. /**
  533. * Clear all autotraders
  534. * @author [Cydh]
  535. */
  536. void do_final_vending_autotrade(void) {
  537. if (autotrader_count && autotraders){
  538. uint16 i = 0;
  539. while (i < autotrader_count) { //Free the autotrader
  540. if (autotraders[i] == NULL)
  541. continue;
  542. if (autotraders[i]->count) {
  543. uint16 j = 0;
  544. while (j < autotraders[i]->count) { //Free the autotrade entries
  545. if (autotraders[i]->entries == NULL)
  546. continue;
  547. if (autotraders[i]->entries[j])
  548. aFree(autotraders[i]->entries[j]);
  549. j++;
  550. }
  551. aFree(autotraders[i]->entries);
  552. }
  553. aFree(autotraders[i]);
  554. i++;
  555. }
  556. aFree(autotraders);
  557. autotrader_count = 0;
  558. }
  559. }
  560. /**
  561. * Initialise the vending module
  562. * called in map::do_init
  563. */
  564. void do_final_vending(void) {
  565. db_destroy(vending_db);
  566. do_final_vending_autotrade(); //Make sure everything is cleared [Cydh]
  567. }
  568. /**
  569. * Destory the vending module
  570. * called in map::do_final
  571. */
  572. void do_init_vending(void) {
  573. vending_db = idb_alloc(DB_OPT_BASE);
  574. vending_nextid = 0;
  575. }