Skip to content

Commit 5be9e1b

Browse files
authored
Fixes to config (#178)
Updated codes for autocommands OnStart Command now only runs immediately on session ready ONCE. Removed ScheduledTime in autocommands Add method to convert ScheduleTime to Interval for commands with Scheduled Trigger (new method) Co-authored-by: Olatide F <tide1988@live.com>
1 parent b5d9e6d commit 5be9e1b

5 files changed

Lines changed: 51 additions & 26 deletions

File tree

‎Essentials/AutoCommand.cs‎

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Runtime.CompilerServices;
55
using System.Threading;
66
using System.Threading.Tasks;
7+
using System.Xml.Serialization;
78
using NLog;
89
using Torch;
910
using Torch.API;
@@ -31,45 +32,44 @@ public class AutoCommand : ViewModel
3132
private float _triggerRatio;
3233
private double _triggerCount;
3334

35+
[XmlIgnore]
36+
public bool Completed { get; set; }
3437

35-
[Display(Name = "Trigger", Description ="Choose a trigger for the command")]
38+
[Display(Order = 3, Name = "Trigger", Description ="Choose a trigger for the command")]
3639
public Trigger CommandTrigger
3740
{
3841
get => _trigger;
3942
set => SetValue(ref _trigger, value);
4043
}
4144

42-
[Display(Name = "Trigger Operator", Description ="Choose a Ratio Comparer for the command")]
45+
[Display(Order = 6, Name = "Trigger Operator", Description ="Choose a comparer for the command")]
4346
public Gtl Compare
4447
{
4548
get => _comparer;
4649
set => SetValue(ref _comparer, value);
4750
}
4851

4952

50-
[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.")]
53+
[Display(Order = 1, 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.")]
5154
public string Name
5255
{
5356
get => _name;
5457
set => SetValue(ref _name, value);
5558
}
5659

57-
[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.")]
60+
//[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.")]
61+
[Display(Visible = false)]
5862
public string ScheduledTime
5963
{
6064
get => _scheduledTime.ToString();
6165
set
6266
{
6367
_scheduledTime = TimeSpan.Parse(value);
6468
OnPropertyChanged();
65-
if (CommandTrigger != Trigger.Scheduled) return;
66-
_nextRun = DateTime.Now.Date + _scheduledTime;
67-
if (_nextRun < DateTime.Now)
68-
_nextRun += TimeSpan.FromDays(1);
6969
}
7070
}
7171

72-
[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!")]
72+
[Display(Order = 2, Description = "Sets an interval/Time for this command to be repeated. Format is HH:MM:SS.")]
7373
public string Interval
7474
{
7575
get => _interval.ToString();
@@ -83,18 +83,19 @@ public string Interval
8383
_nextRun = DateTime.Now + _interval;
8484
}
8585

86+
8687
}
8788
}
8889

89-
[Display(Name = "Trigger Ratio", Description = "Ratio for Sim Speed or Vote Trigger. 0.5 is equivalent to 50%")]
90+
[Display(Order = 5, Name = "Trigger Ratio", Description = "Ratio for Sim Speed or Vote Trigger. 0.5 is equivalent to 50%")]
9091
public float TriggerRatio
9192
{
9293
get => _triggerRatio;
9394
set => SetValue(ref _triggerRatio, Math.Min(Math.Max(value, 0), 1));
9495

9596
}
9697

97-
[Display(Name = "Trigger Count", Description = "Only use with GridCount or PlayerCount Trigger")]
98+
[Display(Order = 4, Name = "Trigger Count", Description = "Only use with GridCount or PlayerCount Trigger")]
9899
public double TriggerCount
99100
{
100101
get => _triggerCount;
@@ -109,7 +110,7 @@ public DayOfWeek DayOfWeek
109110
set => SetValue(ref _day, value);
110111
}
111112

112-
[Display(Description = "Sub-command steps that will be iterated through once the Interval or Scheduled time is reached.")]
113+
[Display(Order = 7, Description = "Sub-command steps that will be iterated through once the Interval or Scheduled time is reached.")]
113114
public ObservableCollection<CommandStep> Steps { get; } = new ObservableCollection<CommandStep>();
114115

115116
public AutoCommand()
@@ -130,7 +131,7 @@ public void Update()
130131
RunNow();
131132
_nextRun = DateTime.Now + _interval;
132133
return;
133-
case Trigger.Scheduled when Interval == TimeSpan.Zero.ToString() && DayOfWeek != DayOfWeek.All && DateTime.Now.DayOfWeek != (System.DayOfWeek)(int)DayOfWeek:
134+
case Trigger.Scheduled when DayOfWeek != DayOfWeek.All && DateTime.Now.DayOfWeek != (System.DayOfWeek)(int)DayOfWeek:
134135
//adding one day because I can't be bothered to calculate exact interval
135136
_nextRun += TimeSpan.FromDays(1);
136137
return;
@@ -148,8 +149,8 @@ public void Update()
148149

149150
if (_currentStep < Steps.Count) return;
150151
_currentStep = 0;
151-
_nextRun = _scheduledTime != TimeSpan.Zero
152-
? DateTime.Now.Date + _scheduledTime + TimeSpan.FromDays(1)
152+
_nextRun = _trigger == Trigger.Scheduled
153+
? DateTime.Now.Date + _interval + TimeSpan.FromDays(1)
153154
: _nextRun = DateTime.Now + _interval;
154155
}
155156

@@ -224,6 +225,7 @@ public enum Gtl
224225
Equal
225226
}
226227

228+
//TODO Remove Scheduled
227229
public enum Trigger
228230
{
229231
Disabled,

‎Essentials/AutoCommands.cs‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
using Sandbox.Game.Multiplayer;
1212
using Sandbox.Game.Entities;
1313
using Sandbox.ModAPI;
14+
using Torch.Session;
15+
using Torch.Managers;
1416
using VRage.Game.ModAPI;
1517
using static Essentials.Gtl;
1618

@@ -43,10 +45,10 @@ private bool CanRun(AutoCommand command)
4345
case Trigger.Disabled:
4446
return false;
4547
case Trigger.OnStart:
46-
var a = Math.Max(TimeSpan.Parse(command.Interval).TotalSeconds, 60);
47-
var b = ((ITorchServer)TorchBase.Instance).ElapsedPlayTime;
48-
if ((a - b.TotalSeconds) <= 1 && (a - b.TotalSeconds > 0))
49-
command.RunNow();
48+
if (command.Completed || MySession.Static?.Ready == false)break;
49+
command.Completed = true;
50+
command.RunNow();
51+
5052
break;
5153
case Trigger.Vote:
5254
break;
@@ -68,15 +70,16 @@ private bool CanRun(AutoCommand command)
6870
throw new Exception("meh");
6971
}
7072
case Trigger.PlayerCount:
71-
switch (command.Compare)
73+
74+
if (command.Compare == GreaterThan)
7275
{
73-
case GreaterThan:
74-
return MySession.Static.Players.GetOnlinePlayerCount() >= command.TriggerCount;
75-
case LessThan:
76-
return MySession.Static.Players.GetOnlinePlayerCount() <= command.TriggerCount;
77-
default:
78-
throw new Exception("meh");
76+
return MySession.Static.Players.GetOnlinePlayerCount() > command.TriggerCount;
7977
}
78+
else if (command.Compare == LessThan)
79+
{
80+
return MySession.Static.Players.GetOnlinePlayerCount() < command.TriggerCount;
81+
}
82+
break;
8083

8184
case Trigger.SimSpeed:
8285
var commandActive = _simSpeedCheck.TryGetValue(command, out var time);

‎Essentials/EssentialsConfig.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public EssentialsConfig()
3434
public ObservableCollection<InfoCommand> InfoCommands { get; } = new ObservableCollection<InfoCommand>();
3535

3636
private string _motd;
37+
[Display(Name = "Motd", Description = "Message displayed to players upon connection")]
3738
public string Motd { get => _motd; set => SetValue(ref _motd, value); }
3839

3940
public bool _enableRanks = false;

‎Essentials/EssentialsPlugin.cs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public override void Init(ITorchBase torch)
6868
string path = Path.Combine(StoragePath, "Essentials.cfg");
6969
Log.Info($"Attempting to load config from {path}");
7070
_config = Persistent<EssentialsConfig>.Load(path);
71+
ConvertScheduleTime();
7172
_sessionManager = Torch.Managers.GetManager<TorchSessionManager>();
7273
if (_sessionManager != null)
7374
_sessionManager.SessionStateChanged += SessionChanged;
@@ -298,5 +299,18 @@ public override void Dispose()
298299
_sessionManager.SessionStateChanged -= SessionChanged;
299300
_sessionManager = null;
300301
}
302+
303+
//Todo Remove This method next update
304+
public void ConvertScheduleTime()
305+
{
306+
foreach (var command in _config.Data.AutoCommands)
307+
{
308+
if (command.ScheduledTime == TimeSpan.Zero.ToString() || command.CommandTrigger != Trigger.Scheduled) continue;
309+
command.Interval = command.ScheduledTime;
310+
command.ScheduledTime = TimeSpan.Zero.ToString();
311+
}
312+
_config.Save();
313+
314+
}
301315
}
302316
}

‎Essentials/InfoCommand.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Runtime.CompilerServices;
22
using Torch;
3+
using Torch.Views;
34

45
namespace Essentials
56
{
@@ -10,24 +11,28 @@ public class InfoCommand : ViewModel
1011
private string _dialogResponse;
1112
private string _urlResponse;
1213

14+
[Display(Order = 1, Name = "Command", Description = "Type this in chat to activate command")]
1315
public string Command
1416
{
1517
get => _command;
1618
set => SetValue(ref _command, value);
1719
}
1820

21+
[Display(Order = 2, Name = "Chat Response", Description = "Chat response to command")]
1922
public string ChatResponse
2023
{
2124
get => _chatResponse;
2225
set =>SetValue(ref _chatResponse, value);
2326
}
2427

28+
[Display(Order = 3, Name = "Dialog Response", Description = "Dialog box response")]
2529
public string DialogResponse
2630
{
2731
get => _dialogResponse;
2832
set => SetValue(ref _dialogResponse, value);
2933
}
3034

35+
[Display(Order = 4, Name = "URL", Description = "url response to command")]
3136
public string URL
3237
{
3338
get => _urlResponse;

0 commit comments

Comments
 (0)