local image caching to redis done?

This commit is contained in:
Master Kwoth
2017-11-05 13:28:08 +01:00
parent 4b7b44f0d4
commit 607decfbcc
17 changed files with 267 additions and 121 deletions

View File

@ -29,19 +29,18 @@ namespace NadekoBot.Modules.Administration
private static readonly object _locker = new object();
private readonly DiscordSocketClient _client;
private readonly IImagesService _images;
private readonly IImageCache _images;
private readonly IBotConfigProvider _bc;
private readonly NadekoBot _bot;
private readonly IBotCredentials _creds;
private readonly IDataCache _cache;
public SelfCommands(DbService db, NadekoBot bot, DiscordSocketClient client,
IImagesService images, IBotConfigProvider bc,
IBotCredentials creds, IDataCache cache)
IBotConfigProvider bc, IBotCredentials creds, IDataCache cache)
{
_db = db;
_client = client;
_images = images;
_images = cache.LocalImages;
_bc = bc;
_bot = bot;
_creds = creds;

View File

@ -30,11 +30,11 @@ namespace NadekoBot.Modules.Administration.Services
private readonly IBotCredentials _creds;
private ImmutableArray<AsyncLazy<IDMChannel>> ownerChannels = new ImmutableArray<AsyncLazy<IDMChannel>>();
private readonly IBotConfigProvider _bc;
private readonly IImagesService _imgs;
private readonly IImageCache _imgs;
public SelfService(DiscordSocketClient client, NadekoBot bot, CommandHandler cmdHandler, DbService db,
IBotConfigProvider bc, ILocalization localization, NadekoStrings strings, IBotCredentials creds,
IDataCache cache, IImagesService imgs)
IDataCache cache)
{
_redis = cache.Redis;
_bot = bot;
@ -46,7 +46,7 @@ namespace NadekoBot.Modules.Administration.Services
_client = client;
_creds = creds;
_bc = bc;
_imgs = imgs;
_imgs = cache.LocalImages;
var sub = _redis.GetSubscriber();
sub.Subscribe(_creds.RedisKey() + "_reload_images",

View File

@ -24,11 +24,11 @@ namespace NadekoBot.Modules.Gambling
private static readonly Regex fudgeRegex = new Regex(@"^(?<n1>\d+)d(?:F|f)$", RegexOptions.Compiled);
private static readonly char[] _fateRolls = { '-', ' ', '+' };
private readonly IImagesService _images;
private readonly IImageCache _images;
public DriceRollCommands(IImagesService images)
public DriceRollCommands(IDataCache data)
{
_images = images;
_images = data.LocalImages;
}
@ -141,10 +141,8 @@ namespace NadekoBot.Modules.Gambling
private async Task InternallDndRoll(string arg, bool ordered)
{
Match match;
int n1;
int n2;
if ((match = fudgeRegex.Match(arg)).Length != 0 &&
int.TryParse(match.Groups["n1"].ToString(), out n1) &&
int.TryParse(match.Groups["n1"].ToString(), out int n1) &&
n1 > 0 && n1 < 500)
{
var rng = new NadekoRandom();
@ -164,13 +162,14 @@ namespace NadekoBot.Modules.Gambling
{
var rng = new NadekoRandom();
if (int.TryParse(match.Groups["n1"].ToString(), out n1) &&
int.TryParse(match.Groups["n2"].ToString(), out n2) &&
int.TryParse(match.Groups["n2"].ToString(), out int n2) &&
n1 <= 50 && n2 <= 100000 && n1 > 0 && n2 > 0)
{
var add = 0;
var sub = 0;
int.TryParse(match.Groups["add"].Value, out add);
int.TryParse(match.Groups["sub"].Value, out sub);
if (!int.TryParse(match.Groups["add"].Value, out int add) ||
!int.TryParse(match.Groups["sub"].Value, out int sub))
{
return;
}
var arr = new int[n1];
for (int i = 0; i < n1; i++)
@ -179,7 +178,7 @@ namespace NadekoBot.Modules.Gambling
}
var sum = arr.Sum();
var embed = new EmbedBuilder().WithOkColor().WithDescription(Context.User.Mention + " " +GetText("dice_rolled_num", n1) + $"`1 - {n2}`")
var embed = new EmbedBuilder().WithOkColor().WithDescription(Context.User.Mention + " " + GetText("dice_rolled_num", n1) + $"`1 - {n2}`")
.AddField(efb => efb.WithName(Format.Bold("Rolls"))
.WithValue(string.Join(" ", (ordered ? arr.OrderBy(x => x).AsEnumerable() : arr).Select(x => Format.Code(x.ToString())))))
.AddField(efb => efb.WithName(Format.Bold("Sum"))

View File

@ -17,15 +17,15 @@ namespace NadekoBot.Modules.Gambling
[Group]
public class FlipCoinCommands : NadekoSubmodule
{
private readonly IImagesService _images;
private readonly IImageCache _images;
private readonly IBotConfigProvider _bc;
private readonly CurrencyService _cs;
private static readonly NadekoRandom rng = new NadekoRandom();
public FlipCoinCommands(IImagesService images, CurrencyService cs, IBotConfigProvider bc)
public FlipCoinCommands(IDataCache data, CurrencyService cs, IBotConfigProvider bc)
{
_images = images;
_images = data.LocalImages;
_bc = bc;
_cs = cs;
}

View File

@ -32,12 +32,12 @@ namespace NadekoBot.Modules.Gambling
//https://lh6.googleusercontent.com/-i1hjAJy_kN4/UswKxmhrbPI/AAAAAAAAB1U/82wq_4ZZc-Y/DE6B0895-6FC1-48BE-AC4F-14D1B91AB75B.jpg
//thanks to judge for helping me with this
private readonly IImagesService _images;
private readonly IImageCache _images;
private readonly CurrencyService _cs;
public SlotCommands(IImagesService images, IBotConfigProvider bc, CurrencyService cs)
public SlotCommands(IDataCache data, IBotConfigProvider bc, CurrencyService cs)
{
_images = images;
_images = data.LocalImages;
_bc = bc;
_cs = cs;
}

View File

@ -14,6 +14,7 @@ namespace NadekoBot.Modules.Games.Common
public class GirlRating
{
private static readonly Logger _log = LogManager.GetCurrentClassLogger();
private readonly IImageCache _images;
public double Crazy { get; }
public double Hot { get; }
@ -21,8 +22,9 @@ namespace NadekoBot.Modules.Games.Common
public string Advice { get; }
public AsyncLazy<string> Url { get; }
public GirlRating(IImagesService _images, double crazy, double hot, int roll, string advice)
public GirlRating(IImageCache images, double crazy, double hot, int roll, string advice)
{
_images = images;
Crazy = crazy;
Hot = hot;
Roll = roll;
@ -45,7 +47,7 @@ namespace NadekoBot.Modules.Games.Common
using (var pointMs = new MemoryStream(_images.RategirlDot.ToArray(), false))
using (var pointImg = Image.Load(pointMs))
{
img.DrawImage(pointImg, 100, default(Size), new Point(pointx - 10, pointy - 10));
img.DrawImage(pointImg, 100, default, new Point(pointx - 10, pointy - 10));
}
string url;

View File

@ -19,11 +19,11 @@ namespace NadekoBot.Modules.Games
//todo update docs
public partial class Games : NadekoTopLevelModule<GamesService>
{
private readonly IImagesService _images;
private readonly IImageCache _images;
public Games(IImagesService images)
public Games(IDataCache data)
{
_images = images;
_images = data.LocalImages;
}
//#if GLOBAL_NADEKO
// [NadekoCommand, Usage, Description, Aliases]

View File

@ -9,6 +9,7 @@ using System.Linq;
using System.Threading.Tasks;
using NadekoBot.Common.Attributes;
using NadekoBot.Modules.Games.Services;
using System;
namespace NadekoBot.Modules.Games
{
@ -88,12 +89,18 @@ namespace NadekoBot.Modules.Games
else
msgToSend += " " + GetText("pick_sn", Prefix);
using (var toSend = imgData.Data.ToStream())
using (var toSend = imgData.ToStream())
{
msg = await Context.Channel.SendFileAsync(toSend, imgData.Name, msgToSend).ConfigureAwait(false);
msg = await Context.Channel.SendFileAsync(toSend, "plant.png", msgToSend, options: new RequestOptions()
{
RetryMode = RetryMode.AlwaysRetry
}).ConfigureAwait(false);
}
}
catch { }
catch (Exception ex)
{
_log.Warn(ex);
}
var msgs = new IUserMessage[amount];
msgs[0] = msg;

View File

@ -35,7 +35,7 @@ namespace NadekoBot.Modules.Games.Services
private readonly Timer _t;
private readonly CommandHandler _cmd;
private readonly NadekoStrings _strings;
private readonly IImagesService _images;
private readonly IImageCache _images;
private readonly Logger _log;
private readonly NadekoRandom _rng;
private readonly CurrencyService _cs;
@ -57,13 +57,13 @@ namespace NadekoBot.Modules.Games.Services
public ConcurrentDictionary<ulong, Nunchi> NunchiGames { get; } = new ConcurrentDictionary<ulong, Common.Nunchi.Nunchi>();
public GamesService(CommandHandler cmd, IBotConfigProvider bc, NadekoBot bot,
NadekoStrings strings, IImagesService images, CommandHandler cmdHandler,
NadekoStrings strings, IDataCache data, CommandHandler cmdHandler,
CurrencyService cs)
{
_bc = bc;
_cmd = cmd;
_strings = strings;
_images = images;
_images = data.LocalImages;
_cmdHandler = cmdHandler;
_log = LogManager.GetCurrentClassLogger();
_rng = new NadekoRandom();
@ -144,10 +144,11 @@ namespace NadekoBot.Modules.Games.Services
private ConcurrentDictionary<ulong, object> _locks { get; } = new ConcurrentDictionary<ulong, object>();
public ConcurrentHashSet<ulong> HalloweenAwardedUsers { get; } = new ConcurrentHashSet<ulong>();
public (string Name, ImmutableArray<byte> Data) GetRandomCurrencyImage()
public byte[] GetRandomCurrencyImage()
{
var rng = new NadekoRandom();
return _images.Currency[rng.Next(0, _images.Currency.Length)];
var cur = _images.Currency;
return cur[rng.Next(0, cur.Length)];
}
private string GetText(ITextChannel ch, string key, params object[] rep)
@ -195,11 +196,11 @@ namespace NadekoBot.Modules.Games.Services
: GetText(channel, "curgen_pl", dropAmount, _bc.BotConfig.CurrencySign)
+ " " + GetText(channel, "pick_pl", prefix);
var file = GetRandomCurrencyImage();
using (var fileStream = file.Data.ToStream())
using (var fileStream = file.ToStream())
{
var sent = await channel.SendFileAsync(
fileStream,
file.Name,
"drop.png",
toSend).ConfigureAwait(false);
msgs[0] = sent;

View File

@ -34,7 +34,7 @@ namespace NadekoBot.Modules.Searches.Services
private readonly IGoogleApiService _google;
private readonly DbService _db;
private readonly Logger _log;
private readonly IImagesService _imgs;
private readonly IImageCache _imgs;
private readonly IDataCache _cache;
private readonly FontProvider _fonts;
private readonly HttpClient http;
@ -60,7 +60,7 @@ namespace NadekoBot.Modules.Searches.Services
private readonly ConcurrentDictionary<ulong, HashSet<string>> _blacklistedTags = new ConcurrentDictionary<ulong, HashSet<string>>();
public SearchesService(DiscordSocketClient client, IGoogleApiService google,
DbService db, NadekoBot bot, IImagesService imgs, IDataCache cache,
DbService db, NadekoBot bot, IDataCache cache,
FontProvider fonts)
{
Http = new HttpClient();
@ -69,7 +69,7 @@ namespace NadekoBot.Modules.Searches.Services
_google = google;
_db = db;
_log = LogManager.GetCurrentClassLogger();
_imgs = imgs;
_imgs = cache.LocalImages;
_cache = cache;
_fonts = fonts;
http = new HttpClient();

View File

@ -19,8 +19,6 @@ using SixLabors.Fonts;
using System.IO;
using SixLabors.Primitives;
using System.Net.Http;
using SixLabors.Shapes;
using System.Numerics;
using ImageSharp.Drawing.Pens;
using ImageSharp.Drawing.Brushes;
@ -33,7 +31,7 @@ namespace NadekoBot.Modules.Xp.Services
private readonly DbService _db;
private readonly CommandHandler _cmd;
private readonly IBotConfigProvider _bc;
private readonly IImagesService _images;
private readonly IImageCache _images;
private readonly Logger _log;
private readonly NadekoStrings _strings;
private readonly IDataCache _cache;
@ -61,14 +59,13 @@ namespace NadekoBot.Modules.Xp.Services
private readonly HttpClient http = new HttpClient();
public XpService(CommandHandler cmd, IBotConfigProvider bc,
NadekoBot bot, IImagesService images,
DbService db, NadekoStrings strings, IDataCache cache,
NadekoBot bot, DbService db, NadekoStrings strings, IDataCache cache,
FontProvider fonts)
{
_db = db;
_cmd = cmd;
_bc = bc;
_images = images;
_images = cache.LocalImages;
_log = LogManager.GetCurrentClassLogger();
_strings = strings;
_cache = cache;