Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Game/Auction): rework async auction logic #166

Merged
merged 1 commit into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions src/server/game/Auction/AsyncAuctionMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ AsyncAuctionMgr::~AsyncAuctionMgr()
if (_queue)
_queue->Cancel();

if (_thread && _thread->joinable())
_thread->join();
for (auto& thread : _threads)
if (thread.joinable())
thread.join();
}

/*static*/ AsyncAuctionMgr* AsyncAuctionMgr::instance()
Expand All @@ -64,9 +65,13 @@ void AsyncAuctionMgr::Initialize()
LOG_INFO("server.loading", "Initialize async auction...");

_queue = std::make_unique<ProducerConsumerQueue<AsyncAuctionOperation*>>();
_thread = std::make_unique<std::thread>([this](){ ExecuteAsyncQueue(); });
_scheduler = std::make_unique<TaskScheduler>();

for (std::size_t i{}; i < 5; i++)
_threads.emplace_back([this](){ ExecuteAsyncQueue(); });

_threads.shrink_to_fit();

LOG_INFO("server.loading", ">> Async auction initialized in {}", sw);
LOG_INFO("server.loading", "");
}
Expand All @@ -86,9 +91,9 @@ void AsyncAuctionMgr::PlaceBid(ObjectGuid playerGuid, ObjectGuid auctioneer, uin
Enqueue(new PlaceBidTask(playerGuid, auctioneer, auctionID, price));
}

void AsyncAuctionMgr::ListBidderItems(ObjectGuid playerGuid, ObjectGuid auctioneer, uint32 listFrom, uint32 outbiddedCount, std::vector<uint32>& outbiddedAuctionIds)
void AsyncAuctionMgr::ListBidderItems(ObjectGuid playerGuid, ObjectGuid auctioneer, uint32 listFrom, uint32 outbiddedCount, std::vector<uint32>&& outbiddedAuctionIds)
{
Enqueue(new ListBidderItemsTask(playerGuid, auctioneer, listFrom, outbiddedCount, outbiddedAuctionIds));
Enqueue(new ListBidderItemsTask(playerGuid, auctioneer, listFrom, outbiddedCount, std::move(outbiddedAuctionIds)));
}

void AsyncAuctionMgr::ListOwnerItems(ObjectGuid playerGuid, ObjectGuid creatureGuid)
Expand Down Expand Up @@ -130,11 +135,7 @@ void AsyncAuctionMgr::ExecuteAsyncQueue()
if (!task)
break;

{
std::lock_guard<std::mutex> guard(_mutex);
task->Execute();
}

task->Execute();
delete task;
}
}
}
11 changes: 4 additions & 7 deletions src/server/game/Auction/AsyncAuctionMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#include "AuctionFwd.h"
#include <memory>
#include <mutex>
#include <vector>
#include <thread>

template <typename T>
Expand All @@ -40,21 +40,18 @@ class WH_GAME_API AsyncAuctionMgr

void SellItem(ObjectGuid playerGuid, std::shared_ptr<AuctionSellItem> listItems);
void PlaceBid(ObjectGuid playerGuid, ObjectGuid auctioneer, uint32 auctionID, uint32 price);
void ListBidderItems(ObjectGuid playerGuid, ObjectGuid auctioneer, uint32 listFrom, uint32 outbiddedCount, std::vector<uint32>& outbiddedAuctionIds);
void ListBidderItems(ObjectGuid playerGuid, ObjectGuid auctioneer, uint32 listFrom, uint32 outbiddedCount, std::vector<uint32>&& outbiddedAuctionIds);
void ListOwnerItems(ObjectGuid playerGuid, ObjectGuid creatureGuid);
void ListItems(ObjectGuid playerGuid, std::shared_ptr<AuctionListItems> listItems);
void UpdateBotAgents();

inline std::mutex& GetLock() { return _mutex; }

private:
void ExecuteAsyncQueue();
void Enqueue(AsyncAuctionOperation* operation);

