using Content.Server.DeviceNetwork.Components; using Content.Server.DeviceNetwork.Components.Devices; using Content.Shared.Interaction; namespace Content.Server.DeviceNetwork.Systems.Devices { public sealed class ApcNetSwitchSystem : EntitySystem { [Dependency] private readonly DeviceNetworkSystem _deviceNetworkSystem = default!; public override void Initialize() { base.Initialize(); SubscribeLocalEvent(OnInteracted); SubscribeLocalEvent(OnPackedReceived); } /// /// Toggles the state of the switch and sents a command with the /// value set to state. /// private void OnInteracted(EntityUid uid, ApcNetSwitchComponent component, InteractHandEvent args) { if (!EntityManager.TryGetComponent(uid, out DeviceNetworkComponent? networkComponent)) return; component.State = !component.State; if (networkComponent.TransmitFrequency == null) return; var payload = new NetworkPayload { [DeviceNetworkConstants.Command] = DeviceNetworkConstants.CmdSetState, [DeviceNetworkConstants.StateEnabled] = component.State, }; _deviceNetworkSystem.QueuePacket(uid, null, payload, device: networkComponent); args.Handled = true; } /// /// Listens to the command of other switches to sync state /// private void OnPackedReceived(EntityUid uid, ApcNetSwitchComponent component, DeviceNetworkPacketEvent args) { if (!EntityManager.TryGetComponent(uid, out DeviceNetworkComponent? networkComponent) || args.SenderAddress == networkComponent.Address) return; if (!args.Data.TryGetValue(DeviceNetworkConstants.Command, out string? command) || command != DeviceNetworkConstants.CmdSetState) return; if (!args.Data.TryGetValue(DeviceNetworkConstants.StateEnabled, out bool enabled)) return; component.State = enabled; } } }