diff --git a/src/NadekoBot/Modules/Gambling/Commands/FlipCoinCommand.cs b/src/NadekoBot/Modules/Gambling/Commands/FlipCoinCommand.cs index 85092dc7..74e201c0 100644 --- a/src/NadekoBot/Modules/Gambling/Commands/FlipCoinCommand.cs +++ b/src/NadekoBot/Modules/Gambling/Commands/FlipCoinCommand.cs @@ -16,32 +16,38 @@ namespace NadekoBot.Modules.Gambling [Group] public class FlipCoinCommands : ModuleBase { + private readonly IImagesService _images; + private static NadekoRandom rng { get; } = new NadekoRandom(); - private const string headsPath = "data/images/coins/heads.png"; - private const string tailsPath = "data/images/coins/tails.png"; - + + public FlipCoinCommands() + { + //todo DI in the future, can't atm + this._images = NadekoBot.Images; + } + [NadekoCommand, Usage, Description, Aliases] public async Task Flip(int count = 1) { if (count == 1) { if (rng.Next(0, 2) == 1) - await Context.Channel.SendFileAsync(File.Open(headsPath, FileMode.OpenOrCreate), "heads.jpg", $"{Context.User.Mention} flipped " + Format.Code("Heads") + ".").ConfigureAwait(false); + await Context.Channel.SendFileAsync(_images.Heads, "heads.jpg", $"{Context.User.Mention} flipped " + Format.Code("Heads") + ".").ConfigureAwait(false); else - await Context.Channel.SendFileAsync(File.Open(tailsPath, FileMode.OpenOrCreate), "tails.jpg", $"{Context.User.Mention} flipped " + Format.Code("Tails") + ".").ConfigureAwait(false); + await Context.Channel.SendFileAsync(_images.Tails, "tails.jpg", $"{Context.User.Mention} flipped " + Format.Code("Tails") + ".").ConfigureAwait(false); return; } if (count > 10 || count < 1) { - await Context.Channel.SendErrorAsync("`Invalid number specified. You can flip 1 to 10 coins.`"); + await Context.Channel.SendErrorAsync("`Invalid number specified. You can flip 1 to 10 coins.`").ConfigureAwait(false); return; } var imgs = new Image[count]; for (var i = 0; i < count; i++) { imgs[i] = rng.Next(0, 10) < 5 ? - new Image(File.OpenRead(headsPath)) : - new Image(File.OpenRead(tailsPath)); + new Image(_images.Heads) : + new Image(_images.Tails); } await Context.Channel.SendFileAsync(imgs.Merge().ToStream(), $"{count} coins.png").ConfigureAwait(false); } @@ -70,15 +76,15 @@ namespace NadekoBot.Modules.Gambling var isHeads = guessStr == "HEADS" || guessStr == "H"; bool result = false; - string imgPathToSend; + Stream imageToSend; if (rng.Next(0, 2) == 1) { - imgPathToSend = headsPath; + imageToSend = _images.Heads; result = true; } else { - imgPathToSend = tailsPath; + imageToSend = _images.Tails; } string str; @@ -93,7 +99,7 @@ namespace NadekoBot.Modules.Gambling str = $"{Context.User.Mention}`Better luck next time.`"; } - await Context.Channel.SendFileAsync(File.Open(imgPathToSend, FileMode.OpenOrCreate), new FileInfo(imgPathToSend).Name, str).ConfigureAwait(false); + await Context.Channel.SendFileAsync(imageToSend, "result.png", str).ConfigureAwait(false); } } } diff --git a/src/NadekoBot/NadekoBot.cs b/src/NadekoBot/NadekoBot.cs index ba411cea..a32abc73 100644 --- a/src/NadekoBot/NadekoBot.cs +++ b/src/NadekoBot/NadekoBot.cs @@ -33,6 +33,7 @@ namespace NadekoBot public static GoogleApiService Google { get; private set; } public static StatsService Stats { get; private set; } + public static IImagesService Images { get; private set; } public static ConcurrentDictionary ModulePrefixes { get; private set; } public static bool Ready { get; private set; } @@ -68,8 +69,11 @@ namespace NadekoBot LogLevel = LogSeverity.Warning, TotalShards = Credentials.TotalShards, ConnectionTimeout = int.MaxValue, +#if !GLOBAL_NADEKO AlwaysDownloadUsers = true, +#endif }); + #if GLOBAL_NADEKO Client.Log += Client_Log; #endif @@ -82,6 +86,7 @@ namespace NadekoBot Google = new GoogleApiService(); CommandHandler = new CommandHandler(Client, CommandService); Stats = new StatsService(Client, CommandHandler); + Images = await ImagesService.Create().ConfigureAwait(false); ////setup DI //var depMap = new DependencyMap(); diff --git a/src/NadekoBot/Services/IImagesService.cs b/src/NadekoBot/Services/IImagesService.cs new file mode 100644 index 00000000..b00b4a4a --- /dev/null +++ b/src/NadekoBot/Services/IImagesService.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NadekoBot.Services +{ + public interface IImagesService + { + Stream Heads { get; } + Stream Tails { get; } + + Task Reload(); + } +} diff --git a/src/NadekoBot/Services/Impl/ImagesService.cs b/src/NadekoBot/Services/Impl/ImagesService.cs new file mode 100644 index 00000000..e57636c1 --- /dev/null +++ b/src/NadekoBot/Services/Impl/ImagesService.cs @@ -0,0 +1,46 @@ +using NLog; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NadekoBot.Services.Impl +{ + public class ImagesService : IImagesService + { + private readonly Logger _log; + + private const string headsPath = "data/images/coins/heads.png"; + private const string tailsPath = "data/images/coins/tails.png"; + + private byte[] heads; + public Stream Heads => new MemoryStream(heads, false); + + private byte[] tails; + public Stream Tails => new MemoryStream(tails, false); + + private ImagesService() + { + _log = LogManager.GetCurrentClassLogger(); + } + + public static async Task Create() + { + var srvc = new ImagesService(); + await srvc.Reload().ConfigureAwait(false); + return srvc; + } + + public Task Reload() => Task.Run(() => + { + _log.Info("Loading images..."); + var sw = Stopwatch.StartNew(); + heads = File.ReadAllBytes(headsPath); + tails = File.ReadAllBytes(tailsPath); + _log.Info($"Images loaded after {sw.Elapsed.TotalSeconds:F2}s!"); + }); + } +} \ No newline at end of file