Skip to content

Commit

Permalink
-Ah performance improvement: Removed direct ah db queries and replace…
Browse files Browse the repository at this point in the history
…d it with a internal lookup.
  • Loading branch information
mostlikely4r committed Oct 9, 2024
1 parent 85b5df1 commit 225d35a
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 13 deletions.
37 changes: 34 additions & 3 deletions playerbot/RandomPlayerbotMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,8 @@ void RandomPlayerbotMgr::UpdateAIInternal(uint32 elapsed, bool minimal)

DelayedFacingFix();

MirrorAh();

//Ping character database.
CharacterDatabase.AsyncPQuery(&RandomPlayerbotMgr::DatabasePing, sWorld.GetCurrentMSTime(), std::string("CharacterDatabase"), "select 1 from dual");
}
Expand Down Expand Up @@ -698,9 +700,6 @@ void RandomPlayerbotMgr::ScaleBotActivity()
out << activityPercentageMod << ",";
out << activeBots << ",";
out << GetPlayerbotsAmount() << ",";
out << ahQueries << ",";

ahQueries = 0;

float totalLevel = 0, totalGold = 0, totalGearscore = 0;

Expand Down Expand Up @@ -3818,6 +3817,38 @@ void RandomPlayerbotMgr::Hotfix(Player* bot, uint32 version)
bot->GetGUIDLow(), MANGOSBOT_VERSION);
}

void RandomPlayerbotMgr::MirrorAh()
{
sRandomPlayerbotMgr.m_ahActionMutex.lock();

ahMirror.clear();

std::vector<AuctionHouseType> houses = { (AuctionHouseType)0,(AuctionHouseType)1,(AuctionHouseType)2 };

//Now loops over all houses. Can probably be faction specific later.
for (auto house : houses)
{
AuctionHouseObject* auctionHouse = sAuctionMgr.GetAuctionsMap(house);

AuctionHouseObject::AuctionEntryMap const& map = auctionHouse->GetAuctions();

for (auto& auction : map)
{
if (!auction.second)
continue;

if (!auction.second->buyout)
continue;

if (!auction.second->itemCount)
continue;

ahMirror[auction.second->itemTemplate].push_back(*auction.second);
}
}
sRandomPlayerbotMgr.m_ahActionMutex.unlock();
}

typedef std::unordered_map <uint32, std::list<float>> botPerformanceMetric;
std::unordered_map<std::string, botPerformanceMetric> botPerformanceMetrics;

Expand Down
6 changes: 5 additions & 1 deletion playerbot/RandomPlayerbotMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class RandomPlayerbotMgr : public PlayerbotHolder

std::mutex m_ahActionMutex;

uint32 ahQueries = 0;
std::vector<AuctionEntry> GetAhPrices(uint32 itemId) { return ahMirror[itemId]; }
protected:
virtual void OnBotLoginInternal(Player * const bot);

Expand Down Expand Up @@ -183,6 +183,7 @@ class RandomPlayerbotMgr : public PlayerbotHolder
void PrepareTeleportCache();
typedef void (RandomPlayerbotMgr::*ConsoleCommandHandler) (Player*);

void MirrorAh();
private:
PlayerBotMap players;
int processTicks;
Expand All @@ -208,6 +209,9 @@ class RandomPlayerbotMgr : public PlayerbotHolder

bool showLoginWarning;
std::unordered_map<uint32, std::unordered_map<uint32, std::vector<std::pair<ObjectGuid, time_t>>>> facingFix;

// itemId, buyout, count
std::unordered_map < uint32, std::vector<AuctionEntry>> ahMirror;
};

