>trivia pokemon added

This commit is contained in:
Kwoth 2017-03-23 12:09:15 +01:00
parent c1b14cc96b
commit 64c52f1cd0
6 changed files with 3319 additions and 19 deletions

View File

@ -1,3 +1,6 @@
{ {
"projects": [ "Discord.Net/src", "src" ] "projects": [ "Discord.Net/src", "src" ],
"sdk": {
"version": "1.0.0-preview2-1-003177"
}
} }

View File

@ -25,6 +25,7 @@ namespace NadekoBot.Modules.Games.Trivia
private int questionDurationMiliseconds { get; } = 30000; private int questionDurationMiliseconds { get; } = 30000;
private int hintTimeoutMiliseconds { get; } = 6000; private int hintTimeoutMiliseconds { get; } = 6000;
public bool ShowHints { get; } public bool ShowHints { get; }
public bool IsPokemon { get; }
private CancellationTokenSource triviaCancelSource { get; set; } private CancellationTokenSource triviaCancelSource { get; set; }
public TriviaQuestion CurrentQuestion { get; private set; } public TriviaQuestion CurrentQuestion { get; private set; }
@ -37,7 +38,7 @@ namespace NadekoBot.Modules.Games.Trivia
public int WinRequirement { get; } public int WinRequirement { get; }
public TriviaGame(IGuild guild, ITextChannel channel, bool showHints, int winReq) public TriviaGame(IGuild guild, ITextChannel channel, bool showHints, int winReq, bool isPokemon)
{ {
_log = LogManager.GetCurrentClassLogger(); _log = LogManager.GetCurrentClassLogger();
@ -45,6 +46,7 @@ namespace NadekoBot.Modules.Games.Trivia
Guild = guild; Guild = guild;
Channel = channel; Channel = channel;
WinRequirement = winReq; WinRequirement = winReq;
IsPokemon = isPokemon;
} }
private string GetText(string key, params object[] replacements) => private string GetText(string key, params object[] replacements) =>
@ -61,7 +63,7 @@ namespace NadekoBot.Modules.Games.Trivia
triviaCancelSource = new CancellationTokenSource(); triviaCancelSource = new CancellationTokenSource();
// load question // load question
CurrentQuestion = TriviaQuestionPool.Instance.GetRandomQuestion(OldQuestions); CurrentQuestion = TriviaQuestionPool.Instance.GetRandomQuestion(OldQuestions, IsPokemon);
if (string.IsNullOrWhiteSpace(CurrentQuestion?.Answer) || string.IsNullOrWhiteSpace(CurrentQuestion.Question)) if (string.IsNullOrWhiteSpace(CurrentQuestion?.Answer) || string.IsNullOrWhiteSpace(CurrentQuestion.Question))
{ {
await Channel.SendErrorAsync(GetText("trivia_game"), GetText("failed_loading_question")).ConfigureAwait(false); await Channel.SendErrorAsync(GetText("trivia_game"), GetText("failed_loading_question")).ConfigureAwait(false);
@ -76,7 +78,8 @@ namespace NadekoBot.Modules.Games.Trivia
questionEmbed = new EmbedBuilder().WithOkColor() questionEmbed = new EmbedBuilder().WithOkColor()
.WithTitle(GetText("trivia_game")) .WithTitle(GetText("trivia_game"))
.AddField(eab => eab.WithName(GetText("category")).WithValue(CurrentQuestion.Category)) .AddField(eab => eab.WithName(GetText("category")).WithValue(CurrentQuestion.Category))
.AddField(eab => eab.WithName(GetText("question")).WithValue(CurrentQuestion.Question)); .AddField(eab => eab.WithName(GetText("question")).WithValue(CurrentQuestion.Question))
.WithImageUrl(CurrentQuestion.ImageUrl);
questionMessage = await Channel.EmbedAsync(questionEmbed).ConfigureAwait(false); questionMessage = await Channel.EmbedAsync(questionEmbed).ConfigureAwait(false);
} }
@ -128,7 +131,19 @@ namespace NadekoBot.Modules.Games.Trivia
NadekoBot.Client.MessageReceived -= PotentialGuess; NadekoBot.Client.MessageReceived -= PotentialGuess;
} }
if (!triviaCancelSource.IsCancellationRequested) if (!triviaCancelSource.IsCancellationRequested)
try { await Channel.SendErrorAsync(GetText("trivia_game"), GetText("trivia_times_up", Format.Bold(CurrentQuestion.Answer))).ConfigureAwait(false); } catch (Exception ex) { _log.Warn(ex); } {
try
{
await Channel.EmbedAsync(new EmbedBuilder().WithErrorColor()
.WithTitle(GetText("trivia_game"))
.WithDescription(GetText("trivia_times_up", Format.Bold(CurrentQuestion.Answer))))
.ConfigureAwait(false);
}
catch (Exception ex)
{
_log.Warn(ex);
}
}
await Task.Delay(2000).ConfigureAwait(false); await Task.Delay(2000).ConfigureAwait(false);
} }
} }
@ -186,10 +201,13 @@ namespace NadekoBot.Modules.Games.Trivia
ShouldStopGame = true; ShouldStopGame = true;
try try
{ {
await Channel.SendConfirmAsync(GetText("trivia_game"), await Channel.EmbedAsync(new EmbedBuilder().WithOkColor()
GetText("trivia_win", .WithTitle(GetText("trivia_game"))
.WithDescription(GetText("trivia_win",
guildUser.Mention, guildUser.Mention,
Format.Bold(CurrentQuestion.Answer))).ConfigureAwait(false); Format.Bold(CurrentQuestion.Answer)))
.WithImageUrl(CurrentQuestion.AnswerImageUrl))
.ConfigureAwait(false);
} }
catch catch
{ {
@ -200,9 +218,12 @@ namespace NadekoBot.Modules.Games.Trivia
await CurrencyHandler.AddCurrencyAsync(guildUser, "Won trivia", reward, true).ConfigureAwait(false); await CurrencyHandler.AddCurrencyAsync(guildUser, "Won trivia", reward, true).ConfigureAwait(false);
return; return;
} }
await Channel.SendConfirmAsync(GetText("trivia_game"),
GetText("trivia_guess", guildUser.Mention, Format.Bold(CurrentQuestion.Answer))).ConfigureAwait(false);
await Channel.EmbedAsync(new EmbedBuilder().WithOkColor()
.WithTitle(GetText("trivia_game"))
.WithDescription(GetText("trivia_guess", guildUser.Mention, Format.Bold(CurrentQuestion.Answer)))
.WithImageUrl(CurrentQuestion.AnswerImageUrl))
.ConfigureAwait(false);
} }
catch (Exception ex) { _log.Warn(ex); } catch (Exception ex) { _log.Warn(ex); }
} }

