diff --git a/Geekbot.net/Commands/GuildInfo.cs b/Geekbot.net/Commands/GuildInfo.cs index 7ee3d43..8eae02e 100644 --- a/Geekbot.net/Commands/GuildInfo.cs +++ b/Geekbot.net/Commands/GuildInfo.cs @@ -10,11 +10,15 @@ namespace Geekbot.net.Commands { public class GuildInfo : ModuleBase { - private readonly IDatabase redis; + private readonly IDatabase _redis; + private readonly ILevelCalc _levelCalc; + private readonly IErrorHandler _errorHandler; - public GuildInfo(IDatabase redis) + public GuildInfo(IDatabase redis, ILevelCalc levelCalc, IErrorHandler errorHandler) { - this.redis = redis; + _redis = redis; + _levelCalc = levelCalc; + _errorHandler = errorHandler; } [Command("serverstats", RunMode = RunMode.Async)] @@ -22,23 +26,30 @@ namespace Geekbot.net.Commands [Summary("Show some info about the bot.")] public async Task getInfo() { - var eb = new EmbedBuilder(); - eb.WithAuthor(new EmbedAuthorBuilder() - .WithIconUrl(Context.Guild.IconUrl) - .WithName(Context.Guild.Name)); - eb.WithColor(new Color(110, 204, 147)); + try + { + var eb = new EmbedBuilder(); + eb.WithAuthor(new EmbedAuthorBuilder() + .WithIconUrl(Context.Guild.IconUrl) + .WithName(Context.Guild.Name)); + eb.WithColor(new Color(110, 204, 147)); - var created = Context.Guild.CreatedAt; - var age = Math.Floor((DateTime.Now - created).TotalDays); + var created = Context.Guild.CreatedAt; + var age = Math.Floor((DateTime.Now - created).TotalDays); - var messages = redis.HashGet($"{Context.Guild.Id}:Messages", 0.ToString()); - var level = LevelCalc.GetLevelAtExperience((int) messages); + var messages = _redis.HashGet($"{Context.Guild.Id}:Messages", 0.ToString()); + var level = _levelCalc.GetLevelAtExperience((int) messages); - eb.AddField("Server Age", $"{created.Day}/{created.Month}/{created.Year} ({age} days)"); - eb.AddInlineField("Level", level) - .AddInlineField("Messages", messages); + eb.AddField("Server Age", $"{created.Day}/{created.Month}/{created.Year} ({age} days)"); + eb.AddInlineField("Level", level) + .AddInlineField("Messages", messages); - await ReplyAsync("", false, eb.Build()); + await ReplyAsync("", false, eb.Build()); + } + catch (Exception e) + { + _errorHandler.HandleCommandException(e, Context); + } } public static string FirstCharToUpper(string input) diff --git a/Geekbot.net/Commands/Stats.cs b/Geekbot.net/Commands/Stats.cs index b35cc45..37c3ea9 100644 --- a/Geekbot.net/Commands/Stats.cs +++ b/Geekbot.net/Commands/Stats.cs @@ -11,11 +11,13 @@ namespace Geekbot.net.Commands { private readonly IDatabase _redis; private readonly IErrorHandler _errorHandler; + private readonly ILevelCalc _levelCalc; - public Stats(IDatabase redis, IErrorHandler errorHandler) + public Stats(IDatabase redis, IErrorHandler errorHandler, ILevelCalc levelCalc) { _redis = redis; _errorHandler = errorHandler; + _levelCalc = levelCalc; } [Command("stats", RunMode = RunMode.Async)] @@ -34,7 +36,7 @@ namespace Geekbot.net.Commands var messages = (int) _redis.HashGet($"{Context.Guild.Id}:Messages", userInfo.Id.ToString()); var guildMessages = (int) _redis.HashGet($"{Context.Guild.Id}:Messages", 0.ToString()); - var level = LevelCalc.GetLevelAtExperience(messages); + var level = _levelCalc.GetLevelAtExperience(messages); var percent = Math.Round((double) (100 * messages) / guildMessages, 2); diff --git a/Geekbot.net/Lib/LevelCalc.cs b/Geekbot.net/Lib/LevelCalc.cs index 5d47dc9..6ab5162 100644 --- a/Geekbot.net/Lib/LevelCalc.cs +++ b/Geekbot.net/Lib/LevelCalc.cs @@ -1,27 +1,38 @@ using System; +using System.Collections.Generic; namespace Geekbot.net.Lib { - internal class LevelCalc + internal class LevelCalc : ILevelCalc { - private static int GetExperienceAtLevel(int level) - { - double total = 0; - for (var i = 1; i < level; i++) - total += Math.Floor(i + 300 * Math.Pow(2, i / 7.0)); + private int[] _levels; - return (int) Math.Floor(total / 16); + public LevelCalc() + { + var levels = new List(); + double total = 0; + for (var i = 1; i < 120; i++) + { + total += Math.Floor(i + 300 * Math.Pow(2, i / 7.0)); + levels.Add((int) Math.Floor(total / 16)); + } + _levels = levels.ToArray(); } - public static int GetLevelAtExperience(int experience) + public int GetLevelAtExperience(int experience) { - int index; - - for (index = 0; index < 120; index++) - if (GetExperienceAtLevel(index + 1) > experience) - break; - - return index; + var returnVal = 1; + foreach (var level in _levels) + { + if (level > experience) break; + returnVal++; + } + return returnVal; } } + + public interface ILevelCalc + { + int GetLevelAtExperience(int experience); + } } \ No newline at end of file diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 0a78266..2488827 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -109,11 +109,13 @@ namespace Geekbot.net var fortunes = new FortunesProvider(randomClient, logger); var mediaProvider = new MediaProvider(randomClient, logger); var malClient = new MalClient(redis, logger); + var levelCalc = new LevelCalc(); services.AddSingleton(errorHandler); services.AddSingleton(redis); services.AddSingleton(logger); services.AddSingleton(userRepository); + services.AddSingleton(levelCalc); services.AddSingleton(randomClient); services.AddSingleton(fortunes); services.AddSingleton(mediaProvider);