Abstract away cache. 2 implementations: redis and memory

This commit is contained in:
Kwoth
2022-06-23 13:07:45 +00:00
parent 1716c69132
commit 210da263ad
75 changed files with 11525 additions and 1547 deletions

View File

@@ -19,8 +19,8 @@ public partial class Gambling
private static readonly char[] _fateRolls = { '-', ' ', '+' };
private readonly IImageCache _images;
public DiceRollCommands(IDataCache data)
=> _images = data.LocalImages;
public DiceRollCommands(ImageCache images)
=> _images = images;
[Cmd]
public async partial Task Roll()
@@ -31,10 +31,10 @@ public partial class Gambling
var num1 = gen / 10;
var num2 = gen % 10;
using var img1 = GetDice(num1);
using var img2 = GetDice(num2);
using var img1 = await GetDiceAsync(num1);
using var img2 = await GetDiceAsync(num2);
using var img = new[] { img1, img2 }.Merge(out var format);
await using var ms = img.ToStream(format);
await using var ms = await img.ToStreamAsync(format);
await ctx.Channel.SendFileAsync(ms,
$"dice.{format.FileExtensions.First()}",
Format.Bold(ctx.User.ToString()) + " " + GetText(strs.dice_rolled(Format.Code(gen.ToString()))));
@@ -96,7 +96,7 @@ public partial class Gambling
else
toInsert = dice.Count;
dice.Insert(toInsert, GetDice(randomNumber));
dice.Insert(toInsert, await GetDiceAsync(randomNumber));
values.Insert(toInsert, randomNumber);
}
@@ -195,20 +195,19 @@ public partial class Gambling
await ReplyConfirmLocalizedAsync(strs.dice_rolled(Format.Bold(rolled.ToString())));
}
private Image<Rgba32> GetDice(int num)
private async Task<Image<Rgba32>> GetDiceAsync(int num)
{
if (num is < 0 or > 10)
throw new ArgumentOutOfRangeException(nameof(num));
if (num == 10)
{
var images = _images.Dice;
using var imgOne = Image.Load(images[1]);
using var imgZero = Image.Load(images[0]);
using var imgOne = Image.Load(await _images.GetDiceAsync(1));
using var imgZero = Image.Load(await _images.GetDiceAsync(0));
return new[] { imgOne, imgZero }.Merge();
}
return Image.Load(_images.Dice[num]);
return Image.Load(await _images.GetDiceAsync(num));
}
}
}