11using System ;
2- using System . Text ;
3- using System . Threading ;
42using System . Collections ;
53using System . Collections . Generic ;
64using System . Linq ;
5+ using System . Text ;
6+ using System . Threading ;
77using System . Threading . Tasks ;
8+ using NLog ;
89using Sandbox . Game . World ;
9- using Sandbox . Engine . Multiplayer ;
1010using Torch . API . Managers ;
1111using Torch . Commands ;
1212using Torch . Commands . Permissions ;
13+ using Torch . Mod ;
14+ using Torch . Mod . Messages ;
1315using VRage . Game . ModAPI ;
1416
15-
1617namespace Essentials . Commands
1718{
1819 public class VotingModule : CommandModule
1920 {
20- private static Random rnd = new Random ( ) ;
21- private static int _cooldown = rnd . Next ( 5 , 30 ) ;
21+ public enum Status
22+ {
23+ voteStandby ,
24+ voteInProgress ,
25+ voteCancel ,
26+
27+ // Last vote
28+ voteFail ,
29+ voteSuccess
30+ }
31+
32+ private static readonly Logger Log = LogManager . GetLogger ( "Essentials Voting" ) ;
33+ private static readonly Random rnd = new Random ( ) ;
34+ private static AutoCommand _command ;
35+ private static readonly int _cooldown = rnd . Next ( 5 , 30 ) ;
2236 private static string voteInProgress ;
23- private static Dictionary < ulong , DateTime > _voteReg = new Dictionary < ulong , DateTime > ( ) ;
24- private static Dictionary < ulong , DateTime > _voteCooldown = new Dictionary < ulong , DateTime > ( ) ;
37+ private static readonly Dictionary < ulong , DateTime > _voteReg = new Dictionary < ulong , DateTime > ( ) ;
38+ private static readonly Dictionary < ulong , DateTime > _voteCooldown = new Dictionary < ulong , DateTime > ( ) ;
2539 public static Status VoteStatus = Status . voteStandby ;
2640
2741 //last vote info for debugging
@@ -38,70 +52,66 @@ public void Vote(string name)
3852
3953 if ( VoteStatus == Status . voteInProgress )
4054 {
41- Context . Respond ( $ "vote for { voteInProgress } is currently active. Use [!yes] to vote and [!no] to retract vote") ;
55+ Context . Respond (
56+ $ "vote for { voteInProgress } is currently active. Use !yes to vote and !no to retract vote") ;
4257 return ;
4358 }
4459
45- var command = EssentialsPlugin . Instance . Config . AutoCommands . FirstOrDefault ( c => c . Name . Equals ( name ) ) ;
60+ _command = EssentialsPlugin . Instance . Config . AutoCommands . FirstOrDefault ( c => c . Name . Equals ( name ) ) ;
4661
47- if ( command == null || command . CommandTrigger != Trigger . Vote )
62+ if ( _command == null || _command . CommandTrigger != Trigger . Vote )
4863 {
49- Context . Respond ( $ "Couldn't find any votable command with the name [{ name } ]") ;
64+ Context . Respond ( $ "Couldn't find any votable command with the name { name } ") ;
65+ _command = null ;
5066 return ;
5167 }
5268
5369
5470 // Rexxar's spam blocker. Timing is random as fuck and unique to each player.
5571 var steamid = Context . Player . SteamUserId ;
56- if ( _voteCooldown . TryGetValue ( steamid , out DateTime activeCooldown ) )
72+ if ( _voteCooldown . TryGetValue ( steamid , out var activeCooldown ) )
5773 {
58- TimeSpan difference = activeCooldown - DateTime . Now ;
74+ var difference = activeCooldown - DateTime . Now ;
5975 if ( difference . TotalSeconds > 0 )
6076 {
61- Context . Respond ( $ "Cooldown active. You can use this command again in { difference . Minutes : N0} minutes : { difference . Seconds : N0} seconds") ;
77+ Context . Respond (
78+ $ "Cooldown active. You can use this command again in { difference . Minutes : N0} minutes : { difference . Seconds : N0} seconds") ;
6279 return ;
6380 }
64- else
65- {
66- _voteCooldown [ steamid ] = DateTime . Now . AddMinutes ( _cooldown ) ;
67- }
6881
82+ _voteCooldown [ steamid ] = DateTime . Now . AddMinutes ( _cooldown ) ;
6983 }
7084
71- else _voteCooldown . Add ( steamid , DateTime . Now . AddMinutes ( _cooldown ) ) ;
85+ else
86+ {
87+ _voteCooldown . Add ( steamid , DateTime . Now . AddMinutes ( _cooldown ) ) ;
88+ }
7289
73- TimeSpan _voteDuration = TimeSpan . Parse ( command . Interval ) ;
90+ var _voteDuration = TimeSpan . Parse ( _command . Interval ) ;
7491 // voting status
7592 voteInProgress = name ;
7693 VoteStatus = Status . voteInProgress ;
7794 VoteYes ( ) ;
95+ var sb = new StringBuilder ( ) ;
96+ sb . AppendLine ( $ "Voting started for { name } by { Context . Player . DisplayName } .") ;
97+ sb . AppendLine ( "Use !yes to vote and !no to retract your vote" ) ;
98+ ModCommunication . SendMessageToClients ( new NotificationMessage ( sb . ToString ( ) , 15000 , "Blue" ) ) ;
7899 //vote countdown
79100 Task . Run ( ( ) =>
80101 {
81102 var countdown = VoteCountdown ( _voteDuration ) . GetEnumerator ( ) ;
82- while ( countdown . MoveNext ( ) )
83- {
84- Thread . Sleep ( 1000 ) ;
85- }
103+ while ( countdown . MoveNext ( ) ) Thread . Sleep ( 1000 ) ;
86104 } ) ;
87105
88106 lastVoteName = voteInProgress ;
89-
90-
91- Context . Torch . CurrentSession ? . Managers ? . GetManager < IChatManagerServer > ( ) ? . SendMessageAsSelf ( $ "Voting started for { name } by { Context . Player . DisplayName } . " +
92- $ "Use [!yes] to vote and [!no] to retract your vote") ;
93-
94107 }
95108
96109 [ Command ( "vote cancel" , "Cancels current vote in progress" ) ]
97110 [ Permission ( MyPromoteLevel . Admin ) ]
98111 public void VoteCancel ( )
99112 {
100113 if ( VoteStatus == Status . voteInProgress )
101- {
102114 VoteStatus = Status . voteCancel ;
103-
104- }
105115 else
106116 Context . Respond ( "A vote is not in progress" ) ;
107117 }
@@ -115,101 +125,94 @@ public void VoteNo()
115125
116126 if ( VoteStatus == Status . voteStandby )
117127 {
118- Context . Respond ( $ "no vote in progress") ;
128+ Context . Respond ( "no vote in progress" ) ;
119129 return ;
120130 }
121131
122132 var steamid = Context . Player . SteamUserId ;
123133
124134 _voteReg . Remove ( steamid ) ;
125135 Context . Respond ( "your vote has been retracted" ) ;
126-
127136 }
128137
129138 [ Command ( "yes" , "Submit a yes vote" ) ]
130139 [ Permission ( MyPromoteLevel . None ) ]
131140 public void VoteYes ( )
132141 {
133-
134142 if ( Context . Player == null )
135143 return ;
136144
137145 if ( VoteStatus == Status . voteStandby )
138146 {
139- Context . Respond ( $ "no vote in progress") ;
147+ Context . Respond ( "no vote in progress" ) ;
140148 return ;
141149 }
142150
143- var command = EssentialsPlugin . Instance . Config . AutoCommands . FirstOrDefault ( c => c . Name . Equals ( voteInProgress ) ) ;
144151 var steamid = Context . Player . SteamUserId ;
145- if ( _voteReg . TryGetValue ( steamid , out DateTime lastcommand ) )
152+ if ( _voteReg . TryGetValue ( steamid , out var lastcommand ) )
146153 {
147- TimeSpan difference = DateTime . Now - lastcommand ;
148- TimeSpan _voteDuration = TimeSpan . Parse ( command . Interval ) ;
154+ var difference = DateTime . Now - lastcommand ;
155+ var _voteDuration = TimeSpan . Parse ( _command . Interval ) ;
149156 if ( difference . TotalSeconds < _voteDuration . TotalSeconds )
150157 {
151- Context . Respond ( $ "Your vote has already been submitted.") ;
158+ Context . Respond ( "Your vote has already been submitted." ) ;
152159 return ;
153160 }
154- else
155- {
156- _voteReg [ steamid ] = DateTime . Now ;
157- }
161+
162+ _voteReg [ steamid ] = DateTime . Now ;
158163 }
159164 else
160165 {
161166 _voteReg . Add ( steamid , DateTime . Now ) ;
162167 }
163- Context . Respond ( $ "Your vote has been submitted.") ;
164168
169+ Context . Respond ( "Your vote has been submitted." ) ;
165170 }
166171
167172 //debug
168173 [ Command ( "vote debug" , "prints out info from the voting module" ) ]
169174 [ Permission ( MyPromoteLevel . Admin ) ]
170175 public void VoteDebgug ( )
171176 {
172- StringBuilder sb = new StringBuilder ( ) ;
177+ var sb = new StringBuilder ( ) ;
173178 sb . AppendLine ( ) ;
174179 sb . AppendLine ( $ "Current Vote Status: { VoteStatus . ToString ( ) } ") ;
175180 sb . AppendLine ( $ "Current Vote Name: { voteInProgress } ") ;
176181 sb . AppendLine ( $ "Current vote count: { _voteReg . Count } ") ;
177182 sb . AppendLine ( ) ;
178183 sb . AppendLine ( "Last vote info" ) ;
179184 if ( lastVoteName != null )
180- sb . AppendLine ( $ "Last vote: { lastVoteName . ToString ( ) } ") ;
185+ sb . AppendLine ( $ "Last vote: { lastVoteName } ") ;
181186 sb . AppendLine ( $ "Last Vote Result: { voteResult . ToString ( ) } ") ;
182187 sb . AppendLine ( $ "Last vote percent: { voteResultPercentage } ") ;
183- Context . Respond ( sb . ToString ( ) ) ;
184-
188+ if ( Context . Player == null )
189+ Context . Respond ( sb . ToString ( ) ) ;
190+ else if ( Context ? . Player ? . SteamUserId > 0 )
191+ ModCommunication . SendMessageTo ( new DialogMessage ( "List of Online Players" , null , sb . ToString ( ) ) ,
192+ Context . Player . SteamUserId ) ;
185193 }
186194
187195 //vote reset
188196 [ Command ( "vote reset" , "Resets the voting module data including cooldowns" ) ]
189197 [ Permission ( MyPromoteLevel . Admin ) ]
190198 public void VoteReset ( )
191199 {
192- if ( VoteStatus == Status . voteInProgress )
193- {
194- VoteCancel ( ) ;
195- }
200+ if ( VoteStatus == Status . voteInProgress ) VoteCancel ( ) ;
196201 _voteReg . Clear ( ) ;
197202 _voteCooldown . Clear ( ) ;
198203 lastVoteName = null ;
199204 voteResult = Status . voteStandby ;
200205 voteResultPercentage = 0 ;
201206 Context . Respond ( "Vote reset successful" ) ;
207+ Log . Info ( $ "Voting module reset by { Context . Player . DisplayName } ") ;
202208 }
203209
204210
205211 //vote countdown
206212 private IEnumerable VoteCountdown ( TimeSpan time )
207213 {
208-
209-
210214 for ( var i = time . TotalSeconds ; i >= 0 ; i -- )
211215 {
212-
213216 if ( VoteStatus != Status . voteInProgress || _voteReg . Count < 1 )
214217 {
215218 Context . Torch . CurrentSession . Managers . GetManager < IChatManagerClient > ( )
@@ -235,25 +238,22 @@ private IEnumerable VoteCountdown(TimeSpan time)
235238 }
236239 else
237240 {
238- var command = EssentialsPlugin . Instance . Config . AutoCommands . FirstOrDefault ( c => c . Name . Equals ( voteInProgress ) ) ;
239- double vr = ( ( _voteReg . Count / MySession . Static . Players . GetOnlinePlayerCount ( ) ) * 100 ) ;
240- Task . Delay ( 5000 ) . ContinueWith ( _ =>
241+ double vr = _voteReg . Count / MySession . Static . Players . GetOnlinePlayerCount ( ) ;
242+ if ( vr >= _command . TriggerRatio )
243+ {
244+ Context . Torch . CurrentSession . Managers . GetManager < IChatManagerClient > ( )
245+ . SendMessageAsSelf ( $ "Vote for { voteInProgress } is successful") ;
246+ voteResult = Status . voteSuccess ;
247+ _command . RunNow ( ) ;
248+ }
249+ else if ( vr < _command . TriggerRatio )
241250 {
242- if ( vr >= command . TriggerRatio )
243- {
244- Context . Torch . CurrentSession . Managers . GetManager < IChatManagerClient > ( )
245- . SendMessageAsSelf ( $ "Vote for { voteInProgress } is successful") ;
246- voteResult = Status . voteSuccess ;
247- command . RunNow ( ) ;
248- }
249- else if ( vr < command . TriggerRatio )
250- {
251- Context . Torch . CurrentSession . Managers . GetManager < IChatManagerClient > ( )
252- . SendMessageAsSelf ( $ "Vote for { voteInProgress } failed") ;
253- voteResult = Status . voteFail ;
254- }
255- } ) ;
256- voteResultPercentage = vr ;
251+ Context . Torch . CurrentSession . Managers . GetManager < IChatManagerClient > ( )
252+ . SendMessageAsSelf ( $ "Vote for { voteInProgress } failed") ;
253+ voteResult = Status . voteFail ;
254+ }
255+
256+ voteResultPercentage = vr * 100 ;
257257 VoteEnd ( ) ;
258258 yield break ;
259259 }
@@ -264,30 +264,15 @@ private IEnumerable VoteCountdown(TimeSpan time)
264264 public void VoteEnd ( )
265265 {
266266 //Make sure it's all good for next round
267+ _command = null ;
267268 VoteStatus = Status . voteStandby ;
268269 voteInProgress = null ;
269270 _voteReg . Clear ( ) ;
270271 }
271272
272- public enum Status
273- {
274- voteStandby ,
275- voteInProgress ,
276- voteCancel ,
277- voteEnd ,
278-
279- // Last vote
280- voteFail ,
281- voteSuccess
282- }
283273 private string Pluralize ( double num )
284274 {
285275 return num == 1 ? "" : "s" ;
286276 }
287-
288-
289277 }
290-
291- }
292-
293-
278+ }
0 commit comments