Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Auto commands updated. Timed and scheduled triggers are now separated
  • Loading branch information
N1Ran committed Mar 16, 2019
commit ba756ba0d0f099c7321b78ace52528cc62a4d8a2
46 changes: 22 additions & 24 deletions Essentials/AutoCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,10 @@ public string ScheduledTime
{
_scheduledTime = TimeSpan.Parse(value);
OnPropertyChanged();
if (_scheduledTime != TimeSpan.Zero)
{
Interval = TimeSpan.Zero.ToString();
_nextRun = DateTime.Now.Date + _scheduledTime;
if (_nextRun < DateTime.Now)
_nextRun += TimeSpan.FromDays(1);
}
if (CommandTrigger != Trigger.Scheduled) return;
_nextRun = DateTime.Now.Date + _scheduledTime;
if (_nextRun < DateTime.Now)
_nextRun += TimeSpan.FromDays(1);
}
}

Expand All @@ -78,11 +75,12 @@ public string Interval
{
_interval = TimeSpan.Parse(value);
OnPropertyChanged();
if (_interval != TimeSpan.Zero)
if (CommandTrigger == Trigger.Timed)
{
ScheduledTime = TimeSpan.Zero.ToString(); //I hate myself for this
//ScheduledTime = TimeSpan.Zero.ToString(); //I hate myself for this **FIXED!!!***
_nextRun = DateTime.Now + _interval;
}

}
}

Expand Down Expand Up @@ -122,13 +120,14 @@ public void Update()
if (DateTime.Now < _nextRun)
return;

//double cast here as I'm unsure how casting directly between enum types will work
if (DayOfWeek != DayOfWeek.All && DateTime.Now.DayOfWeek != (System.DayOfWeek)(int)DayOfWeek)
{
//adding one day because I can't be bothered to calculate exact interval
_nextRun += TimeSpan.FromDays(1);
return;
}
if(CommandTrigger == Trigger.Scheduled && Interval == TimeSpan.Zero.ToString())
if (DayOfWeek != DayOfWeek.All && DateTime.Now.DayOfWeek != (System.DayOfWeek)(int)DayOfWeek)
{
//adding one day because I can't be bothered to calculate exact interval
_nextRun += TimeSpan.FromDays(1);
return;
}


if (Steps.Count <= 0)
return;
Expand All @@ -139,14 +138,13 @@ public void Update()
_currentStep++;
_nextRun += step.DelaySpan;

if (_currentStep >= Steps.Count)
{
_currentStep = 0;
if (_scheduledTime != TimeSpan.Zero)
_nextRun = DateTime.Now.Date + _scheduledTime + TimeSpan.FromDays(1);
else
_nextRun = DateTime.Now + _interval;
}
if (_currentStep < Steps.Count) return;
_currentStep = 0;
if(CommandTrigger == Trigger.Scheduled && Interval == TimeSpan.Zero.ToString())
_nextRun = DateTime.Now.Date + _scheduledTime + TimeSpan.FromDays(1);
else if(CommandTrigger == Trigger.Scheduled && Interval != TimeSpan.Zero.ToString() || CommandTrigger == Trigger.Timed)

_nextRun = DateTime.Now + _interval;
}


Expand Down
45 changes: 17 additions & 28 deletions Essentials/AutoCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,58 +29,47 @@ public void Start()
_timer.Start();
}

public void RunOnStart()
private bool CanRun(AutoCommand command)
{
foreach (var command in EssentialsPlugin.Instance.Config.AutoCommands)
switch (command.CommandTrigger)
{
if (command.CommandTrigger != Trigger.OnStart)
return;
else if(command.CommandTrigger == Trigger.OnStart)
{
case Trigger.Disabled:
break;
case Trigger.OnStart:
var a = TimeSpan.Parse(command.Interval);
var b = ((ITorchServer)TorchBase.Instance).ElapsedPlayTime;
if ((a - b).TotalSeconds <= 1 && (a - b).TotalSeconds > 0)
command.RunNow();
}
}
}
private bool CanRun(AutoCommand command)
{
switch (command.CommandTrigger)
{
default:
return false;
return true;
break;
case Trigger.Vote:
break;
case Trigger.Timed:
return true;
case Trigger.Scheduled:
return true;
case Trigger.GridCount:
int gridCount = 0;
var gridCount = 0;
foreach (var e in MyEntities.GetEntities())
{
if (e is IMyCubeGrid)
gridCount++;
}
if (gridCount >= command.TriggerCount)
return true;
else return false;
return gridCount >= command.TriggerCount;
case Trigger.PlayerCount:
if (MySession.Static.Players.GetOnlinePlayerCount() >= command.TriggerCount)
return true;
else return false;
return MySession.Static.Players.GetOnlinePlayerCount() >= command.TriggerCount;
case Trigger.SimSpeed:
if (Math.Min(Sync.ServerSimulationRatio, 1) <= command.TriggerRatio)
return true;
else
return false;
return Math.Min(Sync.ServerSimulationRatio, 1) <= command.TriggerRatio;

default:
throw new Exception("fuck it");
}

return false;
}


private void TimerElapsed(object sender, ElapsedEventArgs e)
{
RunOnStart();
foreach (var command in EssentialsPlugin.Instance.Config.AutoCommands)
{
if(!CanRun(command))
Expand Down