2021-03-13 23:21:57 -06:00
|
|
|
using System;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Threading;
|
2021-06-09 22:19:39 +02:00
|
|
|
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;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Server.GameObjects;
|
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
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Server.Communications
|
2020-04-09 00:28:56 +02:00
|
|
|
{
|
|
|
|
|
[RegisterComponent]
|
2022-02-16 00:23:23 -07:00
|
|
|
public sealed class CommunicationsConsoleComponent : SharedCommunicationsConsoleComponent, IEntityEventSubscriber
|
2020-04-09 00:28:56 +02:00
|
|
|
{
|
2021-03-13 23:21:57 -06:00
|
|
|
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
|
|
|
|
[Dependency] private readonly IChatManager _chatManager = default!;
|
2021-12-05 18:09:01 +01:00
|
|
|
[Dependency] private readonly IEntityManager _entities = default!;
|
2022-01-10 11:24:41 -08:00
|
|
|
[Dependency] private readonly IEntityManager _entityManager = default!;
|
2021-12-05 18:09:01 +01:00
|
|
|
|
|
|
|
|
private bool Powered => !_entities.TryGetComponent(Owner, out ApcPowerReceiverComponent? 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();
|
|
|
|
|
|
2021-06-19 19:41:26 -07:00
|
|
|
protected override void Initialize()
|
2020-04-09 00:28:56 +02:00
|
|
|
{
|
|
|
|
|
base.Initialize();
|
|
|
|
|
|
2020-08-22 22:29:20 +02:00
|
|
|
if (UserInterface != null)
|
|
|
|
|
{
|
|
|
|
|
UserInterface.OnReceiveMessage += UserInterfaceOnOnReceiveMessage;
|
|
|
|
|
}
|
2020-04-09 00:28:56 +02:00
|
|
|
|
2022-01-10 11:24:41 -08:00
|
|
|
_entityManager.EventBus.SubscribeEvent<RoundEndSystemChangedEvent>(EventSource.Local, this, (s) => UpdateBoundInterface());
|
2021-02-16 09:31:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2021-06-19 19:41:26 -07:00
|
|
|
protected override void OnRemove()
|
2020-11-26 17:07:46 +00:00
|
|
|
{
|
2022-01-10 11:24:41 -08:00
|
|
|
_entityManager.EventBus.UnsubscribeEvent<RoundEndSystemChangedEvent>(EventSource.Local, this);
|
2020-11-26 17:07:46 +00:00
|
|
|
base.OnRemove();
|
2020-04-09 00:28:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UserInterfaceOnOnReceiveMessage(ServerBoundUserInterfaceMessage obj)
|
|
|
|
|
{
|
|
|
|
|
switch (obj.Message)
|
|
|
|
|
{
|
|
|
|
|
case CommunicationsConsoleCallEmergencyShuttleMessage _:
|
2021-11-22 21:31:50 +01:00
|
|
|
RoundEndSystem.RequestRoundEnd(obj.Session.AttachedEntity);
|
2020-04-09 00:28:56 +02:00
|
|
|
break;
|
2020-04-09 01:43:28 +02:00
|
|
|
|
|
|
|
|
case CommunicationsConsoleRecallEmergencyShuttleMessage _:
|
2021-11-22 21:31:50 +01:00
|
|
|
RoundEndSystem.CancelRoundEndCountdown(obj.Session.AttachedEntity);
|
2020-04-09 01:43:28 +02:00
|
|
|
break;
|
2021-03-13 23:21:57 -06:00
|
|
|
case CommunicationsConsoleAnnounceMessage msg:
|
|
|
|
|
if (!CanAnnounce())
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_announceCooldownEndedTokenSource.Cancel();
|
|
|
|
|
_announceCooldownEndedTokenSource = new CancellationTokenSource();
|
|
|
|
|
LastAnnounceTime = _gameTiming.CurTime;
|
2021-11-22 21:31:50 +01:00
|
|
|
Timer.Spawn(AnnounceCooldown, UpdateBoundInterface, _announceCooldownEndedTokenSource.Token);
|
2021-03-13 23:21:57 -06:00
|
|
|
UpdateBoundInterface();
|
|
|
|
|
|
|
|
|
|
var message = msg.Message.Length <= 256 ? msg.Message.Trim() : $"{msg.Message.Trim().Substring(0, 256)}...";
|
|
|
|
|
|
|
|
|
|
var author = "Unknown";
|
2021-12-05 18:09:01 +01:00
|
|
|
if (obj.Session.AttachedEntity is {Valid: true} mob && mob.TryGetHeldId(out var id))
|
2021-03-13 23:21:57 -06:00
|
|
|
{
|
2021-03-16 15:50:20 +01:00
|
|
|
author = $"{id.FullName} ({CultureInfo.CurrentCulture.TextInfo.ToTitleCase(id.JobTitle ?? string.Empty)})".Trim();
|
2021-03-13 23:21:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
message += $"\nSent by {author}";
|
2022-03-01 05:21:28 -08:00
|
|
|
_chatManager.DispatchStationAnnouncement(message, "Communications Console", colorOverride: Color.Gold);
|
2021-03-13 23:21:57 -06:00
|
|
|
break;
|
2020-04-09 00:28:56 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|