1
0

TraitsRequirement.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System.Diagnostics.CodeAnalysis;
  2. using System.Text;
  3. using Content.Shared.Humanoid.Prototypes;
  4. using Content.Shared.Preferences;
  5. using Content.Shared.Traits;
  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. /// <summary>
  12. /// Requires a character to have, or not have, certain traits
  13. /// </summary>
  14. [UsedImplicitly]
  15. [Serializable, NetSerializable]
  16. public sealed partial class TraitsRequirement : JobRequirement
  17. {
  18. [DataField(required: true)]
  19. public HashSet<ProtoId<TraitPrototype>> Traits = new();
  20. public override bool Check(IEntityManager entManager,
  21. IPrototypeManager protoManager,
  22. HumanoidCharacterProfile? profile,
  23. IReadOnlyDictionary<string, TimeSpan> playTimes,
  24. [NotNullWhen(false)] out FormattedMessage? reason)
  25. {
  26. reason = new FormattedMessage();
  27. if (profile is null) //the profile could be null if the player is a ghost. In this case we don't need to block the role selection for ghostrole
  28. return true;
  29. var sb = new StringBuilder();
  30. sb.Append("[color=yellow]");
  31. foreach (var t in Traits)
  32. {
  33. sb.Append(Loc.GetString(protoManager.Index(t).Name) + " ");
  34. }
  35. sb.Append("[/color]");
  36. if (!Inverted)
  37. {
  38. reason = FormattedMessage.FromMarkupPermissive($"{Loc.GetString("role-timer-whitelisted-traits")}\n{sb}");
  39. //at least one of
  40. foreach (var trait in Traits)
  41. {
  42. if (profile.TraitPreferences.Contains(trait))
  43. return true;
  44. }
  45. return false;
  46. }
  47. else
  48. {
  49. reason = FormattedMessage.FromMarkupPermissive($"{Loc.GetString("role-timer-blacklisted-traits")}\n{sb}");
  50. foreach (var trait in Traits)
  51. {
  52. if (profile.TraitPreferences.Contains(trait))
  53. return false;
  54. }
  55. }
  56. return true;
  57. }
  58. }