Example #1
0
        public async Task <ChatCommandOutput> execute(ChatCommandInputArgs args, IServiceScopeFactory scopefactory)
        {
            ChatCommandOutput output = new ChatCommandOutput();

            output.ExecuteEvent = true;
            output.Type         = Eventbus.EventType.CommandResponseReceived;
            string message = "";

            using (var scope = scopefactory.CreateScope())
            {
                var _context     = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>();
                var GiveAwayItem = _context.GiveAwayItems.Include(x => x.Applicants).Where(x => x.current).FirstOrDefault();
                if (GiveAwayItem.Applicants == null)
                {
                    GiveAwayItem.Applicants = new List <User_GiveAwayItem>();
                }
                var user = _context.ChatUserModels.Where(x => x.ChatUserName.ToLower() == args.Sender.ToLower()).FirstOrDefault();
                if (GiveAwayItem.Applicants.Where(x => x.UserID == user.Id).Count() == 0)
                {
                    var item = _context.GiveAwayItems.Where(x => x.current).FirstOrDefault();
                    if (user != null && item != null)
                    {
                        User_GiveAwayItem relation = new User_GiveAwayItem(user, item);
                        relation.User = user;
                        if (user.AppliedTo == null)
                        {
                            user.AppliedTo = new List <User_GiveAwayItem>();
                        }
                        if (item.Applicants == null)
                        {
                            item.Applicants = new List <User_GiveAwayItem>();
                        }
                        user.AppliedTo.Add(relation);
                        item.Applicants.Add(relation);
                        message = "Teilnahme erfolgreich";
                    }
                    else
                    {
                        message = "Gibt nichs zum teilnehmen";
                    }
                }
                else
                {
                    message = "Nimmst schon teil.";
                }
                _context.SaveChanges();
            }
            output.EventData = new CommandResponseArgs(
                ChatType.Discord,
                message,
                MessageType.PrivateMessage,
                args.Sender,
                args.ChannelName
                );
            return(output);
        }
Example #2
0
        private List <string> doRaffle(string channel)
        {
            List <string> winners      = new List <string>();
            bool          repeatRaffle = false;

            using (var scope = _scopeFactory.CreateScope())
            {
                var tmpcontext  = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>();
                var currentitem = tmpcontext.GiveAwayItems.Include(x => x.Applicants).ThenInclude(y => y.User).ThenInclude(x => x.ReceivedItems).Where(x => x.current).FirstOrDefault();
                List <ChatUserModel> Applicants = null;
                ChatUserModel        tmpwinner  = null;
                if (currentitem != null && currentitem.Applicants.Count() > 0)
                {
                    int min            = getLeastGiveAwayCount(currentitem.Applicants);
                    var elligableUsers = currentitem.Applicants.Where(x => x.User.ReceivedItems.Count() == min);

                    int winnerindex = random.Next(elligableUsers.Count());
                    var winner      = elligableUsers.ToArray()[winnerindex];
                    winners.Add(winner.User.ChatUserName);
                    currentitem.Receiver   = winner.User;
                    tmpwinner              = winner.User;
                    currentitem.ReceiverID = winner.UserID;
                    tmpcontext.SaveChanges();
                    _eventBus.TriggerEvent(EventType.CommandResponseReceived, new CommandResponseArgs {
                        Channel = channel, MessageType = Eventbus.MessageType.ChannelMessage, Message = $"Gewonnen hat {winner.User.ChatUserName}"
                    });
                }
                if (currentitem.Applicants.Count() > 1 && tmpcontext.GiveAwayItems.Include(x => x.Applicants).ThenInclude(y => y.User).Where(x => x.Title == currentitem.Title && x.Id != currentitem.Id && x.ReceiverID == null).Count() > 0)
                {
                    var newitem = tmpcontext.GiveAwayItems.Include(x => x.Applicants).ThenInclude(y => y.User).Where(x => x.Title == currentitem.Title && x.ReceiverID == null && x.current == false).FirstOrDefault();
                    foreach (var user in currentitem.Applicants.Where(x => x.UserID != tmpwinner.Id).ToList())
                    {
                        var m_n_relation = new User_GiveAwayItem(user.User, newitem);
                        newitem.Applicants.Add(m_n_relation);
                        user.User.AppliedTo.Add(m_n_relation);
                    }
                    currentitem.current = false;
                    newitem.current     = true;
                    tmpcontext.SaveChanges();
                    repeatRaffle = true;
                }
            }
            if (repeatRaffle)
            {
                winners.AddRange(doRaffle(channel));
            }
            return(winners);
        }