public async Task Start(CommandContext ctx) { if (ctx.Channel.IsPrivate) { await ctx.TriggerTypingAsync(); var interactivity = ctx.Client.GetInteractivityModule(); var text = StaticRandom.NextString(StaticRandom.Next(10, 20), "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPRQSTUVWXYZ0123456789"); var font = new Font(SystemFonts.DefaultFont.FontFamily, StaticRandom.Next(8, 48), (FontStyle)StaticRandom.Next(16)); SizeF textSize; using (var image = new Bitmap(1, 1)) using (var drawing = Graphics.FromImage(image)) { textSize = drawing.MeasureString(text, font); } using (var image = new Bitmap((int)textSize.Width, (int)textSize.Height)) using (var drawing = Graphics.FromImage(image)) using (var brush = new SolidBrush(Color.Black)) using (var stream = new MemoryStream()) { drawing.Clear(Color.White); drawing.DrawString(text, font, brush, 0, 0); image.Save(stream, ImageFormat.Png); stream.Seek(0, SeekOrigin.Begin); await ctx.RespondWithFileAsync(stream, "Captcha.png"); } var response = await interactivity.WaitForMessageAsync(message => message.Author.Id == ctx.User.Id && message.Channel.IsPrivate, TimeSpan.FromSeconds(2.0)); await ctx.TriggerTypingAsync(); if (response == null) { await(await ctx.RespondAsync("Too late!")).CreateReactionAsync(DiscordEmoji.FromUnicode("⌛")); return; } if (text.Equals(response.Message.Content, StringComparison.Ordinal)) { await response.Message.CreateReactionAsync(DiscordEmoji.FromUnicode("🎉")); await ctx.RespondAsync("You did it! The flag is Pycon_{{345y_0cr}}."); } else { await(await ctx.RespondAsync("You lost!")).CreateReactionAsync(DiscordEmoji.FromUnicode("😏")); } } }
/// <summary> /// Tests to make sure the that the requested error rate is achieved /// when inserting random strings. /// </summary> public static void ErrorRateWithinExpectedBounds <T>(BaseBloomFilter <T> bf) { for (int i = 0; i < bf.ExpectedNumberOfElements; i++) { bf.Add(StaticRandom.NextString(10)); } int falsePositives = 0; for (int i = 0; i < bf.ExpectedNumberOfElements; i++) { if (bf.IsPresent(StaticRandom.NextString(5))) { falsePositives++; } } Assert.LessOrEqual(falsePositives, (1.03d * ((double)bf.ExpectedNumberOfElements) * bf.ErrorRate)); }
public async Task Start(CommandContext ctx) { if (ctx.Channel.IsPrivate) { await ctx.TriggerTypingAsync(); var interactivity = ctx.Client.GetInteractivityModule(); var text = StaticRandom.NextString(StaticRandom.Next(6, 8), "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPRQSTUVWXYZ0123456789"); var fontIndex = StaticRandom.Next(-1, Fonts.Count); using (var font = fontIndex == -1 ? null : new MemoryStream(Fonts[fontIndex])) { var asciiText = new AsciiArt(text, font == null ? null : new FIGletFont(font)); await ctx.RespondAsync($"```{asciiText}```"); } var response = await interactivity.WaitForMessageAsync(message => message.Author.Id == ctx.User.Id && message.Channel.IsPrivate, TimeSpan.FromSeconds(1.0)); await ctx.TriggerTypingAsync(); if (response == null) { await(await ctx.RespondAsync("Too late!")).CreateReactionAsync(DiscordEmoji.FromUnicode("⌛")); return; } if (text.Equals(response.Message.Content, StringComparison.Ordinal)) { await response.Message.CreateReactionAsync(DiscordEmoji.FromUnicode("🎉")); await ctx.RespondAsync("You did it! The flag is Pycon_{{45c11_M45t3r}}."); } else { await(await ctx.RespondAsync("You lost!")).CreateReactionAsync(DiscordEmoji.FromUnicode("😏")); } } }
public static void TestThreadSafety() { int NUMBER_OF_THREADS = 5; int NUMBER_OF_OPS = 100000; List <Thread> threads = new List <Thread>(); ThreadStart threadDelegate = (ThreadStart) delegate() { Thread.Sleep(0); byte[] buffer = new byte[1]; for (int i = 0; i < NUMBER_OF_OPS; i++) { StaticRandom.Next(); StaticRandom.Next(100); StaticRandom.Next(10, 100); StaticRandom.NextDouble(); StaticRandom.NextBytes(buffer); StaticRandom.NextString(1, true, true); } }; for (int i = 0; i < NUMBER_OF_THREADS; i++) { threads.Add(new Thread(threadDelegate)); } foreach (Thread t in threads) { t.Start(); } foreach (Thread t in threads) { t.Join(); } }