Skip to content

Commit

Permalink
Add exhaustion to npc say and sell/buy item (#459)
Browse files Browse the repository at this point in the history
Prevent them from using WPE to bug or crash the server
  • Loading branch information
dudantas committed Jul 27, 2022
1 parent be3fb37 commit 3c5831f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/creatures/players/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5613,6 +5613,15 @@ bool Player::isMarketExhausted() const {
return (OTSYS_TIME() - lastMarketInteraction < exhaust_time);
}

// Player talk with npc exhausted
bool Player::isNpcExhausted(uint32_t exhaustionTime /*= 250*/) const {
return (OTSYS_TIME() - lastNpcInteraction < exhaustionTime);
}

void Player::updateNpcExhausted() {
lastNpcInteraction = OTSYS_TIME();
}

uint64_t Player::getItemCustomPrice(uint16_t itemId, bool buyPrice/* = false*/) const
{
auto it = itemPriceMap.find(itemId);
Expand Down
6 changes: 5 additions & 1 deletion src/creatures/players/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -1692,6 +1692,9 @@ class Player final : public Creature, public Cylinder
lastMarketInteraction = OTSYS_TIME();
}

bool isNpcExhausted(uint32_t exhaustionTime = 150) const;
void updateNpcExhausted();

bool isQuickLootListedItem(const Item* item) const {
if (!item) {
return false;
Expand Down Expand Up @@ -2211,7 +2214,8 @@ class Player final : public Creature, public Cylinder
int64_t skullTicks = 0;
int64_t lastWalkthroughAttempt = 0;
int64_t lastToggleMount = 0;
int64_t lastMarketInteraction = 0; // Market exhaust.
int64_t lastMarketInteraction = 0;
int64_t lastNpcInteraction = 0;
int64_t lastStashInteraction = 0;
int64_t lastDepotSearchInteraction = 0;
int64_t lastPing;
Expand Down
27 changes: 27 additions & 0 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4373,7 +4373,14 @@ void Game::playerBuyItem(uint32_t playerId, uint16_t itemId, uint8_t count, uint
return;
}

// Check npc say exhausted
if (player->isNpcExhausted()) {
player->sendCancelMessage(RETURNVALUE_YOUAREEXHAUSTED);
return;
}

merchant->onPlayerBuyItem(player, it.id, count, amount, ignoreCap, inBackpacks);
player->updateNpcExhausted();
}

void Game::playerSellItem(uint32_t playerId, uint16_t itemId, uint8_t count, uint8_t amount, bool ignoreEquipped)
Expand All @@ -4397,7 +4404,14 @@ void Game::playerSellItem(uint32_t playerId, uint16_t itemId, uint8_t count, uin
return;
}

// Check npc say exhausted
if (player->isNpcExhausted()) {
player->sendCancelMessage(RETURNVALUE_YOUAREEXHAUSTED);
return;
}

merchant->onPlayerSellItem(player, it.id, count, amount, ignoreEquipped);
player->updateNpcExhausted();
}

void Game::playerCloseShop(uint32_t playerId)
Expand Down Expand Up @@ -5323,13 +5337,26 @@ bool Game::playerSpeakTo(Player* player, SpeakClasses type, const std::string& r

void Game::playerSpeakToNpc(Player* player, const std::string& text)
{
if (player == nullptr) {
SPDLOG_ERROR("[Game::playerSpeakToNpc] - Player is nullptr");
return;
}

// Check npc say exhausted
if (player->isNpcExhausted()) {
player->sendCancelMessage(RETURNVALUE_YOUAREEXHAUSTED);
return;
}

SpectatorHashSet spectators;
map.getSpectators(spectators, player->getPosition());
for (Creature* spectator : spectators) {
if (spectator->getNpc()) {
spectator->onCreatureSay(player, TALKTYPE_PRIVATE_PN, text);
}
}

player->updateNpcExhausted();
}

//--
Expand Down

0 comments on commit 3c5831f

Please sign in to comment.