AgentIDCardSystem.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using Content.Server.Access.Components;
  2. using Content.Server.Popups;
  3. using Content.Shared.UserInterface;
  4. using Content.Shared.Access.Components;
  5. using Content.Shared.Access.Systems;
  6. using Content.Shared.Interaction;
  7. using Content.Shared.StatusIcon;
  8. using Robust.Server.GameObjects;
  9. using Robust.Shared.Prototypes;
  10. using Content.Shared.Roles;
  11. using System.Diagnostics.CodeAnalysis;
  12. namespace Content.Server.Access.Systems
  13. {
  14. public sealed class AgentIDCardSystem : SharedAgentIdCardSystem
  15. {
  16. [Dependency] private readonly PopupSystem _popupSystem = default!;
  17. [Dependency] private readonly IdCardSystem _cardSystem = default!;
  18. [Dependency] private readonly UserInterfaceSystem _uiSystem = default!;
  19. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  20. public override void Initialize()
  21. {
  22. base.Initialize();
  23. SubscribeLocalEvent<AgentIDCardComponent, AfterInteractEvent>(OnAfterInteract);
  24. // BUI
  25. SubscribeLocalEvent<AgentIDCardComponent, AfterActivatableUIOpenEvent>(AfterUIOpen);
  26. SubscribeLocalEvent<AgentIDCardComponent, AgentIDCardNameChangedMessage>(OnNameChanged);
  27. SubscribeLocalEvent<AgentIDCardComponent, AgentIDCardJobChangedMessage>(OnJobChanged);
  28. SubscribeLocalEvent<AgentIDCardComponent, AgentIDCardJobIconChangedMessage>(OnJobIconChanged);
  29. }
  30. private void OnAfterInteract(EntityUid uid, AgentIDCardComponent component, AfterInteractEvent args)
  31. {
  32. if (args.Target == null || !args.CanReach || !TryComp<AccessComponent>(args.Target, out var targetAccess) || !HasComp<IdCardComponent>(args.Target))
  33. return;
  34. if (!TryComp<AccessComponent>(uid, out var access) || !HasComp<IdCardComponent>(uid))
  35. return;
  36. var beforeLength = access.Tags.Count;
  37. access.Tags.UnionWith(targetAccess.Tags);
  38. var addedLength = access.Tags.Count - beforeLength;
  39. if (addedLength == 0)
  40. {
  41. _popupSystem.PopupEntity(Loc.GetString("agent-id-no-new", ("card", args.Target)), args.Target.Value, args.User);
  42. return;
  43. }
  44. Dirty(uid, access);
  45. if (addedLength == 1)
  46. {
  47. _popupSystem.PopupEntity(Loc.GetString("agent-id-new-1", ("card", args.Target)), args.Target.Value, args.User);
  48. return;
  49. }
  50. _popupSystem.PopupEntity(Loc.GetString("agent-id-new", ("number", addedLength), ("card", args.Target)), args.Target.Value, args.User);
  51. }
  52. private void AfterUIOpen(EntityUid uid, AgentIDCardComponent component, AfterActivatableUIOpenEvent args)
  53. {
  54. if (!_uiSystem.HasUi(uid, AgentIDCardUiKey.Key))
  55. return;
  56. if (!TryComp<IdCardComponent>(uid, out var idCard))
  57. return;
  58. var state = new AgentIDCardBoundUserInterfaceState(idCard.FullName ?? "", idCard.LocalizedJobTitle ?? "", idCard.JobIcon);
  59. _uiSystem.SetUiState(uid, AgentIDCardUiKey.Key, state);
  60. }
  61. private void OnJobChanged(EntityUid uid, AgentIDCardComponent comp, AgentIDCardJobChangedMessage args)
  62. {
  63. if (!TryComp<IdCardComponent>(uid, out var idCard))
  64. return;
  65. _cardSystem.TryChangeJobTitle(uid, args.Job, idCard);
  66. }
  67. private void OnNameChanged(EntityUid uid, AgentIDCardComponent comp, AgentIDCardNameChangedMessage args)
  68. {
  69. if (!TryComp<IdCardComponent>(uid, out var idCard))
  70. return;
  71. _cardSystem.TryChangeFullName(uid, args.Name, idCard);
  72. }
  73. private void OnJobIconChanged(EntityUid uid, AgentIDCardComponent comp, AgentIDCardJobIconChangedMessage args)
  74. {
  75. if (!TryComp<IdCardComponent>(uid, out var idCard))
  76. return;
  77. if (!_prototypeManager.TryIndex(args.JobIconId, out var jobIcon))
  78. return;
  79. _cardSystem.TryChangeJobIcon(uid, jobIcon, idCard);
  80. if (TryFindJobProtoFromIcon(jobIcon, out var job))
  81. _cardSystem.TryChangeJobDepartment(uid, job, idCard);
  82. }
  83. private bool TryFindJobProtoFromIcon(JobIconPrototype jobIcon, [NotNullWhen(true)] out JobPrototype? job)
  84. {
  85. foreach (var jobPrototype in _prototypeManager.EnumeratePrototypes<JobPrototype>())
  86. {
  87. if (jobPrototype.Icon == jobIcon.ID)
  88. {
  89. job = jobPrototype;
  90. return true;
  91. }
  92. }
  93. job = null;
  94. return false;
  95. }
  96. }
  97. }