Abstract away redis even more with AlmostRedis.cs

This commit is contained in:
Runebaas 2018-05-26 02:33:45 +02:00
parent f53258e348
commit 35f0a5c8f8
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
10 changed files with 153 additions and 78 deletions

View file

@ -5,6 +5,7 @@ using System.Text;
using System.Threading.Tasks;
using Discord.Commands;
using Geekbot.net.Database;
using Geekbot.net.Lib.AlmostRedis;
using Geekbot.net.Lib.Converters;
using Geekbot.net.Lib.ErrorHandling;
using Geekbot.net.Lib.Extensions;
@ -19,10 +20,10 @@ namespace Geekbot.net.Commands.User.Ranking
private readonly IErrorHandler _errorHandler;
private readonly DatabaseContext _database;
private readonly IUserRepository _userRepository;
private readonly IDatabase _redis;
private readonly IAlmostRedis _redis;
public Rank(DatabaseContext database, IErrorHandler errorHandler, IUserRepository userRepository,
IEmojiConverter emojiConverter, IDatabase redis)
IEmojiConverter emojiConverter, IAlmostRedis redis)
{
_database = database;
_errorHandler = errorHandler;
@ -138,7 +139,7 @@ namespace Geekbot.net.Commands.User.Ranking
private Dictionary<ulong, int> GetMessageList(int amount)
{
return _redis
return _redis.Db
.HashGetAll($"{Context.Guild.Id}:Messages").ToDictionary().Take(amount + 1)
.Where(user => !user.Key.Equals(0))
.ToDictionary(user => ulong.Parse(user.Key), user => int.Parse(user.Value));

View file

@ -4,10 +4,10 @@ using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Geekbot.net.Database;
using Geekbot.net.Lib.AlmostRedis;
using Geekbot.net.Lib.ErrorHandling;
using Geekbot.net.Lib.Extensions;
using Geekbot.net.Lib.Levels;
using StackExchange.Redis;
namespace Geekbot.net.Commands.User
{
@ -15,10 +15,10 @@ namespace Geekbot.net.Commands.User
{
private readonly IErrorHandler _errorHandler;
private readonly ILevelCalc _levelCalc;
private readonly IDatabase _redis;
private readonly IAlmostRedis _redis;
private readonly DatabaseContext _database;
public Stats(IDatabase redis, DatabaseContext database, IErrorHandler errorHandler, ILevelCalc levelCalc)
public Stats(IAlmostRedis redis, DatabaseContext database, IErrorHandler errorHandler, ILevelCalc levelCalc)
{
_redis = redis;
_database = database;
@ -39,8 +39,8 @@ namespace Geekbot.net.Commands.User
var age = Math.Floor((DateTime.Now - createdAt).TotalDays);
var joinedDayAgo = Math.Floor((DateTime.Now - joinedAt).TotalDays);
var messages = (int) _redis.HashGet($"{Context.Guild.Id}:Messages", userInfo.Id.ToString());
var guildMessages = (int) _redis.HashGet($"{Context.Guild.Id}:Messages", 0.ToString());
var messages = (int) _redis.Db.HashGet($"{Context.Guild.Id}:Messages", userInfo.Id.ToString());
var guildMessages = (int) _redis.Db.HashGet($"{Context.Guild.Id}:Messages", 0.ToString());
var level = _levelCalc.GetLevel(messages);
var percent = Math.Round((double) (100 * messages) / guildMessages, 2);