Skip to content

Commit

Permalink
make getting playtimes not hell
Browse files Browse the repository at this point in the history
  • Loading branch information
DEATHB4DEFEAT committed Jul 23, 2024
1 parent f28bbb3 commit 634e9e1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 33 deletions.
35 changes: 20 additions & 15 deletions Content.Client/Players/PlayTimeTracking/JobRequirementsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public sealed partial class JobRequirementsManager : ISharedPlaytimeManager
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IPrototypeManager _prototypes = default!;

private readonly Dictionary<string, TimeSpan> _roles = new();
public readonly Dictionary<string, TimeSpan> PlayTimes = new();
private readonly List<string> _roleBans = new();

private ISawmill _sawmill = default!;
Expand All @@ -46,7 +46,7 @@ private void ClientOnRunLevelChanged(object? sender, RunLevelChangedEventArgs e)
if (e.NewLevel == ClientRunLevel.Initialize)
{
// Reset on disconnect, just in case.
_roles.Clear();
PlayTimes.Clear();
}
}

Expand All @@ -64,12 +64,12 @@ private void RxRoleBans(MsgRoleBans message)

private void RxPlayTime(MsgPlayTime message)
{
_roles.Clear();
PlayTimes.Clear();

// NOTE: do not assign _roles = message.Trackers due to implicit data sharing in integration tests.
foreach (var (tracker, time) in message.Trackers)
{
_roles[tracker] = time;
PlayTimes[tracker] = time;
}

/*var sawmill = Logger.GetSawmill("play_time");
Expand Down Expand Up @@ -107,7 +107,7 @@ public bool CheckRoleTime(HashSet<JobRequirement>? requirements, [NotNullWhen(fa
var reasons = new List<string>();
foreach (var requirement in requirements)
{
if (JobRequirements.TryRequirementMet(requirement, _roles, out var jobReason, _entManager, _prototypes, _whitelisted, localePrefix))
if (JobRequirements.TryRequirementMet(requirement, PlayTimes, out var jobReason, _entManager, _prototypes, _whitelisted, localePrefix))
continue;

reasons.Add(jobReason.ToMarkup());
Expand All @@ -119,25 +119,30 @@ public bool CheckRoleTime(HashSet<JobRequirement>? requirements, [NotNullWhen(fa

public TimeSpan FetchOverallPlaytime()
{
return _roles.TryGetValue("Overall", out var overallPlaytime) ? overallPlaytime : TimeSpan.Zero;
return PlayTimes.TryGetValue("Overall", out var overallPlaytime) ? overallPlaytime : TimeSpan.Zero;
}

public IEnumerable<KeyValuePair<string, TimeSpan>> FetchPlaytimeByRoles()
public Dictionary<string, TimeSpan> FetchPlaytimeByRoles()
{
var jobsToMap = _prototypes.EnumeratePrototypes<JobPrototype>();
var ret = new Dictionary<string, TimeSpan>();

foreach (var job in jobsToMap)
{
if (_roles.TryGetValue(job.PlayTimeTracker, out var locJobName))
{
yield return new KeyValuePair<string, TimeSpan>(job.Name, locJobName);
}
}
if (PlayTimes.TryGetValue(job.PlayTimeTracker, out var locJobName))
ret.Add(job.Name, locJobName);

return ret;
}


public IReadOnlyDictionary<string, TimeSpan> GetPlayTimes(ICommonSession session)
public Dictionary<string, TimeSpan> GetPlayTimes()
{
return session != _playerManager.LocalSession ? new Dictionary<string, TimeSpan>() : _roles;
var dict = new Dictionary<string, TimeSpan>();

dict.Add(PlayTimeTrackingShared.TrackerOverall, FetchOverallPlaytime());
foreach (var role in FetchPlaytimeByRoles())
dict.Add(role.Key, role.Value);

return dict;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ private static void FlushSingleTracker(PlayTimeData data, TimeSpan time)
}
}

public IReadOnlyDictionary<string, TimeSpan> GetPlayTimes(ICommonSession session)
public Dictionary<string, TimeSpan> GetPlayTimes(ICommonSession session)
{
return GetTrackerTimes(session);
}
Expand Down
17 changes: 9 additions & 8 deletions Content.Shared/Customization/Systems/CharacterRequirements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public override bool IsValid(IPrototype prototype, JobPrototype job, HumanoidCha
reason = Inverted
? null
: FormattedMessage.FromMarkup(Loc.GetString("character-timer-department-too-high",
("time", playtime.Minutes - Max.Minutes),
("time", playtime.TotalMinutes - Max.TotalMinutes),
("department", Loc.GetString($"department-{department.ID}")),
("departmentColor", department.Color)));
return false;
Expand All @@ -322,7 +322,7 @@ public override bool IsValid(IPrototype prototype, JobPrototype job, HumanoidCha
reason = Inverted
? null
: FormattedMessage.FromMarkup(Loc.GetString("character-timer-department-insufficient",
("time", Min.Minutes - playtime.Minutes),
("time", Min.TotalMinutes - playtime.TotalMinutes),
("department", Loc.GetString($"department-{department.ID}")),
("departmentColor", department.Color)));
return false;
Expand Down Expand Up @@ -367,7 +367,7 @@ public override bool IsValid(IPrototype prototype, JobPrototype job, HumanoidCha
reason = Inverted
? null
: FormattedMessage.FromMarkup(Loc.GetString("character-timer-overall-too-high",
("time", overallTime.Minutes - Max.Minutes)));
("time", overallTime.TotalMinutes - Max.TotalMinutes)));
return false;
}

Expand All @@ -377,7 +377,7 @@ public override bool IsValid(IPrototype prototype, JobPrototype job, HumanoidCha
reason = Inverted
? null
: FormattedMessage.FromMarkup(Loc.GetString("character-timer-overall-insufficient",
("time", Min.Minutes - overallTime.Minutes)));
("time", Min.TotalMinutes - overallTime.TotalMinutes)));
return false;
}

Expand Down Expand Up @@ -424,6 +424,7 @@ public override bool IsValid(IPrototype prototype, JobPrototype job, HumanoidCha

// Get the JobPrototype of the Tracker
var trackerJob = jobSystem.GetJobPrototype(Tracker);
var jobStr = prototypeManager.Index<JobPrototype>(trackerJob).LocalizedName;

// Get the primary department of the Tracker
if (!jobSystem.TryGetPrimaryDepartment(trackerJob, out var department) &&
Expand All @@ -444,8 +445,8 @@ public override bool IsValid(IPrototype prototype, JobPrototype job, HumanoidCha
reason = Inverted
? null
: FormattedMessage.FromMarkup(Loc.GetString("character-timer-role-too-high",
("time", time.Minutes - Max.Minutes),
("job", trackerJob),
("time", time.TotalMinutes - Max.TotalMinutes),
("job", jobStr),
("departmentColor", department.Color)));
return false;
}
Expand All @@ -456,8 +457,8 @@ public override bool IsValid(IPrototype prototype, JobPrototype job, HumanoidCha
reason = Inverted
? null
: FormattedMessage.FromMarkup(Loc.GetString("character-timer-role-insufficient",
("time", Min.Minutes - time.Minutes),
("job", trackerJob),
("time", Min.TotalMinutes - time.TotalMinutes),
("job", jobStr),
("departmentColor", department.Color)));
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
using Robust.Shared.Player;

namespace Content.Shared.Players.PlayTimeTracking;

public interface ISharedPlaytimeManager
{
/// <summary>
/// Gets the playtimes for the session or an empty dictionary if none found.
/// </summary>
IReadOnlyDictionary<string, TimeSpan> GetPlayTimes(ICommonSession session);
}
public interface ISharedPlaytimeManager;

0 comments on commit 634e9e1

Please sign in to comment.