diff options
author | Shauren <shauren.trinity@gmail.com> | 2020-09-27 17:07:47 +0200 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2020-09-27 17:07:47 +0200 |
commit | 0a9e239c12ceb1d4a5ef5cdc796320a403b3da2b (patch) | |
tree | d89def44f7424998cad146056fd2b6001d2b259b /src | |
parent | 93f6e7431a73acc2047739ddf6ec006f9ccb59ec (diff) |
Core/Mail: Replaced blocking db query in mail sending with async version
Diffstat (limited to 'src')
-rw-r--r-- | src/server/database/Database/Implementation/CharacterDatabase.cpp | 2 | ||||
-rw-r--r-- | src/server/game/Handlers/MailHandler.cpp | 269 |
2 files changed, 130 insertions, 141 deletions
diff --git a/src/server/database/Database/Implementation/CharacterDatabase.cpp b/src/server/database/Database/Implementation/CharacterDatabase.cpp index d181866634c..75e2895534b 100644 --- a/src/server/database/Database/Implementation/CharacterDatabase.cpp +++ b/src/server/database/Database/Implementation/CharacterDatabase.cpp @@ -97,7 +97,7 @@ void CharacterDatabaseConnection::DoPrepareStatements() PrepareStatement(CHAR_SEL_CHARACTER_INVENTORY, "SELECT creatorGuid, giftCreatorGuid, count, duration, charges, flags, enchantments, randomPropertyId, durability, playedTime, text, bag, slot, " "item, itemEntry FROM character_inventory ci JOIN item_instance ii ON ci.item = ii.guid WHERE ci.guid = ? ORDER BY bag, slot", CONNECTION_ASYNC); PrepareStatement(CHAR_SEL_CHARACTER_ACTIONS, "SELECT a.button, a.action, a.type FROM character_action as a, characters as c WHERE a.guid = c.guid AND a.spec = c.activeTalentGroup AND a.guid = ? ORDER BY button", CONNECTION_ASYNC); - PrepareStatement(CHAR_SEL_MAIL_COUNT, "SELECT COUNT(*) FROM mail WHERE receiver = ?", CONNECTION_SYNCH); + PrepareStatement(CHAR_SEL_MAIL_COUNT, "SELECT COUNT(*) FROM mail WHERE receiver = ?", CONNECTION_ASYNC); PrepareStatement(CHAR_SEL_CHARACTER_SOCIALLIST, "SELECT friend, flags, note FROM character_social JOIN characters ON characters.guid = character_social.friend WHERE character_social.guid = ? AND deleteinfos_name IS NULL LIMIT 255", CONNECTION_ASYNC); PrepareStatement(CHAR_SEL_CHARACTER_HOMEBIND, "SELECT mapId, zoneId, posX, posY, posZ FROM character_homebind WHERE guid = ?", CONNECTION_ASYNC); PrepareStatement(CHAR_SEL_CHARACTER_SPELLCOOLDOWNS, "SELECT spell, item, time, categoryId, categoryEnd FROM character_spell_cooldown WHERE guid = ? AND time > UNIX_TIMESTAMP()", CONNECTION_ASYNC); diff --git a/src/server/game/Handlers/MailHandler.cpp b/src/server/game/Handlers/MailHandler.cpp index 4d97191cecc..224e9643490 100644 --- a/src/server/game/Handlers/MailHandler.cpp +++ b/src/server/game/Handlers/MailHandler.cpp @@ -133,189 +133,178 @@ void WorldSession::HandleSendMail(WorldPackets::Mail::SendMail& sendMail) return; } - Player* receiver = ObjectAccessor::FindConnectedPlayer(receiverGuid); - - uint32 receiverTeam = 0; - uint8 mailsCount = 0; //do not allow to send to one player more than 100 mails - uint8 receiverLevel = 0; - uint32 receiverAccountId = 0; - - if (receiver) - { - receiverTeam = receiver->GetTeam(); - mailsCount = receiver->GetMailSize(); - receiverLevel = receiver->GetLevel(); - receiverAccountId = receiver->GetSession()->GetAccountId(); - } - else + auto mailCountCheckContinuation = [this, player = _player, receiverGuid, mailInfo = std::move(sendMail.Info), reqmoney, cost](uint32 receiverTeam, uint64 mailsCount, uint8 receiverLevel, uint32 receiverAccountId) mutable { - if (CharacterCacheEntry const* characterInfo = sCharacterCache->GetCharacterCacheByGuid(receiverGuid)) - { - receiverTeam = Player::TeamForRace(characterInfo->Race); - receiverLevel = characterInfo->Level; - receiverAccountId = characterInfo->AccountId; - } - - CharacterDatabasePreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_SEL_MAIL_COUNT); - stmt->setUInt32(0, receiverGuid.GetCounter()); + if (_player != player) + return; - PreparedQueryResult result = CharacterDatabase.Query(stmt); - if (result) + // do not allow to have more than 100 mails in mailbox.. mails count is in opcode uint8!!! - so max can be 255.. + if (mailsCount > 100) { - Field* fields = result->Fetch(); - mailsCount = fields[0].GetUInt64(); + player->SendMailResult(0, MAIL_SEND, MAIL_ERR_RECIPIENT_CAP_REACHED); + return; } - } - - // do not allow to have more than 100 mails in mailbox.. mails count is in opcode uint8!!! - so max can be 255.. - if (mailsCount > 100) - { - player->SendMailResult(0, MAIL_SEND, MAIL_ERR_RECIPIENT_CAP_REACHED); - return; - } - // test the receiver's Faction... or all items are account bound - bool accountBound = !sendMail.Info.Attachments.empty(); - for (auto const& att : sendMail.Info.Attachments) - { - if (Item* item = player->GetItemByGuid(att.ItemGUID)) + // test the receiver's Faction... or all items are account bound + bool accountBound = !mailInfo.Attachments.empty(); + for (auto const& att : mailInfo.Attachments) { - ItemTemplate const* itemProto = item->GetTemplate(); - if (!itemProto || !itemProto->HasFlag(ITEM_FLAG_IS_BOUND_TO_ACCOUNT)) + if (Item* item = player->GetItemByGuid(att.ItemGUID)) { - accountBound = false; - break; + ItemTemplate const* itemProto = item->GetTemplate(); + if (!itemProto || !itemProto->HasFlag(ITEM_FLAG_IS_BOUND_TO_ACCOUNT)) + { + accountBound = false; + break; + } } } - } - - if (!accountBound && player->GetTeam() != receiverTeam && !HasPermission(rbac::RBAC_PERM_TWO_SIDE_INTERACTION_MAIL)) - { - player->SendMailResult(0, MAIL_SEND, MAIL_ERR_NOT_YOUR_TEAM); - return; - } - - if (receiverLevel < sWorld->getIntConfig(CONFIG_MAIL_LEVEL_REQ)) - { - SendNotification(GetTrinityString(LANG_MAIL_RECEIVER_REQ), sWorld->getIntConfig(CONFIG_MAIL_LEVEL_REQ)); - return; - } - - std::vector<Item*> items; - for (auto const& att : sendMail.Info.Attachments) - { - if (att.ItemGUID.IsEmpty()) + if (!accountBound && player->GetTeam() != receiverTeam && !HasPermission(rbac::RBAC_PERM_TWO_SIDE_INTERACTION_MAIL)) { - player->SendMailResult(0, MAIL_SEND, MAIL_ERR_MAIL_ATTACHMENT_INVALID); + player->SendMailResult(0, MAIL_SEND, MAIL_ERR_NOT_YOUR_TEAM); return; } - Item* item = player->GetItemByGuid(att.ItemGUID); - - // prevent sending bag with items (cheat: can be placed in bag after adding equipped empty bag to mail) - if (!item) + if (receiverLevel < sWorld->getIntConfig(CONFIG_MAIL_LEVEL_REQ)) { - player->SendMailResult(0, MAIL_SEND, MAIL_ERR_MAIL_ATTACHMENT_INVALID); + SendNotification(GetTrinityString(LANG_MAIL_RECEIVER_REQ), sWorld->getIntConfig(CONFIG_MAIL_LEVEL_REQ)); return; } - if (!item->CanBeTraded(true)) - { - player->SendMailResult(0, MAIL_SEND, MAIL_ERR_EQUIP_ERROR, EQUIP_ERR_MAIL_BOUND_ITEM); - return; - } + std::vector<Item*> items; - if (item->IsBoundAccountWide() && item->IsSoulBound() && player->GetSession()->GetAccountId() != receiverAccountId) + for (auto const& att : mailInfo.Attachments) { - player->SendMailResult(0, MAIL_SEND, MAIL_ERR_EQUIP_ERROR, EQUIP_ERR_ARTEFACTS_ONLY_FOR_OWN_CHARACTERS); - return; - } + if (att.ItemGUID.IsEmpty()) + { + player->SendMailResult(0, MAIL_SEND, MAIL_ERR_MAIL_ATTACHMENT_INVALID); + return; + } - if (item->GetTemplate()->HasFlag(ITEM_FLAG_CONJURED) || item->GetUInt32Value(ITEM_FIELD_DURATION)) - { - player->SendMailResult(0, MAIL_SEND, MAIL_ERR_EQUIP_ERROR, EQUIP_ERR_MAIL_BOUND_ITEM); - return; - } + Item* item = player->GetItemByGuid(att.ItemGUID); - if (sendMail.Info.Cod && item->IsWrapped()) - { - player->SendMailResult(0, MAIL_SEND, MAIL_ERR_CANT_SEND_WRAPPED_COD); - return; - } + // prevent sending bag with items (cheat: can be placed in bag after adding equipped empty bag to mail) + if (!item) + { + player->SendMailResult(0, MAIL_SEND, MAIL_ERR_MAIL_ATTACHMENT_INVALID); + return; + } - if (item->IsNotEmptyBag()) - { - player->SendMailResult(0, MAIL_SEND, MAIL_ERR_EQUIP_ERROR, EQUIP_ERR_CAN_ONLY_DO_WITH_EMPTY_BAGS); - return; - } + if (!item->CanBeTraded(true)) + { + player->SendMailResult(0, MAIL_SEND, MAIL_ERR_EQUIP_ERROR, EQUIP_ERR_MAIL_BOUND_ITEM); + return; + } - items.push_back(item); - } + if (item->IsBoundAccountWide() && item->IsSoulBound() && GetAccountId() != receiverAccountId) + { + player->SendMailResult(0, MAIL_SEND, MAIL_ERR_EQUIP_ERROR, EQUIP_ERR_ARTEFACTS_ONLY_FOR_OWN_CHARACTERS); + return; + } - player->SendMailResult(0, MAIL_SEND, MAIL_OK); + if (item->GetTemplate()->HasFlag(ITEM_FLAG_CONJURED) || item->GetUInt32Value(ITEM_FIELD_DURATION)) + { + player->SendMailResult(0, MAIL_SEND, MAIL_ERR_EQUIP_ERROR, EQUIP_ERR_MAIL_BOUND_ITEM); + return; + } - player->ModifyMoney(-int32(reqmoney)); - player->UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_GOLD_SPENT_FOR_MAIL, cost); + if (mailInfo.Cod && item->IsWrapped()) + { + player->SendMailResult(0, MAIL_SEND, MAIL_ERR_CANT_SEND_WRAPPED_COD); + return; + } - bool needItemDelay = false; + if (item->IsNotEmptyBag()) + { + player->SendMailResult(0, MAIL_SEND, MAIL_ERR_EQUIP_ERROR, EQUIP_ERR_CAN_ONLY_DO_WITH_EMPTY_BAGS); + return; + } - MailDraft draft(sendMail.Info.Subject, sendMail.Info.Body); + items.push_back(item); + } - CharacterDatabaseTransaction trans = CharacterDatabase.BeginTransaction(); + player->SendMailResult(0, MAIL_SEND, MAIL_OK); - if (!sendMail.Info.Attachments.empty() || sendMail.Info.SendMoney > 0) - { - bool log = HasPermission(rbac::RBAC_PERM_LOG_GM_TRADE); - if (!sendMail.Info.Attachments.empty()) + player->ModifyMoney(-int32(reqmoney)); + player->UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_GOLD_SPENT_FOR_MAIL, cost); + + bool needItemDelay = false; + + MailDraft draft(mailInfo.Subject, mailInfo.Body); + + CharacterDatabaseTransaction trans = CharacterDatabase.BeginTransaction(); + + if (!mailInfo.Attachments.empty() || mailInfo.SendMoney > 0) { - for (Item* item : items) + bool log = HasPermission(rbac::RBAC_PERM_LOG_GM_TRADE); + if (!mailInfo.Attachments.empty()) { - if (log) + for (Item* item : items) { - sLog->outCommand(GetAccountId(), "GM %s (GUID: %u) (Account: %u) mail item: %s (Entry: %u Count: %u) " - "to: %s (%s) (Account: %u)", GetPlayerName().c_str(), GetGUIDLow(), GetAccountId(), - item->GetTemplate()->Name1.c_str(), item->GetEntry(), item->GetCount(), - sendMail.Info.Target.c_str(), receiverGuid.ToString().c_str(), receiverAccountId); - } - - item->SetNotRefundable(GetPlayer()); // makes the item no longer refundable - player->MoveItemFromInventory(item->GetBagSlot(), item->GetSlot(), true); + if (log) + { + sLog->outCommand(GetAccountId(), "GM %s (GUID: %u) (Account: %u) mail item: %s (Entry: %u Count: %u) " + "to: %s (%s) (Account: %u)", GetPlayerName().c_str(), GetGUIDLow(), GetAccountId(), + item->GetTemplate()->Name1.c_str(), item->GetEntry(), item->GetCount(), + mailInfo.Target.c_str(), receiverGuid.ToString().c_str(), receiverAccountId); + } + + item->SetNotRefundable(GetPlayer()); // makes the item no longer refundable + player->MoveItemFromInventory(item->GetBagSlot(), item->GetSlot(), true); + + item->DeleteFromInventoryDB(trans); // deletes item from character's inventory + item->SetOwnerGUID(receiverGuid); + item->SetState(ITEM_CHANGED); + item->SaveToDB(trans); // recursive and not have transaction guard into self, item not in inventory and can be save standalone - item->DeleteFromInventoryDB(trans); // deletes item from character's inventory - item->SetOwnerGUID(receiverGuid); - item->SetState(ITEM_CHANGED); - item->SaveToDB(trans); // recursive and not have transaction guard into self, item not in inventory and can be save standalone + draft.AddItem(item); + } - draft.AddItem(item); + // if item send to character at another account, then apply item delivery delay + needItemDelay = GetAccountId() != receiverAccountId; } - // if item send to character at another account, then apply item delivery delay - needItemDelay = player->GetSession()->GetAccountId() != receiverAccountId; + if (log && mailInfo.SendMoney > 0) + { + sLog->outCommand(GetAccountId(), "GM %s (GUID: %u) (Account: %u) mail money: %u to: %s (%s) (Account: %u)", + GetPlayerName().c_str(), GetGUIDLow(), GetAccountId(), mailInfo.SendMoney, mailInfo.Target.c_str(), receiverGuid.ToString().c_str(), receiverAccountId); + } } - if (log && sendMail.Info.SendMoney > 0) - { - sLog->outCommand(GetAccountId(), "GM %s (%s) (Account: %u) mail money: %u to: %s (%s) (Account: %u)", - GetPlayerName().c_str(), _player->GetGUID().ToString().c_str(), GetAccountId(), sendMail.Info.SendMoney, sendMail.Info.Target.c_str(), receiverGuid.ToString().c_str(), receiverAccountId); - } - } + // If theres is an item, there is a one hour delivery delay if sent to another account's character. + uint32 deliver_delay = needItemDelay ? sWorld->getIntConfig(CONFIG_MAIL_DELIVERY_DELAY) : 0; + + // don't ask for COD if there are no items + if (mailInfo.Attachments.empty()) + mailInfo.Cod = 0; - // If theres is an item, there is a one hour delivery delay if sent to another account's character. - uint32 deliver_delay = needItemDelay ? sWorld->getIntConfig(CONFIG_MAIL_DELIVERY_DELAY) : 0; + // will delete item or place to receiver mail list + draft + .AddMoney(mailInfo.SendMoney) + .AddCOD(mailInfo.Cod) + .SendMailTo(trans, MailReceiver(ObjectAccessor::FindConnectedPlayer(receiverGuid), receiverGuid.GetCounter()), MailSender(player), mailInfo.Body.empty() ? MAIL_CHECK_MASK_COPIED : MAIL_CHECK_MASK_HAS_BODY, deliver_delay); - // don't ask for COD if there are no items - if (sendMail.Info.Attachments.empty()) - sendMail.Info.Cod = 0; + player->SaveInventoryAndGoldToDB(trans); + CharacterDatabase.CommitTransaction(trans); + }; - // will delete item or place to receiver mail list - draft - .AddMoney(sendMail.Info.SendMoney) - .AddCOD(sendMail.Info.Cod) - .SendMailTo(trans, MailReceiver(receiver, receiverGuid.GetCounter()), MailSender(player), sendMail.Info.Body.empty() ? MAIL_CHECK_MASK_COPIED : MAIL_CHECK_MASK_HAS_BODY, deliver_delay); + if (Player* receiver = ObjectAccessor::FindConnectedPlayer(receiverGuid)) + { + mailCountCheckContinuation(receiver->GetTeam(), receiver->GetMailSize(), receiver->GetLevel(), receiver->GetSession()->GetAccountId()); + } + else + { + CharacterDatabasePreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_SEL_MAIL_COUNT); + stmt->setUInt32(0, receiverGuid.GetCounter()); - player->SaveInventoryAndGoldToDB(trans); - CharacterDatabase.CommitTransaction(trans); + GetQueryProcessor().AddCallback(CharacterDatabase.AsyncQuery(stmt) + .WithPreparedCallback([continuation = std::move(mailCountCheckContinuation), receiverGuid](PreparedQueryResult result) mutable + { + if (CharacterCacheEntry const* characterInfo = sCharacterCache->GetCharacterCacheByGuid(receiverGuid)) + continuation(Player::TeamForRace(characterInfo->Race), result ? (*result)[0].GetUInt64() : UI64LIT(0), characterInfo->Level, characterInfo->AccountId); + })); + } } //called when mail is read |