From b8dad9a1cd27a03c6bcd58776b2c1ab0e63f9b60 Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Sat, 5 Mar 2016 12:20:27 +0100 Subject: [PATCH] some improvements, cleanup trivias and typings when blacklisting a server --- NadekoBot/Classes/NadekoStats.cs | 7 ++++++- NadekoBot/Classes/Trivia/TriviaGame.cs | 2 +- NadekoBot/Commands/DiceRollCommand.cs | 12 ++++++------ NadekoBot/Commands/PlayingRotate.cs | 2 +- NadekoBot/Commands/SpeedTyping.cs | 23 ++++++++++++----------- NadekoBot/Commands/TriviaCommand.cs | 10 +++++----- NadekoBot/Modules/Permissions.cs | 5 +++++ 7 files changed, 36 insertions(+), 25 deletions(-) diff --git a/NadekoBot/Classes/NadekoStats.cs b/NadekoBot/Classes/NadekoStats.cs index 4733c451..e7d7a8fd 100644 --- a/NadekoBot/Classes/NadekoStats.cs +++ b/NadekoBot/Classes/NadekoStats.cs @@ -6,6 +6,7 @@ using System.Linq; using NadekoBot.Extensions; using System.Threading.Tasks; using System.Reflection; +using System.Timers; using NadekoBot.Modules; namespace NadekoBot { @@ -22,6 +23,8 @@ namespace NadekoBot { public int TextChannelsCount { get; private set; } = 0; public int VoiceChannelsCount { get; private set; } = 0; + private readonly Timer commandLogTimer = new Timer() {Interval = 10000}; + static NadekoStats() { } private NadekoStats() { @@ -31,7 +34,8 @@ namespace NadekoBot { commandService.CommandExecuted += StatsCollector_RanCommand; Task.Run(StartCollecting); - Console.WriteLine("Logging enabled."); + + commandLogTimer.Start(); ServerCount = NadekoBot.Client.Servers.Count(); var channels = NadekoBot.Client.Servers.SelectMany(s => s.AllChannels); @@ -139,6 +143,7 @@ namespace NadekoBot { } private async void StatsCollector_RanCommand(object sender, CommandEventArgs e) { + Console.WriteLine($">>Command {e.Command.Text}"); await Task.Run(() => { try { commandsRan++; diff --git a/NadekoBot/Classes/Trivia/TriviaGame.cs b/NadekoBot/Classes/Trivia/TriviaGame.cs index 238afb1f..613a2af2 100644 --- a/NadekoBot/Classes/Trivia/TriviaGame.cs +++ b/NadekoBot/Classes/Trivia/TriviaGame.cs @@ -83,7 +83,7 @@ namespace NadekoBot.Classes.Trivia { ShouldStopGame = true; await channel.SendMessage("**Trivia game ended**\n" + GetLeaderboard()); TriviaGame throwAwayValue; - Commands.Trivia.runningTrivias.TryRemove(server, out throwAwayValue); + Commands.Trivia.RunningTrivias.TryRemove(server.Id, out throwAwayValue); } public async Task StopGame() { diff --git a/NadekoBot/Commands/DiceRollCommand.cs b/NadekoBot/Commands/DiceRollCommand.cs index 140ce2ab..6ad4e648 100644 --- a/NadekoBot/Commands/DiceRollCommand.cs +++ b/NadekoBot/Commands/DiceRollCommand.cs @@ -54,10 +54,10 @@ namespace NadekoBot.Commands { } var bitmap = dices.Merge(); - await e.Channel.SendMessage(values.Count + " Dies rolled. Total: **" + values.Sum() + "** Average: **" + (values.Sum() / (1.0f * values.Count)).ToString("N2") + "**"); - await e.Channel.SendFile("dices.png", bitmap.ToStream(ImageFormat.Png)); - } catch { - await e.Channel.SendMessage("Please enter a number of dices to roll."); + await e.Channel.SendMessage(values.Count + " Dice rolled. Total: **" + values.Sum() + "** Average: **" + (values.Sum() / (1.0f * values.Count)).ToString("N2") + "**"); + await e.Channel.SendFile("dice.png", bitmap.ToStream(ImageFormat.Png)); + } catch { + await e.Channel.SendMessage("Please enter a number of dice to roll."); } } }; @@ -87,9 +87,9 @@ namespace NadekoBot.Commands { .ToArray(); if (arr[0] > arr[1]) throw new ArgumentException("First argument should be bigger than the second one."); - rolled = new Random().Next(arr[0],arr[1]+1); + rolled = new Random().Next(arr[0], arr[1] + 1); } else { - rolled = new Random().Next(0, int.Parse(e.GetArg("range"))+1); + rolled = new Random().Next(0, int.Parse(e.GetArg("range")) + 1); } await e.Channel.SendMessage($"{e.User.Mention} rolled **{rolled}**."); diff --git a/NadekoBot/Commands/PlayingRotate.cs b/NadekoBot/Commands/PlayingRotate.cs index 472fe9ff..d96cf3a0 100644 --- a/NadekoBot/Commands/PlayingRotate.cs +++ b/NadekoBot/Commands/PlayingRotate.cs @@ -30,7 +30,7 @@ namespace NadekoBot.Commands { } }, {"%queued%", () => Music.MusicPlayers.Sum(kvp => kvp.Value.Playlist.Count).ToString()}, - {"%trivia%", () => Trivia.runningTrivias.Count.ToString()} + {"%trivia%", () => Trivia.RunningTrivias.Count.ToString()} }; private readonly object playingPlaceholderLock = new object(); diff --git a/NadekoBot/Commands/SpeedTyping.cs b/NadekoBot/Commands/SpeedTyping.cs index c8da5740..ccfcb34e 100644 --- a/NadekoBot/Commands/SpeedTyping.cs +++ b/NadekoBot/Commands/SpeedTyping.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -96,7 +97,6 @@ namespace NadekoBot.Commands { if (finishedUserIds.Count % 2 == 0) { await e.Channel.SendMessage($":exclamation: `A lot of people finished, here is the text for those still typing:`\n\n:book:**{CurrentSentence}**:book:"); } - } } catch { } @@ -108,32 +108,33 @@ namespace NadekoBot.Commands { internal class SpeedTyping : IDiscordCommand { - private static Dictionary runningContests; + public static ConcurrentDictionary RunningContests; public SpeedTyping() { - runningContests = new Dictionary(); + RunningContests = new ConcurrentDictionary(); } public Func DoFunc() => async e => { - if (runningContests.ContainsKey(e.User.Server.Id) && runningContests[e.User.Server.Id].IsActive) { - await e.Channel.SendMessage($"Contest already running in { runningContests[e.User.Server.Id].Channell.Mention } channel."); + if (RunningContests.ContainsKey(e.User.Server.Id) && RunningContests[e.User.Server.Id].IsActive) { + await e.Channel.SendMessage($"Contest already running in { RunningContests[e.User.Server.Id].Channell.Mention } channel."); return; } - if (runningContests.ContainsKey(e.User.Server.Id) && !runningContests[e.User.Server.Id].IsActive) { - await runningContests[e.User.Server.Id].Start(); + if (RunningContests.ContainsKey(e.User.Server.Id) && !RunningContests[e.User.Server.Id].IsActive) { + await RunningContests[e.User.Server.Id].Start(); return; } var tg = new TypingGame(e.Channel); - runningContests.Add(e.Server.Id, tg); + RunningContests.TryAdd(e.Server.Id, tg); await tg.Start(); }; private Func QuitFunc() => async e => { - if (runningContests.ContainsKey(e.User.Server.Id) && - await runningContests[e.User.Server.Id].Stop()) { - runningContests.Remove(e.User.Server.Id); + if (RunningContests.ContainsKey(e.User.Server.Id) && + await RunningContests[e.User.Server.Id].Stop()) { + TypingGame throwaway; + RunningContests.TryRemove(e.User.Server.Id, out throwaway); return; } await e.Channel.SendMessage("No contest to stop on this channel."); diff --git a/NadekoBot/Commands/TriviaCommand.cs b/NadekoBot/Commands/TriviaCommand.cs index d5003129..8ff83275 100644 --- a/NadekoBot/Commands/TriviaCommand.cs +++ b/NadekoBot/Commands/TriviaCommand.cs @@ -7,13 +7,13 @@ using TriviaGame = NadekoBot.Classes.Trivia.TriviaGame; namespace NadekoBot.Commands { internal class Trivia : IDiscordCommand { - public static ConcurrentDictionary runningTrivias = new ConcurrentDictionary(); + public static ConcurrentDictionary RunningTrivias = new ConcurrentDictionary(); public Func DoFunc() => async e => { TriviaGame trivia; - if (!runningTrivias.TryGetValue(e.Server, out trivia)) { + if (!RunningTrivias.TryGetValue(e.Server.Id, out trivia)) { var triviaGame = new TriviaGame(e); - if (runningTrivias.TryAdd(e.Server, triviaGame)) + if (RunningTrivias.TryAdd(e.Server.Id, triviaGame)) await e.Channel.SendMessage("**Trivia game started!**\nFirst player to get to 10 points wins! You have 30 seconds per question.\nUse command `tq` if game was started by accident.**"); else await triviaGame.StopGame(); @@ -34,7 +34,7 @@ namespace NadekoBot.Commands { .Alias("-tlb") .Do(async e=> { TriviaGame trivia; - if (runningTrivias.TryGetValue(e.Server, out trivia)) + if (RunningTrivias.TryGetValue(e.Server.Id, out trivia)) await e.Channel.SendMessage(trivia.GetLeaderboard()); else await e.Channel.SendMessage("No trivia is running on this server."); @@ -45,7 +45,7 @@ namespace NadekoBot.Commands { .Alias("-tq") .Do(async e=> { TriviaGame trivia; - if (runningTrivias.TryGetValue(e.Server, out trivia)) { + if (RunningTrivias.TryGetValue(e.Server.Id, out trivia)) { await trivia.StopGame(); } else await e.Channel.SendMessage("No trivia is running on this server."); diff --git a/NadekoBot/Modules/Permissions.cs b/NadekoBot/Modules/Permissions.cs index a8ed6731..f4dcfb0b 100644 --- a/NadekoBot/Modules/Permissions.cs +++ b/NadekoBot/Modules/Permissions.cs @@ -480,6 +480,11 @@ namespace NadekoBot.Modules { } NadekoBot.Config.ServerBlacklist.Add(server.Id); NadekoBot.SaveConfig(); + //cleanup trivias and typeracing + Classes.Trivia.TriviaGame trivia; + Commands.Trivia.RunningTrivias.TryRemove(server.Id, out trivia); + Commands.TypingGame typeracer; + Commands.SpeedTyping.RunningContests.TryRemove(server.Id, out typeracer); await e.Channel.SendMessage($"`Sucessfully blacklisted server {server.Name}`"); }); });