Currency images are preloaded now too.(>plant/>gc)

This commit is contained in:
Kwoth 2017-02-03 00:22:29 +01:00
parent 22f7443f0b
commit a728e6f670
4 changed files with 36 additions and 12 deletions

View File

@ -93,10 +93,10 @@ namespace NadekoBot.Modules.Games
{ {
firstPart = $"{dropAmount} random { NadekoBot.BotConfig.CurrencyPluralName } appeared!"; firstPart = $"{dropAmount} random { NadekoBot.BotConfig.CurrencyPluralName } appeared!";
} }
var file = GetRandomCurrencyImagePath(); var file = GetRandomCurrencyImage();
var sent = await channel.SendFileAsync( var sent = await channel.SendFileAsync(
File.Open(file, FileMode.OpenOrCreate), file.Item2,
new FileInfo(file).Name, file.Item1,
$"❗ {firstPart} Pick it up by typing `{NadekoBot.ModulePrefixes[typeof(Games).Name]}pick`") $"❗ {firstPart} Pick it up by typing `{NadekoBot.ModulePrefixes[typeof(Games).Name]}pick`")
.ConfigureAwait(false); .ConfigureAwait(false);
@ -159,7 +159,7 @@ namespace NadekoBot.Modules.Games
return; return;
} }
var file = GetRandomCurrencyImagePath(); var file = GetRandomCurrencyImage();
IUserMessage msg; IUserMessage msg;
var vowelFirst = new[] { 'a', 'e', 'i', 'o', 'u' }.Contains(NadekoBot.BotConfig.CurrencyName[0]); var vowelFirst = new[] { 'a', 'e', 'i', 'o', 'u' }.Contains(NadekoBot.BotConfig.CurrencyName[0]);
@ -170,7 +170,7 @@ namespace NadekoBot.Modules.Games
} }
else 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]; var msgs = new IUserMessage[amount];
@ -220,10 +220,12 @@ namespace NadekoBot.Modules.Games
} }
} }
private static string GetRandomCurrencyImagePath() private static Tuple<string, Stream> GetRandomCurrencyImage()
{ {
var rng = new NadekoRandom(); 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() int GetRandomNumber()

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -12,6 +13,8 @@ namespace NadekoBot.Services
Stream Heads { get; } Stream Heads { get; }
Stream Tails { get; } Stream Tails { get; }
IImmutableList<Tuple<string, Stream>> CurrencyImages { get; }
Task Reload(); Task Reload();
} }
} }

View File

@ -1,6 +1,8 @@
using NLog; using NLog;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable;
using System.Collections.ObjectModel;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
@ -16,11 +18,18 @@ namespace NadekoBot.Services.Impl
private const string headsPath = "data/images/coins/heads.png"; private const string headsPath = "data/images/coins/heads.png";
private const string tailsPath = "data/images/coins/tails.png"; private const string tailsPath = "data/images/coins/tails.png";
private const string currencyImagesPath = "data/currency_images";
private byte[] heads; private byte[] heads;
public Stream Heads => new MemoryStream(heads, false); public Stream Heads => new MemoryStream(heads, false);
private byte[] tails; private byte[] tails;
public Stream Tails => new MemoryStream(tails, false); public Stream Tails => new MemoryStream(tails, false);
//todo tuple
private IReadOnlyDictionary<string, byte[]> currencyImages;
public IImmutableList<Tuple<string, Stream>> CurrencyImages =>
currencyImages.Select(x => new Tuple<string, Stream>(x.Key, (Stream)new MemoryStream(x.Value, false)))
.ToImmutableArray();
private ImagesService() private ImagesService()
{ {
@ -36,11 +45,21 @@ namespace NadekoBot.Services.Impl
public Task Reload() => Task.Run(() => public Task Reload() => Task.Run(() =>
{ {
_log.Info("Loading images..."); try
var sw = Stopwatch.StartNew(); {
heads = File.ReadAllBytes(headsPath); _log.Info("Loading images...");
tails = File.ReadAllBytes(tailsPath); var sw = Stopwatch.StartNew();
_log.Info($"Images loaded after {sw.Elapsed.TotalSeconds:F2}s!"); 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;
}
}); });
} }
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 225 KiB

After

Width:  |  Height:  |  Size: 33 KiB