void ProtocolGame::sendShop(const std::shared_ptr<Npc> &npc) {
Benchmark brenchmark;
NetworkMessage msg;
msg.addByte(0x7A);
msg.addString(npc->getName());
if (!oldProtocol) {
msg.add<uint16_t>(npc->getCurrency());
msg.addString(std::string()); // Currency name
}
const auto &shoplist = npc->getShopItemVector(player->getGUID());
uint16_t itemsToSend = std::min<size_t>(shoplist.size(), std::numeric_limits<uint16_t>::max());
msg.add<uint16_t>(itemsToSend);
// Initialize before the loop to avoid database overload on each iteration
auto talkactionHidden = player->kv()->get("npc-shop-hidden-sell-item");
// Initialize the inventoryMap outside the loop to avoid creation on each iteration
std::map<uint16_t, uint16_t> inventoryMap;
player->getAllSaleItemIdAndCount(inventoryMap);
uint16_t i = 0;
for (const ShopBlock &shopBlock : shoplist) {
if (++i > itemsToSend) {
break;
}
// Hidden sell items from the shop if they are not in the player's inventory
if (talkactionHidden && talkactionHidden->get<bool>()) {
const auto &foundItem = inventoryMap.find(shopBlock.itemId);
if (foundItem == inventoryMap.end() && shopBlock.itemSellPrice > 0 && shopBlock.itemBuyPrice == 0) {
AddHiddenShopItem(msg);
continue;
}
}
AddShopItem(msg, shopBlock);
}
writeToOutputBuffer(msg);
g_logger().debug("ProtocolGame::sendShop - Time: {} ms, shop items: {}", brenchmark.duration(), shoplist.size());
}