Images service. Preloads images bot uses. Holds heads and tails images only for now.
This commit is contained in:
parent
d8b700d11a
commit
4a3d66e712
@ -16,9 +16,15 @@ 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)
|
||||
@ -26,22 +32,22 @@ namespace NadekoBot.Modules.Gambling
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<string, string> 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();
|
||||
|
17
src/NadekoBot/Services/IImagesService.cs
Normal file
17
src/NadekoBot/Services/IImagesService.cs
Normal file
@ -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();
|
||||
}
|
||||
}
|
46
src/NadekoBot/Services/Impl/ImagesService.cs
Normal file
46
src/NadekoBot/Services/Impl/ImagesService.cs
Normal file
@ -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<IImagesService> 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!");
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user