View File

@ -20,13 +20,17 @@ namespace NadekoBot.Modules.Games.Trivia
public string Category { get; set; } public string Category { get; set; }
public string Question { get; set; } public string Question { get; set; }
public string ImageUrl { get; set; }
public string AnswerImageUrl { get; set; }
public string Answer { get; set; } public string Answer { get; set; }
public TriviaQuestion(string q, string a, string c) public TriviaQuestion(string q, string a, string c, string img = null, string answerImage = null)
{ {
this.Question = q; this.Question = q;
this.Answer = a; this.Answer = a;
this.Category = c; this.Category = c;
this.ImageUrl = img;
this.AnswerImageUrl = answerImage ?? img;
} }
public string GetHint() => Scramble(Answer); public string GetHint() => Scramble(Answer);

View File

@ -1,10 +1,9 @@
using NadekoBot.Extensions; using NadekoBot.Extensions;
using NadekoBot.Services; using NadekoBot.Services;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
@ -12,27 +11,50 @@ namespace NadekoBot.Modules.Games.Trivia
{ {
public class TriviaQuestionPool public class TriviaQuestionPool
{ {
public class PokemonNameId
{
public int Id { get; set; }
public string Name { get; set; }
}
private static TriviaQuestionPool _instance; private static TriviaQuestionPool _instance;
public static TriviaQuestionPool Instance { get; } = _instance ?? (_instance = new TriviaQuestionPool()); public static TriviaQuestionPool Instance { get; } = _instance ?? (_instance = new TriviaQuestionPool());
private const string questionsFile = "data/trivia_questions.json"; private const string questionsFile = "data/trivia_questions.json";
private const string pokemonMapPath = "data/pokemon/name-id_map.json";
private readonly int maxPokemonId;
private Random rng { get; } = new NadekoRandom(); private Random rng { get; } = new NadekoRandom();
private TriviaQuestion[] pool { get; } private TriviaQuestion[] pool { get; }
private ImmutableDictionary<int, string> map { get; }
static TriviaQuestionPool() { } static TriviaQuestionPool() { }
private TriviaQuestionPool() private TriviaQuestionPool()
{ {
pool = JsonConvert.DeserializeObject<TriviaQuestion[]>(File.ReadAllText(questionsFile)); pool = JsonConvert.DeserializeObject<TriviaQuestion[]>(File.ReadAllText(questionsFile));
map = JsonConvert.DeserializeObject<PokemonNameId[]>(File.ReadAllText(pokemonMapPath))
.ToDictionary(x => x.Id, x => x.Name)
.ToImmutableDictionary();
maxPokemonId = 721; //xd
} }
public TriviaQuestion GetRandomQuestion(HashSet<TriviaQuestion> exclude) public TriviaQuestion GetRandomQuestion(HashSet<TriviaQuestion> exclude, bool isPokemon)
{ {
if (pool.Length == 0) if (pool.Length == 0)
return null; return null;
if (isPokemon)
{
var num = rng.Next(1, maxPokemonId + 1);
return new TriviaQuestion("Who's That Pokémon?",
map[num].ToTitleCase(),
"Pokemon",
$@"http://nadekobot.xyz/images/pokemon/shadows/{num}.png",
$@"http://nadekobot.xyz/images/pokemon/real/{num}.png");
}
TriviaQuestion randomQuestion; TriviaQuestion randomQuestion;
while (exclude.Contains(randomQuestion = pool[rng.Next(0, pool.Length)])) ; while (exclude.Contains(randomQuestion = pool[rng.Next(0, pool.Length)])) ;

View File

@ -19,17 +19,21 @@ namespace NadekoBot.Modules.Games
[NadekoCommand, Usage, Description, Aliases] [NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]
public Task Trivia([Remainder] string additionalArgs = "") public Task Trivia([Remainder] string additionalArgs = "")
=> Trivia(10, additionalArgs); => InternalTrivia(10, additionalArgs);
[NadekoCommand, Usage, Description, Aliases] [NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]
public async Task Trivia(int winReq = 10, [Remainder] string additionalArgs = "") public Task Trivia(int winReq = 10, [Remainder] string additionalArgs = "")
=> InternalTrivia(winReq, additionalArgs);
public async Task InternalTrivia(int winReq, string additionalArgs = "")
{ {
var channel = (ITextChannel)Context.Channel; var channel = (ITextChannel)Context.Channel;
var showHints = !additionalArgs.Contains("nohint"); var showHints = !additionalArgs.Contains("nohint");
var isPokemon = additionalArgs.Contains("pokemon");
var trivia = new TriviaGame(channel.Guild, channel, showHints, winReq); var trivia = new TriviaGame(channel.Guild, channel, showHints, winReq, isPokemon);
if (RunningTrivias.TryAdd(channel.Guild.Id, trivia)) if (RunningTrivias.TryAdd(channel.Guild.Id, trivia))
{ {
try try
@ -41,9 +45,9 @@ namespace NadekoBot.Modules.Games
RunningTrivias.TryRemove(channel.Guild.Id, out trivia); RunningTrivias.TryRemove(channel.Guild.Id, out trivia);
await trivia.EnsureStopped().ConfigureAwait(false); await trivia.EnsureStopped().ConfigureAwait(false);
} }
return; return;
} }
await Context.Channel.SendErrorAsync(GetText("trivia_already_running") + "\n" + trivia.CurrentQuestion) await Context.Channel.SendErrorAsync(GetText("trivia_already_running") + "\n" + trivia.CurrentQuestion)
.ConfigureAwait(false); .ConfigureAwait(false);
} }

File diff suppressed because it is too large Load Diff