std::unique_ptr<ProducerConsumerQueue<AsyncAuctionOperation*>> _queue;
std::unique_ptr<std::thread> _thread;
std::vector<std::thread> _threads;
std::unique_ptr<TaskScheduler> _scheduler;
std::mutex _mutex;

AsyncAuctionMgr() = default;
~AsyncAuctionMgr();
Expand All @@ -66,4 +63,4 @@ class WH_GAME_API AsyncAuctionMgr

#define sAsyncAuctionMgr AsyncAuctionMgr::instance()

#endif
#endif
7 changes: 2 additions & 5 deletions src/server/game/Auction/AsyncAuctionOperation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,14 +499,11 @@ void ListItemsTask::Execute()
}

AuctionHouseObject* auctionHouse = sAuctionMgr->GetAuctionsMap(creature->GetFaction());
bool result = auctionHouse->BuildListAuctionItems(packetSend, player, _packet);
if (!result)
return;

auctionHouse->BuildListAuctionItems(packetSend, player, std::move(_packet));
player->SendDirectMessage(packetSend.Write());
}

void AhBotTask::Execute()
{
sAuctionBot->UpdateAgents();
}
}
4 changes: 2 additions & 2 deletions src/server/game/Auction/AsyncAuctionOperation.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class WH_GAME_API PlaceBidTask : public AsyncAuctionOperation
class WH_GAME_API ListBidderItemsTask : public AsyncAuctionOperation
{
public:
ListBidderItemsTask(ObjectGuid playerGuid, ObjectGuid auctioneer, uint32 listFrom, uint32 outbiddedCount, std::vector<uint32>& outbiddedAuctionIds) :
ListBidderItemsTask(ObjectGuid playerGuid, ObjectGuid auctioneer, uint32 listFrom, uint32 outbiddedCount, std::vector<uint32>&& outbiddedAuctionIds) :
AsyncAuctionOperation(playerGuid),
_auctioneer(auctioneer),
_listFrom(listFrom),
Expand Down Expand Up @@ -135,4 +135,4 @@ class WH_GAME_API AhBotTask : public AsyncAuctionOperation
void Execute() override;
};

#endif
#endif
13 changes: 9 additions & 4 deletions src/server/game/Auction/AuctionHouseBot/AuctionHouseBot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,9 @@ void AuctionHouseBot::PrepareStatusInfos(std::unordered_map<AuctionHouseType, Au
for (AuctionQuality quality : EnumUtils::Iterate<AuctionQuality>())
statusInfo[ahType].QualityInfo[quality] = 0;

for (auto const& [auctionID, auction] : sAuctionMgr->GetAuctionsMap(ahType)->GetAuctions())
auto auctionObject{ sAuctionMgr->GetAuctionsMap(ahType) };

auctionObject->ForEachAuctions([ahType, &statusInfo](AuctionEntry* auction)
{
if (Item* item = sAuctionMgr->GetAuctionItem(auction->ItemGuid))
{
Expand All @@ -508,18 +510,21 @@ void AuctionHouseBot::PrepareStatusInfos(std::unordered_map<AuctionHouseType, Au
++statusInfo[ahType].ItemsCount;
}
}
}
});
}
}

void AuctionHouseBot::Rebuild(bool all)
{
for (uint32 i = 0; i < MAX_AUCTION_HOUSE_TYPE; ++i)
{
for (auto const& [auctionID, auction] : sAuctionMgr->GetAuctionsMap(i)->GetAuctions())
auto auctionObject{ sAuctionMgr->GetAuctionsMap(i) };
auctionObject->ForEachAuctions([all](AuctionEntry* auction)
{
if (!auction->PlayerOwner || sAuctionBotConfig->IsBotChar(auction->PlayerOwner.GetCounter())) // Add only ahbot items
if (all || !auction->Bid) // expire now auction if no bid or forced
auction->ExpireTime = GameTime::GetGameTime();
});
}
}

