.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

@ -21,7 +21,7 @@ namespace NadekoBot.Classes.Trivia {
} }
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"));

View File

@ -3,32 +3,28 @@ 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..."); await e.Send("Shuffling cards...");
cards = new Cards(); AllDecks.TryAdd(e.Server, 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;
@ -38,10 +34,8 @@ namespace NadekoBot
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."); await e.Send("No more cards in a deck.");
break; break;
} }
@ -51,19 +45,15 @@ namespace NadekoBot
} }
Bitmap bitmap = images.Merge(); Bitmap bitmap = images.Merge();
await e.Channel.SendFile(images.Count + " cards.jpg", bitmap.ToStream()); await e.Channel.SendFile(images.Count + " cards.jpg", bitmap.ToStream());
if (cardObjects.Count == 5) if (cardObjects.Count == 5) {
{
await e.Send(Cards.GetHandValue(cardObjects)); await e.Send(Cards.GetHandValue(cardObjects));
} }
} } catch (Exception ex) {
catch (Exception ex)
{
Console.WriteLine("Error drawing (a) card(s) " + ex.ToString()); 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,10 +63,9 @@ 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")