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!";
}
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<string, Stream> 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()

View File

@ -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<Tuple<string, Stream>> CurrencyImages { get; }
Task Reload();
}
}

View File

@ -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<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()
{
@ -35,12 +44,22 @@ namespace NadekoBot.Services.Impl
}
public Task Reload() => Task.Run(() =>
{
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;
}
});
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 225 KiB

After

Width:  |  Height:  |  Size: 33 KiB