Skip to content
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
dad1a19
Now we're cooking
N1Ran Jan 27, 2019
617f8a4
Voting updated with reset for admins
N1Ran Feb 2, 2019
93e01e5
This is the percentage limiter for voting system...derp
N1Ran Feb 2, 2019
c2cd494
funsies
N1Ran Feb 3, 2019
b21be2d
?
N1Ran Feb 3, 2019
fedea70
voting module updated
N1Ran Feb 6, 2019
1591f95
voting module updated
N1Ran Feb 7, 2019
8347c1f
voting module updated.
N1Ran Feb 7, 2019
77dc054
Merge branch 'Patron' into ToPatron
N1Ran Feb 9, 2019
aeb591a
GPS Marker for grids list command
N1Ran Feb 9, 2019
b9239dc
some faction functions
N1Ran Feb 9, 2019
514ea6e
List players online
N1Ran Feb 9, 2019
1ba4878
Faction functions
N1Ran Feb 10, 2019
a33e369
New method to calculate vote
N1Ran Feb 10, 2019
6592cbe
new method to calculate votes
N1Ran Feb 10, 2019
86af0de
twe
N1Ran Feb 10, 2019
72d72e0
tweak
N1Ran Feb 10, 2019
b44e4df
Booyah
N1Ran Feb 11, 2019
0160488
Added command to list players online with admin command (useful for d…
N1Ran Feb 11, 2019
e8a59a6
Added GPS marker option to Grids list command
N1Ran Feb 11, 2019
5cb7f03
added faction list and faction remove option to faction command
N1Ran Feb 11, 2019
a528dc1
Resolving conflict before uploading
N1Ran Feb 11, 2019
7271434
Rex's faction removal merged
N1Ran Feb 11, 2019
cb3d6c7
updated with patrol Torch
N1Ran Feb 11, 2019
3c4d312
comma makes a major difference
N1Ran Feb 13, 2019
e602ea5
Resolve
N1Ran Feb 14, 2019
57f4262
Merger
N1Ran Feb 14, 2019
cecb2de
New Faction feation
N1Ran Feb 15, 2019
3115e8d
AutoCommands revamp in progress
N1Ran Feb 16, 2019
9722ca0
Yet another fix for the damned voting module
N1Ran Feb 17, 2019
3a71685
AutoCommands major tweaks and another go at fixing the damned voting …
N1Ran Feb 17, 2019
0e8019e
Let's get those new goodies loaded and decrease conflicts
N1Ran Feb 17, 2019
3a6acc4
meh
N1Ran Feb 17, 2019
7c7442b
Remove Old method and added OnStart Trigger to AutoCommands
N1Ran Feb 18, 2019
2958523
Run on Start trigger added
N1Ran Feb 18, 2019
ffc03e6
updated
N1Ran Feb 18, 2019
326820a
Here are the commit addressing the review.
N1Ran Feb 20, 2019
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
64 changes: 41 additions & 23 deletions Essentials/AutoCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.CompilerServices;
using System.Threading;
Expand All @@ -7,9 +8,12 @@
using Torch;
using Torch.API;
using Torch.API.Managers;
using Torch.Server.ViewModels.Entities;
using Torch.Commands;
using Torch.Server;
using Torch.Views;
using Sandbox.Game.World;
using Sandbox.Game.Entities;

namespace Essentials
{
Expand All @@ -20,21 +24,27 @@ public class AutoCommand : ViewModel
private TimeSpan _interval = TimeSpan.Zero;
private DateTime _nextRun = DateTime.MinValue;
private DayOfWeek _day = DayOfWeek.All;
private Trigger _trigger = Trigger.Disabled;
private int _currentStep;
private int _votepercentage;
private TimeSpan _voteDuration = TimeSpan.Zero;
private string _name;
private bool _enabled;
private bool _votable;
private float _triggerRatio;
private double _triggerCount;

/*
[Display(Description = "Enables or disables this command. NOTE: !admin runauto does NOT respect this setting!")]
public bool Enabled
{
get => _enabled;
set => SetValue(ref _enabled, value);
}


*/
[Display(Name = "Trigger", Description ="Choose a trigger for the command")]
public Trigger CommandTrigger
{
get => _trigger;
set => SetValue(ref _trigger, value);
}

[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.")]
public string Name
{
Expand Down Expand Up @@ -76,25 +86,19 @@ public string Interval
}
}

[Display(Description = "Adds voting option to this command. NOTE: A successful vote will activate this command")]
public bool Votable
[Display(Name = "Trigger Ratio", Description = "Ratio for Sim Speed and Vote Triggers. 0.5 is equivalent to 50%")]
public float TriggerRatio
{
get => _votable;
set => SetValue(ref _votable, value);
}
get => _triggerRatio;
set => SetValue(ref _triggerRatio, Math.Min(Math.Max(value, 0), 1));

[Display(Name = "Vote Duration", Description = "Sets the duration for the vote. NOTE: Make sure to check the votable box to activate. Format is HH:MM:SS.")]
public string VoteDuration
{
get => _voteDuration.ToString();
set => SetValue(ref _voteDuration, TimeSpan.Parse(value));
}

[Display(Name = "Vote Percentage", Description = "Sets the percentage Yes vote needed for the command to activate. NOTE: This only works if the votable box is checked")]
public int Percentage
[Display(Name = "Trigger Count", Description = "Only use with")]
public double TriggerCount
{
get => _votepercentage;
set => SetValue(ref _votepercentage, Math.Min(Math.Max(value, 0), 100));
get => _triggerCount;
set => SetValue(ref _triggerCount, Math.Max(0, value));

}

[Display(Name = "Day of week", GroupName = "Schedule", Description = "Combined with Scheduled Time, will run the command on the given day of the week at the set time.")]
Expand All @@ -117,7 +121,7 @@ public void Update()
if (DateTime.Now < _nextRun)
return;

//double cast here as I'm unsure how casting directly between enum types will work
//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
Expand All @@ -144,6 +148,8 @@ public void Update()
}
}



