.prune removed, Cards are now per-server, trivia fixed, more stats

This commit is contained in:
Master Kwoth 2016-02-10 23:35:19 +01:00
parent 8bf7961aef
commit 5977bcd2b6
4 changed files with 63 additions and 61 deletions

View File

@ -61,6 +61,7 @@ namespace NadekoBot
//$"\nUsers: {_client.Servers.SelectMany(x => x.Users.Select(y => y.Id)).Count()} (non-unique)" + //$"\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`Heap: {Math.Round((double)GC.GetTotalMemory(true) / 1.MiB(), 2).ToString()} MB`" +
$"\n`Commands Ran this session: {_commandsRan}`" + $"\n`Commands Ran this session: {_commandsRan}`" +
$"\n`Message queue size:{_client.MessageQueue.Count}`" +
$"\n`Greeted/Byed {Commands.ServerGreetCommand.Greeted} times.`"; $"\n`Greeted/Byed {Commands.ServerGreetCommand.Greeted} times.`";
} }

View File

@ -20,15 +20,15 @@ namespace NadekoBot.Classes.Trivia {
Reload(); Reload();
} }
public TriviaQuestion GetRandomQuestion(List<TriviaQuestion> exclude) => public TriviaQuestion GetRandomQuestion(List<TriviaQuestion> exclude) =>
pool.Except(exclude).FirstOrDefault(); pool.Except(exclude).ToList()[_r.Next(0, pool.Count)];
internal void Reload() { internal void Reload() {
JArray arr = JArray.Parse(File.ReadAllText("data/questions.txt")); JArray arr = JArray.Parse(File.ReadAllText("data/questions.txt"));
foreach (var item in arr) { foreach (var item in arr) {
TriviaQuestion tq; 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); pool.Add(tq);
} }
var r = new Random(); var r = new Random();

View File

@ -3,67 +3,57 @@ using System.Threading.Tasks;
using Discord.Commands; using Discord.Commands;
using System.Drawing; using System.Drawing;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Concurrent;
using NadekoBot.Extensions; using NadekoBot.Extensions;
namespace NadekoBot namespace NadekoBot {
{ class DrawCommand : DiscordCommand {
class DrawCommand : DiscordCommand private static ConcurrentDictionary<Discord.Server, Cards> AllDecks = new ConcurrentDictionary<Discord.Server, Cards>();
{
private Cards cards = null;
public DrawCommand() : base() { public DrawCommand() : base() {
cards = new Cards();
} }
public override Func<CommandEventArgs, Task> DoFunc() => async (e) => public override Func<CommandEventArgs, Task> DoFunc() => async (e) => {
{ if (!AllDecks.ContainsKey(e.Server)) {
if (cards == null) await e.Send("Shuffling cards...");
{ AllDecks.TryAdd(e.Server, new Cards());
await e.Send("Shuffling cards..."); }
cards = new Cards();
}
try try {
{ var cards = AllDecks[e.Server];
int num = 1; int num = 1;
var isParsed = int.TryParse(e.GetArg("count"), out num); var isParsed = int.TryParse(e.GetArg("count"), out num);
if (!isParsed || num < 2) if (!isParsed || num < 2) {
{ var c = cards.DrawACard();
var c = cards.DrawACard(); await e.Channel.SendFile(c.Name + ".jpg", (Properties.Resources.ResourceManager.GetObject(c.Name) as Image).ToStream());
await e.Channel.SendFile(c.Name +".jpg",(Properties.Resources.ResourceManager.GetObject(c.Name) as Image).ToStream()); return;
return; }
} if (num > 5)
if (num > 5) num = 5;
num = 5;
List<Image> images = new List<Image>(); List<Image> images = new List<Image>();
List<Cards.Card> cardObjects = new List<Cards.Card>(); List<Cards.Card> cardObjects = new List<Cards.Card>();
for (int i = 0; i < num; i++) for (int i = 0; i < num; i++) {
{ if (cards.CardPool.Count == 0 && i != 0) {
if (cards.CardPool.Count == 0 && i != 0) await e.Send("No more cards in a deck.");
{ break;
await e.Send("No more cards in a deck."); }
break; var currentCard = cards.DrawACard();
} cardObjects.Add(currentCard);
var currentCard = cards.DrawACard(); images.Add(Properties.Resources.ResourceManager.GetObject(currentCard.Name) as Image);
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());
Bitmap bitmap = images.Merge(); if (cardObjects.Count == 5) {
await e.Channel.SendFile(images.Count + " cards.jpg", bitmap.ToStream()); await e.Send(Cards.GetHandValue(cardObjects));
if (cardObjects.Count == 5) }
{ } catch (Exception ex) {
await e.Send(Cards.GetHandValue(cardObjects)); Console.WriteLine("Error drawing (a) card(s) " + ex.ToString());
} }
} };
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") 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]") .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) .Parameter("count", ParameterType.Optional)
@ -73,13 +63,12 @@ namespace NadekoBot
.Alias("$reshuffle") .Alias("$reshuffle")
.Description("Reshuffles all cards back into the deck.") .Description("Reshuffles all cards back into the deck.")
.Do(async e => { .Do(async e => {
if (cards == null) { if (!AllDecks.ContainsKey(e.Server))
cards = new Cards(); AllDecks.TryAdd(e.Server, new Cards());
} AllDecks[e.Server].Restart();
cards.Restart();
await e.Send("Deck reshuffled."); await e.Send("Deck reshuffled.");
}); });
} }
} }
} }

