Skip to content
Prev Previous commit
Next Next commit
Vote system fully functional
  • Loading branch information
N1Ran committed Jan 20, 2019
commit 1331a7dfbaa3904282924b527a1f9030ed423ad9
110 changes: 71 additions & 39 deletions Essentials/Commands/VotingModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,21 @@ namespace Essentials.Commands
{
public class VotingModule : CommandModule
{
private static int _votecount;
private static int votePercent;
private static readonly int playerCount = MyMultiplayer.Static.MemberCount - 1;
private static bool _voteInProgress = false;
private static bool _cancelVote = false;
private static string voteInProgress;
private static Dictionary<ulong, DateTime> _voteReg = new Dictionary<ulong, DateTime>();
private static Dictionary<ulong, DateTime> _votetimeout = new Dictionary<ulong, DateTime>();


[Command("vote", "starts a vote for a command")]
[Permission(MyPromoteLevel.None)]
public void Vote(string name)
{
var command = EssentialsPlugin.Instance.Config.AutoCommands.FirstOrDefault(c => c.Name.Equals(name));

if (command == null)
{
Context.Respond($"Couldn't find an auto command with the name {name}");
Expand All @@ -48,29 +49,41 @@ public void Vote(string name)
Context.Respond($"vote for {voteInProgress} is currently active. Use !yes to vote");
return;
}
TimeSpan _voteDuration = TimeSpan.Parse(command.VoteDuration);

if (!command.Votable || command.Percentage == 0)
if (!command.Votable)
{
Context.Respond($"{name} is not set for voting.");
return;
}
voteInProgress = name;
_voteInProgress = true;
Task.Run(() =>
TimeSpan _voteDuration = TimeSpan.Parse(command.VoteDuration);
var steamid = Context.Player.SteamUserId;

// Rexxar's spam blocker

if (_votetimeout.TryGetValue(steamid, out DateTime lastcommand))
{
var countdown = VoteCountdown(_voteDuration).GetEnumerator();
while (countdown.MoveNext())
TimeSpan difference = DateTime.Now - lastcommand;
if (difference.TotalMinutes < 5)
{
Thread.Sleep(1000);
Context.Respond($"Cooldown active. You can use this command again in {difference.TotalMinutes:N0} minutes : {difference.Seconds:N0} seconds");
return;
}
else
{
_votetimeout[steamid] = DateTime.Now;
}
});

var steamid = Context.Player.SteamUserId;
if (_voteReg.TryGetValue(steamid, out DateTime lastcommand))
}

else _votetimeout.Add(steamid, DateTime.Now);

// voting registration filter

if (_voteReg.TryGetValue(steamid, out DateTime lastvote))
{
TimeSpan difference = DateTime.Now - lastcommand;
if (difference.TotalHours < _voteDuration.TotalHours)

TimeSpan difference = DateTime.Now - lastvote;
if (difference.TotalSeconds < _voteDuration.TotalSeconds)
{
Context.Respond($"Your vote has already been submitted. No take backs neither");
return;
Expand All @@ -83,38 +96,57 @@ public void Vote(string name)
else
{
_voteReg.Add(steamid, DateTime.Now);
_votecount++;
}
Context.Torch.CurrentSession?.Managers?.GetManager<IChatManagerServer>()?.SendMessageAsSelf($"Voting started for {name}");
_votecount = 1;


// voting status
voteInProgress = name;
_voteInProgress = true;
if (_voteDuration.TotalSeconds > 10)
{
Context.Torch.CurrentSession?.Managers?.GetManager<IChatManagerServer>()?.SendMessageAsSelf($"Voting started for {name}. " +
$"use '!yes' to vote and '!no' to retract your vote" +
$"voting ends in {_voteDuration.Minutes:N0} minutes : {_voteDuration.Seconds:N0} seconds. ");
}

//vote countdown
Task.Run(() =>
{
var countdown = VoteCountdown(_voteDuration).GetEnumerator();
while (countdown.MoveNext())
{
Thread.Sleep(1000);
}
});
}

[Command("vote cancel", "Cancels current vote in progress") ]
[Permission(MyPromoteLevel.Admin)]
public void VoteCancel()
{
if (_voteInProgress)
{
_cancelVote = true;
}
else
Context.Respond("A vote is not in progress");
}

[Command("no", "cancel your cast vote")]
[Command("no", "cancel your casted vote")]
[Permission(MyPromoteLevel.None)]
public void VoteNo()
{
if (Context.Player == null)
return;

if (!_voteInProgress || _cancelVote)
if (!_voteInProgress)
{
Context.Respond($"no vote in progress");
return;
}
_votecount -= 1;
if (_votecount < 1) VoteCancel();
var steamid = Context.Player.SteamUserId;
_voteReg.Remove(steamid);
Context.Respond("your vote has been retracted");
if (_voteReg.Count < 1) VoteCancel();


}
Expand All @@ -126,7 +158,7 @@ public void VoteYes()
if (Context.Player == null)
return;

if (!_voteInProgress || _cancelVote)
if (!_voteInProgress)
{
Context.Respond($"no vote in progress");
return;
Expand All @@ -149,40 +181,44 @@ public void VoteYes()
else
{
_voteReg.Add(steamid, DateTime.Now);
_votecount++;
}

}

[Command("votecount")]
//debug
[Command("vote debug")]
[Permission(MyPromoteLevel.Admin)]
public void VoteCount()
{
votePercent = (_voteReg.Count / playerCount) * 100;

Context.Respond($"{voteInProgress} is currently active with");
Context.Respond($"vote count: {_votecount} / Vote percent: {votePercent}");
Context.Respond($"vote cancellation is {_cancelVote}");
Context.Respond($"vote in progress is {_voteInProgress}");
Context.Respond($"vote count: {_voteReg.Count} / vote percent: {votePercent}");

}


//votec 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(_cancelVote)
votePercent = (_voteReg.Count / playerCount) * 100;

if (_cancelVote || _voteReg.Count < 1)
{
_voteInProgress = false;
_cancelVote = false;
voteInProgress = null;
Context.Torch.CurrentSession.Managers.GetManager<IChatManagerClient>()
.SendMessageAsSelf($"Vote for {voteInProgress} cancelled");
voteInProgress = "";
_votecount = 0;
votePercent = 0;
voteInProgress = null;
_voteReg.Clear();
yield break;
}
votePercent = (_votecount / playerCount) * 100;

if (i >= 60 && i % 60 == 0)
{
Expand All @@ -200,23 +236,19 @@ private IEnumerable VoteCountdown(TimeSpan time)
}
else
{
if (((_votecount / playerCount) * 100) >= command.Percentage)
if (((_voteReg.Count / playerCount) * 100) >= command.Percentage)
{
Context.Torch.CurrentSession.Managers.GetManager<IChatManagerClient>()
.SendMessageAsSelf($"Vote for {voteInProgress} is successful");
command.RunNow();
_voteInProgress = false;
_cancelVote = false;
_voteReg.Clear();
}
else
{
_voteInProgress = false;
_cancelVote = false;
Context.Torch.CurrentSession.Managers.GetManager<IChatManagerClient>()
.SendMessageAsSelf($"Vote for {voteInProgress} failed");
voteInProgress = "";
_votecount = 0;
votePercent = 0;
_voteInProgress = false;
_voteReg.Clear();
}
yield break;
Expand Down