NadekoBot/NadekoBot.Core/Services/Impl/ImagesService.cs

241 lines
6.2 KiB
C#
Raw Normal View History

2017-11-05 12:28:08 +00:00
using NadekoBot.Extensions;
using Newtonsoft.Json;
using NLog;
using StackExchange.Redis;
using System;
using System.IO;
using System.Linq;
namespace NadekoBot.Core.Services.Impl
{
2017-10-31 08:52:46 +00:00
//todo move everything to redis
2017-11-05 12:28:08 +00:00
public class RedisImagesCache : IImageCache
{
2017-11-05 12:28:08 +00:00
private readonly ConnectionMultiplexer _con;
private readonly IBotCredentials _creds;
private readonly Logger _log;
2017-11-05 12:28:08 +00:00
private IDatabase _db => _con.GetDatabase();
2017-02-25 15:35:09 +00:00
private const string _basePath = "data/images/";
2017-02-25 15:35:09 +00:00
private const string _headsPath = _basePath + "coins/heads.png";
private const string _tailsPath = _basePath + "coins/tails.png";
2017-02-25 15:35:09 +00:00
private const string _currencyImagesPath = _basePath + "currency";
private const string _diceImagesPath = _basePath + "dice";
2017-03-21 12:12:47 +00:00
private const string _slotBackgroundPath = _basePath + "slots/background2.png";
2017-02-25 15:35:09 +00:00
private const string _slotNumbersPath = _basePath + "slots/numbers/";
private const string _slotEmojisPath = _basePath + "slots/emojis/";
2017-02-25 15:35:09 +00:00
private const string _wifeMatrixPath = _basePath + "rategirl/wifematrix.png";
private const string _rategirlDot = _basePath + "rategirl/dot.png";
private const string _xpCardPath = _basePath + "xp/xp.png";
2017-10-15 09:48:29 +00:00
private const string _ripPath = _basePath + "rip/rip.png";
private const string _ripFlowersPath = _basePath + "rip/rose_overlay.png";
2017-11-05 12:28:08 +00:00
public byte[] Heads
{
get
{
return Get<byte[]>("heads");
}
set
{
Set("heads", value);
}
}
2017-11-05 12:28:08 +00:00
public byte[] Tails
{
get
{
return Get<byte[]>("tails");
}
set
{
Set("tails", value);
}
}
2017-11-05 12:28:08 +00:00
public byte[][] Currency
{
get
{
return Get<byte[][]>("currency");
}
set
{
Set("currency", value);
}
}
2017-11-05 12:28:08 +00:00
public byte[][] Dice
{
get
{
return Get<byte[][]>("dice");
}
set
{
Set("dice", value);
}
}
2017-11-05 12:28:08 +00:00
public byte[] SlotBackground
{
get
{
return Get<byte[]>("slot_background");
}
set
{
Set("slot_background", value);
}
}
2017-11-05 12:28:08 +00:00
public byte[][] SlotNumbers
{
get
{
return Get<byte[][]>("slotnumbers");
}
set
{
Set("slotnumbers", value);
}
}
public byte[][] SlotEmojis
{
get
{
return Get<byte[][]>("slotemojis");
}
set
{
Set("slotemojis", value);
}
}
2017-11-05 12:28:08 +00:00
public byte[] WifeMatrix
{
get
{
return Get<byte[]>("wife_matrix");
}
set
{
Set("wife_matrix", value);
}
}
public byte[] RategirlDot
{
get
{
return Get<byte[]>("rategirl_dot");
}
set
{
Set("rategirl_dot", value);
}
}
2017-10-15 09:48:29 +00:00
2017-11-05 12:28:08 +00:00
public byte[] XpCard
{
2017-11-05 12:28:08 +00:00
get
{
return Get<byte[]>("xp_card");
}
set
{
Set("xp_card", value);
}
}
public byte[] Rip
{
get
{
return Get<byte[]>("rip");
}
set
{
Set("rip", value);
}
}
public byte[] FlowerCircle
{
get
{
return Get<byte[]>("flower_circle");
}
set
2017-11-03 12:21:35 +00:00
{
2017-11-05 12:28:08 +00:00
Set("flower_circle", value);
2017-11-03 12:21:35 +00:00
}
}
2017-11-05 12:28:08 +00:00
public RedisImagesCache(ConnectionMultiplexer con, IBotCredentials creds)
{
_con = con;
_creds = creds;
_log = LogManager.GetCurrentClassLogger();
}
public void Reload()
{
try
{
2017-11-05 12:28:08 +00:00
Heads = File.ReadAllBytes(_headsPath);
Tails = File.ReadAllBytes(_tailsPath);
2017-02-25 15:35:09 +00:00
Currency = Directory.GetFiles(_currencyImagesPath)
2017-11-05 12:28:08 +00:00
.Select(x => File.ReadAllBytes(x))
.ToArray();
2017-02-25 15:35:09 +00:00
Dice = Directory.GetFiles(_diceImagesPath)
.OrderBy(x => int.Parse(Path.GetFileNameWithoutExtension(x)))
2017-11-05 12:28:08 +00:00
.Select(x => File.ReadAllBytes(x))
.ToArray();
2017-11-05 12:28:08 +00:00
SlotBackground = File.ReadAllBytes(_slotBackgroundPath);
2017-02-25 15:35:09 +00:00
SlotNumbers = Directory.GetFiles(_slotNumbersPath)
.OrderBy(f => int.Parse(Path.GetFileNameWithoutExtension(f)))
2017-11-05 12:28:08 +00:00
.Select(x => File.ReadAllBytes(x))
.ToArray();
2017-02-25 15:35:09 +00:00
SlotEmojis = Directory.GetFiles(_slotEmojisPath)
2017-02-10 12:03:15 +00:00
.OrderBy(f => int.Parse(Path.GetFileNameWithoutExtension(f)))
2017-11-05 12:28:08 +00:00
.Select(x => File.ReadAllBytes(x))
.ToArray();
2017-11-05 12:28:08 +00:00
WifeMatrix = File.ReadAllBytes(_wifeMatrixPath);
RategirlDot = File.ReadAllBytes(_rategirlDot);
2017-11-05 12:28:08 +00:00
XpCard = File.ReadAllBytes(_xpCardPath);
2017-10-15 09:48:29 +00:00
2017-11-05 12:28:08 +00:00
Rip = File.ReadAllBytes(_ripPath);
FlowerCircle = File.ReadAllBytes(_ripFlowersPath);
}
catch (Exception ex)
{
_log.Error(ex);
throw;
}
2017-05-22 23:59:31 +00:00
}
2017-11-05 12:28:08 +00:00
private T Get<T>(string key) where T : class
{
return JsonConvert.DeserializeObject<T>(_db.StringGet($"{_creds.RedisKey()}_localimg_{key}"));
}
private void Set(string key, object obj)
{
_db.StringSet($"{_creds.RedisKey()}_localimg_{key}", JsonConvert.SerializeObject(obj));
}
}
}