AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
CharacterCache Class Reference

#include "CharacterCache.h"

Public Member Functions

 CharacterCache () noexcept=default
 
 ~CharacterCache () noexcept=default
 
void LoadCharacterCacheStorage ()
 Loads several pieces of information on server startup with the GUID There is no further database query necessary. These are a number of methods that work into the calling function. More...
 
void RefreshCacheEntry (uint32 lowGuid)
 
void AddCharacterCacheEntry (ObjectGuid const &guid, uint32 accountId, std::string const &name, uint8 gender, uint8 race, uint8 playerClass, uint8 level)
 
void DeleteCharacterCacheEntry (ObjectGuid const &guid, std::string const &name)
 
void UpdateCharacterData (ObjectGuid const &guid, std::string const &name, Optional< uint8 > gender={}, Optional< uint8 > race={})
 
void UpdateCharacterLevel (ObjectGuid const &guid, uint8 level)
 
void UpdateCharacterAccountId (ObjectGuid const &guid, uint32 accountId)
 
void UpdateCharacterGuildId (ObjectGuid const &guid, ObjectGuid::LowType guildId)
 
void UpdateCharacterArenaTeamId (ObjectGuid const &guid, uint8 slot, uint32 arenaTeamId)
 
void UpdateCharacterMailCount (ObjectGuid const &guid, int8 count, bool update=false)
 
void DecreaseCharacterMailCount (ObjectGuid const &guid)
 
void IncreaseCharacterMailCount (ObjectGuid const &guid)
 
bool HasCharacterCacheEntry (ObjectGuid const &guid) const
 
CharacterCacheEntry const * GetCharacterCacheByGuid (ObjectGuid const &guid) const
 
CharacterCacheEntry const * GetCharacterCacheByName (std::string const &name) const
 
void UpdateCharacterGroup (ObjectGuid const &guid, ObjectGuid groupGUID)
 
void ClearCharacterGroup (ObjectGuid const &guid)
 
ObjectGuid GetCharacterGuidByName (std::string const &name) const
 
bool GetCharacterNameByGuid (ObjectGuid guid, std::string &name) const
 
uint32 GetCharacterTeamByGuid (ObjectGuid guid) const
 
uint32 GetCharacterAccountIdByGuid (ObjectGuid guid) const
 
uint32 GetCharacterAccountIdByName (std::string const &name) const
 
uint8 GetCharacterLevelByGuid (ObjectGuid guid) const
 
ObjectGuid::LowType GetCharacterGuildIdByGuid (ObjectGuid guid) const
 
uint32 GetCharacterArenaTeamIdByGuid (ObjectGuid guid, uint8 type) const
 
ObjectGuid GetCharacterGroupGuidByGuid (ObjectGuid guid) const
 

Static Public Member Functions

static CharacterCacheinstance ()
 

Detailed Description

Constructor & Destructor Documentation

◆ CharacterCache()

CharacterCache::CharacterCache ( )
defaultnoexcept

◆ ~CharacterCache()

CharacterCache::~CharacterCache ( )
defaultnoexcept

Member Function Documentation

◆ AddCharacterCacheEntry()

