Refactored RoundEndSystem (#5990)

* No RestartRound if round id changed

* Refactored RoundEndSystem
This commit is contained in:
wrexbe
2022-01-09 20:07:40 -08:00
committed by GitHub
parent 5983e492a0
commit 03c56bf23e
3 changed files with 69 additions and 81 deletions

View File

@@ -19,79 +19,60 @@ namespace Content.Server.RoundEnd
{
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly IChatManager _chatManager = default!;
[Dependency] private readonly GameTicker _gameTicker = default!;
[Dependency] private readonly AdminLogSystem _adminLog = default!;
public const float RestartRoundTime = 20f;
private CancellationTokenSource _roundEndCancellationTokenSource = new();
private CancellationTokenSource _callCooldownEndedTokenSource = new();
public bool IsRoundEndCountdownStarted { get; private set; }
public TimeSpan RoundEndCountdownTime { get; set; } = TimeSpan.FromMinutes(4);
public TimeSpan? ExpectedCountdownEnd = null;
private readonly TimeSpan _cooldownDuration = TimeSpan.FromSeconds(30);
private readonly TimeSpan _countdownDuration = TimeSpan.FromMinutes(4);
private readonly TimeSpan _restartRoundDuration = TimeSpan.FromSeconds(20);
public TimeSpan LastCallTime { get; private set; }
public TimeSpan CallCooldown { get; } = TimeSpan.FromSeconds(30);
// TODO: Make these regular eventbus events...
public delegate void RoundEndCountdownStarted();
public event RoundEndCountdownStarted? OnRoundEndCountdownStarted;
public delegate void RoundEndCountdownCancelled();
public event RoundEndCountdownCancelled? OnRoundEndCountdownCancelled;
public delegate void RoundEndCountdownFinished();
public event RoundEndCountdownFinished? OnRoundEndCountdownFinished;
public delegate void CallCooldownEnded();
public event CallCooldownEnded? OnCallCooldownEnded;
private CancellationTokenSource? _countdownTokenSource = null;
private CancellationTokenSource? _cooldownTokenSource = null;
public TimeSpan? ExpectedCountdownEnd { get; set; } = null;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<RoundRestartCleanupEvent>(Reset);
}
void Reset(RoundRestartCleanupEvent ev)
private void Reset(RoundRestartCleanupEvent ev)
{
IsRoundEndCountdownStarted = false;
_roundEndCancellationTokenSource.Cancel();
_roundEndCancellationTokenSource = new CancellationTokenSource();
_callCooldownEndedTokenSource.Cancel();
_callCooldownEndedTokenSource = new CancellationTokenSource();
if (_countdownTokenSource != null)
{
_countdownTokenSource.Cancel();
_countdownTokenSource = null;
}
if (_cooldownTokenSource != null)
{
_cooldownTokenSource.Cancel();
_cooldownTokenSource = null;
}
ExpectedCountdownEnd = null;
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);
return _cooldownTokenSource == null;
}
public void RequestRoundEnd(EntityUid? requester = null, bool checkCooldown = true)
{
RequestRoundEnd(RoundEndCountdownTime, requester, checkCooldown);
RequestRoundEnd(_countdownDuration, requester, checkCooldown);
}
public void RequestRoundEnd(TimeSpan countdownTime, EntityUid? requester = null, bool checkCooldown = true)
{
if (IsRoundEndCountdownStarted)
return;
if (_gameTicker.RunLevel != GameRunLevel.InRound) return;
if (checkCooldown && !CanCall())
{
return;
}
if (checkCooldown && _cooldownTokenSource != null) return;
if (_countdownTokenSource != null) return;
_countdownTokenSource = new();
if (requester != null)
{
@@ -102,29 +83,25 @@ namespace Content.Server.RoundEnd
_adminLog.Add(LogType.ShuttleCalled, LogImpact.High, $"Shuttle called");
}
IsRoundEndCountdownStarted = true;
_chatManager.DispatchStationAnnouncement(Loc.GetString("round-end-system-shuttle-called-announcement",("minutes", countdownTime.Minutes)), Loc.GetString("Station"), false);
SoundSystem.Play(Filter.Broadcast(), "/Audio/Announcements/shuttlecalled.ogg");
ExpectedCountdownEnd = _gameTiming.CurTime + countdownTime;
Timer.Spawn(countdownTime, EndRound, _roundEndCancellationTokenSource.Token);
Timer.Spawn(countdownTime, EndRound, _countdownTokenSource.Token);
ActivateCooldown();
OnRoundEndCountdownStarted?.Invoke();
RaiseLocalEvent(RoundEndSystemChangedEvent.Default);
}
public void CancelRoundEndCountdown(EntityUid? requester = null, bool checkCooldown = true)
{
if (!IsRoundEndCountdownStarted)
return;
if (_gameTicker.RunLevel != GameRunLevel.InRound) return;
if (checkCooldown && _cooldownTokenSource != null) return;
if (checkCooldown && !CanCall())
{
return;
}
if (_countdownTokenSource == null) return;
_countdownTokenSource.Cancel();
_countdownTokenSource = null;
if (requester != null)
{
@@ -135,31 +112,47 @@ namespace Content.Server.RoundEnd
_adminLog.Add(LogType.ShuttleRecalled, LogImpact.High, $"Shuttle recalled");
}
IsRoundEndCountdownStarted = false;
_chatManager.DispatchStationAnnouncement(Loc.GetString("round-end-system-shuttle-recalled-announcement"), Loc.GetString("Station"), false);
SoundSystem.Play(Filter.Broadcast(), "/Audio/Announcements/shuttlerecalled.ogg");
_roundEndCancellationTokenSource.Cancel();
_roundEndCancellationTokenSource = new CancellationTokenSource();
ExpectedCountdownEnd = null;
ActivateCooldown();
OnRoundEndCountdownCancelled?.Invoke();
RaiseLocalEvent(RoundEndSystemChangedEvent.Default);
}
public void EndRound()
{
OnRoundEndCountdownFinished?.Invoke();
var gameTicker = Get<GameTicker>();
gameTicker.EndRound();
if (_gameTicker.RunLevel != GameRunLevel.InRound) return;
RaiseLocalEvent(RoundEndSystemChangedEvent.Default);
_gameTicker.EndRound();
_countdownTokenSource?.Cancel();
_countdownTokenSource = new();
_chatManager.DispatchServerAnnouncement(Loc.GetString("round-end-system-round-restart-eta-announcement", ("seconds", _restartRoundDuration.Seconds)));
Timer.Spawn(_restartRoundDuration, AfterEndRoundRestart, _countdownTokenSource.Token);
}
_chatManager.DispatchServerAnnouncement(Loc.GetString("round-end-system-round-restart-eta-announcement", ("seconds", RestartRoundTime)));
private void AfterEndRoundRestart()
{
if (_gameTicker.RunLevel != GameRunLevel.InRound) return;
_gameTicker.RestartRound();
}
Timer.Spawn(TimeSpan.FromSeconds(RestartRoundTime), () => gameTicker.RestartRound(), CancellationToken.None);
private void ActivateCooldown()
{
_cooldownTokenSource?.Cancel();
_cooldownTokenSource = new();
Timer.Spawn(_cooldownDuration, () =>
{
_cooldownTokenSource.Cancel();
_cooldownTokenSource = null;
RaiseLocalEvent(RoundEndSystemChangedEvent.Default);
}, _cooldownTokenSource.Token);
}
}
public class RoundEndSystemChangedEvent : EntityEventArgs
{
public static RoundEndSystemChangedEvent Default { get; } = new();
}
}