1
0

DepartmentTimeRequirement.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System.Diagnostics.CodeAnalysis;
  2. using Content.Shared.Localizations;
  3. using Content.Shared.Preferences;
  4. using JetBrains.Annotations;
  5. using Robust.Shared.Prototypes;
  6. using Robust.Shared.Serialization;
  7. using Robust.Shared.Utility;
  8. namespace Content.Shared.Roles;
  9. [UsedImplicitly]
  10. [Serializable, NetSerializable]
  11. public sealed partial class DepartmentTimeRequirement : JobRequirement
  12. {
  13. /// <summary>
  14. /// Which department needs the required amount of time.
  15. /// </summary>
  16. [DataField(required: true)]
  17. public ProtoId<DepartmentPrototype> Department;
  18. /// <summary>
  19. /// How long (in seconds) this requirement is.
  20. /// </summary>
  21. [DataField(required: true)]
  22. public TimeSpan Time;
  23. public override bool Check(IEntityManager entManager,
  24. IPrototypeManager protoManager,
  25. HumanoidCharacterProfile? profile,
  26. IReadOnlyDictionary<string, TimeSpan> playTimes,
  27. [NotNullWhen(false)] out FormattedMessage? reason)
  28. {
  29. reason = new FormattedMessage();
  30. var playtime = TimeSpan.Zero;
  31. // Check all jobs' departments
  32. var department = protoManager.Index(Department);
  33. var jobs = department.Roles;
  34. string proto;
  35. // Check all jobs' playtime
  36. foreach (var other in jobs)
  37. {
  38. // The schema is stored on the Job role but we want to explode if the timer isn't found anyway.
  39. proto = protoManager.Index(other).PlayTimeTracker;
  40. playTimes.TryGetValue(proto, out var otherTime);
  41. playtime += otherTime;
  42. }
  43. var deptDiffSpan = Time - playtime;
  44. var deptDiff = deptDiffSpan.TotalMinutes;
  45. var formattedDeptDiff = ContentLocalizationManager.FormatPlaytime(deptDiffSpan);
  46. var nameDepartment = "role-timer-department-unknown";
  47. if (protoManager.TryIndex(Department, out var departmentIndexed))
  48. {
  49. nameDepartment = departmentIndexed.Name;
  50. }
  51. if (!Inverted)
  52. {
  53. if (deptDiff <= 0)
  54. return true;
  55. reason = FormattedMessage.FromMarkupPermissive(Loc.GetString(
  56. "role-timer-department-insufficient",
  57. ("time", formattedDeptDiff),
  58. ("department", Loc.GetString(nameDepartment)),
  59. ("departmentColor", department.Color.ToHex())));
  60. return false;
  61. }
  62. if (deptDiff <= 0)
  63. {
  64. reason = FormattedMessage.FromMarkupPermissive(Loc.GetString(
  65. "role-timer-department-too-high",
  66. ("time", formattedDeptDiff),
  67. ("department", Loc.GetString(nameDepartment)),
  68. ("departmentColor", department.Color.ToHex())));
  69. return false;
  70. }
  71. return true;
  72. }
  73. }