public override void Process(CmdTrigger<IrcCmdArgs> trigger) { var chan = trigger.Args.Channel; var voteChan = trigger.Text.NextWord(); Vote vote; // If we have a channel given in the argument list if (voteChan.Length > 0) vote = Vote.GetVote(trigger.Args.IrcClient.GetChannel(voteChan)); vote = Vote.GetVote(chan); if (vote != null) { trigger.Reply("The vote has ended!"); trigger.Reply(VoteMgr.Mgr.Stats(vote)); trigger.Reply(VoteMgr.Mgr.Result(vote)); VoteMgr.Mgr.EndVote(vote); return; } else { Vote[] votesArray = new Vote[VoteMgr.Votes.Values.Count]; VoteMgr.Votes.Values.CopyTo(votesArray, 0); foreach (var vte in votesArray) VoteMgr.Mgr.EndVote(vte); trigger.Reply("Disposed of {0} vote[s].", votesArray.Length); } }
/// <summary> /// Gets the result of the vote. Usually called after a vote has ended /// </summary> /// <param name="vote"></param> /// <returns>Returns a string of the result depending on the outcome</returns> public string Result(Vote vote) { if (vote.PositiveCount > vote.NegativeCount) return "Vote resulted in favour of \"" + vote.VoteQuestion + "\" !"; if (vote.PositiveCount < vote.NegativeCount) return "The crowd is NOT in favour of \"" + vote.VoteQuestion + "\" !"; //vote.PositiveCount == vote.NegativeCount return "It's a draw!"; }
/// <summary> /// Cleans up after the vote so that it could be disposed. /// </summary> /// <param name="vote"></param> private void Destroy(Vote vote) { if(vote.m_Timer != null) vote.m_Timer.Stop(); VoteMgr.Votes.Remove(vote.Channel); vote.disposed = true; }
/// <summary> /// Start a new vote in a channel /// </summary> /// <param name="channel">The channel where the voting takes place</param> /// <param name="question">The string we are voting over</param> public void StartNewVote(IrcChannel channel, string question) { var vote = new Vote(question, channel); Votes.Add(channel, vote); channel.TextReceived += (user, text) => { var voteCmdAlias = VoteStartCommand.VoteAnswerPrefix; if (text.String.StartsWith(voteCmdAlias.ToString())) { var answer = text.NextWord(); if (answer.TrimStart(voteCmdAlias).Equals("yes", StringComparison.OrdinalIgnoreCase)) { AddVote(user, vote, true); return; } if (answer.TrimStart(voteCmdAlias).Equals("no", StringComparison.OrdinalIgnoreCase)) { AddVote(user, vote, false); } } }; }
/// <summary> /// Starts the cleanup process /// </summary> /// <param name="vote"></param> /// <returns></returns> public void EndVote(Vote vote) { vote.Dispose(); }
/// <summary> /// Adds a "yes" or a "no" to the given vote /// </summary> /// <param name="user">The user voting</param> /// <param name="vote">The vote we are handling</param> /// <param name="answer">True = "yes", False = "no"</param> private void AddVote(IrcUser user, Vote vote, bool answer) { if (VoteReceived != null) VoteReceived(vote, user, answer); if (vote.CanVote(user)) { // Answer is yes if (answer) vote.PositiveCount += 1; // Answer is no else { vote.NegativeCount += 1; } // We add a user to it's corresponding vote so that no one can vote more than once vote.votedUsers.Add(user); } }
/// <summary> /// Gets the stats of the vote. Usually called any time you want to display vote information /// </summary> /// <param name="vote"></param> /// <returns>Returns total/positive/negative votes</returns> public string Stats(Vote vote) { return "There are a total of '" + vote.TotalVotes + "' votes, '" + vote.PositiveCount + "' positive, '" + vote.NegativeCount + "' negative. "; }