TrinityCore
cs_instance.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18/* ScriptData
19Name: instance_commandscript
20%Complete: 100
21Comment: All instance related commands
22Category: commandscripts
23EndScriptData */
24
25#include "ScriptMgr.h"
26#include "Chat.h"
27#include "ChatCommand.h"
28#include "DB2Stores.h"
29#include "GameTime.h"
30#include "Group.h"
31#include "InstanceLockMgr.h"
32#include "InstanceScript.h"
33#include "Language.h"
34#include "Map.h"
35#include "MapManager.h"
36#include "Player.h"
37#include "RBAC.h"
38#include "Util.h"
39#include "World.h"
40#include "WorldSession.h"
41#include <sstream>
42
43using namespace Trinity::ChatCommands;
44
46{
47public:
48 instance_commandscript() : CommandScript("instance_commandscript") { }
49
51 {
52 static ChatCommandTable instanceCommandTable =
53 {
59 };
60
61 static ChatCommandTable commandTable =
62 {
63 { "instance", instanceCommandTable },
64 };
65
66 return commandTable;
67 }
68
70 {
71 Player* player = handler->getSelectedPlayer();
72 if (!player)
73 player = handler->GetSession()->GetPlayer();
74
75 InstanceResetTimePoint now = GameTime::GetTime<InstanceResetTimePoint::clock>();
76 std::vector<InstanceLock const*> instanceLocks = sInstanceLockMgr.GetInstanceLocksForPlayer(player->GetGUID());
77 for (InstanceLock const* instanceLock : instanceLocks)
78 {
79 MapDb2Entries entries{ instanceLock->GetMapId(), instanceLock->GetDifficultyId() };
80 std::string timeleft = !instanceLock->IsExpired() ? secsToTimeString(std::chrono::duration_cast<Seconds>(instanceLock->GetEffectiveExpiryTime() - now).count()) : "-";
82 entries.Map->ID, entries.Map->MapName[sWorld->GetDefaultDbcLocale()],
83 uint32(entries.MapDifficulty->DifficultyID), sDifficultyStore.AssertEntry(entries.MapDifficulty->DifficultyID)->Name[sWorld->GetDefaultDbcLocale()],
84 instanceLock->GetInstanceId(),
85 handler->GetTrinityString(instanceLock->IsExpired() ? LANG_YES : LANG_NO),
86 handler->GetTrinityString(instanceLock->IsExtended() ? LANG_YES : LANG_NO),
87 timeleft.c_str());
88 }
89
90 handler->PSendSysMessage(LANG_COMMAND_LIST_BIND_PLAYER_BINDS, uint32(instanceLocks.size()));
91 return true;
92 }
93
94 static bool HandleInstanceUnbindCommand(ChatHandler* handler, Variant<uint32, EXACT_SEQUENCE("all")> mapArg, Optional<uint32> difficultyArg)
95 {
96 Player* player = handler->getSelectedPlayer();
97 if (!player)
98 player = handler->GetSession()->GetPlayer();
99
100 Optional<uint32> mapId;
101 Optional<Difficulty> difficulty;
102
103 if (mapArg.holds_alternative<uint32>())
104 mapId = mapArg.get<uint32>();
105
106 if (difficultyArg && sDifficultyStore.LookupEntry(*difficultyArg))
107 difficulty = Difficulty(*difficultyArg);
108
109 std::vector<InstanceLock const*> locksReset;
110 std::vector<InstanceLock const*> locksNotReset;
111
112 sInstanceLockMgr.ResetInstanceLocksForPlayer(player->GetGUID(), mapId, difficulty, &locksReset, &locksNotReset);
113
114 InstanceResetTimePoint now = GameTime::GetTime<InstanceResetTimePoint::clock>();
115 for (InstanceLock const* instanceLock : locksReset)
116 {
117 MapDb2Entries entries{ instanceLock->GetMapId(), instanceLock->GetDifficultyId() };
118 std::string timeleft = !instanceLock->IsExpired() ? secsToTimeString(std::chrono::duration_cast<Seconds>(instanceLock->GetEffectiveExpiryTime() - now).count()) : "-";
120 entries.Map->ID, entries.Map->MapName[sWorld->GetDefaultDbcLocale()],
121 uint32(entries.MapDifficulty->DifficultyID), sDifficultyStore.AssertEntry(entries.MapDifficulty->DifficultyID)->Name[sWorld->GetDefaultDbcLocale()],
122 instanceLock->GetInstanceId(),
123 handler->GetTrinityString(instanceLock->IsExpired() ? LANG_YES : LANG_NO),
124 handler->GetTrinityString(instanceLock->IsExtended() ? LANG_YES : LANG_NO),
125 timeleft.c_str());
126 }
127
128 handler->PSendSysMessage(LANG_COMMAND_INST_UNBIND_UNBOUND, uint32(locksReset.size()));
129
130 for (InstanceLock const* instanceLock : locksNotReset)
131 {
132 MapDb2Entries entries{ instanceLock->GetMapId(), instanceLock->GetDifficultyId() };
133 std::string timeleft = !instanceLock->IsExpired() ? secsToTimeString(std::chrono::duration_cast<Seconds>(instanceLock->GetEffectiveExpiryTime() - now).count()) : "-";
135 entries.Map->ID, entries.Map->MapName[sWorld->GetDefaultDbcLocale()],
136 uint32(entries.MapDifficulty->DifficultyID), sDifficultyStore.AssertEntry(entries.MapDifficulty->DifficultyID)->Name[sWorld->GetDefaultDbcLocale()],
137 instanceLock->GetInstanceId(),
138 handler->GetTrinityString(instanceLock->IsExpired() ? LANG_YES : LANG_NO),
139 handler->GetTrinityString(instanceLock->IsExtended() ? LANG_YES : LANG_NO),
140 timeleft.c_str());
141 }
142
143 player->SendRaidInfo();
144
145 return true;
146 }
147
149 {
150 handler->PSendSysMessage(LANG_COMMAND_INST_STAT_LOADED_INST, sMapMgr->GetNumInstances());
151 handler->PSendSysMessage(LANG_COMMAND_INST_STAT_PLAYERS_IN, sMapMgr->GetNumPlayersInInstances());
152
153 InstanceLocksStatistics statistics = sInstanceLockMgr.GetStatistics();
154
157
158 return true;
159 }
160
162 {
163 // Character name must be provided when using this from console.
164 if (!player && !handler->GetSession())
165 {
167 handler->SetSentErrorMessage(true);
168 return false;
169 }
170
171 if (!player)
172 player = PlayerIdentifier::FromSelf(handler);
173
174 if (!player->IsConnected())
175 {
177 handler->SetSentErrorMessage(true);
178 return false;
179 }
180
181 InstanceMap* map = player->GetConnectedPlayer()->GetMap()->ToInstanceMap();
182 if (!map)
183 {
185 handler->SetSentErrorMessage(true);
186 return false;
187 }
188
189 if (!map->GetInstanceScript())
190 {
192 handler->SetSentErrorMessage(true);
193 return false;
194 }
195
196 // Reject improper values.
197 if (encounterId > map->GetInstanceScript()->GetEncounterCount())
198 {
200 handler->SetSentErrorMessage(true);
201 return false;
202 }
203
204 map->GetInstanceScript()->SetBossState(encounterId, state);
205 handler->PSendSysMessage(LANG_COMMAND_INST_SET_BOSS_STATE, encounterId, state, EnumUtils::ToConstant(state));
206 return true;
207 }
208
210 {
211 // Character name must be provided when using this from console.
212 if (!player && !handler->GetSession())
213 {
215 handler->SetSentErrorMessage(true);
216 return false;
217 }
218
219 if (!player)
220 player = PlayerIdentifier::FromSelf(handler);
221
222 if (!player->IsConnected())
223 {
225 handler->SetSentErrorMessage(true);
226 return false;
227 }
228
229 InstanceMap* map = player->GetConnectedPlayer()->GetMap()->ToInstanceMap();
230 if (!map)
231 {
233 handler->SetSentErrorMessage(true);
234 return false;
235 }
236
237 if (!map->GetInstanceScript())
238 {
240 handler->SetSentErrorMessage(true);
241 return false;
242 }
243
244 if (encounterId > map->GetInstanceScript()->GetEncounterCount())
245 {
247 handler->SetSentErrorMessage(true);
248 return false;
249 }
250
251 EncounterState state = map->GetInstanceScript()->GetBossState(encounterId);
252 handler->PSendSysMessage(LANG_COMMAND_INST_GET_BOSS_STATE, encounterId, state, EnumUtils::ToConstant(state));
253 return true;
254 }
255};
256
258{
260}
#define EXACT_SEQUENCE(str)
DB2Storage< DifficultyEntry > sDifficultyStore("Difficulty.db2", &DifficultyLoadInfo::Instance)
Difficulty
Definition: DBCEnums.h:873
uint32_t uint32
Definition: Define.h:142
std::chrono::system_clock::time_point InstanceResetTimePoint
#define sInstanceLockMgr
EncounterState
@ LANG_YES
Definition: Language.h:479
@ LANG_COMMAND_INST_SET_BOSS_STATE
Definition: Language.h:1115
@ LANG_COMMAND_INST_GET_BOSS_STATE
Definition: Language.h:1116
@ LANG_COMMAND_LIST_BIND_PLAYER_BINDS
Definition: Language.h:1104
@ LANG_COMMAND_INST_STAT_SAVES
Definition: Language.h:1110
@ LANG_NOT_DUNGEON
Definition: Language.h:1113
@ LANG_COMMAND_LIST_BIND_INFO
Definition: Language.h:1103
@ LANG_NO_INSTANCE_DATA
Definition: Language.h:1114
@ LANG_CMD_SYNTAX
Definition: Language.h:42
@ LANG_NO
Definition: Language.h:480
@ LANG_COMMAND_INST_STAT_PLAYERSBOUND
Definition: Language.h:1111
@ LANG_COMMAND_INST_UNBIND_UNBOUND
Definition: Language.h:1107
@ LANG_PLAYER_NOT_FOUND
Definition: Language.h:567
@ LANG_COMMAND_INST_STAT_PLAYERS_IN
Definition: Language.h:1109
@ LANG_COMMAND_INST_UNBIND_FAILED
Definition: Language.h:1105
@ LANG_COMMAND_INST_UNBIND_UNBINDING
Definition: Language.h:1106
@ LANG_BAD_VALUE
Definition: Language.h:149
@ LANG_COMMAND_INST_STAT_LOADED_INST
Definition: Language.h:1108
#define sMapMgr
Definition: MapManager.h:184
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition: Optional.h:25
Role Based Access Control related classes definition.
std::string secsToTimeString(uint64 timeInSecs, TimeFormat timeFormat, bool hoursOnly)
Definition: Util.cpp:115
Player * getSelectedPlayer()
Definition: Chat.cpp:200
WorldSession * GetSession()
Definition: Chat.h:42
void PSendSysMessage(const char *fmt, Args &&... args)
Definition: Chat.h:57
void SetSentErrorMessage(bool val)
Definition: Chat.h:114
virtual char const * GetTrinityString(uint32 entry) const
Definition: Chat.cpp:48
static char const * ToConstant(Enum value)
Definition: SmartEnum.h:120
InstanceScript * GetInstanceScript()
Definition: Map.h:870
virtual bool SetBossState(uint32 id, EncounterState state)
EncounterState GetBossState(uint32 id) const
uint32 GetEncounterCount() const
InstanceMap * ToInstanceMap()
Definition: Map.h:454
static ObjectGuid GetGUID(Object const *o)
Definition: Object.h:159
void SendRaidInfo()
Definition: Player.cpp:19688
Player * GetPlayer() const
static bool HandleInstanceUnbindCommand(ChatHandler *handler, Variant< uint32, EXACT_SEQUENCE("all")> mapArg, Optional< uint32 > difficultyArg)
Definition: cs_instance.cpp:94
ChatCommandTable GetCommands() const override
Definition: cs_instance.cpp:50
static bool HandleInstanceGetBossStateCommand(ChatHandler *handler, uint32 encounterId, Optional< PlayerIdentifier > player)
static bool HandleInstanceSetBossStateCommand(ChatHandler *handler, uint32 encounterId, EncounterState state, Optional< PlayerIdentifier > player)
static bool HandleInstanceStatsCommand(ChatHandler *handler)
static bool HandleInstanceListBindsCommand(ChatHandler *handler)
Definition: cs_instance.cpp:69
void AddSC_instance_commandscript()
#define sWorld
Definition: World.h:931
std::vector< ChatCommandBuilder > ChatCommandTable
Definition: ChatCommand.h:49
@ RBAC_PERM_COMMAND_INSTANCE_LISTBINDS
Definition: RBAC.h:285
@ RBAC_PERM_COMMAND_INSTANCE_SET_BOSS_STATE
Definition: RBAC.h:667
@ RBAC_PERM_COMMAND_INSTANCE_GET_BOSS_STATE
Definition: RBAC.h:668
@ RBAC_PERM_COMMAND_INSTANCE_STATS
Definition: RBAC.h:287
@ RBAC_PERM_COMMAND_INSTANCE_UNBIND
Definition: RBAC.h:286