1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
/*
* This file is part of the TrinityCore 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 General Public License as published by the
* Free Software Foundation; either version 2 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 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 "CharacterCache.h"
#include "ArenaTeam.h"
#include "DatabaseEnv.h"
#include "Log.h"
#include "MiscPackets.h"
#include "Player.h"
#include "Timer.h"
#include "World.h"
#include "WorldPacket.h"
#include <unordered_map>
namespace
{
std::unordered_map<ObjectGuid, CharacterCacheEntry> _characterCacheStore;
std::unordered_map<std::string, CharacterCacheEntry*> _characterCacheByNameStore;
}
CharacterCache::CharacterCache()
{
}
CharacterCache::~CharacterCache()
{
}
CharacterCache* CharacterCache::instance()
{
static CharacterCache instance;
return &instance;
}
/**
* @brief 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.
*
* @param guid Requires a guid to call
* @return Name, Gender, Race, Class and Level of player character
* Example Usage:
* @code
* 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;
* @endcode
**/
void CharacterCache::LoadCharacterCacheStorage()
{
_characterCacheStore.clear();
uint32 oldMSTime = getMSTime();
QueryResult result = CharacterDatabase.Query("SELECT guid, name, account, race, gender, class, level, deleteDate FROM characters");
if (!result)
{
TC_LOG_INFO("server.loading", "No character name data loaded, empty query");
return;
}
do
{
Field* fields = result->Fetch();
AddCharacterCacheEntry(ObjectGuid::Create<HighGuid::Player>(fields[0].GetUInt64()) /*guid*/, fields[2].GetUInt32() /*account*/, fields[1].GetString() /*name*/,
fields[4].GetUInt8() /*gender*/, fields[3].GetUInt8() /*race*/, fields[5].GetUInt8() /*class*/, fields[6].GetUInt8() /*level*/, fields[7].GetUInt32() != 0);
} while (result->NextRow());
TC_LOG_INFO("server.loading", "Loaded character infos for " SZFMTD " characters in %u ms", _characterCacheStore.size(), GetMSTimeDiffToNow(oldMSTime));
}
/*
Modifying functions
*/
void CharacterCache::AddCharacterCacheEntry(ObjectGuid const& guid, uint32 accountId, std::string const& name, uint8 gender, uint8 race, uint8 playerClass, uint8 level, bool isDeleted)
{
CharacterCacheEntry& data = _characterCacheStore[guid];
data.Guid = guid;
data.Name = name;
data.AccountId = accountId;
data.Race = race;
data.Sex = gender;
data.Class = playerClass;
data.Level = level;
data.GuildId = 0; // Will be set in guild loading or guild setting
for (uint8 i = 0; i < MAX_ARENA_SLOT; ++i)
data.ArenaTeamId[i] = 0; // Will be set in arena teams loading
data.IsDeleted = isDeleted;
// Fill Name to Guid Store
_characterCacheByNameStore[name] = &data;
}
void CharacterCache::DeleteCharacterCacheEntry(ObjectGuid const& guid, std::string const& name)
{
_characterCacheStore.erase(guid);
_characterCacheByNameStore.erase(name);
}
void CharacterCache::UpdateCharacterData(ObjectGuid const& guid, std::string const& name, uint8* gender /*= nullptr*/, uint8* race /*= nullptr*/)
{
auto itr = _characterCacheStore.find(guid);
if (itr == _characterCacheStore.end())
return;
std::string oldName = itr->second.Name;
itr->second.Name = name;
if (gender)
itr->second.Sex = *gender;
if (race)
itr->second.Race = *race;
WorldPackets::Misc::InvalidatePlayer invalidatePlayer;
invalidatePlayer.Guid = guid;
sWorld->SendGlobalMessage(invalidatePlayer.Write());
// Correct name -> pointer storage
_characterCacheByNameStore.erase(oldName);
_characterCacheByNameStore[name] = &itr->second;
}
void CharacterCache::UpdateCharacterGender(ObjectGuid const& guid, uint8 gender)
{
auto itr = _characterCacheStore.find(guid);
if (itr == _characterCacheStore.end())
return;
itr->second.Sex = gender;
}
void CharacterCache::UpdateCharacterLevel(ObjectGuid const& guid, uint8 level)
{
auto itr = _characterCacheStore.find(guid);
if (itr == _characterCacheStore.end())
return;
itr->second.Level = level;
}
void CharacterCache::UpdateCharacterAccountId(ObjectGuid const& guid, uint32 accountId)
{
auto itr = _characterCacheStore.find(guid);
if (itr == _characterCacheStore.end())
return;
itr->second.AccountId = accountId;
}
void CharacterCache::UpdateCharacterGuildId(ObjectGuid const& guid, ObjectGuid::LowType guildId)
{
auto itr = _characterCacheStore.find(guid);
if (itr == _characterCacheStore.end())
return;
itr->second.GuildId = guildId;
}
void CharacterCache::UpdateCharacterArenaTeamId(ObjectGuid const& guid, uint8 slot, uint32 arenaTeamId)
{
auto itr = _characterCacheStore.find(guid);
if (itr == _characterCacheStore.end())
return;
itr->second.ArenaTeamId[slot] = arenaTeamId;
}
void CharacterCache::UpdateCharacterInfoDeleted(ObjectGuid const& guid, bool deleted, std::string const* name /*=nullptr*/)
{
auto itr = _characterCacheStore.find(guid);
if (itr == _characterCacheStore.end())
return;
itr->second.IsDeleted = deleted;
if (name)
itr->second.Name = *name;
}
/*
Getters
*/
bool CharacterCache::HasCharacterCacheEntry(ObjectGuid const& guid) const
{
return _characterCacheStore.find(guid) != _characterCacheStore.end();
}
CharacterCacheEntry const* CharacterCache::GetCharacterCacheByGuid(ObjectGuid const& guid) const
{
auto itr = _characterCacheStore.find(guid);
if (itr != _characterCacheStore.end())
return &itr->second;
return nullptr;
}
CharacterCacheEntry const* CharacterCache::GetCharacterCacheByName(std::string const& name) const
{
auto itr = _characterCacheByNameStore.find(name);
if (itr != _characterCacheByNameStore.end())
return itr->second;
return nullptr;
}
ObjectGuid CharacterCache::GetCharacterGuidByName(std::string const& name) const
{
auto itr = _characterCacheByNameStore.find(name);
if (itr != _characterCacheByNameStore.end())
return itr->second->Guid;
return ObjectGuid::Empty;
}
bool CharacterCache::GetCharacterNameByGuid(ObjectGuid guid, std::string& name) const
{
auto itr = _characterCacheStore.find(guid);
if (itr == _characterCacheStore.end())
return false;
name = itr->second.Name;
return true;
}
uint32 CharacterCache::GetCharacterTeamByGuid(ObjectGuid guid) const
{
auto itr = _characterCacheStore.find(guid);
if (itr == _characterCacheStore.end())
return 0;
return Player::TeamForRace(itr->second.Race);
}
uint32 CharacterCache::GetCharacterAccountIdByGuid(ObjectGuid guid) const
{
auto itr = _characterCacheStore.find(guid);
if (itr == _characterCacheStore.end())
return 0;
return itr->second.AccountId;
}
uint32 CharacterCache::GetCharacterAccountIdByName(std::string const& name) const
{
auto itr = _characterCacheByNameStore.find(name);
if (itr != _characterCacheByNameStore.end())
return itr->second->AccountId;
return 0;
}
uint8 CharacterCache::GetCharacterLevelByGuid(ObjectGuid guid) const
{
auto itr = _characterCacheStore.find(guid);
if (itr == _characterCacheStore.end())
return 0;
return itr->second.Level;
}
ObjectGuid::LowType CharacterCache::GetCharacterGuildIdByGuid(ObjectGuid guid) const
{
auto itr = _characterCacheStore.find(guid);
if (itr == _characterCacheStore.end())
return 0;
return itr->second.GuildId;
}
uint32 CharacterCache::GetCharacterArenaTeamIdByGuid(ObjectGuid guid, uint8 type) const
{
auto itr = _characterCacheStore.find(guid);
if (itr == _characterCacheStore.end())
return 0;
return itr->second.ArenaTeamId[ArenaTeam::GetSlotByType(type)];
}
bool CharacterCache::GetCharacterNameAndClassByGUID(ObjectGuid guid, std::string& name, uint8& _class) const
{
auto itr = _characterCacheStore.find(guid);
if (itr == _characterCacheStore.end())
return false;
name = itr->second.Name;
_class = itr->second.Class;
return true;
}
|