diff --git a/Content.Server/Administration/Commands/PlayTimeCommands.cs b/Content.Server/Administration/Commands/PlayTimeCommands.cs index 3fe8e38de95..97d3f12e38d 100644 --- a/Content.Server/Administration/Commands/PlayTimeCommands.cs +++ b/Content.Server/Administration/Commands/PlayTimeCommands.cs @@ -3,94 +3,10 @@ using Content.Shared.Players.PlayTimeTracking; using Robust.Server.Player; using Robust.Shared.Console; -using System.Text.RegularExpressions; namespace Content.Server.Administration.Commands; -public sealed class PlayTimeCommandUtilities -{ - private readonly static Dictionary Units = new() { - { "y", 525960 }, - { "mo", 43800 }, - { "w", 10080 }, - { "d", 1440 }, - { "h", 60 }, - { "m", 1 }, - }; - - public struct TimeUnit - { - public int TimeValue { get; } - public string Unit { get; } - - public TimeUnit(int timeValue) - { - TimeValue = timeValue; - Unit = "m"; - } - - public TimeUnit(int timeValue, string unit) - { - TimeValue = timeValue; - Unit = unit; - } - public int ToMinutes() - { - var unitExists = Units.TryGetValue(Unit, out int minutes); - - if (!unitExists) - return TimeValue; - - return TimeValue * minutes; - } - } - - public static List ConvertToTimeUnits(string timeString) - { - // Searching for something similar to 365d24h, etc. - List result = new(); - - // We want to support plain numbers as a translation to just minutes, just in case people don't know things like 30d or 1d are an option. - if (int.TryParse(timeString, out int timeValue)) - { - result.Add(new TimeUnit(timeValue, "m")); - return result; - } - - MatchCollection timeRegex = Regex.Matches(timeString, "(\\d+)([A-Za-z]+)"); - - foreach (Match match in timeRegex) - { - bool isTimeAmountNumber = int.TryParse(match.Groups[1].Value, out int amountOfTime); - string timeUnit = match.Groups[2].Value; - - if (!isTimeAmountNumber) - continue; - - if (!Units.ContainsKey(timeUnit)) - continue; - - result.Add(new TimeUnit(amountOfTime, timeUnit)); - } - - return result; - } - - public static int CountMinutes(string timeString) - { - List timeUnits = ConvertToTimeUnits(timeString); - int total = 0; - - foreach (var timeUnit in timeUnits) - { - total += timeUnit.ToMinutes(); - } - - return total; - } -} - -[AdminCommand(AdminFlags.Moderator)] +[AdminCommand(AdminFlags.Admin)] public sealed class PlayTimeAddOverallCommand : IConsoleCommand { [Dependency] private readonly IPlayerManager _playerManager = default!; @@ -108,7 +24,11 @@ public async void Execute(IConsoleShell shell, string argStr, string[] args) return; } - var minutes = PlayTimeCommandUtilities.CountMinutes(args[1]); + if (!int.TryParse(args[1], out var minutes)) + { + shell.WriteError(Loc.GetString("parse-minutes-fail", ("minutes", args[1]))); + return; + } if (!_playerManager.TryGetSessionByUsername(args[0], out var player)) { @@ -138,7 +58,7 @@ public CompletionResult GetCompletion(IConsoleShell shell, string[] args) } } -[AdminCommand(AdminFlags.Moderator)] +[AdminCommand(AdminFlags.Admin)] public sealed class PlayTimeAddRoleCommand : IConsoleCommand { [Dependency] private readonly IPlayerManager _playerManager = default!; @@ -165,10 +85,15 @@ public async void Execute(IConsoleShell shell, string argStr, string[] args) var role = args[1]; - var m = PlayTimeCommandUtilities.CountMinutes(args[2]); + var m = args[2]; + if (!int.TryParse(m, out var minutes)) + { + shell.WriteError(Loc.GetString("parse-minutes-fail", ("minutes", minutes))); + return; + } - _playTimeTracking.AddTimeToTracker(player, role, TimeSpan.FromMinutes(m)); - var time = _playTimeTracking.GetPlayTimeForTracker(player, role); + _playTimeTracking.AddTimeToTracker(player, role, TimeSpan.FromMinutes(minutes)); + var time = _playTimeTracking.GetOverallPlaytime(player); shell.WriteLine(Loc.GetString("cmd-playtime_addrole-succeed", ("username", userName), ("role", role), @@ -198,7 +123,7 @@ public CompletionResult GetCompletion(IConsoleShell shell, string[] args) } } -[AdminCommand(AdminFlags.Moderator)] +[AdminCommand(AdminFlags.Admin)] public sealed class PlayTimeGetOverallCommand : IConsoleCommand { [Dependency] private readonly IPlayerManager _playerManager = default!; @@ -243,7 +168,7 @@ public CompletionResult GetCompletion(IConsoleShell shell, string[] args) } } -[AdminCommand(AdminFlags.Moderator)] +[AdminCommand(AdminFlags.Admin)] public sealed class PlayTimeGetRoleCommand : IConsoleCommand { [Dependency] private readonly IPlayerManager _playerManager = default!; @@ -322,7 +247,7 @@ public CompletionResult GetCompletion(IConsoleShell shell, string[] args) /// /// Saves the timers for a particular player immediately /// -[AdminCommand(AdminFlags.Moderator)] +[AdminCommand(AdminFlags.Admin)] public sealed class PlayTimeSaveCommand : IConsoleCommand { [Dependency] private readonly IPlayerManager _playerManager = default!; @@ -409,4 +334,4 @@ public CompletionResult GetCompletion(IConsoleShell shell, string[] args) return CompletionResult.Empty; } -} \ No newline at end of file +}