2020-12-18 11:42:02 +00:00
|
|
|
#nullable enable
|
2021-03-13 23:21:57 -06:00
|
|
|
using System;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using Content.Server.GameObjects.Components.PDA;
|
2020-08-22 22:29:20 +02:00
|
|
|
using Content.Server.GameObjects.Components.Power.ApcNetComponents;
|
2020-08-13 14:40:27 +02:00
|
|
|
using Content.Server.GameObjects.EntitySystems;
|
2021-03-13 23:21:57 -06:00
|
|
|
using Content.Server.Interfaces.Chat;
|
2020-08-24 20:47:17 +02:00
|
|
|
using Content.Server.Utility;
|
2020-04-09 00:28:56 +02:00
|
|
|
using Content.Shared.GameObjects.Components.Command;
|
2020-07-18 22:51:56 -07:00
|
|
|
using Content.Shared.Interfaces.GameObjects.Components;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Server.GameObjects;
|
|
|
|
|
using Robust.Server.Player;
|
2020-04-09 00:28:56 +02:00
|
|
|
using Robust.Shared.GameObjects;
|
2021-03-13 23:21:57 -06:00
|
|
|
using Robust.Shared.IoC;
|
|
|
|
|
using Robust.Shared.Timing;
|
2020-08-22 22:29:20 +02:00
|
|
|
using Robust.Shared.ViewVariables;
|
2021-03-13 23:21:57 -06:00
|
|
|
using Timer = Robust.Shared.Timing.Timer;
|
2020-04-09 00:28:56 +02:00
|
|
|
|
|
|
|
|
namespace Content.Server.GameObjects.Components.Command
|
|
|
|
|
{
|
|
|
|
|
[RegisterComponent]
|
|
|
|
|
[ComponentReference(typeof(IActivate))]
|
|
|
|
|
public class CommunicationsConsoleComponent : SharedCommunicationsConsoleComponent, IActivate
|
|
|
|
|
{
|
2021-03-13 23:21:57 -06:00
|
|
|
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
|
|
|
|
[Dependency] private readonly IChatManager _chatManager = default!;
|
2020-08-22 22:29:20 +02:00
|
|
|
private bool Powered => !Owner.TryGetComponent(out PowerReceiverComponent? receiver) || receiver.Powered;
|
2020-04-09 00:28:56 +02:00
|
|
|
|
2021-02-16 09:31:57 +01:00
|
|
|
private RoundEndSystem RoundEndSystem => EntitySystem.Get<RoundEndSystem>();
|
2020-04-09 00:28:56 +02:00
|
|
|
|
2020-08-24 20:47:17 +02:00
|
|
|
[ViewVariables] private BoundUserInterface? UserInterface => Owner.GetUIOrNull(CommunicationsConsoleUiKey.Key);
|
2020-08-22 22:29:20 +02:00
|
|
|
|
2021-03-13 23:21:57 -06:00
|
|
|
public TimeSpan LastAnnounceTime { get; private set; } = TimeSpan.Zero;
|
|
|
|
|
public TimeSpan AnnounceCooldown { get; } = TimeSpan.FromSeconds(90);
|
|
|
|
|
private CancellationTokenSource _announceCooldownEndedTokenSource = new();
|
|
|
|
|
|
2020-04-09 00:28:56 +02:00
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
base.Initialize();
|
|
|
|
|
|
2020-08-22 22:29:20 +02:00
|
|
|
if (UserInterface != null)
|
|
|
|
|
{
|
|
|
|
|
UserInterface.OnReceiveMessage += UserInterfaceOnOnReceiveMessage;
|
|
|
|
|
}
|
2020-04-09 00:28:56 +02:00
|
|
|
|
|
|
|
|
RoundEndSystem.OnRoundEndCountdownStarted += UpdateBoundInterface;
|
|
|
|
|
RoundEndSystem.OnRoundEndCountdownCancelled += UpdateBoundInterface;
|
|
|
|
|
RoundEndSystem.OnRoundEndCountdownFinished += UpdateBoundInterface;
|
2021-02-16 09:31:57 +01:00
|
|
|
RoundEndSystem.OnCallCooldownEnded += UpdateBoundInterface;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Startup()
|
|
|
|
|
{
|
|
|
|
|
base.Startup();
|
|
|
|
|
|
|
|
|
|
UpdateBoundInterface();
|
2020-04-09 00:28:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateBoundInterface()
|
|
|
|
|
{
|
2020-11-26 17:07:46 +00:00
|
|
|
if (!Deleted)
|
2021-02-16 09:31:57 +01:00
|
|
|
{
|
|
|
|
|
var system = RoundEndSystem;
|
|
|
|
|
|
2021-03-13 23:21:57 -06:00
|
|
|
UserInterface?.SetState(new CommunicationsConsoleInterfaceState(CanAnnounce(), system.CanCall(), system.ExpectedCountdownEnd));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool CanAnnounce()
|
|
|
|
|
{
|
|
|
|
|
if (LastAnnounceTime == TimeSpan.Zero)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
2021-02-16 09:31:57 +01:00
|
|
|
}
|
2021-03-13 23:21:57 -06:00
|
|
|
return _gameTiming.CurTime >= LastAnnounceTime + AnnounceCooldown;
|
2020-11-26 17:07:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnRemove()
|
|
|
|
|
{
|
|
|
|
|
RoundEndSystem.OnRoundEndCountdownStarted -= UpdateBoundInterface;
|
|
|
|
|
RoundEndSystem.OnRoundEndCountdownCancelled -= UpdateBoundInterface;
|
|
|
|
|
RoundEndSystem.OnRoundEndCountdownFinished -= UpdateBoundInterface;
|
|
|
|
|
base.OnRemove();
|
2020-04-09 00:28:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UserInterfaceOnOnReceiveMessage(ServerBoundUserInterfaceMessage obj)
|
|
|
|
|
{
|
|
|
|
|
switch (obj.Message)
|
|
|
|
|
{
|
|
|
|
|
case CommunicationsConsoleCallEmergencyShuttleMessage _:
|
|
|
|
|
RoundEndSystem.RequestRoundEnd();
|
|
|
|
|
break;
|
2020-04-09 01:43:28 +02:00
|
|
|
|
|
|
|
|
case CommunicationsConsoleRecallEmergencyShuttleMessage _:
|
|
|
|
|
RoundEndSystem.CancelRoundEndCountdown();
|
|
|
|
|
break;
|
2021-03-13 23:21:57 -06:00
|
|
|
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;
|
2020-04-09 00:28:56 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OpenUserInterface(IPlayerSession session)
|
|
|
|
|
{
|
2020-08-22 22:29:20 +02:00
|
|
|
UserInterface?.Open(session);
|
2020-04-09 00:28:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IActivate.Activate(ActivateEventArgs eventArgs)
|
|
|
|
|
{
|
2020-08-22 22:29:20 +02:00
|
|
|
if (!eventArgs.User.TryGetComponent(out IActorComponent? actor))
|
2020-04-09 00:28:56 +02:00
|
|
|
return;
|
2020-12-18 11:42:02 +00:00
|
|
|
/*
|
2020-04-09 00:28:56 +02:00
|
|
|
if (!Powered)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-12-18 11:42:02 +00:00
|
|
|
*/
|
2020-04-09 00:28:56 +02:00
|
|
|
OpenUserInterface(actor.playerSession);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|