void CharacterCache::AddCharacterCacheEntry ( ObjectGuid const &  guid,
uint32  accountId,
std::string const &  name,
uint8  gender,
uint8  race,
uint8  playerClass,
uint8  level 
)
123{
125 data.Guid = guid;
126 data.Name = name;
127 data.AccountId = accountId;
128 data.Race = race;
129 data.Sex = gender;
130 data.Class = playerClass;
131 data.Level = level;
132 data.GuildId = 0; // Will be set in guild loading or guild setting
133 for (uint8 i = 0; i < MAX_ARENA_SLOT; ++i)
134 {
135 data.ArenaTeamId[i] = 0; // Will be set in arena teams loading
136 }
137
138 // Fill Name to Guid Store
139 _characterCacheByNameStore[name] = &data;
140}
std::uint8_t uint8
Definition: Define.h:109
#define MAX_ARENA_SLOT
Definition: ArenaTeam.h:134
std::unordered_map< ObjectGuid, CharacterCacheEntry > _characterCacheStore
Definition: CharacterCache.cpp:29
std::unordered_map< std::string, CharacterCacheEntry * > _characterCacheByNameStore
Definition: CharacterCache.cpp:30
Definition: CharacterCache.h:28
uint8 Class
Definition: CharacterCache.h:32
std::array< uint32, MAX_ARENA_SLOT > ArenaTeamId
Definition: CharacterCache.h:38
uint8 Level
Definition: CharacterCache.h:35
ObjectGuid::LowType GuildId
Definition: CharacterCache.h:37
std::string Name
Definition: CharacterCache.h:30
uint8 Sex
Definition: CharacterCache.h:34
uint8 Race
Definition: CharacterCache.h:33
ObjectGuid Guid
Definition: CharacterCache.h:29
uint32 AccountId
Definition: CharacterCache.h:31

References CharacterCacheEntry::AccountId, CharacterCacheEntry::ArenaTeamId, CharacterCacheEntry::Class, CharacterCacheEntry::Guid, CharacterCacheEntry::GuildId, CharacterCacheEntry::Level, MAX_ARENA_SLOT, CharacterCacheEntry::Name, CharacterCacheEntry::Race, and CharacterCacheEntry::Sex.

Referenced by LoadCharacterCacheStorage(), and RefreshCacheEntry().

◆ ClearCharacterGroup()

void CharacterCache::ClearCharacterGroup ( ObjectGuid const &  guid)
inline
void UpdateCharacterGroup(ObjectGuid const &guid, ObjectGuid groupGUID)
Definition: CharacterCache.cpp:242
static ObjectGuid const Empty
Definition: ObjectGuid.h:120

References ObjectGuid::Empty.

◆ DecreaseCharacterMailCount()

void CharacterCache::DecreaseCharacterMailCount ( ObjectGuid const &  guid)
inline
62{ UpdateCharacterMailCount(guid, -1); };
void UpdateCharacterMailCount(ObjectGuid const &guid, int8 count, bool update=false)
Definition: CharacterCache.cpp:219

◆ DeleteCharacterCacheEntry()

void CharacterCache::DeleteCharacterCacheEntry ( ObjectGuid const &  guid,
std::string const &  name 
)
143{
144 _characterCacheStore.erase(guid);
145 _characterCacheByNameStore.erase(name);
146}

Referenced by RefreshCacheEntry().

◆ GetCharacterAccountIdByGuid()

uint32 CharacterCache::GetCharacterAccountIdByGuid ( ObjectGuid  guid) const
318{
319 auto itr = _characterCacheStore.find(guid);
320 if (itr == _characterCacheStore.end())
321 {
322 return 0;
323 }
324
325 return itr->second.AccountId;
326}

◆ GetCharacterAccountIdByName()

uint32 CharacterCache::GetCharacterAccountIdByName ( std::string const &  name) const
329{
330 auto itr = _characterCacheByNameStore.find(name);
331 if (itr != _characterCacheByNameStore.end())
332 {
333 return itr->second->AccountId;
334 }
335
336 return 0;
337}

◆ GetCharacterArenaTeamIdByGuid()

uint32 CharacterCache::GetCharacterArenaTeamIdByGuid ( ObjectGuid  guid,
uint8  type 
) const
362{
363 auto itr = _characterCacheStore.find(guid);
364 if (itr == _characterCacheStore.end())
365 {
366 return 0;
367 }
368
369 return itr->second.ArenaTeamId[type];
370}

◆ GetCharacterCacheByGuid()

CharacterCacheEntry const * CharacterCache::GetCharacterCacheByGuid ( ObjectGuid const &  guid) const
262{
263 auto itr = _characterCacheStore.find(guid);
264 if (itr != _characterCacheStore.end())
265 {
266 return &itr->second;
267 }
268
269 return nullptr;
270}

◆ GetCharacterCacheByName()

