CatStation/Content.Server/GameTicking/Rules/InactivityTimeRestartRuleSystem.cs
Princess Cheeseballs 2c6ffecf62
Move GameTicker to Shared (#45834)
* Scope creep my ass

* why was this shit not pushed????

* gone...

* fix that

* review plus buildd

* asfsafsafsafsafas

* grug

* burber

* I'll fix this later

* shrimply die

* also this

* it's snerf or snuthin

* assaads

* round snend

* aaaa

* fix this shit for real

* actually, don't start GameRules until MapInit now that I think aboot it.

* forgot u

* don't actually need this part nullable

* Deffer comp removal

* one liner

* do that too

* chungus

* Honestly makes more sense here, plus WHY are we doing all that shit on "GameRuleAdded" rather than GameRuleStarted(?)

* deferred removal :V

* aaaaaaaaaaaaaaa

* whhop

* Whoop

* better

* landmine that I placed for myself then forgot about lmao

* cleanup

* FUCKING `!`

* gotovo

* amogus

* please don't check by metadata I beg

* waawsasfdfsasfa

* sfafsasfafsas

* fdsaasffsasfa

* code

* fix

* fasasffasfasfsad

* chungaas

* I may be stupid

* gra

* graaaaaaaaaaaaaaaaa

* comment

* gra

* clearing is not predicted and would require major refactors, so don't cache on client.

* lame shit :P

* last holes

* nukeopsfsafsasafsfafsa

* why  was that shit public bruh

* bubrbgwahrghfaka

* dynamic

* revert change that did nothing lmao
2026-09-16 19:34:19 +00:00

110 lines
3.4 KiB
C#

using System.Threading;
using Content.Server.Chat.Managers;
using Content.Server.GameTicking.Rules.Components;
using Content.Shared.GameTicking;
using Content.Shared.GameTicking.Components;
using Content.Shared.GameTicking.Rules;
using Robust.Server.Player;
using Robust.Shared.Player;
using Timer = Robust.Shared.Timing.Timer;
namespace Content.Server.GameTicking.Rules;
public sealed partial class InactivityTimeRestartRuleSystem : GameRuleSystem<InactivityRuleComponent>
{
[Dependency] private IChatManager _chatManager = default!;
[Dependency] private IPlayerManager _playerManager = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<GameRunLevelChangedEvent>(RunLevelChanged);
_playerManager.PlayerStatusChanged += PlayerStatusChanged;
}
public override void Shutdown()
{
base.Shutdown();
_playerManager.PlayerStatusChanged -= PlayerStatusChanged;
}
protected override void Ended(Entity<InactivityRuleComponent> rule, ref GameRuleEndedEvent args)
{
base.Ended(rule, ref args);
StopTimer(rule, rule.Comp);
}
public void RestartTimer(EntityUid uid, InactivityRuleComponent? component = null)
{
if (!Resolve(uid, ref component))
return;
component.TimerCancel.Cancel();
component.TimerCancel = new CancellationTokenSource();
Timer.Spawn(component.InactivityMaxTime, () => TimerFired(uid, component), component.TimerCancel.Token);
}
public void StopTimer(EntityUid uid, InactivityRuleComponent? component = null)
{
if (!Resolve(uid, ref component))
return;
component.TimerCancel.Cancel();
}
private void TimerFired(EntityUid uid, InactivityRuleComponent? component = null)
{
if (!Resolve(uid, ref component))
return;
GameTicker.EndRound(Loc.GetString("rule-time-has-run-out"));
_chatManager.DispatchServerAnnouncement(Loc.GetString("rule-restarting-in-seconds", ("seconds",(int) component.RoundEndDelay.TotalSeconds)));
Timer.Spawn(component.RoundEndDelay, () => GameTicker.RestartRound());
}
private void RunLevelChanged(GameRunLevelChangedEvent args)
{
var query = QueryActiveRules();
while (query.MoveNext(out var uid, out var inactivity, out _, out _))
{
switch (args.New)
{
case GameRunLevel.InRound:
RestartTimer(uid, inactivity);
break;
case GameRunLevel.PreRoundLobby:
case GameRunLevel.PostRound:
StopTimer(uid, inactivity);
break;
}
}
}
private void PlayerStatusChanged(object? sender, SessionStatusEventArgs e)
{
var query = EntityQueryEnumerator<InactivityRuleComponent, GameRuleComponent>();
while (query.MoveNext(out var uid, out var inactivity, out var gameRule))
{
if (!GameTicker.IsGameRuleActive((uid, gameRule)))
return;
if (GameTicker.RunLevel != GameRunLevel.InRound)
{
return;
}
if (_playerManager.PlayerCount == 0)
{
RestartTimer(uid, inactivity);
}
else
{
StopTimer(uid, inactivity);
}
}
}
}