From 40edefa6f4e5f14d5913f9a92f409e05014191a5 Mon Sep 17 00:00:00 2001 From: Kinglykrab <89047260+Kinglykrab@users.noreply.github.com> Date: Sun, 21 Nov 2021 10:03:20 -0500 Subject: [PATCH] [Commands] Cleanup #camerashake Command. (#1787) - Cleanup message and logic. - Fix ConvertSecondsToTime logic for milliseconds. - Add ConvertMillisecondsToTime inline function. --- common/string_util.cpp | 21 ++++++++++++++++-- common/string_util.h | 5 ++++- world/zonelist.cpp | 2 +- zone/command.cpp | 2 +- zone/gm_commands/camerashake.cpp | 37 ++++++++++++++++++++------------ zone/gm_commands/stun.cpp | 2 +- zone/worldserver.cpp | 2 +- 7 files changed, 50 insertions(+), 21 deletions(-) diff --git a/common/string_util.cpp b/common/string_util.cpp index a770ce202..d89bbe39b 100644 --- a/common/string_util.cpp +++ b/common/string_util.cpp @@ -1021,9 +1021,26 @@ std::vector GetBadWords() }; } -std::string ConvertSecondsToTime(int duration) +std::string ConvertSecondsToTime(int duration, bool is_milliseconds) { - int timer_length = duration; + if (duration <= 0) { + return "Unknown"; + } + + if (is_milliseconds && duration < 1000) { + return fmt::format( + "{} Millisecond{}", + duration, + duration != 1 ? "s" : "" + ); + } + + int timer_length = ( + is_milliseconds ? + static_cast(std::ceil(static_cast(duration) / 1000.0f)) : + duration + ); + int days = int(timer_length / 86400000); timer_length %= 86400000; int hours = int(timer_length / 3600); diff --git a/common/string_util.h b/common/string_util.h index 07b5ef5d0..e9c3bfa15 100644 --- a/common/string_util.h +++ b/common/string_util.h @@ -45,7 +45,10 @@ std::vector wrap(std::vector &src, std::string charact std::string implode(std::string glue, std::vector src); std::string convert2digit(int n, std::string suffix); std::string numberToWords(unsigned long long int n); -std::string ConvertSecondsToTime(int duration); +std::string ConvertSecondsToTime(int duration, bool is_milliseconds = false); +inline std::string ConvertMillisecondsToTime(int duration) { + return ConvertSecondsToTime(duration, true); +} // For converstion of numerics into English // Used for grid nodes, as NPC names remove numerals. diff --git a/world/zonelist.cpp b/world/zonelist.cpp index c3aa358d3..128800e12 100644 --- a/world/zonelist.cpp +++ b/world/zonelist.cpp @@ -51,7 +51,7 @@ ZSList::~ZSList() { void ZSList::ShowUpTime(WorldTCPConnection* con, const char* adminname) { uint32 ms = Timer::GetCurrentTime(); - std::string time_string = ConvertSecondsToTime(ms); + std::string time_string = ConvertMillisecondsToTime(ms); con->SendEmoteMessage( adminname, 0, diff --git a/zone/command.cpp b/zone/command.cpp index 173316ebf..1d87b1861 100755 --- a/zone/command.cpp +++ b/zone/command.cpp @@ -133,7 +133,7 @@ int command_init(void) command_add("bot", "- Type \"#bot help\" or \"^help\" to the see the list of available commands for bots.", AccountStatus::Player, command_bot) || #endif - command_add("camerashake", "Shakes the camera on everyone's screen globally.", AccountStatus::QuestTroupe, command_camerashake) || + command_add("camerashake", "[Duration (Milliseconds)] [Intensity (1-10)] - Shakes the camera on everyone's screen globally.", AccountStatus::QuestTroupe, command_camerashake) || command_add("castspell", "[Spell ID] [Instant (0 = False, 1 = True, Default is 1 if Unused)] - Cast a spell", AccountStatus::Guide, command_castspell) || command_add("chat", "[channel num] [message] - Send a channel message to all zones", AccountStatus::GMMgmt, command_chat) || command_add("checklos", "- Check for line of sight to your target", AccountStatus::Guide, command_checklos) || diff --git a/zone/gm_commands/camerashake.cpp b/zone/gm_commands/camerashake.cpp index 39a6ebcfd..cb781a99a 100755 --- a/zone/gm_commands/camerashake.cpp +++ b/zone/gm_commands/camerashake.cpp @@ -5,20 +5,29 @@ extern WorldServer worldserver; void command_camerashake(Client *c, const Seperator *sep) { - if (c) { - if (sep->arg[1][0] && sep->arg[2][0]) { - auto pack = new ServerPacket(ServerOP_CameraShake, sizeof(ServerCameraShake_Struct)); - ServerCameraShake_Struct *scss = (ServerCameraShake_Struct *) pack->pBuffer; - scss->duration = atoi(sep->arg[1]); - scss->intensity = atoi(sep->arg[2]); - worldserver.SendPacket(pack); - c->Message(Chat::Red, "Successfully sent the packet to world! Shake it, world, shake it!"); - safe_delete(pack); - } - else { - c->Message(Chat::Red, "Usage -- #camerashake [duration], [intensity [1-10])"); - } + int arguments = sep->argnum; + if (!arguments || !sep->IsNumber(1) || !sep->IsNumber(2)) { + c->Message(Chat::Red, "Usage: #camerashake [Duration (Milliseconds)] [Intensity (1-10)]"); + return; } - return; + + auto duration = std::stoi(sep->arg[1]); + auto intensity = std::stoi(sep->arg[2]); + + auto pack = new ServerPacket(ServerOP_CameraShake, sizeof(ServerCameraShake_Struct)); + ServerCameraShake_Struct *camera_shake = (ServerCameraShake_Struct *) pack->pBuffer; + camera_shake->duration = duration; + camera_shake->intensity = intensity; + worldserver.SendPacket(pack); + c->Message( + Chat::White, + fmt::format( + "Sending camera shake to world with a duration of {} ({}) and an intensity of {}.", + ConvertMillisecondsToTime(duration), + duration, + intensity + ).c_str() + ); + safe_delete(pack); } diff --git a/zone/gm_commands/stun.cpp b/zone/gm_commands/stun.cpp index 9775a45a3..01acce4f0 100755 --- a/zone/gm_commands/stun.cpp +++ b/zone/gm_commands/stun.cpp @@ -41,7 +41,7 @@ void command_stun(Client *c, const Seperator *sep) target->GetID() ) ), - ConvertSecondsToTime(duration) + ConvertMillisecondsToTime(duration) ) : fmt::format( "You unstunned {}.", diff --git a/zone/worldserver.cpp b/zone/worldserver.cpp index be039be8d..09f1e8134 100644 --- a/zone/worldserver.cpp +++ b/zone/worldserver.cpp @@ -826,7 +826,7 @@ void WorldServer::HandleMessage(uint16 opcode, const EQ::Net::Packet &p) } ServerUptime_Struct* sus = (ServerUptime_Struct*)pack->pBuffer; uint32 ms = Timer::GetCurrentTime(); - std::string time_string = ConvertSecondsToTime(ms); + std::string time_string = ConvertMillisecondsToTime(ms); SendEmoteMessage( sus->adminname, 0,