/* * 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 . */ #include "GroupMgr.h" #include "Common.h" #include "DatabaseEnv.h" #include "Group.h" #include "Log.h" #include "World.h" GroupMgr::GroupMgr() { NextGroupDbStoreId = 1; NextGroupId = UI64LIT(1); } GroupMgr::~GroupMgr() { for (GroupContainer::iterator itr = GroupStore.begin(); itr != GroupStore.end(); ++itr) delete itr->second; } uint32 GroupMgr::GenerateNewGroupDbStoreId() { uint32 newStorageId = NextGroupDbStoreId; for (uint32 i = ++NextGroupDbStoreId; i < 0xFFFFFFFF; ++i) { if ((i < GroupDbStore.size() && GroupDbStore[i] == nullptr) || i >= GroupDbStore.size()) { NextGroupDbStoreId = i; break; } } if (newStorageId == NextGroupDbStoreId) { TC_LOG_ERROR("misc", "Group storage ID overflow!! Can't continue, shutting down server. "); World::StopNow(ERROR_EXIT_CODE); } return newStorageId; } void GroupMgr::RegisterGroupDbStoreId(uint32 storageId, Group* group) { // Allocate space if necessary. if (storageId >= uint32(GroupDbStore.size())) GroupDbStore.resize(storageId + 1); GroupDbStore[storageId] = group; } void GroupMgr::FreeGroupDbStoreId(Group* group) { uint32 storageId = group->GetDbStoreId(); if (storageId < NextGroupDbStoreId) NextGroupDbStoreId = storageId; GroupDbStore[storageId] = nullptr; } Group* GroupMgr::GetGroupByDbStoreId(uint32 storageId) const { if (storageId < GroupDbStore.size()) return GroupDbStore[storageId]; return nullptr; } ObjectGuid::LowType GroupMgr::GenerateGroupId() { if (NextGroupId >= 0xFFFFFFFE) { TC_LOG_ERROR("misc", "Group guid overflow!! Can't continue, shutting down server. "); World::StopNow(ERROR_EXIT_CODE); } return NextGroupId++; } GroupMgr* GroupMgr::instance() { static GroupMgr instance; return &instance; } Group* GroupMgr::GetGroupByGUID(ObjectGuid const& groupId) const { GroupContainer::const_iterator itr = GroupStore.find(groupId.GetCounter()); if (itr != GroupStore.end()) return itr->second; return nullptr; } void GroupMgr::Update(uint32 diff) { for (std::pair const& group : GroupStore) group.second->Update(diff); } void GroupMgr::AddGroup(Group* group) { GroupStore[group->GetGUID().GetCounter()] = group; } void GroupMgr::RemoveGroup(Group* group) { GroupStore.erase(group->GetGUID().GetCounter()); } void GroupMgr::LoadGroups() { { uint32 oldMSTime = getMSTime(); // Delete all members that does not exist CharacterDatabase.DirectExecute("DELETE FROM group_member WHERE memberGuid NOT IN (SELECT guid FROM characters)"); // Delete all groups whose leader does not exist CharacterDatabase.DirectExecute("DELETE FROM `groups` WHERE leaderGuid NOT IN (SELECT guid FROM characters)"); // Delete all groups with less than 2 members CharacterDatabase.DirectExecute("DELETE FROM `groups` WHERE guid NOT IN (SELECT guid FROM group_member GROUP BY guid HAVING COUNT(guid) > 1)"); // Delete all rows from group_member with no group CharacterDatabase.DirectExecute("DELETE FROM group_member WHERE guid NOT IN (SELECT guid FROM `groups`)"); // 0 1 2 3 4 5 6 7 8 9 QueryResult result = CharacterDatabase.Query("SELECT g.leaderGuid, g.lootMethod, g.looterGuid, g.lootThreshold, g.icon1, g.icon2, g.icon3, g.icon4, g.icon5, g.icon6" // 10 11 12 13 14 15 16 17 18 19 20 ", g.icon7, g.icon8, g.groupType, g.difficulty, g.raiddifficulty, g.legacyRaidDifficulty, g.masterLooterGuid, g.guid, g.pingRestriction, lfg.dungeon, lfg.state FROM `groups` g LEFT JOIN lfg_data lfg ON lfg.guid = g.guid ORDER BY g.guid ASC"); if (!result) { TC_LOG_INFO("server.loading", ">> Loaded 0 group definitions. DB table `groups` is empty!"); return; } uint32 count = 0; do { Field* fields = result->Fetch(); Group* group = new Group; group->LoadGroupFromDB(fields); AddGroup(group); // Get the ID used for storing the group in the database and register it in the pool. uint32 storageId = group->GetDbStoreId(); RegisterGroupDbStoreId(storageId, group); // Increase the next available storage ID if (storageId == NextGroupDbStoreId) NextGroupDbStoreId++; ++count; } while (result->NextRow()); TC_LOG_INFO("server.loading", ">> Loaded {} group definitions in {} ms", count, GetMSTimeDiffToNow(oldMSTime)); } TC_LOG_INFO("server.loading", "Loading Group members..."); { uint32 oldMSTime = getMSTime(); // 0 1 2 3 4 QueryResult result = CharacterDatabase.Query("SELECT guid, memberGuid, memberFlags, subgroup, roles FROM group_member ORDER BY guid"); if (!result) { TC_LOG_INFO("server.loading", ">> Loaded 0 group members. DB table `group_member` is empty!"); return; } uint32 count = 0; do { Field* fields = result->Fetch(); Group* group = GetGroupByDbStoreId(fields[0].GetUInt32()); if (group) group->LoadMemberFromDB(fields[1].GetUInt64(), fields[2].GetUInt8(), fields[3].GetUInt8(), fields[4].GetUInt8()); else TC_LOG_ERROR("misc", "GroupMgr::LoadGroups: Consistency failed, can't find group (storage id: {})", fields[0].GetUInt32()); ++count; } while (result->NextRow()); TC_LOG_INFO("server.loading", ">> Loaded {} group members in {} ms", count, GetMSTimeDiffToNow(oldMSTime)); } }