[2] | 1 | /* |
---|
| 2 | * Copyright (C) 2005,2006,2007 MaNGOS <http://www.mangosproject.org/> |
---|
| 3 | * |
---|
| 4 | * This program is free software; you can redistribute it and/or modify |
---|
| 5 | * it under the terms of the GNU General Public License as published by |
---|
| 6 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | * (at your option) any later version. |
---|
| 8 | * |
---|
| 9 | * This program is distributed in the hope that it will be useful, |
---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | * GNU General Public License for more details. |
---|
| 13 | * |
---|
| 14 | * You should have received a copy of the GNU General Public License |
---|
| 15 | * along with this program; if not, write to the Free Software |
---|
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | #include "InstanceSaveMgr.h" |
---|
| 20 | #include "Common.h" |
---|
| 21 | #include "Database/SQLStorage.h" |
---|
| 22 | |
---|
| 23 | #include "Player.h" |
---|
| 24 | #include "GridNotifiers.h" |
---|
| 25 | #include "WorldSession.h" |
---|
| 26 | #include "Log.h" |
---|
| 27 | #include "GridStates.h" |
---|
| 28 | #include "CellImpl.h" |
---|
| 29 | #include "Map.h" |
---|
| 30 | #include "MapManager.h" |
---|
| 31 | #include "MapInstanced.h" |
---|
| 32 | #include "InstanceSaveMgr.h" |
---|
| 33 | #include "Timer.h" |
---|
| 34 | #include "GridNotifiersImpl.h" |
---|
| 35 | #include "Config/ConfigEnv.h" |
---|
| 36 | #include "Transports.h" |
---|
| 37 | #include "ObjectAccessor.h" |
---|
| 38 | #include "ObjectMgr.h" |
---|
| 39 | #include "World.h" |
---|
| 40 | #include "Group.h" |
---|
| 41 | #include "InstanceData.h" |
---|
| 42 | #include "ProgressBar.h" |
---|
| 43 | |
---|
| 44 | INSTANTIATE_SINGLETON_1( InstanceSaveManager ); |
---|
| 45 | |
---|
| 46 | InstanceSaveManager::InstanceSaveManager() : lock_instLists(false) |
---|
| 47 | { |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | InstanceSaveManager::~InstanceSaveManager() |
---|
| 51 | { |
---|
| 52 | // it is undefined whether this or objectmgr will be unloaded first |
---|
| 53 | // so we must be prepared for both cases |
---|
| 54 | lock_instLists = true; |
---|
| 55 | for (InstanceSaveHashMap::iterator itr = m_instanceSaveById.begin(); itr != m_instanceSaveById.end(); ++itr) |
---|
| 56 | { |
---|
| 57 | InstanceSave *save = itr->second; |
---|
| 58 | for(InstanceSave::PlayerListType::iterator itr = save->m_playerList.begin(), next = itr; itr != save->m_playerList.end(); itr = next) |
---|
| 59 | { |
---|
| 60 | ++next; |
---|
| 61 | (*itr)->UnbindInstance(save->GetMapId(), save->GetDifficulty(), true); |
---|
| 62 | } |
---|
| 63 | save->m_playerList.clear(); |
---|
| 64 | for(InstanceSave::GroupListType::iterator itr = save->m_groupList.begin(), next = itr; itr != save->m_groupList.end(); itr = next) |
---|
| 65 | { |
---|
| 66 | ++next; |
---|
| 67 | (*itr)->UnbindInstance(save->GetMapId(), save->GetDifficulty(), true); |
---|
| 68 | } |
---|
| 69 | save->m_groupList.clear(); |
---|
| 70 | delete save; |
---|
| 71 | } |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | /* |
---|
| 75 | - adding instance into manager |
---|
| 76 | - called from InstanceMap::Add, _LoadBoundInstances, LoadGroups |
---|
| 77 | */ |
---|
| 78 | InstanceSave* InstanceSaveManager::AddInstanceSave(uint32 mapId, uint32 instanceId, uint8 difficulty, time_t resetTime, bool canReset, bool load) |
---|
| 79 | { |
---|
| 80 | InstanceSave *save = GetInstanceSave(instanceId); |
---|
| 81 | if(save) return save; |
---|
| 82 | |
---|
| 83 | const MapEntry* entry = sMapStore.LookupEntry(mapId); |
---|
| 84 | if(!entry || instanceId == 0) |
---|
| 85 | { |
---|
| 86 | sLog.outError("InstanceSaveManager::AddInstanceSave: mapid = %d, instanceid = %d!", mapId, instanceId); |
---|
| 87 | return NULL; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | if(!resetTime) |
---|
| 91 | { |
---|
| 92 | // initialize reset time |
---|
| 93 | // for normal instances if no creatures are killed the instance will reset in two hours |
---|
| 94 | if(entry->map_type == MAP_RAID || difficulty == DIFFICULTY_HEROIC) |
---|
| 95 | resetTime = GetResetTimeFor(mapId); |
---|
| 96 | else |
---|
| 97 | { |
---|
| 98 | resetTime = time(NULL) + 2 * HOUR; |
---|
| 99 | // normally this will be removed soon after in InstanceMap::Add, prevent error |
---|
| 100 | ScheduleReset(true, resetTime, InstResetEvent(0, mapId, instanceId)); |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | sLog.outDebug("InstanceSaveManager::AddInstanceSave: mapid = %d, instanceid = %d", mapId, instanceId); |
---|
| 105 | |
---|
| 106 | save = new InstanceSave(mapId, instanceId, difficulty, resetTime, canReset); |
---|
| 107 | if(!load) save->SaveToDB(); |
---|
| 108 | |
---|
| 109 | m_instanceSaveById[instanceId] = save; |
---|
| 110 | return save; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | InstanceSave *InstanceSaveManager::GetInstanceSave(uint32 InstanceId) |
---|
| 114 | { |
---|
| 115 | InstanceSaveHashMap::iterator itr = m_instanceSaveById.find(InstanceId); |
---|
| 116 | return itr != m_instanceSaveById.end() ? itr->second : NULL; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | void InstanceSaveManager::DeleteInstanceFromDB(uint32 instanceid) |
---|
| 120 | { |
---|
| 121 | CharacterDatabase.BeginTransaction(); |
---|
| 122 | CharacterDatabase.PExecute("DELETE FROM instance WHERE id = '%u'", instanceid); |
---|
| 123 | CharacterDatabase.PExecute("DELETE FROM character_instance WHERE instance = '%u'", instanceid); |
---|
| 124 | CharacterDatabase.PExecute("DELETE FROM group_instance WHERE instance = '%u'", instanceid); |
---|
| 125 | CharacterDatabase.CommitTransaction(); |
---|
| 126 | // respawn times should be deleted only when the map gets unloaded |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | void InstanceSaveManager::RemoveInstanceSave(uint32 InstanceId) |
---|
| 130 | { |
---|
| 131 | InstanceSaveHashMap::iterator itr = m_instanceSaveById.find( InstanceId ); |
---|
| 132 | if(itr != m_instanceSaveById.end()) |
---|
| 133 | { |
---|
| 134 | // save the resettime for normal instances only when they get unloaded |
---|
| 135 | if(time_t resettime = itr->second->GetResetTimeForDB()) |
---|
| 136 | CharacterDatabase.PExecute("UPDATE instance SET resettime = '"I64FMTD"' WHERE id = '%u'", (uint64)resettime, InstanceId); |
---|
| 137 | delete itr->second; |
---|
| 138 | m_instanceSaveById.erase(itr); |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | InstanceSave::InstanceSave(uint16 MapId, uint32 InstanceId, uint8 difficulty, |
---|
| 143 | time_t resetTime, bool canReset) |
---|
| 144 | : m_mapid(MapId), m_instanceid(InstanceId), m_resetTime(resetTime), |
---|
| 145 | m_difficulty(difficulty), m_canReset(canReset) |
---|
| 146 | { |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | InstanceSave::~InstanceSave() |
---|
| 150 | { |
---|
| 151 | // the players and groups must be unbound before deleting the save |
---|
| 152 | assert(m_playerList.empty() && m_groupList.empty()); |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | /* |
---|
| 156 | Called from AddInstanceSave |
---|
| 157 | */ |
---|
| 158 | void InstanceSave::SaveToDB() |
---|
| 159 | { |
---|
| 160 | // save instance data too |
---|
| 161 | std::string data; |
---|
| 162 | |
---|
| 163 | Map *map = MapManager::Instance().FindMap(m_instanceid, GetMapId()); |
---|
| 164 | if(map) |
---|
| 165 | { |
---|
| 166 | assert(map->IsDungeon()); |
---|
| 167 | InstanceData *iData = ((InstanceMap *)map)->GetInstanceData(); |
---|
| 168 | if(iData && iData->Save()) |
---|
| 169 | { |
---|
| 170 | data = iData->Save(); |
---|
| 171 | CharacterDatabase.escape_string(data); |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | CharacterDatabase.PExecute("INSERT INTO instance VALUES ('%u', '%u', '"I64FMTD"', '%u', '%s')", m_instanceid, GetMapId(), (uint64)GetResetTimeForDB(), GetDifficulty(), data.c_str()); |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | time_t InstanceSave::GetResetTimeForDB() |
---|
| 179 | { |
---|
| 180 | // only save the reset time for normal instances |
---|
| 181 | const MapEntry *entry = sMapStore.LookupEntry(GetMapId()); |
---|
| 182 | if(!entry || entry->map_type == MAP_RAID || GetDifficulty() == DIFFICULTY_HEROIC) |
---|
| 183 | return 0; |
---|
| 184 | else |
---|
| 185 | return GetResetTime(); |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | // to cache or not to cache, that is the question |
---|
| 189 | InstanceTemplate const* InstanceSave::GetTemplate() |
---|
| 190 | { |
---|
| 191 | return objmgr.GetInstanceTemplate(m_mapid); |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | MapEntry const* InstanceSave::GetMapEntry() |
---|
| 195 | { |
---|
| 196 | return sMapStore.LookupEntry(m_mapid); |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | void InstanceSave::DeleteFromDB() |
---|
| 200 | { |
---|
| 201 | InstanceSaveManager::DeleteInstanceFromDB(GetInstanceId()); |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | /* true if the instance save is still valid */ |
---|
| 205 | bool InstanceSave::UnloadIfEmpty() |
---|
| 206 | { |
---|
| 207 | if(m_playerList.empty() && m_groupList.empty()) |
---|
| 208 | { |
---|
| 209 | if(!sInstanceSaveManager.lock_instLists) |
---|
| 210 | sInstanceSaveManager.RemoveInstanceSave(GetInstanceId()); |
---|
| 211 | return false; |
---|
| 212 | } |
---|
| 213 | else |
---|
| 214 | return true; |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | void InstanceSaveManager::_DelHelper(DatabaseType &db, const char *fields, const char *table, const char *queryTail,...) |
---|
| 218 | { |
---|
| 219 | Tokens fieldTokens = StrSplit(fields, ", "); |
---|
| 220 | assert(fieldTokens.size() != 0); |
---|
| 221 | |
---|
| 222 | va_list ap; |
---|
| 223 | char szQueryTail [MAX_QUERY_LEN]; |
---|
| 224 | va_start(ap, queryTail); |
---|
| 225 | int res = vsnprintf( szQueryTail, MAX_QUERY_LEN, queryTail, ap ); |
---|
| 226 | va_end(ap); |
---|
| 227 | |
---|
| 228 | QueryResult *result = db.PQuery("SELECT %s FROM %s %s", fields, table, szQueryTail); |
---|
| 229 | if(result) |
---|
| 230 | { |
---|
| 231 | do |
---|
| 232 | { |
---|
| 233 | Field *fields = result->Fetch(); |
---|
| 234 | std::ostringstream ss; |
---|
| 235 | for(size_t i = 0; i < fieldTokens.size(); i++) |
---|
| 236 | { |
---|
| 237 | std::string fieldValue = fields[i].GetCppString(); |
---|
| 238 | db.escape_string(fieldValue); |
---|
| 239 | ss << (i != 0 ? " AND " : "") << fieldTokens[i] << " = '" << fieldValue << "'"; |
---|
| 240 | } |
---|
| 241 | db.DirectPExecute("DELETE FROM %s WHERE %s", table, ss.str().c_str()); |
---|
| 242 | } while (result->NextRow()); |
---|
| 243 | delete result; |
---|
| 244 | } |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | void InstanceSaveManager::CleanupInstances() |
---|
| 248 | { |
---|
| 249 | uint64 now = (uint64)time(NULL); |
---|
| 250 | |
---|
| 251 | barGoLink bar(2); |
---|
| 252 | bar.step(); |
---|
| 253 | |
---|
| 254 | // load reset times and clean expired instances |
---|
| 255 | sInstanceSaveManager.LoadResetTimes(); |
---|
| 256 | |
---|
| 257 | // clean character/group - instance binds with invalid group/characters |
---|
| 258 | _DelHelper(CharacterDatabase, "character_instance.guid, instance", "character_instance", "LEFT JOIN characters ON character_instance.guid = characters.guid WHERE characters.guid IS NULL"); |
---|
| 259 | _DelHelper(CharacterDatabase, "group_instance.leaderGuid, instance", "group_instance", "LEFT JOIN characters ON group_instance.leaderGuid = characters.guid LEFT JOIN groups ON group_instance.leaderGuid = groups.leaderGuid WHERE characters.guid IS NULL OR groups.leaderGuid IS NULL"); |
---|
| 260 | |
---|
| 261 | // clean instances that do not have any players or groups bound to them |
---|
| 262 | _DelHelper(CharacterDatabase, "id, map, difficulty", "instance", "LEFT JOIN character_instance ON character_instance.instance = id LEFT JOIN group_instance ON group_instance.instance = id WHERE character_instance.instance IS NULL AND group_instance.instance IS NULL"); |
---|
| 263 | |
---|
| 264 | // clean invalid instance references in other tables |
---|
| 265 | _DelHelper(CharacterDatabase, "character_instance.guid, instance", "character_instance", "LEFT JOIN instance ON character_instance.instance = instance.id WHERE instance.id IS NULL"); |
---|
| 266 | _DelHelper(CharacterDatabase, "group_instance.leaderGuid, instance", "group_instance", "LEFT JOIN instance ON group_instance.instance = instance.id WHERE instance.id IS NULL"); |
---|
| 267 | |
---|
| 268 | // creature_respawn and gameobject_respawn are in another database |
---|
| 269 | // first, obtain total instance set |
---|
| 270 | std::set< uint32 > InstanceSet; |
---|
| 271 | QueryResult *result = CharacterDatabase.PQuery("SELECT id FROM instance"); |
---|
| 272 | if( result ) |
---|
| 273 | { |
---|
| 274 | do |
---|
| 275 | { |
---|
| 276 | Field *fields = result->Fetch(); |
---|
| 277 | InstanceSet.insert(fields[0].GetUInt32()); |
---|
| 278 | } |
---|
| 279 | while (result->NextRow()); |
---|
| 280 | delete result; |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | // creature_respawn |
---|
| 284 | result = WorldDatabase.PQuery("SELECT DISTINCT(instance) FROM creature_respawn WHERE instance <> 0"); |
---|
| 285 | if( result ) |
---|
| 286 | { |
---|
| 287 | do |
---|
| 288 | { |
---|
| 289 | Field *fields = result->Fetch(); |
---|
| 290 | if(InstanceSet.find(fields[0].GetUInt32()) == InstanceSet.end()) |
---|
| 291 | WorldDatabase.DirectPExecute("DELETE FROM creature_respawn WHERE instance = '%u'", fields[0].GetUInt32()); |
---|
| 292 | } |
---|
| 293 | while (result->NextRow()); |
---|
| 294 | delete result; |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | // gameobject_respawn |
---|
| 298 | result = WorldDatabase.PQuery("SELECT DISTINCT(instance) FROM gameobject_respawn WHERE instance <> 0"); |
---|
| 299 | if( result ) |
---|
| 300 | { |
---|
| 301 | do |
---|
| 302 | { |
---|
| 303 | Field *fields = result->Fetch(); |
---|
| 304 | if(InstanceSet.find(fields[0].GetUInt32()) == InstanceSet.end()) |
---|
| 305 | WorldDatabase.DirectPExecute("DELETE FROM gameobject_respawn WHERE instance = '%u'", fields[0].GetUInt32()); |
---|
| 306 | } |
---|
| 307 | while (result->NextRow()); |
---|
| 308 | delete result; |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | bar.step(); |
---|
| 312 | sLog.outString(); |
---|
| 313 | sLog.outString( ">> Initialized %u instances", (uint32)InstanceSet.size()); |
---|
| 314 | } |
---|
| 315 | |
---|
| 316 | void InstanceSaveManager::PackInstances() |
---|
| 317 | { |
---|
| 318 | // this routine renumbers player instance associations in such a way so they start from 1 and go up |
---|
| 319 | // TODO: this can be done a LOT more efficiently |
---|
| 320 | |
---|
| 321 | // obtain set of all associations |
---|
| 322 | std::set< uint32 > InstanceSet; |
---|
| 323 | |
---|
| 324 | // all valid ids are in the instance table |
---|
| 325 | // any associations to ids not in this table are assumed to be |
---|
| 326 | // cleaned already in CleanupInstances |
---|
| 327 | QueryResult *result = CharacterDatabase.PQuery("SELECT id FROM instance"); |
---|
| 328 | if( result ) |
---|
| 329 | { |
---|
| 330 | do |
---|
| 331 | { |
---|
| 332 | Field *fields = result->Fetch(); |
---|
| 333 | InstanceSet.insert(fields[0].GetUInt32()); |
---|
| 334 | } |
---|
| 335 | while (result->NextRow()); |
---|
| 336 | delete result; |
---|
| 337 | } |
---|
| 338 | |
---|
| 339 | barGoLink bar( InstanceSet.size() + 1); |
---|
| 340 | bar.step(); |
---|
| 341 | |
---|
| 342 | uint32 InstanceNumber = 1; |
---|
| 343 | // we do assume std::set is sorted properly on integer value |
---|
| 344 | for (std::set< uint32 >::iterator i = InstanceSet.begin(); i != InstanceSet.end(); i++) |
---|
| 345 | { |
---|
| 346 | if (*i != InstanceNumber) |
---|
| 347 | { |
---|
| 348 | // remap instance id |
---|
| 349 | WorldDatabase.PExecute("UPDATE creature_respawn SET instance = '%u' WHERE instance = '%u'", InstanceNumber, *i); |
---|
| 350 | WorldDatabase.PExecute("UPDATE gameobject_respawn SET instance = '%u' WHERE instance = '%u'", InstanceNumber, *i); |
---|
| 351 | CharacterDatabase.PExecute("UPDATE corpse SET instance = '%u' WHERE instance = '%u'", InstanceNumber, *i); |
---|
| 352 | CharacterDatabase.PExecute("UPDATE character_instance SET instance = '%u' WHERE instance = '%u'", InstanceNumber, *i); |
---|
| 353 | CharacterDatabase.PExecute("UPDATE instance SET id = '%u' WHERE id = '%u'", InstanceNumber, *i); |
---|
| 354 | CharacterDatabase.PExecute("UPDATE group_instance SET instance = '%u' WHERE instance = '%u'", InstanceNumber, *i); |
---|
| 355 | } |
---|
| 356 | |
---|
| 357 | ++InstanceNumber; |
---|
| 358 | bar.step(); |
---|
| 359 | } |
---|
| 360 | |
---|
| 361 | sLog.outString(); |
---|
| 362 | sLog.outString( ">> Instance numbers remapped, next instance id is %u", InstanceNumber ); |
---|
| 363 | } |
---|
| 364 | |
---|
| 365 | void InstanceSaveManager::LoadResetTimes() |
---|
| 366 | { |
---|
| 367 | time_t now = time(NULL); |
---|
| 368 | time_t today = (now / DAY) * DAY; |
---|
| 369 | |
---|
| 370 | // NOTE: Use DirectPExecute for tables that will be queried later |
---|
| 371 | |
---|
| 372 | // get the current reset times for normal instances (these may need to be updated) |
---|
| 373 | // these are only kept in memory for InstanceSaves that are loaded later |
---|
| 374 | // resettime = 0 in the DB for raid/heroic instances so those are skipped |
---|
| 375 | typedef std::map<uint32, std::pair<uint32, uint64> > ResetTimeMapType; |
---|
| 376 | ResetTimeMapType InstResetTime; |
---|
| 377 | QueryResult *result = CharacterDatabase.PQuery("SELECT id, map, resettime FROM instance WHERE resettime > 0"); |
---|
| 378 | if( result ) |
---|
| 379 | { |
---|
| 380 | do |
---|
| 381 | { |
---|
| 382 | if(uint64 resettime = (*result)[2].GetUInt64()) |
---|
| 383 | { |
---|
| 384 | uint32 id = (*result)[0].GetUInt32(); |
---|
| 385 | uint32 mapid = (*result)[1].GetUInt32(); |
---|
| 386 | InstResetTime[id] = std::pair<uint32, uint64>(mapid, resettime); |
---|
| 387 | } |
---|
| 388 | } |
---|
| 389 | while (result->NextRow()); |
---|
| 390 | delete result; |
---|
| 391 | |
---|
| 392 | // update reset time for normal instances with the max creature respawn time + X hours |
---|
| 393 | result = WorldDatabase.PQuery("SELECT MAX(respawntime), instance FROM creature_respawn WHERE instance > 0 GROUP BY instance"); |
---|
| 394 | if( result ) |
---|
| 395 | { |
---|
| 396 | do |
---|
| 397 | { |
---|
| 398 | Field *fields = result->Fetch(); |
---|
| 399 | uint32 instance = fields[1].GetUInt32(); |
---|
| 400 | uint64 resettime = fields[0].GetUInt64() + 2 * HOUR; |
---|
| 401 | ResetTimeMapType::iterator itr = InstResetTime.find(instance); |
---|
| 402 | if(itr != InstResetTime.end() && itr->second.second != resettime) |
---|
| 403 | { |
---|
| 404 | CharacterDatabase.DirectPExecute("UPDATE instance SET resettime = '"I64FMTD"' WHERE id = '%u'", resettime, instance); |
---|
| 405 | itr->second.second = resettime; |
---|
| 406 | } |
---|
| 407 | } |
---|
| 408 | while (result->NextRow()); |
---|
| 409 | delete result; |
---|
| 410 | } |
---|
| 411 | |
---|
| 412 | // schedule the reset times |
---|
| 413 | for(ResetTimeMapType::iterator itr = InstResetTime.begin(); itr != InstResetTime.end(); ++itr) |
---|
| 414 | if(itr->second.second > now) |
---|
| 415 | ScheduleReset(true, itr->second.second, InstResetEvent(0, itr->second.first, itr->first)); |
---|
| 416 | } |
---|
| 417 | |
---|
| 418 | // load the global respawn times for raid/heroic instances |
---|
| 419 | uint32 diff = sWorld.getConfig(CONFIG_INSTANCE_RESET_TIME_HOUR) * HOUR; |
---|
| 420 | m_resetTimeByMapId.resize(sMapStore.GetNumRows()+1); |
---|
| 421 | result = CharacterDatabase.Query("SELECT mapid, resettime FROM instance_reset"); |
---|
| 422 | if(result) |
---|
| 423 | { |
---|
| 424 | do |
---|
| 425 | { |
---|
| 426 | Field *fields = result->Fetch(); |
---|
| 427 | uint32 mapid = fields[0].GetUInt32(); |
---|
| 428 | if(!objmgr.GetInstanceTemplate(mapid)) |
---|
| 429 | { |
---|
| 430 | sLog.outError("InstanceSaveManager::LoadResetTimes: invalid mapid %u in instance_reset!", mapid); |
---|
| 431 | CharacterDatabase.DirectPExecute("DELETE FROM instance_reset WHERE mapid = '%u'", mapid); |
---|
| 432 | continue; |
---|
| 433 | } |
---|
| 434 | |
---|
| 435 | // update the reset time if the hour in the configs changes |
---|
| 436 | uint64 oldresettime = fields[1].GetUInt64(); |
---|
| 437 | uint64 newresettime = (oldresettime / DAY) * DAY + diff; |
---|
| 438 | if(oldresettime != newresettime) |
---|
| 439 | CharacterDatabase.DirectPExecute("UPDATE instance_reset SET resettime = '"I64FMTD"' WHERE mapid = '%u'", newresettime, mapid); |
---|
| 440 | |
---|
| 441 | m_resetTimeByMapId[mapid] = newresettime; |
---|
| 442 | } while(result->NextRow()); |
---|
| 443 | delete result; |
---|
| 444 | } |
---|
| 445 | |
---|
| 446 | // clean expired instances, references to them will be deleted in CleanupInstances |
---|
| 447 | // must be done before calculating new reset times |
---|
| 448 | _DelHelper(CharacterDatabase, "id, map, difficulty", "instance", "LEFT JOIN instance_reset ON mapid = map WHERE (instance.resettime < '"I64FMTD"' AND instance.resettime > '0') OR (NOT instance_reset.resettime IS NULL AND instance_reset.resettime < '"I64FMTD"')", (uint64)now, (uint64)now); |
---|
| 449 | |
---|
| 450 | // calculate new global reset times for expired instances and those that have never been reset yet |
---|
| 451 | // add the global reset times to the priority queue |
---|
| 452 | for(uint32 i = 0; i < sInstanceTemplate.MaxEntry; i++) |
---|
| 453 | { |
---|
| 454 | InstanceTemplate* temp = (InstanceTemplate*)objmgr.GetInstanceTemplate(i); |
---|
| 455 | if(!temp) continue; |
---|
| 456 | // only raid/heroic maps have a global reset time |
---|
| 457 | const MapEntry* entry = sMapStore.LookupEntry(temp->map); |
---|
| 458 | if(!entry || !entry->HasResetTime()) |
---|
| 459 | continue; |
---|
| 460 | |
---|
| 461 | uint32 period = temp->reset_delay * DAY; |
---|
| 462 | assert(period != 0); |
---|
| 463 | time_t t = m_resetTimeByMapId[temp->map]; |
---|
| 464 | if(!t) |
---|
| 465 | { |
---|
| 466 | // initialize the reset time |
---|
| 467 | t = today + period + diff; |
---|
| 468 | CharacterDatabase.DirectPExecute("INSERT INTO instance_reset VALUES ('%u','"I64FMTD"')", i, (uint64)t); |
---|
| 469 | } |
---|
| 470 | |
---|
| 471 | if(t < now) |
---|
| 472 | { |
---|
| 473 | // assume that expired instances have already been cleaned |
---|
| 474 | // calculate the next reset time |
---|
| 475 | t = (t / DAY) * DAY; |
---|
| 476 | t += ((today - t) / period + 1) * period + diff; |
---|
| 477 | CharacterDatabase.DirectPExecute("UPDATE instance_reset SET resettime = '"I64FMTD"' WHERE mapid = '%u'", (uint64)t, i); |
---|
| 478 | } |
---|
| 479 | |
---|
| 480 | m_resetTimeByMapId[temp->map] = t; |
---|
| 481 | |
---|
| 482 | // schedule the global reset/warning |
---|
| 483 | uint8 type = 1; |
---|
| 484 | static int tim[4] = {3600, 900, 300, 60}; |
---|
| 485 | for(; type < 4; type++) |
---|
| 486 | if(t - tim[type-1] > now) break; |
---|
| 487 | ScheduleReset(true, t - tim[type-1], InstResetEvent(type, i)); |
---|
| 488 | } |
---|
| 489 | } |
---|
| 490 | |
---|
| 491 | void InstanceSaveManager::ScheduleReset(bool add, time_t time, InstResetEvent event) |
---|
| 492 | { |
---|
| 493 | if(add) m_resetTimeQueue.insert(std::pair<time_t, InstResetEvent>(time, event)); |
---|
| 494 | else |
---|
| 495 | { |
---|
| 496 | // find the event in the queue and remove it |
---|
| 497 | ResetTimeQueue::iterator itr; |
---|
| 498 | std::pair<ResetTimeQueue::iterator, ResetTimeQueue::iterator> range; |
---|
| 499 | range = m_resetTimeQueue.equal_range(time); |
---|
| 500 | for(itr = range.first; itr != range.second; ++itr) |
---|
| 501 | if(itr->second == event) { m_resetTimeQueue.erase(itr); return; } |
---|
| 502 | // in case the reset time changed (should happen very rarely), we search the whole queue |
---|
| 503 | if(itr == range.second) |
---|
| 504 | { |
---|
| 505 | for(itr = m_resetTimeQueue.begin(); itr != m_resetTimeQueue.end(); ++itr) |
---|
| 506 | if(itr->second == event) { m_resetTimeQueue.erase(itr); return; } |
---|
| 507 | if(itr == m_resetTimeQueue.end()) |
---|
| 508 | sLog.outError("InstanceSaveManager::ScheduleReset: cannot cancel the reset, the event(%d,%d,%d) was not found!", event.type, event.mapid, event.instanceId); |
---|
| 509 | } |
---|
| 510 | } |
---|
| 511 | } |
---|
| 512 | |
---|
| 513 | void InstanceSaveManager::Update() |
---|
| 514 | { |
---|
| 515 | time_t now = time(NULL), t; |
---|
| 516 | while(!m_resetTimeQueue.empty() && (t = m_resetTimeQueue.begin()->first) < now) |
---|
| 517 | { |
---|
| 518 | InstResetEvent &event = m_resetTimeQueue.begin()->second; |
---|
| 519 | if(event.type == 0) |
---|
| 520 | { |
---|
| 521 | // for individual normal instances, max creature respawn + X hours |
---|
| 522 | _ResetInstance(event.mapid, event.instanceId); |
---|
| 523 | m_resetTimeQueue.erase(m_resetTimeQueue.begin()); |
---|
| 524 | } |
---|
| 525 | else |
---|
| 526 | { |
---|
| 527 | // global reset/warning for a certain map |
---|
| 528 | time_t resetTime = GetResetTimeFor(event.mapid); |
---|
| 529 | _ResetOrWarnAll(event.mapid, event.type != 4, resetTime - now); |
---|
| 530 | if(event.type != 4) |
---|
| 531 | { |
---|
| 532 | // schedule the next warning/reset |
---|
| 533 | event.type++; |
---|
| 534 | static int tim[4] = {3600, 900, 300, 60}; |
---|
| 535 | ScheduleReset(true, resetTime - tim[event.type-1], event); |
---|
| 536 | } |
---|
| 537 | m_resetTimeQueue.erase(m_resetTimeQueue.begin()); |
---|
| 538 | } |
---|
| 539 | } |
---|
| 540 | } |
---|
| 541 | |
---|
| 542 | void InstanceSaveManager::_ResetSave(InstanceSaveHashMap::iterator &itr) |
---|
| 543 | { |
---|
| 544 | // unbind all players bound to the instance |
---|
| 545 | // do not allow UnbindInstance to automatically unload the InstanceSaves |
---|
| 546 | lock_instLists = true; |
---|
| 547 | InstanceSave::PlayerListType &pList = itr->second->m_playerList; |
---|
| 548 | while(!pList.empty()) |
---|
| 549 | { |
---|
| 550 | Player *player = *(pList.begin()); |
---|
| 551 | player->UnbindInstance(itr->second->GetMapId(), itr->second->GetDifficulty(), true); |
---|
| 552 | } |
---|
| 553 | InstanceSave::GroupListType &gList = itr->second->m_groupList; |
---|
| 554 | while(!gList.empty()) |
---|
| 555 | { |
---|
| 556 | Group *group = *(gList.begin()); |
---|
| 557 | group->UnbindInstance(itr->second->GetMapId(), itr->second->GetDifficulty(), true); |
---|
| 558 | } |
---|
| 559 | m_instanceSaveById.erase(itr++); |
---|
| 560 | lock_instLists = false; |
---|
| 561 | } |
---|
| 562 | |
---|
| 563 | void InstanceSaveManager::_ResetInstance(uint32 mapid, uint32 instanceId) |
---|
| 564 | { |
---|
| 565 | sLog.outDebug("InstanceSaveMgr::_ResetInstance %u, %u", mapid, instanceId); |
---|
| 566 | Map *map = (MapInstanced*)MapManager::Instance().GetBaseMap(mapid); |
---|
| 567 | if(!map->Instanceable()) |
---|
| 568 | return; |
---|
| 569 | |
---|
| 570 | InstanceSaveHashMap::iterator itr = m_instanceSaveById.find(instanceId); |
---|
| 571 | if(itr != m_instanceSaveById.end()) _ResetSave(itr); |
---|
| 572 | DeleteInstanceFromDB(instanceId); // even if save not loaded |
---|
| 573 | |
---|
| 574 | Map* iMap = ((MapInstanced*)map)->FindMap(instanceId); |
---|
| 575 | if(iMap && iMap->IsDungeon()) ((InstanceMap*)iMap)->Reset(INSTANCE_RESET_RESPAWN_DELAY); |
---|
| 576 | else objmgr.DeleteRespawnTimeForInstance(instanceId); // even if map is not loaded |
---|
| 577 | } |
---|
| 578 | |
---|
| 579 | void InstanceSaveManager::_ResetOrWarnAll(uint32 mapid, bool warn, uint32 timeLeft) |
---|
| 580 | { |
---|
| 581 | // global reset for all instances of the given map |
---|
| 582 | // note: this isn't fast but it's meant to be executed very rarely |
---|
| 583 | Map *map = (MapInstanced*)MapManager::Instance().GetBaseMap(mapid); |
---|
| 584 | if(!map->Instanceable()) |
---|
| 585 | return; |
---|
| 586 | uint64 now = (uint64)time(NULL); |
---|
| 587 | |
---|
| 588 | if(!warn) |
---|
| 589 | { |
---|
| 590 | // this is called one minute before the reset time |
---|
| 591 | InstanceTemplate* temp = (InstanceTemplate*)objmgr.GetInstanceTemplate(mapid); |
---|
| 592 | if(!temp || !temp->reset_delay) |
---|
| 593 | { |
---|
| 594 | sLog.outError("InstanceSaveManager::ResetOrWarnAll: no instance template or reset delay for map %d", mapid); |
---|
| 595 | return; |
---|
| 596 | } |
---|
| 597 | |
---|
| 598 | // remove all binds to instances of the given map |
---|
| 599 | for(InstanceSaveHashMap::iterator itr = m_instanceSaveById.begin(); itr != m_instanceSaveById.end();) |
---|
| 600 | { |
---|
| 601 | if(itr->second->GetMapId() == mapid) |
---|
| 602 | _ResetSave(itr); |
---|
| 603 | else |
---|
| 604 | ++itr; |
---|
| 605 | } |
---|
| 606 | |
---|
| 607 | // delete them from the DB, even if not loaded |
---|
| 608 | CharacterDatabase.BeginTransaction(); |
---|
| 609 | CharacterDatabase.PExecute("DELETE FROM character_instance USING character_instance LEFT JOIN instance ON character_instance.instance = id WHERE map = '%u'", mapid); |
---|
| 610 | CharacterDatabase.PExecute("DELETE FROM group_instance USING group_instance LEFT JOIN instance ON group_instance.instance = id WHERE map = '%u'", mapid); |
---|
| 611 | CharacterDatabase.PExecute("DELETE FROM instance WHERE map = '%u'", mapid); |
---|
| 612 | CharacterDatabase.CommitTransaction(); |
---|
| 613 | |
---|
| 614 | // calculate the next reset time |
---|
| 615 | uint32 diff = sWorld.getConfig(CONFIG_INSTANCE_RESET_TIME_HOUR) * HOUR; |
---|
| 616 | uint32 period = temp->reset_delay * DAY; |
---|
| 617 | uint64 next_reset = ((now + timeLeft + MINUTE) / DAY * DAY) + period + diff; |
---|
| 618 | // update it in the DB |
---|
| 619 | CharacterDatabase.PExecute("UPDATE instance_reset SET resettime = '"I64FMTD"' WHERE mapid = '%d'", next_reset, mapid); |
---|
| 620 | } |
---|
| 621 | |
---|
| 622 | MapInstanced::InstancedMaps &instMaps = ((MapInstanced*)map)->GetInstancedMaps(); |
---|
| 623 | MapInstanced::InstancedMaps::iterator mitr; |
---|
| 624 | for(mitr = instMaps.begin(); mitr != instMaps.end(); ++mitr) |
---|
| 625 | { |
---|
| 626 | Map *map = mitr->second; |
---|
| 627 | if(!map->IsDungeon()) continue; |
---|
| 628 | if(warn) ((InstanceMap*)map)->SendResetWarnings(timeLeft); |
---|
| 629 | else ((InstanceMap*)map)->Reset(INSTANCE_RESET_GLOBAL); |
---|
| 630 | } |
---|
| 631 | |
---|
| 632 | // TODO: delete creature/gameobject respawn times even if the maps are not loaded |
---|
| 633 | } |
---|
| 634 | |
---|
| 635 | uint32 InstanceSaveManager::GetNumBoundPlayersTotal() |
---|
| 636 | { |
---|
| 637 | uint32 ret = 0; |
---|
| 638 | for(InstanceSaveHashMap::iterator itr = m_instanceSaveById.begin(); itr != m_instanceSaveById.end(); ++itr) |
---|
| 639 | ret += itr->second->GetPlayerCount(); |
---|
| 640 | return ret; |
---|
| 641 | } |
---|
| 642 | |
---|
| 643 | uint32 InstanceSaveManager::GetNumBoundGroupsTotal() |
---|
| 644 | { |
---|
| 645 | uint32 ret = 0; |
---|
| 646 | for(InstanceSaveHashMap::iterator itr = m_instanceSaveById.begin(); itr != m_instanceSaveById.end(); ++itr) |
---|
| 647 | ret += itr->second->GetGroupCount(); |
---|
| 648 | return ret; |
---|
| 649 | } |
---|