Files
tbd-station-14/Content.Server/GameTicking/Rules/MaxTimeRestartRuleSystem.cs
Kara b9a0894d7c Event refactor (#9589)
* Station event refactor

* Remove clientside `IStationEventManager`

we can just use prototypes

* Basic API idea

* Cruft

* first attempt at epicness

* okay yeah this shit is really clean

* sort out minor stuff

* Convert `BreakerFlip`

* `BureaucraticError` + general cleanup

* `DiseaseOutbreak`

* `FalseAlarm`

* `GasLeak`

* `KudzuGrowth`

* `MeteorSwarm`

* `MouseMigration`

* misc errors

* `PowerGridCheck`

* `RandomSentience`

* `VentClog`

* `VentCritters`

* `ZombieOutbreak`

* Rewrite basic event scheduler

* Minor fixes and logging

* ooooops

* errors + fix

* linter

* completions, `RuleStarted` property, update loop fixes

* Tweaks

* Fix #9462

* Basic scheduler update fix, and fixes #8174

* Add test

* UI cleanup

* really this was just for testing
2022-07-10 20:48:41 -05:00

81 lines
2.1 KiB
C#

using System.Threading;
using Content.Server.Chat.Managers;
using Content.Server.GameTicking.Rules.Configurations;
using Timer = Robust.Shared.Timing.Timer;
namespace Content.Server.GameTicking.Rules;
public sealed class MaxTimeRestartRuleSystem : GameRuleSystem
{
[Dependency] private readonly IChatManager _chatManager = default!;
public override string Prototype => "MaxTimeRestart";
private CancellationTokenSource _timerCancel = new();
public TimeSpan RoundMaxTime { get; set; } = TimeSpan.FromMinutes(5);
public TimeSpan RoundEndDelay { get; set; } = TimeSpan.FromSeconds(10);
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<GameRunLevelChangedEvent>(RunLevelChanged);
}
public override void Started()
{
if (Configuration is not MaxTimeRestartRuleConfiguration maxTimeRestartConfig)
return;
RoundMaxTime = maxTimeRestartConfig.RoundMaxTime;
RoundEndDelay = maxTimeRestartConfig.RoundEndDelay;
if(GameTicker.RunLevel == GameRunLevel.InRound)
RestartTimer();
}
public override void Ended()
{
StopTimer();
}
public void RestartTimer()
{
_timerCancel.Cancel();
_timerCancel = new CancellationTokenSource();
Timer.Spawn(RoundMaxTime, TimerFired, _timerCancel.Token);
}
public void StopTimer()
{
_timerCancel.Cancel();
}
private void TimerFired()
{
GameTicker.EndRound(Loc.GetString("rule-time-has-run-out"));
_chatManager.DispatchServerAnnouncement(Loc.GetString("rule-restarting-in-seconds",("seconds", (int) RoundEndDelay.TotalSeconds)));
Timer.Spawn(RoundEndDelay, () => GameTicker.RestartRound());
}
private void RunLevelChanged(GameRunLevelChangedEvent args)
{
if (!RuleAdded)
return;
switch (args.New)
{
case GameRunLevel.InRound:
RestartTimer();
break;
case GameRunLevel.PreRoundLobby:
case GameRunLevel.PostRound:
StopTimer();
break;
}
}
}