diff --git a/src/NadekoBot/Modules/Games/Commands/PlantAndPickCommands.cs b/src/NadekoBot/Modules/Games/Commands/PlantAndPickCommands.cs index 6b5da1b1..0fe72678 100644 --- a/src/NadekoBot/Modules/Games/Commands/PlantAndPickCommands.cs +++ b/src/NadekoBot/Modules/Games/Commands/PlantAndPickCommands.cs @@ -93,10 +93,10 @@ namespace NadekoBot.Modules.Games { firstPart = $"{dropAmount} random { NadekoBot.BotConfig.CurrencyPluralName } appeared!"; } - var file = GetRandomCurrencyImagePath(); + var file = GetRandomCurrencyImage(); var sent = await channel.SendFileAsync( - File.Open(file, FileMode.OpenOrCreate), - new FileInfo(file).Name, + file.Item2, + file.Item1, $"❗ {firstPart} Pick it up by typing `{NadekoBot.ModulePrefixes[typeof(Games).Name]}pick`") .ConfigureAwait(false); @@ -159,7 +159,7 @@ namespace NadekoBot.Modules.Games return; } - var file = GetRandomCurrencyImagePath(); + var file = GetRandomCurrencyImage(); IUserMessage msg; var vowelFirst = new[] { 'a', 'e', 'i', 'o', 'u' }.Contains(NadekoBot.BotConfig.CurrencyName[0]); @@ -170,7 +170,7 @@ namespace NadekoBot.Modules.Games } else { - msg = await Context.Channel.SendFileAsync(File.Open(file, FileMode.OpenOrCreate), new FileInfo(file).Name, msgToSend).ConfigureAwait(false); + msg = await Context.Channel.SendFileAsync(file.Item2, file.Item1, msgToSend).ConfigureAwait(false); } var msgs = new IUserMessage[amount]; @@ -220,10 +220,12 @@ namespace NadekoBot.Modules.Games } } - private static string GetRandomCurrencyImagePath() + private static Tuple GetRandomCurrencyImage() { var rng = new NadekoRandom(); - return Directory.GetFiles("data/currency_images").OrderBy(s => rng.Next()).FirstOrDefault(); + var images = NadekoBot.Images.CurrencyImages; + + return images[rng.Next(0, images.Count)]; } int GetRandomNumber() diff --git a/src/NadekoBot/Services/IImagesService.cs b/src/NadekoBot/Services/IImagesService.cs index b00b4a4a..192ee842 100644 --- a/src/NadekoBot/Services/IImagesService.cs +++ b/src/NadekoBot/Services/IImagesService.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.Immutable; using System.IO; using System.Linq; using System.Text; @@ -12,6 +13,8 @@ namespace NadekoBot.Services Stream Heads { get; } Stream Tails { get; } + IImmutableList> CurrencyImages { get; } + Task Reload(); } } diff --git a/src/NadekoBot/Services/Impl/ImagesService.cs b/src/NadekoBot/Services/Impl/ImagesService.cs index e57636c1..f63e2eb5 100644 --- a/src/NadekoBot/Services/Impl/ImagesService.cs +++ b/src/NadekoBot/Services/Impl/ImagesService.cs @@ -1,6 +1,8 @@ using NLog; using System; using System.Collections.Generic; +using System.Collections.Immutable; +using System.Collections.ObjectModel; using System.Diagnostics; using System.IO; using System.Linq; @@ -16,11 +18,18 @@ namespace NadekoBot.Services.Impl private const string headsPath = "data/images/coins/heads.png"; private const string tailsPath = "data/images/coins/tails.png"; + private const string currencyImagesPath = "data/currency_images"; + private byte[] heads; public Stream Heads => new MemoryStream(heads, false); private byte[] tails; public Stream Tails => new MemoryStream(tails, false); + //todo tuple + private IReadOnlyDictionary currencyImages; + public IImmutableList> CurrencyImages => + currencyImages.Select(x => new Tuple(x.Key, (Stream)new MemoryStream(x.Value, false))) + .ToImmutableArray(); private ImagesService() { @@ -36,11 +45,21 @@ namespace NadekoBot.Services.Impl 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!"); + try + { + _log.Info("Loading images..."); + var sw = Stopwatch.StartNew(); + heads = File.ReadAllBytes(headsPath); + tails = File.ReadAllBytes(tailsPath); + + currencyImages = Directory.GetFiles(currencyImagesPath).ToDictionary(x => Path.GetFileName(x), x => File.ReadAllBytes(x)); + _log.Info($"Images loaded after {sw.Elapsed.TotalSeconds:F2}s!"); + } + catch (Exception ex) + { + _log.Error(ex); + throw; + } }); } } \ No newline at end of file diff --git a/src/NadekoBot/data/currency_images/img2.jpg b/src/NadekoBot/data/currency_images/img2.jpg index 5697f8bb..cb22be2c 100644 Binary files a/src/NadekoBot/data/currency_images/img2.jpg and b/src/NadekoBot/data/currency_images/img2.jpg differ