trivia fixy

This commit is contained in:
Master Kwoth 2016-02-28 08:47:47 +01:00
parent 99656095b7
commit 430758537e
2 changed files with 19 additions and 17 deletions

View File

@ -39,6 +39,7 @@ namespace NadekoBot.Classes.Trivia {
while (!ShouldStopGame) { while (!ShouldStopGame) {
// reset the cancellation source // reset the cancellation source
triviaCancelSource = new CancellationTokenSource(); triviaCancelSource = new CancellationTokenSource();
var token = triviaCancelSource.Token;
// load question // load question
CurrentQuestion = TriviaQuestionPool.Instance.GetRandomQuestion(oldQuestions); CurrentQuestion = TriviaQuestionPool.Instance.GetRandomQuestion(oldQuestions);
if (CurrentQuestion == null) { if (CurrentQuestion == null) {
@ -58,21 +59,19 @@ namespace NadekoBot.Classes.Trivia {
try { try {
//hint //hint
await Task.Delay(HintTimeoutMiliseconds, triviaCancelSource.Token); await Task.Delay(HintTimeoutMiliseconds, token);
await _channel.SendMessage($":exclamation:**Hint:** {CurrentQuestion.GetHint()}"); await _channel.SendMessage($":exclamation:**Hint:** {CurrentQuestion.GetHint()}");
//timeout //timeout
await Task.Delay(QuestionDurationMiliseconds - HintTimeoutMiliseconds, triviaCancelSource.Token); await Task.Delay(QuestionDurationMiliseconds - HintTimeoutMiliseconds, token);
} catch (TaskCanceledException) { } catch (TaskCanceledException) {
Console.WriteLine("Trivia cancelled"); Console.WriteLine("Trivia cancelled");
} finally {
GameActive = false;
if (!triviaCancelSource.IsCancellationRequested)
await _channel.Send($":clock2: :question: **Time's up!** The correct answer was **{CurrentQuestion.Answer}**");
NadekoBot.client.MessageReceived -= PotentialGuess;
} }
GameActive = false;
if (!triviaCancelSource.IsCancellationRequested)
await _channel.Send($":clock2: :question: **Time's up!** The correct answer was **{CurrentQuestion.Answer}**");
NadekoBot.client.MessageReceived -= PotentialGuess;
// load next question if game is still running // load next question if game is still running
await Task.Delay(2000); await Task.Delay(2000);
} }
@ -103,10 +102,10 @@ namespace NadekoBot.Classes.Trivia {
users.TryAdd(e.User, 0); //add if not exists users.TryAdd(e.User, 0); //add if not exists
users[e.User]++; //add 1 point to the winner users[e.User]++; //add 1 point to the winner
guess = true; guess = true;
triviaCancelSource.Cancel();
} }
} }
if (guess) { if (guess) {
triviaCancelSource.Cancel();
await _channel.SendMessage($"☑️ {e.User.Mention} guessed it! The answer was: **{CurrentQuestion.Answer}**"); await _channel.SendMessage($"☑️ {e.User.Mention} guessed it! The answer was: **{CurrentQuestion.Answer}**");
if (users[e.User] == WinRequirement) { if (users[e.User] == WinRequirement) {
ShouldStopGame = true; ShouldStopGame = true;

View File

@ -11,11 +11,12 @@ namespace NadekoBot.Commands {
public static ConcurrentDictionary<Server, TriviaGame> runningTrivias = new ConcurrentDictionary<Server, TriviaGame>(); public static ConcurrentDictionary<Server, TriviaGame> runningTrivias = new ConcurrentDictionary<Server, TriviaGame>();
public override Func<CommandEventArgs, Task> DoFunc() => async e => { public override Func<CommandEventArgs, Task> DoFunc() => async e => {
if (!runningTrivias.ContainsKey(e.Server)) { TriviaGame trivia;
runningTrivias.TryAdd(e.Server, new TriviaGame(e)); if (!runningTrivias.TryGetValue(e.Server, out trivia)) {
await e.Channel.SendMessage("**Trivia game started!**\nFirst player to get to 10 points wins! You have 30 seconds per question.\nUse command `tq` if game was started by accident.**"); if(runningTrivias.TryAdd(e.Server, new TriviaGame(e)))
await e.Channel.SendMessage("**Trivia game started!**\nFirst player to get to 10 points wins! You have 30 seconds per question.\nUse command `tq` if game was started by accident.**");
} else } else
await e.Channel.SendMessage("Trivia game is already running on this server.\n" + runningTrivias[e.Server].CurrentQuestion); await e.Channel.SendMessage("Trivia game is already running on this server.\n" + trivia.CurrentQuestion);
}; };
public override void Init(CommandGroupBuilder cgb) { public override void Init(CommandGroupBuilder cgb) {
@ -30,8 +31,9 @@ namespace NadekoBot.Commands {
.Alias("tlb") .Alias("tlb")
.Alias("-tlb") .Alias("-tlb")
.Do(async e=> { .Do(async e=> {
if (runningTrivias.ContainsKey(e.Server)) TriviaGame trivia;
await e.Channel.SendMessage(runningTrivias[e.Server].GetLeaderboard()); if (runningTrivias.TryGetValue(e.Server, out trivia))
await e.Channel.SendMessage(trivia.GetLeaderboard());
else else
await e.Channel.SendMessage("No trivia is running on this server."); await e.Channel.SendMessage("No trivia is running on this server.");
}); });
@ -40,8 +42,9 @@ namespace NadekoBot.Commands {
.Description("Quits current trivia after current question.") .Description("Quits current trivia after current question.")
.Alias("-tq") .Alias("-tq")
.Do(async e=> { .Do(async e=> {
if (runningTrivias.ContainsKey(e.Server)) { TriviaGame trivia;
await runningTrivias[e.Server].StopGame(); if (runningTrivias.TryGetValue(e.Server, out trivia)) {
await trivia.StopGame();
} else } else
await e.Channel.SendMessage("No trivia is running on this server."); await e.Channel.SendMessage("No trivia is running on this server.");
}); });