Expand Down Expand Up @@ -574,4 +579,4 @@ void AuctionHouseBot::UpdateAgents()
LOG_DEBUG("ahbot", "");

ScheduleAsyncUpdateAgents();
}
}
14 changes: 8 additions & 6 deletions src/server/game/Auction/AuctionHouseBot/AuctionHouseBotBuyer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,23 @@ bool AuctionBotBuyer::Update(AuctionHouseType houseType)
}

// Collects information about item counts and minimum prices to SameItemInfo and updates EligibleItems - a list with new items eligible for bot to buy and bid
// Returns count of items in AH that were eligible for being bought or bidded on by ahbot buyer (EligibleItems size)
// Returns count of items in AH that were eligible for being bought or bid on by ahbot buyer (EligibleItems size)
uint32 AuctionBotBuyer::GetItemInformation(BuyerConfiguration& config)
{
config.SameItemInfo.clear();
auto timeNow{ GameTime::GetGameTime() };
uint32 count = 0;

for (auto const& [auctionID, auction] : sAuctionMgr->GetAuctionsMap(config.GetHouseType())->GetAuctions())
auto auctionObject{ sAuctionMgr->GetAuctionsMap(config.GetHouseType()) };

auctionObject->ForEachAuctions([this, timeNow, &config, &count](AuctionEntry* auction)
{
if (!auction->PlayerOwner || sAuctionBotConfig->IsBotChar(auction->PlayerOwner.GetCounter()))
continue; // Skip auctions owned by AHBot
return; // Skip auctions owned by AHBot

Item* item = sAuctionMgr->GetAuctionItem(auction->ItemGuid);
if (!item)
continue;
return;

BuyerItemInfo& itemInfo = config.SameItemInfo[item->GetEntry()];

Expand Down Expand Up @@ -148,7 +150,7 @@ uint32 AuctionBotBuyer::GetItemInformation(BuyerConfiguration& config)
config.EligibleItems[auction->Id].AuctionId = auction->Id;
++count;
}
}
});

LOG_TRACE("ahbot", "AHBotBuyer: {} items added to buyable/biddable vector for ah type: {}", count, config.GetHouseType());
return count;
Expand Down Expand Up @@ -459,4 +461,4 @@ void AuctionBotBuyer::PlaceBidToEntry(AuctionEntry* auction, uint32 bidPrice)

// Run SQLs
CharacterDatabase.CommitTransaction(trans);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -564,8 +564,9 @@ void AuctionBotSeller::LoadSellerValues(SellerConfiguration& config)
uint32 AuctionBotSeller::SetStat(SellerConfiguration& config)
{
AllItemsArray itemsSaved(MAX_AUCTION_QUALITY, std::vector<uint32>(MAX_ITEM_CLASS));
auto auctionObject{ sAuctionMgr->GetAuctionsMap(config.GetHouseType()) };

for (auto const& [auctionID, auction] : sAuctionMgr->GetAuctionsMap(config.GetHouseType())->GetAuctions())
auctionObject->ForEachAuctions([this, &config, &itemsSaved](AuctionEntry* auction)
{
Item* item = sAuctionMgr->GetAuctionItem(auction->ItemGuid);
if (item)
Expand All @@ -575,7 +576,7 @@ uint32 AuctionBotSeller::SetStat(SellerConfiguration& config)
if (!auction->PlayerOwner || sAuctionBotConfig->IsBotChar(auction->PlayerOwner.GetCounter())) // Add only ahbot items
++itemsSaved[prototype->Quality][prototype->Class];
}
}
});

uint32 count = 0;
for (uint32 j = 0; j < MAX_AUCTION_QUALITY; ++j)
Expand Down Expand Up @@ -991,4 +992,4 @@ bool AuctionBotSeller::Update(AuctionHouseType houseType)
}

return false;
}
}
Loading
Loading