Files
tbd-station-14/Content.Server/Communications/CommunicationsConsoleComponent.cs
20kdc f6d44be34f Activatable UI component (#5184)
* Transfer most Instrument UI logic to a new component, ActivatableUIComponent

* Move more ActivatableUIComponent stuff to ECS

* ActivatableUI component ignore on client

* ActivatableUI: Get rid of component interfaces where possible

* Add in adminOnly attribute for activatable UIs

This is so that porting #4926 to this will be easier

* Transition Solar Control Computer to ActivatableUI

* Move communications console to ActivatableUI

* Move cargo console to ActivatableUI

* Move ID card console to ActivatableUI

* ActivatableUI: Make things more amiable to entity tests adding components weirdly

* ActivatableUI: Use handling or lack thereof of events properly

* ActivatableUI: component dependency issue resolution stuffs

* ActivatableUISystem: Fix #5258

* More fixes because master did stuffo

* Check for HandDeselectedEvent again because otherwise active-hand check doesn't work

* Move just a bit more code into the system, introduce a workaround for #5258

* Purge the player status detection stuff

* Oh and some obsolete stuff too
2021-11-23 19:19:08 +01:00

125 lines
4.7 KiB
C#

using System;
using System.Globalization;
using System.Threading;
using Content.Server.Chat.Managers;
using Content.Server.PDA;
using Content.Server.Power.Components;
using Content.Server.RoundEnd;
using Content.Server.UserInterface;
using Content.Shared.Communications;
using Content.Shared.Interaction;
using Robust.Server.GameObjects;
using Robust.Server.Player;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Player;
using Robust.Shared.Timing;
using Robust.Shared.ViewVariables;
using Timer = Robust.Shared.Timing.Timer;
namespace Content.Server.Communications
{
[RegisterComponent]
public class CommunicationsConsoleComponent : SharedCommunicationsConsoleComponent
{
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly IChatManager _chatManager = default!;
private bool Powered => !Owner.TryGetComponent(out ApcPowerReceiverComponent? 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();
protected override void Initialize()
{
base.Initialize();
if (UserInterface != null)
{
UserInterface.OnReceiveMessage += UserInterfaceOnOnReceiveMessage;
}
RoundEndSystem.OnRoundEndCountdownStarted += UpdateBoundInterface;
RoundEndSystem.OnRoundEndCountdownCancelled += UpdateBoundInterface;
RoundEndSystem.OnRoundEndCountdownFinished += UpdateBoundInterface;
RoundEndSystem.OnCallCooldownEnded += UpdateBoundInterface;
}
protected override void Startup()
{
base.Startup();
UpdateBoundInterface();
}
private void UpdateBoundInterface()
{
if (!Deleted)
{
var system = RoundEndSystem;
UserInterface?.SetState(new CommunicationsConsoleInterfaceState(CanAnnounce(), system.CanCall(), system.ExpectedCountdownEnd));
}
}
public bool CanAnnounce()
{
if (LastAnnounceTime == TimeSpan.Zero)
{
return true;
}
return _gameTiming.CurTime >= LastAnnounceTime + AnnounceCooldown;
}
protected override void OnRemove()
{
RoundEndSystem.OnRoundEndCountdownStarted -= UpdateBoundInterface;
RoundEndSystem.OnRoundEndCountdownCancelled -= UpdateBoundInterface;
RoundEndSystem.OnRoundEndCountdownFinished -= UpdateBoundInterface;
base.OnRemove();
}
private void UserInterfaceOnOnReceiveMessage(ServerBoundUserInterfaceMessage obj)
{
switch (obj.Message)
{
case CommunicationsConsoleCallEmergencyShuttleMessage _:
RoundEndSystem.RequestRoundEnd(obj.Session.AttachedEntity);
break;
case CommunicationsConsoleRecallEmergencyShuttleMessage _:
RoundEndSystem.CancelRoundEndCountdown(obj.Session.AttachedEntity);
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 ?? string.Empty)})".Trim();
}
message += $"\nSent by {author}";
_chatManager.DispatchStationAnnouncement(message, "Communications Console");
break;
}
}
}
}