Refactor !rank, add HighscoreManager and add /v1/highscore to the api
This commit is contained in:
parent
a5c70859a4
commit
99245b9ead
15 changed files with 261 additions and 109 deletions
13
Geekbot.net/Lib/Highscores/HighscoreListEmptyException.cs
Normal file
13
Geekbot.net/Lib/Highscores/HighscoreListEmptyException.cs
Normal file
|
@ -0,0 +1,13 @@
|
|||
using System;
|
||||
|
||||
namespace Geekbot.net.Lib.Highscores
|
||||
{
|
||||
public class HighscoreListEmptyException : Exception
|
||||
{
|
||||
public HighscoreListEmptyException() {}
|
||||
|
||||
public HighscoreListEmptyException(string message) : base(message) {}
|
||||
|
||||
public HighscoreListEmptyException(string message, Exception inner) : base(message, inner) {}
|
||||
}
|
||||
}
|
106
Geekbot.net/Lib/Highscores/HighscoreManager.cs
Normal file
106
Geekbot.net/Lib/Highscores/HighscoreManager.cs
Normal file
|
@ -0,0 +1,106 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Geekbot.net.Database;
|
||||
using Geekbot.net.Lib.Extensions;
|
||||
using Geekbot.net.Lib.Logger;
|
||||
using Geekbot.net.Lib.UserRepository;
|
||||
|
||||
namespace Geekbot.net.Lib.Highscores
|
||||
{
|
||||
public class HighscoreManager : IHighscoreManager
|
||||
{
|
||||
private readonly DatabaseContext _database;
|
||||
private readonly IUserRepository _userRepository;
|
||||
|
||||
public HighscoreManager(DatabaseContext databaseContext, IUserRepository userRepository)
|
||||
{
|
||||
_database = databaseContext;
|
||||
_userRepository = userRepository;
|
||||
|
||||
}
|
||||
|
||||
public Dictionary<HighscoreUserDto, int> GetHighscoresWithUserData(HighscoreTypes type, ulong guildId, int amount)
|
||||
{
|
||||
Dictionary<ulong, int> list;
|
||||
switch (type)
|
||||
{
|
||||
case HighscoreTypes.messages:
|
||||
list = GetMessageList(guildId, amount);
|
||||
break;
|
||||
case HighscoreTypes.karma:
|
||||
list = GetKarmaList(guildId, amount);
|
||||
break;
|
||||
case HighscoreTypes.rolls:
|
||||
list = GetRollsList(guildId, amount);
|
||||
break;
|
||||
default:
|
||||
list = new Dictionary<ulong, int>();
|
||||
break;
|
||||
}
|
||||
|
||||
if (!list.Any())
|
||||
{
|
||||
throw new HighscoreListEmptyException($"No {type} found for guild {guildId}");
|
||||
}
|
||||
|
||||
var highscoreUsers = new Dictionary<HighscoreUserDto, int>();
|
||||
foreach (var user in list)
|
||||
{
|
||||
try
|
||||
{
|
||||
var guildUser = _userRepository.Get(user.Key);
|
||||
if (guildUser?.Username != null)
|
||||
{
|
||||
highscoreUsers.Add(new HighscoreUserDto
|
||||
{
|
||||
Username = guildUser.Username,
|
||||
Discriminator = guildUser.Discriminator,
|
||||
Avatar = guildUser.AvatarUrl
|
||||
}, user.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
highscoreUsers.Add(new HighscoreUserDto
|
||||
{
|
||||
Id = user.Key.ToString()
|
||||
}, user.Value);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
return highscoreUsers;
|
||||
}
|
||||
|
||||
public Dictionary<ulong, int> GetMessageList(ulong guildId, int amount)
|
||||
{
|
||||
return _database.Messages
|
||||
.Where(k => k.GuildId.Equals(guildId.AsLong()))
|
||||
.OrderByDescending(o => o.MessageCount)
|
||||
.Take(amount)
|
||||
.ToDictionary(key => key.UserId.AsUlong(), key => key.MessageCount);
|
||||
}
|
||||
|
||||
public Dictionary<ulong, int> GetKarmaList(ulong guildId, int amount)
|
||||
{
|
||||
return _database.Karma
|
||||
.Where(k => k.GuildId.Equals(guildId.AsLong()))
|
||||
.OrderByDescending(o => o.Karma)
|
||||
.Take(amount)
|
||||
.ToDictionary(key => key.UserId.AsUlong(), key => key.Karma);
|
||||
}
|
||||
|
||||
public Dictionary<ulong, int> GetRollsList(ulong guildId, int amount)
|
||||
{
|
||||
return _database.Rolls
|
||||
.Where(k => k.GuildId.Equals(guildId.AsLong()))
|
||||
.OrderByDescending(o => o.Rolls)
|
||||
.Take(amount)
|
||||
.ToDictionary(key => key.UserId.AsUlong(), key => key.Rolls);
|
||||
}
|
||||
}
|
||||
}
|
9
Geekbot.net/Lib/Highscores/HighscoreTypes.cs
Normal file
9
Geekbot.net/Lib/Highscores/HighscoreTypes.cs
Normal file
|
@ -0,0 +1,9 @@
|
|||
namespace Geekbot.net.Lib.Highscores
|
||||
{
|
||||
public enum HighscoreTypes
|
||||
{
|
||||
messages,
|
||||
karma,
|
||||
rolls
|
||||
}
|
||||
}
|
10
Geekbot.net/Lib/Highscores/HighscoreUserDto.cs
Normal file
10
Geekbot.net/Lib/Highscores/HighscoreUserDto.cs
Normal file
|
@ -0,0 +1,10 @@
|
|||
namespace Geekbot.net.Lib.Highscores
|
||||
{
|
||||
public class HighscoreUserDto
|
||||
{
|
||||
public string Username { get; set; }
|
||||
public string Avatar { get; set; }
|
||||
public string Discriminator { get; set; }
|
||||
public string Id { get; set; }
|
||||
}
|
||||
}
|
12
Geekbot.net/Lib/Highscores/IHighscoreManager.cs
Normal file
12
Geekbot.net/Lib/Highscores/IHighscoreManager.cs
Normal file
|
@ -0,0 +1,12 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace Geekbot.net.Lib.Highscores
|
||||
{
|
||||
public interface IHighscoreManager
|
||||
{
|
||||
Dictionary<HighscoreUserDto, int> GetHighscoresWithUserData(HighscoreTypes type, ulong guildId, int amount);
|
||||
Dictionary<ulong, int> GetMessageList(ulong guildId, int amount);
|
||||
Dictionary<ulong, int> GetKarmaList(ulong guildId, int amount);
|
||||
Dictionary<ulong, int> GetRollsList(ulong guildId, int amount);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue