Files
tbd-station-14/Content.Server/Medical/CrewMonitoring/CrewMonitoringServerSystem.cs
metalgearsloth 63dfd21b14 Predict dumping (#32394)
* Predict dumping

- This got soaped really fucking hard.
- Dumping is predicted, this required disposals to be predicte.d
- Disposals required mailing (because it's tightly coupled), and a smidge of other content systems.
- I also had to fix a compnetworkgenerator issue at the same time so it wouldn't mispredict.

* Fix a bunch of stuff

* nasty merge

* Some reviews

* Some more reviews while I stash

* Fix merge

* Fix merge

* Half of review

* Review

* re(h)f

* lizards

* feexes

* feex
2025-04-19 16:20:40 +10:00

113 lines
3.9 KiB
C#

using Content.Server.DeviceNetwork.Systems;
using Content.Server.Medical.SuitSensors;
using Content.Shared.DeviceNetwork;
using Content.Shared.DeviceNetwork.Events;
using Content.Shared.Medical.SuitSensor;
using Robust.Shared.Timing;
using Content.Shared.DeviceNetwork.Components;
namespace Content.Server.Medical.CrewMonitoring;
public sealed class CrewMonitoringServerSystem : EntitySystem
{
[Dependency] private readonly SuitSensorSystem _sensors = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly DeviceNetworkSystem _deviceNetworkSystem = default!;
[Dependency] private readonly SingletonDeviceNetServerSystem _singletonServerSystem = default!;
private const float UpdateRate = 3f;
private float _updateDiff;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<CrewMonitoringServerComponent, ComponentRemove>(OnRemove);
SubscribeLocalEvent<CrewMonitoringServerComponent, DeviceNetworkPacketEvent>(OnPacketReceived);
SubscribeLocalEvent<CrewMonitoringServerComponent, DeviceNetServerDisconnectedEvent>(OnDisconnected);
}
public override void Update(float frameTime)
{
base.Update(frameTime);
// check update rate
_updateDiff += frameTime;
if (_updateDiff < UpdateRate)
return;
_updateDiff -= UpdateRate;
var servers = EntityQueryEnumerator<CrewMonitoringServerComponent>();
while (servers.MoveNext(out var id, out var server))
{
if (!_singletonServerSystem.IsActiveServer(id))
continue;
UpdateTimeout(id);
BroadcastSensorStatus(id, server);
}
}
/// <summary>
/// Adds or updates a sensor status entry if the received package is a sensor status update
/// </summary>
private void OnPacketReceived(EntityUid uid, CrewMonitoringServerComponent component, DeviceNetworkPacketEvent args)
{
var sensorStatus = _sensors.PacketToSuitSensor(args.Data);
if (sensorStatus == null)
return;
sensorStatus.Timestamp = _gameTiming.CurTime;
component.SensorStatus[args.SenderAddress] = sensorStatus;
}
/// <summary>
/// Clears the servers sensor status list
/// </summary>
private void OnRemove(EntityUid uid, CrewMonitoringServerComponent component, ComponentRemove args)
{
component.SensorStatus.Clear();
}
/// <summary>
/// Drop the sensor status if it hasn't been updated for to long
/// </summary>
private void UpdateTimeout(EntityUid uid, CrewMonitoringServerComponent? component = null)
{
if (!Resolve(uid, ref component))
return;
foreach (var (address, sensor) in component.SensorStatus)
{
var dif = _gameTiming.CurTime - sensor.Timestamp;
if (dif.Seconds > component.SensorTimeout)
component.SensorStatus.Remove(address);
}
}
/// <summary>
/// Broadcasts the status of all connected sensors
/// </summary>
private void BroadcastSensorStatus(EntityUid uid, CrewMonitoringServerComponent? serverComponent = null, DeviceNetworkComponent? device = null)
{
if (!Resolve(uid, ref serverComponent, ref device))
return;
var payload = new NetworkPayload()
{
[DeviceNetworkConstants.Command] = DeviceNetworkConstants.CmdUpdatedState,
[SuitSensorConstants.NET_STATUS_COLLECTION] = serverComponent.SensorStatus
};
_deviceNetworkSystem.QueuePacket(uid, null, payload, device: device);
}
/// <summary>
/// Clears sensor data on disconnect
/// </summary>
private void OnDisconnected(EntityUid uid, CrewMonitoringServerComponent component, ref DeviceNetServerDisconnectedEvent _)
{
component.SensorStatus.Clear();
}
}