System for single device net server per station functionality (#23946)
This commit is contained in:
@@ -2,8 +2,6 @@
|
||||
using Content.Server.DeviceNetwork.Components;
|
||||
using Content.Server.DeviceNetwork.Systems;
|
||||
using Content.Server.Medical.SuitSensors;
|
||||
using Content.Server.Power.Components;
|
||||
using Content.Server.Station.Systems;
|
||||
using Content.Shared.DeviceNetwork;
|
||||
using Content.Shared.Medical.SuitSensor;
|
||||
using Robust.Shared.Timing;
|
||||
@@ -15,7 +13,7 @@ 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 StationSystem _stationSystem = default!;
|
||||
[Dependency] private readonly SingletonDeviceNetServerSystem _singletonServerSystem = default!;
|
||||
|
||||
private const float UpdateRate = 3f;
|
||||
private float _updateDiff;
|
||||
@@ -25,7 +23,7 @@ public sealed class CrewMonitoringServerSystem : EntitySystem
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<CrewMonitoringServerComponent, ComponentRemove>(OnRemove);
|
||||
SubscribeLocalEvent<CrewMonitoringServerComponent, DeviceNetworkPacketEvent>(OnPacketReceived);
|
||||
SubscribeLocalEvent<CrewMonitoringServerComponent, PowerChangedEvent>(OnPowerChanged);
|
||||
SubscribeLocalEvent<CrewMonitoringServerComponent, DeviceNetServerDisconnectedEvent>(OnDisconnected);
|
||||
}
|
||||
|
||||
public override void Update(float frameTime)
|
||||
@@ -39,70 +37,15 @@ public sealed class CrewMonitoringServerSystem : EntitySystem
|
||||
_updateDiff -= UpdateRate;
|
||||
|
||||
var servers = EntityQueryEnumerator<CrewMonitoringServerComponent>();
|
||||
List<EntityUid> activeServers = new();
|
||||
|
||||
while (servers.MoveNext(out var id, out var server))
|
||||
{
|
||||
//Make sure the server is disconnected when it becomes unavailable
|
||||
if (!server.Available)
|
||||
{
|
||||
if (server.Active)
|
||||
DisconnectServer(id, server);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!server.Active)
|
||||
if (!_singletonServerSystem.IsActiveServer(id))
|
||||
continue;
|
||||
|
||||
activeServers.Add(id);
|
||||
UpdateTimeout(id);
|
||||
BroadcastSensorStatus(id, server);
|
||||
}
|
||||
|
||||
foreach (var activeServer in activeServers)
|
||||
{
|
||||
UpdateTimeout(activeServer);
|
||||
BroadcastSensorStatus(activeServer);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the address of the currently active server for the given station id if there is one
|
||||
/// </summary>
|
||||
public bool TryGetActiveServerAddress(EntityUid stationId, out string? address)
|
||||
{
|
||||
var servers = EntityQueryEnumerator<CrewMonitoringServerComponent, DeviceNetworkComponent>();
|
||||
(EntityUid id, CrewMonitoringServerComponent server, DeviceNetworkComponent device)? last = default;
|
||||
|
||||
while (servers.MoveNext(out var uid, out var server, out var device))
|
||||
{
|
||||
if (!_stationSystem.GetOwningStation(uid)?.Equals(stationId) ?? true)
|
||||
continue;
|
||||
|
||||
if (!server.Available)
|
||||
{
|
||||
DisconnectServer(uid,server, device);
|
||||
continue;
|
||||
}
|
||||
|
||||
last = (uid, server, device);
|
||||
|
||||
if (server.Active)
|
||||
{
|
||||
address = device.Address;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
//If there was no active server for the station make the last available inactive one active
|
||||
if (last.HasValue)
|
||||
{
|
||||
ConnectServer(last.Value.id, last.Value.server, last.Value.device);
|
||||
address = last.Value.device.Address;
|
||||
return true;
|
||||
}
|
||||
|
||||
address = null;
|
||||
return address != null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -126,17 +69,6 @@ public sealed class CrewMonitoringServerSystem : EntitySystem
|
||||
component.SensorStatus.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disconnects the server losing power
|
||||
/// </summary>
|
||||
private void OnPowerChanged(EntityUid uid, CrewMonitoringServerComponent component, ref PowerChangedEvent args)
|
||||
{
|
||||
component.Available = args.Powered;
|
||||
|
||||
if (!args.Powered)
|
||||
DisconnectServer(uid, component);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Drop the sensor status if it hasn't been updated for to long
|
||||
/// </summary>
|
||||
@@ -170,30 +102,11 @@ public sealed class CrewMonitoringServerSystem : EntitySystem
|
||||
_deviceNetworkSystem.QueuePacket(uid, null, payload, device: device);
|
||||
}
|
||||
|
||||
private void ConnectServer(EntityUid uid, CrewMonitoringServerComponent? server = null, DeviceNetworkComponent? device = null)
|
||||
{
|
||||
if (!Resolve(uid, ref server, ref device))
|
||||
return;
|
||||
|
||||
server.Active = true;
|
||||
|
||||
if (_deviceNetworkSystem.IsDeviceConnected(uid, device))
|
||||
return;
|
||||
|
||||
_deviceNetworkSystem.ConnectDevice(uid, device);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disconnects a server from the device network and clears the currently active server
|
||||
/// Clears sensor data on disconnect
|
||||
/// </summary>
|
||||
private void DisconnectServer(EntityUid uid, CrewMonitoringServerComponent? server = null, DeviceNetworkComponent? device = null)
|
||||
private void OnDisconnected(EntityUid uid, CrewMonitoringServerComponent component, ref DeviceNetServerDisconnectedEvent _)
|
||||
{
|
||||
if (!Resolve(uid, ref server, ref device))
|
||||
return;
|
||||
|
||||
server.SensorStatus.Clear();
|
||||
server.Active = false;
|
||||
|
||||
_deviceNetworkSystem.DisconnectDevice(uid, device, false);
|
||||
component.SensorStatus.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user