Skip to content

Commit

Permalink
* Optimized the onPlayerSellAllLoot code to prevent prolonged freezes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jprzimba committed Dec 18, 2024
1 parent 945e8a6 commit 6f45409
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
1 change: 1 addition & 0 deletions markdowns/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Protocol 14.05 support. ([Tryller](https://github.com/jprzimba))
- New protocol 14.05 assets. ([Tryller](https://github.com/jprzimba))
- Fix gotoHouse talkaction. ([Tryller](https://github.com/jprzimba))
- Optimized the `onPlayerSellAllLoot` code to prevent prolonged freezes. ([Tryller](https://github.com/jprzimba))

## Added files

Expand Down
50 changes: 31 additions & 19 deletions src/creatures/npcs/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,48 +443,60 @@ void Npc::onPlayerSellAllLoot(uint32_t playerId, uint16_t itemId, bool ignore, u
if (!player) {
return;
}

if (itemId == ITEM_GOLD_POUCH) {
const auto &container = player->getLootPouch();
if (!container) {
return;
}
bool hasMore = false;

uint64_t toSellCount = 0;
phmap::flat_hash_map<uint16_t, uint16_t> toSell;
for (ContainerIterator it = container->iterator(); it.hasNext(); it.advance()) {
if (toSellCount >= 500) {
hasMore = true;
break;
}
const auto &item = *it;
ContainerIterator it = container->iterator();

while (it.hasNext()) {
auto item = *it;
if (!item) {
continue;
}

toSell[item->getID()] += item->getItemAmount();
if (item->isStackable()) {
toSellCount++;
} else {
toSellCount += item->getItemAmount();
}

if (toSellCount >= 100) {
break;
}

it.advance();
}
for (const auto &[m_itemId, amount] : toSell) {
onPlayerSellItem(player, m_itemId, 0, amount, ignore, totalPrice, container);
}
auto ss = std::stringstream();
if (totalPrice == 0) {

if (toSell.empty()) {
std::stringstream ss;
ss << "You have no items in your loot pouch.";
player->sendTextMessage(MESSAGE_TRANSACTION, ss.str());
player->sendTextMessage(MESSAGE_FAILURE, ss.str());
return;
}
if (hasMore) {
g_dispatcher().scheduleEvent(
SCHEDULER_MINTICKS, [this, playerId = player->getID(), itemId, ignore, totalPrice] { onPlayerSellAllLoot(playerId, itemId, ignore, totalPrice); }, __FUNCTION__
);
return;

for (auto &[itemId, amount] : toSell) {
onPlayerSellItem(player, itemId, 0, amount, ignore, totalPrice, container);
}

const auto &task = player->createPlayerTask(
100, [this, playerId, itemId, ignore, totalPrice]() {
onPlayerSellAllLoot(playerId, itemId, ignore, totalPrice);
},
__FUNCTION__
);
player->setNextActionPushTask(task);

auto ss = std::stringstream();
ss << "You sold all of the items from your loot pouch for ";
ss << totalPrice << " gold.";
player->sendTextMessage(MESSAGE_TRANSACTION, ss.str());
player->sendTextMessage(MESSAGE_LOOK, ss.str());
player->openPlayerContainers();
}
}
Expand Down

0 comments on commit 6f45409

Please sign in to comment.