Merge pull request #508 from atidot3/extractregexfix

Optimised ExtractAllQuestIds ExtractAllItemIds
This commit is contained in:
Yunfan Li
2024-09-07 00:38:51 +08:00
committed by GitHub
3 changed files with 59 additions and 58 deletions

View File

@@ -593,34 +593,41 @@ void ChatHelper::eraseAllSubStr(std::string& mainStr, std::string const toErase)
}
}
std::set<uint32> ChatHelper::ExtractAllQuestIds(const std::string& text)
std::set<uint32> extractGeneric(std::string_view text, std::string_view prefix)
{
std::set<uint32> ids;
std::set<uint32_t> 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<uint32> ChatHelper::ExtractAllQuestIds(const std::string& text)
{
return extractGeneric(text, "Hquest:");
}
std::set<uint32> ChatHelper::ExtractAllItemIds(const std::string& text)
{
std::set<uint32> 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:");
}

View File

@@ -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;

View File

@@ -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())