From 5f8744fa691cde46b53a35e91a48cbbb99943cab Mon Sep 17 00:00:00 2001 From: hondacrx Date: Fri, 7 Jan 2022 10:03:32 -0500 Subject: [PATCH] Core/Server: correct timestamp format for shutdown/restart notification broadcasts Port From (https://github.com/TrinityCore/TrinityCore/commit/69f768605082222a3ca573c623d3fc019106be3b) --- Source/Framework/Util/Time.cs | 82 +++++++++++++++++-- Source/Game/Chat/Commands/BanCommands.cs | 12 +-- .../Game/Chat/Commands/GameObjectCommands.cs | 4 +- Source/Game/Chat/Commands/ListCommands.cs | 2 +- Source/Game/Chat/Commands/MiscCommands.cs | 6 +- Source/Game/Chat/Commands/NPCCommands.cs | 4 +- Source/Game/Server/WorldManager.cs | 2 +- Source/Game/SupportSystem/SupportTickets.cs | 6 +- Source/Game/Warden/Warden.cs | 2 +- 9 files changed, 94 insertions(+), 26 deletions(-) diff --git a/Source/Framework/Util/Time.cs b/Source/Framework/Util/Time.cs index e446bd649..125d2c66a 100644 --- a/Source/Framework/Util/Time.cs +++ b/Source/Framework/Util/Time.cs @@ -16,6 +16,14 @@ */ using System; +using System.Text; + +public enum TimeFormat +{ + FullText, // 1 Days 2 Hours 3 Minutes 4 Seconds + ShortText, // 1d 2h 3m 4s + Numeric // 1:2:3:4 +} public static class Time { @@ -133,27 +141,87 @@ public static class Time return hourLocal; } - public static string secsToTimeString(ulong timeInSecs, bool shortText = false, bool hoursOnly = false) + public static string secsToTimeString(ulong timeInSecs, TimeFormat timeFormat = TimeFormat.FullText, bool hoursOnly = false) { ulong secs = timeInSecs % Minute; ulong minutes = timeInSecs % Hour / Minute; ulong hours = timeInSecs % Day / Hour; ulong days = timeInSecs / Day; - string ss = ""; + StringBuilder ss = new(); if (days != 0) - ss += days + (shortText ? "d" : " Day(s) "); + { + ss.Append(days); + if (timeFormat == TimeFormat.Numeric) + ss.Append(":"); + else if (timeFormat == TimeFormat.ShortText) + ss.Append("d"); + else // if (timeFormat == TimeFormat::FullText) + { + if (days == 1) + ss.Append(" Day "); + else + ss.Append(" Days "); + } + } if (hours != 0 || hoursOnly) - ss += hours + (shortText ? "h" : " Hour(s) "); + { + ss.Append(hours); + if (timeFormat == TimeFormat.Numeric) + ss.Append(":"); + else if (timeFormat == TimeFormat.ShortText) + ss.Append("h"); + else // if (timeFormat == TimeFormat::FullText) + { + if (hours <= 1) + ss.Append(" Hour "); + else + ss.Append(" Hours "); + } + } if (!hoursOnly) { if (minutes != 0) - ss += minutes + (shortText ? "m" : " Minute(s) "); + { + ss.Append(minutes); + if (timeFormat == TimeFormat.Numeric) + ss.Append(":"); + else if (timeFormat == TimeFormat.ShortText) + ss.Append("m"); + else // if (timeFormat == TimeFormat::FullText) + { + if (minutes == 1) + ss.Append(" Minute "); + else + ss.Append(" Minutes "); + } + } + else + { + if (timeFormat == TimeFormat.Numeric) + ss.Append("0:"); + } if (secs != 0 || (days == 0 && hours == 0 && minutes == 0)) - ss += secs + (shortText ? "s" : " Second(s)."); + { + ss.Append($"{secs:2}"); + if (timeFormat == TimeFormat.ShortText) + ss.Append("s"); + else if (timeFormat == TimeFormat.FullText) + { + if (secs <= 1) + ss.Append(" Second."); + else + ss.Append(" Seconds."); + } + } + else + { + if (timeFormat == TimeFormat.Numeric) + ss.Append("00"); + } } - return ss; + return ss.ToString(); } public static uint TimeStringToSecs(string timestring) diff --git a/Source/Game/Chat/Commands/BanCommands.cs b/Source/Game/Chat/Commands/BanCommands.cs index 465ccf104..910b2ea9f 100644 --- a/Source/Game/Chat/Commands/BanCommands.cs +++ b/Source/Game/Chat/Commands/BanCommands.cs @@ -69,9 +69,9 @@ namespace Game.Chat.Commands if (duration > 0) { if (WorldConfig.GetBoolValue(WorldCfg.ShowBanInWorld)) - Global.WorldMgr.SendWorldText(CypherStrings.BanCharacterYoubannedmessageWorld, author, name, Time.secsToTimeString(Time.TimeStringToSecs(durationStr), true), reasonStr); + Global.WorldMgr.SendWorldText(CypherStrings.BanCharacterYoubannedmessageWorld, author, name, Time.secsToTimeString(Time.TimeStringToSecs(durationStr), TimeFormat.ShortText), reasonStr); else - handler.SendSysMessage(CypherStrings.BanYoubanned, name, Time.secsToTimeString(Time.TimeStringToSecs(durationStr), true), reasonStr); + handler.SendSysMessage(CypherStrings.BanYoubanned, name, Time.secsToTimeString(Time.TimeStringToSecs(durationStr), TimeFormat.ShortText), reasonStr); } else { @@ -147,7 +147,7 @@ namespace Game.Chat.Commands if (WorldConfig.GetBoolValue(WorldCfg.ShowBanInWorld)) Global.WorldMgr.SendWorldText(CypherStrings.BanAccountYoubannedmessageWorld, author, nameOrIP, Time.secsToTimeString(Time.TimeStringToSecs(durationStr)), reasonStr); else - handler.SendSysMessage(CypherStrings.BanYoubanned, nameOrIP, Time.secsToTimeString(Time.TimeStringToSecs(durationStr), true), reasonStr); + handler.SendSysMessage(CypherStrings.BanYoubanned, nameOrIP, Time.secsToTimeString(Time.TimeStringToSecs(durationStr), TimeFormat.ShortText), reasonStr); } else { @@ -250,7 +250,7 @@ namespace Game.Chat.Commands if (result.Read(2) && (result.Read(1) == 0L || unbanDate >= GameTime.GetGameTime())) active = true; bool permanent = (result.Read(1) == 0L); - string banTime = permanent ? handler.GetCypherString(CypherStrings.BaninfoInfinite) : Time.secsToTimeString(result.Read(1), true); + string banTime = permanent ? handler.GetCypherString(CypherStrings.BaninfoInfinite) : Time.secsToTimeString(result.Read(1), TimeFormat.ShortText); handler.SendSysMessage(CypherStrings.BaninfoHistoryentry, Time.UnixTimeToDateTime(result.Read(0)).ToShortTimeString(), banTime, active ? handler.GetCypherString(CypherStrings.Yes) : handler.GetCypherString(CypherStrings.No), result.Read(4), result.Read(5)); } @@ -278,7 +278,7 @@ namespace Game.Chat.Commands bool permanent = result.Read(6) == 0; handler.SendSysMessage(CypherStrings.BaninfoIpentry, result.Read(0), result.Read(1), permanent ? handler.GetCypherString( CypherStrings.BaninfoNever) : result.Read(2), - permanent ? handler.GetCypherString( CypherStrings.BaninfoInfinite) : Time.secsToTimeString(result.Read(3), true), result.Read(4), result.Read(5)); + permanent ? handler.GetCypherString( CypherStrings.BaninfoInfinite) : Time.secsToTimeString(result.Read(3), TimeFormat.ShortText), result.Read(4), result.Read(5)); return true; } @@ -300,7 +300,7 @@ namespace Game.Chat.Commands if (result.Read(2) && (result.Read(1) == 0 || unbanDate >= GameTime.GetGameTime())) active = true; bool permanent = (result.Read(1) == 0); - string banTime = permanent ? handler.GetCypherString(CypherStrings.BaninfoInfinite) : Time.secsToTimeString(result.Read(1), true); + string banTime = permanent ? handler.GetCypherString(CypherStrings.BaninfoInfinite) : Time.secsToTimeString(result.Read(1), TimeFormat.ShortText); handler.SendSysMessage(CypherStrings.BaninfoHistoryentry, result.Read(0), banTime, active ? handler.GetCypherString(CypherStrings.Yes) : handler.GetCypherString(CypherStrings.No), result.Read(4), result.Read(5)); } diff --git a/Source/Game/Chat/Commands/GameObjectCommands.cs b/Source/Game/Chat/Commands/GameObjectCommands.cs index 17f8e901c..c384c451b 100644 --- a/Source/Game/Chat/Commands/GameObjectCommands.cs +++ b/Source/Game/Chat/Commands/GameObjectCommands.cs @@ -495,8 +495,8 @@ namespace Game.Chat if (curRespawnDelay < 0) curRespawnDelay = 0; - string curRespawnDelayStr = Time.secsToTimeString((uint)curRespawnDelay, true); - string defRespawnDelayStr = Time.secsToTimeString(target.GetRespawnDelay(), true); + string curRespawnDelayStr = Time.secsToTimeString((uint)curRespawnDelay, TimeFormat.ShortText); + string defRespawnDelayStr = Time.secsToTimeString(target.GetRespawnDelay(), TimeFormat.ShortText); handler.SendSysMessage(CypherStrings.CommandRawpawntimes, defRespawnDelayStr, curRespawnDelayStr); } diff --git a/Source/Game/Chat/Commands/ListCommands.cs b/Source/Game/Chat/Commands/ListCommands.cs index 77e47d285..be481203f 100644 --- a/Source/Game/Chat/Commands/ListCommands.cs +++ b/Source/Game/Chat/Commands/ListCommands.cs @@ -616,7 +616,7 @@ namespace Game.Chat.Commands uint gridY = ri.gridId / MapConst.MaxGrids; uint gridX = ri.gridId % MapConst.MaxGrids; - string respawnTime = ri.respawnTime > GameTime.GetGameTime() ? Time.secsToTimeString((ulong)(ri.respawnTime - GameTime.GetGameTime()), true) : stringOverdue; + string respawnTime = ri.respawnTime > GameTime.GetGameTime() ? Time.secsToTimeString((ulong)(ri.respawnTime - GameTime.GetGameTime()), TimeFormat.ShortText) : stringOverdue; handler.SendSysMessage($"{ri.spawnId} | {ri.entry} | [{gridX:2},{gridY:2}] | {GetZoneName(respawnZoneId, locale)} ({respawnZoneId}) | {respawnTime}{(map.IsSpawnGroupActive(data.spawnGroupData.groupId) ? "" : " (inactive)")}"); } } diff --git a/Source/Game/Chat/Commands/MiscCommands.cs b/Source/Game/Chat/Commands/MiscCommands.cs index 385c6e5ba..eeade0e51 100644 --- a/Source/Game/Chat/Commands/MiscCommands.cs +++ b/Source/Game/Chat/Commands/MiscCommands.cs @@ -1738,11 +1738,11 @@ namespace Game.Chat // Output III. LANG_PINFO_BANNED if ban exists and is applied if (banTime >= 0) - handler.SendSysMessage(CypherStrings.PinfoBanned, banType, banReason, banTime > 0 ? Time.secsToTimeString((ulong)(banTime - GameTime.GetGameTime()), true) : handler.GetCypherString(CypherStrings.Permanently), bannedBy); + handler.SendSysMessage(CypherStrings.PinfoBanned, banType, banReason, banTime > 0 ? Time.secsToTimeString((ulong)(banTime - GameTime.GetGameTime()), TimeFormat.ShortText) : handler.GetCypherString(CypherStrings.Permanently), bannedBy); // Output IV. LANG_PINFO_MUTED if mute is applied if (muteTime > 0) - handler.SendSysMessage(CypherStrings.PinfoMuted, muteReason, Time.secsToTimeString((ulong)(muteTime - GameTime.GetGameTime()), true), muteBy); + handler.SendSysMessage(CypherStrings.PinfoMuted, muteReason, Time.secsToTimeString((ulong)(muteTime - GameTime.GetGameTime()), TimeFormat.ShortText), muteBy); // Output V. LANG_PINFO_ACC_ACCOUNT handler.SendSysMessage(CypherStrings.PinfoAccAccount, userName, accId, security); @@ -1817,7 +1817,7 @@ namespace Game.Chat } // Output XX. LANG_PINFO_CHR_PLAYEDTIME - handler.SendSysMessage(CypherStrings.PinfoChrPlayedtime, (Time.secsToTimeString(totalPlayerTime, true, true))); + handler.SendSysMessage(CypherStrings.PinfoChrPlayedtime, (Time.secsToTimeString(totalPlayerTime, TimeFormat.ShortText, true))); // Mail Data - an own query, because it may or may not be useful. // SQL: "SELECT SUM(CASE WHEN (checked & 1) THEN 1 ELSE 0 END) AS 'readmail', COUNT(*) AS 'totalmail' FROM mail WHERE `receiver` = ?" diff --git a/Source/Game/Chat/Commands/NPCCommands.cs b/Source/Game/Chat/Commands/NPCCommands.cs index d0bfea445..10617370d 100644 --- a/Source/Game/Chat/Commands/NPCCommands.cs +++ b/Source/Game/Chat/Commands/NPCCommands.cs @@ -135,8 +135,8 @@ namespace Game.Chat if (curRespawnDelay < 0) curRespawnDelay = 0; - string curRespawnDelayStr = Time.secsToTimeString((ulong)curRespawnDelay, true); - string defRespawnDelayStr = Time.secsToTimeString(target.GetRespawnDelay(), true); + string curRespawnDelayStr = Time.secsToTimeString((ulong)curRespawnDelay, TimeFormat.ShortText); + string defRespawnDelayStr = Time.secsToTimeString(target.GetRespawnDelay(), TimeFormat.ShortText); handler.SendSysMessage(CypherStrings.NpcinfoChar, target.GetName(), target.GetSpawnId(), target.GetGUID().ToString(), entry, faction, npcflags, displayid, nativeid); if (target.GetCreatureData() != null && target.GetCreatureData().spawnGroupData.groupId != 0) diff --git a/Source/Game/Server/WorldManager.cs b/Source/Game/Server/WorldManager.cs index 240d4cf6e..28d819cb6 100644 --- a/Source/Game/Server/WorldManager.cs +++ b/Source/Game/Server/WorldManager.cs @@ -1842,7 +1842,7 @@ namespace Game (m_ShutdownTimer < 12 * Time.Hour && (m_ShutdownTimer % Time.Hour) == 0) || // < 12 h ; every 1 h (m_ShutdownTimer > 12 * Time.Hour && (m_ShutdownTimer % (12 * Time.Hour)) == 0)) // > 12 h ; every 12 h { - var str = Time.secsToTimeString(m_ShutdownTimer); + var str = Time.secsToTimeString(m_ShutdownTimer, TimeFormat.Numeric); if (!reason.IsEmpty()) str += " - " + reason; diff --git a/Source/Game/SupportSystem/SupportTickets.cs b/Source/Game/SupportSystem/SupportTickets.cs index f46d700c4..80049ce62 100644 --- a/Source/Game/SupportSystem/SupportTickets.cs +++ b/Source/Game/SupportSystem/SupportTickets.cs @@ -189,7 +189,7 @@ namespace Game.SupportSystem StringBuilder ss = new(); ss.Append(handler.GetParsedString(CypherStrings.CommandTicketlistguid, _id)); ss.Append(handler.GetParsedString(CypherStrings.CommandTicketlistname, GetPlayerName())); - ss.Append(handler.GetParsedString(CypherStrings.CommandTicketlistagecreate, Time.secsToTimeString(curTime - _createTime, true, false))); + ss.Append(handler.GetParsedString(CypherStrings.CommandTicketlistagecreate, Time.secsToTimeString(curTime - _createTime, TimeFormat.ShortText, false))); if (!_assignedTo.IsEmpty()) ss.Append(handler.GetParsedString(CypherStrings.CommandTicketlistassignedto, GetAssignedToName())); @@ -327,7 +327,7 @@ namespace Game.SupportSystem StringBuilder ss = new(); ss.Append(handler.GetParsedString(CypherStrings.CommandTicketlistguid, _id)); ss.Append(handler.GetParsedString(CypherStrings.CommandTicketlistname, GetPlayerName())); - ss.Append(handler.GetParsedString(CypherStrings.CommandTicketlistagecreate, Time.secsToTimeString(curTime - _createTime, true, false))); + ss.Append(handler.GetParsedString(CypherStrings.CommandTicketlistagecreate, Time.secsToTimeString(curTime - _createTime, TimeFormat.ShortText, false))); if (!_assignedTo.IsEmpty()) ss.Append(handler.GetParsedString(CypherStrings.CommandTicketlistassignedto, GetAssignedToName())); @@ -433,7 +433,7 @@ namespace Game.SupportSystem StringBuilder ss = new(); ss.Append(handler.GetParsedString(CypherStrings.CommandTicketlistguid, _id)); ss.Append(handler.GetParsedString(CypherStrings.CommandTicketlistname, GetPlayerName())); - ss.Append(handler.GetParsedString(CypherStrings.CommandTicketlistagecreate, Time.secsToTimeString(curTime - _createTime, true, false))); + ss.Append(handler.GetParsedString(CypherStrings.CommandTicketlistagecreate, Time.secsToTimeString(curTime - _createTime, TimeFormat.ShortText, false))); if (!_assignedTo.IsEmpty()) ss.Append(handler.GetParsedString(CypherStrings.CommandTicketlistassignedto, GetAssignedToName())); diff --git a/Source/Game/Warden/Warden.cs b/Source/Game/Warden/Warden.cs index 30ab445c8..6cb7a3dfa 100644 --- a/Source/Game/Warden/Warden.cs +++ b/Source/Game/Warden/Warden.cs @@ -100,7 +100,7 @@ namespace Game if (_clientResponseTimer > maxClientResponseDelay * Time.InMilliseconds) { Log.outWarn(LogFilter.Warden, "{0} (latency: {1}, IP: {2}) exceeded Warden module response delay for more than {3} - disconnecting client", - _session.GetPlayerInfo(), _session.GetLatency(), _session.GetRemoteAddress(), Time.secsToTimeString(maxClientResponseDelay, true)); + _session.GetPlayerInfo(), _session.GetLatency(), _session.GetRemoteAddress(), Time.secsToTimeString(maxClientResponseDelay, TimeFormat.ShortText)); _session.KickPlayer(); } else