2021-06-09 22:19:39 +02:00
|
|
|
using Content.Server.Radio.Components;
|
|
|
|
|
using Content.Server.Radio.EntitySystems;
|
2020-10-07 14:02:12 +02:00
|
|
|
using Content.Shared.Chat;
|
2022-06-23 20:11:03 +10:00
|
|
|
using Content.Shared.Radio;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Server.GameObjects;
|
2020-10-07 14:02:12 +02:00
|
|
|
using Robust.Shared.Containers;
|
2022-06-23 20:11:03 +10:00
|
|
|
using Robust.Shared.Prototypes;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Shared.Network;
|
2022-06-23 20:11:03 +10:00
|
|
|
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.List;
|
|
|
|
|
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Set;
|
2020-10-07 14:02:12 +02:00
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Server.Headset
|
2020-10-07 14:02:12 +02:00
|
|
|
{
|
|
|
|
|
[RegisterComponent]
|
|
|
|
|
[ComponentReference(typeof(IRadio))]
|
|
|
|
|
[ComponentReference(typeof(IListen))]
|
2021-10-27 18:10:40 +02:00
|
|
|
#pragma warning disable 618
|
2022-04-08 17:17:25 -04:00
|
|
|
public sealed class HeadsetComponent : Component, IListen, IRadio
|
2021-10-27 18:10:40 +02:00
|
|
|
#pragma warning restore 618
|
2020-10-07 14:02:12 +02:00
|
|
|
{
|
2021-12-08 17:17:12 +01:00
|
|
|
[Dependency] private readonly IEntityManager _entMan = default!;
|
2020-10-07 14:02:12 +02:00
|
|
|
[Dependency] private readonly IServerNetManager _netManager = default!;
|
|
|
|
|
|
|
|
|
|
private RadioSystem _radioSystem = default!;
|
|
|
|
|
|
2022-06-23 20:11:03 +10:00
|
|
|
[DataField("channels", customTypeSerializer:typeof(PrototypeIdHashSetSerializer<RadioChannelPrototype>))]
|
|
|
|
|
public HashSet<string> Channels = new()
|
|
|
|
|
{
|
|
|
|
|
"Common"
|
|
|
|
|
};
|
2020-10-07 14:02:12 +02:00
|
|
|
|
|
|
|
|
[ViewVariables(VVAccess.ReadWrite)]
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("listenRange")]
|
2020-10-07 14:02:12 +02:00
|
|
|
public int ListenRange { get; private set; }
|
|
|
|
|
|
|
|
|
|
public bool RadioRequested { get; set; }
|
|
|
|
|
|
2021-06-19 19:41:26 -07:00
|
|
|
protected override void Initialize()
|
2020-10-07 14:02:12 +02:00
|
|
|
{
|
|
|
|
|
base.Initialize();
|
|
|
|
|
|
|
|
|
|
_radioSystem = EntitySystem.Get<RadioSystem>();
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-23 20:11:03 +10:00
|
|
|
public bool CanListen(string message, EntityUid source, RadioChannelPrototype prototype)
|
2020-10-07 14:02:12 +02:00
|
|
|
{
|
2022-06-23 20:11:03 +10:00
|
|
|
return Channels.Contains(prototype.ID) && RadioRequested;
|
2020-10-07 14:02:12 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-23 20:11:03 +10:00
|
|
|
public void Receive(string message, RadioChannelPrototype channel, EntityUid source)
|
2020-10-07 14:02:12 +02:00
|
|
|
{
|
2022-06-23 20:11:03 +10:00
|
|
|
if (!Channels.Contains(channel.ID) || !Owner.TryGetContainer(out var container)) return;
|
2020-10-07 14:02:12 +02:00
|
|
|
|
2022-06-23 20:11:03 +10:00
|
|
|
if (!_entMan.TryGetComponent(container.Owner, out ActorComponent? actor)) return;
|
2020-10-07 14:02:12 +02:00
|
|
|
|
2022-06-23 20:11:03 +10:00
|
|
|
var playerChannel = actor.PlayerSession.ConnectedClient;
|
2020-10-07 14:02:12 +02:00
|
|
|
|
2022-06-23 20:11:03 +10:00
|
|
|
var msg = new MsgChatMessage
|
|
|
|
|
{
|
|
|
|
|
Channel = ChatChannel.Radio,
|
|
|
|
|
Message = message,
|
2021-04-20 18:39:39 -05:00
|
|
|
//Square brackets are added here to avoid issues with escaping
|
2022-06-29 06:27:19 +03:00
|
|
|
MessageWrap = Loc.GetString("chat-radio-message-wrap", ("color", channel.Color), ("channel", $"\\[{channel.LocalizedName}\\]"), ("name", _entMan.GetComponent<MetaDataComponent>(source).EntityName))
|
2022-06-23 20:11:03 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_netManager.ServerSendMessage(msg, playerChannel);
|
2020-10-07 14:02:12 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-23 20:11:03 +10:00
|
|
|
public void Listen(string message, EntityUid speaker, RadioChannelPrototype channel)
|
2020-10-07 14:02:12 +02:00
|
|
|
{
|
2022-06-23 20:11:03 +10:00
|
|
|
Broadcast(message, speaker, channel);
|
2020-10-07 14:02:12 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-23 20:11:03 +10:00
|
|
|
public void Broadcast(string message, EntityUid speaker, RadioChannelPrototype channel)
|
2020-10-07 14:02:12 +02:00
|
|
|
{
|
2022-06-23 20:11:03 +10:00
|
|
|
if (!Channels.Contains(channel.ID)) return;
|
|
|
|
|
|
|
|
|
|
_radioSystem.SpreadMessage(this, speaker, message, channel);
|
2020-10-07 14:02:12 +02:00
|
|
|
RadioRequested = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|