1
0

MailingUnitSystem.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using Content.Server.Configurable;
  2. using Content.Server.DeviceNetwork;
  3. using Content.Server.DeviceNetwork.Components;
  4. using Content.Server.DeviceNetwork.Systems;
  5. using Content.Server.Disposal.Unit.EntitySystems;
  6. using Content.Server.Power.Components;
  7. using Content.Shared.DeviceNetwork;
  8. using Content.Shared.Disposal;
  9. using Content.Shared.Interaction;
  10. using Robust.Server.GameObjects;
  11. using Robust.Shared.Player;
  12. using Robust.Shared.Utility;
  13. namespace Content.Server.Disposal.Mailing;
  14. public sealed class MailingUnitSystem : EntitySystem
  15. {
  16. [Dependency] private readonly DeviceNetworkSystem _deviceNetworkSystem = default!;
  17. [Dependency] private readonly UserInterfaceSystem _userInterfaceSystem = default!;
  18. private const string MailTag = "mail";
  19. private const string TagConfigurationKey = "tag";
  20. private const string NetTag = "tag";
  21. private const string NetSrc = "src";
  22. private const string NetTarget = "target";
  23. private const string NetCmdSent = "mail_sent";
  24. private const string NetCmdRequest = "get_mailer_tag";
  25. private const string NetCmdResponse = "mailer_tag";
  26. public override void Initialize()
  27. {
  28. base.Initialize();
  29. SubscribeLocalEvent<MailingUnitComponent, ComponentInit>(OnComponentInit);
  30. SubscribeLocalEvent<MailingUnitComponent, DeviceNetworkPacketEvent>(OnPacketReceived);
  31. SubscribeLocalEvent<MailingUnitComponent, BeforeDisposalFlushEvent>(OnBeforeFlush);
  32. SubscribeLocalEvent<MailingUnitComponent, ConfigurationSystem.ConfigurationUpdatedEvent>(OnConfigurationUpdated);
  33. SubscribeLocalEvent<MailingUnitComponent, ActivateInWorldEvent>(HandleActivate, before: new[] { typeof(DisposalUnitSystem) });
  34. SubscribeLocalEvent<MailingUnitComponent, DisposalUnitUIStateUpdatedEvent>(OnDisposalUnitUIStateChange);
  35. SubscribeLocalEvent<MailingUnitComponent, TargetSelectedMessage>(OnTargetSelected);
  36. }
  37. private void OnComponentInit(EntityUid uid, MailingUnitComponent component, ComponentInit args)
  38. {
  39. UpdateTargetList(uid, component);
  40. }
  41. private void OnPacketReceived(EntityUid uid, MailingUnitComponent component, DeviceNetworkPacketEvent args)
  42. {
  43. if (!args.Data.TryGetValue(DeviceNetworkConstants.Command, out string? command) || !IsPowered(uid))
  44. return;
  45. switch (command)
  46. {
  47. case NetCmdRequest:
  48. SendTagRequestResponse(uid, args, component.Tag);
  49. break;
  50. case NetCmdResponse when args.Data.TryGetValue(NetTag, out string? tag):
  51. //Add the received tag request response to the list of targets
  52. component.TargetList.Add(tag);
  53. UpdateUserInterface(uid, component);
  54. break;
  55. }
  56. }
  57. /// <summary>
  58. /// Sends the given tag as a response to a <see cref="NetCmdRequest"/> if it's not null
  59. /// </summary>
  60. private void SendTagRequestResponse(EntityUid uid, DeviceNetworkPacketEvent args, string? tag)
  61. {
  62. if (tag == null)
  63. return;
  64. var payload = new NetworkPayload
  65. {
  66. [DeviceNetworkConstants.Command] = NetCmdResponse,
  67. [NetTag] = tag
  68. };
  69. _deviceNetworkSystem.QueuePacket(uid, args.Address, payload, args.Frequency);
  70. }
  71. /// <summary>
  72. /// Prevents the unit from flushing if no target is selected
  73. /// </summary>
  74. private void OnBeforeFlush(EntityUid uid, MailingUnitComponent component, BeforeDisposalFlushEvent args)
  75. {
  76. if (string.IsNullOrEmpty(component.Target))
  77. {
  78. args.Cancel();
  79. return;
  80. }
  81. args.Tags.Add(MailTag);
  82. args.Tags.Add(component.Target);
  83. BroadcastSentMessage(uid, component);
  84. }
  85. /// <summary>
  86. /// Broadcast that a mail was sent including the src and target tags
  87. /// </summary>
  88. private void BroadcastSentMessage(EntityUid uid, MailingUnitComponent component, DeviceNetworkComponent? device = null)
  89. {
  90. if (string.IsNullOrEmpty(component.Tag) || string.IsNullOrEmpty(component.Target) || !Resolve(uid, ref device))
  91. return;
  92. var payload = new NetworkPayload
  93. {
  94. [DeviceNetworkConstants.Command] = NetCmdSent,
  95. [NetSrc] = component.Tag,
  96. [NetTarget] = component.Target
  97. };
  98. _deviceNetworkSystem.QueuePacket(uid, null, payload, null, null, device);
  99. }
  100. /// <summary>
  101. /// Clears the units target list and broadcasts a <see cref="NetCmdRequest"/>.
  102. /// The target list will then get populated with <see cref="NetCmdResponse"/> responses from all active mailing units on the same grid
  103. /// </summary>
  104. private void UpdateTargetList(EntityUid uid, MailingUnitComponent component, DeviceNetworkComponent? device = null)
  105. {
  106. if (!Resolve(uid, ref device, false))
  107. return;
  108. var payload = new NetworkPayload
  109. {
  110. [DeviceNetworkConstants.Command] = NetCmdRequest
  111. };
  112. component.TargetList.Clear();
  113. _deviceNetworkSystem.QueuePacket(uid, null, payload, null, null, device);
  114. }
  115. /// <summary>
  116. /// Gets called when the units tag got updated
  117. /// </summary>
  118. private void OnConfigurationUpdated(EntityUid uid, MailingUnitComponent component, ConfigurationSystem.ConfigurationUpdatedEvent args)
  119. {
  120. var configuration = args.Configuration.Config;
  121. if (!configuration.ContainsKey(TagConfigurationKey) || configuration[TagConfigurationKey] == string.Empty)
  122. {
  123. component.Tag = null;
  124. return;
  125. }
  126. component.Tag = configuration[TagConfigurationKey];
  127. UpdateUserInterface(uid, component);
  128. }
  129. private void HandleActivate(EntityUid uid, MailingUnitComponent component, ActivateInWorldEvent args)
  130. {
  131. if (args.Handled || !args.Complex)
  132. return;
  133. if (!EntityManager.TryGetComponent(args.User, out ActorComponent? actor))
  134. {
  135. return;
  136. }
  137. args.Handled = true;
  138. UpdateTargetList(uid, component);
  139. _userInterfaceSystem.OpenUi(uid, MailingUnitUiKey.Key, actor.PlayerSession);
  140. }
  141. /// <summary>
  142. /// Gets called when the disposal unit components ui state changes. This is required because the mailing unit requires a disposal unit component and overrides its ui
  143. /// </summary>
  144. private void OnDisposalUnitUIStateChange(EntityUid uid, MailingUnitComponent component, DisposalUnitUIStateUpdatedEvent args)
  145. {
  146. component.DisposalUnitInterfaceState = args.State;
  147. UpdateUserInterface(uid, component);
  148. }
  149. private void UpdateUserInterface(EntityUid uid, MailingUnitComponent component)
  150. {
  151. if (component.DisposalUnitInterfaceState == null)
  152. return;
  153. var state = new MailingUnitBoundUserInterfaceState(component.DisposalUnitInterfaceState, component.Target, component.TargetList.ShallowClone(), component.Tag);
  154. _userInterfaceSystem.SetUiState(uid, MailingUnitUiKey.Key, state);
  155. }
  156. private void OnTargetSelected(EntityUid uid, MailingUnitComponent component, TargetSelectedMessage args)
  157. {
  158. component.Target = args.Target;
  159. UpdateUserInterface(uid, component);
  160. }
  161. /// <summary>
  162. /// Checks if the unit is powered if an <see cref="ApcPowerReceiverComponent"/> is present
  163. /// </summary>
  164. /// <returns>True if the power receiver component is powered or not present</returns>
  165. private bool IsPowered(EntityUid uid, ApcPowerReceiverComponent? powerReceiver = null)
  166. {
  167. if (Resolve(uid, ref powerReceiver) && !powerReceiver.Powered)
  168. return false;
  169. return true;
  170. }
  171. }