CharacterCacheEntry const * CharacterCache::GetCharacterCacheByName ( std::string const &  name) const
273{
274 auto itr = _characterCacheByNameStore.find(name);
275 if (itr != _characterCacheByNameStore.end())
276 {
277 return itr->second;
278 }
279
280 return nullptr;
281}

◆ GetCharacterGroupGuidByGuid()

ObjectGuid CharacterCache::GetCharacterGroupGuidByGuid ( ObjectGuid  guid) const
373{
374 auto itr = _characterCacheStore.find(guid);
375 if (itr == _characterCacheStore.end())
376 {
377 return ObjectGuid::Empty;
378 }
379
380 return itr->second.GroupGuid;
381}

References ObjectGuid::Empty.

◆ GetCharacterGuidByName()

ObjectGuid CharacterCache::GetCharacterGuidByName ( std::string const &  name) const
284{
285 auto itr = _characterCacheByNameStore.find(name);
286 if (itr != _characterCacheByNameStore.end())
287 {
288 return itr->second->Guid;
289 }
290
291 return ObjectGuid::Empty;
292}

References ObjectGuid::Empty.

◆ GetCharacterGuildIdByGuid()

ObjectGuid::LowType CharacterCache::GetCharacterGuildIdByGuid ( ObjectGuid  guid) const
351{
352 auto itr = _characterCacheStore.find(guid);
353 if (itr == _characterCacheStore.end())
354 {
355 return 0;
356 }
357
358 return itr->second.GuildId;
359}

◆ GetCharacterLevelByGuid()

uint8 CharacterCache::GetCharacterLevelByGuid ( ObjectGuid  guid) const
340{
341 auto itr = _characterCacheStore.find(guid);
342 if (itr == _characterCacheStore.end())
343 {
344 return 0;
345 }
346
347 return itr->second.Level;
348}

◆ GetCharacterNameByGuid()

bool CharacterCache::GetCharacterNameByGuid ( ObjectGuid  guid,
std::string &  name 
) const
295{
296 auto itr = _characterCacheStore.find(guid);
297 if (itr == _characterCacheStore.end())
298 {
299 return false;
300 }
301
302 name = itr->second.Name;
303 return true;
304}

◆ GetCharacterTeamByGuid()

uint32 CharacterCache::GetCharacterTeamByGuid ( ObjectGuid  guid) const
307{
308 auto itr = _characterCacheStore.find(guid);
309 if (itr == _characterCacheStore.end())
310 {
311 return 0;
312 }
313
314 return Player::TeamIdForRace(itr->second.Race);
315}
static TeamId TeamIdForRace(uint8 race)
Definition: Player.cpp:5861

References Player::TeamIdForRace().

◆ HasCharacterCacheEntry()

bool CharacterCache::HasCharacterCacheEntry ( ObjectGuid const &  guid) const
257{
258 return _characterCacheStore.find(guid) != _characterCacheStore.end();
259}

◆ IncreaseCharacterMailCount()

void CharacterCache::IncreaseCharacterMailCount ( ObjectGuid const &  guid)
inline
63{ UpdateCharacterMailCount(guid, 1); };

◆ instance()

CharacterCache * CharacterCache::instance ( )
static
34{
36 return &instance;
37}
Definition: CharacterCache.h:43
static CharacterCache * instance()
Definition: CharacterCache.cpp:33

References instance().

Referenced by instance().

◆ LoadCharacterCacheStorage()

void CharacterCache::LoadCharacterCacheStorage ( )

Loads several pieces of information on server startup with the GUID There is no further database query necessary. These are a number of methods that work into the calling function.