public class CommandStep : ViewModel
{
internal TimeSpan DelaySpan;
Expand Down Expand Up @@ -202,10 +208,22 @@ internal void RunNow()

public override string ToString()
{
return $"{Name} : {(Enabled ? "Enabled" : "Disabled")} : {Steps.Count}";
return $"{Name} : {_trigger.ToString()} : {Steps.Count}";
}
}

public enum Trigger
{
Disabled = -1,
Comment thread
rexxar-tc marked this conversation as resolved.
Outdated
Timed,
Scheduled,
Vote,
PlayerCount,
GridCount,
SimSpeed,
OnStart
}

public enum DayOfWeek
{
All = -1,
Expand Down
58 changes: 56 additions & 2 deletions Essentials/AutoCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
using System.Text;
using System.Timers;
using NLog;
using Torch;
using Torch.API;
using Sandbox.Game.World;
using Sandbox.Game.Multiplayer;
using Sandbox.Game.Entities;
using VRage.Game.ModAPI;


namespace Essentials
{
Expand All @@ -12,7 +19,6 @@ public class AutoCommands : IDisposable
private static AutoCommands _instance;
public static AutoCommands Instance => _instance ?? (_instance = new AutoCommands());
private static readonly Logger Log = LogManager.GetLogger("Essentials");

private Timer _timer;

public void Start()
Expand All @@ -23,11 +29,59 @@ public void Start()
_timer.Start();
}

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

}
}


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

try
Expand Down
54 changes: 27 additions & 27 deletions Essentials/Commands/VotingModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NLog;
using Sandbox.Game.World;
using Sandbox.Engine.Multiplayer;
using Torch.API.Managers;
Expand Down Expand Up @@ -39,13 +38,13 @@ public void Vote(string name)

if (VoteStatus == Status.voteInProgress)
{
Context.Respond($"vote for {voteInProgress} is currently active. Use [!yes] to vote");
Context.Respond($"vote for {voteInProgress} is currently active. Use [!yes] to vote and [!no] to retract vote");
return;
}

var command = EssentialsPlugin.Instance.Config.AutoCommands.FirstOrDefault(c => c.Name.Equals(name));

