>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 hintTimeoutMiliseconds { get; } = 6000;
public bool ShowHints { get; }
public bool IsPokemon { get; }
private CancellationTokenSource triviaCancelSource { get; set; }
public TriviaQuestion CurrentQuestion { get; private set; }
@ -37,7 +38,7 @@ namespace NadekoBot.Modules.Games.Trivia
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();
@ -45,6 +46,7 @@ namespace NadekoBot.Modules.Games.Trivia
Guild = guild;
Channel = channel;
WinRequirement = winReq;
IsPokemon = isPokemon;
}
private string GetText(string key, params object[] replacements) =>
@ -61,7 +63,7 @@ namespace NadekoBot.Modules.Games.Trivia
triviaCancelSource = new CancellationTokenSource();
// load question
CurrentQuestion = TriviaQuestionPool.Instance.GetRandomQuestion(OldQuestions);
CurrentQuestion = TriviaQuestionPool.Instance.GetRandomQuestion(OldQuestions, IsPokemon);
if (string.IsNullOrWhiteSpace(CurrentQuestion?.Answer) || string.IsNullOrWhiteSpace(CurrentQuestion.Question))
{
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()
.WithTitle(GetText("trivia_game"))
.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);
}
@ -128,7 +131,19 @@ namespace NadekoBot.Modules.Games.Trivia
NadekoBot.Client.MessageReceived -= PotentialGuess;
}
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);
}
}
@ -186,10 +201,13 @@ namespace NadekoBot.Modules.Games.Trivia
ShouldStopGame = true;
try
{
await Channel.SendConfirmAsync(GetText("trivia_game"),
GetText("trivia_win",
await Channel.EmbedAsync(new EmbedBuilder().WithOkColor()
.WithTitle(GetText("trivia_game"))
.WithDescription(GetText("trivia_win",
guildUser.Mention,
Format.Bold(CurrentQuestion.Answer))).ConfigureAwait(false);
Format.Bold(CurrentQuestion.Answer)))
.WithImageUrl(CurrentQuestion.AnswerImageUrl))
.ConfigureAwait(false);
}
catch
{
@ -200,9 +218,12 @@ namespace NadekoBot.Modules.Games.Trivia
await CurrencyHandler.AddCurrencyAsync(guildUser, "Won trivia", reward, true).ConfigureAwait(false);
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); }
}

View File

@ -20,13 +20,17 @@ namespace NadekoBot.Modules.Games.Trivia
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 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.Answer = a;
this.Category = c;
this.ImageUrl = img;
this.AnswerImageUrl = answerImage ?? img;
}
public string GetHint() => Scramble(Answer);

View File

@ -1,10 +1,9 @@
using NadekoBot.Extensions;
using NadekoBot.Services;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
@ -12,27 +11,50 @@ namespace NadekoBot.Modules.Games.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_map.json";
private readonly int maxPokemonId;
private Random rng { get; } = new NadekoRandom();
private TriviaQuestion[] pool { get; }
private ImmutableDictionary<int, string> map { get; }
static TriviaQuestionPool() { }
private TriviaQuestionPool()
{
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)
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;
while (exclude.Contains(randomQuestion = pool[rng.Next(0, pool.Length)])) ;

View File

@ -19,17 +19,21 @@ namespace NadekoBot.Modules.Games
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public Task Trivia([Remainder] string additionalArgs = "")
=> Trivia(10, additionalArgs);
=> InternalTrivia(10, additionalArgs);
[NadekoCommand, Usage, Description, Aliases]
[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 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))
{
try

File diff suppressed because it is too large Load Diff