Skip to content

Commit

Permalink
Refactor ghost & admin roles tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
DexlerXD committed Nov 9, 2023
1 parent a9d256f commit dedd872
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 80 deletions.
49 changes: 0 additions & 49 deletions Content.Server/Administration/Commands/PlayTimeCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,55 +168,6 @@ public CompletionResult GetCompletion(IConsoleShell shell, string[] args)
}
}

//SS220-aghost-playtime begin
[AdminCommand(AdminFlags.Admin)]
public sealed class PlayTimeGetAGhost : IConsoleCommand
{
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly PlayTimeTrackingManager _playTimeTracking = default!;

public string Command => "playtime_getaghost";
public string Description => Loc.GetString("cmd-playtime_getaghost-desc");
public string Help => Loc.GetString("cmd-playtime_getaghost-help", ("command", Command));

public async void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (args.Length != 1)
{
shell.WriteError(Loc.GetString("cmd-playtime_getaghost-error-args"));
return;
}

var userName = args[0];
if (!_playerManager.TryGetSessionByUsername(userName, out var player))
{
shell.WriteError(Loc.GetString("parse-session-fail", ("username", userName)));
return;
}

// I thought that displaying total hours will be more practical than displaying days:hours:minutes.
double value = _playTimeTracking.GetAGhostPlaytime(player).TotalHours;
value = Math.Round(value, 2); //Round to the second digit, i. e. 3,14 hours of aghost playtime
shell.WriteLine(Loc.GetString(
"cmd-playtime_getaghost-success",
("username", userName),
("time", value)));
}

public CompletionResult GetCompletion(IConsoleShell shell, string[] args)
{
if (args.Length == 1)
{
return CompletionResult.FromHintOptions(
CompletionHelper.SessionNames(players: _playerManager),
Loc.GetString("cmd-playtime_getaghost-arg-user"));
}

return CompletionResult.Empty;
}
}
//SS220-aghost-playtime end

[AdminCommand(AdminFlags.Admin)]
public sealed class PlayTimeGetRoleCommand : IConsoleCommand
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,13 +351,6 @@ public TimeSpan GetOverallPlaytime(ICommonSession id)
return GetPlayTimeForTracker(id, PlayTimeTrackingShared.TrackerOverall);
}

//SS220-aghost-playtime begin
public TimeSpan GetAGhostPlaytime(ICommonSession id)
{
return GetPlayTimeForTracker(id, PlayTimeTrackingShared.TrackerAGhost);
}
//SS220-aghost-playtime end

