Comms Console Announcements (#3629)

* Communications console announcements

* title case for jobs

* Helper

* Prevent abuse

* Cap message length

* more abuse prevention

* Validate clientside too to reduce bandwidth from unmodified clients
This commit is contained in:
ike709
2021-03-13 23:21:57 -06:00
committed by GitHub
parent 31d74d5909
commit 899a5cfa80
4 changed files with 87 additions and 3 deletions

View File

@@ -14,6 +14,8 @@ namespace Content.Client.Command
{ {
private CommunicationsConsoleBoundUserInterface Owner { get; set; } private CommunicationsConsoleBoundUserInterface Owner { get; set; }
private readonly CancellationTokenSource _timerCancelTokenSource = new(); private readonly CancellationTokenSource _timerCancelTokenSource = new();
private LineEdit _messageInput { get; set; }
public readonly Button AnnounceButton;
public readonly Button EmergencyShuttleButton; public readonly Button EmergencyShuttleButton;
private readonly RichTextLabel _countdownLabel; private readonly RichTextLabel _countdownLabel;
@@ -25,13 +27,27 @@ namespace Content.Client.Command
Title = Loc.GetString("Communications Console"); Title = Loc.GetString("Communications Console");
Owner = owner; Owner = owner;
_messageInput = new LineEdit
{
PlaceHolder = Loc.GetString("Announcement"),
HorizontalExpand = true,
SizeFlagsStretchRatio = 1
};
AnnounceButton = new Button();
AnnounceButton.Text = "Announce";
AnnounceButton.OnPressed += (_) => Owner.AnnounceButtonPressed(_messageInput.Text.Trim());
AnnounceButton.Disabled = !owner.CanAnnounce;
_countdownLabel = new RichTextLabel(){MinSize = new Vector2(0, 200)}; _countdownLabel = new RichTextLabel(){MinSize = new Vector2(0, 200)};
EmergencyShuttleButton = new Button(); EmergencyShuttleButton = new Button();
EmergencyShuttleButton.OnPressed += (_) => Owner.EmergencyShuttleButtonPressed(); EmergencyShuttleButton.OnPressed += (_) => Owner.EmergencyShuttleButtonPressed();
EmergencyShuttleButton.Disabled = !owner.CanCall; EmergencyShuttleButton.Disabled = !owner.CanCall;
var vbox = new VBoxContainer() {HorizontalExpand = true, VerticalExpand = true}; var vbox = new VBoxContainer() {HorizontalExpand = true, VerticalExpand = true};
vbox.AddChild(_messageInput);
vbox.AddChild(new Control(){MinSize = new Vector2(0,10), HorizontalExpand = true});
vbox.AddChild(AnnounceButton);
vbox.AddChild(new Control(){MinSize = new Vector2(0,10), HorizontalExpand = true});
vbox.AddChild(_countdownLabel); vbox.AddChild(_countdownLabel);
vbox.AddChild(EmergencyShuttleButton); vbox.AddChild(EmergencyShuttleButton);

View File

@@ -15,6 +15,7 @@ namespace Content.Client.GameObjects.Components.Command
[ViewVariables] private CommunicationsConsoleMenu? _menu; [ViewVariables] private CommunicationsConsoleMenu? _menu;
public bool CanAnnounce { get; private set; }
public bool CanCall { get; private set; } public bool CanCall { get; private set; }
public bool CountdownStarted { get; private set; } public bool CountdownStarted { get; private set; }
@@ -44,6 +45,12 @@ namespace Content.Client.GameObjects.Components.Command
CallShuttle(); CallShuttle();
} }
public void AnnounceButtonPressed(string message)
{
var msg = message.Length <= 256 ? message.Trim() : $"{message.Trim().Substring(0, 256)}...";
SendMessage(new CommunicationsConsoleAnnounceMessage(msg));
}
public void CallShuttle() public void CallShuttle()
{ {
SendMessage(new CommunicationsConsoleCallEmergencyShuttleMessage()); SendMessage(new CommunicationsConsoleCallEmergencyShuttleMessage());
@@ -61,6 +68,7 @@ namespace Content.Client.GameObjects.Components.Command
if (state is not CommunicationsConsoleInterfaceState commsState) if (state is not CommunicationsConsoleInterfaceState commsState)
return; return;
CanAnnounce = commsState.CanAnnounce;
CanCall = commsState.CanCall; CanCall = commsState.CanCall;
_expectedCountdownTime = commsState.ExpectedCountdownEnd; _expectedCountdownTime = commsState.ExpectedCountdownEnd;
CountdownStarted = commsState.CountdownStarted; CountdownStarted = commsState.CountdownStarted;
@@ -69,6 +77,7 @@ namespace Content.Client.GameObjects.Components.Command
{ {
_menu.UpdateCountdown(); _menu.UpdateCountdown();
_menu.EmergencyShuttleButton.Disabled = !CanCall; _menu.EmergencyShuttleButton.Disabled = !CanCall;
_menu.AnnounceButton.Disabled = !CanAnnounce;
} }
} }

View File

@@ -1,13 +1,21 @@
#nullable enable #nullable enable
using System;
using System.Globalization;
using System.Threading;
using Content.Server.GameObjects.Components.PDA;
using Content.Server.GameObjects.Components.Power.ApcNetComponents; using Content.Server.GameObjects.Components.Power.ApcNetComponents;
using Content.Server.GameObjects.EntitySystems; using Content.Server.GameObjects.EntitySystems;
using Content.Server.Interfaces.Chat;
using Content.Server.Utility; using Content.Server.Utility;
using Content.Shared.GameObjects.Components.Command; using Content.Shared.GameObjects.Components.Command;
using Content.Shared.Interfaces.GameObjects.Components; using Content.Shared.Interfaces.GameObjects.Components;
using Robust.Server.GameObjects; using Robust.Server.GameObjects;
using Robust.Server.Player; using Robust.Server.Player;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Timing;
using Robust.Shared.ViewVariables; using Robust.Shared.ViewVariables;
using Timer = Robust.Shared.Timing.Timer;
namespace Content.Server.GameObjects.Components.Command namespace Content.Server.GameObjects.Components.Command
{ {
@@ -15,12 +23,18 @@ namespace Content.Server.GameObjects.Components.Command
[ComponentReference(typeof(IActivate))] [ComponentReference(typeof(IActivate))]
public class CommunicationsConsoleComponent : SharedCommunicationsConsoleComponent, IActivate public class CommunicationsConsoleComponent : SharedCommunicationsConsoleComponent, IActivate
{ {
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly IChatManager _chatManager = default!;
private bool Powered => !Owner.TryGetComponent(out PowerReceiverComponent? receiver) || receiver.Powered; private bool Powered => !Owner.TryGetComponent(out PowerReceiverComponent? receiver) || receiver.Powered;
private RoundEndSystem RoundEndSystem => EntitySystem.Get<RoundEndSystem>(); private RoundEndSystem RoundEndSystem => EntitySystem.Get<RoundEndSystem>();
[ViewVariables] private BoundUserInterface? UserInterface => Owner.GetUIOrNull(CommunicationsConsoleUiKey.Key); [ViewVariables] private BoundUserInterface? UserInterface => Owner.GetUIOrNull(CommunicationsConsoleUiKey.Key);
public TimeSpan LastAnnounceTime { get; private set; } = TimeSpan.Zero;
public TimeSpan AnnounceCooldown { get; } = TimeSpan.FromSeconds(90);
private CancellationTokenSource _announceCooldownEndedTokenSource = new();
public override void Initialize() public override void Initialize()
{ {
base.Initialize(); base.Initialize();
@@ -49,10 +63,19 @@ namespace Content.Server.GameObjects.Components.Command
{ {
var system = RoundEndSystem; var system = RoundEndSystem;
UserInterface?.SetState(new CommunicationsConsoleInterfaceState(system.CanCall(), system.ExpectedCountdownEnd)); UserInterface?.SetState(new CommunicationsConsoleInterfaceState(CanAnnounce(), system.CanCall(), system.ExpectedCountdownEnd));
} }
} }
public bool CanAnnounce()
{
if (LastAnnounceTime == TimeSpan.Zero)
{
return true;
}
return _gameTiming.CurTime >= LastAnnounceTime + AnnounceCooldown;
}
public override void OnRemove() public override void OnRemove()
{ {
RoundEndSystem.OnRoundEndCountdownStarted -= UpdateBoundInterface; RoundEndSystem.OnRoundEndCountdownStarted -= UpdateBoundInterface;
@@ -72,6 +95,29 @@ namespace Content.Server.GameObjects.Components.Command
case CommunicationsConsoleRecallEmergencyShuttleMessage _: case CommunicationsConsoleRecallEmergencyShuttleMessage _:
RoundEndSystem.CancelRoundEndCountdown(); RoundEndSystem.CancelRoundEndCountdown();
break; break;
case CommunicationsConsoleAnnounceMessage msg:
if (!CanAnnounce())
{
return;
}
_announceCooldownEndedTokenSource.Cancel();
_announceCooldownEndedTokenSource = new CancellationTokenSource();
LastAnnounceTime = _gameTiming.CurTime;
Timer.Spawn(AnnounceCooldown, () => UpdateBoundInterface(), _announceCooldownEndedTokenSource.Token);
UpdateBoundInterface();
var message = msg.Message.Length <= 256 ? msg.Message.Trim() : $"{msg.Message.Trim().Substring(0, 256)}...";
var author = "Unknown";
var mob = obj.Session.AttachedEntity;
if (mob != null && mob.TryGetHeldId(out var id))
{
author = $"{id.FullName} ({CultureInfo.CurrentCulture.TextInfo.ToTitleCase(id.JobTitle)})";
}
message += $"\nSent by {author}";
_chatManager.DispatchStationAnnouncement(message, "Communications Console");
break;
} }
} }

View File

@@ -13,18 +13,31 @@ namespace Content.Shared.GameObjects.Components.Command
[Serializable, NetSerializable] [Serializable, NetSerializable]
public class CommunicationsConsoleInterfaceState : BoundUserInterfaceState public class CommunicationsConsoleInterfaceState : BoundUserInterfaceState
{ {
public readonly bool CanAnnounce;
public readonly bool CanCall; public readonly bool CanCall;
public readonly TimeSpan? ExpectedCountdownEnd; public readonly TimeSpan? ExpectedCountdownEnd;
public readonly bool CountdownStarted; public readonly bool CountdownStarted;
public CommunicationsConsoleInterfaceState(bool canCall, TimeSpan? expectedCountdownEnd = null) public CommunicationsConsoleInterfaceState(bool canAnnounce, bool canCall, TimeSpan? expectedCountdownEnd = null)
{ {
CanAnnounce = canAnnounce;
CanCall = canCall; CanCall = canCall;
ExpectedCountdownEnd = expectedCountdownEnd; ExpectedCountdownEnd = expectedCountdownEnd;
CountdownStarted = expectedCountdownEnd != null; CountdownStarted = expectedCountdownEnd != null;
} }
} }
[Serializable, NetSerializable]
public class CommunicationsConsoleAnnounceMessage : BoundUserInterfaceMessage
{
public readonly string Message;
public CommunicationsConsoleAnnounceMessage(string message)
{
Message = message;
}
}
[Serializable, NetSerializable] [Serializable, NetSerializable]
public class CommunicationsConsoleCallEmergencyShuttleMessage : BoundUserInterfaceMessage public class CommunicationsConsoleCallEmergencyShuttleMessage : BoundUserInterfaceMessage
{ {