HandleautoCombine converted to QueryDatabase

This commit is contained in:
Arthur Ice 2014-10-08 14:28:40 -07:00
parent e2d52ec3e5
commit e2333e671b

View File

@ -424,38 +424,28 @@ void Object::HandleAutoCombine(Client* user, const RecipeAutoCombine_Struct* rac
return; return;
} }
//pull the list of components
char errbuf[MYSQL_ERRMSG_SIZE]; std::string query = StringFormat("SELECT tre.item_id,tre.componentcount "
MYSQL_RES *result; "FROM tradeskill_recipe_entries AS tre "
MYSQL_ROW row; "WHERE tre.componentcount > 0 AND tre.recipe_id = %u",
char *query = 0; rac->recipe_id);
auto results = database.QueryDatabase(query);
uint32 qlen = 0; if (!results.Success()) {
uint8 qcount = 0; LogFile->write(EQEMuLog::Error, "Error in HandleAutoCombine query '%s': %s", query.c_str(), results.ErrorMessage().c_str());
//pull the list of components
qlen = MakeAnyLenString(&query, "SELECT tre.item_id,tre.componentcount "
" FROM tradeskill_recipe_entries AS tre "
" WHERE tre.componentcount > 0 AND tre.recipe_id=%u", rac->recipe_id);
if (!database.RunQuery(query, qlen, errbuf, &result)) {
LogFile->write(EQEMuLog::Error, "Error in HandleAutoCombine query '%s': %s", query, errbuf);
safe_delete_array(query);
user->QueuePacket(outapp); user->QueuePacket(outapp);
safe_delete(outapp); safe_delete(outapp);
return; return;
} }
safe_delete_array(query);
qcount = mysql_num_rows(result); if(results.RowCount() < 1) {
if(qcount < 1) {
LogFile->write(EQEMuLog::Error, "Error in HandleAutoCombine: no components returned"); LogFile->write(EQEMuLog::Error, "Error in HandleAutoCombine: no components returned");
user->QueuePacket(outapp); user->QueuePacket(outapp);
safe_delete(outapp); safe_delete(outapp);
return; return;
} }
if(qcount > 10) {
LogFile->write(EQEMuLog::Error, "Error in HandleAutoCombine: too many components returned (%u)", qcount); if(results.RowCount() > 10) {
LogFile->write(EQEMuLog::Error, "Error in HandleAutoCombine: too many components returned (%u)", results.RowCount());
user->QueuePacket(outapp); user->QueuePacket(outapp);
safe_delete(outapp); safe_delete(outapp);
return; return;
@ -466,17 +456,15 @@ void Object::HandleAutoCombine(Client* user, const RecipeAutoCombine_Struct* rac
uint8 counts[10]; uint8 counts[10];
memset(counts, 0, sizeof(counts)); memset(counts, 0, sizeof(counts));
//search for all the items in their inventory //search for all the items in their inventory
Inventory& user_inv = user->GetInv(); Inventory& user_inv = user->GetInv();
uint8 count = 0; uint8 count = 0;
uint8 needcount = 0; uint8 needcount = 0;
uint8 r,k;
std::list<int> MissingItems; std::list<int> MissingItems;
for(r = 0; r < qcount; r++) { uint8 needItemIndex = 0;
row = mysql_fetch_row(result); for (auto row = results.begin(); row != results.end(); ++row, ++needItemIndex) {
uint32 item = (uint32)atoi(row[0]); uint32 item = (uint32)atoi(row[0]);
uint8 num = (uint8) atoi(row[1]); uint8 num = (uint8) atoi(row[1]);
@ -491,10 +479,9 @@ void Object::HandleAutoCombine(Client* user, const RecipeAutoCombine_Struct* rac
MissingItems.push_back(item); MissingItems.push_back(item);
//dont start deleting anything until we have found it all. //dont start deleting anything until we have found it all.
items[r] = item; items[needItemIndex] = item;
counts[r] = num; counts[needItemIndex] = num;
} }
mysql_free_result(result);
//make sure we found it all... //make sure we found it all...
if(count != needcount) if(count != needcount)
@ -520,12 +507,12 @@ void Object::HandleAutoCombine(Client* user, const RecipeAutoCombine_Struct* rac
//remove all the items from the players inventory, with updates... //remove all the items from the players inventory, with updates...
int16 slot; int16 slot;
for(r = 0; r < qcount; r++) { for(uint8 r = 0; r < results.RowCount(); r++) {
if(items[r] == 0 || counts[r] == 0) if(items[r] == 0 || counts[r] == 0)
continue; //skip empties, could prolly break here continue; //skip empties, could prolly break here
//we have to loop here to delete 1 at a time in case its in multiple stacks. //we have to loop here to delete 1 at a time in case its in multiple stacks.
for(k = 0; k < counts[r]; k++) { for(uint8 k = 0; k < counts[r]; k++) {
slot = user_inv.HasItem(items[r], 1, invWherePersonal); slot = user_inv.HasItem(items[r], 1, invWherePersonal);
if (slot == INVALID_INDEX) { if (slot == INVALID_INDEX) {
//WTF... I just checked this above, but just to be sure... //WTF... I just checked this above, but just to be sure...
@ -539,19 +526,14 @@ void Object::HandleAutoCombine(Client* user, const RecipeAutoCombine_Struct* rac
const ItemInst* inst = user_inv.GetItem(slot); const ItemInst* inst = user_inv.GetItem(slot);
if (inst && !inst->IsStackable()) if (inst && !inst->IsStackable())
{
user->DeleteItemInInventory(slot, 0, true); user->DeleteItemInInventory(slot, 0, true);
}
else else
{
user->DeleteItemInInventory(slot, 1, true); user->DeleteItemInInventory(slot, 1, true);
}
} }
} }
//otherwise, we found it all... //otherwise, we found it all...
outp->reply_code = 0x00000000; //success for finding it... outp->reply_code = 0x00000000; //success for finding it...
user->QueuePacket(outapp); user->QueuePacket(outapp);
safe_delete(outapp); safe_delete(outapp);