Skip to content

Commit f1e6262

Browse files
N1Ranrexxar-tc
authored andcommitted
Auto Commands Update Try again (#129)
* AutoCommand update 1/2 * AutoCommands Update 2/2
1 parent 3bda1c9 commit f1e6262

2 files changed

Lines changed: 95 additions & 25 deletions

File tree

‎Essentials/AutoCommand.cs‎

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,28 @@ public class AutoCommand : ViewModel
2525
private DateTime _nextRun = DateTime.MinValue;
2626
private DayOfWeek _day = DayOfWeek.All;
2727
private Trigger _trigger = Trigger.Disabled;
28+
private Gtl _comparer = Gtl.LessThan;
2829
private int _currentStep;
2930
private string _name;
3031
private float _triggerRatio;
3132
private double _triggerCount;
3233

33-
/*
34-
[Display(Description = "Enables or disables this command. NOTE: !admin runauto does NOT respect this setting!")]
35-
public bool Enabled
36-
{
37-
get => _enabled;
38-
set => SetValue(ref _enabled, value);
39-
}
40-
*/
34+
4135
[Display(Name = "Trigger", Description ="Choose a trigger for the command")]
4236
public Trigger CommandTrigger
4337
{
4438
get => _trigger;
4539
set => SetValue(ref _trigger, value);
4640
}
4741

42+
[Display(Name = "Trigger Operator", Description ="Choose a Ratio Comparer for the command")]
43+
public Gtl Compare
44+
{
45+
get => _comparer;
46+
set => SetValue(ref _comparer, value);
47+
}
48+
49+
4850
[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.")]
4951
public string Name
5052
{
@@ -120,13 +122,19 @@ public void Update()
120122
if (DateTime.Now < _nextRun)
121123
return;
122124

123-
if(CommandTrigger == Trigger.Scheduled && Interval == TimeSpan.Zero.ToString())
124-
if (DayOfWeek != DayOfWeek.All && DateTime.Now.DayOfWeek != (System.DayOfWeek)(int)DayOfWeek)
125-
{
125+
switch (CommandTrigger)
126+
{
127+
case Trigger.GridCount:
128+
case Trigger.SimSpeed:
129+
case Trigger.PlayerCount:
130+
RunNow();
131+
_nextRun = DateTime.Now + _interval;
132+
return;
133+
case Trigger.Scheduled when Interval == TimeSpan.Zero.ToString() && DayOfWeek != DayOfWeek.All && DateTime.Now.DayOfWeek != (System.DayOfWeek)(int)DayOfWeek:
126134
//adding one day because I can't be bothered to calculate exact interval
127135
_nextRun += TimeSpan.FromDays(1);
128136
return;
129-
}
137+
}
130138

131139

132140
if (Steps.Count <= 0)
@@ -140,11 +148,9 @@ public void Update()
140148

141149
if (_currentStep < Steps.Count) return;
142150
_currentStep = 0;
143-
if(CommandTrigger == Trigger.Scheduled && Interval == TimeSpan.Zero.ToString())
144-
_nextRun = DateTime.Now.Date + _scheduledTime + TimeSpan.FromDays(1);
145-
else if((CommandTrigger != Trigger.Disabled || CommandTrigger != Trigger.Vote) && Interval != TimeSpan.Zero.ToString())
146-
147-
_nextRun = DateTime.Now + _interval;
151+
_nextRun = _scheduledTime != TimeSpan.Zero
152+
? DateTime.Now.Date + _scheduledTime + TimeSpan.FromDays(1)
153+
: _nextRun = DateTime.Now + _interval;
148154
}
149155

150156

@@ -210,6 +216,13 @@ public override string ToString()
210216
return $"{Name} : {_trigger.ToString()} : {Steps.Count}";
211217
}
212218
}
219+
220+
public enum Gtl
221+
{
222+
LessThan,
223+
GreaterThan,
224+
Equal
225+
}
213226

