TrinityCore
Loading...
Searching...
No Matches
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
42using namespace Trinity::ChatCommands;
43
45{
46public:
47 instance_commandscript() : CommandScript("instance_commandscript") { }
48
49 std::span<ChatCommandBuilder const> GetCommands() const override
50 {
51 static ChatCommandTable instanceCommandTable =
52 {
58 };
59
60 static ChatCommandTable commandTable =
61 {
62 { "instance", instanceCommandTable },
63 };
64
65 return commandTable;
66 }
67
69 {
70 Player* player = handler->getSelectedPlayer();
71 if (!player)
72 player = handler->GetSession()->GetPlayer();
73
74 InstanceResetTimePoint now = GameTime::GetTime<InstanceResetTimePoint::clock>();
75 std::vector<InstanceLock const*> instanceLocks = sInstanceLockMgr.GetInstanceLocksForPlayer(player->GetGUID());
76 for (InstanceLock const* instanceLock : instanceLocks)
77 {
78 MapDb2Entries entries{ instanceLock->GetMapId(), instanceLock->GetDifficultyId() };
79 std::string timeleft = !instanceLock->IsExpired() ? secsToTimeString(std::chrono::duration_cast<Seconds>(instanceLock->GetEffectiveExpiryTime() - now).count()) : "-";
81 entries.Map->ID, entries.Map->MapName[sWorld->GetDefaultDbcLocale()],
82 uint32(entries.MapDifficulty->DifficultyID), DB2Manager::GetDifficultyName(entries.MapDifficulty->GetDifficultyID()),
83 instanceLock->GetInstanceId(),
84 handler->GetTrinityString(instanceLock->IsExpired() ? LANG_YES : LANG_NO),
85 handler->GetTrinityString(instanceLock->IsExtended() ? LANG_YES : LANG_NO),
86 timeleft.c_str());
87 }
88
89 handler->PSendSysMessage(LANG_COMMAND_LIST_BIND_PLAYER_BINDS, uint32(instanceLocks.size()));
90 return true;
91 }
92
93 static bool HandleInstanceUnbindCommand(ChatHandler* handler, Variant<uint32, EXACT_SEQUENCE("all")> mapArg, Optional<uint32> difficultyArg)
94 {
95 Player* player = handler->getSelectedPlayer();
96 if (!player)
97 player = handler->GetSession()->GetPlayer();
98
99 Optional<uint32> mapId;
100 Optional<Difficulty> difficulty;
101
102 if (mapArg.holds_alternative<uint32>())
103 mapId = mapArg.get<uint32>();
104
105 if (difficultyArg && sDifficultyStore.LookupEntry(*difficultyArg))
106 difficulty = Difficulty(*difficultyArg);
107
108 std::vector<InstanceLock const*> locksReset;
109 std::vector<InstanceLock const*> locksNotReset;
110
111 sInstanceLockMgr.ResetInstanceLocksForPlayer(player->GetGUID(), mapId, difficulty, &locksReset, &locksNotReset);
112
113 InstanceResetTimePoint now = GameTime::GetTime<InstanceResetTimePoint::clock>();
114 for (InstanceLock const* instanceLock : locksReset)
115 {
116 MapDb2Entries entries{ instanceLock->GetMapId(), instanceLock->GetDifficultyId() };
117 std::string timeleft = !instanceLock->IsExpired() ? secsToTimeString(std::chrono::duration_cast<Seconds>(instanceLock->GetEffectiveExpiryTime() - now).count()) : "-";
119 entries.Map->ID, entries.Map->MapName[sWorld->GetDefaultDbcLocale()],
120 uint32(entries.MapDifficulty->DifficultyID), DB2Manager::GetDifficultyName(entries.MapDifficulty->GetDifficultyID()),
121 instanceLock->GetInstanceId(),
122 handler->GetTrinityString(instanceLock->IsExpired() ? LANG_YES : LANG_NO),
123 handler->GetTrinityString(instanceLock->IsExtended() ? LANG_YES : LANG_NO),
124 timeleft.c_str());
125 }
126
127 handler->PSendSysMessage(LANG_COMMAND_INST_UNBIND_UNBOUND, uint32(locksReset.size()));
128
129 for (InstanceLock const* instanceLock : locksNotReset)
130 {
131 MapDb2Entries entries{ instanceLock->GetMapId(), instanceLock->GetDifficultyId() };
132 std::string timeleft = !instanceLock->IsExpired() ? secsToTimeString(std::chrono::duration_cast<Seconds>(instanceLock->GetEffectiveExpiryTime() - now).count()) : "-";
134 entries.Map->ID, entries.Map->MapName[sWorld->GetDefaultDbcLocale()],
135 uint32(entries.MapDifficulty->DifficultyID), DB2Manager::GetDifficultyName(entries.MapDifficulty->GetDifficultyID()),
136 instanceLock->GetInstanceId(),
137 handler->GetTrinityString(instanceLock->IsExpired() ? LANG_YES : LANG_NO),
138 handler->GetTrinityString(instanceLock->IsExtended() ? LANG_YES : LANG_NO),
139 timeleft.c_str());
140 }
141
142 player->SendRaidInfo();
143
144 return true;
145 }
146
148 {
149 handler->PSendSysMessage(LANG_COMMAND_INST_STAT_LOADED_INST, sMapMgr->GetNumInstances());
150 handler->PSendSysMessage(LANG_COMMAND_INST_STAT_PLAYERS_IN, sMapMgr->GetNumPlayersInInstances());
151
152 InstanceLocksStatistics statistics = sInstanceLockMgr.GetStatistics();
153
156
157 return true;
158 }
159
161 {
162 // Character name must be provided when using this from console.
163 if (!player && !handler->GetSession())
164 {
166 handler->SetSentErrorMessage(true);
167 return false;
168 }
169
170 if (!player)
171 player = PlayerIdentifier::FromSelf(handler);
172
173 if (!player->IsConnected())
174 {
176 handler->SetSentErrorMessage(true);
177 return false;
178 }
179
180 InstanceMap* map = player->GetConnectedPlayer()->GetMap()->ToInstanceMap();
181 if (!map)
182 {
184 handler->SetSentErrorMessage(true);
185 return false;
186 }
187
188 if (!map->GetInstanceScript())
189 {
191 handler->SetSentErrorMessage(true);
192 return false;
193 }
194
195 // Reject improper values.
196 if (encounterId > map->GetInstanceScript()->GetEncounterCount())
197 {
199 handler->SetSentErrorMessage(true);
200 return false;
201 }
202
203 map->GetInstanceScript()->SetBossState(encounterId, state);
204 handler->PSendSysMessage(LANG_COMMAND_INST_SET_BOSS_STATE, encounterId, state, EnumUtils::ToConstant(state));
205 return true;
206 }
207
209 {
210 // Character name must be provided when using this from console.
211 if (!player && !handler->GetSession())
212 {
214 handler->SetSentErrorMessage(true);
215 return false;
216 }
217
218 if (!player)
219 player = PlayerIdentifier::FromSelf(handler);
220
221 if (!player->IsConnected())
222 {
224 handler->SetSentErrorMessage(true);
225 return false;
226 }
227
228 InstanceMap* map = player->GetConnectedPlayer()->GetMap()->ToInstanceMap();
229 if (!map)
230 {
232 handler->SetSentErrorMessage(true);
233 return false;
234 }
235
236 if (!map->GetInstanceScript())
237 {
239 handler->SetSentErrorMessage(true);
240 return false;
241 }
242
243 if (encounterId > map->GetInstanceScript()->GetEncounterCount())
244 {
246 handler->SetSentErrorMessage(true);
247 return false;
248 }
249
250 EncounterState state = map->GetInstanceScript()->GetBossState(encounterId);
251 handler->PSendSysMessage(LANG_COMMAND_INST_GET_BOSS_STATE, encounterId, state, EnumUtils::ToConstant(state));
252 return true;
253 }
254};
255
#define EXACT_SEQUENCE(str)
DB2Storage< DifficultyEntry > sDifficultyStore("Difficulty.db2", &DifficultyLoadInfo::Instance)
Difficulty
Definition DBCEnums.h:932
uint32_t uint32
Definition Define.h:154
std::chrono::system_clock::time_point InstanceResetTimePoint
#define sInstanceLockMgr
EncounterState
@ LANG_YES
Definition Language.h:482
@ LANG_COMMAND_INST_SET_BOSS_STATE
Definition Language.h:1124
@ LANG_COMMAND_INST_GET_BOSS_STATE
Definition Language.h:1125
@ LANG_COMMAND_LIST_BIND_PLAYER_BINDS
Definition Language.h:1113
@ LANG_COMMAND_INST_STAT_SAVES
Definition Language.h:1119
@ LANG_NOT_DUNGEON
Definition Language.h:1122
@ LANG_COMMAND_LIST_BIND_INFO
Definition Language.h:1112
@ LANG_NO_INSTANCE_DATA
Definition Language.h:1123
@ LANG_CMD_SYNTAX
Definition Language.h:42
@ LANG_NO
Definition Language.h:483
@ LANG_COMMAND_INST_STAT_PLAYERSBOUND
Definition Language.h:1120
@ LANG_COMMAND_INST_UNBIND_UNBOUND
Definition Language.h:1116
@ LANG_PLAYER_NOT_FOUND
Definition Language.h:570
@ LANG_COMMAND_INST_STAT_PLAYERS_IN
Definition Language.h:1118
@ LANG_COMMAND_INST_UNBIND_FAILED
Definition Language.h:1114
@ LANG_COMMAND_INST_UNBIND_UNBINDING
Definition Language.h:1115
@ LANG_BAD_VALUE
Definition Language.h:149
@ LANG_COMMAND_INST_STAT_LOADED_INST
Definition Language.h:1117
#define sMapMgr
Definition MapManager.h:186
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:116
ObjectGuid const & GetGUID() const
Definition BaseEntity.h:163
Player * getSelectedPlayer()
Definition Chat.cpp:204
WorldSession * GetSession()
Definition Chat.h:42
void SetSentErrorMessage(bool val)
Definition Chat.h:127
void PSendSysMessage(char const *fmt, Args &&... args)
Definition Chat.h:62
virtual char const * GetTrinityString(uint32 entry) const
Definition Chat.cpp:46
static std::string_view GetDifficultyName(Difficulty difficulty)
static char const * ToConstant(Enum value)
Definition SmartEnum.h:121
InstanceScript * GetInstanceScript()
Definition Map.h:890
virtual bool SetBossState(uint32 id, EncounterState state)
EncounterState GetBossState(uint32 id) const
uint32 GetEncounterCount() const
InstanceMap * ToInstanceMap()
Definition Map.h:490
void SendRaidInfo()
Definition Player.cpp:20047
Player * GetPlayer() const
static bool HandleInstanceUnbindCommand(ChatHandler *handler, Variant< uint32, EXACT_SEQUENCE("all")> mapArg, Optional< uint32 > difficultyArg)
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)
std::span< ChatCommandBuilder const > GetCommands() const override
void AddSC_instance_commandscript()
#define sWorld
Definition World.h:916
ChatCommandBuilder const [] 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
static Optional< PlayerIdentifier > FromSelf(ChatHandler *handler)