예제 #1
0
 private void Remove(CurrentVoteData vote)
 {
     currentVotes.Remove(vote.Command);
     if (vote.RemoveOnResourceEnd)
     {
         removeOnResourceEnded.Remove(vote);
     }
 }
예제 #2
0
 private void Add(CurrentVoteData vote)
 {
     currentVotes.Add(vote.Command, vote);
     if (vote.RemoveOnResourceEnd)
     {
         removeOnResourceEnded.Add(vote);
     }
 }
예제 #3
0
        private bool CheckAndFire(CurrentVoteData vote)
        {
            // Vote is not finished if the needed vote count is not reached
            if (Needed > vote.Voters.Count)
            {
                return(false);
            }

            client.SendChannelMessage($"Enough votes, executing \"{vote.Command}\"...");

            Remove(vote);
            var res = ExecuteTryCatch(config, true, vote.Executor,
                                      err => client.SendChannelMessage(err).UnwrapToLog(Log));

            if (!string.IsNullOrEmpty(res))
            {
                client.SendChannelMessage(res).UnwrapToLog(Log);
            }

            return(true);
        }
예제 #4
0
        public Result CommandVote(ExecutionInformation info, Uid invoker, string command, string args = null)
        {
            command = command.ToLower();
            if (string.IsNullOrWhiteSpace(command))
            {
                throw new CommandException("No command to vote for given.", CommandExceptionReason.CommandError);
            }

            if (!VotableCommands.Commands.TryGetValue(command, out var votableCommand))
            {
                throw new CommandException($"The given command \"{command}\" can't be put up to vote.",
                                           CommandExceptionReason.CommandError);
            }

            OnBotChannelChanged();

            bool voteAdded;
            bool votesChanged;
            bool voteCompleted;

            if (CurrentVotes.TryGetValue(command, out var currentVote))
            {
                if (!string.IsNullOrWhiteSpace(args))
                {
                    throw new CommandException(
                              "There is already a vote going on for this command. You can't start another vote for the same command with other parameters right now.",
                              CommandExceptionReason.CommandError);
                }

                if (currentVote.Voters.Remove(invoker))
                {
                    int count = currentVote.Voters.Count;
                    voteAdded = false;
                    if (count == 0)
                    {
                        Remove(currentVote);
                        votesChanged = true;

                        client.SendChannelMessage($"Removed vote of {ClientUtility.GetClientNameFromUid(ts3FullClient, invoker)} and stopped vote for \"{command}\".");
                    }
                    else
                    {
                        votesChanged = false;
                        client.SendChannelMessage($"Removed vote of {ClientUtility.GetClientNameFromUid(ts3FullClient, invoker)} for \"{command}\" ({currentVote.Voters.Count} votes of {Needed})");
                    }

                    voteCompleted = false;
                }
                else
                {
                    currentVote.Voters.Add(invoker);
                    voteAdded = true;
                    client.SendChannelMessage($"Added vote of {ClientUtility.GetClientNameFromUid(ts3FullClient, invoker)} for \"{command}\" ({currentVote.Voters.Count} votes of {Needed})");
                    votesChanged  = false;
                    voteCompleted = CheckAndFire(currentVote);
                }
            }
            else
            {
                info.AddModule(CreateCallerInfo());
                var(executor, removeOnResourceEnd) = votableCommand.Create(info, command, args);

                currentVote = new CurrentVoteData(command, executor, removeOnResourceEnd);
                Add(currentVote);
                voteAdded = true;
                currentVote.Voters.Add(invoker);
                votesChanged = true;
                client.SendChannelMessage($"{ClientUtility.GetClientNameFromUid(ts3FullClient, invoker)} started vote for \"{command}\" ({currentVote.Voters.Count} votes of {Needed})");
                voteCompleted = CheckAndFire(currentVote);
            }

            var result = new Result {
                VoteAdded    = voteAdded,
                VoteComplete = voteCompleted,
                VotesChanged = votesChanged,
                VoteCount    = currentVote.Voters.Count,
                VotesNeeded  = Needed
            };

            if (currentVote.Command == "skip" && (voteAdded || votesChanged || voteCompleted))
            {
                OnSkipVoteChanged?.Invoke(this, new SkipVoteEventArgs(result));
            }

            return(result);
        }