Parameters
guidRequires a guid to call
Returns
Name, Gender, Race, Class and Level of player character Example Usage:
CharacterCacheEntry const* characterInfo = sCharacterCache->GetCharacterCacheByGuid(GUID);
if (!characterInfo)
return;
std::string playerName = characterInfo->Name;
uint8 playerGender = characterInfo->Sex;
uint8 playerRace = characterInfo->Race;
uint8 playerClass = characterInfo->Class;
uint8 playerLevel = characterInfo->Level;
#define sCharacterCache
Definition: CharacterCache.h:83
61{
63 uint32 oldMSTime = getMSTime();
64
65 QueryResult result = CharacterDatabase.Query("SELECT guid, name, account, race, gender, class, level FROM characters");
66 if (!result)
67 {
68 LOG_INFO("server.loading", "No character name data loaded, empty query!");
69 return;
70 }
71
72 do
73 {
74 Field* fields = result->Fetch();
75 AddCharacterCacheEntry(ObjectGuid::Create<HighGuid::Player>(fields[0].Get<uint32>()) /*guid*/, fields[2].Get<uint32>() /*account*/, fields[1].Get<std::string>() /*name*/,
76 fields[4].Get<uint8>() /*gender*/, fields[3].Get<uint8>() /*race*/, fields[5].Get<uint8>() /*class*/, fields[6].Get<uint8>() /*level*/);
77 } while (result->NextRow());
78
79 QueryResult mailCountResult = CharacterDatabase.Query("SELECT receiver, COUNT(receiver) FROM mail GROUP BY receiver");
80 if (mailCountResult)
81 {
82 do
83 {
84 Field* fields = mailCountResult->Fetch();
85 UpdateCharacterMailCount(ObjectGuid(HighGuid::Player, fields[0].Get<uint32>()), static_cast<int8>(fields[1].Get<uint64>()), true);
86 } while (mailCountResult->NextRow());
87 }
88
89 LOG_INFO("server.loading", ">> Loaded Character Infos For {} Characters in {} ms", _characterCacheStore.size(), GetMSTimeDiffToNow(oldMSTime));
90 LOG_INFO("server.loading", " ");
91}
#define LOG_INFO(filterType__,...)
Definition: Log.h:165
uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
Definition: Timer.h:131
uint32 getMSTime()
Definition: Timer.h:103
std::uint32_t uint32
Definition: Define.h:107
std::int8_t int8
Definition: Define.h:105
std::shared_ptr< ResultSet > QueryResult
Definition: DatabaseEnvFwd.h:27
DatabaseWorkerPool< CharacterDatabaseConnection > CharacterDatabase
Accessor to the character database.
Definition: DatabaseEnv.cpp:21
Class used to access individual fields of database query result.
Definition: Field.h:98
void AddCharacterCacheEntry(ObjectGuid const &guid, uint32 accountId, std::string const &name, uint8 gender, uint8 race, uint8 playerClass, uint8 level)
Definition: CharacterCache.cpp:122
Definition: ObjectGuid.h:118

References AddCharacterCacheEntry(), CharacterDatabase, getMSTime(), GetMSTimeDiffToNow(), LOG_INFO, and UpdateCharacterMailCount().

◆ RefreshCacheEntry()

void CharacterCache::RefreshCacheEntry ( uint32  lowGuid)
94{
95 QueryResult result = CharacterDatabase.Query("SELECT guid, name, account, race, gender, class, level FROM characters WHERE guid = {}", lowGuid);
96 if (!result)
97 {
98 return;
99 }
100
101 do
102 {
103 Field* fields = result->Fetch();
104 DeleteCharacterCacheEntry(ObjectGuid::Create<HighGuid::Player>(lowGuid), fields[1].Get<std::string>());
105 AddCharacterCacheEntry(ObjectGuid::Create<HighGuid::Player>(fields[0].Get<uint32>()) /*guid*/, fields[2].Get<uint32>() /*account*/, fields[1].Get<std::string>() /*name*/, fields[4].Get<uint8>() /*gender*/, fields[3].Get<uint8>() /*race*/, fields[5].Get<uint8>() /*class*/, fields[6].Get<uint8>() /*level*/);
106 } while (result->NextRow());
107
108 QueryResult mailCountResult = CharacterDatabase.Query("SELECT receiver, COUNT(receiver) FROM mail WHERE receiver = {} GROUP BY receiver", lowGuid);
109 if (mailCountResult)
110 {
111 do
112 {
113 Field* fields = mailCountResult->Fetch();
114 UpdateCharacterMailCount(ObjectGuid(HighGuid::Player, fields[0].Get<uint32>()), static_cast<int8>(fields[1].Get<uint64>()), true);
115 } while (mailCountResult->NextRow());
116 }
117}
void DeleteCharacterCacheEntry(ObjectGuid const &guid, std::string const &name)
Definition: CharacterCache.cpp:142

