From ea06dcf418d6207f1344ad50c54689f89923b9ea Mon Sep 17 00:00:00 2001 From: leak Date: Wed, 4 May 2011 10:08:09 +0200 Subject: Core/ObjectMgr: Refactor guild related functions into dedicated class --- src/server/game/Guilds/GuildMgr.cpp | 415 ++++++++++++++++++++++++++++++++++++ 1 file changed, 415 insertions(+) create mode 100644 src/server/game/Guilds/GuildMgr.cpp (limited to 'src/server/game/Guilds/GuildMgr.cpp') diff --git a/src/server/game/Guilds/GuildMgr.cpp b/src/server/game/Guilds/GuildMgr.cpp new file mode 100644 index 00000000000..45844823912 --- /dev/null +++ b/src/server/game/Guilds/GuildMgr.cpp @@ -0,0 +1,415 @@ +/* + * Copyright (C) 2008-2011 TrinityCore + * + * 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 . + */ + +#include "Common.h" +#include "GuildMgr.h" + +GuildMgr::GuildMgr() +{ + NextGuildId = 1; +} + +GuildMgr::~GuildMgr() +{ + for (GuildContainer::iterator itr = GuildStore.begin(); itr != GuildStore.end(); ++itr) + delete itr->second; +} + +void GuildMgr::AddGuild(Guild* guild) +{ + GuildStore[guild->GetId()] = guild; +} + +void GuildMgr::RemoveGuild(uint32 guildId) +{ + GuildStore.erase(guildId); +} + +uint32 GuildMgr::GenerateGuildId() +{ + if (NextGuildId >= 0xFFFFFFFE) + { + sLog->outError("Guild ids overflow!! Can't continue, shutting down server. "); + World::StopNow(ERROR_EXIT_CODE); + } + return NextGuildId++; +} + +// Guild collection +Guild* GuildMgr::GetGuildById(uint32 guildId) const +{ + GuildContainer::const_iterator itr = GuildStore.find(guildId); + if (itr != GuildStore.end()) + return itr->second; + + return NULL; +} + +Guild* GuildMgr::GetGuildByName(const std::string& guildName) const +{ + std::string search = guildName; + std::transform(search.begin(), search.end(), search.begin(), ::toupper); + for (GuildContainer::const_iterator itr = GuildStore.begin(); itr != GuildStore.end(); ++itr) + { + std::string gname = itr->second->GetName(); + std::transform(gname.begin(), gname.end(), gname.begin(), ::toupper); + if (search == gname) + return itr->second; + } + return NULL; +} + +std::string GuildMgr::GetGuildNameById(uint32 guildId) const +{ + if (Guild* guild = GetGuildById(guildId)) + return guild->GetName(); + + return ""; +} + +Guild* GuildMgr::GetGuildByLeader(const uint64 &guid) const +{ + for (GuildContainer::const_iterator itr = GuildStore.begin(); itr != GuildStore.end(); ++itr) + if (itr->second->GetLeaderGUID() == guid) + return itr->second; + + return NULL; +} + +void GuildMgr::LoadGuilds() +{ + // 1. Load all guilds + sLog->outString("Loading guilds definitions..."); + { + uint32 oldMSTime = getMSTime(); + + PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_LOAD_GUILDS); + PreparedQueryResult result = CharacterDatabase.Query(stmt); + + if (!result) + { + sLog->outString(">> Loaded 0 guild definitions. DB table `guild` is empty."); + sLog->outString(); + return; + } + else + { + uint32 count = 0; + do + { + Field* fields = result->Fetch(); + Guild* guild = new Guild(); + + if (!guild->LoadFromDB(fields)) + { + delete guild; + continue; + } + AddGuild(guild); + + ++count; + } + while (result->NextRow()); + + sLog->outString(">> Loaded %u guild definitions in %u ms", count, GetMSTimeDiffToNow(oldMSTime)); + sLog->outString(); + } + } + + // 2. Load all guild ranks + sLog->outString("Loading guild ranks..."); + { + uint32 oldMSTime = getMSTime(); + + // Delete orphaned guild rank entries before loading the valid ones + PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_NONEXISTENT_GUILD_RANKS); + CharacterDatabase.Execute(stmt); + + stmt = CharacterDatabase.GetPreparedStatement(CHAR_LOAD_GUILD_RANKS); + PreparedQueryResult result = CharacterDatabase.Query(stmt); + + if (!result) + { + sLog->outString(">> Loaded 0 guild ranks. DB table `guild_rank` is empty."); + sLog->outString(); + } + else + { + uint32 count = 0; + do + { + Field* fields = result->Fetch(); + uint32 guildId = fields[0].GetUInt32(); + + if (Guild* guild = GetGuildById(guildId)) + guild->LoadRankFromDB(fields); + + ++count; + } + while (result->NextRow()); + + sLog->outString(">> Loaded %u guild ranks in %u ms", count, GetMSTimeDiffToNow(oldMSTime)); + sLog->outString(); + } + } + + // 3. Load all guild members + sLog->outString("Loading guild members..."); + { + uint32 oldMSTime = getMSTime(); + + // Delete orphaned guild member entries before loading the valid ones + PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_NONEXISTENT_GUILD_MEMBERS); + CharacterDatabase.Execute(stmt); + + stmt = CharacterDatabase.GetPreparedStatement(CHAR_LOAD_GUILD_MEMBERS); + PreparedQueryResult result = CharacterDatabase.Query(stmt); + + if (!result) + { + sLog->outString(">> Loaded 0 guild members. DB table `guild_member` is empty."); + sLog->outString(); + } + else + { + uint32 count = 0; + + do + { + Field* fields = result->Fetch(); + uint32 guildId = fields[0].GetUInt32(); + + if (Guild* guild = GetGuildById(guildId)) + guild->LoadMemberFromDB(fields); + + ++count; + } + while (result->NextRow()); + + sLog->outString(">> Loaded %u guild members int %u ms", count, GetMSTimeDiffToNow(oldMSTime)); + sLog->outString(); + } + } + + // 4. Load all guild bank tab rights + sLog->outString("Loading bank tab rights..."); + { + uint32 oldMSTime = getMSTime(); + + // Delete orphaned guild bank right entries before loading the valid ones + PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_NONEXISTENT_GUILD_BANK_RIGHTS); + CharacterDatabase.Execute(stmt); + + stmt = CharacterDatabase.GetPreparedStatement(CHAR_LOAD_GUILD_BANK_RIGHTS); + PreparedQueryResult result = CharacterDatabase.Query(stmt); + + if (!result) + { + sLog->outString(">> Loaded 0 guild bank tab rights. DB table `guild_bank_right` is empty."); + sLog->outString(); + } + else + { + uint32 count = 0; + do + { + Field* fields = result->Fetch(); + uint32 guildId = fields[0].GetUInt32(); + + if (Guild* guild = GetGuildById(guildId)) + guild->LoadBankRightFromDB(fields); + + ++count; + } + while (result->NextRow()); + + sLog->outString(">> Loaded %u bank tab rights in %u ms", count, GetMSTimeDiffToNow(oldMSTime)); + sLog->outString(); + } + } + + // 5. Load all event logs + sLog->outString("Loading guild event logs..."); + { + uint32 oldMSTime = getMSTime(); + + PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_OLD_GUILD_EVENT_LOGS); + stmt->setUInt32(0, sWorld->getIntConfig(CONFIG_GUILD_EVENT_LOG_COUNT)); + CharacterDatabase.Execute(stmt); + + stmt = CharacterDatabase.GetPreparedStatement(CHAR_LOAD_GUILD_EVENTLOGS); + PreparedQueryResult result = CharacterDatabase.Query(stmt); + + if (!result) + { + sLog->outString(">> Loaded 0 guild event logs. DB table `guild_eventlog` is empty."); + sLog->outString(); + } + else + { + uint32 count = 0; + do + { + Field* fields = result->Fetch(); + uint32 guildId = fields[0].GetUInt32(); + + if (Guild* guild = GetGuildById(guildId)) + guild->LoadEventLogFromDB(fields); + + ++count; + } + while (result->NextRow()); + + sLog->outString(">> Loaded %u guild event logs in %u ms", count, GetMSTimeDiffToNow(oldMSTime)); + sLog->outString(); + } + } + + // 6. Load all bank event logs + sLog->outString("Loading guild bank event logs..."); + { + uint32 oldMSTime = getMSTime(); + + // Remove log entries that exceed the number of allowed entries per guild + PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_OLD_GUILD_BANK_EVENT_LOGS); + stmt->setUInt32(0, sWorld->getIntConfig(CONFIG_GUILD_BANK_EVENT_LOG_COUNT)); + CharacterDatabase.Execute(stmt); + + stmt = CharacterDatabase.GetPreparedStatement(CHAR_LOAD_GUILD_BANK_EVENTLOGS); + PreparedQueryResult result = CharacterDatabase.Query(stmt); + + if (!result) + { + sLog->outString(">> Loaded 0 guild bank event logs. DB table `guild_bank_eventlog` is empty."); + sLog->outString(); + } + else + { + uint32 count = 0; + do + { + Field* fields = result->Fetch(); + uint32 guildId = fields[0].GetUInt32(); + + if (Guild* guild = GetGuildById(guildId)) + guild->LoadBankEventLogFromDB(fields); + + ++count; + } + while (result->NextRow()); + + sLog->outString(">> Loaded %u guild bank event logs in %u ms", count, GetMSTimeDiffToNow(oldMSTime)); + sLog->outString(); + } + } + + // 7. Load all guild bank tabs + sLog->outString("Loading guild bank tabs..."); + { + uint32 oldMSTime = getMSTime(); + + // Delete orphaned guild bank tab entries before loading the valid ones + PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_NONEXISTENT_GUILD_BANK_TABS); + CharacterDatabase.Execute(stmt); + + stmt = CharacterDatabase.GetPreparedStatement(CHAR_LOAD_GUILD_BANK_TABS); + PreparedQueryResult result = CharacterDatabase.Query(stmt); + + if (!result) + { + sLog->outString(">> Loaded 0 guild bank tabs. DB table `guild_bank_tab` is empty."); + sLog->outString(); + } + else + { + uint32 count = 0; + do + { + Field* fields = result->Fetch(); + uint32 guildId = fields[0].GetUInt32(); + + if (Guild* guild = GetGuildById(guildId)) + guild->LoadBankTabFromDB(fields); + + ++count; + } + while (result->NextRow()); + + sLog->outString(">> Loaded %u guild bank tabs in %u ms", count, GetMSTimeDiffToNow(oldMSTime)); + sLog->outString(); + } + } + + // 8. Fill all guild bank tabs + sLog->outString("Filling bank tabs with items..."); + { + uint32 oldMSTime = getMSTime(); + + // Delete orphan guild bank items + PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_NONEXISTENT_GUILD_BANK_ITEMS); + CharacterDatabase.Execute(stmt); + + stmt = CharacterDatabase.GetPreparedStatement(CHAR_LOAD_GUILD_BANK_ITEMS); + PreparedQueryResult result = CharacterDatabase.Query(stmt); + + if (!result) + { + sLog->outString(">> Loaded 0 guild bank tab items. DB table `guild_bank_item` or `item_instance` is empty."); + sLog->outString(); + } + else + { + uint32 count = 0; + do + { + Field* fields = result->Fetch(); + uint32 guildId = fields[11].GetUInt32(); + + if (Guild* guild = GetGuildById(guildId)) + guild->LoadBankItemFromDB(fields); + + ++count; + } + while (result->NextRow()); + + sLog->outString(">> Loaded %u guild bank tab items in %u ms", count, GetMSTimeDiffToNow(oldMSTime)); + sLog->outString(); + } + } + + // 9. Validate loaded guild data + sLog->outString("Validating data of loaded guilds..."); + { + uint32 oldMSTime = getMSTime(); + + for (GuildContainer::iterator itr = GuildStore.begin(); itr != GuildStore.end(); ++itr) + { + Guild* guild = itr->second; + if (guild) + { + if (!guild->Validate()) + { + RemoveGuild(guild->GetId()); + delete guild; + } + } + } + + sLog->outString(">> Validated data of loaded guilds in %u ms", GetMSTimeDiffToNow(oldMSTime)); + sLog->outString(); + } +} -- cgit v1.2.3