55using System . Collections . Generic ;
66using System . Linq ;
77using System . Threading . Tasks ;
8+ using NLog ;
89using Sandbox . Game . World ;
10+ using Sandbox . Engine . Multiplayer ;
911using Torch . API . Managers ;
1012using Torch . Commands ;
1113using Torch . Commands . Permissions ;
@@ -16,13 +18,17 @@ namespace Essentials.Commands
1618{
1719 public class VotingModule : CommandModule
1820 {
19- private static int votePercent ;
20- private static int _cooldown = 0 ;
21+ private static Random rnd = new Random ( ) ;
22+ private static int _cooldown = rnd . Next ( 5 , 30 ) ;
2123 private static string voteInProgress ;
2224 private static Dictionary < ulong , DateTime > _voteReg = new Dictionary < ulong , DateTime > ( ) ;
23- private static Dictionary < ulong , DateTime > _votetimeout = new Dictionary < ulong , DateTime > ( ) ;
24- public static Status VoteStatus = Status . VoteEnded ;
25+ private static Dictionary < ulong , DateTime > _voteCooldown = new Dictionary < ulong , DateTime > ( ) ;
26+ public static Status VoteStatus = Status . voteStandby ;
2527
28+ //last vote info for debugging
29+ private static string lastVoteName ;
30+ private static Status voteResult ;
31+ private static double voteResultPercentage ;
2632
2733 [ Command ( "vote" , "starts a vote for a command" ) ]
2834 [ Permission ( MyPromoteLevel . None ) ]
@@ -31,65 +37,45 @@ public void Vote(string name)
3137 if ( Context . Player == null )
3238 return ;
3339
34- if ( VoteStatus == Status . VoteInProgress )
40+ if ( VoteStatus == Status . voteInProgress )
3541 {
36- Context . Respond ( $ "vote for { voteInProgress } is currently active. Use !yes to vote") ;
42+ Context . Respond ( $ "vote for { voteInProgress } is currently active. Use [ !yes] to vote") ;
3743 return ;
3844 }
3945
4046 var command = EssentialsPlugin . Instance . Config . AutoCommands . FirstOrDefault ( c => c . Name . Equals ( name ) ) ;
4147
4248 if ( command == null || ! command . Votable )
4349 {
44- Context . Respond ( $ "Couldn't find any votable command with the name ' { name } ' ") ;
50+ Context . Respond ( $ "Couldn't find any votable command with the name [ { name } ] ") ;
4551 return ;
4652 }
4753
4854
4955 // Rexxar's spam blocker. Timing is random as fuck and unique to each player.
5056 var steamid = Context . Player . SteamUserId ;
51- if ( _votetimeout . TryGetValue ( steamid , out DateTime lastcommand ) )
57+ if ( _voteCooldown . TryGetValue ( steamid , out DateTime activeCooldown ) )
5258 {
53- TimeSpan difference = DateTime . Now - lastcommand ;
54- if ( difference . TotalMinutes < ( _cooldown + 1 ) )
59+ TimeSpan difference = activeCooldown - DateTime . Now ;
60+ if ( difference . TotalSeconds > 0 )
5561 {
56- Context . Respond ( $ "Cooldown active. You can use this command again in { _cooldown - difference . Minutes : N0} minutes : { 60 - difference . Seconds : N0} seconds") ;
62+ Context . Respond ( $ "Cooldown active. You can use this command again in { difference . Minutes : N0} minutes : { difference . Seconds : N0} seconds") ;
5763 return ;
5864 }
5965 else
6066 {
61- _votetimeout [ steamid ] = DateTime . Now ;
67+ _voteCooldown [ steamid ] = DateTime . Now . AddMinutes ( _cooldown ) ;
6268 }
6369
6470 }
6571
66- else _votetimeout . Add ( steamid , DateTime . Now ) ;
72+ else _voteCooldown . Add ( steamid , DateTime . Now . AddMinutes ( _cooldown ) ) ;
6773
68- // voting registration filter
6974 TimeSpan _voteDuration = TimeSpan . Parse ( command . VoteDuration ) ;
70- if ( _voteReg . TryGetValue ( steamid , out DateTime lastvote ) )
71- {
72-
73- TimeSpan difference = DateTime . Now - lastvote ;
74- if ( difference . TotalSeconds < _voteDuration . TotalSeconds )
75- {
76- Context . Respond ( $ "Your vote has already been submitted. use '!no' to retract your vote") ;
77- return ;
78- }
79- else
80- {
81- _voteReg [ steamid ] = DateTime . Now ;
82- }
83- }
84- else
85- {
86- _voteReg . Add ( steamid , DateTime . Now ) ;
87- }
88-
8975 // voting status
9076 voteInProgress = name ;
91- VoteStatus = Status . VoteInProgress ;
92-
77+ VoteStatus = Status . voteInProgress ;
78+ VoteYes ( ) ;
9379 //vote countdown
9480 Task . Run ( ( ) =>
9581 {
@@ -100,18 +86,22 @@ public void Vote(string name)
10086 }
10187 } ) ;
10288
89+ lastVoteName = voteInProgress ;
90+
91+
10392 Context . Torch . CurrentSession ? . Managers ? . GetManager < IChatManagerServer > ( ) ? . SendMessageAsSelf ( $ "Voting started for { name } by { Context . Player . DisplayName } . " +
104- $ "Use ' !yes' to vote and ' !no' to retract your vote") ;
93+ $ "Use [ !yes] to vote and [ !no] to retract your vote") ;
10594
10695 }
10796
108- [ Command ( "vote cancel" , "Cancels current vote in progress" ) ]
97+ [ Command ( "vote cancel" , "Cancels current vote in progress" ) ]
10998 [ Permission ( MyPromoteLevel . Admin ) ]
11099 public void VoteCancel ( )
111100 {
112- if ( VoteStatus == Status . VoteInProgress )
101+ if ( VoteStatus == Status . voteInProgress )
113102 {
114- VoteStatus = Status . VoteCancel ;
103+ VoteStatus = Status . voteCancel ;
104+
115105 }
116106 else
117107 Context . Respond ( "A vote is not in progress" ) ;
@@ -124,7 +114,7 @@ public void VoteNo()
124114 if ( Context . Player == null )
125115 return ;
126116
127- if ( VoteStatus == Status . VoteEnded )
117+ if ( VoteStatus == Status . voteStandby )
128118 {
129119 Context . Respond ( $ "no vote in progress") ;
130120 return ;
@@ -145,7 +135,7 @@ public void VoteYes()
145135 if ( Context . Player == null )
146136 return ;
147137
148- if ( VoteStatus == Status . VoteEnded )
138+ if ( VoteStatus == Status . voteStandby )
149139 {
150140 Context . Respond ( $ "no vote in progress") ;
151141 return ;
@@ -176,20 +166,44 @@ public void VoteYes()
176166 }
177167
178168 //debug
179- [ Command ( "vote debug" ) ]
169+ [ Command ( "vote debug" , "prints out info from the voting module" ) ]
180170 [ Permission ( MyPromoteLevel . Admin ) ]
181- public void VoteCount ( )
171+ public void VoteDebgug ( )
182172 {
183173 StringBuilder sb = new StringBuilder ( ) ;
184- sb . AppendLine ( $ "Current vote: { voteInProgress } ") ;
185- sb . AppendLine ( $ "vote Status: { VoteStatus . ToString ( ) } ") ;
186- sb . AppendLine ( $ "vote count: { _voteReg . Count } / player count: { MySession . Static . Players . GetOnlinePlayerCount ( ) } ") ;
187- sb . AppendLine ( $ "vote percent: { votePercent } ") ;
174+ sb . AppendLine ( ) ;
175+ sb . AppendLine ( $ "Current Vote Status: { VoteStatus . ToString ( ) } ") ;
176+ sb . AppendLine ( $ "Current Vote Name: { voteInProgress } ") ;
177+ sb . AppendLine ( $ "Current vote count: { _voteReg . Count } ") ;
178+ sb . AppendLine ( ) ;
179+ sb . AppendLine ( "Last vote info" ) ;
180+ if ( lastVoteName != null )
181+ sb . AppendLine ( $ "Last vote: { lastVoteName . ToString ( ) } ") ;
182+ sb . AppendLine ( $ "Last Vote Result: { voteResult } ") ;
183+ sb . AppendLine ( $ "Last vote percent: { voteResultPercentage } ") ;
188184 Context . Respond ( sb . ToString ( ) ) ;
189185
190186 }
191187
192- //votec countdown
188+ //vote reset
189+ [ Command ( "vote reset" , "Resets the voting module data including cooldowns" ) ]
190+ [ Permission ( MyPromoteLevel . Admin ) ]
191+ public void VoteReset ( )
192+ {
193+ if ( VoteStatus == Status . voteInProgress )
194+ {
195+ VoteCancel ( ) ;
196+ }
197+ _voteReg . Clear ( ) ;
198+ _voteCooldown . Clear ( ) ;
199+ lastVoteName = null ;
200+ voteResult = Status . voteStandby ;
201+ voteResultPercentage = 0 ;
202+ Context . Respond ( "Vote reset successful" ) ;
203+ }
204+
205+
206+ //vote countdown
193207 private IEnumerable VoteCountdown ( TimeSpan time )
194208 {
195209 var command = EssentialsPlugin . Instance . Config . AutoCommands . FirstOrDefault ( c => c . Name . Equals ( voteInProgress ) ) ;
@@ -198,11 +212,13 @@ private IEnumerable VoteCountdown(TimeSpan time)
198212 for ( var i = time . TotalSeconds ; i >= 0 ; i -- )
199213 {
200214
201- if ( VoteStatus == Status . VoteCancel || _voteReg . Count < 1 )
215+ if ( VoteStatus == Status . voteCancel || _voteReg . Count < 1 )
202216 {
203217 Context . Torch . CurrentSession . Managers . GetManager < IChatManagerClient > ( )
204218 . SendMessageAsSelf ( $ "Vote for { voteInProgress } cancelled") ;
205- VoteClear ( ) ;
219+ voteResult = Status . voteCancel ;
220+ VoteEnd ( ) ;
221+
206222 yield break ;
207223 }
208224
@@ -222,56 +238,47 @@ private IEnumerable VoteCountdown(TimeSpan time)
222238 }
223239 else
224240 {
225- votePercent = ( int ) Math . Round ( ( double ) 100 * ( _voteReg . Count / ( MySession . Static . Players . GetOnlinePlayerCount ( ) ) ) ) ;
226-
227- if ( votePercent >= command . Percentage )
241+ var _votePercent = 100 * ( _voteReg . Count / MySession . Static . Players . GetOnlinePlayerCount ( ) ) ;
242+ if ( _votePercent >= command . Percentage )
228243 {
229244 Context . Torch . CurrentSession . Managers . GetManager < IChatManagerClient > ( )
230245 . SendMessageAsSelf ( $ "Vote for { voteInProgress } is successful") ;
246+ voteResult = Status . voteSuccess ;
231247 command . RunNow ( ) ;
232248 }
233249 else
234250 {
235251 Context . Torch . CurrentSession . Managers . GetManager < IChatManagerClient > ( )
236252 . SendMessageAsSelf ( $ "Vote for { voteInProgress } failed") ;
253+ voteResult = Status . voteFail ;
237254 }
238- VoteClear ( ) ;
255+ voteResultPercentage = _votePercent ;
256+ VoteEnd ( ) ;
257+
239258 yield break ;
240259 }
241260 }
242261 }
243-
244- [ Command ( "vote reset" , "Reset all vote logs" ) ]
245- [ Permission ( MyPromoteLevel . Admin ) ]
246- public void VoteReset ( )
262+
263+ public void VoteEnd ( )
247264 {
248- if ( VoteStatus == Status . VoteInProgress )
249- {
250- VoteCancel ( ) ;
251- }
252- else
253- VoteClear ( ) ;
254- VoteClear ( ) ;
255- votePercent = 0 ;
256- _votetimeout . Clear ( ) ;
257- }
258-
259- public void VoteClear ( )
260- {
261- Random rnd = new Random ( ) ;
262- _cooldown = rnd . Next ( 5 , 30 ) ;
263265
264266 //Make sure it's all good for next round
265- VoteStatus = Status . VoteEnded ;
267+ VoteStatus = Status . voteStandby ;
266268 voteInProgress = null ;
267269 _voteReg . Clear ( ) ;
268270 }
269-
271+
270272 public enum Status
271273 {
272- VoteInProgress ,
273- VoteEnded ,
274- VoteCancel
274+ voteStandby ,
275+ voteInProgress ,
276+ voteCancel ,
277+ voteEnd ,
278+
279+ // Last vote
280+ voteFail ,
281+ voteSuccess
275282 }
276283 private string Pluralize ( double num )
277284 {
0 commit comments