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 "GameTime.h"
28#include "Group.h"
29#include "InstanceSaveMgr.h"
30#include "InstanceScript.h"
31#include "Language.h"
32#include "MapManager.h"
33#include "ObjectAccessor.h"
34#include "ObjectMgr.h"
35#include "Player.h"
36#include "RBAC.h"
37#include "WorldSession.h"
38
39using namespace Trinity::ChatCommands;
40
42{
43public:
44 instance_commandscript() : CommandScript("instance_commandscript") { }
45
47 {
48 static ChatCommandTable instanceCommandTable =
49 {
56 };
57
58 static ChatCommandTable commandTable =
59 {
60 { "instance", instanceCommandTable },
61 };
62
63 return commandTable;
64 }
65
67 {
68 Player* player = handler->getSelectedPlayer();
69 if (!player)
70 player = handler->GetSession()->GetPlayer();
71
72 uint32 counter = 0;
73 for (uint8 i = 0; i < MAX_DIFFICULTY; ++i)
74 {
75 for (auto const& [mapId, bind] : player->GetBoundInstances(Difficulty(i)))
76 {
77 InstanceSave const* save = bind.save;
79 handler->PSendSysMessage(LANG_COMMAND_LIST_BIND_INFO, mapId, save->GetInstanceId(), bind.perm ? "yes" : "no", bind.extendState == EXTEND_STATE_EXPIRED ? "expired" : bind.extendState == EXTEND_STATE_EXTENDED ? "yes" : "no", save->GetDifficulty(), save->CanReset() ? "yes" : "no", timeleft.c_str());
80 counter++;
81 }
82 }
84
85 counter = 0;
86 if (Group* group = player->GetGroup())
87 {
88 for (uint8 i = 0; i < MAX_DIFFICULTY; ++i)
89 {
90 for (auto const& [mapId, bind] : group->GetBoundInstances(Difficulty(i)))
91 {
92 InstanceSave* save = bind.save;
94 handler->PSendSysMessage(LANG_COMMAND_LIST_BIND_INFO, mapId, save->GetInstanceId(), bind.perm ? "yes" : "no", "-", save->GetDifficulty(), save->CanReset() ? "yes" : "no", timeleft.c_str());
95 counter++;
96 }
97 }
98 }
100
101 return true;
102 }
103
104 static bool HandleInstanceUnbindCommand(ChatHandler* handler, Variant<uint16, EXACT_SEQUENCE("all")> mapArg, Optional<uint8> difficultyArg)
105 {
106 Player* player = handler->getSelectedPlayer();
107 if (!player)
108 player = handler->GetSession()->GetPlayer();
109
110 uint16 counter = 0;
111 uint16 mapId = 0;
112
113 if (mapArg.holds_alternative<uint16>())
114 {
115 mapId = mapArg.get<uint16>();
116 if (!mapId)
117 return false;
118 }
119
120 for (uint8 i = 0; i < MAX_DIFFICULTY; ++i)
121 {
123 for (Player::BoundInstancesMap::iterator itr = binds.begin(); itr != binds.end();)
124 {
125 InstanceSave const* save = itr->second.save;
126 if (itr->first != player->GetMapId() && (!mapId || mapId == itr->first) && (!difficultyArg || difficultyArg == save->GetDifficulty()))
127 {
129 handler->PSendSysMessage(LANG_COMMAND_INST_UNBIND_UNBINDING, itr->first, save->GetInstanceId(), itr->second.perm ? "yes" : "no", save->GetDifficulty(), save->CanReset() ? "yes" : "no", timeleft.c_str());
130 player->UnbindInstance(itr, Difficulty(i));
131 counter++;
132 }
133 else
134 ++itr;
135 }
136 }
138
139 return true;
140 }
141
143 {
144 handler->PSendSysMessage(LANG_COMMAND_INST_STAT_LOADED_INST, sMapMgr->GetNumInstances());
145 handler->PSendSysMessage(LANG_COMMAND_INST_STAT_PLAYERS_IN, sMapMgr->GetNumPlayersInInstances());
146 handler->PSendSysMessage(LANG_COMMAND_INST_STAT_SAVES, sInstanceSaveMgr->GetNumInstanceSaves());
147 handler->PSendSysMessage(LANG_COMMAND_INST_STAT_PLAYERSBOUND, sInstanceSaveMgr->GetNumBoundPlayersTotal());
148 handler->PSendSysMessage(LANG_COMMAND_INST_STAT_GROUPSBOUND, sInstanceSaveMgr->GetNumBoundGroupsTotal());
149
150 return true;
151 }
152
154 {
155 Player* player = handler->GetSession()->GetPlayer();
156 InstanceMap* map = player->GetMap()->ToInstanceMap();
157 if (!map)
158 {
160 handler->SetSentErrorMessage(true);
161 return false;
162 }
163
164 if (!map->GetInstanceScript())
165 {
167 handler->SetSentErrorMessage(true);
168 return false;
169 }
170
171 map->GetInstanceScript()->SaveToDB();
172
173 return true;
174 }
175
177 {
178 // Character name must be provided when using this from console.
179 if (!player && !handler->GetSession())
180 {
182 handler->SetSentErrorMessage(true);
183 return false;
184 }
185
186 if (!player)
187 player = PlayerIdentifier::FromSelf(handler);
188
189 if (!player->IsConnected())
190 {
192 handler->SetSentErrorMessage(true);
193 return false;
194 }
195
196 InstanceMap* map = player->GetConnectedPlayer()->GetMap()->ToInstanceMap();
197 if (!map)
198 {
200 handler->SetSentErrorMessage(true);
201 return false;
202 }
203
204 if (!map->GetInstanceScript())
205 {
207 handler->SetSentErrorMessage(true);
208 return false;
209 }
210
211 // Reject improper values.
212 if (encounterId > map->GetInstanceScript()->GetEncounterCount())
213 {
215 handler->SetSentErrorMessage(true);
216 return false;
217 }
218
219 map->GetInstanceScript()->SetBossState(encounterId, state);
220 handler->PSendSysMessage(LANG_COMMAND_INST_SET_BOSS_STATE, encounterId, state, EnumUtils::ToConstant(state));
221 return true;
222 }
223
225 {
226 // Character name must be provided when using this from console.
227 if (!player && !handler->GetSession())
228 {
230 handler->SetSentErrorMessage(true);
231 return false;
232 }
233
234 if (!player)
235 player = PlayerIdentifier::FromSelf(handler);
236
237 if (!player->IsConnected())
238 {
240 handler->SetSentErrorMessage(true);
241 return false;
242 }
243
244 InstanceMap* map = player->GetConnectedPlayer()->GetMap()->ToInstanceMap();
245 if (!map)
246 {
248 handler->SetSentErrorMessage(true);
249 return false;
250 }
251
252 if (!map->GetInstanceScript())
253 {
255 handler->SetSentErrorMessage(true);
256 return false;
257 }
258
259 if (encounterId > map->GetInstanceScript()->GetEncounterCount())
260 {
262 handler->SetSentErrorMessage(true);
263 return false;
264 }
265
266 EncounterState state = map->GetInstanceScript()->GetBossState(encounterId);
267 handler->PSendSysMessage(LANG_COMMAND_INST_GET_BOSS_STATE, encounterId, state, EnumUtils::ToConstant(state));
268 return true;
269 }
270};
271
#define EXACT_SEQUENCE(str)
Difficulty
Definition DBCEnums.h:279
#define MAX_DIFFICULTY
Definition DBCEnums.h:296
uint8_t uint8
Definition Define.h:135
uint16_t uint16
Definition Define.h:134
uint32_t uint32
Definition Define.h:133
#define sInstanceSaveMgr
EncounterState
@ LANG_COMMAND_INST_SET_BOSS_STATE
Definition Language.h:1118
@ LANG_COMMAND_LIST_BIND_GROUP_BINDS
Definition Language.h:1108
@ LANG_COMMAND_INST_STAT_GROUPSBOUND
Definition Language.h:1115
@ LANG_COMMAND_INST_GET_BOSS_STATE
Definition Language.h:1119
@ LANG_COMMAND_LIST_BIND_PLAYER_BINDS
Definition Language.h:1107
@ LANG_COMMAND_INST_STAT_SAVES
Definition Language.h:1113
@ LANG_NOT_DUNGEON
Definition Language.h:1116
@ LANG_COMMAND_LIST_BIND_INFO
Definition Language.h:1106
@ LANG_NO_INSTANCE_DATA
Definition Language.h:1117
@ LANG_CMD_SYNTAX
Definition Language.h:42
@ LANG_COMMAND_INST_STAT_PLAYERSBOUND
Definition Language.h:1114
@ LANG_COMMAND_INST_UNBIND_UNBOUND
Definition Language.h:1110
@ LANG_PLAYER_NOT_FOUND
Definition Language.h:570
@ LANG_COMMAND_INST_STAT_PLAYERS_IN
Definition Language.h:1112
@ LANG_COMMAND_INST_UNBIND_UNBINDING
Definition Language.h:1109
@ LANG_BAD_VALUE
Definition Language.h:149
@ LANG_COMMAND_INST_STAT_LOADED_INST
Definition Language.h:1111
#define sMapMgr
Definition MapManager.h:211
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition Optional.h:25
@ EXTEND_STATE_EXTENDED
Definition Player.h:777
@ EXTEND_STATE_EXPIRED
Definition Player.h:775
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:302
WorldSession * GetSession()
Definition Chat.h:46
void SetSentErrorMessage(bool val)
Definition Chat.h:134
void PSendSysMessage(char const *fmt, Args &&... args)
Definition Chat.h:69
static char const * ToConstant(Enum value)
Definition SmartEnum.h:120
Definition Group.h:165
InstanceScript * GetInstanceScript()
Definition Map.h:887
uint32 GetInstanceId() const
Difficulty GetDifficulty() const
bool CanReset() const
time_t GetResetTime() const
virtual bool SetBossState(uint32 id, EncounterState state)
EncounterState GetBossState(uint32 id) const
uint32 GetEncounterCount() const
InstanceMap * ToInstanceMap()
Definition Map.h:520
std::unordered_map< uint32, InstancePlayerBind > BoundInstancesMap
Definition Player.h:2139
void UnbindInstance(uint32 mapid, Difficulty difficulty, bool unload=false)
Definition Player.cpp:18590
BoundInstancesMap & GetBoundInstances(Difficulty difficulty)
Definition Player.h:2148
Group * GetGroup()
Definition Player.h:2171
uint32 GetMapId() const
Definition Position.h:193
Map * GetMap() const
Definition Object.h:449
Player * GetPlayer() const
ChatCommandTable GetCommands() const override
static bool HandleInstanceGetBossStateCommand(ChatHandler *handler, uint32 encounterId, Optional< PlayerIdentifier > player)
static bool HandleInstanceSetBossStateCommand(ChatHandler *handler, uint32 encounterId, EncounterState state, Optional< PlayerIdentifier > player)
static bool HandleInstanceSaveDataCommand(ChatHandler *handler)
static bool HandleInstanceUnbindCommand(ChatHandler *handler, Variant< uint16, EXACT_SEQUENCE("all")> mapArg, Optional< uint8 > difficultyArg)
static bool HandleInstanceStatsCommand(ChatHandler *handler)
static bool HandleInstanceListBindsCommand(ChatHandler *handler)
void AddSC_instance_commandscript()
time_t GetGameTime()
Definition GameTime.cpp:42
std::vector< ChatCommandBuilder > ChatCommandTable
Definition ChatCommand.h:50
@ RBAC_PERM_COMMAND_INSTANCE_LISTBINDS
Definition RBAC.h:283
@ RBAC_PERM_COMMAND_INSTANCE_SET_BOSS_STATE
Definition RBAC.h:665
@ RBAC_PERM_COMMAND_INSTANCE_GET_BOSS_STATE
Definition RBAC.h:666
@ RBAC_PERM_COMMAND_INSTANCE_STATS
Definition RBAC.h:285
@ RBAC_PERM_COMMAND_INSTANCE_UNBIND
Definition RBAC.h:284
@ RBAC_PERM_COMMAND_INSTANCE_SAVEDATA
Definition RBAC.h:286
static Optional< PlayerIdentifier > FromSelf(ChatHandler *handler)