NadekoBot/NadekoBot.Modules.Games/Common/Trivia/TriviaQuestionPool.cs

65 lines
2.1 KiB
C#
Raw Normal View History

2017-07-17 19:42:36 +00:00
using System;
2016-02-02 11:23:58 +00:00
using System.Collections.Generic;
2017-03-23 11:09:15 +00:00
using System.Collections.Immutable;
2016-02-02 11:23:58 +00:00
using System.IO;
using System.Linq;
2017-07-17 19:42:36 +00:00
using NadekoBot.Common;
using NadekoBot.Extensions;
using Newtonsoft.Json;
2016-02-02 11:23:58 +00:00
2017-07-17 19:42:36 +00:00
namespace NadekoBot.Modules.Games.Common.Trivia
2016-04-14 22:17:29 +00:00
{
public class TriviaQuestionPool
{
2017-03-23 11:09:15 +00:00
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";
2017-03-23 11:09:15 +00:00
private readonly int maxPokemonId;
2016-02-02 11:23:58 +00:00
private Random rng { get; } = new NadekoRandom();
private TriviaQuestion[] pool { get; }
2017-03-23 11:09:15 +00:00
private ImmutableDictionary<int, string> map { get; }
2016-02-02 11:23:58 +00:00
static TriviaQuestionPool() { }
2016-04-14 22:17:29 +00:00
private TriviaQuestionPool()
{
pool = JsonConvert.DeserializeObject<TriviaQuestion[]>(File.ReadAllText(questionsFile));
2017-03-23 11:09:15 +00:00
map = JsonConvert.DeserializeObject<PokemonNameId[]>(File.ReadAllText(pokemonMapPath))
.ToDictionary(x => x.Id, x => x.Name)
.ToImmutableDictionary();
maxPokemonId = 721; //xd
2016-02-02 11:23:58 +00:00
}
2017-03-23 11:09:15 +00:00
public TriviaQuestion GetRandomQuestion(HashSet<TriviaQuestion> exclude, bool isPokemon)
2016-04-14 22:17:29 +00:00
{
if (pool.Length == 0)
return null;
2016-02-02 11:23:58 +00:00
2017-03-23 11:09:15 +00:00
if (isPokemon)
{
var num = rng.Next(1, maxPokemonId + 1);
return new TriviaQuestion("Who's That Pokémon?",
map[num].ToTitleCase(),
"Pokemon",
2017-05-10 09:34:40 +00:00
$@"http://nadekobot.me/images/pokemon/shadows/{num}.png",
$@"http://nadekobot.me/images/pokemon/real/{num}.png");
2017-03-23 11:09:15 +00:00
}
TriviaQuestion randomQuestion;
while (exclude.Contains(randomQuestion = pool[rng.Next(0, pool.Length)])) ;
return randomQuestion;
2016-02-02 11:23:58 +00:00
}
}
}