.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)" +
$"\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.`";
}

View File

@ -20,15 +20,15 @@ namespace NadekoBot.Classes.Trivia {
Reload();
}
public TriviaQuestion GetRandomQuestion(List<TriviaQuestion> exclude) =>
pool.Except(exclude).FirstOrDefault();
public TriviaQuestion GetRandomQuestion(List<TriviaQuestion> 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();

View File

@ -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<Discord.Server, Cards> AllDecks = new ConcurrentDictionary<Discord.Server, Cards>();
public DrawCommand() : base() {
cards = new Cards();
}
public override Func<CommandEventArgs, Task> DoFunc() => async (e) =>
{
if (cards == null)
{
await e.Send("Shuffling cards...");
cards = new Cards();
}
public override Func<CommandEventArgs, Task> 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<Image> images = new List<Image>();
List<Cards.Card> cardObjects = new List<Cards.Card>();
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<Image> images = new List<Image>();
List<Cards.Card> cardObjects = new List<Cards.Card>();
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.");
});
}
}
}

View File

@ -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<Server, bool> pruneDict = new ConcurrentDictionary<Server, bool>();
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")