RoleTimeRequirement.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System.Diagnostics.CodeAnalysis;
  2. using Content.Shared.Localizations;
  3. using Content.Shared.Players.PlayTimeTracking;
  4. using Content.Shared.Preferences;
  5. using Content.Shared.Roles.Jobs;
  6. using JetBrains.Annotations;
  7. using Robust.Shared.Prototypes;
  8. using Robust.Shared.Serialization;
  9. using Robust.Shared.Utility;
  10. namespace Content.Shared.Roles;
  11. [UsedImplicitly]
  12. [Serializable, NetSerializable]
  13. public sealed partial class RoleTimeRequirement : JobRequirement
  14. {
  15. /// <summary>
  16. /// What particular role they need the time requirement with.
  17. /// </summary>
  18. [DataField(required: true)]
  19. public ProtoId<PlayTimeTrackerPrototype> Role;
  20. /// <inheritdoc cref="DepartmentTimeRequirement.Time"/>
  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. string proto = Role;
  31. playTimes.TryGetValue(proto, out var roleTime);
  32. var roleDiffSpan = Time - roleTime;
  33. var roleDiff = roleDiffSpan.TotalMinutes;
  34. var formattedRoleDiff = ContentLocalizationManager.FormatPlaytime(roleDiffSpan);
  35. var departmentColor = Color.Yellow;
  36. if (!entManager.EntitySysManager.TryGetEntitySystem(out SharedJobSystem? jobSystem))
  37. return false;
  38. var jobProto = jobSystem.GetJobPrototype(proto);
  39. if (jobSystem.TryGetDepartment(jobProto, out var departmentProto))
  40. departmentColor = departmentProto.Color;
  41. if (!protoManager.TryIndex<JobPrototype>(jobProto, out var indexedJob))
  42. return false;
  43. if (!Inverted)
  44. {
  45. if (roleDiff <= 0)
  46. return true;
  47. reason = FormattedMessage.FromMarkupPermissive(Loc.GetString(
  48. "role-timer-role-insufficient",
  49. ("time", formattedRoleDiff),
  50. ("job", indexedJob.LocalizedName),
  51. ("departmentColor", departmentColor.ToHex())));
  52. return false;
  53. }
  54. if (roleDiff <= 0)
  55. {
  56. reason = FormattedMessage.FromMarkupPermissive(Loc.GetString(
  57. "role-timer-role-too-high",
  58. ("time", formattedRoleDiff),
  59. ("job", indexedJob.LocalizedName),
  60. ("departmentColor", departmentColor.ToHex())));
  61. return false;
  62. }
  63. return true;
  64. }
  65. }