Make role timer tooltips pretty (#19605)

Co-authored-by: ElectroJr <leonsfriedrich@gmail.com>
This commit is contained in:
metalgearsloth
2023-09-11 15:44:21 +10:00
committed by GitHub
parent 2a367af7ed
commit b77265314b
9 changed files with 141 additions and 41 deletions

View File

@@ -1,8 +1,10 @@
using System.Diagnostics.CodeAnalysis;
using Content.Shared.Players.PlayTimeTracking;
using Content.Shared.Roles.Jobs;
using JetBrains.Annotations;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
using Robust.Shared.Utility;
namespace Content.Shared.Roles
{
@@ -67,7 +69,8 @@ namespace Content.Shared.Roles
public static bool TryRequirementsMet(
JobPrototype job,
Dictionary<string, TimeSpan> playTimes,
[NotNullWhen(false)] out string? reason,
[NotNullWhen(false)] out FormattedMessage? reason,
IEntityManager entManager,
IPrototypeManager prototypes)
{
reason = null;
@@ -76,7 +79,7 @@ namespace Content.Shared.Roles
foreach (var requirement in job.Requirements)
{
if (!TryRequirementMet(requirement, playTimes, out reason, prototypes))
if (!TryRequirementMet(requirement, playTimes, out reason, entManager, prototypes))
return false;
}
@@ -89,9 +92,9 @@ namespace Content.Shared.Roles
public static bool TryRequirementMet(
JobRequirement requirement,
Dictionary<string, TimeSpan> playTimes,
[NotNullWhen(false)] out string? reason,
[NotNullWhen(false)] out FormattedMessage? reason,
IEntityManager entManager,
IPrototypeManager prototypes)
{
reason = null;
@@ -101,7 +104,8 @@ namespace Content.Shared.Roles
var playtime = TimeSpan.Zero;
// Check all jobs' departments
var jobs = prototypes.Index<DepartmentPrototype>(deptRequirement.Department).Roles;
var department = prototypes.Index<DepartmentPrototype>(deptRequirement.Department);
var jobs = department.Roles;
string proto;
// Check all jobs' playtime
@@ -121,20 +125,22 @@ namespace Content.Shared.Roles
if (deptDiff <= 0)
return true;
reason = Loc.GetString(
reason = FormattedMessage.FromMarkup(Loc.GetString(
"role-timer-department-insufficient",
("time", deptDiff),
("department", Loc.GetString(deptRequirement.Department)));
("department", Loc.GetString(deptRequirement.Department)),
("departmentColor", department.Color.ToHex())));
return false;
}
else
{
if (deptDiff <= 0)
{
reason = Loc.GetString(
reason = FormattedMessage.FromMarkup(Loc.GetString(
"role-timer-department-too-high",
("time", -deptDiff),
("department", Loc.GetString(deptRequirement.Department)));
("department", Loc.GetString(deptRequirement.Department)),
("departmentColor", department.Color.ToHex())));
return false;
}
@@ -150,14 +156,14 @@ namespace Content.Shared.Roles
if (overallDiff <= 0 || overallTime >= overallRequirement.Time)
return true;
reason = Loc.GetString("role-timer-overall-insufficient", ("time", overallDiff));
reason = FormattedMessage.FromMarkup(Loc.GetString("role-timer-overall-insufficient", ("time", overallDiff)));
return false;
}
else
{
if (overallDiff <= 0 || overallTime >= overallRequirement.Time)
{
reason = Loc.GetString("role-timer-overall-too-high", ("time", -overallDiff));
reason = FormattedMessage.FromMarkup(Loc.GetString("role-timer-overall-too-high", ("time", -overallDiff)));
return false;
}
@@ -169,26 +175,37 @@ namespace Content.Shared.Roles
playTimes.TryGetValue(proto, out var roleTime);
var roleDiff = roleRequirement.Time.TotalMinutes - roleTime.TotalMinutes;
var departmentColor = Color.Yellow;
if (entManager.EntitySysManager.TryGetEntitySystem(out SharedJobSystem? jobSystem))
{
var jobProto = jobSystem.GetJobPrototype(proto);
if (jobSystem.TryGetDepartment(jobProto, out var departmentProto))
departmentColor = departmentProto.Color;
}
if (!roleRequirement.Inverted)
{
if (roleDiff <= 0)
return true;
reason = Loc.GetString(
reason = FormattedMessage.FromMarkup(Loc.GetString(
"role-timer-role-insufficient",
("time", roleDiff),
("job", Loc.GetString(proto)));
("job", Loc.GetString(proto)),
("departmentColor", departmentColor.ToHex())));
return false;
}
else
{
if (roleDiff <= 0)
{
reason = Loc.GetString(
reason = FormattedMessage.FromMarkup(Loc.GetString(
"role-timer-role-too-high",
("time", -roleDiff),
("job", Loc.GetString(proto)));
("job", Loc.GetString(proto)),
("departmentColor", departmentColor.ToHex())));
return false;
}

View File

@@ -1,7 +1,10 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Content.Shared.Players;
using Content.Shared.Players.PlayTimeTracking;
using Robust.Shared.Players;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
namespace Content.Shared.Roles.Jobs;
@@ -13,6 +16,71 @@ public abstract class SharedJobSystem : EntitySystem
[Dependency] private readonly IPrototypeManager _prototypes = default!;
[Dependency] private readonly SharedPlayerSystem _playerSystem = default!;
[Dependency] private readonly IPrototypeManager _protoManager = default!;
private readonly Dictionary<string, string> _inverseTrackerLookup = new();
public override void Initialize()
{
base.Initialize();
_protoManager.PrototypesReloaded += OnProtoReload;
SetupTrackerLookup();
}
public override void Shutdown()
{
base.Shutdown();
_protoManager.PrototypesReloaded -= OnProtoReload;
_inverseTrackerLookup.Clear();
}
private void OnProtoReload(PrototypesReloadedEventArgs obj)
{
_inverseTrackerLookup.Clear();
SetupTrackerLookup();
}
private void SetupTrackerLookup()
{
// This breaks if you have N trackers to 1 JobId but future concern.
foreach (var job in _protoManager.EnumeratePrototypes<JobPrototype>())
{
_inverseTrackerLookup.Add(job.PlayTimeTracker, job.ID);
}
}
/// <summary>
/// Gets the corresponding Job Prototype to a <see cref="PlayTimeTrackerPrototype"/>
/// </summary>
/// <param name="trackerProto"></param>
/// <returns></returns>
public string GetJobPrototype(string trackerProto)
{
DebugTools.Assert(_protoManager.HasIndex<PlayTimeTrackerPrototype>(trackerProto));
return _inverseTrackerLookup[trackerProto];
}
/// <summary>
/// Tries to get the first corresponding department for this job prototype.
/// </summary>
public bool TryGetDepartment(string jobProto, [NotNullWhen(true)] out DepartmentPrototype? departmentPrototype)
{
// Not that many departments so we can just eat the cost instead of storing the inverse lookup.
var departmentProtos = _protoManager.EnumeratePrototypes<DepartmentPrototype>().ToList();
departmentProtos.Sort((x, y) => string.Compare(x.ID, y.ID, StringComparison.Ordinal));
foreach (var department in departmentProtos)
{
if (department.Roles.Contains(jobProto))
{
departmentPrototype = department;
return true;
}
}
departmentPrototype = null;
return false;
}
public bool MindHasJobWithId(EntityUid? mindId, string prototypeId)
{
return CompOrNull<JobComponent>(mindId)?.PrototypeId == prototypeId;