/** * Initializing autotraders from table */ void do_init_vending_autotrade( void ) { if (battle_config.feature_autotrade) { uint16 i, items = 0; autotrader_count = autotrader_loaded_count = 0; // Get autotrader from table. `map`, `x`, and `y`, aren't used here // Just read player that has data at vending_items [Cydh] if (Sql_Query(mmysql_handle, "SELECT `id`, `account_id`, `char_id`, `sex`, `title`, `body_direction`, `head_direction`, `sit` " "FROM `%s` " "WHERE `autotrade` = 1 AND (SELECT COUNT(`vending_id`) FROM `%s` WHERE `vending_id` = `id`) > 0 " "ORDER BY `id`;", vendings_db, vending_items_db ) != SQL_SUCCESS ) { Sql_ShowDebug(mmysql_handle); return; } if( (autotrader_count = (uint16)Sql_NumRows(mmysql_handle)) > 0 ){ // Init autotraders CREATE(autotraders, struct s_autotrade *, autotrader_count); if (autotraders == NULL) { //This is shouldn't happen [Cydh] ShowError("Failed to initialize vending autotraders!\n"); Sql_FreeResult(mmysql_handle); return; } // Init each autotrader data i = 0; while (SQL_SUCCESS == Sql_NextRow(mmysql_handle) && i < autotrader_count) { size_t len; char* data; struct s_autotrade *at = NULL; CREATE(autotraders[i], struct s_autotrade, 1); at = autotraders[i]; Sql_GetData(mmysql_handle, 0, &data, NULL); at->vendor_id = atoi(data); Sql_GetData(mmysql_handle, 1, &data, NULL); at->account_id = atoi(data); Sql_GetData(mmysql_handle, 2, &data, NULL); at->char_id = atoi(data); Sql_GetData(mmysql_handle, 3, &data, NULL); at->sex = (data[0] == 'F') ? 0 : 1; Sql_GetData(mmysql_handle, 4, &data, &len); safestrncpy(at->title, data, min(len + 1, MESSAGE_SIZE)); Sql_GetData(mmysql_handle, 5, &data, NULL); at->dir = atoi(data); Sql_GetData(mmysql_handle, 6, &data, NULL); at->head_dir = atoi(data); Sql_GetData(mmysql_handle, 7, &data, NULL); at->sit = atoi(data); at->count = 0; if (battle_config.feature_autotrade_direction >= 0) at->dir = battle_config.feature_autotrade_direction; if (battle_config.feature_autotrade_head_direction >= 0) at->head_dir = battle_config.feature_autotrade_head_direction; if (battle_config.feature_autotrade_sit >= 0) at->sit = battle_config.feature_autotrade_sit; // initialize player CREATE(at->sd, struct map_session_data, 1); pc_setnewpc(at->sd, at->account_id, at->char_id, 0, gettick(), at->sex, 0); at->sd->state.autotrade = 1; at->sd->state.monster_ignore = (battle_config.autotrade_monsterignore); chrif_authreq(at->sd, true); i++; } Sql_FreeResult(mmysql_handle); //Init items on vending list each autotrader for (i = 0; i < autotrader_count; i++){ struct s_autotrade *at = NULL; uint16 j; if ((at = autotraders[i]) == NULL) continue; if (SQL_ERROR == Sql_Query(mmysql_handle, "SELECT `cartinventory_id`, `amount`, `price` " "FROM `%s` " "WHERE `vending_id` = %d " "ORDER BY `index` ASC;", vending_items_db, at->vendor_id ) ) { Sql_ShowDebug(mmysql_handle); continue; } if (!(at->count = (uint16)Sql_NumRows(mmysql_handle))) { map_quit(at->sd); continue; } //Init the list CREATE(at->entries, struct s_autotrade_entry *,at->count); //Add the item into list j = 0; while (SQL_SUCCESS == Sql_NextRow(mmysql_handle) && j < at->count) { char* data; CREATE(at->entries[j], struct s_autotrade_entry, 1); Sql_GetData(mmysql_handle, 0, &data, NULL); at->entries[j]->cartinventory_id = atoi(data); Sql_GetData(mmysql_handle, 1, &data, NULL); at->entries[j]->amount = atoi(data); Sql_GetData(mmysql_handle, 2, &data, NULL); at->entries[j]->price = atoi(data); j++; } items += j; Sql_FreeResult(mmysql_handle); } ShowStatus("Done loading '"CL_WHITE"%d"CL_RESET"' vending autotraders with '"CL_WHITE"%d"CL_RESET"' items.\n", autotrader_count, items); }
/** * Initializing autotraders from table */ void do_init_buyingstore_autotrade( void ) { if(battle_config.feature_autotrade) { if (Sql_Query(mmysql_handle, "SELECT `id`, `account_id`, `char_id`, `sex`, `title`, `limit`, `body_direction`, `head_direction`, `sit` " "FROM `%s` " "WHERE `autotrade` = 1 AND `limit` > 0 AND (SELECT COUNT(`buyingstore_id`) FROM `%s` WHERE `buyingstore_id` = `id`) > 0 " "ORDER BY `id`;", buyingstores_table, buyingstore_items_table ) != SQL_SUCCESS ) { Sql_ShowDebug(mmysql_handle); return; } if( Sql_NumRows(mmysql_handle) > 0 ) { uint16 items = 0; DBIterator *iter = NULL; struct s_autotrader *at = NULL; // Init each autotrader data while (SQL_SUCCESS == Sql_NextRow(mmysql_handle)) { size_t len; char* data; at = NULL; CREATE(at, struct s_autotrader, 1); Sql_GetData(mmysql_handle, 0, &data, NULL); at->id = atoi(data); Sql_GetData(mmysql_handle, 1, &data, NULL); at->account_id = atoi(data); Sql_GetData(mmysql_handle, 2, &data, NULL); at->char_id = atoi(data); Sql_GetData(mmysql_handle, 3, &data, NULL); at->sex = (data[0] == 'F') ? 0 : 1; Sql_GetData(mmysql_handle, 4, &data, &len); safestrncpy(at->title, data, zmin(len + 1, MESSAGE_SIZE)); Sql_GetData(mmysql_handle, 5, &data, NULL); at->limit = atoi(data); Sql_GetData(mmysql_handle, 6, &data, NULL); at->dir = atoi(data); Sql_GetData(mmysql_handle, 7, &data, NULL); at->head_dir = atoi(data); Sql_GetData(mmysql_handle, 8, &data, NULL); at->sit = atoi(data); at->count = 0; if (battle_config.feature_autotrade_direction >= 0) at->dir = battle_config.feature_autotrade_direction; if (battle_config.feature_autotrade_head_direction >= 0) at->head_dir = battle_config.feature_autotrade_head_direction; if (battle_config.feature_autotrade_sit >= 0) at->sit = battle_config.feature_autotrade_sit; // initialize player CREATE(at->sd, struct map_session_data, 1); pc_setnewpc(at->sd, at->account_id, at->char_id, 0, gettick(), at->sex, 0); at->sd->state.autotrade = 1|4; at->sd->state.monster_ignore = (battle_config.autotrade_monsterignore); chrif_authreq(at->sd, true); uidb_put(buyingstore_autotrader_db, at->char_id, at); } Sql_FreeResult(mmysql_handle); // Init items for each autotraders iter = db_iterator(buyingstore_autotrader_db); for (at = (struct s_autotrader *)dbi_first(iter); dbi_exists(iter); at = (struct s_autotrader *)dbi_next(iter)) { uint16 j = 0; if (SQL_ERROR == Sql_Query(mmysql_handle, "SELECT `item_id`, `amount`, `price` " "FROM `%s` " "WHERE `buyingstore_id` = %d " "ORDER BY `index` ASC;", buyingstore_items_table, at->id ) ) { Sql_ShowDebug(mmysql_handle); continue; } if (!(at->count = (uint16)Sql_NumRows(mmysql_handle))) { map_quit(at->sd); buyingstore_autotrader_remove(at, true); continue; } //Init the list CREATE(at->entries, struct s_autotrade_entry *,at->count); //Add the item into list j = 0; while (SQL_SUCCESS == Sql_NextRow(mmysql_handle) && j < at->count) { char *data; CREATE(at->entries[j], struct s_autotrade_entry, 1); Sql_GetData(mmysql_handle, 0, &data, NULL); at->entries[j]->item_id = atoi(data); Sql_GetData(mmysql_handle, 1, &data, NULL); at->entries[j]->amount = atoi(data); Sql_GetData(mmysql_handle, 2, &data, NULL); at->entries[j]->price = atoi(data); j++; } items += j; Sql_FreeResult(mmysql_handle); } dbi_destroy(iter); ShowStatus("Done loading '"CL_WHITE"%d"CL_RESET"' buyingstore autotraders with '"CL_WHITE"%d"CL_RESET"' items.\n", db_size(buyingstore_autotrader_db), items); } }