#define sRandomPlayerbotMgr RandomPlayerbotMgr::instance()
Expand Down
1 change: 0 additions & 1 deletion playerbot/TravelMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1323,7 +1323,6 @@ void TravelMgr::LoadQuestTravelTable()
out << "activityPercentageMod,";
out << "activeBots,";
out << "playerBots.size(),";
out << "ahqueries,";
out << "totalLevel,";
out << "avarageLevel1-9,";
out << "avarageLevel10-19,";
Expand Down
23 changes: 19 additions & 4 deletions playerbot/strategy/actions/AhAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,25 @@ bool AhAction::ExecuteCommand(Player* requester, std::string text, Unit* auction
if (!pricePerItemCache[proto->ItemId])
{
//check current AH listing prices for this item and try to set lower (undercut)
AuctionEntry lowestPrice;

uint32 lowestBuyoutItemPricePerItem = 0;
uint32 lowestBuyoutItemPricePerItemOwnerDbGuid = 0;
lowestPrice.Id = 0;

// check for the cheapest listing with the same count
// (simple dumping protection, can be improved by comparing listing prices but it's a bit complicated)
sRandomPlayerbotMgr.ahQueries += 1;

std::vector<AuctionEntry> auctions;

for (auto& auction : sRandomPlayerbotMgr.GetAhPrices(proto->ItemId))
{
if (auction.itemCount != item->GetCount())
continue;

if (lowestPrice.Id == 0 || float(auction.buyout) / float(auction.itemCount) < float(lowestPrice.buyout) / float(lowestPrice.itemCount))
lowestPrice = auction;
}

/*
auto lowestBuyoutPriceListing = CharacterDatabase.PQuery(
"SELECT buyoutprice, item_count, itemowner FROM auction WHERE item_template = '%u' AND item_count = '%u' ORDER BY buyoutprice / item_count ASC LIMIT 1",
item->GetProto()->ItemId,
Expand All @@ -110,6 +122,9 @@ bool AhAction::ExecuteCommand(Player* requester, std::string text, Unit* auction
}
} while (lowestBuyoutPriceListing->NextRow());
}
*/

uint32 lowestBuyoutItemPricePerItem = float(lowestPrice.buyout) / float(lowestPrice.itemCount);

// default desired price if there are no item listings
// (is the max price because why not? sounds reasonable to try selling at max price if there are no listings)
Expand All @@ -120,7 +135,7 @@ bool AhAction::ExecuteCommand(Player* requester, std::string text, Unit* auction

// check if it would be reasonable to sell lower than current cheapest listing
// also check if the item poster is not self to not to undercut yourself
if (lowestBuyoutItemPricePerItem > 0 && lowestBuyoutItemPricePerItemOwnerDbGuid != bot->GetDbGuid())
if (lowestBuyoutItemPricePerItem > 0 && lowestPrice.owner != bot->GetDbGuid())
{
//try to undercut by randomly 1 copper to 10% (may result in not actually undercutting but it's alright, even better)
uint32 undercutByMoney = std::max(static_cast<uint32>(1), static_cast<uint32>(lowestBuyoutItemPricePerItem * frand(0.0f, 0.1f)));
Expand Down
35 changes: 31 additions & 4 deletions playerbot/strategy/values/ItemUsageValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,6 @@ void ItemUsageValue::PopulateReagentItemIdsForCraftableItemIds()

void ItemUsageValue::PopulateSoldByVendorItemIds()
{
sRandomPlayerbotMgr.ahQueries += 1;
if (auto result = WorldDatabase.PQuery("%s", "SELECT item, entry FROM npc_vendor"))
{
BarGoLink bar(result->GetRowCount());
Expand All @@ -1112,7 +1111,6 @@ void ItemUsageValue::PopulateSoldByVendorItemIds()
} while (result->NextRow());
}

sRandomPlayerbotMgr.ahQueries += 1;
if (auto result = WorldDatabase.PQuery("%s", "SELECT item, entry FROM npc_vendor WHERE maxcount > 0"))
{
BarGoLink bar(result->GetRowCount());
Expand Down Expand Up @@ -1162,7 +1160,21 @@ uint32 ItemUsageValue::GetAHMedianBuyoutPricePerItem(ItemPrototype const* proto)
{
if (sPlayerbotAIConfig.shouldQueryAHListingsOutsideOfAH)
{
sRandomPlayerbotMgr.ahQueries += 1;
std::vector<float> prices;

for (auto& auction : sRandomPlayerbotMgr.GetAhPrices(proto->ItemId))
{
prices.push_back((float)auction.buyout / (float)auction.itemCount);
}

if (prices.empty())
return 0;

size_t n = prices.size() / 2;
std::nth_element(prices.begin(), prices.begin() + n, prices.end());
return prices[n];

/*
auto query = CharacterDatabase.PQuery(
" SELECT item_template, AVG(median)"
" FROM (SELECT item_template, (buyoutprice / item_count) median"
Expand All @@ -1185,6 +1197,7 @@ uint32 ItemUsageValue::GetAHMedianBuyoutPricePerItem(ItemPrototype const* proto)
return medianPrice;
} while (query->NextRow());
}
*/
}

return 0;
Expand All @@ -1194,7 +1207,20 @@ uint32 ItemUsageValue::GetAHListingLowestBuyoutPricePerItem(ItemPrototype const*
{
if (sPlayerbotAIConfig.shouldQueryAHListingsOutsideOfAH)
{
sRandomPlayerbotMgr.ahQueries += 1;
float minPrice = 0;
uint32 minBuyout = 0;

for (auto& auction : sRandomPlayerbotMgr.GetAhPrices(proto->ItemId))
{
if (!minBuyout || minBuyout > auction.buyout)
{
minBuyout = auction.buyout;
minPrice = (float)auction.buyout / (float)auction.itemCount;
}
}

return minBuyout;
/*
auto query = CharacterDatabase.PQuery(
"SELECT buyoutprice / item_count"
" FROM auction"
Expand All @@ -1214,6 +1240,7 @@ uint32 ItemUsageValue::GetAHListingLowestBuyoutPricePerItem(ItemPrototype const*
return lowestBuyoutPrice;
} while (query->NextRow());
}
*/
}

return 0;
Expand Down

0 comments on commit 225d35a

Please sign in to comment.