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.
 
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)
 
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 CharacterCache * instance ()
 

Private Member Functions

void UpdateCharacterMailCount (ObjectGuid const &guid, int32 count, bool update=false)
 
void DecreaseCharacterMailCount (ObjectGuid const &guid)
 
void IncreaseCharacterMailCount (ObjectGuid const &guid)
 

Friends

class MailMgr
 

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 
)
110{
111 CharacterCacheEntry& data = _characterCacheStore[guid];
112 data.Guid = guid;
113 data.Name = name;
114 data.AccountId = accountId;
115 data.Race = race;
116 data.Sex = gender;
117 data.Class = playerClass;
118 data.Level = level;
119 data.GuildId = 0; // Will be set in guild loading or guild setting
120 for (uint8 i = 0; i < MAX_ARENA_SLOT; ++i)
121 {
122 data.ArenaTeamId[i] = 0; // Will be set in arena teams loading
123 }
124
125 // Fill Name to Guid Store
126 _characterCacheByNameStore[name] = &data;
127}
#define MAX_ARENA_SLOT
Definition ArenaTeam.h:134
std::uint8_t uint8
Definition Define.h:109
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:227
static ObjectGuid const Empty
Definition ObjectGuid.h:123

References ObjectGuid::Empty.

◆ DecreaseCharacterMailCount()

void CharacterCache::DecreaseCharacterMailCount ( ObjectGuid const &  guid)
inlineprivate
82{ UpdateCharacterMailCount(guid, -1); }
void UpdateCharacterMailCount(ObjectGuid const &guid, int32 count, bool update=false)
Definition CharacterCache.cpp:206

◆ DeleteCharacterCacheEntry()

void CharacterCache::DeleteCharacterCacheEntry ( ObjectGuid const &  guid,
std::string const &  name 
)
130{
131 _characterCacheStore.erase(guid);
132 _characterCacheByNameStore.erase(name);
133}

Referenced by RefreshCacheEntry().

◆ GetCharacterAccountIdByGuid()

uint32 CharacterCache::GetCharacterAccountIdByGuid ( ObjectGuid  guid) const
303{
304 auto itr = _characterCacheStore.find(guid);
305 if (itr == _characterCacheStore.end())
306 {
307 return 0;
308 }
309
310 return itr->second.AccountId;
311}

◆ GetCharacterAccountIdByName()

uint32 CharacterCache::GetCharacterAccountIdByName ( std::string const &  name) const
314{
315 auto itr = _characterCacheByNameStore.find(name);
316 if (itr != _characterCacheByNameStore.end())
317 {
318 return itr->second->AccountId;
319 }
320
321 return 0;
322}

◆ GetCharacterArenaTeamIdByGuid()

uint32 CharacterCache::GetCharacterArenaTeamIdByGuid ( ObjectGuid  guid,
uint8  type 
) const
347{
348 auto itr = _characterCacheStore.find(guid);
349 if (itr == _characterCacheStore.end())
350 {
351 return 0;
352 }
353
354 return itr->second.ArenaTeamId[type];
355}

◆ GetCharacterCacheByGuid()

CharacterCacheEntry const * CharacterCache::GetCharacterCacheByGuid ( ObjectGuid const &  guid) const
247{
248 auto itr = _characterCacheStore.find(guid);
249 if (itr != _characterCacheStore.end())
250 {
251 return &itr->second;
252 }
253
254 return nullptr;
255}

◆ GetCharacterCacheByName()

CharacterCacheEntry const * CharacterCache::GetCharacterCacheByName ( std::string const &  name) const
258{
259 auto itr = _characterCacheByNameStore.find(name);
260 if (itr != _characterCacheByNameStore.end())
261 {
262 return itr->second;
263 }
264
265 return nullptr;
266}

◆ GetCharacterGroupGuidByGuid()

ObjectGuid CharacterCache::GetCharacterGroupGuidByGuid ( ObjectGuid  guid) const
358{
359 auto itr = _characterCacheStore.find(guid);
360 if (itr == _characterCacheStore.end())
361 {
362 return ObjectGuid::Empty;
363 }
364
365 return itr->second.GroupGuid;
366}

References ObjectGuid::Empty.

◆ GetCharacterGuidByName()

ObjectGuid CharacterCache::GetCharacterGuidByName ( std::string const &  name) const
269{
270 auto itr = _characterCacheByNameStore.find(name);
271 if (itr != _characterCacheByNameStore.end())
272 {
273 return itr->second->Guid;
274 }
275
276 return ObjectGuid::Empty;
277}