public bool TryGetTrackerTimes(ICommonSession id, [NotNullWhen(true)] out Dictionary<string, TimeSpan>? time)
{
time = null;
Expand Down
21 changes: 13 additions & 8 deletions Content.Server/Players/PlayTimeTracking/PlayTimeTrackingSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ public sealed class PlayTimeTrackingSystem : EntitySystem
[Dependency] private readonly MindSystem _minds = default!;
[Dependency] private readonly PlayTimeTrackingManager _tracking = default!;

private string AdminTrackerTime => "AdminTime";
private const string AGhostTrackerTime = "AGhostTime"; //SS220-aghost-playtime
private const string AGhostPrototypeID = "AdminObserver"; //SS220-aghost-playtime

public override void Initialize()
Expand Down Expand Up @@ -76,24 +74,31 @@ private bool IsBypassingChecks(ICommonSession player)

private void CalcTrackers(ICommonSession player, HashSet<string> trackers)
{
if (_afk.IsAfk(player))
//SS220-aghost-playtime begin
// Checking for attached entity to prevent tracking for players in lobby
if (_afk.IsAfk(player) || player.AttachedEntity == null)
return;

if (_adminManager.IsAdmin(player, includeDeAdmin: false))
{
trackers.Add(AdminTrackerTime);
//SS220-aghost-playtime begin
trackers.Add(PlayTimeTrackingShared.TrackerAdmin);
if (player.AttachedEntity is { } attachedEntity &&
Comp<MetaDataComponent>(attachedEntity).EntityPrototype?.ID == AGhostPrototypeID)
trackers.Add(AGhostTrackerTime);
//SS220-aghost-playtime end
{
trackers.Add(PlayTimeTrackingShared.TrackerAGhost);
return;
}
}

if (!IsPlayerAlive(player))
{
trackers.Add(PlayTimeTrackingShared.TrackerObserver);
return;
}
//SS220-aghost-playtime end

trackers.Add(PlayTimeTrackingShared.TrackerOverall);
trackers.UnionWith(GetTimedRoles(player));
trackers.Add(PlayTimeTrackingShared.TrackerOverall);
}

private bool IsPlayerAlive(ICommonSession session)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// © SS220, An EULA/CLA with a hosting restriction, full text: https://raw.githubusercontent.com/SerbiaStrong-220/space-station-14/master/CLA.txt

using Content.Server.Ghost.Roles;
using Content.Shared.Roles;

namespace Content.Server.SS220.GhostRoleMarkerRole;

public sealed class GhostRoleMarkerRoleTimeTracker : EntitySystem
{
private const string UnknownRoleName = "game-ticker-unknown-role";
private const string GhostRoleTracker = "JobGhostRole";

public override void Initialize()
{
base.Initialize();

SubscribeLocalEvent<GhostRoleMarkerRoleComponent, MindGetAllRolesEvent>(OnMindGetAllRoles);
}

private void OnMindGetAllRoles(EntityUid uid, GhostRoleMarkerRoleComponent component, ref MindGetAllRolesEvent args)
{
string name = component.Name == null ? UnknownRoleName : component.Name;
args.Roles.Add(new RoleInfo(component, name, false, GhostRoleTracker));
}
}
10 changes: 10 additions & 0 deletions Content.Shared/Players/PlayTimeTracking/PlayTimeTrackingShared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,19 @@ public static class PlayTimeTrackingShared
public const string TrackerOverall = "Overall";

//SS220-aghost-playtime begin
/// <summary>
/// The prototype ID of the play time tracker that represents overall time with admin priveleges.
/// </summary>
public const string TrackerAdmin = "AdminTime";

/// <summary>
/// The prototype ID of the play time tracker that represents admin ghost playtime.
/// </summary>
public const string TrackerAGhost = "AGhostTime";

/// <summary>
/// The prototype ID of the play time tracker that represents overall time in ghost.
/// </summary>
public const string TrackerObserver = "ObserverTime";
//SS220-aghost-playtime end
}
10 changes: 2 additions & 8 deletions Resources/Locale/en-US/players/play-time/play-time-commands.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ cmd-playtime_getoverall-success = Overall time for {$username} is {TOSTRING($tim
cmd-playtime_getoverall-arg-user = <user name>
cmd-playtime_getoverall-error-args = Expected exactly one argument
# - SS220 playtime_getaghost
cmd-playtime_getaghost-desc = Gets the specified hours for a player's admin-ghost playtime
cmd-playtime_getaghost-help = Usage: { $command } <user name>
cmd-playtime_getaghost-success = Admin-ghost playtime for { $username } is { $time } hours.
cmd-playtime_getaghost-arg-user = <user name>
cmd-playtime_getaghost-error-args = Expected exactly one argument
# - GetRoleTimer
cmd-playtime_getrole-desc = Gets all or one role timers from a player
cmd-playtime_getrole-help = Usage: {$command} <user name> [role]
Expand All @@ -42,7 +35,8 @@ cmd-playtime_getrole-role = Role: {$role}, Playtime: {$time}
cmd-playtime_getrole-overall = Overall playtime is {$time}
cmd-playtime_getrole-succeed = Playtime for {$username} is: {TOSTRING($time, "dddd\\:hh\\:mm")}.
cmd-playtime_getrole-arg-user = <user name>
cmd-playtime_getrole-arg-role = <role|'Overall'>
#SS220-aghost-playtime
cmd-playtime_getrole-arg-role = <role|'Overall'|'AdminTime'|'AGhostTime'|'ObserverTime'>
cmd-playtime_getrole-error-args = Expected exactly one or two arguments
# - playtime_save
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ cmd-playtime_getoverall-help = Использование: { $command } <user na
cmd-playtime_getoverall-success = Общее игровое время { $username } составляет { TOSTRING($time, "dddd\\:hh\\:mm") }.
cmd-playtime_getoverall-arg-user = <user name>
cmd-playtime_getoverall-error-args = Ожидается ровно один аргумент
# - SS220 playtime_getaghost
cmd-playtime_getaghost-desc = Получить количество часов, проведенное игроком в админ-госте
cmd-playtime_getaghost-help = Использование: { $command } <user name>
cmd-playtime_getaghost-success = Проведенное время на админ-госте { $username } составляет { $time } часов.
cmd-playtime_getaghost-arg-user = <user name>
cmd-playtime_getaghost-error-args = Ожидается ровно один аргумент
# - GetRoleTimer
cmd-playtime_getrole-desc = Получает все или один таймер роли от игрока
cmd-playtime_getrole-help = Использование: { $command } <user name> [role]
Expand All @@ -38,7 +32,8 @@ cmd-playtime_getrole-role = Роль: { $role }, игровое время: { $t
cmd-playtime_getrole-overall = Общее игровое время { $time }
cmd-playtime_getrole-succeed = Игровое время { $username } составляет: { TOSTRING($time, "dddd\\:hh\\:mm") }.
cmd-playtime_getrole-arg-user = <user name>
cmd-playtime_getrole-arg-role = <role|'Overall'>
#SS220-aghost-playtime
cmd-playtime_getrole-arg-role = <role|'Overall'|'AdminTime'|'AGhostTime'|'ObserverTime'>
cmd-playtime_getrole-error-args = Ожидается ровно один или два аргумента
# - playtime_save
cmd-playtime_save-desc = Сохранение игрового времени игрока в БД
Expand Down
11 changes: 10 additions & 1 deletion Resources/Prototypes/SS220/Roles/play_time_trackers.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
- type: playTimeTracker
id: JobGhostRole
id: JobGhostRole

- type: playTimeTracker
id: AdminTime

- type: playTimeTracker
id: AGhostTime

- type: playTimeTracker
id: ObserverTime

0 comments on commit dedd872

Please sign in to comment.