refactor(Core/Cache): move the GlobalPlayerCache to its own class (#9166)

This commit is contained in:
Skjalf
2021-11-18 12:53:36 -03:00
committed by GitHub
parent 00dc369cb6
commit 731d256420
47 changed files with 898 additions and 595 deletions

View File

@@ -195,7 +195,7 @@ public:
}
std::string oldCaptainName;
sObjectMgr->GetPlayerNameByGUID(arena->GetCaptain().GetCounter(), oldCaptainName);
sCharacterCache->GetCharacterNameByGuid(arena->GetCaptain(), oldCaptainName);
arena->SetCaptain(target->GetGUID());
handler->PSendSysMessage(LANG_ARENA_CAPTAIN, arena->GetName().c_str(), arena->GetId(), oldCaptainName.c_str(), target->GetName().c_str());

View File

@@ -24,6 +24,7 @@ EndScriptData */
#include "AccountMgr.h"
#include "BanMgr.h"
#include "CharacterCache.h"
#include "Chat.h"
#include "Language.h"
#include "ObjectAccessor.h"
@@ -326,7 +327,7 @@ public:
if (!target)
{
targetGuid = sWorld->GetGlobalPlayerGUID(name);
targetGuid = sCharacterCache->GetCharacterGuidByName(name);
if (!targetGuid)
{
handler->PSendSysMessage(LANG_BANINFO_NOCHARACTER);

View File

@@ -0,0 +1,157 @@
/*
* This file is part of the AzerothCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by the
* Free Software Foundation; either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ScriptMgr.h"
#include "Chat.h"
#include "Group.h"
#include "Language.h"
#include "Player.h"
using namespace Acore::ChatCommands;
class cache_commandscript : public CommandScript
{
public:
cache_commandscript() : CommandScript("cache_commandscript") { }
ChatCommandTable GetCommands() const override
{
static ChatCommandTable cacheCommandTable =
{
{ "info", HandleCacheInfoCommand, SEC_GAMEMASTER, Console::Yes },
{ "delete", HandleCacheDeleteCommand, SEC_ADMINISTRATOR, Console::Yes },
{ "refresh", HandleCacheRefreshCommand, SEC_GAMEMASTER, Console::Yes }
};
static ChatCommandTable commandTable =
{
{ "cache", cacheCommandTable },
};
return commandTable;
}
static bool HandleCacheInfoCommand(ChatHandler* handler, Optional<PlayerIdentifier> player)
{
if (!player)
{
player = PlayerIdentifier::FromTargetOrSelf(handler);
}
if (!player)
{
handler->SendSysMessage(LANG_PLAYER_NOT_FOUND);
handler->SetSentErrorMessage(true);
return false;
}
CharacterCacheEntry const* cache = sCharacterCache->GetCharacterCacheByGuid(player->GetGUID());
if (!cache)
{
handler->PSendSysMessage(LANG_COMMAND_CACHE_NOT_FOUND, player->GetName());
handler->SetSentErrorMessage(true);
return false;
}
handler->PSendSysMessage(LANG_COMMAND_CACHE_INFO, cache->Name, cache->Guid.GetCounter(), cache->AccountId,
cache->Class, cache->Race, cache->Sex, cache->Level, cache->MailCount, cache->GuildId, cache->GroupGuid.GetCounter(),
cache->ArenaTeamId[ARENA_SLOT_2v2], cache->ArenaTeamId[ARENA_SLOT_3v3], cache->ArenaTeamId[ARENA_SLOT_5v5]);
handler->SetSentErrorMessage(false);
return true;
}
static bool HandleCacheDeleteCommand(ChatHandler* handler, Optional<PlayerIdentifier> player)
{
if (!player)
{
player = PlayerIdentifier::FromTargetOrSelf(handler);
}
if (!player)
{
handler->SendSysMessage(LANG_PLAYER_NOT_FOUND);
handler->SetSentErrorMessage(true);
return false;
}
sCharacterCache->DeleteCharacterCacheEntry(player->GetGUID(), player->GetName());
handler->PSendSysMessage(LANG_COMMAND_CACHE_DELETE, player->GetName(), player->GetGUID().GetCounter());
handler->SetSentErrorMessage(false);
return true;
}
static bool HandleCacheRefreshCommand(ChatHandler* handler, Optional<PlayerIdentifier> player)
{
if (!player)
{
player = PlayerIdentifier::FromTargetOrSelf(handler);
}
if (!player)
{
handler->SendSysMessage(LANG_PLAYER_NOT_FOUND);
handler->SetSentErrorMessage(true);
return false;
}
if (player->IsConnected())
{
if (Player* cPlayer = ObjectAccessor::FindConnectedPlayer(player->GetGUID()))
{
if (sCharacterCache->HasCharacterCacheEntry(cPlayer->GetGUID()))
{
sCharacterCache->UpdateCharacterData(cPlayer->GetGUID(), cPlayer->GetName(), cPlayer->getGender(), cPlayer->getRace());
}
else
{
sCharacterCache->AddCharacterCacheEntry(cPlayer->GetGUID(), cPlayer->GetSession()->GetAccountId(), cPlayer->GetName(),
cPlayer->getGender(), cPlayer->getRace(), cPlayer->getClass(), cPlayer->getLevel());
}
sCharacterCache->UpdateCharacterAccountId(cPlayer->GetGUID(), cPlayer->GetSession()->GetAccountId());
sCharacterCache->UpdateCharacterGuildId(cPlayer->GetGUID(), cPlayer->GetGuildId());
sCharacterCache->UpdateCharacterMailCount(cPlayer->GetGUID(), cPlayer->GetMailSize(), true);
sCharacterCache->UpdateCharacterArenaTeamId(cPlayer->GetGUID(), ARENA_SLOT_2v2, cPlayer->GetArenaTeamId(ARENA_SLOT_2v2));
sCharacterCache->UpdateCharacterArenaTeamId(cPlayer->GetGUID(), ARENA_SLOT_3v3, cPlayer->GetArenaTeamId(ARENA_SLOT_3v3));
sCharacterCache->UpdateCharacterArenaTeamId(cPlayer->GetGUID(), ARENA_SLOT_5v5, cPlayer->GetArenaTeamId(ARENA_SLOT_5v5));
if (Group* group = cPlayer->GetGroup())
{
sCharacterCache->UpdateCharacterGroup(cPlayer->GetGUID(), group->GetGUID());
}
else
{
sCharacterCache->ClearCharacterGroup(cPlayer->GetGUID());
}
}
}
else
{
sCharacterCache->RefreshCacheEntry(player->GetGUID().GetCounter());
}
handler->PSendSysMessage(LANG_COMMAND_CACHE_REFRESH, player->GetName(), player->GetGUID().GetCounter());
handler->SetSentErrorMessage(false);
return true;
}
};
void AddSC_cache_commandscript()
{
new cache_commandscript();
}

View File

@@ -224,7 +224,7 @@ public:
return;
}
if (sObjectMgr->GetPlayerGUIDByName(delInfo.name))
if (sCharacterCache->GetCharacterGuidByName(delInfo.name))
{
handler->PSendSysMessage(LANG_CHARACTER_DELETED_SKIP_NAME, delInfo.name.c_str(), delInfo.lowGuid, delInfo.accountId);
return;
@@ -239,7 +239,9 @@ public:
stmt = CharacterDatabase.GetPreparedStatement(CHAR_SEL_CHARACTER_NAME_DATA);
stmt->setUInt32(0, delInfo.lowGuid);
if (PreparedQueryResult result = CharacterDatabase.Query(stmt))
sWorld->AddGlobalPlayerData(delInfo.lowGuid, delInfo.accountId, delInfo.name, (*result)[2].GetUInt8(), (*result)[0].GetUInt8(), (*result)[1].GetUInt8(), (*result)[3].GetUInt8(), 0, 0);
{
sCharacterCache->AddCharacterCacheEntry(ObjectGuid(HighGuid::Player, delInfo.lowGuid), delInfo.accountId, delInfo.name, (*result)[2].GetUInt8(), (*result)[0].GetUInt8(), (*result)[1].GetUInt8(), (*result)[3].GetUInt8());
}
}
static void HandleCharacterLevel(Player* player, ObjectGuid playerGuid, uint32 oldLevel, uint32 newLevel, ChatHandler* handler)
@@ -268,8 +270,7 @@ public:
stmt->setUInt32(1, playerGuid.GetCounter());
CharacterDatabase.Execute(stmt);
// xinef: update global storage
sWorld->UpdateGlobalPlayerData(playerGuid.GetCounter(), PLAYER_UPDATE_DATA_LEVEL, "", newLevel);
sCharacterCache->UpdateCharacterLevel(playerGuid, newLevel);
}
}
@@ -390,8 +391,7 @@ public:
CharacterDatabase.Execute(stmt);
}
sWorld->UpdateGlobalNameData(player->GetGUID().GetCounter(), player->GetName().c_str(), newName);
sWorld->UpdateGlobalPlayerData(player->GetGUID().GetCounter(), PLAYER_UPDATE_DATA_NAME, newName);
sCharacterCache->UpdateCharacterData(player->GetGUID(), newName);
handler->PSendSysMessage(LANG_RENAME_PLAYER_WITH_NEW_NAME, player->GetName().c_str(), newName.c_str());
}
@@ -433,7 +433,7 @@ public:
if (!player)
return false;
uint8 oldlevel = player->IsConnected() ? player->GetConnectedPlayer()->getLevel() : static_cast<uint8>(Player::GetLevelFromStorage(player->GetGUID().GetCounter()));
uint8 oldlevel = player->IsConnected() ? player->GetConnectedPlayer()->getLevel() : sCharacterCache->GetCharacterLevelByGuid(player->GetGUID());
if (newlevel < 1)
return false; // invalid level
@@ -737,7 +737,9 @@ public:
target->GetSession()->KickPlayer("HandleCharacterEraseCommand GM Command deleting character");
}
else
accountId = sObjectMgr->GetPlayerAccountIdByGUID(player.GetGUID().GetCounter());
{
accountId = sCharacterCache->GetCharacterAccountIdByGuid(player.GetGUID());
}
std::string accountName;
AccountMgr::GetName(accountId, accountName);
@@ -756,7 +758,7 @@ public:
if (!player)
return false;
uint8 oldlevel = static_cast<uint8>(player->IsConnected() ? player->GetConnectedPlayer()->getLevel() : Player::GetLevelFromStorage(player->GetGUID().GetCounter()));
uint8 oldlevel = player->IsConnected() ? player->GetConnectedPlayer()->getLevel() : sCharacterCache->GetCharacterLevelByGuid(player->GetGUID());
int16 newlevel = static_cast<int16>(oldlevel) + level;
if (newlevel < 1)
@@ -796,7 +798,7 @@ public:
if (characterGUID)
{
if (sObjectMgr->GetPlayerAccountIdByGUID(*characterGUID))
if (sCharacterCache->GetCharacterAccountIdByGuid(ObjectGuid(HighGuid::Player, *characterGUID)))
{
handler->PSendSysMessage(LANG_CHARACTER_GUID_IN_USE, *characterGUID);
handler->SetSentErrorMessage(true);

View File

@@ -176,7 +176,7 @@ public:
if (!handler->extractPlayerTarget((char*)args, &target, &targetGuid))
return false;
uint32 guildId = target ? target->GetGuildId() : Player::GetGuildIdFromStorage(targetGuid.GetCounter());
uint32 guildId = target ? target->GetGuildId() : sCharacterCache->GetCharacterGuildIdByGuid(targetGuid);
if (!guildId)
return false;
@@ -196,7 +196,7 @@ public:
if (!player)
return false;
uint32 guildId = player->IsConnected() ? player->GetConnectedPlayer()->GetGuildId() : Player::GetGuildIdFromStorage(player->GetGUID().GetCounter());
uint32 guildId = player->IsConnected() ? player->GetConnectedPlayer()->GetGuildId() : sCharacterCache->GetCharacterGuildIdByGuid(player->GetGUID());
if (!guildId)
return false;
@@ -227,8 +227,10 @@ public:
// Display Guild Information
handler->PSendSysMessage(LANG_GUILD_INFO_NAME, guild->GetName().c_str(), guild->GetId()); // Guild Id + Name
std::string guildMasterName;
if (sObjectMgr->GetPlayerNameByGUID(guild->GetLeaderGUID().GetCounter(), guildMasterName))
if (sCharacterCache->GetCharacterNameByGuid(guild->GetLeaderGUID(), guildMasterName))
{
handler->PSendSysMessage(LANG_GUILD_INFO_GUILD_MASTER, guildMasterName.c_str(), guild->GetLeaderGUID().GetCounter()); // Guild Master
}
// Format creation date
char createdDateStr[20];

View File

@@ -23,6 +23,7 @@ Category: commandscripts
EndScriptData */
#include "AccountMgr.h"
#include "CharacterCache.h"
#include "Chat.h"
#include "GameEventMgr.h"
#include "ObjectAccessor.h"
@@ -1615,11 +1616,11 @@ public:
uint8 plevel = 0, prace = 0, pclass = 0;
bool online = ObjectAccessor::FindPlayerByLowGUID(guid) != nullptr;
if (const GlobalPlayerData* gpd = sWorld->GetGlobalPlayerData(guid))
if (CharacterCacheEntry const* gpd = sCharacterCache->GetCharacterCacheByName(name))
{
plevel = gpd->level;
prace = gpd->race;
pclass = gpd->playerClass;
plevel = gpd->Level;
prace = gpd->Race;
pclass = gpd->Class;
}
if (plevel > 0 && prace > 0 && prace <= RACE_DRAENEI && pclass > 0 && pclass <= CLASS_DRUID)

View File

@@ -19,6 +19,7 @@
#include "ArenaTeamMgr.h"
#include "BattlegroundMgr.h"
#include "CellImpl.h"
#include "CharacterCache.h"
#include "Chat.h"
#include "GameGraveyard.h"
#include "GridNotifiers.h"
@@ -1869,7 +1870,7 @@ public:
ObjectGuid parseGUID = ObjectGuid::Create<HighGuid::Player>(atol((char*)args));
if (sObjectMgr->GetPlayerNameByGUID(parseGUID.GetCounter(), targetName))
if (sCharacterCache->GetCharacterNameByGuid(parseGUID, targetName))
{
target = ObjectAccessor::FindConnectedPlayer(parseGUID);
targetGuid = parseGUID;
@@ -2317,7 +2318,7 @@ public:
}
Player* target = player->GetConnectedPlayer();
uint32 accountId = target ? target->GetSession()->GetAccountId() : sObjectMgr->GetPlayerAccountIdByGUID(player->GetGUID().GetCounter());
uint32 accountId = target ? target->GetSession()->GetAccountId() : sCharacterCache->GetCharacterAccountIdByGuid(player->GetGUID());
// find only player from same account if any
if (!target)
@@ -2393,7 +2394,7 @@ public:
if (!handler->extractPlayerTarget((char*)args, &target, &targetGuid, &targetName))
return false;
uint32 accountId = target ? target->GetSession()->GetAccountId() : sObjectMgr->GetPlayerAccountIdByGUID(targetGuid.GetCounter());
uint32 accountId = target ? target->GetSession()->GetAccountId() : sCharacterCache->GetCharacterAccountIdByGuid(targetGuid);
// find only player from same account if any
if (!target)
@@ -3193,7 +3194,7 @@ public:
}
else if (targetName)
{
if (ObjectGuid playerGUID = sWorld->GetGlobalPlayerGUID(name))
if (ObjectGuid playerGUID = sCharacterCache->GetCharacterGuidByName(name))
{
CharacterDatabasePreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_CHAR_AURA_FROZEN);
stmt->setUInt32(0, playerGUID.GetCounter());
@@ -3315,7 +3316,7 @@ public:
ObjectGuid parseGUID = ObjectGuid::Create<HighGuid::Player>(atol((char*)args));
if (sObjectMgr->GetPlayerNameByGUID(parseGUID.GetCounter(), nameTarget))
if (sCharacterCache->GetCharacterNameByGuid(parseGUID, nameTarget))
{
playerTarget = ObjectAccessor::FindConnectedPlayer(parseGUID);
guidTarget = parseGUID;
@@ -3328,8 +3329,12 @@ public:
groupTarget = playerTarget->GetGroup();
if (!groupTarget && guidTarget)
if (uint32 groupGUID = Player::GetGroupIdFromStorage(guidTarget.GetCounter()))
groupTarget = sGroupMgr->GetGroupByGUID(groupGUID);
{
if (ObjectGuid groupGUID = sCharacterCache->GetCharacterGroupGuidByGuid(guidTarget))
{
groupTarget = sGroupMgr->GetGroupByGUID(groupGUID.GetCounter());
}
}
if (groupTarget)
{

View File

@@ -53,6 +53,7 @@ void AddSC_ticket_commandscript();
void AddSC_titles_commandscript();
void AddSC_wp_commandscript();
void AddSC_player_commandscript();
void AddSC_cache_commandscript();
// The name of this function should match:
// void Add${NameOfDirectory}Scripts()
@@ -95,4 +96,5 @@ void AddCommandsScripts()
AddSC_titles_commandscript();
AddSC_wp_commandscript();
AddSC_player_commandscript();
AddSC_cache_commandscript();
}

View File

@@ -100,8 +100,8 @@ public:
}
// Get target information
ObjectGuid targetGuid = sObjectMgr->GetPlayerGUIDByName(target.c_str());
uint32 targetAccountId = sObjectMgr->GetPlayerAccountIdByGUID(targetGuid.GetCounter());
ObjectGuid targetGuid = sCharacterCache->GetCharacterGuidByName(target);
uint32 targetAccountId = sCharacterCache->GetCharacterAccountIdByGuid(targetGuid);
uint32 targetGmLevel = AccountMgr::GetSecurity(targetAccountId, realm.Id.Realm);
// Target must exist and have administrative rights
@@ -399,7 +399,7 @@ public:
else
{
ObjectGuid guid = ticket->GetAssignedToGUID();
uint32 accountId = sObjectMgr->GetPlayerAccountIdByGUID(guid.GetCounter());
uint32 accountId = sCharacterCache->GetCharacterAccountIdByGuid(guid);
security = AccountMgr::GetSecurity(accountId, realm.Id.Realm);
}
@@ -458,9 +458,13 @@ public:
// Detect target's GUID
ObjectGuid guid;
if (Player* player = ObjectAccessor::FindPlayerByName(name, false))
{
guid = player->GetGUID();
}
else
guid = sObjectMgr->GetPlayerGUIDByName(name);
{
guid = sCharacterCache->GetCharacterGuidByName(name);
}
// Target must exist
if (!guid)