feat(Core/Player): add helper for send large count mail items (#8460)

This commit is contained in:
Kargatum
2021-10-14 15:25:15 +07:00
committed by GitHub
parent 5fafa92594
commit 1c40caa4d6
3 changed files with 90 additions and 20 deletions

View File

@@ -416,3 +416,83 @@ void Player::UpdateDuelFlag(time_t currTime)
}
/*********************************************************/
void Player::SendItemRetrievalMail(uint32 itemEntry, uint32 count)
{
SendItemRetrievalMail({ { itemEntry, count } });
}
void Player::SendItemRetrievalMail(std::vector<std::pair<uint32, uint32>> mailItems)
{
if (mailItems.empty())
{
// Skip send if empty items
FMT_LOG_ERROR("entities.player.items", "> SendItemRetrievalMail: Attempt to send almost with items without items. Player {}", GetGUID().ToString());
return;
}
using SendMailTempateVector = std::vector<std::pair<uint32, uint32>>;
std::vector<SendMailTempateVector> allItems;
auto AddMailItem = [&allItems](uint32 itemEntry, uint32 itemCount)
{
SendMailTempateVector toSendItems;
ItemTemplate const* itemTemplate = sObjectMgr->GetItemTemplate(itemEntry);
if (!itemTemplate)
{
FMT_LOG_ERROR("entities.player.items", "> SendItemRetrievalMail: Item id {} is invalid", itemEntry);
return;
}
if (itemCount < 1 || (itemTemplate->MaxCount > 0 && itemCount > static_cast<uint32>(itemTemplate->MaxCount)))
{
FMT_LOG_ERROR("entities.player.items", "> SendItemRetrievalMail: Incorrect item count ({}) for item id {}", itemEntry, itemCount);
return;
}
while (itemCount > itemTemplate->GetMaxStackSize())
{
if (toSendItems.size() <= MAX_MAIL_ITEMS)
{
toSendItems.emplace_back(itemEntry, itemTemplate->GetMaxStackSize());
itemCount -= itemTemplate->GetMaxStackSize();
}
else
{
allItems.emplace_back(toSendItems);
toSendItems.clear();
}
}
toSendItems.emplace_back(itemEntry, itemCount);
allItems.emplace_back(toSendItems);
};
for (auto& [itemEntry, itemCount] : mailItems)
{
AddMailItem(itemEntry, itemCount);
}
CharacterDatabaseTransaction trans = CharacterDatabase.BeginTransaction();
for (auto const& items : allItems)
{
MailSender sender(MAIL_CREATURE, 34337 /* The Postmaster */);
MailDraft draft("Recovered Item", "We recovered a lost item in the twisting nether and noted that it was yours.$B$BPlease find said object enclosed."); // This is the text used in Cataclysm, it probably wasn't changed.
for (auto const& [itemEntry, itemCount] : items)
{
if (Item* mailItem = Item::CreateItem(itemEntry, itemCount))
{
mailItem->SaveToDB(trans);
draft.AddItem(mailItem);
}
}
draft.SendMailTo(trans, MailReceiver(this, GetGUID().GetCounter()), sender);
}
CharacterDatabase.CommitTransaction(trans);
}