Revert "Gamerule Entities" (#15724)
This commit is contained in:
@@ -1,14 +1,21 @@
|
||||
using System.Threading;
|
||||
using Content.Server.Chat.Managers;
|
||||
using Content.Server.GameTicking.Rules.Components;
|
||||
using Content.Server.GameTicking.Rules.Configurations;
|
||||
using Timer = Robust.Shared.Timing.Timer;
|
||||
|
||||
namespace Content.Server.GameTicking.Rules;
|
||||
|
||||
public sealed class MaxTimeRestartRuleSystem : GameRuleSystem<MaxTimeRestartRuleComponent>
|
||||
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();
|
||||
@@ -16,60 +23,58 @@ public sealed class MaxTimeRestartRuleSystem : GameRuleSystem<MaxTimeRestartRule
|
||||
SubscribeLocalEvent<GameRunLevelChangedEvent>(RunLevelChanged);
|
||||
}
|
||||
|
||||
protected override void Started(EntityUid uid, MaxTimeRestartRuleComponent component, GameRuleComponent gameRule, GameRuleStartedEvent args)
|
||||
public override void Started()
|
||||
{
|
||||
base.Started(uid, component, gameRule, args);
|
||||
if (Configuration is not MaxTimeRestartRuleConfiguration maxTimeRestartConfig)
|
||||
return;
|
||||
|
||||
RoundMaxTime = maxTimeRestartConfig.RoundMaxTime;
|
||||
RoundEndDelay = maxTimeRestartConfig.RoundEndDelay;
|
||||
|
||||
if(GameTicker.RunLevel == GameRunLevel.InRound)
|
||||
RestartTimer(component);
|
||||
RestartTimer();
|
||||
}
|
||||
|
||||
protected override void Ended(EntityUid uid, MaxTimeRestartRuleComponent component, GameRuleComponent gameRule, GameRuleEndedEvent args)
|
||||
public override void Ended()
|
||||
{
|
||||
base.Ended(uid, component, gameRule, args);
|
||||
|
||||
StopTimer(component);
|
||||
StopTimer();
|
||||
}
|
||||
|
||||
public void RestartTimer(MaxTimeRestartRuleComponent component)
|
||||
public void RestartTimer()
|
||||
{
|
||||
component.TimerCancel.Cancel();
|
||||
component.TimerCancel = new CancellationTokenSource();
|
||||
Timer.Spawn(component.RoundMaxTime, () => TimerFired(component), component.TimerCancel.Token);
|
||||
_timerCancel.Cancel();
|
||||
_timerCancel = new CancellationTokenSource();
|
||||
Timer.Spawn(RoundMaxTime, TimerFired, _timerCancel.Token);
|
||||
}
|
||||
|
||||
public void StopTimer(MaxTimeRestartRuleComponent component)
|
||||
public void StopTimer()
|
||||
{
|
||||
component.TimerCancel.Cancel();
|
||||
_timerCancel.Cancel();
|
||||
}
|
||||
|
||||
private void TimerFired(MaxTimeRestartRuleComponent component)
|
||||
private void TimerFired()
|
||||
{
|
||||
GameTicker.EndRound(Loc.GetString("rule-time-has-run-out"));
|
||||
|
||||
_chatManager.DispatchServerAnnouncement(Loc.GetString("rule-restarting-in-seconds",("seconds", (int) component.RoundEndDelay.TotalSeconds)));
|
||||
_chatManager.DispatchServerAnnouncement(Loc.GetString("rule-restarting-in-seconds",("seconds", (int) RoundEndDelay.TotalSeconds)));
|
||||
|
||||
Timer.Spawn(component.RoundEndDelay, () => GameTicker.RestartRound());
|
||||
Timer.Spawn(RoundEndDelay, () => GameTicker.RestartRound());
|
||||
}
|
||||
|
||||
private void RunLevelChanged(GameRunLevelChangedEvent args)
|
||||
{
|
||||
var query = EntityQueryEnumerator<MaxTimeRestartRuleComponent, GameRuleComponent>();
|
||||
while (query.MoveNext(out var uid, out var timer, out var gameRule))
|
||||
{
|
||||
if (!GameTicker.IsGameRuleActive(uid, gameRule))
|
||||
return;
|
||||
if (!RuleAdded)
|
||||
return;
|
||||
|
||||
switch (args.New)
|
||||
{
|
||||
case GameRunLevel.InRound:
|
||||
RestartTimer(timer);
|
||||
break;
|
||||
case GameRunLevel.PreRoundLobby:
|
||||
case GameRunLevel.PostRound:
|
||||
StopTimer(timer);
|
||||
break;
|
||||
}
|
||||
switch (args.New)
|
||||
{
|
||||
case GameRunLevel.InRound:
|
||||
RestartTimer();
|
||||
break;
|
||||
case GameRunLevel.PreRoundLobby:
|
||||
case GameRunLevel.PostRound:
|
||||
StopTimer();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user