1
0

TotalJobsTimeRequirement.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System.Diagnostics.CodeAnalysis;
  2. using Content.Shared.Preferences;
  3. using Content.Shared.Roles;
  4. using Robust.Shared.Prototypes;
  5. using Robust.Shared.Serialization;
  6. using Robust.Shared.Utility;
  7. namespace Content.Shared._RMC14.Roles;
  8. [Serializable, NetSerializable]
  9. public sealed partial class TotalJobsTimeRequirement : JobRequirement
  10. {
  11. /// <summary>
  12. /// Which roles to add up to the required amount of time.
  13. /// </summary>
  14. [DataField(required: true)]
  15. public EntProtoId Group;
  16. /// <summary>
  17. /// How long (in seconds) this requirement is.
  18. /// </summary>
  19. [DataField(required: true)]
  20. public TimeSpan Time;
  21. public bool TryRequirementsMet(IReadOnlyDictionary<string, TimeSpan> playTimes, out FormattedMessage? reason, IEntityManager entManager, IPrototypeManager prototypes)
  22. {
  23. reason = null;
  24. var playtime = TimeSpan.Zero;
  25. var trackers = new HashSet<string>();
  26. if (!prototypes.Index(Group).TryGetComponent(out JobGroupComponent? comp))
  27. {
  28. var sawmill = Logger.GetSawmill("job.requirements");
  29. sawmill.Error($"No {nameof(JobGroupComponent)} found on entity {Group}");
  30. return true;
  31. }
  32. // Check all jobs' playtime
  33. foreach (var jobId in comp.Jobs)
  34. {
  35. // The schema is stored on the Job role but we want to explode if the timer isn't found anyway.
  36. var proto = prototypes.Index(jobId).PlayTimeTracker;
  37. trackers.Add(proto);
  38. }
  39. foreach (var tracker in trackers)
  40. {
  41. playTimes.TryGetValue(tracker, out var otherTime);
  42. playtime += otherTime;
  43. }
  44. var deptDiff = Time.TotalMinutes - playtime.TotalMinutes;
  45. if (!Inverted)
  46. {
  47. if (deptDiff <= 0)
  48. return true;
  49. reason = FormattedMessage.FromMarkupOrThrow(Loc.GetString(
  50. "role-timer-total-department-insufficient",
  51. ("time", Math.Ceiling(deptDiff)),
  52. ("roles", Loc.GetString(comp.Name)),
  53. ("rolesColor", comp.Color.ToHex())));
  54. return false;
  55. }
  56. else
  57. {
  58. if (deptDiff <= 0)
  59. {
  60. reason = FormattedMessage.FromMarkupOrThrow(Loc.GetString(
  61. "role-timer-total-department-too-high",
  62. ("time", -deptDiff),
  63. ("roles", Loc.GetString(comp.Name)),
  64. ("rolesColor", comp.Color.ToHex())));
  65. return false;
  66. }
  67. return true;
  68. }
  69. }
  70. public override bool Check(IEntityManager entManager,
  71. IPrototypeManager protoManager,
  72. HumanoidCharacterProfile? profile,
  73. IReadOnlyDictionary<string, TimeSpan> playTimes,
  74. [NotNullWhen(false)] out FormattedMessage? reason)
  75. {
  76. return TryRequirementsMet(playTimes, out reason, entManager, protoManager);
  77. }
  78. }