Ejemplo n.º 1
0
        private Message BroadcastMessage(string subjectName, string broadcastText, Message message)
        {
            List <ChannelAccount> recipients = new List <ChannelAccount>();

            foreach (string user in accountsForId.Keys)
            {
                if (accountsForId[user].Id == message.From.Id)
                {
                    continue;
                }

                recipients.Add(accountsForId[user]);
            }

            Broadcast.Add(subjectName, message.From, recipients, -1);

            Message ack = message.CreateReplyMessage($"And what was your question?");

            return(ack);
        }
Ejemplo n.º 2
0
        private Message FindExpert(string subjectName, Message message)
        {
            subjectName = subjectName.ToLower();
            Subject subject  = Subject.GetAll().FirstOrDefault(p => p.Name == subjectName);
            string  question = $"{Person.GetAll().FirstOrDefault(p => p.Id == message.From.Id).Username} needs help with {subjectName}";

            if (subject == null)
            {
                return(BroadcastMessage(subjectName, question, message));
            }

            List <Person> persons = Person.GetAll().Where(p => p.Id != message.From.Id).ToList();

            List <KeyValuePair <Person, int> > potentialExperts = new List <KeyValuePair <Person, int> >();

            foreach (var person in persons)
            {
                int points = Matrix.GetPoints(person, subject);
                if (points >= 5)
                {
                    potentialExperts.Add(new KeyValuePair <Person, int>(person, points));
                }
            }

            potentialExperts.Sort((p1, p2) => p2.Value - p1.Value);

            List <ChannelAccount> potentialAnswerers = new List <ChannelAccount>();
            List <string>         experts            = new List <string>();
            List <string>         randoms            = new List <string>();
            List <string>         chosenIds          = new List <string>();

            for (int i = 0; i < potentialExperts.Count && i < 3; ++i)
            {
                string chosenId = potentialExperts[i].Key.Id;
                if (accountsForId.ContainsKey(chosenId))
                {
                    chosenIds.Add(chosenId);
                    potentialAnswerers.Add(accountsForId[chosenId]);
                    experts.Add("@" + accountsForId[chosenId].Name);
                }
            }

            List <string> allIds = accountsForId.Keys.ToList();

            allIds.Remove(message.From.Id);
            foreach (string id in chosenIds)
            {
                allIds.Remove(id);
            }

            int    n      = allIds.Count;
            Random random = new Random();

            while (n > 1)
            {
                n--;
                int    k     = random.Next(n + 1);
                string value = allIds[k];
                allIds[k] = allIds[n];
                allIds[n] = value;
            }

            for (int i = 0; i < 5 - potentialAnswerers.Count && i < allIds.Count; ++i)
            {
                potentialAnswerers.Add(accountsForId[allIds[i]]);
                randoms.Add("@" + accountsForId[allIds[i]].Name);
            }

            if (potentialAnswerers.Count > 0)
            {
                Broadcast.Add(subjectName, message.From, potentialAnswerers, experts.Count);

                return(message.CreateReplyMessage($"And what was your question?", "en"));
            }
            else
            {
                return(message.CreateReplyMessage(Reply.GetReply(ReplyType.NoExpertsFound).Text));
            }
        }