Add a crew monitoring server (#7542)
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
using System.Linq;
|
||||
using Content.Server.DeviceNetwork;
|
||||
using Content.Server.DeviceNetwork.Systems;
|
||||
using Content.Server.Medical.SuitSensors;
|
||||
using Content.Server.UserInterface;
|
||||
using Content.Shared.Medical.CrewMonitoring;
|
||||
using Robust.Shared.Map;
|
||||
using Content.Shared.Medical.SuitSensor;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Server.Medical.CrewMonitoring
|
||||
@@ -13,33 +15,14 @@ namespace Content.Server.Medical.CrewMonitoring
|
||||
[Dependency] private readonly SuitSensorSystem _sensors = default!;
|
||||
[Dependency] private readonly SharedTransformSystem _xform = default!;
|
||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||
|
||||
private const float UpdateRate = 3f;
|
||||
private float _updateDif;
|
||||
[Dependency] private readonly IMapManager _mapManager = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<CrewMonitoringConsoleComponent, ComponentRemove>(OnRemove);
|
||||
SubscribeLocalEvent<CrewMonitoringConsoleComponent, DeviceNetworkPacketEvent>(OnPacketReceived);
|
||||
}
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
base.Update(frameTime);
|
||||
|
||||
// check update rate
|
||||
_updateDif += frameTime;
|
||||
if (_updateDif < UpdateRate)
|
||||
return;
|
||||
_updateDif = 0f;
|
||||
|
||||
var consoles = EntityManager.EntityQuery<CrewMonitoringConsoleComponent>();
|
||||
foreach (var console in consoles)
|
||||
{
|
||||
UpdateTimeouts(console.Owner, console);
|
||||
UpdateUserInterface(console.Owner, console);
|
||||
}
|
||||
SubscribeLocalEvent<CrewMonitoringConsoleComponent, BoundUIOpenedEvent>(OnUIOpened);
|
||||
}
|
||||
|
||||
private void OnRemove(EntityUid uid, CrewMonitoringConsoleComponent component, ComponentRemove args)
|
||||
@@ -49,12 +32,22 @@ namespace Content.Server.Medical.CrewMonitoring
|
||||
|
||||
private void OnPacketReceived(EntityUid uid, CrewMonitoringConsoleComponent component, DeviceNetworkPacketEvent args)
|
||||
{
|
||||
var suitSensor = _sensors.PacketToSuitSensor(args.Data);
|
||||
if (suitSensor == null)
|
||||
var payload = args.Data;
|
||||
// check command
|
||||
if (!payload.TryGetValue(DeviceNetworkConstants.Command, out string? command))
|
||||
return;
|
||||
if (command != DeviceNetworkConstants.CmdUpdatedState)
|
||||
return;
|
||||
if (!payload.TryGetValue(SuitSensorConstants.NET_STATUS_COLLECTION, out Dictionary<string, SuitSensorStatus>? sensorStatus))
|
||||
return;
|
||||
|
||||
suitSensor.Timestamp = _gameTiming.CurTime;
|
||||
component.ConnectedSensors[args.SenderAddress] = suitSensor;
|
||||
component.ConnectedSensors = sensorStatus;
|
||||
UpdateUserInterface(uid, component);
|
||||
}
|
||||
|
||||
private void OnUIOpened(EntityUid uid, CrewMonitoringConsoleComponent component, BoundUIOpenedEvent args)
|
||||
{
|
||||
UpdateUserInterface(uid, component);
|
||||
}
|
||||
|
||||
private void UpdateUserInterface(EntityUid uid, CrewMonitoringConsoleComponent? component = null)
|
||||
@@ -72,19 +65,5 @@ namespace Content.Server.Medical.CrewMonitoring
|
||||
var uiState = new CrewMonitoringState(allSensors, xform.WorldPosition, component.Snap, component.Precision);
|
||||
ui.SetState(uiState);
|
||||
}
|
||||
|
||||
private void UpdateTimeouts(EntityUid uid, CrewMonitoringConsoleComponent? component = null)
|
||||
{
|
||||
if (!Resolve(uid, ref component))
|
||||
return;
|
||||
|
||||
foreach (var (address, sensor) in component.ConnectedSensors)
|
||||
{
|
||||
// if too many time passed - sensor just dropped connection
|
||||
var dif = _gameTiming.CurTime - sensor.Timestamp;
|
||||
if (dif.Seconds > component.SensorTimeout)
|
||||
component.ConnectedSensors.Remove(address);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user