2020-04-09 00:28:56 +02:00
|
|
|
using System;
|
|
|
|
|
using System.Threading;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Server.Chat.Managers;
|
|
|
|
|
using Content.Server.GameTicking;
|
2020-11-26 17:07:46 +00:00
|
|
|
using Content.Shared.GameTicking;
|
2021-03-01 20:42:54 -08:00
|
|
|
using Robust.Shared.Audio;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Shared.GameObjects;
|
2020-04-09 00:28:56 +02:00
|
|
|
using Robust.Shared.IoC;
|
2020-09-23 11:53:31 +02:00
|
|
|
using Robust.Shared.Localization;
|
2021-03-01 20:42:54 -08:00
|
|
|
using Robust.Shared.Player;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Shared.Timing;
|
2021-02-18 20:45:45 -08: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.RoundEnd
|
2020-04-09 00:28:56 +02:00
|
|
|
{
|
2021-06-29 15:56:07 +02:00
|
|
|
public class RoundEndSystem : EntitySystem
|
2020-04-09 00:28:56 +02:00
|
|
|
{
|
2020-08-24 14:10:28 +02:00
|
|
|
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
2020-09-23 11:53:31 +02:00
|
|
|
[Dependency] private readonly IChatManager _chatManager = default!;
|
|
|
|
|
|
|
|
|
|
public const float RestartRoundTime = 20f;
|
2020-04-09 00:28:56 +02:00
|
|
|
|
2020-11-27 11:00:49 +01:00
|
|
|
private CancellationTokenSource _roundEndCancellationTokenSource = new();
|
2021-02-16 09:31:57 +01:00
|
|
|
private CancellationTokenSource _callCooldownEndedTokenSource = new();
|
2020-04-09 00:28:56 +02:00
|
|
|
public bool IsRoundEndCountdownStarted { get; private set; }
|
2020-04-10 13:37:13 +02:00
|
|
|
public TimeSpan RoundEndCountdownTime { get; set; } = TimeSpan.FromMinutes(4);
|
2020-04-09 20:28:22 +02:00
|
|
|
public TimeSpan? ExpectedCountdownEnd = null;
|
2020-04-09 00:28:56 +02:00
|
|
|
|
2021-02-16 09:31:57 +01:00
|
|
|
public TimeSpan LastCallTime { get; private set; }
|
|
|
|
|
|
|
|
|
|
public TimeSpan CallCooldown { get; } = TimeSpan.FromSeconds(30);
|
|
|
|
|
|
2021-06-29 15:56:07 +02:00
|
|
|
// TODO: Make these regular eventbus events...
|
2020-04-09 00:28:56 +02:00
|
|
|
public delegate void RoundEndCountdownStarted();
|
2021-03-16 15:50:20 +01:00
|
|
|
public event RoundEndCountdownStarted? OnRoundEndCountdownStarted;
|
2020-04-09 00:28:56 +02:00
|
|
|
|
|
|
|
|
public delegate void RoundEndCountdownCancelled();
|
2021-03-16 15:50:20 +01:00
|
|
|
public event RoundEndCountdownCancelled? OnRoundEndCountdownCancelled;
|
2020-04-09 00:28:56 +02:00
|
|
|
|
|
|
|
|
public delegate void RoundEndCountdownFinished();
|
2021-03-16 15:50:20 +01:00
|
|
|
public event RoundEndCountdownFinished? OnRoundEndCountdownFinished;
|
2020-04-09 00:28:56 +02:00
|
|
|
|
2021-02-16 09:31:57 +01:00
|
|
|
public delegate void CallCooldownEnded();
|
2021-03-16 15:50:20 +01:00
|
|
|
public event CallCooldownEnded? OnCallCooldownEnded;
|
2021-02-16 09:31:57 +01:00
|
|
|
|
2021-06-29 15:56:07 +02:00
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
base.Initialize();
|
|
|
|
|
|
|
|
|
|
SubscribeLocalEvent<RoundRestartCleanupEvent>(Reset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Reset(RoundRestartCleanupEvent ev)
|
2020-11-26 17:07:46 +00:00
|
|
|
{
|
|
|
|
|
IsRoundEndCountdownStarted = false;
|
|
|
|
|
_roundEndCancellationTokenSource.Cancel();
|
|
|
|
|
_roundEndCancellationTokenSource = new CancellationTokenSource();
|
2021-02-16 09:31:57 +01:00
|
|
|
_callCooldownEndedTokenSource.Cancel();
|
|
|
|
|
_callCooldownEndedTokenSource = new CancellationTokenSource();
|
2020-11-26 17:07:46 +00:00
|
|
|
ExpectedCountdownEnd = null;
|
2021-02-16 09:31:57 +01:00
|
|
|
LastCallTime = default;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool CanCall()
|
|
|
|
|
{
|
|
|
|
|
return _gameTiming.CurTime >= LastCallTime + CallCooldown;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ActivateCooldown()
|
|
|
|
|
{
|
|
|
|
|
_callCooldownEndedTokenSource.Cancel();
|
|
|
|
|
_callCooldownEndedTokenSource = new CancellationTokenSource();
|
|
|
|
|
LastCallTime = _gameTiming.CurTime;
|
|
|
|
|
Timer.Spawn(CallCooldown, () => OnCallCooldownEnded?.Invoke(), _callCooldownEndedTokenSource.Token);
|
2020-11-26 17:07:46 +00:00
|
|
|
}
|
|
|
|
|
|
2021-10-13 12:15:28 -05:00
|
|
|
public void RequestRoundEnd(bool checkCooldown = true)
|
|
|
|
|
{
|
|
|
|
|
RequestRoundEnd(RoundEndCountdownTime, checkCooldown);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RequestRoundEnd(TimeSpan countdownTime, bool checkCooldown = true)
|
2020-04-09 00:28:56 +02:00
|
|
|
{
|
|
|
|
|
if (IsRoundEndCountdownStarted)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-10-13 12:15:28 -05:00
|
|
|
if (checkCooldown && !CanCall())
|
2021-02-16 09:31:57 +01:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-09 00:28:56 +02:00
|
|
|
IsRoundEndCountdownStarted = true;
|
|
|
|
|
|
2021-10-13 12:15:28 -05:00
|
|
|
_chatManager.DispatchStationAnnouncement(Loc.GetString("round-end-system-shuttle-called-announcement",("minutes", countdownTime.Minutes)), Loc.GetString("Station"));
|
2021-01-01 16:34:54 +01:00
|
|
|
|
2021-03-01 20:42:54 -08:00
|
|
|
SoundSystem.Play(Filter.Broadcast(), "/Audio/Announcements/shuttlecalled.ogg");
|
2021-01-01 16:34:54 +01:00
|
|
|
|
2021-10-13 12:15:28 -05:00
|
|
|
ExpectedCountdownEnd = _gameTiming.CurTime + countdownTime;
|
|
|
|
|
Timer.Spawn(countdownTime, EndRound, _roundEndCancellationTokenSource.Token);
|
2021-02-16 09:31:57 +01:00
|
|
|
|
|
|
|
|
ActivateCooldown();
|
|
|
|
|
|
2020-04-09 00:28:56 +02:00
|
|
|
OnRoundEndCountdownStarted?.Invoke();
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 12:15:28 -05:00
|
|
|
public void CancelRoundEndCountdown( bool checkCooldown = true)
|
2020-04-09 00:28:56 +02:00
|
|
|
{
|
2020-04-09 01:43:28 +02:00
|
|
|
if (!IsRoundEndCountdownStarted)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-10-13 12:15:28 -05:00
|
|
|
if (checkCooldown && !CanCall())
|
2021-02-16 09:31:57 +01:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-09 01:43:28 +02:00
|
|
|
IsRoundEndCountdownStarted = false;
|
|
|
|
|
|
2021-06-21 02:13:54 +02:00
|
|
|
_chatManager.DispatchStationAnnouncement(Loc.GetString("round-end-system-shuttle-recalled-announcement"), Loc.GetString("Station"));
|
2021-01-01 16:34:54 +01:00
|
|
|
|
2021-03-01 20:42:54 -08:00
|
|
|
SoundSystem.Play(Filter.Broadcast(), "/Audio/Announcements/shuttlerecalled.ogg");
|
2021-01-01 16:34:54 +01:00
|
|
|
|
2020-04-09 00:28:56 +02:00
|
|
|
_roundEndCancellationTokenSource.Cancel();
|
|
|
|
|
_roundEndCancellationTokenSource = new CancellationTokenSource();
|
|
|
|
|
|
|
|
|
|
ExpectedCountdownEnd = null;
|
|
|
|
|
|
2021-02-16 09:31:57 +01:00
|
|
|
ActivateCooldown();
|
|
|
|
|
|
2020-04-09 00:28:56 +02:00
|
|
|
OnRoundEndCountdownCancelled?.Invoke();
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-09 13:18:20 -05:00
|
|
|
public void EndRound()
|
2020-04-09 00:28:56 +02:00
|
|
|
{
|
|
|
|
|
OnRoundEndCountdownFinished?.Invoke();
|
2021-07-10 17:35:33 +02:00
|
|
|
var gameTicker = Get<GameTicker>();
|
2021-06-20 10:09:24 +02:00
|
|
|
gameTicker.EndRound();
|
2020-09-23 11:53:31 +02:00
|
|
|
|
2021-06-21 02:13:54 +02:00
|
|
|
_chatManager.DispatchServerAnnouncement(Loc.GetString("round-end-system-round-restart-eta-announcement", ("seconds", RestartRoundTime)));
|
2020-09-23 11:53:31 +02:00
|
|
|
|
2021-06-20 10:09:24 +02:00
|
|
|
Timer.Spawn(TimeSpan.FromSeconds(RestartRoundTime), () => gameTicker.RestartRound(), CancellationToken.None);
|
2020-04-09 00:28:56 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|