예제 #1
0
        private bool Initialize(SlackBotCommand command)
        {
            m_Players    = new string[2];
            m_Moves      = new Dictionary <string, string>();
            m_Players[0] = command.User.id;

            string opponentId;

            if (command.Text.StartsWith("<@") && command.Text.EndsWith(">"))
            {
                opponentId = command.Text.Substring(2, command.Text.Length - 3);
                if (Array.Find(command.Channel.members, userId => opponentId == userId) == null)
                {
                    return(false);
                }
            }
            else
            {
                opponentId = Array.Find(command.Channel.members, userId => slackBot.GetUser(userId) != null && slackBot.GetUser(userId).name == command.Text);
                if (opponentId == null)
                {
                    return(false);
                }
            }

            m_Players[1] = opponentId;

            m_FalseStartCount = new Dictionary <string, int>()
            {
                { m_Players[0], 0 },
                { m_Players[1], 0 }
            };

            return(true);
        }
예제 #2
0
        private string GetUsername(SlackBotCommand command, string name)
        {
            string opponentId;

            if (name.StartsWith("<@") && name.EndsWith(">"))
            {
                opponentId = name.Substring(2, name.Length - 3);
                if (Array.Find(command.Channel.members, userId => opponentId == userId) == null)
                {
                    return(null);
                }
            }
            else
            {
                opponentId = Array.Find(command.Channel.members,
                                        userId => slackBot.GetUser(userId) != null && slackBot.GetUser(userId).name == name);
                if (opponentId == null)
                {
                    return(null);
                }
            }
            return(slackBot.GetUser(opponentId).name);
        }
예제 #3
0
        public bool Execute(SlackBotCommand command)
        {
            if (command.Name == "makevote")
            {
                if (string.IsNullOrWhiteSpace(command.Text))
                {
                    slackBot.Reply(command, "What do you want to vote on?");
                    return(false);
                }
                else if (VotingTopic != null)
                {
                    slackBot.Reply(command, "There is already an ongoing vote.");
                    return(true);
                }

                VotingTopic = command.Text;
                return(true);
            }
            else
            {
                if (VotingTopic == null)
                {
                    slackBot.Reply(command, "There is no vote going on currently");
                    return(false);
                }
                if (!(command.Text == "yes" || command.Text == "no"))
                {
                    slackBot.Reply(command, "You must vote 'yes' or 'no'");
                    return(true);
                }
                else if (Voted.Contains(command.User.id))
                {
                    slackBot.Reply(command, "You already voted");
                    return(true);
                }

                if (command.Text == "yes")
                {
                    YesVotes++;
                }
                else
                {
                    NoVotes++;
                }
                Voted.Add(command.User.id);

                int difference = Math.Abs(YesVotes - NoVotes);
                int votes      = YesVotes + NoVotes;
                int votesLeft  = Array.FindAll(command.Channel.members, userId => slackBot.GetUser(userId).presence == "active" && !slackBot.GetUser(userId).is_bot).Length - votes;

                if (difference > votesLeft)
                {
                    string result = (YesVotes > NoVotes) ? "yes" : "no";
                    slackBot.Reply(command, "The people have spoken, the vote is " + result + " to " + VotingTopic);
                    return(false);
                }
                else if (votesLeft == 0)
                {
                    slackBot.Reply(command, "The vote was inconclusive.");
                    return(false);
                }

                return(true);
            }
        }