diff --git a/src/ChatHelper.cpp b/src/ChatHelper.cpp index 156dc0ea..8f464d5d 100644 --- a/src/ChatHelper.cpp +++ b/src/ChatHelper.cpp @@ -593,34 +593,41 @@ void ChatHelper::eraseAllSubStr(std::string& mainStr, std::string const toErase) } } -std::set ChatHelper::ExtractAllQuestIds(const std::string& text) +std::set extractGeneric(std::string_view text, std::string_view prefix) { - std::set ids; + std::set ids; + std::string_view text_view = text; - std::regex rgx("Hquest:[0-9]+"); - auto begin = std::sregex_iterator(text.begin(), text.end(), rgx); - auto end = std::sregex_iterator(); - for (std::sregex_iterator i = begin; i != end; ++i) + size_t pos = 0; + while ((pos = text_view.find(prefix, pos)) != std::string::npos) { - std::smatch match = *i; - ids.insert(std::stoi(match.str().erase(0, 7))); + // skip "Hquest:/Hitem:" + pos += prefix.size(); + + // extract everything after "Hquest:/Hitem:" + size_t end_pos = text_view.find_first_not_of("0123456789", pos); + std::string_view number_str = text_view.substr(pos, end_pos - pos); + + uint32 number = 0; + + auto [ptr, ec] = std::from_chars(number_str.data(), number_str.data() + number_str.size(), number); + + if (ec == std::errc()) + { + ids.insert(number); + } + pos = end_pos; } return ids; } +std::set ChatHelper::ExtractAllQuestIds(const std::string& text) +{ + return extractGeneric(text, "Hquest:"); +} + std::set ChatHelper::ExtractAllItemIds(const std::string& text) { - std::set ids; - - std::regex rgx("Hitem:[0-9]+"); - auto begin = std::sregex_iterator(text.begin(), text.end(), rgx); - auto end = std::sregex_iterator(); - for (std::sregex_iterator i = begin; i != end; ++i) - { - std::smatch match = *i; - ids.insert(std::stoi(match.str().erase(0, 6))); - } - - return ids; + return extractGeneric(text, "Hitem:"); } diff --git a/src/PlayerbotAI.cpp b/src/PlayerbotAI.cpp index 0504b6b7..e8f0d8f2 100644 --- a/src/PlayerbotAI.cpp +++ b/src/PlayerbotAI.cpp @@ -1005,26 +1005,25 @@ void PlayerbotAI::HandleBotOutgoingPacket(WorldPacket const& packet) if (lang == LANG_ADDON) return; - // Disable since ExtractAllItemIds bad performance - // if (message.starts_with(sPlayerbotAIConfig->toxicLinksPrefix) && - // (GetChatHelper()->ExtractAllItemIds(message).size() > 0 || - // GetChatHelper()->ExtractAllQuestIds(message).size() > 0) && - // sPlayerbotAIConfig->toxicLinksRepliesChance) - // { - // if (urand(0, 50) > 0 || urand(1, 100) > sPlayerbotAIConfig->toxicLinksRepliesChance) - // { - // return; - // } - // } - // else if ((GetChatHelper()->ExtractAllItemIds(message).count(19019) && - // sPlayerbotAIConfig->thunderfuryRepliesChance)) - // { - // if (urand(0, 60) > 0 || urand(1, 100) > sPlayerbotAIConfig->thunderfuryRepliesChance) - // { - // return; - // } - // } - // else + if (message.starts_with(sPlayerbotAIConfig->toxicLinksPrefix) && + (GetChatHelper()->ExtractAllItemIds(message).size() > 0 || + GetChatHelper()->ExtractAllQuestIds(message).size() > 0) && + sPlayerbotAIConfig->toxicLinksRepliesChance) + { + if (urand(0, 50) > 0 || urand(1, 100) > sPlayerbotAIConfig->toxicLinksRepliesChance) + { + return; + } + } + else if ((GetChatHelper()->ExtractAllItemIds(message).count(19019) && + sPlayerbotAIConfig->thunderfuryRepliesChance)) + { + if (urand(0, 60) > 0 || urand(1, 100) > sPlayerbotAIConfig->thunderfuryRepliesChance) + { + return; + } + } + else { if (isFromFreeBot && urand(0, 20)) return; diff --git a/src/strategy/actions/SayAction.cpp b/src/strategy/actions/SayAction.cpp index 5b12fabf..93d36a00 100644 --- a/src/strategy/actions/SayAction.cpp +++ b/src/strategy/actions/SayAction.cpp @@ -205,21 +205,20 @@ void ChatReplyAction::ChatReplyDo(Player* bot, uint32& type, uint32& guid1, uint return; } - // Disable since ExtractAllItemIds bad performance - // //toxic links - // if (msg.starts_with(sPlayerbotAIConfig->toxicLinksPrefix) - // && (GET_PLAYERBOT_AI(bot)->GetChatHelper()->ExtractAllItemIds(msg).size() > 0 || GET_PLAYERBOT_AI(bot)->GetChatHelper()->ExtractAllQuestIds(msg).size() > 0)) - // { - // HandleToxicLinksReply(bot, chatChannelSource, msg, name); - // return; - // } + //toxic links + if (msg.starts_with(sPlayerbotAIConfig->toxicLinksPrefix) + && (GET_PLAYERBOT_AI(bot)->GetChatHelper()->ExtractAllItemIds(msg).size() > 0 || GET_PLAYERBOT_AI(bot)->GetChatHelper()->ExtractAllQuestIds(msg).size() > 0)) + { + HandleToxicLinksReply(bot, chatChannelSource, msg, name); + return; + } - // //thunderfury - // if (GET_PLAYERBOT_AI(bot)->GetChatHelper()->ExtractAllItemIds(msg).count(19019)) - // { - // HandleThunderfuryReply(bot, chatChannelSource, msg, name); - // return; - // } + //thunderfury + if (GET_PLAYERBOT_AI(bot)->GetChatHelper()->ExtractAllItemIds(msg).count(19019)) + { + HandleThunderfuryReply(bot, chatChannelSource, msg, name); + return; + } auto messageRepy = GenerateReplyMessage(bot, msg, guid1, name); SendGeneralResponse(bot, chatChannelSource, messageRepy, name); @@ -321,8 +320,6 @@ bool ChatReplyAction::HandleToxicLinksReply(Player* bot, ChatChannelSource chatC } bool ChatReplyAction::HandleWTBItemsReply(Player* bot, ChatChannelSource chatChannelSource, std::string& msg, std::string& name) { - // Disable since ExtractAllItemIds bad performance - return false; auto messageItemIds = GET_PLAYERBOT_AI(bot)->GetChatHelper()->ExtractAllItemIds(msg); if (messageItemIds.empty()) @@ -418,8 +415,6 @@ bool ChatReplyAction::HandleWTBItemsReply(Player* bot, ChatChannelSource chatCha } bool ChatReplyAction::HandleLFGQuestsReply(Player* bot, ChatChannelSource chatChannelSource, std::string& msg, std::string& name) { - // Disable since ExtractAllQuestIds bad performance - return false; auto messageQuestIds = GET_PLAYERBOT_AI(bot)->GetChatHelper()->ExtractAllQuestIds(msg); if (messageQuestIds.empty())