Skip to content

Commit

Permalink
Revert "Fixed admin things" (Simple-Station#55)
Browse files Browse the repository at this point in the history
Reverts Grimbly-Station/Grimbly-Station#54 because something something
(this was not a copy and paste job)
  • Loading branch information
SsalamethVersaach authored Sep 10, 2024
2 parents fe2af75 + bdb5bd8 commit cb619d4
Showing 1 changed file with 19 additions and 94 deletions.
113 changes: 19 additions & 94 deletions Content.Server/Administration/Commands/PlayTimeCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, int> 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<TimeUnit> ConvertToTimeUnits(string timeString)
{
// Searching for something similar to 365d24h, etc.
List<TimeUnit> 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<TimeUnit> 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!;
Expand All @@ -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))
{
Expand Down Expand Up @@ -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!;
Expand All @@ -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),
Expand Down Expand Up @@ -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!;
Expand Down Expand Up @@ -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!;
Expand Down Expand Up @@ -322,7 +247,7 @@ public CompletionResult GetCompletion(IConsoleShell shell, string[] args)
/// <summary>
/// Saves the timers for a particular player immediately
/// </summary>
[AdminCommand(AdminFlags.Moderator)]
[AdminCommand(AdminFlags.Admin)]
public sealed class PlayTimeSaveCommand : IConsoleCommand
{
[Dependency] private readonly IPlayerManager _playerManager = default!;
Expand Down Expand Up @@ -409,4 +334,4 @@ public CompletionResult GetCompletion(IConsoleShell shell, string[] args)

return CompletionResult.Empty;
}
}
}

0 comments on commit cb619d4

Please sign in to comment.