add !rank seasons to the rank command

This commit is contained in:
Daan Boerlage 2020-12-30 23:17:00 +01:00
parent 29c0def713
commit 29bb8035fe
Signed by: daan
GPG key ID: FCE070E1E4956606
6 changed files with 58 additions and 18 deletions

View file

@ -30,9 +30,12 @@ namespace Geekbot.Bot.Commands.User.Ranking
} }
[Command("rank", RunMode = RunMode.Async)] [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] [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 try
{ {
@ -59,7 +62,7 @@ namespace Geekbot.Bot.Commands.User.Ranking
Dictionary<HighscoreUserDto, int> highscoreUsers; Dictionary<HighscoreUserDto, int> highscoreUsers;
try try
{ {
highscoreUsers = _highscoreManager.GetHighscoresWithUserData(type, guildId, amount); highscoreUsers = _highscoreManager.GetHighscoresWithUserData(type, guildId, amount, season);
} }
catch (HighscoreListEmptyException) catch (HighscoreListEmptyException)
{ {
@ -80,7 +83,18 @@ namespace Geekbot.Bot.Commands.User.Ranking
if (failedToRetrieveUser) replyBuilder.AppendLine(Localization.Rank.FailedToResolveAllUsernames).AppendLine(); 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; var highscorePlace = 1;
foreach (var (user, value) in highscoreUsers) foreach (var (user, value) in highscoreUsers)

View file

@ -1,11 +1,11 @@
using System; using System;
using System.Globalization;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Timers; using System.Timers;
using Discord.WebSocket; using Discord.WebSocket;
using Geekbot.Core.Database; using Geekbot.Core.Database;
using Geekbot.Core.Database.Models; using Geekbot.Core.Database.Models;
using Geekbot.Core.Extensions; using Geekbot.Core.Extensions;
using Geekbot.Core.Highscores;
using Geekbot.Core.Logger; using Geekbot.Core.Logger;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@ -23,7 +23,7 @@ namespace Geekbot.Bot.Handlers
{ {
_logger = logger; _logger = logger;
_database = database; _database = database;
_season = GetSeason(); _season = SeasonsUtils.GetCurrentSeason();
_seasonsStarted = DateTime.Now.Year == 2021; _seasonsStarted = DateTime.Now.Year == 2021;
var timer = new Timer() var timer = new Timer()
@ -34,7 +34,9 @@ namespace Geekbot.Bot.Handlers
}; };
timer.Elapsed += (sender, args) => timer.Elapsed += (sender, args) =>
{ {
_season = GetSeason(); var current = SeasonsUtils.GetCurrentSeason();
if (current == _season) return;
_season = SeasonsUtils.GetCurrentSeason();
_seasonsStarted = DateTime.Now.Year == 2021; _seasonsStarted = DateTime.Now.Year == 2021;
}; };
} }
@ -108,13 +110,5 @@ namespace Geekbot.Bot.Handlers
await _database.SaveChangesAsync(); 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}";
}
} }
} }

View file

@ -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 var list = type switch
{ {
@ -26,6 +26,7 @@ namespace Geekbot.Core.Highscores
HighscoreTypes.karma => GetKarmaList(guildId, amount), HighscoreTypes.karma => GetKarmaList(guildId, amount),
HighscoreTypes.rolls => GetRollsList(guildId, amount), HighscoreTypes.rolls => GetRollsList(guildId, amount),
HighscoreTypes.cookies => GetCookiesList(guildId, amount), HighscoreTypes.cookies => GetCookiesList(guildId, amount),
HighscoreTypes.seasons => GetMessageSeasonList(guildId, amount, season),
_ => new Dictionary<ulong, int>() _ => new Dictionary<ulong, int>()
}; };
@ -75,6 +76,19 @@ namespace Geekbot.Core.Highscores
.ToDictionary(key => key.UserId.AsUlong(), key => key.MessageCount); .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) public Dictionary<ulong, int> GetKarmaList(ulong guildId, int amount)
{ {
return _database.Karma return _database.Karma

View file

@ -5,6 +5,7 @@
messages, messages,
karma, karma,
rolls, rolls,
cookies cookies,
seasons
} }
} }

View file

@ -4,8 +4,9 @@ namespace Geekbot.Core.Highscores
{ {
public interface IHighscoreManager 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> 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> GetKarmaList(ulong guildId, int amount);
Dictionary<ulong, int> GetRollsList(ulong guildId, int amount); Dictionary<ulong, int> GetRollsList(ulong guildId, int amount);
} }

View 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}";
}
}
}