예제 #1
0
        public async Task <VoteGetModel> CastVoteAsync(VoteCreateModel voteModel)
        {
            var matchedVote = await _voteRepository
                              .GetSingleAsync(v => v.UserId == voteModel.UserId &&
                                              (v.QuestionId == null || v.QuestionId == voteModel.TargetId) &&
                                              (v.AnswerId == null || v.AnswerId == voteModel.TargetId) &&
                                              (v.CommentId == null || v.CommentId == voteModel.TargetId));

            if (matchedVote != null)
            {
                throw new BusinessException($"User already voted on {voteModel.VoteTarget} on '{matchedVote.CreatedOn}'.");
            }
            IVoteable target = await GetVoteableFromRepositoryAsync(voteModel.VoteTarget, voteModel.TargetId);

            if (target == null)
            {
                throw new EntityNotFoundException(voteModel.VoteTarget.ToString(), voteModel.TargetId);
            }
            var vote = Vote.CreateVote(voteModel.UserId, target, voteModel.VoteType);

            target.ApplyVote(vote);
            await _uow.SaveAsync();

            await ChangeCachedVotesSumAfterVoteCast(vote);

            _eventRegister.RegisterEvent <VoteCast>(new { UserId = vote.UserId, VoteType = vote.VoteType });
            return(_mapper.Map <VoteGetModel>(vote));
        }
예제 #2
0
 public static Vote CreateVote(Guid userId, IVoteable voteable, VoteTypeEnum voteType)
 {
     return(voteable switch
     {
         Question question => new Vote(userId, question, voteType),
         Answer answer => new Vote(userId, answer, voteType),
         Comment comment => new Vote(userId, comment, voteType),
         _ => throw new ArgumentException()
     });
예제 #3
0
 public CommentService(IQuestionRepository questionRepository,
                       ICommentRepository commentRepository,
                       IAnswerRepository answerRepository,
                       IUserRepository userRepository,
                       ICache cache,
                       IRepository <Vote> voteRepository,
                       IUnitOfWork unitOfWork,
                       BaseLimits limits,
                       IMapper mapper)
 {
     _questionRepository = questionRepository;
     _answerRepository   = answerRepository;
     _commentRepository  = commentRepository;
     _userRepository     = userRepository;
     _cache          = cache;
     _voteRepository = voteRepository;
     _uow            = unitOfWork;
     _limits         = limits;
     _voteable       = new Voteable();
     _mapper         = mapper;
 }
예제 #4
0
        public static async Task <IVoteable> DoVote(List <IVoteable> options, int seconds)
        {
            Dictionary <Emoji, IVoteable> tracker = new Dictionary <Emoji, IVoteable>();

            EmbedBuilder embed = new EmbedBuilder()
            {
                Color = new Color(0, 100, 255),
                Title = "Vote:"
            };

            //string mes = "|";

            foreach (IVoteable option in options)
            {
                if (!tracker.ContainsKey(option.GetEmote()))
                {
                    tracker.Add(option.GetEmote(), option);
                    //mes += $" {option.GetName()} = {option.GetEmote()} |";

                    embed.AddField((efb) =>
                    {
                        efb.Name  = option.GetName();
                        efb.Value = option.GetEmote();
                    });
                }
            }

            RestUserMessage msg = await VoopAI.gameChannel.SendMessageAsync(embed : embed.Build());

            List <Emoji> distinct = new List <Emoji>();

            Emoji c = null;

            try
            {
                foreach (Emoji e in tracker.Keys)
                {
                    c = e;
                    await msg.AddReactionAsync(e);
                }
            }
            catch (System.Exception e)
            {
                await(gameChannel.SendMessageAsync(e.ToString()));
                await(gameChannel.SendMessageAsync(c.ToString()));
            }


            Thread.Sleep(seconds * 1000);

            IEmote chosen = tracker.Keys.PickRandom();
            int    top    = 0;

            IUserMessage liveMessage = (IUserMessage)await VoopAI.gameChannel.GetMessageAsync(msg.Id);

            foreach (KeyValuePair <IEmote, ReactionMetadata> pair in liveMessage.Reactions)
            {
                //await VoopAI.gameChannel.SendMessageAsync($"DEBUG: {pair.Key.Name} = {pair.Value.ReactionCount}");

                if (pair.Value.ReactionCount > top)
                {
                    top    = pair.Value.ReactionCount;
                    chosen = pair.Key;
                }
            }

            IVoteable obj = tracker[new Emoji(chosen.Name)];

            await VoopAI.gameChannel.SendMessageAsync($"You have chosen [ {obj.GetName()} ]");

            Thread.Sleep(5000);

            return(obj);
        }
예제 #5
0
 public void DownVote(IVoteable <object> votableThing)
 {
     throw new NotImplementedException();
 }
예제 #6
0
 public void Visit(IVoteable Subject)
 {
     Subject.vote();
 }