Skip to content

Commit b85ea4f

Browse files
N1RanYuri
authored andcommitted
Fixes to config (TorchAPI#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 ce75b5d commit b85ea4f

5 files changed

Lines changed: 52 additions & 26 deletions

File tree

‎Essentials/AutoCommand.cs‎

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.ObjectModel;
33
using System.Threading;
44
using System.Threading.Tasks;
5+
using System.Xml.Serialization;
56
using NLog;
67
using Torch;
78
using Torch.API;
@@ -26,45 +27,44 @@ public class AutoCommand : ViewModel
2627
private float _triggerRatio;
2728
private double _triggerCount;
2829

30+
[XmlIgnore]
31+
public bool Completed { get; set; }
2932

30-
[Display(Name = "Trigger", Description ="Choose a trigger for the command")]
33+
[Display(Order = 3, Name = "Trigger", Description ="Choose a trigger for the command")]
3134
public Trigger CommandTrigger
3235
{
3336
get => _trigger;
3437
set => SetValue(ref _trigger, value);
3538
}
3639

37-
[Display(Name = "Trigger Operator", Description ="Choose a Ratio Comparer for the command")]
40+
[Display(Order = 6, Name = "Trigger Operator", Description ="Choose a comparer for the command")]
3841
public Gtl Compare
3942
{
4043
get => _comparer;
4144
set => SetValue(ref _comparer, value);
4245
}
4346

4447

45-
[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.")]
48+
[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.")]
4649
public string Name
4750
{
4851
get => _name;
4952
set => SetValue(ref _name, value);
5053
}
5154

52-
[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.")]
55+
//[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.")]
56+
[Display(Visible = false)]
5357
public string ScheduledTime
5458
{
5559
get => _scheduledTime.ToString();
5660
set
5761
{
5862
_scheduledTime = TimeSpan.Parse(value);
5963
OnPropertyChanged();
60-
if (CommandTrigger != Trigger.Scheduled) return;
61-
_nextRun = DateTime.Now.Date + _scheduledTime;
62-
if (_nextRun < DateTime.Now)
63-
_nextRun += TimeSpan.FromDays(1);
6464
}
6565
}
6666

67-
[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!")]
67+
[Display(Order = 2, Description = "Sets an interval/Time for this command to be repeated. Format is HH:MM:SS.")]
6868
public string Interval
6969
{
7070
get => _interval.ToString();
@@ -78,18 +78,19 @@ public string Interval
7878
_nextRun = DateTime.Now + _interval;
7979
}
8080

81+
8182
}
8283
}
8384

84-
[Display(Name = "Trigger Ratio", Description = "Ratio for Sim Speed or Vote Trigger. 0.5 is equivalent to 50%")]
85+
[Display(Order = 5, Name = "Trigger Ratio", Description = "Ratio for Sim Speed or Vote Trigger. 0.5 is equivalent to 50%")]
8586
public float TriggerRatio
8687
{
8788
get => _triggerRatio;
8889
set => SetValue(ref _triggerRatio, Math.Min(Math.Max(value, 0), 1));
8990

9091
}
9192

92-
[Display(Name = "Trigger Count", Description = "Only use with GridCount or PlayerCount Trigger")]
93+
[Display(Order = 4, Name = "Trigger Count", Description = "Only use with GridCount or PlayerCount Trigger")]
9394
public double TriggerCount
9495
{
9596
get => _triggerCount;
@@ -104,7 +105,7 @@ public DayOfWeek DayOfWeek
104105
set => SetValue(ref _day, value);
105106
}
106107

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

110111
public AutoCommand()
@@ -125,7 +126,7 @@ public void Update()
125126
RunNow();
126127
_nextRun = DateTime.Now + _interval;
127128
return;
128-
case Trigger.Scheduled when Interval == TimeSpan.Zero.ToString() && DayOfWeek != DayOfWeek.All && DateTime.Now.DayOfWeek != (System.DayOfWeek)(int)DayOfWeek:
129+
case Trigger.Scheduled when DayOfWeek != DayOfWeek.All && DateTime.Now.DayOfWeek != (System.DayOfWeek)(int)DayOfWeek:
129130
//adding one day because I can't be bothered to calculate exact interval
130131
_nextRun += TimeSpan.FromDays(1);
131132
return;
@@ -143,8 +144,8 @@ public void Update()
143144

144145
if (_currentStep < Steps.Count) return;
145146
_currentStep = 0;
146-
_nextRun = _scheduledTime != TimeSpan.Zero
147-
? DateTime.Now.Date + _scheduledTime + TimeSpan.FromDays(1)
147+
_nextRun = _trigger == Trigger.Scheduled
148+
? DateTime.Now.Date + _interval + TimeSpan.FromDays(1)
148149
: _nextRun = DateTime.Now + _interval;
149150
}
150151

@@ -219,6 +220,7 @@ public enum Gtl
219220
Equal
220221
}
221222

223+
//TODO Remove Scheduled
222224
public enum Trigger
223225
{
224226
Disabled,

‎Essentials/AutoCommands.cs‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
using Sandbox.Game.World;
1010
using Sandbox.Game.Multiplayer;
1111
using Sandbox.Game.Entities;
12+
using Sandbox.ModAPI;
13+
using Torch.Session;
14+
using Torch.Managers;
1215
using VRage.Game.ModAPI;
1316
using static Essentials.Gtl;
1417

@@ -42,10 +45,10 @@ private bool CanRun(AutoCommand command)
4245
case Trigger.Disabled:
4346
return false;
4447
case Trigger.OnStart:
45-
var a = Math.Max(TimeSpan.Parse(command.Interval).TotalSeconds, 60);
46-
var b = ((ITorchServer)TorchBase.Instance).ElapsedPlayTime;
47-
if ((a - b.TotalSeconds) <= 1 && (a - b.TotalSeconds > 0))
48-
command.RunNow();
48+
if (command.Completed || MySession.Static?.Ready == false)break;
49+
command.Completed = true;
50+
command.RunNow();
51+
4952
break;
5053
case Trigger.Vote:
5154
break;
@@ -67,15 +70,16 @@ private bool CanRun(AutoCommand command)
6770
throw new Exception("meh");
6871
}
6972
case Trigger.PlayerCount:
70-
switch (command.Compare)
73+
74+
if (command.Compare == GreaterThan)
7175
{
72-
case GreaterThan:
73-
return MySession.Static.Players.GetOnlinePlayerCount() >= command.TriggerCount;
74-
case LessThan:
75-
return MySession.Static.Players.GetOnlinePlayerCount() <= command.TriggerCount;
76-
default:
77-
throw new Exception("meh");
76+
return MySession.Static.Players.GetOnlinePlayerCount() > command.TriggerCount;
7877
}
78+
else if (command.Compare == LessThan)
79+
{
80+
return MySession.Static.Players.GetOnlinePlayerCount() < command.TriggerCount;
81+
}
82+
break;
7983

8084
case Trigger.SimSpeed:
8185
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
@@ -65,6 +65,7 @@ public override void Init(ITorchBase torch)
6565
string path = Path.Combine(StoragePath, "Essentials.cfg");
6666
Log.Info($"Attempting to load config from {path}");
6767
_config = Persistent<EssentialsConfig>.Load(path);
68+
ConvertScheduleTime();
6869
_sessionManager = Torch.Managers.GetManager<TorchSessionManager>();
6970
if (_sessionManager != null)
7071
_sessionManager.SessionStateChanged += SessionChanged;
@@ -299,5 +300,18 @@ public override void Dispose()
299300
_sessionManager.SessionStateChanged -= SessionChanged;
300301
_sessionManager = null;
301302
}
303+
304+
//Todo Remove This method next update
305+
public void ConvertScheduleTime()
306+
{
307+
foreach (var command in _config.Data.AutoCommands)
308+
{
309+
if (command.ScheduledTime == TimeSpan.Zero.ToString() || command.CommandTrigger != Trigger.Scheduled) continue;
310+
command.Interval = command.ScheduledTime;
311+
command.ScheduledTime = TimeSpan.Zero.ToString();
312+
}
313+
_config.Save();
314+
315+
}
302316
}
303317
}

‎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)