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

@@ -485,22 +485,6 @@ enum Rates
MAX_RATES
};
// xinef: global storage
struct GlobalPlayerData
{
ObjectGuid::LowType guidLow;
uint32 accountId;
std::string name;
uint8 race;
uint8 playerClass;
uint8 gender;
uint8 level;
uint16 mailCount;
uint32 guildId;
uint32 groupId;
std::map<uint8, uint32> arenaTeamId;
};
class IWorld
{
public:
@@ -582,17 +566,6 @@ public:
virtual void KickAll() = 0;
virtual void KickAllLess(AccountTypes sec) = 0;
virtual uint32 GetNextWhoListUpdateDelaySecs() = 0;
virtual void LoadGlobalPlayerDataStore() = 0;
virtual ObjectGuid GetGlobalPlayerGUID(std::string const& name) const = 0;
virtual GlobalPlayerData const* GetGlobalPlayerData(ObjectGuid::LowType guid) const = 0;
virtual void AddGlobalPlayerData(ObjectGuid::LowType guid, uint32 accountId, std::string const& name, uint8 gender, uint8 race, uint8 playerClass, uint8 level, uint16 mailCount, uint32 guildId) = 0;
virtual void UpdateGlobalPlayerData(ObjectGuid::LowType guid, uint8 mask, std::string const& name, uint8 level = 0, uint8 gender = 0, uint8 race = 0, uint8 playerClass = 0) = 0;
virtual void UpdateGlobalPlayerMails(ObjectGuid::LowType guid, int16 count, bool add = true) = 0;
virtual void UpdateGlobalPlayerGuild(ObjectGuid::LowType guid, uint32 guildId) = 0;
virtual void UpdateGlobalPlayerGroup(ObjectGuid::LowType guid, uint32 groupId) = 0;
virtual void UpdateGlobalPlayerArenaTeam(ObjectGuid::LowType guid, uint8 slot, uint32 arenaTeamId) = 0;
virtual void UpdateGlobalNameData(ObjectGuid::LowType guidLow, std::string const& oldName, std::string const& newName) = 0;
virtual void DeleteGlobalPlayerData(ObjectGuid::LowType guid, std::string const& name) = 0;
virtual void ProcessCliCommands() = 0;
virtual void QueueCliCommand(CliCommandHolder* commandHolder) = 0;
virtual void ForceGameEventUpdate() = 0;

View File

@@ -1564,9 +1564,8 @@ void World::SetInitialWorldSettings()
LOG_INFO("server.loading", "Loading Instance Template...");
sObjectMgr->LoadInstanceTemplate();
// xinef: Global Storage, should be loaded asap
LOG_INFO("server.loading", "Load Global Player Data...");
sWorld->LoadGlobalPlayerDataStore();
LOG_INFO("server.loading", "Load Character Cache...");
sCharacterCache->LoadCharacterCacheStorage();
// Must be called before `creature_respawn`/`gameobject_respawn` tables
LOG_INFO("server.loading", "Loading instances...");
@@ -3334,260 +3333,6 @@ void World::ProcessQueryCallbacks()
_queryProcessor.ProcessReadyCallbacks();
}
void World::LoadGlobalPlayerDataStore()
{
uint32 oldMSTime = getMSTime();
_globalPlayerDataStore.clear();
QueryResult result = CharacterDatabase.Query("SELECT guid, account, name, gender, race, class, level FROM characters WHERE deleteDate IS NULL");
if (!result)
{
LOG_INFO("server.loading", ">> Loaded 0 Players data.");
return;
}
uint32 count = 0;
// query to load number of mails by receiver
std::map<uint32, uint16> _mailCountMap;
QueryResult mailCountResult = CharacterDatabase.Query("SELECT receiver, COUNT(receiver) FROM mail GROUP BY receiver");
if (mailCountResult)
{
do
{
Field* fields = mailCountResult->Fetch();
_mailCountMap[fields[0].GetUInt32()] = uint16(fields[1].GetUInt64());
} while (mailCountResult->NextRow());
}
do
{
Field* fields = result->Fetch();
ObjectGuid::LowType guidLow = fields[0].GetUInt32();
// count mails
uint16 mailCount = 0;
std::map<uint32, uint16>::const_iterator itr = _mailCountMap.find(guidLow);
if (itr != _mailCountMap.end())
mailCount = itr->second;
AddGlobalPlayerData(
guidLow, /*guid*/
fields[1].GetUInt32(), /*accountId*/
fields[2].GetString(), /*name*/
fields[3].GetUInt8(), /*gender*/
fields[4].GetUInt8(), /*race*/
fields[5].GetUInt8(), /*class*/
fields[6].GetUInt8(), /*level*/
mailCount, /*mail count*/
0 /*guild id*/);
++count;
} while (result->NextRow());
LOG_INFO("server.loading", ">> Loaded %d Players data in %u ms", count, GetMSTimeDiffToNow(oldMSTime));
LOG_INFO("server.loading", " ");
}
void World::AddGlobalPlayerData(ObjectGuid::LowType guid, uint32 accountId, std::string const& name, uint8 gender, uint8 race, uint8 playerClass, uint8 level, uint16 mailCount, uint32 guildId)
{
GlobalPlayerData data;
data.guidLow = guid;
data.accountId = accountId;
data.name = name;
data.level = level;
data.race = race;
data.playerClass = playerClass;
data.gender = gender;
data.mailCount = mailCount;
data.guildId = guildId;
data.groupId = 0;
data.arenaTeamId[0] = 0;
data.arenaTeamId[1] = 0;
data.arenaTeamId[2] = 0;
_globalPlayerDataStore[guid] = data;
_globalPlayerNameStore[name] = guid;
}
void World::UpdateGlobalPlayerData(ObjectGuid::LowType guid, uint8 mask, std::string const& name, uint8 level, uint8 gender, uint8 race, uint8 playerClass)
{
GlobalPlayerDataMap::iterator itr = _globalPlayerDataStore.find(guid);
if (itr == _globalPlayerDataStore.end())
return;
if (mask & PLAYER_UPDATE_DATA_LEVEL)
itr->second.level = level;
if (mask & PLAYER_UPDATE_DATA_RACE)
itr->second.race = race;
if (mask & PLAYER_UPDATE_DATA_CLASS)
itr->second.playerClass = playerClass;
if (mask & PLAYER_UPDATE_DATA_GENDER)
itr->second.gender = gender;
if (mask & PLAYER_UPDATE_DATA_NAME)
itr->second.name = name;
WorldPacket data(SMSG_INVALIDATE_PLAYER, 8);
data << guid;
SendGlobalMessage(&data);
}
void World::UpdateGlobalPlayerMails(ObjectGuid::LowType guid, int16 count, bool add)
{
GlobalPlayerDataMap::iterator itr = _globalPlayerDataStore.find(guid);
if (itr == _globalPlayerDataStore.end())
return;
if (!add)
{
itr->second.mailCount = count;
return;
}
int16 icount = (int16)itr->second.mailCount;
if (count < 0 && abs(count) > icount)
count = -icount;
itr->second.mailCount = uint16(icount + count); // addition or subtraction
}
void World::UpdateGlobalPlayerGuild(ObjectGuid::LowType guid, uint32 guildId)
{
GlobalPlayerDataMap::iterator itr = _globalPlayerDataStore.find(guid);
if (itr == _globalPlayerDataStore.end())
return;
itr->second.guildId = guildId;
}
void World::UpdateGlobalPlayerGroup(ObjectGuid::LowType guid, uint32 groupId)
{
GlobalPlayerDataMap::iterator itr = _globalPlayerDataStore.find(guid);
if (itr == _globalPlayerDataStore.end())
return;
itr->second.groupId = groupId;
}
void World::UpdateGlobalPlayerArenaTeam(ObjectGuid::LowType guid, uint8 slot, uint32 arenaTeamId)
{
GlobalPlayerDataMap::iterator itr = _globalPlayerDataStore.find(guid);
if (itr == _globalPlayerDataStore.end())
return;
itr->second.arenaTeamId[slot] = arenaTeamId;
}
void World::UpdateGlobalNameData(ObjectGuid::LowType guidLow, std::string const& oldName, std::string const& newName)
{
_globalPlayerNameStore.erase(oldName);
_globalPlayerNameStore[newName] = guidLow;
}
void World::DeleteGlobalPlayerData(ObjectGuid::LowType guid, std::string const& name)
{
if (guid)
_globalPlayerDataStore.erase(guid);
if (!name.empty())
_globalPlayerNameStore.erase(name);
}
GlobalPlayerData const* World::GetGlobalPlayerData(ObjectGuid::LowType guid) const
{
// Get data from global storage
GlobalPlayerDataMap::const_iterator itr = _globalPlayerDataStore.find(guid);
if (itr != _globalPlayerDataStore.end())
return &itr->second;
// Player is not in the global storage, try to get it from the Database
CharacterDatabasePreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_SEL_DATA_BY_GUID);
stmt->setUInt32(0, guid);
PreparedQueryResult result = CharacterDatabase.Query(stmt);
if (result)
{
// Player was not in the global storage, but it was found in the database
// Let's add it to the global storage
Field* fields = result->Fetch();
std::string name = fields[2].GetString();
LOG_INFO("server.worldserver", "Player %s [GUID: %u] was not found in the global storage, but it was found in the database.", name.c_str(), guid);
sWorld->AddGlobalPlayerData(
fields[0].GetUInt32(), /*guid*/
fields[1].GetUInt32(), /*accountId*/
fields[2].GetString(), /*name*/
fields[3].GetUInt8(), /*gender*/
fields[4].GetUInt8(), /*race*/
fields[5].GetUInt8(), /*class*/
fields[6].GetUInt8(), /*level*/
0, /*mail count*/
0 /*guild id*/
);
itr = _globalPlayerDataStore.find(guid);
if (itr != _globalPlayerDataStore.end())
{
LOG_INFO("server.worldserver", "Player %s [GUID: %u] added to the global storage.", name.c_str(), guid);
return &itr->second;
}
}
// Player not found
return nullptr;
}
ObjectGuid World::GetGlobalPlayerGUID(std::string const& name) const
{
// Get data from global storage
GlobalPlayerNameMap::const_iterator itr = _globalPlayerNameStore.find(name);
if (itr != _globalPlayerNameStore.end())
return ObjectGuid::Create<HighGuid::Player>(itr->second);
// Player is not in the global storage, try to get it from the Database
CharacterDatabasePreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_SEL_DATA_BY_NAME);
stmt->setString(0, name);
PreparedQueryResult result = CharacterDatabase.Query(stmt);
if (result)
{
// Player was not in the global storage, but it was found in the database
// Let's add it to the global storage
Field* fields = result->Fetch();
ObjectGuid::LowType guidLow = fields[0].GetUInt32();
LOG_INFO("server.worldserver", "Player %s [GUID: %u] was not found in the global storage, but it was found in the database.", name.c_str(), guidLow);
sWorld->AddGlobalPlayerData(
guidLow, /*guid*/
fields[1].GetUInt32(), /*accountId*/
fields[2].GetString(), /*name*/
fields[3].GetUInt8(), /*gender*/
fields[4].GetUInt8(), /*race*/
fields[5].GetUInt8(), /*class*/
fields[6].GetUInt8(), /*level*/
0, /*mail count*/
0 /*guild id*/
);
itr = _globalPlayerNameStore.find(name);
if (itr != _globalPlayerNameStore.end())
{
LOG_INFO("server.worldserver", "Player %s [GUID: %u] added to the global storage.", name.c_str(), guidLow);
return ObjectGuid::Create<HighGuid::Player>(guidLow);
}
}
// Player not found
return ObjectGuid::Empty;
}
void World::RemoveOldCorpses()
{
m_timers[WUPDATE_CORPSES].SetCurrent(m_timers[WUPDATE_CORPSES].GetInterval());

View File

@@ -143,18 +143,6 @@ enum WorldStates
#define WORLD_SLEEP_CONST 10
enum GlobalPlayerUpdateMask
{
PLAYER_UPDATE_DATA_LEVEL = 0x01,
PLAYER_UPDATE_DATA_RACE = 0x02,
PLAYER_UPDATE_DATA_CLASS = 0x04,
PLAYER_UPDATE_DATA_GENDER = 0x08,
PLAYER_UPDATE_DATA_NAME = 0x10,
};
typedef std::map<ObjectGuid::LowType, GlobalPlayerData> GlobalPlayerDataMap;
typedef std::map<std::string, ObjectGuid::LowType> GlobalPlayerNameMap;
// xinef: petitions storage
struct PetitionData
{
@@ -350,19 +338,6 @@ public:
// our: needed for arena spectator subscriptions
uint32 GetNextWhoListUpdateDelaySecs();
// xinef: Global Player Data Storage system
void LoadGlobalPlayerDataStore();
ObjectGuid GetGlobalPlayerGUID(std::string const& name) const;
GlobalPlayerData const* GetGlobalPlayerData(ObjectGuid::LowType guid) const;
void AddGlobalPlayerData(ObjectGuid::LowType guid, uint32 accountId, std::string const& name, uint8 gender, uint8 race, uint8 playerClass, uint8 level, uint16 mailCount, uint32 guildId);
void UpdateGlobalPlayerData(ObjectGuid::LowType guid, uint8 mask, std::string const& name, uint8 level = 0, uint8 gender = 0, uint8 race = 0, uint8 playerClass = 0);
void UpdateGlobalPlayerMails(ObjectGuid::LowType guid, int16 count, bool add = true);
void UpdateGlobalPlayerGuild(ObjectGuid::LowType guid, uint32 guildId);
void UpdateGlobalPlayerGroup(ObjectGuid::LowType guid, uint32 groupId);
void UpdateGlobalPlayerArenaTeam(ObjectGuid::LowType guid, uint8 slot, uint32 arenaTeamId);
void UpdateGlobalNameData(ObjectGuid::LowType guidLow, std::string const& oldName, std::string const& newName);
void DeleteGlobalPlayerData(ObjectGuid::LowType guid, std::string const& name);
void ProcessCliCommands();
void QueueCliCommand(CliCommandHolder* commandHolder) { cliCmdQueue.add(commandHolder); }
@@ -460,10 +435,6 @@ private:
static float m_MaxVisibleDistanceInInstances;
static float m_MaxVisibleDistanceInBGArenas;
// our speed ups
GlobalPlayerDataMap _globalPlayerDataStore; // xinef
GlobalPlayerNameMap _globalPlayerNameStore; // xinef
std::string _realmName;
// CLI command holder to be thread safe