Skip to content

Commit b64d3b0

Browse files
authored
Merge pull request #95 from N1Ran/master
Auto commands updated. Timed and scheduled triggers are now separated
2 parents 93d35e9 + 2263cf2 commit b64d3b0

4 files changed

Lines changed: 56 additions & 58 deletions

File tree

‎Essentials/AutoCommand.cs‎

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,10 @@ public string ScheduledTime
6060
{
6161
_scheduledTime = TimeSpan.Parse(value);
6262
OnPropertyChanged();
63-
if (_scheduledTime != TimeSpan.Zero)
64-
{
65-
Interval = TimeSpan.Zero.ToString();
66-
_nextRun = DateTime.Now.Date + _scheduledTime;
67-
if (_nextRun < DateTime.Now)
68-
_nextRun += TimeSpan.FromDays(1);
69-
}
63+
if (CommandTrigger != Trigger.Scheduled) return;
64+
_nextRun = DateTime.Now.Date + _scheduledTime;
65+
if (_nextRun < DateTime.Now)
66+
_nextRun += TimeSpan.FromDays(1);
7067
}
7168
}
7269

@@ -78,11 +75,12 @@ public string Interval
7875
{
7976
_interval = TimeSpan.Parse(value);
8077
OnPropertyChanged();
81-
if (_interval != TimeSpan.Zero)
78+
if (CommandTrigger == Trigger.Timed)
8279
{
83-
ScheduledTime = TimeSpan.Zero.ToString(); //I hate myself for this
80+
//ScheduledTime = TimeSpan.Zero.ToString(); //I hate myself for this **FIXED!!!***
8481
_nextRun = DateTime.Now + _interval;
8582
}
83+
8684
}
8785
}
8886

@@ -122,13 +120,14 @@ public void Update()
122120
if (DateTime.Now < _nextRun)
123121
return;
124122

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

133132
if (Steps.Count <= 0)
134133
return;
@@ -139,14 +138,13 @@ public void Update()
139138
_currentStep++;
140139
_nextRun += step.DelaySpan;
141140

142-
if (_currentStep >= Steps.Count)
143-
{
144-
_currentStep = 0;
145-
if (_scheduledTime != TimeSpan.Zero)
146-
_nextRun = DateTime.Now.Date + _scheduledTime + TimeSpan.FromDays(1);
147-
else
148-
_nextRun = DateTime.Now + _interval;
149-
}
141+
if (_currentStep < Steps.Count) return;
142+
_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;
150148
}
151149

152150

