From ca99404b42db49ff7c698595d77ce41e5bae09bc Mon Sep 17 00:00:00 2001 From: N1Ran <38772205+N1Ran@users.noreply.github.com> Date: Tue, 10 Sep 2019 17:58:50 -0400 Subject: [PATCH] Stop command countdown (#343) Hopefully this works --- Torch/Commands/TorchCommands.cs | 85 ++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 2 deletions(-) diff --git a/Torch/Commands/TorchCommands.cs b/Torch/Commands/TorchCommands.cs index 05c6867..f91f928 100644 --- a/Torch/Commands/TorchCommands.cs +++ b/Torch/Commands/TorchCommands.cs @@ -29,6 +29,8 @@ namespace Torch.Commands { private static bool _restartPending = false; private static bool _cancelRestart = false; + private bool _stopPending = false; + private bool _cancelStop = false; private static readonly Logger Log = LogManager.GetCurrentClassLogger(); [Command("whatsmyip")] @@ -156,7 +158,23 @@ namespace Torch.Commands [Command("stop", "Stops the server.")] public void Stop(bool save = true) { - Context.Respond("Stopping server."); + if (_stopPending) + { + Context.Respond("A stop is already pending."); + return; + } + + _stopPending = true; + Task.Run(() => + { + var countdown = StopCountdown(countdownSeconds, save).GetEnumerator(); + while (countdown.MoveNext()) + { + Thread.Sleep(1000); + } + }); + + /*Context.Respond("Stopping server."); if (save) DoSave()?.ContinueWith((a, mod) => { @@ -165,7 +183,7 @@ namespace Torch.Commands torch.Stop(); }, this, TaskContinuationOptions.RunContinuationsAsynchronously); else - Context.Torch.Stop(); + Context.Torch.Stop();*/ } [Command("restart", "Restarts the server.")] @@ -204,6 +222,69 @@ namespace Torch.Commands else Context.Respond("A restart is not pending."); } + + [Command("stop cancel", "Cancel a pending stop.")] + public void CancelStop() + { + if (_restartPending) + _cancelStop = true; + else + Context.Respond("Server Stop is not pending."); + } + + private IEnumerable StopCountdown(int countdown, bool save) + { + for (var i = countdown; i >= 0; i--) + { + if (_cancelStop) + { + Context.Torch.CurrentSession.Managers.GetManager() + .SendMessageAsSelf($"Stop cancelled."); + + _stopPending = false; + _cancelStop = false; + yield break; + } + + if (i >= 60 && i % 60 == 0) + { + Context.Torch.CurrentSession.Managers.GetManager() + .SendMessageAsSelf($"Stopping server in {i / 60} minute{Pluralize(i / 60)}."); + yield return null; + } + else if (i > 0) + { + if (i < 11) + Context.Torch.CurrentSession.Managers.GetManager() + .SendMessageAsSelf($"Stopping server in {i} second{Pluralize(i)}."); + yield return null; + } + else + { + if (save) + { + Log.Info("Saving game before stop."); + Context.Torch.CurrentSession.Managers.GetManager() + .SendMessageAsSelf($"Saving game before stop."); + DoSave()?.ContinueWith((a, mod) => + { + ITorchBase torch = (mod as CommandModule)?.Context?.Torch; + Debug.Assert(torch != null); + torch.Stop(); + }, this, TaskContinuationOptions.RunContinuationsAsynchronously); + + } + else + { + Log.Info("Stopping server."); + Context.Torch.Invoke(() => Context.Torch.Stop()); + } + + + yield break; + } + } + } private IEnumerable RestartCountdown(int countdown, bool save) {