aboutsummaryrefslogtreecommitdiff
path: root/src/server/game/Guilds
diff options
context:
space:
mode:
authorleak <leakzx@googlemail.com>2011-05-04 10:08:09 +0200
committerleak <leakzx@googlemail.com>2011-05-04 10:08:09 +0200
commitea06dcf418d6207f1344ad50c54689f89923b9ea (patch)
treea07b6d87b29677ef23191b1e91f37f3a15a06c45 /src/server/game/Guilds
parent6d63f92bdb88d8159353215f186c611b2d90965d (diff)
Core/ObjectMgr: Refactor guild related functions into dedicated class
Diffstat (limited to 'src/server/game/Guilds')
-rwxr-xr-xsrc/server/game/Guilds/Guild.cpp7
-rw-r--r--src/server/game/Guilds/GuildMgr.cpp415
-rw-r--r--src/server/game/Guilds/GuildMgr.h51
3 files changed, 470 insertions, 3 deletions
diff --git a/src/server/game/Guilds/Guild.cpp b/src/server/game/Guilds/Guild.cpp
index ffb56e20cb7..76c46392903 100755
--- a/src/server/game/Guilds/Guild.cpp
+++ b/src/server/game/Guilds/Guild.cpp
@@ -18,6 +18,7 @@
#include "DatabaseEnv.h"
#include "Guild.h"
+#include "GuildMgr.h"
#include "ScriptMgr.h"
#include "Chat.h"
#include "Config.h"
@@ -1092,14 +1093,14 @@ Guild::~Guild()
bool Guild::Create(Player* pLeader, const std::string& name)
{
// Check if guild with such name already exists
- if (sObjectMgr->GetGuildByName(name))
+ if (sGuildMgr->GetGuildByName(name))
return false;
WorldSession* pLeaderSession = pLeader->GetSession();
if (!pLeaderSession)
return false;
- m_id = sObjectMgr->GenerateGuildId();
+ m_id = sGuildMgr->GenerateGuildId();
m_leaderGuid = pLeader->GetGUID();
m_name = name;
m_info = "";
@@ -1194,7 +1195,7 @@ void Guild::Disband()
trans->Append(stmt);
CharacterDatabase.CommitTransaction(trans);
- sObjectMgr->RemoveGuild(m_id);
+ sGuildMgr->RemoveGuild(m_id);
}
///////////////////////////////////////////////////////////////////////////////
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 <http://www.trinitycore.org/>
+ *
+ * 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 "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();
+ }
+}
diff --git a/src/server/game/Guilds/GuildMgr.h b/src/server/game/Guilds/GuildMgr.h
new file mode 100644
index 00000000000..2eb91ff4ad8
--- /dev/null
+++ b/src/server/game/Guilds/GuildMgr.h
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2008-2011 TrinityCore <http://www.trinitycore.org/>
+ *
+ * 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/>.
+ */
+
+#ifndef _GUILDMGR_H
+#define _GUILDMGR_H
+
+#include "Guild.h"
+
+class GuildMgr
+{
+ friend class ACE_Singleton<GuildMgr, ACE_Null_Mutex>;
+ GuildMgr();
+ ~GuildMgr();
+
+public:
+ typedef UNORDERED_MAP<uint32, Guild*> GuildContainer;
+
+ Guild* GetGuildByLeader(uint64 const& guid) const;
+ Guild* GetGuildById(uint32 guildId) const;
+ Guild* GetGuildByName(const std::string& guildName) const;
+ std::string GetGuildNameById(uint32 guildId) const;
+
+ void LoadGuilds();
+ void AddGuild(Guild* guild);
+ void RemoveGuild(uint32 guildId);
+
+ uint32 GenerateGuildId();
+ void SetNextGuildId(uint32 Id) { NextGuildId = Id; }
+
+protected:
+ uint32 NextGuildId;
+ GuildContainer GuildStore;
+};
+
+#define sGuildMgr ACE_Singleton<GuildMgr, ACE_Null_Mutex>::instance()
+
+#endif