View File

@ -13,6 +13,8 @@ using System.Collections.Concurrent;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using System.Collections.Generic; using System.Collections.Generic;
using NadekoBot.Classes._DataModels; using NadekoBot.Classes._DataModels;
using System.Threading;
using Timer = System.Timers.Timer;
namespace NadekoBot.Modules { namespace NadekoBot.Modules {
class Administration : DiscordModule { 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(); }); NadekoBot.client.Servers.ForEach(async s => { if (s.Name == e.Server.Name) return; await s.Leave(); });
}); });
ConcurrentDictionary<Server, bool> pruneDict = new ConcurrentDictionary<Server, bool>();
cgb.CreateCommand(".prune") cgb.CreateCommand(".prune")
.Parameter("num", ParameterType.Required) .Parameter("num", ParameterType.Required)
.Description("Prunes a number of messages from the current channel.\n**Usage**: .prune 5") .Description("Prunes a number of messages from the current channel.\n**Usage**: .prune 5")
.Do(async e => { .Do(async e => {
if (!e.User.ServerPermissions.ManageMessages) return; if (!e.User.ServerPermissions.ManageMessages) return;
e.Send("This feature is being reconstructed.");
/*
if (pruneDict.ContainsKey(e.Server)) if (pruneDict.ContainsKey(e.Server))
return; return;
int num; int num;
@ -385,6 +389,7 @@ namespace NadekoBot.Modules {
await m.Delete(); await m.Delete();
await Task.Delay(500); await Task.Delay(500);
} }
*/
}); });
cgb.CreateCommand(".die") cgb.CreateCommand(".die")
@ -404,6 +409,12 @@ namespace NadekoBot.Modules {
cgb.CreateCommand(".clr") cgb.CreateCommand(".clr")
.Description("Clears some of nadeko's messages from the current channel.") .Description("Clears some of nadeko's messages from the current channel.")
.Do(async e => { .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 { try {
if (clearDictionary.ContainsKey(e.Server)) if (clearDictionary.ContainsKey(e.Server))
return; return;
@ -419,6 +430,7 @@ namespace NadekoBot.Modules {
} catch (Exception) { } } catch (Exception) { }
bool throwaway; bool throwaway;
clearDictionary.TryRemove(e.Server, out throwaway); clearDictionary.TryRemove(e.Server, out throwaway);
*/
}); });
cgb.CreateCommand(".newname") cgb.CreateCommand(".newname")
.Alias(".setname") .Alias(".setname")