Example #1
0
        public async Task <bool> TryGuess(SocketMessage msg)
        {
            Guess G;
            await _locker.WaitAsync().ConfigureAwait(false);

            try
            {
                if (msg == null || msg.Author.IsBot || msg.Channel.Id != Game.Channel)
                {
                    return(false);
                }

                if (!int.TryParse(msg.Content, out int vote))
                {
                    return(false);
                }

                if (vote < Game.MinGuess || vote > Game.MaxGuess)
                {
                    return(false);
                }

                var usr = msg.Author as IGuildUser;
                if (usr == null)
                {
                    return(false);
                }

                G = new Guess()
                {
                    UserID     = msg.Author.Id,
                    GuessIndex = vote
                };

                if (Game.Guesses.Any(x => x.UserID == msg.Author.Id))
                {
                    return(false);
                }

                if (Game.Guesses.Any(x => x.GuessIndex == vote))
                {
                    return(false);
                }

                if (!Game.Guesses.Add(G))
                {
                    return(false);
                }

                var _ = OnVoted?.Invoke(msg as IUserMessage, usr);
            }
            finally { _locker.Release(); }
            return(true);
        }
Example #2
0
        public async Task <bool> TryVote(IUserMessage msg)
        {
            PollVote voteObj;
            await _locker.WaitAsync().ConfigureAwait(false);

            try
            {
                // has to be a user message
                // channel must be the same the poll started in
                if (msg == null || msg.Author.IsBot || msg.Channel.Id != Poll.ChannelId)
                {
                    return(false);
                }

                // has to be an integer
                if (!int.TryParse(msg.Content, out int vote))
                {
                    return(false);
                }
                --vote;
                if (vote < 0 || vote >= Poll.Answers.Count)
                {
                    return(false);
                }

                var usr = msg.Author as IGuildUser;
                if (usr == null)
                {
                    return(false);
                }

                voteObj = new PollVote()
                {
                    UserId    = msg.Author.Id,
                    VoteIndex = vote,
                };
                if (!Poll.Votes.Add(voteObj))
                {
                    return(false);
                }

                var _ = OnVoted?.Invoke(msg, usr);
            }
            finally { _locker.Release(); }
            using (var uow = _db.UnitOfWork)
            {
                var trackedPoll = uow.Polls.GetById(Poll.Id);
                trackedPoll.Votes.Add(voteObj);
                uow.Complete();
            }
            return(true);
        }
        public async Task <bool> TryGuess(SocketMessage msg)
        {
            QGuess G;
            await _locker.WaitAsync().ConfigureAwait(false);

            try
            {
                if (msg == null || msg.Author.IsBot || msg.Channel.Id != Game.Channel)
                {
                    return(false);
                }

                string vote = msg.Content.ToUpper();

                var usr = msg.Author as IGuildUser;
                if (usr == null)
                {
                    return(false);
                }

                G = new QGuess()
                {
                    UserID     = msg.Author.Id,
                    QuoteGuess = vote,
                    Timestamp  = DateTime.UtcNow.Ticks
                };

                if (Game.Guesses.Any(x => x.UserID == msg.Author.Id))
                {
                    return(false);
                }

                if (!Game.Guesses.Add(G))
                {
                    return(false);
                }

                var _ = OnVoted?.Invoke(msg as IUserMessage, usr);
            }
            finally { _locker.Release(); }
            return(true);
        }