References ObjectGuid::Empty.

◆ GetCharacterGuildIdByGuid()

ObjectGuid::LowType CharacterCache::GetCharacterGuildIdByGuid ( ObjectGuid  guid) const
336{
337 auto itr = _characterCacheStore.find(guid);
338 if (itr == _characterCacheStore.end())
339 {
340 return 0;
341 }
342
343 return itr->second.GuildId;
344}

◆ GetCharacterLevelByGuid()

uint8 CharacterCache::GetCharacterLevelByGuid ( ObjectGuid  guid) const
325{
326 auto itr = _characterCacheStore.find(guid);
327 if (itr == _characterCacheStore.end())
328 {
329 return 0;
330 }
331
332 return itr->second.Level;
333}

◆ GetCharacterNameByGuid()

bool CharacterCache::GetCharacterNameByGuid ( ObjectGuid  guid,
std::string &  name 
) const
280{
281 auto itr = _characterCacheStore.find(guid);
282 if (itr == _characterCacheStore.end())
283 {
284 return false;
285 }
286
287 name = itr->second.Name;
288 return true;
289}

◆ GetCharacterTeamByGuid()

uint32 CharacterCache::GetCharacterTeamByGuid ( ObjectGuid  guid) const
292{
293 auto itr = _characterCacheStore.find(guid);
294 if (itr == _characterCacheStore.end())
295 {
296 return 0;
297 }
298
299 return Player::TeamIdForRace(itr->second.Race);
300}
static TeamId TeamIdForRace(uint8 race)
Definition Player.cpp:6001

References Player::TeamIdForRace().

◆ HasCharacterCacheEntry()

bool CharacterCache::HasCharacterCacheEntry ( ObjectGuid const &  guid) const
242{
243 return _characterCacheStore.find(guid) != _characterCacheStore.end();
244}

◆ IncreaseCharacterMailCount()

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

◆ instance()

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

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:88
64{
65 _characterCacheStore.clear();
66 uint32 oldMSTime = getMSTime();
67
68 QueryResult result = CharacterDatabase.Query("SELECT guid, name, account, race, gender, class, level FROM characters");
69 if (!result)
70 {
71 LOG_INFO("server.loading", "No character name data loaded, empty query!");
72 return;
73 }
74
75 do
76 {
77 Field* fields = result->Fetch();
78 AddCharacterCacheEntry(ObjectGuid::Create<HighGuid::Player>(fields[0].Get<uint32>()) /*guid*/, fields[2].Get<uint32>() /*account*/, fields[1].Get<std::string>() /*name*/,
79 fields[4].Get<uint8>() /*gender*/, fields[3].Get<uint8>() /*race*/, fields[5].Get<uint8>() /*class*/, fields[6].Get<uint8>() /*level*/);
80 } while (result->NextRow());
81
82 sMailMgr->LoadMailCounts();
83
84 LOG_INFO("server.loading", ">> Loaded Character Infos For {} Characters in {} ms", _characterCacheStore.size(), GetMSTimeDiffToNow(oldMSTime));
85 LOG_INFO("server.loading", " ");
86}
std::shared_ptr< ResultSet > QueryResult
Definition DatabaseEnvFwd.h:27
DatabaseWorkerPool< CharacterDatabaseConnection > CharacterDatabase
Accessor to the character database.
Definition DatabaseEnv.cpp:21
std::uint32_t uint32
Definition Define.h:107
#define LOG_INFO(filterType__,...)
Definition Log.h:153
#define sMailMgr
Definition MailMgr.h:85
uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
Definition Timer.h:131
uint32 getMSTime()
Definition Timer.h:103
void AddCharacterCacheEntry(ObjectGuid const &guid, uint32 accountId, std::string const &name, uint8 gender, uint8 race, uint8 playerClass, uint8 level)
Definition CharacterCache.cpp:109
Class used to access individual fields of database query result.
Definition Field.h:99

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

◆ RefreshCacheEntry()

void CharacterCache::RefreshCacheEntry ( uint32  lowGuid)
89{
90 QueryResult result = CharacterDatabase.Query("SELECT guid, name, account, race, gender, class, level FROM characters WHERE guid = {}", lowGuid);
91 if (!result)
92 {
93 return;
94 }
95
96 do
97 {
98 Field* fields = result->Fetch();
99 DeleteCharacterCacheEntry(ObjectGuid::Create<HighGuid::Player>(lowGuid), fields[1].Get<std::string>());
100 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*/);
101 } while (result->NextRow());
102
103 sMailMgr->RecountMailCount(lowGuid);
104}
void DeleteCharacterCacheEntry(ObjectGuid const &guid, std::string const &name)
Definition CharacterCache.cpp:129

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

