Files
OldThink/Content.Server/Power/EntitySystems/PowerMonitoringConsoleSystem.cs

111 lines
4.0 KiB
C#
Raw Normal View History

2022-05-04 18:59:40 +01:00
using Content.Shared.Power;
using Content.Server.NodeContainer;
2023-06-28 14:28:38 +03:00
using Content.Server.NodeContainer.EntitySystems;
2022-05-04 18:59:40 +01:00
using Content.Server.NodeContainer.Nodes;
using Content.Server.Power.Components;
using Content.Server.Power.NodeGroups;
using JetBrains.Annotations;
using Robust.Server.GameObjects;
namespace Content.Server.Power.EntitySystems;
[UsedImplicitly]
internal sealed class PowerMonitoringConsoleSystem : EntitySystem
{
private float _updateTimer = 0.0f;
private const float UpdateTime = 1.0f;
2023-06-28 14:28:38 +03:00
[Dependency] private readonly NodeContainerSystem _nodeContainer = default!;
2023-07-08 09:02:17 -07:00
[Dependency] private readonly UserInterfaceSystem _userInterfaceSystem = default!;
2022-05-04 18:59:40 +01:00
public override void Update(float frameTime)
{
_updateTimer += frameTime;
if (_updateTimer >= UpdateTime)
{
_updateTimer -= UpdateTime;
2023-07-08 09:02:17 -07:00
var query = EntityQueryEnumerator<PowerMonitoringConsoleComponent>();
while (query.MoveNext(out var uid, out var component))
2022-05-04 18:59:40 +01:00
{
2023-07-08 09:02:17 -07:00
UpdateUIState(uid, component);
2022-05-04 18:59:40 +01:00
}
}
}
public void UpdateUIState(EntityUid target, PowerMonitoringConsoleComponent? pmcComp = null, NodeContainerComponent? ncComp = null)
{
if (!Resolve(target, ref pmcComp))
return;
if (!Resolve(target, ref ncComp))
return;
var totalSources = 0.0d;
var totalLoads = 0.0d;
var sources = new List<PowerMonitoringConsoleEntry>();
var loads = new List<PowerMonitoringConsoleEntry>();
PowerMonitoringConsoleEntry LoadOrSource(Component comp, double rate, bool isBattery)
{
var md = MetaData(comp.Owner);
var prototype = md.EntityPrototype?.ID ?? "";
return new PowerMonitoringConsoleEntry(md.EntityName, prototype, rate, isBattery);
}
// Right, so, here's what needs to be considered here.
2023-06-28 14:28:38 +03:00
if (!_nodeContainer.TryGetNode<Node>(ncComp, "hv", out var node))
return;
2023-07-08 09:02:17 -07:00
if (node.NodeGroup is PowerNet netQ)
2022-05-04 18:59:40 +01:00
{
foreach (PowerConsumerComponent pcc in netQ.Consumers)
2022-05-04 18:59:40 +01:00
{
if (!pcc.ShowInMonitor)
continue;
2022-05-04 18:59:40 +01:00
loads.Add(LoadOrSource(pcc, pcc.DrawRate, false));
totalLoads += pcc.DrawRate;
}
foreach (BatteryChargerComponent pcc in netQ.Chargers)
2022-05-04 18:59:40 +01:00
{
if (!TryComp(pcc.Owner, out PowerNetworkBatteryComponent? batteryComp))
{
continue;
}
var rate = batteryComp.NetworkBattery.CurrentReceiving;
loads.Add(LoadOrSource(pcc, rate, true));
totalLoads += rate;
}
foreach (PowerSupplierComponent pcc in netQ.Suppliers)
2022-05-04 18:59:40 +01:00
{
var supply = pcc.Enabled
? pcc.MaxSupply
: 0f;
sources.Add(LoadOrSource(pcc, supply, false));
totalSources += supply;
2022-05-04 18:59:40 +01:00
}
foreach (BatteryDischargerComponent pcc in netQ.Dischargers)
2022-05-04 18:59:40 +01:00
{
if (!TryComp(pcc.Owner, out PowerNetworkBatteryComponent? batteryComp))
{
continue;
}
var rate = batteryComp.NetworkBattery.CurrentSupply;
sources.Add(LoadOrSource(pcc, rate, true));
totalSources += rate;
}
}
// Sort
loads.Sort(CompareLoadOrSources);
sources.Sort(CompareLoadOrSources);
2023-07-08 09:02:17 -07:00
2022-05-04 18:59:40 +01:00
// Actually set state.
2023-07-08 09:02:17 -07:00
if (_userInterfaceSystem.TryGetUi(target, PowerMonitoringConsoleUiKey.Key, out var bui))
UserInterfaceSystem.SetUiState(bui, new PowerMonitoringConsoleBoundInterfaceState(totalSources, totalLoads, sources.ToArray(), loads.ToArray()));
2022-05-04 18:59:40 +01:00
}
private int CompareLoadOrSources(PowerMonitoringConsoleEntry x, PowerMonitoringConsoleEntry y)
{
return -x.Size.CompareTo(y.Size);
}
}