AzeorthCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
GuildMgr Class Reference

#include "GuildMgr.h"

Public Member Functions

GuildGetGuildByLeader (ObjectGuid guid) const
 
GuildGetGuildById (uint32 guildId) const
 
GuildGetGuildByName (std::string_view guildName) const
 
std::string GetGuildNameById (uint32 guildId) const
 
void LoadGuilds ()
 
void AddGuild (Guild *guild)
 
void RemoveGuild (uint32 guildId)
 
uint32 GenerateGuildId ()
 
void SetNextGuildId (uint32 Id)
 
void ResetTimes ()
 

Static Public Member Functions

static GuildMgrinstance ()
 

Protected Types

typedef std::unordered_map< uint32, Guild * > GuildContainer
 

Protected Attributes

uint32 NextGuildId
 
GuildContainer GuildStore
 

Private Member Functions

 GuildMgr ()
 
 ~GuildMgr ()
 

Detailed Description

Member Typedef Documentation

◆ GuildContainer

typedef std::unordered_map<uint32, Guild*> GuildMgr::GuildContainer
protected

Constructor & Destructor Documentation

◆ GuildMgr()

GuildMgr::GuildMgr ( )
private
21 : NextGuildId(1)
22{ }
uint32 NextGuildId
Definition: GuildMgr.h:47

◆ ~GuildMgr()

GuildMgr::~GuildMgr ( )
private
25{
26 for (GuildContainer::iterator itr = GuildStore.begin(); itr != GuildStore.end(); ++itr)
27 delete itr->second;
28}
GuildContainer GuildStore
Definition: GuildMgr.h:48

References GuildStore.

Member Function Documentation

◆ AddGuild()

void GuildMgr::AddGuild ( Guild guild)
37{
38 GuildStore[guild->GetId()] = guild;
39}
uint32 GetId() const
Definition: Guild.h:689

References Guild::GetId(), and GuildStore.

Referenced by LoadGuilds().

◆ GenerateGuildId()

uint32 GuildMgr::GenerateGuildId ( )
47{
48 if (NextGuildId >= 0xFFFFFFFE)
49 {
50 LOG_ERROR("server.worldserver", "Guild ids overflow!! Can't continue, shutting down server.");
52 }
53 return NextGuildId++;
54}
#define LOG_ERROR(filterType__,...)
Definition: Log.h:159
@ ERROR_EXIT_CODE
Definition: World.h:55
static void StopNow(uint8 exitcode)
Definition: World.h:262

References ERROR_EXIT_CODE, LOG_ERROR, NextGuildId, and World::StopNow().

◆ GetGuildById()

Guild * GuildMgr::GetGuildById ( uint32  guildId) const
58{
59 GuildContainer::const_iterator itr = GuildStore.find(guildId);
60 if (itr != GuildStore.end())
61 return itr->second;
62
63 return nullptr;
64}

References GuildStore.

Referenced by GetGuildNameById(), and LoadGuilds().

◆ GetGuildByLeader()

Guild * GuildMgr::GetGuildByLeader ( ObjectGuid  guid) const
84{
85 for (GuildContainer::const_iterator itr = GuildStore.begin(); itr != GuildStore.end(); ++itr)
86 if (itr->second->GetLeaderGUID() == guid)
87 return itr->second;
88
89 return nullptr;
90}

References GuildStore.

◆ GetGuildByName()

Guild * GuildMgr::GetGuildByName ( std::string_view  guildName) const
67{
68 for (auto const& [id, guild] : GuildStore)
69 if (StringEqualI(guild->GetName(), guildName))
70 return guild;
71
72 return nullptr;
73}
bool StringEqualI(std::string_view a, std::string_view b)
Definition: Util.cpp:588

References GuildStore, and StringEqualI().

◆ GetGuildNameById()

std::string GuildMgr::GetGuildNameById ( uint32  guildId) const
76{
77 if (Guild* guild = GetGuildById(guildId))
78 return guild->GetName();
79
80 return "";
81}
Definition: Guild.h:295
Guild * GetGuildById(uint32 guildId) const
Definition: GuildMgr.cpp:57

References GetGuildById().

◆ instance()

