MaxTimeRestartRuleSystem.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System.Threading;
  2. using Content.Server.Chat.Managers;
  3. using Content.Server.GameTicking.Rules.Components;
  4. using Content.Shared.GameTicking.Components;
  5. using Timer = Robust.Shared.Timing.Timer;
  6. namespace Content.Server.GameTicking.Rules;
  7. public sealed class MaxTimeRestartRuleSystem : GameRuleSystem<MaxTimeRestartRuleComponent>
  8. {
  9. [Dependency] private readonly IChatManager _chatManager = default!;
  10. public override void Initialize()
  11. {
  12. base.Initialize();
  13. SubscribeLocalEvent<GameRunLevelChangedEvent>(RunLevelChanged);
  14. }
  15. protected override void Started(EntityUid uid, MaxTimeRestartRuleComponent component, GameRuleComponent gameRule, GameRuleStartedEvent args)
  16. {
  17. base.Started(uid, component, gameRule, args);
  18. if(GameTicker.RunLevel == GameRunLevel.InRound)
  19. RestartTimer(component);
  20. }
  21. protected override void Ended(EntityUid uid, MaxTimeRestartRuleComponent component, GameRuleComponent gameRule, GameRuleEndedEvent args)
  22. {
  23. base.Ended(uid, component, gameRule, args);
  24. StopTimer(component);
  25. }
  26. public void RestartTimer(MaxTimeRestartRuleComponent component)
  27. {
  28. // TODO FULL GAME SAVE
  29. component.TimerCancel.Cancel();
  30. component.TimerCancel = new CancellationTokenSource();
  31. Timer.Spawn(component.RoundMaxTime, () => TimerFired(component), component.TimerCancel.Token);
  32. }
  33. public void StopTimer(MaxTimeRestartRuleComponent component)
  34. {
  35. component.TimerCancel.Cancel();
  36. }
  37. private void TimerFired(MaxTimeRestartRuleComponent component)
  38. {
  39. GameTicker.EndRound(Loc.GetString("rule-time-has-run-out"));
  40. _chatManager.DispatchServerAnnouncement(Loc.GetString("rule-restarting-in-seconds",("seconds", (int) component.RoundEndDelay.TotalSeconds)));
  41. // TODO FULL GAME SAVE
  42. Timer.Spawn(component.RoundEndDelay, () => GameTicker.RestartRound());
  43. }
  44. private void RunLevelChanged(GameRunLevelChangedEvent args)
  45. {
  46. var query = EntityQueryEnumerator<MaxTimeRestartRuleComponent, GameRuleComponent>();
  47. while (query.MoveNext(out var uid, out var timer, out var gameRule))
  48. {
  49. if (!GameTicker.IsGameRuleActive(uid, gameRule))
  50. return;
  51. switch (args.New)
  52. {
  53. case GameRunLevel.InRound:
  54. RestartTimer(timer);
  55. break;
  56. case GameRunLevel.PreRoundLobby:
  57. case GameRunLevel.PostRound:
  58. StopTimer(timer);
  59. break;
  60. }
  61. }
  62. }
  63. }