Files
tbd-station-14/Content.Server/GameObjects/EntitySystems/RoundEndSystem.cs
DrSmugleaf 5c0cf1b1a0 Use 'new' expression in places where the type is evident for content (#2590)
* Content.Client

* Content.Benchmarks

* Content.IntegrationTests

* Content.Server

* Content.Server.Database

* Content.Shared

* Content.Tests

* Merge fixes

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
2020-11-27 21:00:49 +11:00

82 lines
2.9 KiB
C#

using System;
using System.Threading;
using Content.Server.Interfaces.Chat;
using Content.Server.Interfaces.GameTicking;
using Content.Shared.GameTicking;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.Timing;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Timer = Robust.Shared.Timers.Timer;
namespace Content.Server.GameObjects.EntitySystems
{
public class RoundEndSystem : EntitySystem, IResettingEntitySystem
{
[Dependency] private readonly IGameTicker _gameTicker = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly IChatManager _chatManager = default!;
public const float RestartRoundTime = 20f;
private CancellationTokenSource _roundEndCancellationTokenSource = new();
public bool IsRoundEndCountdownStarted { get; private set; }
public TimeSpan RoundEndCountdownTime { get; set; } = TimeSpan.FromMinutes(4);
public TimeSpan? ExpectedCountdownEnd = null;
public delegate void RoundEndCountdownStarted();
public event RoundEndCountdownStarted OnRoundEndCountdownStarted;
public delegate void RoundEndCountdownCancelled();
public event RoundEndCountdownCancelled OnRoundEndCountdownCancelled;
public delegate void RoundEndCountdownFinished();
public event RoundEndCountdownFinished OnRoundEndCountdownFinished;
void IResettingEntitySystem.Reset()
{
IsRoundEndCountdownStarted = false;
_roundEndCancellationTokenSource.Cancel();
_roundEndCancellationTokenSource = new CancellationTokenSource();
ExpectedCountdownEnd = null;
}
public void RequestRoundEnd()
{
if (IsRoundEndCountdownStarted)
return;
IsRoundEndCountdownStarted = true;
ExpectedCountdownEnd = _gameTiming.CurTime + RoundEndCountdownTime;
Timer.Spawn(RoundEndCountdownTime, EndRound, _roundEndCancellationTokenSource.Token);
OnRoundEndCountdownStarted?.Invoke();
}
public void CancelRoundEndCountdown()
{
if (!IsRoundEndCountdownStarted)
return;
IsRoundEndCountdownStarted = false;
_roundEndCancellationTokenSource.Cancel();
_roundEndCancellationTokenSource = new CancellationTokenSource();
ExpectedCountdownEnd = null;
OnRoundEndCountdownCancelled?.Invoke();
}
private void EndRound()
{
OnRoundEndCountdownFinished?.Invoke();
_gameTicker.EndRound();
_chatManager.DispatchServerAnnouncement(Loc.GetString("Restarting the round in {0} seconds...", RestartRoundTime));
Timer.Spawn(TimeSpan.FromSeconds(RestartRoundTime), () => _gameTicker.RestartRound(), CancellationToken.None);
}
}
}