feat(Scripts/Commands): Implement restart/shutdown reasons (#9242)

This commit is contained in:
Skjalf
2021-11-20 16:47:41 -03:00
committed by GitHub
parent 3b80396993
commit 3d95308d40
5 changed files with 86 additions and 17 deletions
+2 -2
View File
@@ -545,9 +545,9 @@ public:
virtual void SendServerMessage(ServerMessageType type, const char* text = "", Player* player = nullptr) = 0; virtual void SendServerMessage(ServerMessageType type, const char* text = "", Player* player = nullptr) = 0;
virtual bool IsShuttingDown() const = 0; virtual bool IsShuttingDown() const = 0;
virtual uint32 GetShutDownTimeLeft() const = 0; virtual uint32 GetShutDownTimeLeft() const = 0;
virtual void ShutdownServ(uint32 time, uint32 options, uint8 exitcode) = 0; virtual void ShutdownServ(uint32 time, uint32 options, uint8 exitcode, const std::string& reason = std::string()) = 0;
virtual void ShutdownCancel() = 0; virtual void ShutdownCancel() = 0;
virtual void ShutdownMsg(bool show = false, Player* player = nullptr) = 0; virtual void ShutdownMsg(bool show = false, Player* player = nullptr, const std::string& reason = std::string()) = 0;
virtual void Update(uint32 diff) = 0; virtual void Update(uint32 diff) = 0;
virtual void UpdateSessions(uint32 diff) = 0; virtual void UpdateSessions(uint32 diff) = 0;
virtual void setRate(Rates rate, float value) = 0; virtual void setRate(Rates rate, float value) = 0;
+8 -3
View File
@@ -2712,7 +2712,7 @@ void World::_UpdateGameTime()
} }
/// Shutdown the server /// Shutdown the server
void World::ShutdownServ(uint32 time, uint32 options, uint8 exitcode) void World::ShutdownServ(uint32 time, uint32 options, uint8 exitcode, const std::string& reason)
{ {
// ignore if server shutdown at next tick // ignore if server shutdown at next tick
if (IsStopped()) if (IsStopped())
@@ -2733,14 +2733,14 @@ void World::ShutdownServ(uint32 time, uint32 options, uint8 exitcode)
else else
{ {
m_ShutdownTimer = time; m_ShutdownTimer = time;
ShutdownMsg(true); ShutdownMsg(true, nullptr, reason);
} }
sScriptMgr->OnShutdownInitiate(ShutdownExitCode(exitcode), ShutdownMask(options)); sScriptMgr->OnShutdownInitiate(ShutdownExitCode(exitcode), ShutdownMask(options));
} }
/// Display a shutdown message to the user(s) /// Display a shutdown message to the user(s)
void World::ShutdownMsg(bool show, Player* player) void World::ShutdownMsg(bool show, Player* player, const std::string& reason)
{ {
// not show messages for idle shutdown mode // not show messages for idle shutdown mode
if (m_ShutdownMask & SHUTDOWN_MASK_IDLE) if (m_ShutdownMask & SHUTDOWN_MASK_IDLE)
@@ -2756,6 +2756,11 @@ void World::ShutdownMsg(bool show, Player* player)
{ {
std::string str = secsToTimeString(m_ShutdownTimer).append("."); std::string str = secsToTimeString(m_ShutdownTimer).append(".");
if (!reason.empty())
{
str += " - " + reason;
}
ServerMessageType msgid = (m_ShutdownMask & SHUTDOWN_MASK_RESTART) ? SERVER_MSG_RESTART_TIME : SERVER_MSG_SHUTDOWN_TIME; ServerMessageType msgid = (m_ShutdownMask & SHUTDOWN_MASK_RESTART) ? SERVER_MSG_RESTART_TIME : SERVER_MSG_SHUTDOWN_TIME;
SendServerMessage(msgid, str.c_str(), player); SendServerMessage(msgid, str.c_str(), player);
+2 -2
View File
@@ -265,9 +265,9 @@ public:
/// Are we in the middle of a shutdown? /// Are we in the middle of a shutdown?
bool IsShuttingDown() const { return m_ShutdownTimer > 0; } bool IsShuttingDown() const { return m_ShutdownTimer > 0; }
uint32 GetShutDownTimeLeft() const { return m_ShutdownTimer; } uint32 GetShutDownTimeLeft() const { return m_ShutdownTimer; }
void ShutdownServ(uint32 time, uint32 options, uint8 exitcode); void ShutdownServ(uint32 time, uint32 options, uint8 exitcode, const std::string& reason = std::string());
void ShutdownCancel(); void ShutdownCancel();
void ShutdownMsg(bool show = false, Player* player = nullptr); void ShutdownMsg(bool show = false, Player* player = nullptr, const std::string& reason = std::string());
static uint8 GetExitCode() { return m_ExitCode; } static uint8 GetExitCode() { return m_ExitCode; }
static void StopNow(uint8 exitcode) { m_stopEvent = true; m_ExitCode = exitcode; } static void StopNow(uint8 exitcode) { m_stopEvent = true; m_ExitCode = exitcode; }
static bool IsStopped() { return m_stopEvent; } static bool IsStopped() { return m_stopEvent; }
+72 -8
View File
@@ -267,57 +267,121 @@ public:
return true; return true;
} }
static bool HandleServerShutDownCommand(ChatHandler* /*handler*/, int32 time, Optional<int32> exitCode) static bool HandleServerShutDownCommand(ChatHandler* /*handler*/, int32 time, Optional<int32> exitCode, Tail reason)
{ {
std::wstring wReason = std::wstring();
std::string strReason = std::string();
if (!reason.empty())
{
if (!Utf8toWStr(reason, wReason))
{
return false;
}
if (!WStrToUtf8(wReason, strReason))
{
return false;
}
}
if (exitCode && *exitCode >= 0 && *exitCode <= 125) if (exitCode && *exitCode >= 0 && *exitCode <= 125)
{ {
sWorld->ShutdownServ(time, 0, *exitCode); sWorld->ShutdownServ(time, 0, *exitCode);
} }
else else
{ {
sWorld->ShutdownServ(time, 0, SHUTDOWN_EXIT_CODE); sWorld->ShutdownServ(time, 0, SHUTDOWN_EXIT_CODE, strReason);
} }
return true; return true;
} }
static bool HandleServerRestartCommand(ChatHandler* /*handler*/, int32 time, Optional<int32> exitCode) static bool HandleServerRestartCommand(ChatHandler* /*handler*/, int32 time, Optional<int32> exitCode, Tail reason)
{ {
std::wstring wReason = std::wstring();
std::string strReason = std::string();
if (!reason.empty())
{
if (!Utf8toWStr(reason, wReason))
{
return false;
}
if (!WStrToUtf8(wReason, strReason))
{
return false;
}
}
if (exitCode && *exitCode >= 0 && *exitCode <= 125) if (exitCode && *exitCode >= 0 && *exitCode <= 125)
{ {
sWorld->ShutdownServ(time, SHUTDOWN_MASK_RESTART, *exitCode); sWorld->ShutdownServ(time, SHUTDOWN_MASK_RESTART, *exitCode);
} }
else else
{ {
sWorld->ShutdownServ(time, SHUTDOWN_MASK_RESTART, RESTART_EXIT_CODE); sWorld->ShutdownServ(time, SHUTDOWN_MASK_RESTART, RESTART_EXIT_CODE, strReason);
} }
return true; return true;
} }
static bool HandleServerIdleRestartCommand(ChatHandler* /*handler*/, int32 time, Optional<int32> exitCode) static bool HandleServerIdleRestartCommand(ChatHandler* /*handler*/, int32 time, Optional<int32> exitCode, Tail reason)
{ {
std::wstring wReason = std::wstring();
std::string strReason = std::string();
if (!reason.empty())
{
if (!Utf8toWStr(reason, wReason))
{
return false;
}
if (!WStrToUtf8(wReason, strReason))
{
return false;
}
}
if (exitCode && *exitCode >= 0 && *exitCode <= 125) if (exitCode && *exitCode >= 0 && *exitCode <= 125)
{ {
sWorld->ShutdownServ(time, SHUTDOWN_MASK_RESTART | SHUTDOWN_MASK_IDLE, *exitCode); sWorld->ShutdownServ(time, SHUTDOWN_MASK_RESTART | SHUTDOWN_MASK_IDLE, *exitCode);
} }
else else
{ {
sWorld->ShutdownServ(time, SHUTDOWN_MASK_RESTART | SHUTDOWN_MASK_IDLE, RESTART_EXIT_CODE); sWorld->ShutdownServ(time, SHUTDOWN_MASK_RESTART | SHUTDOWN_MASK_IDLE, RESTART_EXIT_CODE, strReason);
} }
return true; return true;
} }
static bool HandleServerIdleShutDownCommand(ChatHandler* /*handler*/, int32 time, Optional<int32> exitCode) static bool HandleServerIdleShutDownCommand(ChatHandler* /*handler*/, int32 time, Optional<int32> exitCode, Tail reason)
{ {
std::wstring wReason = std::wstring();
std::string strReason = std::string();
if (!reason.empty())
{
if (!Utf8toWStr(reason, wReason))
{
return false;
}
if (!WStrToUtf8(wReason, strReason))
{
return false;
}
}
if (exitCode && *exitCode >= 0 && *exitCode <= 125) if (exitCode && *exitCode >= 0 && *exitCode <= 125)
{ {
sWorld->ShutdownServ(time, SHUTDOWN_MASK_IDLE, *exitCode); sWorld->ShutdownServ(time, SHUTDOWN_MASK_IDLE, *exitCode);
} }
else else
{ {
sWorld->ShutdownServ(time, SHUTDOWN_MASK_IDLE, SHUTDOWN_EXIT_CODE); sWorld->ShutdownServ(time, SHUTDOWN_MASK_IDLE, SHUTDOWN_EXIT_CODE, strReason);
} }
return true; return true;
+2 -2
View File
@@ -86,9 +86,9 @@ public:
MOCK_METHOD(void, SendServerMessage, (ServerMessageType type, const char* text, Player* player)); MOCK_METHOD(void, SendServerMessage, (ServerMessageType type, const char* text, Player* player));
MOCK_METHOD(bool, IsShuttingDown, (), (const)); MOCK_METHOD(bool, IsShuttingDown, (), (const));
MOCK_METHOD(uint32, GetShutDownTimeLeft, (), (const)); MOCK_METHOD(uint32, GetShutDownTimeLeft, (), (const));
MOCK_METHOD(void, ShutdownServ, (uint32 time, uint32 options, uint8 exitcode), ()); MOCK_METHOD(void, ShutdownServ, (uint32 time, uint32 options, uint8 exitcode, const std::string& reason), ());
MOCK_METHOD(void, ShutdownCancel, ()); MOCK_METHOD(void, ShutdownCancel, ());
MOCK_METHOD(void, ShutdownMsg, (bool show, Player* player), ()); MOCK_METHOD(void, ShutdownMsg, (bool show, Player* player, const std::string& reason), ());
MOCK_METHOD(void, Update, (uint32 diff), ()); MOCK_METHOD(void, Update, (uint32 diff), ());
MOCK_METHOD(void, UpdateSessions, (uint32 diff), ()); MOCK_METHOD(void, UpdateSessions, (uint32 diff), ());
MOCK_METHOD(void, setRate, (Rates rate, float value), ()); MOCK_METHOD(void, setRate, (Rates rate, float value), ());