feat(Scripts/Commands): convert cs_instance to new system (#8831)

This commit is contained in:
IntelligentQuantum
2021-11-03 16:55:25 +03:30
committed by GitHub
parent 6da6cfdd4a
commit cde621f9eb
+48 -105
View File
@@ -15,25 +15,22 @@
* with this program. If not, see <http://www.gnu.org/licenses/>. * with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
/* ScriptData /* ScriptData
Name: instance_commandscript Name: instance_commandscript
%Complete: 100 %Complete: 100
Comment: All instance related commands Comment: All instance related commands
Category: commandscripts Category: commandscripts
EndScriptData */ EndScriptData */
#include "ScriptMgr.h"
#include "Chat.h" #include "Chat.h"
#include "Group.h" #include "Group.h"
#include "InstanceSaveMgr.h" #include "InstanceSaveMgr.h"
#include "InstanceScript.h" #include "InstanceScript.h"
#include "Language.h" #include "Language.h"
#include "MapMgr.h" #include "MapMgr.h"
#include "ObjectAccessor.h"
#include "Player.h" #include "Player.h"
#include "ScriptMgr.h"
#if AC_COMPILER == AC_COMPILER_GNU
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
using namespace Acore::ChatCommands; using namespace Acore::ChatCommands;
@@ -46,80 +43,62 @@ public:
{ {
static ChatCommandTable instanceCommandTable = static ChatCommandTable instanceCommandTable =
{ {
{ "listbinds", SEC_MODERATOR, false, &HandleInstanceListBindsCommand, "" }, { "listbinds", HandleInstanceListBindsCommand, SEC_MODERATOR, Console::No },
{ "unbind", SEC_GAMEMASTER, false, &HandleInstanceUnbindCommand, "" }, { "unbind", HandleInstanceUnbindCommand, SEC_GAMEMASTER, Console::No },
{ "stats", SEC_MODERATOR, true, &HandleInstanceStatsCommand, "" }, { "stats", HandleInstanceStatsCommand, SEC_MODERATOR, Console::Yes },
{ "savedata", SEC_ADMINISTRATOR, false, &HandleInstanceSaveDataCommand, "" }, { "savedata", HandleInstanceSaveDataCommand, SEC_ADMINISTRATOR, Console::No },
{ "setbossstate", SEC_GAMEMASTER, true, &HandleInstanceSetBossStateCommand, "" }, { "setbossstate", HandleInstanceSetBossStateCommand, SEC_GAMEMASTER, Console::Yes },
{ "getbossstate", SEC_MODERATOR, true, &HandleInstanceGetBossStateCommand, "" } { "getbossstate", HandleInstanceGetBossStateCommand, SEC_MODERATOR, Console::Yes },
}; };
static ChatCommandTable commandTable = static ChatCommandTable commandTable =
{ {
{ "instance", SEC_MODERATOR, true, nullptr, "", instanceCommandTable } { "instance", instanceCommandTable },
}; };
return commandTable; return commandTable;
} }
static std::string GetTimeString(uint64 time) static bool HandleInstanceListBindsCommand(ChatHandler* handler)
{
uint64 days = time / DAY, hours = (time % DAY) / HOUR, minute = (time % HOUR) / MINUTE;
std::ostringstream ss;
if (days)
ss << days << "d ";
if (hours)
ss << hours << "h ";
ss << minute << 'm';
return ss.str();
}
static bool HandleInstanceListBindsCommand(ChatHandler* handler, char const* /*args*/)
{ {
Player* player = handler->getSelectedPlayer(); Player* player = handler->getSelectedPlayer();
if (!player) if (!player)
player = handler->GetSession()->GetPlayer(); player = handler->GetSession()->GetPlayer();
uint32 counter = 0; uint32 counter = 0;
for (uint8 i = 0; i < MAX_DIFFICULTY; ++i) for (uint8 i = 0; i < MAX_DIFFICULTY; ++i)
{ {
BoundInstancesMap const& m_boundInstances = sInstanceSaveMgr->PlayerGetBoundInstances(player->GetGUID(), Difficulty(i)); for (auto const& [mapId, bind] : sInstanceSaveMgr->PlayerGetBoundInstances(player->GetGUID(), Difficulty(i)))
for (BoundInstancesMap::const_iterator itr = m_boundInstances.begin(); itr != m_boundInstances.end(); ++itr)
{ {
InstanceSave* save = itr->second.save; InstanceSave const* save = bind.save;
uint32 resetTime = itr->second.extended ? save->GetExtendedResetTime() : save->GetResetTime(); uint32 resetTime = bind.extended ? save->GetExtendedResetTime() : save->GetResetTime();
uint32 ttr = (resetTime >= time(nullptr) ? resetTime - time(nullptr) : 0); uint32 ttr = (resetTime >= time(nullptr) ? resetTime - time(nullptr) : 0);
std::string timeleft = GetTimeString(ttr); std::string timeleft = secsToTimeString(ttr);
handler->PSendSysMessage("map: %d, inst: %d, perm: %s, diff: %d, canReset: %s, TTR: %s%s", itr->first, save->GetInstanceId(), itr->second.perm ? "yes" : "no", save->GetDifficulty(), save->CanReset() ? "yes" : "no", timeleft.c_str(), (itr->second.extended ? " (extended)" : "")); handler->PSendSysMessage("map: %d, inst: %d, perm: %s, diff: %d, canReset: %s, TTR: %s%s",
mapId, save->GetInstanceId(), bind.perm ? "yes" : "no", save->GetDifficulty(), save->CanReset() ? "yes" : "no", timeleft.c_str(), (bind.extended ? " (extended)" : ""));
counter++; counter++;
} }
} }
handler->PSendSysMessage("player binds: %d", counter); handler->PSendSysMessage("player binds: %d", counter);
return true; return true;
} }
static bool HandleInstanceUnbindCommand(ChatHandler* handler, char const* args) static bool HandleInstanceUnbindCommand(ChatHandler* handler, Variant<uint16, EXACT_SEQUENCE("all")> mapArg, Optional<uint8> difficultyArg)
{ {
if (!*args)
return false;
Player* player = handler->getSelectedPlayer(); Player* player = handler->getSelectedPlayer();
if (!player) if (!player)
player = handler->GetSession()->GetPlayer(); player = handler->GetSession()->GetPlayer();
char* map = strtok((char*)args, " ");
char* pDiff = strtok(nullptr, " ");
int8 diff = -1;
if (pDiff)
diff = atoi(pDiff);
uint16 counter = 0; uint16 counter = 0;
uint16 MapId = 0; uint16 mapId = 0;
if (strcmp(map, "all")) if (mapArg.holds_alternative<uint16>())
{ {
MapId = uint16(atoi(map)); mapId = mapArg.get<uint16>();
if (!MapId) if (!mapId)
return false; return false;
} }
@@ -128,12 +107,12 @@ public:
BoundInstancesMap const& m_boundInstances = sInstanceSaveMgr->PlayerGetBoundInstances(player->GetGUID(), Difficulty(i)); BoundInstancesMap const& m_boundInstances = sInstanceSaveMgr->PlayerGetBoundInstances(player->GetGUID(), Difficulty(i));
for (BoundInstancesMap::const_iterator itr = m_boundInstances.begin(); itr != m_boundInstances.end();) for (BoundInstancesMap::const_iterator itr = m_boundInstances.begin(); itr != m_boundInstances.end();)
{ {
InstanceSave* save = itr->second.save; InstanceSave const* save = itr->second.save;
if (itr->first != player->GetMapId() && (!MapId || MapId == itr->first) && (diff == -1 || diff == save->GetDifficulty())) if (itr->first != player->GetMapId() && (!mapId || mapId == itr->first) && (!difficultyArg || difficultyArg == save->GetDifficulty()))
{ {
uint32 resetTime = itr->second.extended ? save->GetExtendedResetTime() : save->GetResetTime(); uint32 resetTime = itr->second.extended ? save->GetExtendedResetTime() : save->GetResetTime();
uint32 ttr = (resetTime >= time(nullptr) ? resetTime - time(nullptr) : 0); uint32 ttr = (resetTime >= time(nullptr) ? resetTime - time(nullptr) : 0);
std::string timeleft = GetTimeString(ttr); std::string timeleft = secsToTimeString(ttr);
handler->PSendSysMessage("unbinding map: %d, inst: %d, perm: %s, diff: %d, canReset: %s, TTR: %s%s", itr->first, save->GetInstanceId(), itr->second.perm ? "yes" : "no", save->GetDifficulty(), save->CanReset() ? "yes" : "no", timeleft.c_str(), (itr->second.extended ? " (extended)" : "")); handler->PSendSysMessage("unbinding map: %d, inst: %d, perm: %s, diff: %d, canReset: %s, TTR: %s%s", itr->first, save->GetInstanceId(), itr->second.perm ? "yes" : "no", save->GetDifficulty(), save->CanReset() ? "yes" : "no", timeleft.c_str(), (itr->second.extended ? " (extended)" : ""));
sInstanceSaveMgr->PlayerUnbindInstance(player->GetGUID(), itr->first, Difficulty(i), true, player); sInstanceSaveMgr->PlayerUnbindInstance(player->GetGUID(), itr->first, Difficulty(i), true, player);
itr = m_boundInstances.begin(); itr = m_boundInstances.begin();
@@ -143,12 +122,13 @@ public:
++itr; ++itr;
} }
} }
handler->PSendSysMessage("instances unbound: %d", counter); handler->PSendSysMessage("instances unbound: %d", counter);
return true; return true;
} }
static bool HandleInstanceStatsCommand(ChatHandler* handler, char const* /*args*/) static bool HandleInstanceStatsCommand(ChatHandler* handler)
{ {
uint32 dungeon = 0, battleground = 0, arena = 0, spectators = 0; uint32 dungeon = 0, battleground = 0, arena = 0, spectators = 0;
sMapMgr->GetNumInstances(dungeon, battleground, arena); sMapMgr->GetNumInstances(dungeon, battleground, arena);
@@ -164,7 +144,7 @@ public:
return false; return false;
} }
static bool HandleInstanceSaveDataCommand(ChatHandler* handler, char const* /*args*/) static bool HandleInstanceSaveDataCommand(ChatHandler* handler)
{ {
Player* player = handler->GetSession()->GetPlayer(); Player* player = handler->GetSession()->GetPlayer();
Map* map = player->GetMap(); Map* map = player->GetMap();
@@ -187,44 +167,27 @@ public:
return true; return true;
} }
static bool HandleInstanceSetBossStateCommand(ChatHandler* handler, char const* args) static bool HandleInstanceSetBossStateCommand(ChatHandler* handler, uint32 encounterId, uint32 state, Optional<PlayerIdentifier> player)
{ {
if (!*args)
return false;
char* param1 = strtok((char*)args, " ");
char* param2 = strtok(nullptr, " ");
char* param3 = strtok(nullptr, " ");
uint32 encounterId = 0;
int32 state = 0;
Player* player = nullptr;
std::string playerName;
// Character name must be provided when using this from console. // Character name must be provided when using this from console.
if (!param2 || (!param3 && !handler->GetSession())) if (!player && !handler->GetSession())
{ {
handler->PSendSysMessage(LANG_CMD_SYNTAX); handler->PSendSysMessage(LANG_CMD_SYNTAX);
handler->SetSentErrorMessage(true); handler->SetSentErrorMessage(true);
return false; return false;
} }
if (!param3)
player = handler->GetSession()->GetPlayer();
else
{
playerName = param3;
if (normalizePlayerName(playerName))
player = ObjectAccessor::FindPlayerByName(playerName);
}
if (!player) if (!player)
player = PlayerIdentifier::FromSelf(handler);
if (!player->IsConnected())
{ {
handler->PSendSysMessage(LANG_PLAYER_NOT_FOUND); handler->PSendSysMessage(LANG_PLAYER_NOT_FOUND);
handler->SetSentErrorMessage(true); handler->SetSentErrorMessage(true);
return false; return false;
} }
InstanceMap* map = player->GetMap()->ToInstanceMap(); InstanceMap* map = player->GetConnectedPlayer()->GetMap()->ToInstanceMap();
if (!map) if (!map)
{ {
handler->PSendSysMessage(LANG_NOT_DUNGEON); handler->PSendSysMessage(LANG_NOT_DUNGEON);
@@ -239,11 +202,8 @@ public:
return false; return false;
} }
encounterId = atoi(param1);
state = atoi(param2);
// Reject improper values. // Reject improper values.
if (state > TO_BE_DECIDED || encounterId > map->GetInstanceScript()->GetEncounterCount()) if (encounterId > map->GetInstanceScript()->GetEncounterCount())
{ {
handler->PSendSysMessage(LANG_BAD_VALUE); handler->PSendSysMessage(LANG_BAD_VALUE);
handler->SetSentErrorMessage(true); handler->SetSentErrorMessage(true);
@@ -256,42 +216,27 @@ public:
return true; return true;
} }
static bool HandleInstanceGetBossStateCommand(ChatHandler* handler, char const* args) static bool HandleInstanceGetBossStateCommand(ChatHandler* handler, uint32 encounterId, Optional<PlayerIdentifier> player)
{ {
if (!*args)
return false;
char* param1 = strtok((char*)args, " ");
char* param2 = strtok(nullptr, " ");
uint32 encounterId = 0;
Player* player = nullptr;
std::string playerName;
// Character name must be provided when using this from console. // Character name must be provided when using this from console.
if (!param1 || (!param2 && !handler->GetSession())) if (!player && !handler->GetSession())
{ {
handler->PSendSysMessage(LANG_CMD_SYNTAX); handler->PSendSysMessage(LANG_CMD_SYNTAX);
handler->SetSentErrorMessage(true); handler->SetSentErrorMessage(true);
return false; return false;
} }
if (!param2)
player = handler->GetSession()->GetPlayer();
else
{
playerName = param2;
if (normalizePlayerName(playerName))
player = ObjectAccessor::FindPlayerByName(playerName);
}
if (!player) if (!player)
player = PlayerIdentifier::FromSelf(handler);
if (!player->IsConnected())
{ {
handler->PSendSysMessage(LANG_PLAYER_NOT_FOUND); handler->PSendSysMessage(LANG_PLAYER_NOT_FOUND);
handler->SetSentErrorMessage(true); handler->SetSentErrorMessage(true);
return false; return false;
} }
InstanceMap* map = player->GetMap()->ToInstanceMap(); InstanceMap* map = player->GetConnectedPlayer()->GetMap()->ToInstanceMap();
if (!map) if (!map)
{ {
handler->PSendSysMessage(LANG_NOT_DUNGEON); handler->PSendSysMessage(LANG_NOT_DUNGEON);
@@ -306,8 +251,6 @@ public:
return false; return false;
} }
encounterId = atoi(param1);
if (encounterId > map->GetInstanceScript()->GetEncounterCount()) if (encounterId > map->GetInstanceScript()->GetEncounterCount())
{ {
handler->PSendSysMessage(LANG_BAD_VALUE); handler->PSendSysMessage(LANG_BAD_VALUE);