Abstract away cache. 2 implementations: redis and memory

This commit is contained in:
Kwoth
2022-06-23 13:07:45 +00:00
parent 1716c69132
commit 210da263ad
75 changed files with 11525 additions and 1547 deletions

View File

@@ -9,14 +9,14 @@ public partial class Games
[Group]
public partial class TriviaCommands : NadekoModule<GamesService>
{
private readonly IDataCache _cache;
private readonly ILocalDataCache _cache;
private readonly ICurrencyService _cs;
private readonly GamesConfigService _gamesConfig;
private readonly DiscordSocketClient _client;
public TriviaCommands(
DiscordSocketClient client,
IDataCache cache,
ILocalDataCache cache,
ICurrencyService cs,
GamesConfigService gamesConfig)
{

View File

@@ -17,7 +17,7 @@ public class TriviaGame
public bool GameActive { get; private set; }
public bool ShouldStopGame { get; private set; }
private readonly SemaphoreSlim _guessLock = new(1, 1);
private readonly IDataCache _cache;
private readonly ILocalDataCache _cache;
private readonly IBotStrings _strings;
private readonly DiscordSocketClient _client;
private readonly GamesConfig _config;
@@ -35,7 +35,7 @@ public class TriviaGame
IBotStrings strings,
DiscordSocketClient client,
GamesConfig config,
IDataCache cache,
ILocalDataCache cache,
ICurrencyService cs,
IGuild guild,
ITextChannel channel,
@@ -70,7 +70,7 @@ public class TriviaGame
showHowToQuit = !showHowToQuit;
// load question
CurrentQuestion = _questionPool.GetRandomQuestion(OldQuestions, _options.IsPokemon);
CurrentQuestion = await _questionPool.GetRandomQuestionAsync(OldQuestions, _options.IsPokemon);
if (string.IsNullOrWhiteSpace(CurrentQuestion?.Answer)
|| string.IsNullOrWhiteSpace(CurrentQuestion.Question))
{

View File

@@ -4,6 +4,15 @@ using System.Text.RegularExpressions;
// THANKS @ShoMinamimoto for suggestions and coding help
namespace NadekoBot.Modules.Games.Common.Trivia;
public sealed class TriviaQuestionModel
{
public string Category { get; init; }
public string Question { get; init; }
public string ImageUrl { get; init; }
public string AnswerImageUrl { get; init; }
public string Answer { get; init; }
}
public class TriviaQuestion
{
public const int MAX_STRING_LENGTH = 22;
@@ -17,29 +26,30 @@ public class TriviaQuestion
new(22, 3)
};
public string Category { get; set; }
public string Question { get; set; }
public string ImageUrl { get; set; }
public string AnswerImageUrl { get; set; }
public string Answer { get; set; }
public string Category
=> _qModel.Category;
public string Question
=> _qModel.Question;
public string ImageUrl
=> _qModel.ImageUrl;
public string AnswerImageUrl
=> _qModel.AnswerImageUrl ?? ImageUrl;
public string Answer
=> _qModel.Answer;
public string CleanAnswer
=> cleanAnswer ?? (cleanAnswer = Clean(Answer));
private string cleanAnswer;
private readonly TriviaQuestionModel _qModel;
public TriviaQuestion(
string q,
string a,
string c,
string img = null,
string answerImage = null)
public TriviaQuestion(TriviaQuestionModel qModel)
{
Question = q;
Answer = a;
Category = c;
ImageUrl = img;
AnswerImageUrl = answerImage ?? img;
_qModel = qModel;
}
public string GetHint()

View File

@@ -1,45 +1,48 @@
#nullable disable
namespace NadekoBot.Modules.Games.Common.Trivia;
public class TriviaQuestionPool
{
private TriviaQuestion[] Pool
=> _cache.LocalData.TriviaQuestions;
private IReadOnlyDictionary<int, string> Map
=> _cache.LocalData.PokemonMap;
private readonly IDataCache _cache;
private readonly ILocalDataCache _cache;
private readonly int _maxPokemonId;
private readonly NadekoRandom _rng = new();
public TriviaQuestionPool(IDataCache cache)
public TriviaQuestionPool(ILocalDataCache cache)
{
_cache = cache;
_maxPokemonId = 721; //xd
}
public TriviaQuestion GetRandomQuestion(HashSet<TriviaQuestion> exclude, bool isPokemon)
public async Task<TriviaQuestion?> GetRandomQuestionAsync(HashSet<TriviaQuestion> exclude, bool isPokemon)
{
if (Pool.Length == 0)
return null;
if (isPokemon)
{
var pokes = await _cache.GetPokemonMapAsync();
if (pokes is null or { Length: 0 })
return default;
var num = _rng.Next(1, _maxPokemonId + 1);
return new("Who's That Pokémon?",
Map[num].ToTitleCase(),
"Pokemon",
$@"https://nadeko.bot/images/pokemon/shadows/{num}.png",
$@"https://nadeko.bot/images/pokemon/real/{num}.png");
return new(new()
{
Question = "Who's That Pokémon?",
Answer = pokes[num].Name.ToTitleCase(),
Category = "Pokemon",
ImageUrl = $@"https://nadeko.bot/images/pokemon/shadows/{num}.png",
AnswerImageUrl = $@"https://nadeko.bot/images/pokemon/real/{num}.png"
});
}
TriviaQuestion randomQuestion;
while (exclude.Contains(randomQuestion = Pool[_rng.Next(0, Pool.Length)]))
var pool = await _cache.GetTriviaQuestionsAsync();
if(pool is null)
return default;
while (exclude.Contains(randomQuestion = new(pool[_rng.Next(0, pool.Length)])))
{
// if too many questions are excluded, clear the exclusion list and start over
if (exclude.Count > Pool.Length / 10 * 9)
if (exclude.Count > pool.Length / 10 * 9)
{
exclude.Clear();
break;