1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef _INSTANCESAVEMGR_H
#define _INSTANCESAVEMGR_H
#include <list>
#include <map>
#include <mutex>
#include <unordered_map>
#include "Define.h"
#include "DatabaseEnvFwd.h"
#include "DBCEnums.h"
#include "ObjectDefines.h"
struct InstanceTemplate;
struct MapEntry;
class Player;
class Group;
/*
Holds the information necessary for creating a new map for an existing instance
Is referenced in three cases:
- player-instance binds for solo players (not in group)
- player-instance binds for permanent heroic/raid saves
- group-instance binds (both solo and permanent) cache the player binds for the group leader
*/
class TC_GAME_API InstanceSave
{
friend class InstanceSaveManager;
public:
/* Created either when:
- any new instance is being generated
- the first time a player bound to InstanceId logs in
- when a group bound to the instance is loaded */
InstanceSave(uint16 MapId, uint32 InstanceId, Difficulty difficulty, uint32 entranceId, time_t resetTime, bool canReset);
/* Unloaded when m_playerList and m_groupList become empty
or when the instance is reset */
~InstanceSave();
InstanceSave(InstanceSave const& right) = delete;
InstanceSave(InstanceSave&& right) = delete;
InstanceSave& operator=(InstanceSave const& right) = delete;
InstanceSave& operator=(InstanceSave&& right) = delete;
uint32 GetPlayerCount() const { return uint32(m_playerList.size()); }
uint32 GetGroupCount() const { return uint32(m_groupList.size()); }
/* A map corresponding to the InstanceId/MapId does not always exist.
InstanceSave objects may be created on player logon but the maps are
created and loaded only when a player actually enters the instance. */
uint32 GetInstanceId() const { return m_instanceid; }
uint32 GetMapId() const { return m_mapid; }
/* Saved when the instance is generated for the first time */
void SaveToDB();
/* When the instance is being reset (permanently deleted) */
void DeleteFromDB();
/* for normal instances this corresponds to max(creature respawn time) + X hours
for raid/heroic instances this caches the global respawn time for the map */
time_t GetResetTime() const { return m_resetTime; }
void SetResetTime(time_t resetTime) { m_resetTime = resetTime; }
time_t GetResetTimeForDB();
uint32 GetEntranceLocation() const { return m_entranceId; }
void SetEntranceLocation(uint32 entranceId) { m_entranceId = entranceId; }
InstanceTemplate const* GetTemplate();
MapEntry const* GetMapEntry();
/* online players bound to the instance (perm/solo)
does not include the members of the group unless they have permanent saves */
void AddPlayer(Player* player)
{
std::lock_guard<std::mutex> lock(_playerListLock);
m_playerList.push_back(player);
}
bool RemovePlayer(Player* player)
{
_playerListLock.lock();
m_playerList.remove(player);
bool isStillValid = UnloadIfEmpty();
_playerListLock.unlock();
//delete here if needed, after releasing the lock
if (m_toDelete)
delete this;
return isStillValid;
}
/* all groups bound to the instance */
void AddGroup(Group* group) { m_groupList.push_back(group); }
bool RemoveGroup(Group* group)
{
m_groupList.remove(group);
bool isStillValid = UnloadIfEmpty();
if (m_toDelete)
delete this;
return isStillValid;
}
/* instances cannot be reset (except at the global reset time)
if there are players permanently bound to it
this is cached for the case when those players are offline */
bool CanReset() const { return m_canReset; }
void SetCanReset(bool canReset) { m_canReset = canReset; }
/* currently it is possible to omit this information from this structure
but that would depend on a lot of things that can easily change in future */
Difficulty GetDifficultyID() const { return m_difficulty; }
/* used to flag the InstanceSave as to be deleted, so the caller can delete it */
void SetToDelete(bool toDelete)
{
m_toDelete = toDelete;
}
typedef std::list<Player*> PlayerListType;
typedef std::list<Group*> GroupListType;
private:
bool UnloadIfEmpty();
/* the only reason the instSave-object links are kept is because
the object-instSave links need to be broken at reset time */
/// @todo: Check if maybe it's enough to just store the number of players/groups
PlayerListType m_playerList;
GroupListType m_groupList;
time_t m_resetTime;
uint32 m_instanceid;
uint32 m_mapid;
Difficulty m_difficulty;
uint32 m_entranceId;
bool m_canReset;
bool m_toDelete;
std::mutex _playerListLock;
};
typedef std::unordered_map<uint64 /*PAIR64(map, difficulty)*/, time_t /*resetTime*/> ResetTimeByMapDifficultyMap;
class TC_GAME_API InstanceSaveManager
{
friend class InstanceSave;
private:
InstanceSaveManager() : lock_instLists(false) { };
~InstanceSaveManager();
public:
typedef std::unordered_map<uint32 /*InstanceId*/, InstanceSave*> InstanceSaveHashMap;
static InstanceSaveManager* instance();
void Unload();
/* resetTime is a global propery of each (raid/heroic) map
all instances of that map reset at the same time */
struct InstResetEvent
{
uint8 type;
Difficulty difficulty:8;
uint32 mapid;
uint32 instanceId;
InstResetEvent() : type(0), difficulty(DIFFICULTY_NORMAL), mapid(0), instanceId(0) { }
InstResetEvent(uint8 t, uint32 _mapid, Difficulty d, uint32 _instanceid)
: type(t), difficulty(d), mapid(_mapid), instanceId(_instanceid) { }
bool operator==(InstResetEvent const& e) const { return e.instanceId == instanceId; }
};
typedef std::multimap<time_t /*resetTime*/, InstResetEvent> ResetTimeQueue;
void LoadInstances();
void LoadResetTimes();
time_t GetResetTimeFor(uint32 mapid, Difficulty d) const
{
ResetTimeByMapDifficultyMap::const_iterator itr = m_resetTimeByMapDifficulty.find(MAKE_PAIR64(mapid, d));
return itr != m_resetTimeByMapDifficulty.end() ? itr->second : 0;
}
time_t GetSubsequentResetTime(uint32 mapid, Difficulty difficulty, time_t resetTime) const;
// Use this on startup when initializing reset times
void InitializeResetTimeFor(uint32 mapid, Difficulty d, time_t t)
{
m_resetTimeByMapDifficulty[MAKE_PAIR64(mapid, d)] = t;
}
// Use this only when updating existing reset times
void SetResetTimeFor(uint32 mapid, Difficulty d, time_t t);
ResetTimeByMapDifficultyMap const& GetResetTimeMap() const
{
return m_resetTimeByMapDifficulty;
}
void ScheduleReset(bool add, time_t time, InstResetEvent event);
void ForceGlobalReset(uint32 mapId, Difficulty difficulty);
void Update();
InstanceSave* AddInstanceSave(uint32 mapId, uint32 instanceId, Difficulty difficulty, time_t resetTime, uint32 entranceId,
bool canReset, bool load = false);
void RemoveInstanceSave(uint32 InstanceId);
void UnloadInstanceSave(uint32 InstanceId);
static void DeleteInstanceFromDB(uint32 instanceid);
InstanceSave* GetInstanceSave(uint32 InstanceId);
/* statistics */
uint32 GetNumInstanceSaves() const { return uint32(m_instanceSaveById.size()); }
uint32 GetNumBoundPlayersTotal() const;
uint32 GetNumBoundGroupsTotal() const;
protected:
static uint16 ResetTimeDelay[];
private:
void _ResetOrWarnAll(uint32 mapid, Difficulty difficulty, bool warn, time_t resetTime);
void _ResetInstance(uint32 mapid, uint32 instanceId);
void _ResetSave(InstanceSaveHashMap::iterator &itr);
// used during global instance resets
bool lock_instLists;
// fast lookup by instance id
InstanceSaveHashMap m_instanceSaveById;
// fast lookup for reset times (always use existed functions for access/set)
ResetTimeByMapDifficultyMap m_resetTimeByMapDifficulty;
ResetTimeQueue m_resetTimeQueue;
};
#define sInstanceSaveMgr InstanceSaveManager::instance()
#endif
|