add !rank seasons to the rank command
This commit is contained in:
parent
29c0def713
commit
29bb8035fe
6 changed files with 58 additions and 18 deletions
|
@ -30,9 +30,12 @@ namespace Geekbot.Bot.Commands.User.Ranking
|
|||
}
|
||||
|
||||
[Command("rank", RunMode = RunMode.Async)]
|
||||
[Summary("get user top 10 in messages or karma")]
|
||||
[Summary("Get the highscore for various stats like message count, karma, correctly guessed roles, etc...")]
|
||||
[DisableInDirectMessage]
|
||||
public async Task RankCmd([Summary("type")] string typeUnformated = "messages", [Summary("amount")] int amount = 10)
|
||||
public async Task RankCmd(
|
||||
[Summary("type")] string typeUnformated = "messages",
|
||||
[Summary("amount")] int amount = 10,
|
||||
[Summary("season")] string season = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
@ -59,7 +62,7 @@ namespace Geekbot.Bot.Commands.User.Ranking
|
|||
Dictionary<HighscoreUserDto, int> highscoreUsers;
|
||||
try
|
||||
{
|
||||
highscoreUsers = _highscoreManager.GetHighscoresWithUserData(type, guildId, amount);
|
||||
highscoreUsers = _highscoreManager.GetHighscoresWithUserData(type, guildId, amount, season);
|
||||
}
|
||||
catch (HighscoreListEmptyException)
|
||||
{
|
||||
|
@ -80,7 +83,18 @@ namespace Geekbot.Bot.Commands.User.Ranking
|
|||
|
||||
if (failedToRetrieveUser) replyBuilder.AppendLine(Localization.Rank.FailedToResolveAllUsernames).AppendLine();
|
||||
|
||||
replyBuilder.AppendLine(string.Format(Localization.Rank.HighscoresFor, type.ToString().CapitalizeFirst(), Context.Guild.Name));
|
||||
if (type == HighscoreTypes.seasons)
|
||||
{
|
||||
if (string.IsNullOrEmpty(season))
|
||||
{
|
||||
season = SeasonsUtils.GetCurrentSeason();
|
||||
}
|
||||
replyBuilder.AppendLine(string.Format(Localization.Rank.HighscoresFor, $"{type.ToString().CapitalizeFirst()} ({season})", Context.Guild.Name));
|
||||
}
|
||||
else
|
||||
{
|
||||
replyBuilder.AppendLine(string.Format(Localization.Rank.HighscoresFor, type.ToString().CapitalizeFirst(), Context.Guild.Name));
|
||||
}
|
||||
|
||||
var highscorePlace = 1;
|
||||
foreach (var (user, value) in highscoreUsers)
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
using System;
|
||||
using System.Globalization;
|
||||
using System.Threading.Tasks;
|
||||
using System.Timers;
|
||||
using Discord.WebSocket;
|
||||
using Geekbot.Core.Database;
|
||||
using Geekbot.Core.Database.Models;
|
||||
using Geekbot.Core.Extensions;
|
||||
using Geekbot.Core.Highscores;
|
||||
using Geekbot.Core.Logger;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
|
@ -23,7 +23,7 @@ namespace Geekbot.Bot.Handlers
|
|||
{
|
||||
_logger = logger;
|
||||
_database = database;
|
||||
_season = GetSeason();
|
||||
_season = SeasonsUtils.GetCurrentSeason();
|
||||
_seasonsStarted = DateTime.Now.Year == 2021;
|
||||
|
||||
var timer = new Timer()
|
||||
|
@ -34,7 +34,9 @@ namespace Geekbot.Bot.Handlers
|
|||
};
|
||||
timer.Elapsed += (sender, args) =>
|
||||
{
|
||||
_season = GetSeason();
|
||||
var current = SeasonsUtils.GetCurrentSeason();
|
||||
if (current == _season) return;
|
||||
_season = SeasonsUtils.GetCurrentSeason();
|
||||
_seasonsStarted = DateTime.Now.Year == 2021;
|
||||
};
|
||||
}
|
||||
|
@ -108,13 +110,5 @@ namespace Geekbot.Bot.Handlers
|
|||
await _database.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetSeason()
|
||||
{
|
||||
var now = DateTime.Now;
|
||||
var year = (now.Year - 2000).ToString(CultureInfo.InvariantCulture);
|
||||
var quarter = Math.Ceiling(now.Month / 3.0).ToString(CultureInfo.InvariantCulture);
|
||||
return $"{year}Q{quarter}";
|
||||
}
|
||||
}
|
||||
}
|
|
@ -18,7 +18,7 @@ namespace Geekbot.Core.Highscores
|
|||
|
||||
}
|
||||
|
||||
public Dictionary<HighscoreUserDto, int> GetHighscoresWithUserData(HighscoreTypes type, ulong guildId, int amount)
|
||||
public Dictionary<HighscoreUserDto, int> GetHighscoresWithUserData(HighscoreTypes type, ulong guildId, int amount, string season = null)
|
||||
{
|
||||
var list = type switch
|
||||
{
|
||||
|
@ -26,6 +26,7 @@ namespace Geekbot.Core.Highscores
|
|||
HighscoreTypes.karma => GetKarmaList(guildId, amount),
|
||||
HighscoreTypes.rolls => GetRollsList(guildId, amount),
|
||||
HighscoreTypes.cookies => GetCookiesList(guildId, amount),
|
||||
HighscoreTypes.seasons => GetMessageSeasonList(guildId, amount, season),
|
||||
_ => new Dictionary<ulong, int>()
|
||||
};
|
||||
|
||||
|
@ -75,6 +76,19 @@ namespace Geekbot.Core.Highscores
|
|||
.ToDictionary(key => key.UserId.AsUlong(), key => key.MessageCount);
|
||||
}
|
||||
|
||||
public Dictionary<ulong, int> GetMessageSeasonList(ulong guildId, int amount, string season)
|
||||
{
|
||||
if (string.IsNullOrEmpty(season))
|
||||
{
|
||||
season = SeasonsUtils.GetCurrentSeason();
|
||||
}
|
||||
return _database.MessagesSeasons
|
||||
.Where(k => k.GuildId.Equals(guildId.AsLong()) && k.Season.Equals(season))
|
||||
.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
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
messages,
|
||||
karma,
|
||||
rolls,
|
||||
cookies
|
||||
cookies,
|
||||
seasons
|
||||
}
|
||||
}
|
|
@ -4,8 +4,9 @@ namespace Geekbot.Core.Highscores
|
|||
{
|
||||
public interface IHighscoreManager
|
||||
{
|
||||
Dictionary<HighscoreUserDto, int> GetHighscoresWithUserData(HighscoreTypes type, ulong guildId, int amount);
|
||||
Dictionary<HighscoreUserDto, int> GetHighscoresWithUserData(HighscoreTypes type, ulong guildId, int amount, string season = null);
|
||||
Dictionary<ulong, int> GetMessageList(ulong guildId, int amount);
|
||||
Dictionary<ulong, int> GetMessageSeasonList(ulong guildId, int amount, string season);
|
||||
Dictionary<ulong, int> GetKarmaList(ulong guildId, int amount);
|
||||
Dictionary<ulong, int> GetRollsList(ulong guildId, int amount);
|
||||
}
|
||||
|
|
16
src/Core/Highscores/SeasonsUtils.cs
Normal file
16
src/Core/Highscores/SeasonsUtils.cs
Normal file
|
@ -0,0 +1,16 @@
|
|||
using System;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Geekbot.Core.Highscores
|
||||
{
|
||||
public class SeasonsUtils
|
||||
{
|
||||
public static string GetCurrentSeason()
|
||||
{
|
||||
var now = DateTime.Now;
|
||||
var year = (now.Year - 2000).ToString(CultureInfo.InvariantCulture);
|
||||
var quarter = Math.Ceiling(now.Month / 3.0).ToString(CultureInfo.InvariantCulture);
|
||||
return $"{year}Q{quarter}";
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue