trivia questions and pokemon data will be stored in redis, instead of per-shard
This commit is contained in:
		@@ -19,6 +19,7 @@ namespace NadekoBot.Modules.Games.Common.Trivia
 | 
			
		||||
    {
 | 
			
		||||
        private readonly SemaphoreSlim _guessLock = new SemaphoreSlim(1, 1);
 | 
			
		||||
        private readonly Logger _log;
 | 
			
		||||
        private readonly IDataCache _cache;
 | 
			
		||||
        private readonly NadekoStrings _strings;
 | 
			
		||||
        private readonly DiscordSocketClient _client;
 | 
			
		||||
        private readonly IBotConfigProvider _bc;
 | 
			
		||||
@@ -43,11 +44,15 @@ namespace NadekoBot.Modules.Games.Common.Trivia
 | 
			
		||||
 | 
			
		||||
        public int WinRequirement { get; }
 | 
			
		||||
 | 
			
		||||
        private readonly TriviaQuestionPool _questionPool;
 | 
			
		||||
 | 
			
		||||
        public TriviaGame(NadekoStrings strings, DiscordSocketClient client, IBotConfigProvider bc,
 | 
			
		||||
            CurrencyService cs, IGuild guild, ITextChannel channel,
 | 
			
		||||
            IDataCache cache, CurrencyService cs, IGuild guild, ITextChannel channel,
 | 
			
		||||
            bool showHints, int winReq, bool isPokemon)
 | 
			
		||||
        {
 | 
			
		||||
            _log = LogManager.GetCurrentClassLogger();
 | 
			
		||||
            _cache = cache;
 | 
			
		||||
            _questionPool = new TriviaQuestionPool(_cache);
 | 
			
		||||
            _strings = strings;
 | 
			
		||||
            _client = client;
 | 
			
		||||
            _bc = bc;
 | 
			
		||||
@@ -74,7 +79,7 @@ namespace NadekoBot.Modules.Games.Common.Trivia
 | 
			
		||||
                _triviaCancelSource = new CancellationTokenSource();
 | 
			
		||||
 | 
			
		||||
                // load question
 | 
			
		||||
                CurrentQuestion = TriviaQuestionPool.Instance.GetRandomQuestion(OldQuestions, IsPokemon);
 | 
			
		||||
                CurrentQuestion = _questionPool.GetRandomQuestion(OldQuestions, IsPokemon);
 | 
			
		||||
                if (string.IsNullOrWhiteSpace(CurrentQuestion?.Answer) || string.IsNullOrWhiteSpace(CurrentQuestion.Question))
 | 
			
		||||
                {
 | 
			
		||||
                    await Channel.SendErrorAsync(GetText("trivia_game"), GetText("failed_loading_question")).ConfigureAwait(false);
 | 
			
		||||
 
 | 
			
		||||
@@ -1,62 +1,43 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Collections.Immutable;
 | 
			
		||||
using System.IO;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using NadekoBot.Common;
 | 
			
		||||
using NadekoBot.Extensions;
 | 
			
		||||
using Newtonsoft.Json;
 | 
			
		||||
using NadekoBot.Core.Services;
 | 
			
		||||
 | 
			
		||||
namespace NadekoBot.Modules.Games.Common.Trivia
 | 
			
		||||
{
 | 
			
		||||
    public class TriviaQuestionPool
 | 
			
		||||
    {
 | 
			
		||||
        public class PokemonNameId
 | 
			
		||||
        {
 | 
			
		||||
            public int Id { get; set; }
 | 
			
		||||
            public string Name { get; set; }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private static TriviaQuestionPool _instance;
 | 
			
		||||
        public static TriviaQuestionPool Instance { get; } = _instance ?? (_instance = new TriviaQuestionPool());
 | 
			
		||||
 | 
			
		||||
        private const string questionsFile = "data/trivia_questions.json";
 | 
			
		||||
        private const string pokemonMapPath = "data/pokemon/name-id_map4.json";
 | 
			
		||||
        private readonly IDataCache _cache;
 | 
			
		||||
        private readonly int maxPokemonId;
 | 
			
		||||
 | 
			
		||||
        private Random rng { get; } = new NadekoRandom();
 | 
			
		||||
        
 | 
			
		||||
        private TriviaQuestion[] pool { get; }
 | 
			
		||||
        private ImmutableDictionary<int, string> map { get; }
 | 
			
		||||
        private readonly NadekoRandom _rng = new NadekoRandom();
 | 
			
		||||
 | 
			
		||||
        static TriviaQuestionPool() { }
 | 
			
		||||
        private TriviaQuestion[] Pool => _cache.LocalData.TriviaQuestions;
 | 
			
		||||
        private IReadOnlyDictionary<int, string> Map => _cache.LocalData.PokemonMap;
 | 
			
		||||
 | 
			
		||||
        private TriviaQuestionPool()
 | 
			
		||||
        public TriviaQuestionPool(IDataCache cache)
 | 
			
		||||
        {
 | 
			
		||||
            pool = JsonConvert.DeserializeObject<TriviaQuestion[]>(File.ReadAllText(questionsFile));
 | 
			
		||||
            map = JsonConvert.DeserializeObject<PokemonNameId[]>(File.ReadAllText(pokemonMapPath))
 | 
			
		||||
                    .ToDictionary(x => x.Id, x => x.Name)
 | 
			
		||||
                    .ToImmutableDictionary();
 | 
			
		||||
 | 
			
		||||
            _cache = cache;
 | 
			
		||||
            maxPokemonId = 721; //xd
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public TriviaQuestion GetRandomQuestion(HashSet<TriviaQuestion> exclude, bool isPokemon)
 | 
			
		||||
        {
 | 
			
		||||
            if (pool.Length == 0)
 | 
			
		||||
            if (Pool.Length == 0)
 | 
			
		||||
                return null;
 | 
			
		||||
 | 
			
		||||
            if (isPokemon)
 | 
			
		||||
            {
 | 
			
		||||
                var num = rng.Next(1, maxPokemonId + 1);
 | 
			
		||||
                var num = _rng.Next(1, maxPokemonId + 1);
 | 
			
		||||
                return new TriviaQuestion("Who's That Pokémon?", 
 | 
			
		||||
                    map[num].ToTitleCase(),
 | 
			
		||||
                    Map[num].ToTitleCase(),
 | 
			
		||||
                    "Pokemon",
 | 
			
		||||
                    $@"http://nadekobot.me/images/pokemon/shadows/{num}.png",
 | 
			
		||||
                    $@"http://nadekobot.me/images/pokemon/real/{num}.png");
 | 
			
		||||
            }
 | 
			
		||||
            TriviaQuestion randomQuestion;
 | 
			
		||||
            while (exclude.Contains(randomQuestion = pool[rng.Next(0, pool.Length)])) ;
 | 
			
		||||
            while (exclude.Contains(randomQuestion = Pool[_rng.Next(0, Pool.Length)])) ;
 | 
			
		||||
 | 
			
		||||
            return randomQuestion;
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
@@ -15,12 +15,15 @@ namespace NadekoBot.Modules.Games
 | 
			
		||||
        [Group]
 | 
			
		||||
        public class TriviaCommands : NadekoSubmodule<GamesService>
 | 
			
		||||
        {
 | 
			
		||||
            private readonly IDataCache _cache;
 | 
			
		||||
            private readonly CurrencyService _cs;
 | 
			
		||||
            private readonly DiscordSocketClient _client;
 | 
			
		||||
            private readonly IBotConfigProvider _bc;
 | 
			
		||||
 | 
			
		||||
            public TriviaCommands(DiscordSocketClient client, IBotConfigProvider bc, CurrencyService cs)
 | 
			
		||||
            public TriviaCommands(DiscordSocketClient client, IDataCache cache,
 | 
			
		||||
                IBotConfigProvider bc, CurrencyService cs)
 | 
			
		||||
            {
 | 
			
		||||
                _cache = cache;
 | 
			
		||||
                _cs = cs;
 | 
			
		||||
                _client = client;
 | 
			
		||||
                _bc = bc;
 | 
			
		||||
@@ -45,7 +48,7 @@ namespace NadekoBot.Modules.Games
 | 
			
		||||
                var showHints = !additionalArgs.Contains("nohint");
 | 
			
		||||
                var isPokemon = additionalArgs.Contains("pokemon");
 | 
			
		||||
 | 
			
		||||
                var trivia = new TriviaGame(_strings, _client, _bc, _cs, channel.Guild, channel, showHints, winReq, isPokemon);
 | 
			
		||||
                var trivia = new TriviaGame(_strings, _client, _bc, _cache, _cs, channel.Guild, channel, showHints, winReq, isPokemon);
 | 
			
		||||
                if (_service.RunningTrivias.TryAdd(channel.Guild.Id, trivia))
 | 
			
		||||
                {
 | 
			
		||||
                    try
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user