|
1 | 1 | using System; |
2 | | -using System.Collections.Generic; |
3 | | -using System.Linq; |
4 | | -using System.Text; |
| 2 | +using System.Collections.ObjectModel; |
| 3 | +using System.Runtime.CompilerServices; |
5 | 4 | using System.Threading; |
6 | 5 | using System.Threading.Tasks; |
| 6 | +using NLog; |
7 | 7 | using Torch; |
8 | 8 | using Torch.API; |
9 | | -using Torch.Commands; |
10 | 9 | using Torch.API.Managers; |
| 10 | +using Torch.Commands; |
11 | 11 | using Torch.Server; |
| 12 | +using Torch.Views; |
12 | 13 |
|
13 | 14 | namespace Essentials |
14 | 15 | { |
15 | | - public class AutoCommand : ViewModel, IDisposable |
| 16 | + public class AutoCommand : ViewModel |
16 | 17 | { |
17 | | - private Timer _timer; |
18 | | - |
| 18 | + private TimeSpan _scheduledTime = TimeSpan.Zero; |
| 19 | + private static readonly Logger Log = LogManager.GetLogger("Essentials"); |
| 20 | + private TimeSpan _interval = TimeSpan.Zero; |
| 21 | + private DateTime _nextRun = DateTime.MinValue; |
| 22 | + private DayOfWeek _day = DayOfWeek.All; |
| 23 | + private int _currentStep; |
| 24 | + private string _name; |
19 | 25 | private bool _enabled; |
20 | | - public bool Enabled { get => _enabled; set { _enabled = value; OnTimerChanged(); OnPropertyChanged(); } } |
21 | | - private string _command; |
22 | | - public string Command { get => _command; set { _command = value; OnTimerChanged(); OnPropertyChanged(); } } |
23 | | - private int _dueTime; |
24 | | - public int DueTime { get => _dueTime / 1000; set { _dueTime = value * 1000; OnTimerChanged(); OnPropertyChanged(); } } |
25 | | - private int _period; |
26 | | - public int Period { get => _period / 1000; set { _period = value * 1000; OnTimerChanged(); OnPropertyChanged(); } } |
27 | | - |
28 | | - private void OnTimerChanged() |
| 26 | + |
| 27 | + [Display(Description = "Enables or disables this command. NOTE: !admin runauto does NOT respect this setting!")] |
| 28 | + public bool Enabled |
| 29 | + { |
| 30 | + get => _enabled; |
| 31 | + set => SetValue(ref _enabled, value); |
| 32 | + } |
| 33 | + |
| 34 | + [Display(Description = "Sets the name of this command. Use this name in conjunction with !admin runauto to trigger the command from ingame or from other auto commands.")] |
| 35 | + public string Name |
29 | 36 | { |
30 | | - _timer?.Dispose(); |
31 | | - if (Enabled && Period > 0) |
32 | | - _timer = new Timer(RunCommand, this, _dueTime, _period); |
| 37 | + get => _name; |
| 38 | + set => SetValue(ref _name, value); |
33 | 39 | } |
34 | 40 |
|
35 | | - private void RunCommand(object state) |
| 41 | + [Display(Name = "Scheduled Time", GroupName = "Schedule", Description = "Sets a time of day for this command to be run. Format is HH:MM:SS. MUST use 24 hour format! Will be reset to zero if Interval is set.")] |
| 42 | + public string ScheduledTime |
36 | 43 | { |
37 | | - if (((TorchServer)TorchBase.Instance).State != ServerState.Running) |
| 44 | + get => _scheduledTime.ToString(); |
| 45 | + set |
| 46 | + { |
| 47 | + _scheduledTime = TimeSpan.Parse(value); |
| 48 | + OnPropertyChanged(); |
| 49 | + if (_scheduledTime != TimeSpan.Zero) |
| 50 | + { |
| 51 | + Interval = TimeSpan.Zero.ToString(); |
| 52 | + _nextRun = DateTime.Now.Date + _scheduledTime; |
| 53 | + if (_nextRun < DateTime.Now) |
| 54 | + _nextRun += TimeSpan.FromDays(1); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + [Display(Description = "Sets an interval for this command to be repeated. Format is HH:MM:SS. Will be reset to zero if Scheduled Time is set!")] |
| 60 | + public string Interval |
| 61 | + { |
| 62 | + get => _interval.ToString(); |
| 63 | + set |
| 64 | + { |
| 65 | + _interval = TimeSpan.Parse(value); |
| 66 | + OnPropertyChanged(); |
| 67 | + if (_interval != TimeSpan.Zero) |
| 68 | + { |
| 69 | + ScheduledTime = TimeSpan.Zero.ToString(); //I hate myself for this |
| 70 | + _nextRun = DateTime.Now + _interval; |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + [Display(Name = "Day of week", GroupName = "Schedule", Description = "Combined with Scheduled Time, will run the command on the given day of the week at the set time.")] |
| 76 | + public DayOfWeek DayOfWeek |
| 77 | + { |
| 78 | + get => _day; |
| 79 | + set => SetValue(ref _day, value); |
| 80 | + } |
| 81 | + |
| 82 | + [Display(Description = "Sub-command steps that will be iterated through once the Interval or Scheduled time is reached.")] |
| 83 | + public ObservableCollection<CommandStep> Steps { get; } = new ObservableCollection<CommandStep>(); |
| 84 | + |
| 85 | + public AutoCommand() |
| 86 | + { |
| 87 | + Steps.CollectionChanged += (sender, args) => OnPropertyChanged(); |
| 88 | + } |
| 89 | + |
| 90 | + public void Update() |
| 91 | + { |
| 92 | + if (DateTime.Now < _nextRun) |
| 93 | + return; |
| 94 | + |
| 95 | + //double cast here as I'm unsure how casting directly between enum types will work |
| 96 | + if (DayOfWeek != DayOfWeek.All && DateTime.Now.DayOfWeek != (System.DayOfWeek)(int)DayOfWeek) |
| 97 | + { |
| 98 | + //adding one day because I can't be bothered to calculate exact interval |
| 99 | + _nextRun += TimeSpan.FromDays(1); |
38 | 100 | return; |
| 101 | + } |
| 102 | + |
| 103 | + if (Steps.Count <= 0) |
| 104 | + return; |
| 105 | + |
| 106 | + var step = Steps[_currentStep]; |
| 107 | + |
| 108 | + step.RunStep(); |
| 109 | + _currentStep++; |
| 110 | + _nextRun += step.DelaySpan; |
39 | 111 |
|
40 | | - var autoCommand = (AutoCommand)state; |
41 | | - TorchBase.Instance.Invoke(() => |
| 112 | + if (_currentStep >= Steps.Count) |
42 | 113 | { |
43 | | - var manager = TorchBase.Instance.CurrentSession.Managers.GetManager<CommandManager>(); |
44 | | - manager?.HandleCommandFromServer(autoCommand.Command); |
45 | | - }); |
| 114 | + _currentStep = 0; |
| 115 | + if (_scheduledTime != TimeSpan.Zero) |
| 116 | + _nextRun = DateTime.Now.Date + _scheduledTime + TimeSpan.FromDays(1); |
| 117 | + else |
| 118 | + _nextRun = DateTime.Now + _interval; |
| 119 | + } |
46 | 120 | } |
47 | 121 |
|
48 | | - ~AutoCommand() |
| 122 | + public class CommandStep : ViewModel |
49 | 123 | { |
50 | | - try |
| 124 | + internal TimeSpan DelaySpan; |
| 125 | + private string _command; |
| 126 | + |
| 127 | + [Display(Description = "Delay AFTER this step and BEFORE the next step. Format is HH:MM:SS.")] |
| 128 | + public string Delay |
51 | 129 | { |
52 | | - Dispose(); |
| 130 | + get => DelaySpan.ToString(); |
| 131 | + set => SetValue(ref DelaySpan, TimeSpan.Parse(value)); |
53 | 132 | } |
54 | | - catch |
| 133 | + |
| 134 | + [Display(Description = "Command to be run as the server.")] |
| 135 | + public string Command |
55 | 136 | { |
56 | | - // ignored |
| 137 | + get => _command; |
| 138 | + set => SetValue(ref _command, value); |
57 | 139 | } |
| 140 | + |
| 141 | + public void RunStep() |
| 142 | + { |
| 143 | + if (((TorchServer)EssentialsPlugin.Instance.Torch).State != ServerState.Running) |
| 144 | + return; |
| 145 | + |
| 146 | + if (string.IsNullOrEmpty(Command)) |
| 147 | + return; |
| 148 | + |
| 149 | + EssentialsPlugin.Instance.Torch.Invoke(() => |
| 150 | + { |
| 151 | + var manager = EssentialsPlugin.Instance.Torch.CurrentSession.Managers.GetManager<CommandManager>(); |
| 152 | + manager?.HandleCommandFromServer(Command); |
| 153 | + }); |
| 154 | + } |
| 155 | + |
| 156 | + public override string ToString() |
| 157 | + { |
| 158 | + return Command; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + |
| 163 | + /// <summary> |
| 164 | + /// Runs the command and all steps immediately, in a new thread |
| 165 | + /// </summary> |
| 166 | + internal void RunNow() |
| 167 | + { |
| 168 | + Task.Run(() => |
| 169 | + { |
| 170 | + foreach (var step in Steps) |
| 171 | + { |
| 172 | + step.RunStep(); |
| 173 | + Thread.Sleep(step.DelaySpan); |
| 174 | + } |
| 175 | + }); |
58 | 176 | } |
59 | 177 |
|
60 | | - /// <inheritdoc /> |
61 | | - public void Dispose() |
| 178 | + public override string ToString() |
62 | 179 | { |
63 | | - _timer?.Dispose(); |
| 180 | + return $"{Name} : {(Enabled ? "Enabled" : "Disabled")} : {Steps.Count}"; |
64 | 181 | } |
65 | 182 | } |
| 183 | + |
| 184 | + public enum DayOfWeek |
| 185 | + { |
| 186 | + All = -1, |
| 187 | + Sunday = System.DayOfWeek.Sunday, |
| 188 | + Monday = System.DayOfWeek.Monday, |
| 189 | + Tuesday = System.DayOfWeek.Tuesday, |
| 190 | + Wednesday = System.DayOfWeek.Wednesday, |
| 191 | + Thursday = System.DayOfWeek.Thursday, |
| 192 | + Friday = System.DayOfWeek.Friday, |
| 193 | + Saturday = System.DayOfWeek.Saturday |
| 194 | + } |
66 | 195 | } |
0 commit comments