Files
OldThink/Content.Server/GameTicking/Rules/MaxTimeRestartRuleSystem.cs

76 lines
2.5 KiB
C#
Raw Normal View History

2021-12-21 21:23:29 +01:00
using System.Threading;
using Content.Server.Chat.Managers;
2023-04-25 20:23:14 -04:00
using Content.Server.GameTicking.Rules.Components;
2021-12-21 21:23:29 +01:00
using Timer = Robust.Shared.Timing.Timer;
namespace Content.Server.GameTicking.Rules;
2023-04-25 20:23:14 -04:00
public sealed class MaxTimeRestartRuleSystem : GameRuleSystem<MaxTimeRestartRuleComponent>
2021-12-21 21:23:29 +01:00
{
[Dependency] private readonly IChatManager _chatManager = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<GameRunLevelChangedEvent>(RunLevelChanged);
}
2023-04-25 20:23:14 -04:00
protected override void Started(EntityUid uid, MaxTimeRestartRuleComponent component, GameRuleComponent gameRule, GameRuleStartedEvent args)
2021-12-21 21:23:29 +01:00
{
2023-04-25 20:23:14 -04:00
base.Started(uid, component, gameRule, args);
2021-12-21 21:23:29 +01:00
if(GameTicker.RunLevel == GameRunLevel.InRound)
2023-04-25 20:23:14 -04:00
RestartTimer(component);
2021-12-21 21:23:29 +01:00
}
2023-04-25 20:23:14 -04:00
protected override void Ended(EntityUid uid, MaxTimeRestartRuleComponent component, GameRuleComponent gameRule, GameRuleEndedEvent args)
2021-12-21 21:23:29 +01:00
{
2023-04-25 20:23:14 -04:00
base.Ended(uid, component, gameRule, args);
StopTimer(component);
2021-12-21 21:23:29 +01:00
}
2023-04-25 20:23:14 -04:00
public void RestartTimer(MaxTimeRestartRuleComponent component)
2021-12-21 21:23:29 +01:00
{
2023-04-25 20:23:14 -04:00
component.TimerCancel.Cancel();
component.TimerCancel = new CancellationTokenSource();
Timer.Spawn(component.RoundMaxTime, () => TimerFired(component), component.TimerCancel.Token);
2021-12-21 21:23:29 +01:00
}
2023-04-25 20:23:14 -04:00
public void StopTimer(MaxTimeRestartRuleComponent component)
2021-12-21 21:23:29 +01:00
{
2023-04-25 20:23:14 -04:00
component.TimerCancel.Cancel();
2021-12-21 21:23:29 +01:00
}
2023-04-25 20:23:14 -04:00
private void TimerFired(MaxTimeRestartRuleComponent component)
2021-12-21 21:23:29 +01:00
{
GameTicker.EndRound(Loc.GetString("rule-time-has-run-out"));
2023-04-25 20:23:14 -04:00
_chatManager.DispatchServerAnnouncement(Loc.GetString("rule-restarting-in-seconds",("seconds", (int) component.RoundEndDelay.TotalSeconds)));
2021-12-21 21:23:29 +01:00
2023-04-25 20:23:14 -04:00
Timer.Spawn(component.RoundEndDelay, () => GameTicker.RestartRound());
2021-12-21 21:23:29 +01:00
}
private void RunLevelChanged(GameRunLevelChangedEvent args)
{
2023-04-25 20:23:14 -04:00
var query = EntityQueryEnumerator<MaxTimeRestartRuleComponent, GameRuleComponent>();
while (query.MoveNext(out var uid, out var timer, out var gameRule))
2021-12-21 21:23:29 +01:00
{
2023-04-25 20:23:14 -04:00
if (!GameTicker.IsGameRuleActive(uid, gameRule))
return;
switch (args.New)
{
case GameRunLevel.InRound:
RestartTimer(timer);
break;
case GameRunLevel.PreRoundLobby:
case GameRunLevel.PostRound:
StopTimer(timer);
break;
}
2021-12-21 21:23:29 +01:00
}
}
}