GuildMgr * GuildMgr::instance ( )
static
31{
32 static GuildMgr instance;
33 return &instance;
34}
Definition: GuildMgr.h:24
static GuildMgr * instance()
Definition: GuildMgr.cpp:30

References instance().

Referenced by instance().

◆ LoadGuilds()

void GuildMgr::LoadGuilds ( )
93{
94 // 1. Load all guilds
95 LOG_INFO("server.loading", "Loading Guilds Definitions...");
96 {
97 uint32 oldMSTime = getMSTime();
98
99 CharacterDatabase.DirectExecute("DELETE g FROM guild g LEFT JOIN guild_member gm ON g.guildid = gm.guildid WHERE gm.guildid IS NULL");
100
101 // 0 1 2 3 4 5 6
102 QueryResult result = CharacterDatabase.Query("SELECT g.guildid, g.name, g.leaderguid, g.EmblemStyle, g.EmblemColor, g.BorderStyle, g.BorderColor, "
103 // 7 8 9 10 11 12
104 "g.BackgroundColor, g.info, g.motd, g.createdate, g.BankMoney, COUNT(gbt.guildid) "
105 "FROM guild g LEFT JOIN guild_bank_tab gbt ON g.guildid = gbt.guildid GROUP BY g.guildid ORDER BY g.guildid ASC");
106
107 if (!result)
108 {
109 LOG_WARN("server.loading", ">> Loaded 0 guild definitions. DB table `guild` is empty.");
110 LOG_INFO("server.loading", " ");
111 }
112 else
113 {
114 uint32 count = 0;
115 do
116 {
117 Field* fields = result->Fetch();
118 Guild* guild = new Guild();
119
120 if (!guild->LoadFromDB(fields))
121 {
122 delete guild;
123 continue;
124 }
125
126 AddGuild(guild);
127
128 ++count;
129 } while (result->NextRow());
130
131 LOG_INFO("server.loading", ">> Loaded {} guild definitions in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
132 LOG_INFO("server.loading", " ");
133 }
134 }
135
136 // 2. Load all guild ranks
137 LOG_INFO("server.loading", "Loading Guild Ranks...");
138 {
139 uint32 oldMSTime = getMSTime();
140
141 // Delete orphaned guild rank entries before loading the valid ones
142 CharacterDatabase.DirectExecute("DELETE gr FROM guild_rank gr LEFT JOIN guild g ON gr.guildId = g.guildId WHERE g.guildId IS NULL");
143
144 // 0 1 2 3 4
145 QueryResult result = CharacterDatabase.Query("SELECT guildid, rid, rname, rights, BankMoneyPerDay FROM guild_rank ORDER BY guildid ASC, rid ASC");
146
147 if (!result)
148 {
149 LOG_WARN("server.loading", ">> Loaded 0 guild ranks. DB table `guild_rank` is empty.");
150 LOG_INFO("server.loading", " ");
151 }
152 else
153 {
154 uint32 count = 0;
155 do
156 {
157 Field* fields = result->Fetch();
158 uint32 guildId = fields[0].Get<uint32>();
159
160 if (Guild* guild = GetGuildById(guildId))
161 guild->LoadRankFromDB(fields);
162
163 ++count;
164 } while (result->NextRow());
165
166 LOG_INFO("server.loading", ">> Loaded {} guild ranks in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
167 LOG_INFO("server.loading", " ");
168 }
169 }
170
171 // 3. Load all guild members
172 LOG_INFO("server.loading", "Loading Guild Members...");
173 {
174 uint32 oldMSTime = getMSTime();
175
176 // Delete orphaned guild member entries before loading the valid ones
177 CharacterDatabase.DirectExecute("DELETE gm FROM guild_member gm LEFT JOIN guild g ON gm.guildId = g.guildId WHERE g.guildId IS NULL");
178 CharacterDatabase.DirectExecute("DELETE gm FROM guild_member_withdraw gm LEFT JOIN guild_member g ON gm.guid = g.guid WHERE g.guid IS NULL");
179
180 // 0 1 2 3 4 5 6 7 8 9 10
181 QueryResult result = CharacterDatabase.Query("SELECT guildid, gm.guid, `rank`, pnote, offnote, w.tab0, w.tab1, w.tab2, w.tab3, w.tab4, w.tab5, "
182 // 11 12 13 14 15 16 17 18
183 "w.money, c.name, c.level, c.class, c.gender, c.zone, c.account, c.logout_time "
184 "FROM guild_member gm "
185 "LEFT JOIN guild_member_withdraw w ON gm.guid = w.guid "
186 "LEFT JOIN characters c ON c.guid = gm.guid ORDER BY guildid ASC");
187
188 if (!result)
189 {
190 LOG_WARN("server.loading", ">> Loaded 0 guild members. DB table `guild_member` is empty.");
191 LOG_INFO("server.loading", " ");
192 }
193 else
194 {
195 uint32 count = 0;
196
197 do
198 {
199 Field* fields = result->Fetch();
200 uint32 guildId = fields[0].Get<uint32>();
201
202 if (Guild* guild = GetGuildById(guildId))
203 guild->LoadMemberFromDB(fields);
204
205 ++count;
206 } while (result->NextRow());
207
208 LOG_INFO("server.loading", ">> Loaded {} guild members int {} ms", count, GetMSTimeDiffToNow(oldMSTime));
209 LOG_INFO("server.loading", " ");
210 }
211 }
212
213 // 4. Load all guild bank tab rights
214 LOG_INFO("server.loading", "Loading Bank Tab Rights...");
215 {
216 uint32 oldMSTime = getMSTime();
217
218 // Delete orphaned guild bank right entries before loading the valid ones
219 CharacterDatabase.DirectExecute("DELETE gbr FROM guild_bank_right gbr LEFT JOIN guild g ON gbr.guildId = g.guildId WHERE g.guildId IS NULL");
220
221 // 0 1 2 3 4
222 QueryResult result = CharacterDatabase.Query("SELECT guildid, TabId, rid, gbright, SlotPerDay FROM guild_bank_right ORDER BY guildid ASC, TabId ASC");
223
224 if (!result)
225 {
226 LOG_WARN("server.loading", ">> Loaded 0 guild bank tab rights. DB table `guild_bank_right` is empty.");
227 LOG_INFO("server.loading", " ");
228 }
229 else
230 {
231 uint32 count = 0;
232 do
233 {
234 Field* fields = result->Fetch();
235 uint32 guildId = fields[0].Get<uint32>();
236
237 if (Guild* guild = GetGuildById(guildId))
238 guild->LoadBankRightFromDB(fields);
239
240 ++count;
241 } while (result->NextRow());
242
243 LOG_INFO("server.loading", ">> Loaded {} bank tab rights in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
244 LOG_INFO("server.loading", " ");
245 }
246 }
247
248 // 5. Load all event logs
249 LOG_INFO("server.loading", "Loading Guild Event Logs...");
250 {
251 uint32 oldMSTime = getMSTime();
252
253 CharacterDatabase.DirectExecute("DELETE FROM guild_eventlog WHERE LogGuid > {}", sWorld->getIntConfig(CONFIG_GUILD_EVENT_LOG_COUNT));
254
255 // 0 1 2 3 4 5 6
256 QueryResult result = CharacterDatabase.Query("SELECT guildid, LogGuid, EventType, PlayerGuid1, PlayerGuid2, NewRank, TimeStamp FROM guild_eventlog ORDER BY TimeStamp DESC, LogGuid DESC");
257
258 if (!result)
259 {
260 LOG_WARN("server.loading", ">> Loaded 0 guild event logs. DB table `guild_eventlog` is empty.");
261 LOG_INFO("server.loading", " ");
262 }
263 else
264 {
265 uint32 count = 0;
266 do
267 {
268 Field* fields = result->Fetch();
269 uint32 guildId = fields[0].Get<uint32>();
270
271 if (Guild* guild = GetGuildById(guildId))
272 guild->LoadEventLogFromDB(fields);
273
274 ++count;
275 } while (result->NextRow());
276
277 LOG_INFO("server.loading", ">> Loaded {} guild event logs in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
278 LOG_INFO("server.loading", " ");
279 }
280 }
281
282 // 6. Load all bank event logs
283 LOG_INFO("server.loading", "Loading Guild Bank Event Logs...");
284 {
285 uint32 oldMSTime = getMSTime();
286
287 // Remove log entries that exceed the number of allowed entries per guild
288 CharacterDatabase.DirectExecute("DELETE FROM guild_bank_eventlog WHERE LogGuid > {}", sWorld->getIntConfig(CONFIG_GUILD_BANK_EVENT_LOG_COUNT));
289
290 // 0 1 2 3 4 5 6 7 8
291 QueryResult result = CharacterDatabase.Query("SELECT guildid, TabId, LogGuid, EventType, PlayerGuid, ItemOrMoney, ItemStackCount, DestTabId, TimeStamp FROM guild_bank_eventlog ORDER BY TimeStamp DESC, LogGuid DESC");
292
293 if (!result)
294 {
295 LOG_WARN("server.loading", ">> Loaded 0 guild bank event logs. DB table `guild_bank_eventlog` is empty.");
296 LOG_INFO("server.loading", " ");
297 }
298 else
299 {
300 uint32 count = 0;
301 do
302 {
303 Field* fields = result->Fetch();
304 uint32 guildId = fields[0].Get<uint32>();
305
306 if (Guild* guild = GetGuildById(guildId))
307 guild->LoadBankEventLogFromDB(fields);
308
309 ++count;
310 } while (result->NextRow());
311
312 LOG_INFO("server.loading", ">> Loaded {} guild bank event logs in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
313 LOG_INFO("server.loading", " ");
314 }
315 }
316
317 // 7. Load all guild bank tabs
318 LOG_INFO("server.loading", "Loading Guild Bank Tabs...");
319 {
320 uint32 oldMSTime = getMSTime();
321
322 // Delete orphaned guild bank tab entries before loading the valid ones
323 CharacterDatabase.DirectExecute("DELETE gbt FROM guild_bank_tab gbt LEFT JOIN guild g ON gbt.guildId = g.guildId WHERE g.guildId IS NULL");
324
325 // 0 1 2 3 4
326 QueryResult result = CharacterDatabase.Query("SELECT guildid, TabId, TabName, TabIcon, TabText FROM guild_bank_tab ORDER BY guildid ASC, TabId ASC");
327
328 if (!result)
329 {
330 LOG_WARN("server.loading", ">> Loaded 0 guild bank tabs. DB table `guild_bank_tab` is empty.");
331 LOG_INFO("server.loading", " ");
332 }
333 else
334 {
335 uint32 count = 0;
336 do
337 {
338 Field* fields = result->Fetch();
339 uint32 guildId = fields[0].Get<uint32>();
340
341 if (Guild* guild = GetGuildById(guildId))
342 guild->LoadBankTabFromDB(fields);
343
344 ++count;
345 } while (result->NextRow());
346
347 LOG_INFO("server.loading", ">> Loaded {} guild bank tabs in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
348 LOG_INFO("server.loading", " ");
349 }
350 }
351
352 // 8. Fill all guild bank tabs
353 LOG_INFO("server.loading", "Filling bank tabs with items...");
354 {
355 uint32 oldMSTime = getMSTime();
356
357 // Delete orphan guild bank items
358 CharacterDatabase.DirectExecute("DELETE gbi FROM guild_bank_item gbi LEFT JOIN guild g ON gbi.guildId = g.guildId WHERE g.guildId IS NULL");
359
360 // 0 1 2 3 4 5 6 7 8 9 10
361 QueryResult result = CharacterDatabase.Query("SELECT creatorGuid, giftCreatorGuid, count, duration, charges, flags, enchantments, randomPropertyId, durability, playedTime, text, "
362 // 11 12 13 14 15
363 "guildid, TabId, SlotId, item_guid, itemEntry FROM guild_bank_item gbi INNER JOIN item_instance ii ON gbi.item_guid = ii.guid");
364
365 if (!result)
366 {
367 LOG_WARN("server.loading", ">> Loaded 0 guild bank tab items. DB table `guild_bank_item` or `item_instance` is empty.");
368 LOG_INFO("server.loading", " ");
369 }
370 else
371 {
372 uint32 count = 0;
373 do
374 {
375 Field* fields = result->Fetch();
376 uint32 guildId = fields[11].Get<uint32>();
377
378 if (Guild* guild = GetGuildById(guildId))
379 guild->LoadBankItemFromDB(fields);
380
381 ++count;
382 } while (result->NextRow());
383
384 LOG_INFO("server.loading", ">> Loaded {} guild bank tab items in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
385 LOG_INFO("server.loading", " ");
386 }
387 }
388
389 // 9. Validate loaded guild data
390 LOG_INFO("server.loading", "Validating data of loaded guilds...");
391 {
392 uint32 oldMSTime = getMSTime();
393
394 for (GuildContainer::iterator itr = GuildStore.begin(); itr != GuildStore.end();)
395 {
396 Guild* guild = itr->second;
397 ++itr;
398 if (guild && !guild->Validate())
399 delete guild;
400 }
401
402 LOG_INFO("server.loading", ">> Validated data of loaded guilds in {} ms", GetMSTimeDiffToNow(oldMSTime));
403 LOG_INFO("server.loading", " ");
404 }
405}
std::uint32_t uint32
Definition: Define.h:108
#define LOG_INFO(filterType__,...)
Definition: Log.h:167
#define LOG_WARN(filterType__,...)
Definition: Log.h:163
uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
Definition: Timer.h:131
uint32 getMSTime()
Definition: Timer.h:103
DatabaseWorkerPool< CharacterDatabaseConnection > CharacterDatabase
Accessor to the character database.
Definition: DatabaseEnv.cpp:21
std::shared_ptr< ResultSet > QueryResult
Definition: DatabaseEnvFwd.h:28
@ CONFIG_GUILD_EVENT_LOG_COUNT
Definition: IWorld.h:338
@ CONFIG_GUILD_BANK_EVENT_LOG_COUNT
Definition: IWorld.h:339
#define sWorld
Definition: World.h:458
Class used to access individual fields of database query result.
Definition: Field.h:99
std::enable_if_t< std::is_arithmetic_v< T >, T > Get() const
Definition: Field.h:113
void LoadBankRightFromDB(Field *fields)
Definition: Guild.cpp:1939
bool LoadMemberFromDB(Field *fields)
Definition: Guild.cpp:1915
bool LoadBankItemFromDB(Field *fields)
Definition: Guild.cpp:2013
bool LoadFromDB(Field *fields)
Definition: Guild.cpp:1884
bool LoadEventLogFromDB(Field *fields)
Definition: Guild.cpp:1947
bool LoadBankEventLogFromDB(Field *fields)
Definition: Guild.cpp:1964
void LoadBankTabFromDB(Field *fields)
Definition: Guild.cpp:2004
bool Validate()
Definition: Guild.cpp:2026
void LoadRankFromDB(Field *fields)
Definition: Guild.cpp:1906
void AddGuild(Guild *guild)
Definition: GuildMgr.cpp:36

References AddGuild(), CharacterDatabase, CONFIG_GUILD_BANK_EVENT_LOG_COUNT, CONFIG_GUILD_EVENT_LOG_COUNT, Field::Get(), GetGuildById(), getMSTime(), GetMSTimeDiffToNow(), GuildStore, Guild::LoadFromDB(), LOG_INFO, LOG_WARN, sWorld, and Guild::Validate().

◆ RemoveGuild()

void GuildMgr::RemoveGuild ( uint32  guildId)
42{
43 GuildStore.erase(guildId);
44}

References GuildStore.

◆ ResetTimes()

void GuildMgr::ResetTimes ( )
408{
409 for (GuildContainer::const_iterator itr = GuildStore.begin(); itr != GuildStore.end(); ++itr)
410 if (Guild* guild = itr->second)
411 guild->ResetTimes();
412
413 CharacterDatabase.DirectExecute("TRUNCATE guild_member_withdraw");
414}

References CharacterDatabase, and GuildStore.

◆ SetNextGuildId()

void GuildMgr::SetNextGuildId ( uint32  Id)
inline
42{ NextGuildId = Id; }

References NextGuildId.

Member Data Documentation

◆ GuildStore

◆ NextGuildId

uint32 GuildMgr::NextGuildId
protected

Referenced by GenerateGuildId(), and SetNextGuildId().