using System; using System.Collections.Generic; using Robust.Shared.GameObjects; using Robust.Shared.Maths; namespace Content.Server.DeviceNetwork.Connections { public class WirelessNetworkConnection : BaseNetworkConnection { public const string WIRELESS_POSITION = "position"; private readonly IEntity _owner; private float _range; public float Range { get => _range; set => _range = Math.Abs(value); } public WirelessNetworkConnection(int frequency, OnReceiveNetMessage onReceive, bool receiveAll, IEntity owner, float range) : base(NetworkUtils.WIRELESS, frequency, onReceive, receiveAll) { _owner = owner; Range = range; } protected override bool CanReceive(int frequency, string sender, IReadOnlyDictionary payload, Metadata metadata, bool broadcast) { if (_owner.Deleted) { Connection.Close(); return false; } if (metadata.TryParseMetadata(WIRELESS_POSITION, out var position)) { var ownPosition = _owner.Transform.WorldPosition; var distance = (ownPosition - position).Length; return distance <= Range; } //Only receive packages with the same frequency return frequency == Frequency; } protected override Metadata GetMetadata() { if (_owner.Deleted) { Connection.Close(); return new Metadata(); } var position = _owner.Transform.WorldPosition; var metadata = new Metadata { {WIRELESS_POSITION, position} }; return metadata; } protected override Dictionary ManipulatePayload(Dictionary payload) { return payload; } } }