From 5977bcd2b6c32ae27d439c48ad0c8c7428816064 Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Wed, 10 Feb 2016 23:35:19 +0100 Subject: [PATCH] .prune removed, Cards are now per-server, trivia fixed, more stats --- NadekoBot/Classes/NadekoStats.cs | 1 + .../Classes/Trivia/TriviaQuestionPool.cs | 6 +- NadekoBot/Commands/DrawCommand.cs | 103 ++++++++---------- NadekoBot/Modules/Administration.cs | 14 ++- 4 files changed, 63 insertions(+), 61 deletions(-) diff --git a/NadekoBot/Classes/NadekoStats.cs b/NadekoBot/Classes/NadekoStats.cs index dd6b66c3..dd220dd3 100644 --- a/NadekoBot/Classes/NadekoStats.cs +++ b/NadekoBot/Classes/NadekoStats.cs @@ -61,6 +61,7 @@ namespace NadekoBot //$"\nUsers: {_client.Servers.SelectMany(x => x.Users.Select(y => y.Id)).Count()} (non-unique)" + $"\n`Heap: {Math.Round((double)GC.GetTotalMemory(true) / 1.MiB(), 2).ToString()} MB`" + $"\n`Commands Ran this session: {_commandsRan}`" + + $"\n`Message queue size:{_client.MessageQueue.Count}`" + $"\n`Greeted/Byed {Commands.ServerGreetCommand.Greeted} times.`"; } diff --git a/NadekoBot/Classes/Trivia/TriviaQuestionPool.cs b/NadekoBot/Classes/Trivia/TriviaQuestionPool.cs index 84670f15..68cf1d3a 100644 --- a/NadekoBot/Classes/Trivia/TriviaQuestionPool.cs +++ b/NadekoBot/Classes/Trivia/TriviaQuestionPool.cs @@ -20,15 +20,15 @@ namespace NadekoBot.Classes.Trivia { Reload(); } - public TriviaQuestion GetRandomQuestion(List exclude) => - pool.Except(exclude).FirstOrDefault(); + public TriviaQuestion GetRandomQuestion(List exclude) => + pool.Except(exclude).ToList()[_r.Next(0, pool.Count)]; internal void Reload() { JArray arr = JArray.Parse(File.ReadAllText("data/questions.txt")); foreach (var item in arr) { TriviaQuestion tq; - tq = new TriviaQuestion(item["Question"].ToString(), item["Answer"].ToString(),item["Category"]?.ToString()); + tq = new TriviaQuestion(item["Question"].ToString(), item["Answer"].ToString(), item["Category"]?.ToString()); pool.Add(tq); } var r = new Random(); diff --git a/NadekoBot/Commands/DrawCommand.cs b/NadekoBot/Commands/DrawCommand.cs index 87c94588..cf42f107 100644 --- a/NadekoBot/Commands/DrawCommand.cs +++ b/NadekoBot/Commands/DrawCommand.cs @@ -3,67 +3,57 @@ using System.Threading.Tasks; using Discord.Commands; using System.Drawing; using System.Collections.Generic; +using System.Collections.Concurrent; using NadekoBot.Extensions; -namespace NadekoBot -{ - class DrawCommand : DiscordCommand - { - private Cards cards = null; +namespace NadekoBot { + class DrawCommand : DiscordCommand { + private static ConcurrentDictionary AllDecks = new ConcurrentDictionary(); public DrawCommand() : base() { - cards = new Cards(); + } - public override Func DoFunc() => async (e) => - { - if (cards == null) - { - await e.Send("Shuffling cards..."); - cards = new Cards(); - } + public override Func DoFunc() => async (e) => { + if (!AllDecks.ContainsKey(e.Server)) { + await e.Send("Shuffling cards..."); + AllDecks.TryAdd(e.Server, new Cards()); + } - try - { - int num = 1; - var isParsed = int.TryParse(e.GetArg("count"), out num); - if (!isParsed || num < 2) - { - var c = cards.DrawACard(); - await e.Channel.SendFile(c.Name +".jpg",(Properties.Resources.ResourceManager.GetObject(c.Name) as Image).ToStream()); - return; - } - if (num > 5) - num = 5; + try { + var cards = AllDecks[e.Server]; + int num = 1; + var isParsed = int.TryParse(e.GetArg("count"), out num); + if (!isParsed || num < 2) { + var c = cards.DrawACard(); + await e.Channel.SendFile(c.Name + ".jpg", (Properties.Resources.ResourceManager.GetObject(c.Name) as Image).ToStream()); + return; + } + if (num > 5) + num = 5; - List images = new List(); - List cardObjects = new List(); - for (int i = 0; i < num; i++) - { - if (cards.CardPool.Count == 0 && i != 0) - { - await e.Send("No more cards in a deck."); - break; - } - var currentCard = cards.DrawACard(); - cardObjects.Add(currentCard); - images.Add(Properties.Resources.ResourceManager.GetObject(currentCard.Name) as Image); - } - Bitmap bitmap = images.Merge(); - await e.Channel.SendFile(images.Count + " cards.jpg", bitmap.ToStream()); - if (cardObjects.Count == 5) - { - await e.Send(Cards.GetHandValue(cardObjects)); - } - } - catch (Exception ex) - { - Console.WriteLine("Error drawing (a) card(s) " + ex.ToString()); - } - }; + List images = new List(); + List cardObjects = new List(); + for (int i = 0; i < num; i++) { + if (cards.CardPool.Count == 0 && i != 0) { + await e.Send("No more cards in a deck."); + break; + } + var currentCard = cards.DrawACard(); + cardObjects.Add(currentCard); + images.Add(Properties.Resources.ResourceManager.GetObject(currentCard.Name) as Image); + } + Bitmap bitmap = images.Merge(); + await e.Channel.SendFile(images.Count + " cards.jpg", bitmap.ToStream()); + if (cardObjects.Count == 5) { + await e.Send(Cards.GetHandValue(cardObjects)); + } + } catch (Exception ex) { + Console.WriteLine("Error drawing (a) card(s) " + ex.ToString()); + } + }; - public override void Init(CommandGroupBuilder cgb) - { + public override void Init(CommandGroupBuilder cgb) { cgb.CreateCommand("$draw") .Description("Draws a card from the deck.If you supply number [x], she draws up to 5 cards from the deck.\n**Usage**: $draw [x]") .Parameter("count", ParameterType.Optional) @@ -73,13 +63,12 @@ namespace NadekoBot .Alias("$reshuffle") .Description("Reshuffles all cards back into the deck.") .Do(async e => { - if (cards == null) { - cards = new Cards(); - } - cards.Restart(); + if (!AllDecks.ContainsKey(e.Server)) + AllDecks.TryAdd(e.Server, new Cards()); + AllDecks[e.Server].Restart(); await e.Send("Deck reshuffled."); }); } - + } } diff --git a/NadekoBot/Modules/Administration.cs b/NadekoBot/Modules/Administration.cs index b945f530..2e836583 100644 --- a/NadekoBot/Modules/Administration.cs +++ b/NadekoBot/Modules/Administration.cs @@ -13,6 +13,8 @@ using System.Collections.Concurrent; using Newtonsoft.Json.Linq; using System.Collections.Generic; using NadekoBot.Classes._DataModels; +using System.Threading; +using Timer = System.Timers.Timer; namespace NadekoBot.Modules { class Administration : DiscordModule { @@ -365,12 +367,14 @@ namespace NadekoBot.Modules { NadekoBot.client.Servers.ForEach(async s => { if (s.Name == e.Server.Name) return; await s.Leave(); }); }); - ConcurrentDictionary pruneDict = new ConcurrentDictionary(); cgb.CreateCommand(".prune") .Parameter("num", ParameterType.Required) .Description("Prunes a number of messages from the current channel.\n**Usage**: .prune 5") .Do(async e => { if (!e.User.ServerPermissions.ManageMessages) return; + + e.Send("This feature is being reconstructed."); + /* if (pruneDict.ContainsKey(e.Server)) return; int num; @@ -385,6 +389,7 @@ namespace NadekoBot.Modules { await m.Delete(); await Task.Delay(500); } + */ }); cgb.CreateCommand(".die") @@ -404,6 +409,12 @@ namespace NadekoBot.Modules { cgb.CreateCommand(".clr") .Description("Clears some of nadeko's messages from the current channel.") .Do(async e => { + await Task.Run(async () => { + var msgs = (await e.Channel.DownloadMessages(100)).Where(m => m.User.Id == NadekoBot.client.CurrentUser.Id); + foreach (var m in msgs) + await m.Delete(); + }); + /* try { if (clearDictionary.ContainsKey(e.Server)) return; @@ -419,6 +430,7 @@ namespace NadekoBot.Modules { } catch (Exception) { } bool throwaway; clearDictionary.TryRemove(e.Server, out throwaway); + */ }); cgb.CreateCommand(".newname") .Alias(".setname")