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:
@@ -1,13 +1,21 @@
|
||||
#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.EntitySystems;
|
||||
using Content.Server.Interfaces.Chat;
|
||||
using Content.Server.Utility;
|
||||
using Content.Shared.GameObjects.Components.Command;
|
||||
using Content.Shared.Interfaces.GameObjects.Components;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Server.Player;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Timing;
|
||||
using Robust.Shared.ViewVariables;
|
||||
using Timer = Robust.Shared.Timing.Timer;
|
||||
|
||||
namespace Content.Server.GameObjects.Components.Command
|
||||
{
|
||||
@@ -15,12 +23,18 @@ namespace Content.Server.GameObjects.Components.Command
|
||||
[ComponentReference(typeof(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 RoundEndSystem RoundEndSystem => EntitySystem.Get<RoundEndSystem>();
|
||||
|
||||
[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()
|
||||
{
|
||||
base.Initialize();
|
||||
@@ -49,10 +63,19 @@ namespace Content.Server.GameObjects.Components.Command
|
||||
{
|
||||
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()
|
||||
{
|
||||
RoundEndSystem.OnRoundEndCountdownStarted -= UpdateBoundInterface;
|
||||
@@ -72,6 +95,29 @@ namespace Content.Server.GameObjects.Components.Command
|
||||
case CommunicationsConsoleRecallEmergencyShuttleMessage _:
|
||||
RoundEndSystem.CancelRoundEndCountdown();
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user