214227
public enum Trigger
215228
{

‎Essentials/AutoCommands.cs‎

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,81 @@ private bool CanRun(AutoCommand command)
3939
case Trigger.Disabled:
4040
return false;
4141
case Trigger.OnStart:
42-
var a = Math.Max(TimeSpan.Parse(command.Interval).TotalSeconds, 60);
43-
var b = ((ITorchServer)TorchBase.Instance).ElapsedPlayTime;
44-
if ((a - b.TotalSeconds) <= 1 && (a - b.TotalSeconds > 0))
45-
command.RunNow();
46-
break;
42+
return Math.Abs((MyAPIGateway.Session.ElapsedPlayTime - TimeSpan.Parse(command.Interval))
43+
.TotalSeconds) < 1;
4744
case Trigger.Vote:
4845
break;
4946
case Trigger.Timed:
5047
return true;
5148
case Trigger.Scheduled:
5249
return true;
5350
case Trigger.GridCount:
54-
return Tree.Grids.Count >= command.TriggerCount;
51+
var gridCount = MyEntities.GetEntities().OfType<IMyCubeGrid>().Count();
52+
switch (command.Compare)
53+
{
54+
case GreaterThan:
55+
return gridCount > command.TriggerCount;
56+
case LessThan:
57+
return gridCount < command.TriggerCount;
58+
case Equal:
59+
return Math.Abs(gridCount-command.TriggerCount)<1;
60+
default:
61+
throw new Exception("meh");
62+
}
5563
case Trigger.PlayerCount:
56-
return MySession.Static.Players.GetOnlinePlayerCount() >= command.TriggerCount;
64+
switch (command.Compare)
65+
{
66+
case GreaterThan:
67+
return MySession.Static.Players.GetOnlinePlayerCount() >= command.TriggerCount;
68+
case LessThan:
69+
return MySession.Static.Players.GetOnlinePlayerCount() <= command.TriggerCount;
70+
default:
71+
throw new Exception("meh");
72+
}
73+
5774
case Trigger.SimSpeed:
58-
return Math.Min(Sync.ServerSimulationRatio, 1) <= command.TriggerRatio;
75+
var commandActive = _simSpeedCheck.TryGetValue(command, out var time);
76+
switch (command.Compare)
77+
{
78+
case GreaterThan:
79+
if (commandActive)
80+
{
81+
if ((DateTime.Now - time).TotalSeconds < command.TriggerCount) break;
82+
_simSpeedCheck.Remove(command);
83+
return Math.Min(Sync.ServerSimulationRatio, 1) > command.TriggerRatio;
84+
}
85+
86+
if (Math.Min(Sync.ServerSimulationRatio, 1) < command.TriggerRatio) break;
87+
_simSpeedCheck.Add(command, DateTime.Now);
88+
break;
89+
90+
case LessThan:
91+
if (commandActive)
92+
{
93+
if ((DateTime.Now - time).TotalSeconds < command.TriggerCount) break;
94+
_simSpeedCheck.Remove(command);
95+
return Math.Min(Sync.ServerSimulationRatio, 1) < command.TriggerRatio;
96+
}
97+
98+
if (Math.Min(Sync.ServerSimulationRatio, 1) > command.TriggerRatio) break;
99+
_simSpeedCheck.Add(command, DateTime.Now);
100+
break;
101+
102+
case Equal:
103+
if (commandActive)
104+
{
105+
if ((DateTime.Now - time).TotalSeconds < command.TriggerCount) break;
106+
_simSpeedCheck.Remove(command);
107+
return (Math.Abs(Sync.ServerSimulationRatio - command.TriggerRatio) <= 0);
108+
}
59109

110+
if (Math.Abs(Sync.ServerSimulationRatio - command.TriggerRatio)>0)
111+
break;
112+
_simSpeedCheck.Add(command, DateTime.Now);
113+
break;
114+
}
115+
break;
116+
60117
default:
61118
throw new Exception("fuck it");
62119
}

0 commit comments

Comments
 (0)