aboutsummaryrefslogtreecommitdiff
path: root/src/server/game/Handlers/SocialHandler.cpp
diff options
context:
space:
mode:
authorShauren <shauren.trinity@gmail.com>2020-07-30 17:41:20 +0200
committerShauren <shauren.trinity@gmail.com>2022-01-24 13:38:40 +0100
commit54a6e603ffc8b4913669cf0f189a966d25b620d8 (patch)
tree3b0f83d7e12a899113715cb018c4c2b9315141ed /src/server/game/Handlers/SocialHandler.cpp
parent1977d26050d1b0c6f2d21fe3316147ae0df9fc64 (diff)
Core/Misc: Replace database query in WorldSession::HandleAddFriendOpcode with async version
(cherry picked from commit 2f0893d279ddab86ae7c3e4fd1d7a47b15e938f7)
Diffstat (limited to 'src/server/game/Handlers/SocialHandler.cpp')
-rw-r--r--src/server/game/Handlers/SocialHandler.cpp93
1 files changed, 65 insertions, 28 deletions
diff --git a/src/server/game/Handlers/SocialHandler.cpp b/src/server/game/Handlers/SocialHandler.cpp
index 98fddfb491f..e84c64ab1f0 100644
--- a/src/server/game/Handlers/SocialHandler.cpp
+++ b/src/server/game/Handlers/SocialHandler.cpp
@@ -23,6 +23,7 @@
#include "ObjectAccessor.h"
#include "ObjectMgr.h"
#include "Player.h"
+#include "QueryCallback.h"
#include "RBAC.h"
#include "Realm.h"
#include "SocialMgr.h"
@@ -43,41 +44,77 @@ void WorldSession::HandleAddFriendOpcode(WorldPackets::Social::AddFriend& packet
TC_LOG_DEBUG("network", "WorldSession::HandleAddFriendOpcode: %s asked to add friend: %s",
GetPlayerInfo().c_str(), packet.Name.c_str());
- FriendsResult friendResult = FRIEND_NOT_FOUND;
- ObjectGuid friendGuid;
-
- if (CharacterCacheEntry const* characterInfo = sCharacterCache->GetCharacterCacheByName(packet.Name))
+ CharacterCacheEntry const* friendCharacterInfo = sCharacterCache->GetCharacterCacheByName(packet.Name);
+ if (!friendCharacterInfo)
{
- friendGuid = characterInfo->Guid;
- ObjectGuid friendAccountGuid = ObjectGuid::Create<HighGuid::WowAccount>(characterInfo->AccountId);
- uint32 team = Player::TeamForRace(characterInfo->Race);
- uint32 friendAccountId = characterInfo->AccountId;
+ sSocialMgr->SendFriendStatus(GetPlayer(), FRIEND_NOT_FOUND, ObjectGuid::Empty);
+ return;
+ }
- if (HasPermission(rbac::RBAC_PERM_ALLOW_GM_FRIEND) || AccountMgr::IsPlayerAccount(AccountMgr::GetSecurity(friendAccountId, realm.Id.Realm)))
+ auto processFriendRequest = [this,
+ playerGuid = _player->GetGUID(),
+ friendGuid = friendCharacterInfo->Guid,
+ friendAccountGuid = ObjectGuid::Create<HighGuid::WowAccount>(friendCharacterInfo->AccountId),
+ team = Player::TeamForRace(friendCharacterInfo->Race),
+ friendNote = std::move(packet.Notes)]()
+ {
+ if (playerGuid.GetCounter() != m_GUIDLow)
+ return; // not the player initiating request, do nothing
+
+ FriendsResult friendResult = FRIEND_NOT_FOUND;
+ if (friendGuid == GetPlayer()->GetGUID())
+ friendResult = FRIEND_SELF;
+ else if (GetPlayer()->GetTeam() != team && !HasPermission(rbac::RBAC_PERM_TWO_SIDE_ADD_FRIEND))
+ friendResult = FRIEND_ENEMY;
+ else if (GetPlayer()->GetSocial()->HasFriend(friendGuid))
+ friendResult = FRIEND_ALREADY;
+ else
{
- if (friendGuid == GetPlayer()->GetGUID())
- friendResult = FRIEND_SELF;
- else if (GetPlayer()->GetTeam() != team && !HasPermission(rbac::RBAC_PERM_TWO_SIDE_ADD_FRIEND))
- friendResult = FRIEND_ENEMY;
- else if (GetPlayer()->GetSocial()->HasFriend(friendGuid))
- friendResult = FRIEND_ALREADY;
+ Player* pFriend = ObjectAccessor::FindPlayer(friendGuid);
+ if (pFriend && pFriend->IsVisibleGloballyFor(GetPlayer()))
+ friendResult = FRIEND_ADDED_ONLINE;
else
- {
- Player* playerFriend = ObjectAccessor::FindPlayer(friendGuid);
- if (playerFriend && playerFriend->IsVisibleGloballyFor(GetPlayer()))
- friendResult = FRIEND_ADDED_ONLINE;
- else
- friendResult = FRIEND_ADDED_OFFLINE;
-
- if (GetPlayer()->GetSocial()->AddToSocialList(friendGuid, friendAccountGuid, SOCIAL_FLAG_FRIEND))
- GetPlayer()->GetSocial()->SetFriendNote(friendGuid, packet.Notes);
- else
- friendResult = FRIEND_LIST_FULL;
- }
+ friendResult = FRIEND_ADDED_OFFLINE;
+ if (GetPlayer()->GetSocial()->AddToSocialList(friendGuid, friendAccountGuid, SOCIAL_FLAG_FRIEND))
+ GetPlayer()->GetSocial()->SetFriendNote(friendGuid, friendNote);
+ else
+ friendResult = FRIEND_LIST_FULL;
+ }
+
+ sSocialMgr->SendFriendStatus(GetPlayer(), friendResult, friendGuid);
+ };
+
+ if (HasPermission(rbac::RBAC_PERM_ALLOW_GM_FRIEND))
+ {
+ processFriendRequest();
+ return;
+ }
+
+ // First try looking up friend candidate security from online object
+ if (Player* friendPlayer = ObjectAccessor::FindPlayer(friendCharacterInfo->Guid))
+ {
+ if (!AccountMgr::IsPlayerAccount(friendPlayer->GetSession()->GetSecurity()))
+ {
+ sSocialMgr->SendFriendStatus(GetPlayer(), FRIEND_NOT_FOUND, ObjectGuid::Empty);
+ return;
}
+
+ processFriendRequest();
+ return;
}
- sSocialMgr->SendFriendStatus(GetPlayer(), friendResult, friendGuid);
+ // When not found, consult database
+ GetQueryProcessor().AddCallback(AccountMgr::GetSecurityAsync(friendCharacterInfo->AccountId, realm.Id.Realm,
+ [this, continuation = std::move(processFriendRequest)](uint32 friendSecurity)
+ {
+ if (!AccountMgr::IsPlayerAccount(friendSecurity))
+ {
+ sSocialMgr->SendFriendStatus(GetPlayer(), FRIEND_NOT_FOUND, ObjectGuid::Empty);
+ return;
+ }
+
+ continuation();
+ }));
}
void WorldSession::HandleDelFriendOpcode(WorldPackets::Social::DelFriend& packet)