References AddCharacterCacheEntry(), CharacterDatabase, DeleteCharacterCacheEntry(), and UpdateCharacterMailCount().

◆ UpdateCharacterAccountId()

void CharacterCache::UpdateCharacterAccountId ( ObjectGuid const &  guid,
uint32  accountId 
)
187{
188 auto itr = _characterCacheStore.find(guid);
189 if (itr == _characterCacheStore.end())
190 {
191 return;
192 }
193
194 itr->second.AccountId = accountId;
195}

◆ UpdateCharacterArenaTeamId()

void CharacterCache::UpdateCharacterArenaTeamId ( ObjectGuid const &  guid,
uint8  slot,
uint32  arenaTeamId 
)
209{
210 auto itr = _characterCacheStore.find(guid);
211 if (itr == _characterCacheStore.end())
212 {
213 return;
214 }
215
216 itr->second.ArenaTeamId[slot] = arenaTeamId;
217}

◆ UpdateCharacterData()

void CharacterCache::UpdateCharacterData ( ObjectGuid const &  guid,
std::string const &  name,
Optional< uint8 gender = {},
Optional< uint8 race = {} 
)
149{
150 auto itr = _characterCacheStore.find(guid);
151 if (itr == _characterCacheStore.end())
152 return;
153
154 std::string oldName = itr->second.Name;
155 itr->second.Name = name;
156
157 if (gender)
158 {
159 itr->second.Sex = *gender;
160 }
161
162 if (race)
163 {
164 itr->second.Race = *race;
165 }
166
167 //WorldPackets::Misc::InvalidatePlayer packet(guid);
168 //sWorld->SendGlobalMessage(packet.Write());
169
170 // Correct name -> pointer storage
171 _characterCacheByNameStore.erase(oldName);
172 _characterCacheByNameStore[name] = &itr->second;
173}

◆ UpdateCharacterGroup()

void CharacterCache::UpdateCharacterGroup ( ObjectGuid const &  guid,
ObjectGuid  groupGUID 
)
243{
244 auto itr = _characterCacheStore.find(guid);
245 if (itr == _characterCacheStore.end())
246 {
247 return;
248 }
249
250 itr->second.GroupGuid = groupGUID;
251}

◆ UpdateCharacterGuildId()

void CharacterCache::UpdateCharacterGuildId ( ObjectGuid const &  guid,
ObjectGuid::LowType  guildId 
)
198{
199 auto itr = _characterCacheStore.find(guid);
200 if (itr == _characterCacheStore.end())
201 {
202 return;
203 }
204
205 itr->second.GuildId = guildId;
206}

◆ UpdateCharacterLevel()

void CharacterCache::UpdateCharacterLevel ( ObjectGuid const &  guid,
uint8  level 
)
176{
177 auto itr = _characterCacheStore.find(guid);
178 if (itr == _characterCacheStore.end())
179 {
180 return;
181 }
182
183 itr->second.Level = level;
184}

◆ UpdateCharacterMailCount()

void CharacterCache::UpdateCharacterMailCount ( ObjectGuid const &  guid,
int8  count,
bool  update = false 
)
220{
221 auto itr = _characterCacheStore.find(guid);
222 if (itr == _characterCacheStore.end())
223 {
224 return;
225 }
226
227 if (update)
228 {
229 itr->second.MailCount = count;
230 return;
231 }
232
233 // Let's be safe and prevent overflow
234 if (!itr->second.MailCount && count < 0)
235 {
236 return;
237 }
238
239 itr->second.MailCount += count;
240}

Referenced by LoadCharacterCacheStorage(), and RefreshCacheEntry().