Пример #1
0
        public async Task Fishy()
        {
            if (!CheckFunEnabled(Context).Result)
            {
                return;
            }

            var fishEmotes = new List <FishEmote>()
            {
                new FishEmote {
                    Name = ":octopus:", Weight = 5
                },
                new FishEmote {
                    Name = ":crab:", Weight = 20
                },
                new FishEmote {
                    Name = ":fish:", Weight = 70
                },
                new FishEmote {
                    Name = ":tropical_fish:", Weight = 50
                },
                new FishEmote {
                    Name = ":blowfish:", Weight = 30
                },
                new FishEmote {
                    Name = ":shark:", Weight = 10
                },
                new FishEmote {
                    Name = ":shrimp:", Weight = 20
                },
                new FishEmote {
                    Name = ":dolphin:", Weight = 3
                },
                new FishEmote {
                    Name = ":whale:", Weight = 1
                },
                new FishEmote {
                    Name = ":squid:", Weight = 15
                },
                new FishEmote {
                    Name = ":whale2:", Weight = 1
                }
            };

            FishEmote randomEmote = WeightedRandomization.Choose(fishEmotes);

            await Context.Channel.SendMessageAsync(randomEmote.Name);
        }
Пример #2
0
    public void Randomize()
    {
        Random         r    = new Random();
        List <MyClass> list = new List <MyClass>();

        list.Add(new MyClass {
            Id = 1, Weight = 1
        });
        list.Add(new MyClass {
            Id = 2, Weight = 10
        });
        list.Add(new MyClass {
            Id = 3, Weight = 100
        });
        list.Add(new MyClass {
            Id = 4, Weight = 1000
        });
        MyClass randomlyselectedobject = WeightedRandomization.Choose(list.Cast <IWeighted>().ToList <IWeighted>(), r) as MyClass;
    }
Пример #3
0
 static void Main(string[] args)
 {
     var books = new List <Book> {
         new Book {
             Isbn = 1, Name = "A", Weight = 1
         },
         new Book {
             Isbn = 2, Name = "B", Weight = 100
         },
         new Book {
             Isbn = 3, Name = "C", Weight = 1000
         },
         new Book {
             Isbn = 4, Name = "D", Weight = 10000
         },
         new Book {
             Isbn = 5, Name = "E", Weight = 100000
         }
     };
     Book randomlySelectedBook = WeightedRandomization.Choose(books);
 }
Пример #4
0
    public static Book GetBook()
    {
        var books = new List <Book> {
            new Book {
                Isbn = 1, Name = "A", Weight = 1
            },
            new Book {
                Isbn = 2, Name = "B", Weight = 100
            },
            new Book {
                Isbn = 3, Name = "C", Weight = 1000
            },
            new Book {
                Isbn = 4, Name = "D", Weight = 10000
            },
            new Book {
                Isbn = 5, Name = "E", Weight = 100000
            }
        };

        return(WeightedRandomization.Choose(books) as Book);
    }
Пример #5
0
 public Point Choose()
 {
     return(WeightedRandomization.Choose(points));
 }
Пример #6
0
        DnsMessageBase ProcessQuery(DnsMessageBase message, IPAddress clientAddress, ProtocolType protocol)
        {
            message.IsQuery = false;

            DnsMessage query = message as DnsMessage;

            ret1 : repcnt++;
            if ((query != null) && (query.Questions.Count == 1))
            {
                // send query to upstream server
                DnsQuestion question = query.Questions[0];
                Random      rnd      = new Random();
                ret2 : if (enb == null)
                {
                    InitDNS();
                    goto ret2;
                }
                else
                {
                    //if (question.Name.Contains("xaapi"))
                    //{
                    //    message.ReturnCode = ReturnCode.ServerFailure;
                    //    return message;
                    //}

                    var tt = from q in hash where q.key == question.Name select q;
                    if (tt.Any())
                    {
                        tts = DateTime.Now - tt.ToList()[0].lastupdtime;
                        if (tts.TotalHours > 12)
                        {
                            tt.ToList()[0].answer = null;
                        }
                        //WriteToConsole(question.Name + " returned from hash");
                        if (tt.ToList()[0].answer != null)
                        {
                            tt.ToList()[0].selected++;
                            return(tt.ToList()[0].answer);
                        }
                    }
                    WriteToConsole(question.Name);
                    templ.Clear();
                    for (int i = 0; i < retcnt; i++)
                    {
                        ret3 :  var q = WeightedRandomization.Choose(enb);
                        if (!templ.Contains(q))
                        {
                            templ.Add(q);
                        }
                        else
                        {
                            goto ret3;
                        }
                    }

                    System.Threading.Tasks.Parallel.ForEach(templ, (site, state) =>
                    {
                        WriteToConsole("Get Info for " + question.Name + " from: " + site.IP);
                        DnsClient cd      = new DnsClient(IPAddress.Parse(site.IP), 1000);
                        DnsMessage answer = cd.Resolve(question.Name, question.RecordType, question.RecordClass);
                        if (answer != null)
                        {
                            foreach (DnsRecordBase record in (answer.AnswerRecords))
                            {
                                lock (query)
                                {
                                    query.AnswerRecords.Add(record);
                                }
                                site.Selects++;
                            }
                            foreach (DnsRecordBase record in (answer.AdditionalRecords))
                            {
                                lock (query)
                                {
                                    query.AnswerRecords.Add(record);
                                }
                                site.Selects++;
                            }
                            lock (query)
                            {
                                //site.Weight--;
                                query.ReturnCode = ReturnCode.NoError;
                            }
                            if (tt.Any())
                            {
                                tt.ToList()[0].answer      = query;
                                tt.ToList()[0].lastupdtime = DateTime.Now;
                                tt.ToList()[0].selected++;
                            }
                            else
                            {
                                var t = new hashitem(question.Name, query);
                                lock (hash)
                                {
                                    hash.Add(t);
                                    if (frst)
                                    {
                                        qsample = question;
                                        frst    = false;
                                    }
                                }
                            }

                            state.Break();
                        }
                    });
                    System.Threading.Tasks.Parallel.ForEach(templ, (site, state) =>
                    {
                        WriteToConsole("Get Info from: " + site.IP);
                        DnsClient cd      = new DnsClient(IPAddress.Parse(site.IP), 1000);
                        DnsMessage answer = cd.Resolve(question.Name, question.RecordType, question.RecordClass);
                        if (answer != null)
                        {
                            foreach (DnsRecordBase record in (answer.AnswerRecords))
                            {
                                lock (query)
                                {
                                    query.AnswerRecords.Add(record);
                                }
                                site.Selects++;
                                WriteToConsole(record.Name);
                            }
                            foreach (DnsRecordBase record in (answer.AdditionalRecords))
                            {
                                lock (query)
                                {
                                    query.AnswerRecords.Add(record);
                                }
                                site.Selects++;
                                WriteToConsole(record.Name);
                            }
                            lock (query)
                            {
                                site.Weight--;
                                query.ReturnCode = ReturnCode.NoError;
                                state.Break();
                            }
                        }
                    });
                }
                // if got an answer, copy it to the message sent to the client
            }
            if (query.ReturnCode == ReturnCode.NoError)
            {
                return(query);
            }
            if (repcnt > 5)
            {
                message.ReturnCode = ReturnCode.ServerFailure;
                return(message);
            }
            else
            {
                goto ret1;
            }
        }