if (command == null || !command.Votable)
if (command == null || command.CommandTrigger != Trigger.Vote)
Comment thread
rexxar-tc marked this conversation as resolved.
{
Context.Respond($"Couldn't find any votable command with the name [{name}]");
return;
Expand All @@ -71,7 +70,7 @@ public void Vote(string name)

else _voteCooldown.Add(steamid, DateTime.Now.AddMinutes(_cooldown));

TimeSpan _voteDuration = TimeSpan.Parse(command.VoteDuration);
TimeSpan _voteDuration = TimeSpan.Parse(command.Interval);
// voting status
voteInProgress = name;
VoteStatus = Status.voteInProgress;
Expand Down Expand Up @@ -146,7 +145,7 @@ public void VoteYes()
if (_voteReg.TryGetValue(steamid, out DateTime lastcommand))
{
TimeSpan difference = DateTime.Now - lastcommand;
TimeSpan _voteDuration = TimeSpan.Parse(command.VoteDuration);
TimeSpan _voteDuration = TimeSpan.Parse(command.Interval);
if (difference.TotalSeconds < _voteDuration.TotalSeconds)
{
Context.Respond($"Your vote has already been submitted.");
Expand Down Expand Up @@ -179,7 +178,7 @@ public void VoteDebgug()
sb.AppendLine("Last vote info");
if (lastVoteName != null)
sb.AppendLine($"Last vote: {lastVoteName.ToString()}");
sb.AppendLine($"Last Vote Result: {voteResult}");
sb.AppendLine($"Last Vote Result: {voteResult.ToString()}");
sb.AppendLine($"Last vote percent: {voteResultPercentage}");
Context.Respond(sb.ToString());

Expand All @@ -206,19 +205,17 @@ public void VoteReset()
//vote countdown
private IEnumerable VoteCountdown(TimeSpan time)
{
var command = EssentialsPlugin.Instance.Config.AutoCommands.FirstOrDefault(c => c.Name.Equals(voteInProgress));


for (var i = time.TotalSeconds; i >= 0; i--)
{

if (VoteStatus == Status.voteCancel || _voteReg.Count < 1)
if (VoteStatus != Status.voteInProgress || _voteReg.Count < 1)
{
Context.Torch.CurrentSession.Managers.GetManager<IChatManagerClient>()
.SendMessageAsSelf($"Vote for {voteInProgress} cancelled");
voteResult = Status.voteCancel;
VoteEnd();

yield break;
}

Expand All @@ -238,31 +235,34 @@ private IEnumerable VoteCountdown(TimeSpan time)
}
else
{
var _votePercent = 100 * (_voteReg.Count / MySession.Static.Players.GetOnlinePlayerCount());
if (_votePercent >= command.Percentage)
var command = EssentialsPlugin.Instance.Config.AutoCommands.FirstOrDefault(c => c.Name.Equals(voteInProgress));
double vr = ((_voteReg.Count / MySession.Static.Players.GetOnlinePlayerCount()) * 100);
Task.Delay(5000).ContinueWith(_ =>
{
Context.Torch.CurrentSession.Managers.GetManager<IChatManagerClient>()
.SendMessageAsSelf($"Vote for {voteInProgress} is successful");
voteResult = Status.voteSuccess;
command.RunNow();
}
else
{
Context.Torch.CurrentSession.Managers.GetManager<IChatManagerClient>()
.SendMessageAsSelf($"Vote for {voteInProgress} failed");
voteResult = Status.voteFail;
}
voteResultPercentage = _votePercent;
if (vr >= command.TriggerRatio)
{
Context.Torch.CurrentSession.Managers.GetManager<IChatManagerClient>()
.SendMessageAsSelf($"Vote for {voteInProgress} is successful");
voteResult = Status.voteSuccess;
command.RunNow();
}
else if (vr < command.TriggerRatio)
{
Context.Torch.CurrentSession.Managers.GetManager<IChatManagerClient>()
.SendMessageAsSelf($"Vote for {voteInProgress} failed");
voteResult = Status.voteFail;
}
});
voteResultPercentage = vr;
VoteEnd();

yield break;
}
}
}



public void VoteEnd()
{

//Make sure it's all good for next round
VoteStatus = Status.voteStandby;
voteInProgress = null;
Expand All @@ -275,7 +275,7 @@ public enum Status
voteInProgress,
voteCancel,
voteEnd,

// Last vote
voteFail,
voteSuccess
Expand Down
Loading