◆ UpdateCharacterAccountId()

void CharacterCache::UpdateCharacterAccountId ( ObjectGuid const &  guid,
uint32  accountId 
)
174{
175 auto itr = _characterCacheStore.find(guid);
176 if (itr == _characterCacheStore.end())
177 {
178 return;
179 }
180
181 itr->second.AccountId = accountId;
182}

◆ UpdateCharacterArenaTeamId()

void CharacterCache::UpdateCharacterArenaTeamId ( ObjectGuid const &  guid,
uint8  slot,
uint32  arenaTeamId 
)
196{
197 auto itr = _characterCacheStore.find(guid);
198 if (itr == _characterCacheStore.end())
199 {
200 return;
201 }
202
203 itr->second.ArenaTeamId[slot] = arenaTeamId;
204}

◆ UpdateCharacterData()

void CharacterCache::UpdateCharacterData ( ObjectGuid const &  guid,
std::string const &  name,
Optional< uint8 >  gender = {},
Optional< uint8 >  race = {} 
)
136{
137 auto itr = _characterCacheStore.find(guid);
138 if (itr == _characterCacheStore.end())
139 return;
140
141 std::string oldName = itr->second.Name;
142 itr->second.Name = name;
143
144 if (gender)
145 {
146 itr->second.Sex = *gender;
147 }
148
149 if (race)
150 {
151 itr->second.Race = *race;
152 }
153
154 //WorldPackets::Misc::InvalidatePlayer packet(guid);
155 //sWorld->SendGlobalMessage(packet.Write());
156
157 // Correct name -> pointer storage
158 _characterCacheByNameStore.erase(oldName);
159 _characterCacheByNameStore[name] = &itr->second;
160}

References CharacterCacheEntry::Name.

◆ UpdateCharacterGroup()

void CharacterCache::UpdateCharacterGroup ( ObjectGuid const &  guid,
ObjectGuid  groupGUID 
)
228{
229 auto itr = _characterCacheStore.find(guid);
230 if (itr == _characterCacheStore.end())
231 {
232 return;
233 }
234
235 itr->second.GroupGuid = groupGUID;
236}

◆ UpdateCharacterGuildId()

void CharacterCache::UpdateCharacterGuildId ( ObjectGuid const &  guid,
ObjectGuid::LowType  guildId 
)
185{
186 auto itr = _characterCacheStore.find(guid);
187 if (itr == _characterCacheStore.end())
188 {
189 return;
190 }
191
192 itr->second.GuildId = guildId;
193}

◆ UpdateCharacterLevel()

void CharacterCache::UpdateCharacterLevel ( ObjectGuid const &  guid,
uint8  level 
)
163{
164 auto itr = _characterCacheStore.find(guid);
165 if (itr == _characterCacheStore.end())
166 {
167 return;
168 }
169
170 itr->second.Level = level;
171}

◆ UpdateCharacterMailCount()

void CharacterCache::UpdateCharacterMailCount ( ObjectGuid const &  guid,
int32  count,
bool  update = false 
)
private
207{
208 auto itr = _characterCacheStore.find(guid);
209 if (itr == _characterCacheStore.end())
210 return;
211
212 constexpr int32 maxCount = std::numeric_limits<uint16>::max();
213
214 if (update)
215 {
216 itr->second.MailCount = static_cast<uint16>(std::clamp<int32>(count, 0, maxCount));
217 return;
218 }
219
220 int32 newCount = static_cast<int32>(itr->second.MailCount) + count;
221 if (newCount < 0)
222 LOG_WARN("entities.player", "CharacterCache::UpdateCharacterMailCount: mail count for {} would go negative ({}), a mail insert was not reported; clamping to 0", guid.ToString(), newCount);
223
224 itr->second.MailCount = static_cast<uint16>(std::clamp(newCount, 0, maxCount));
225}
std::int32_t int32
Definition Define.h:103
std::uint16_t uint16
Definition Define.h:108
#define LOG_WARN(filterType__,...)
Definition Log.h:149

References LOG_WARN, and ObjectGuid::ToString().

Friends And Related Symbol Documentation

◆ MailMgr

friend class MailMgr
friend

The documentation for this class was generated from the following files: