1
0

RoleTimeRequirement.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. {
  47. return true;
  48. }
  49. reason = FormattedMessage.FromMarkupPermissive(Loc.GetString(
  50. "role-timer-role-insufficient",
  51. ("time", formattedRoleDiff),
  52. ("job", indexedJob.LocalizedName),
  53. ("departmentColor", departmentColor.ToHex())));
  54. return false;
  55. }
  56. if (roleDiff <= 0)
  57. {
  58. reason = FormattedMessage.FromMarkupPermissive(Loc.GetString(
  59. "role-timer-role-too-high",
  60. ("time", formattedRoleDiff),
  61. ("job", indexedJob.LocalizedName),
  62. ("departmentColor", departmentColor.ToHex())));
  63. return false;
  64. }
  65. return true;
  66. }
  67. }