‎Essentials/AutoCommands.cs‎

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using NLog;
77
using Torch;
88
using Torch.API;
9+
using Torch.Server.ViewModels;
910
using Sandbox.Game.World;
1011
using Sandbox.Game.Multiplayer;
1112
using Sandbox.Game.Entities;
@@ -16,6 +17,8 @@ namespace Essentials
1617
{
1718
public class AutoCommands : IDisposable
1819
{
20+
protected EntityTreeViewModel Tree { get; }
21+
1922
private static AutoCommands _instance;
2023
public static AutoCommands Instance => _instance ?? (_instance = new AutoCommands());
2124
private static readonly Logger Log = LogManager.GetLogger("Essentials");
@@ -29,58 +32,41 @@ public void Start()
2932
_timer.Start();
3033
}
3134

32-
public void RunOnStart()
35+
private bool CanRun(AutoCommand command)
3336
{
34-
foreach (var command in EssentialsPlugin.Instance.Config.AutoCommands)
37+
switch (command.CommandTrigger)
3538
{
36-
if (command.CommandTrigger != Trigger.OnStart)
37-
return;
38-
else if(command.CommandTrigger == Trigger.OnStart)
39-
{
39+
case Trigger.Disabled:
40+
break;
41+
case Trigger.OnStart:
4042
var a = TimeSpan.Parse(command.Interval);
4143
var b = ((ITorchServer)TorchBase.Instance).ElapsedPlayTime;
4244
if ((a - b).TotalSeconds <= 1 && (a - b).TotalSeconds > 0)
43-
command.RunNow();
44-
}
45-
}
46-
}
47-
private bool CanRun(AutoCommand command)
48-
{
49-
switch (command.CommandTrigger)
50-
{
51-
default:
52-
return false;
45+
return true;
46+
break;
47+
case Trigger.Vote:
48+
break;
5349
case Trigger.Timed:
5450
return true;
5551
case Trigger.Scheduled:
5652
return true;
5753
case Trigger.GridCount:
58-
int gridCount = 0;
59-
foreach (var e in MyEntities.GetEntities())
60-
{
61-
if (e is IMyCubeGrid)
62-
gridCount++;
63-
}
64-
if (gridCount >= command.TriggerCount)
65-
return true;
66-
else return false;
54+
return Tree.Grids.Count >= command.TriggerCount;
6755
case Trigger.PlayerCount:
68-
if (MySession.Static.Players.GetOnlinePlayerCount() >= command.TriggerCount)
69-
return true;
70-
else return false;
56+
return MySession.Static.Players.GetOnlinePlayerCount() >= command.TriggerCount;
7157
case Trigger.SimSpeed:
72-
if (Math.Min(Sync.ServerSimulationRatio, 1) <= command.TriggerRatio)
73-
return true;
74-
else
75-
return false;
58+
return Math.Min(Sync.ServerSimulationRatio, 1) <= command.TriggerRatio;
7659

60+
default:
61+
throw new Exception("fuck it");
7762
}
63+
64+
return false;
7865
}
7966

8067

8168
private void TimerElapsed(object sender, ElapsedEventArgs e)
8269
{
83-
RunOnStart();
8470
foreach (var command in EssentialsPlugin.Instance.Config.AutoCommands)
8571
{
8672
if(!CanRun(command))

‎Essentials/EssentialsConfig.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,15 @@ public EssentialsConfig()
3939
[Display(Name = "MotdURL", Description = "Sets a URL to show to players when they connect. Opens in the steam overlay, if enabled.")]
4040
public string MotdUrl { get => _motdUrl; set => SetValue(ref _motdUrl, value); }
4141

42+
private bool _newUserMotdUrl;
43+
[Display(Name = "Url for New Users Only", Description = "MOTD URL for new users only")]
44+
public bool NewUserMotdUrl{get => _newUserMotdUrl;set => SetValue(ref _newUserMotdUrl, value);}
45+
4246
private bool _stopShips;
4347
[Display(Name = "Stop entities on start", Description = "Stop all entities in the world when the server starts.")]
4448
public bool StopShipsOnStart { get => _stopShips; set => SetValue(ref _stopShips, value); }
4549

50+
4651
private bool _utilityShowPosition;
4752

4853
[Display(Name = "Grid list show position",Description = "Show users the position of all grids they own in the grids list command.")]

‎Essentials/EssentialsPlugin.cs‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ private void MotdOnce(IPlayer player)
216216
public void SendMotd(MyPlayer player)
217217
{
218218
long playerId = player.Identity.IdentityId;
219-
if (!string.IsNullOrEmpty(Config.MotdUrl))
219+
220+
if (!string.IsNullOrEmpty(Config.MotdUrl) && !Config.NewUserMotdUrl)
220221
{
221222
if (MyGuiSandbox.IsUrlWhitelisted(Config.MotdUrl))
222223
MyVisualScriptLogicProvider.OpenSteamOverlay(Config.MotdUrl, playerId);
@@ -233,10 +234,18 @@ public void SendMotd(MyPlayer player)
233234
bool newUser = !Config.KnownSteamIds.Contains(id);
234235
if (newUser)
235236
Config.KnownSteamIds.Add(id);
237+
if (!string.IsNullOrEmpty(Config.MotdUrl) && newUser && Config.NewUserMotdUrl)
238+
{
239+
if (MyGuiSandbox.IsUrlWhitelisted(Config.MotdUrl))
240+
MyVisualScriptLogicProvider.OpenSteamOverlay(Config.MotdUrl, playerId);
241+
else
242+
MyVisualScriptLogicProvider.OpenSteamOverlay($"https://steamcommunity.com/linkfilter/?url={Config.MotdUrl}", playerId);
243+
}
236244

237245
if (newUser && !string.IsNullOrEmpty(Config.NewUserMotd))
238246
{
239247
ModCommunication.SendMessageTo(new DialogMessage(MySession.Static.Name, "New User Message Of The Day", Config.NewUserMotd.Replace("%player%", name)), id);
248+
240249
}
241250
else if (!string.IsNullOrEmpty(Config.Motd))
242251
{

0 commit comments

Comments
 (0)