ApcNet.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System.Linq;
  2. using Content.Server.NodeContainer.NodeGroups;
  3. using Content.Server.NodeContainer.Nodes;
  4. using Content.Server.Power.Components;
  5. using Content.Server.Power.EntitySystems;
  6. using JetBrains.Annotations;
  7. namespace Content.Server.Power.NodeGroups
  8. {
  9. public interface IApcNet : IBasePowerNet
  10. {
  11. void AddApc(EntityUid uid, ApcComponent apc);
  12. void RemoveApc(EntityUid uid, ApcComponent apc);
  13. void AddPowerProvider(ApcPowerProviderComponent provider);
  14. void RemovePowerProvider(ApcPowerProviderComponent provider);
  15. void QueueNetworkReconnect();
  16. }
  17. [NodeGroup(NodeGroupID.Apc)]
  18. [UsedImplicitly]
  19. public sealed partial class ApcNet : BasePowerNet<IApcNet>, IApcNet
  20. {
  21. [ViewVariables] public readonly List<ApcComponent> Apcs = new();
  22. [ViewVariables] public readonly List<ApcPowerProviderComponent> Providers = new();
  23. //Debug property
  24. [ViewVariables] private int TotalReceivers => Providers.Sum(provider => provider.LinkedReceivers.Count);
  25. [ViewVariables]
  26. private IEnumerable<ApcPowerReceiverComponent> AllReceivers =>
  27. Providers.SelectMany(provider => provider.LinkedReceivers);
  28. public override void Initialize(Node sourceNode, IEntityManager entMan)
  29. {
  30. base.Initialize(sourceNode, entMan);
  31. PowerNetSystem.InitApcNet(this);
  32. }
  33. public override void AfterRemake(IEnumerable<IGrouping<INodeGroup?, Node>> newGroups)
  34. {
  35. base.AfterRemake(newGroups);
  36. PowerNetSystem?.DestroyApcNet(this);
  37. }
  38. public void AddApc(EntityUid uid, ApcComponent apc)
  39. {
  40. if (EntMan.TryGetComponent(uid, out PowerNetworkBatteryComponent? netBattery))
  41. netBattery.NetworkBattery.LinkedNetworkDischarging = default;
  42. QueueNetworkReconnect();
  43. Apcs.Add(apc);
  44. }
  45. public void RemoveApc(EntityUid uid, ApcComponent apc)
  46. {
  47. if (EntMan.TryGetComponent(uid, out PowerNetworkBatteryComponent? netBattery))
  48. netBattery.NetworkBattery.LinkedNetworkDischarging = default;
  49. QueueNetworkReconnect();
  50. Apcs.Remove(apc);
  51. }
  52. public void AddPowerProvider(ApcPowerProviderComponent provider)
  53. {
  54. Providers.Add(provider);
  55. QueueNetworkReconnect();
  56. }
  57. public void RemovePowerProvider(ApcPowerProviderComponent provider)
  58. {
  59. Providers.Remove(provider);
  60. QueueNetworkReconnect();
  61. }
  62. public override void QueueNetworkReconnect()
  63. {
  64. PowerNetSystem?.QueueReconnectApcNet(this);
  65. }
  66. protected override void SetNetConnectorNet(IBaseNetConnectorComponent<IApcNet> netConnectorComponent)
  67. {
  68. netConnectorComponent.Net = this;
  69. }
  70. public override string? GetDebugData()
  71. {
  72. // This is just recycling the multi-tool examine.
  73. var ps = PowerNetSystem.GetNetworkStatistics(NetworkNode);
  74. float storageRatio = ps.InStorageCurrent / Math.Max(ps.InStorageMax, 1.0f);
  75. float outStorageRatio = ps.OutStorageCurrent / Math.Max(ps.OutStorageMax, 1.0f);
  76. return @$"Current Supply: {ps.SupplyCurrent:G3}
  77. From Batteries: {ps.SupplyBatteries:G3}
  78. Theoretical Supply: {ps.SupplyTheoretical:G3}
  79. Ideal Consumption: {ps.Consumption:G3}
  80. Input Storage: {ps.InStorageCurrent:G3} / {ps.InStorageMax:G3} ({storageRatio:P1})
  81. Output Storage: {ps.OutStorageCurrent:G3} / {ps.OutStorageMax:G3} ({outStorageRatio:P1})";
  82. }
  83. }
  84. }