diff --git a/NadekoBot/Classes/Trivia/TriviaGame.cs b/NadekoBot/Classes/Trivia/TriviaGame.cs index 9f5ad478..81c088a1 100644 --- a/NadekoBot/Classes/Trivia/TriviaGame.cs +++ b/NadekoBot/Classes/Trivia/TriviaGame.cs @@ -39,6 +39,7 @@ namespace NadekoBot.Classes.Trivia { while (!ShouldStopGame) { // reset the cancellation source triviaCancelSource = new CancellationTokenSource(); + var token = triviaCancelSource.Token; // load question CurrentQuestion = TriviaQuestionPool.Instance.GetRandomQuestion(oldQuestions); if (CurrentQuestion == null) { @@ -58,21 +59,19 @@ namespace NadekoBot.Classes.Trivia { try { //hint - await Task.Delay(HintTimeoutMiliseconds, triviaCancelSource.Token); + await Task.Delay(HintTimeoutMiliseconds, token); await _channel.SendMessage($":exclamation:**Hint:** {CurrentQuestion.GetHint()}"); //timeout - await Task.Delay(QuestionDurationMiliseconds - HintTimeoutMiliseconds, triviaCancelSource.Token); + await Task.Delay(QuestionDurationMiliseconds - HintTimeoutMiliseconds, token); } catch (TaskCanceledException) { 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 await Task.Delay(2000); } @@ -103,10 +102,10 @@ namespace NadekoBot.Classes.Trivia { users.TryAdd(e.User, 0); //add if not exists users[e.User]++; //add 1 point to the winner guess = true; - triviaCancelSource.Cancel(); } } if (guess) { + triviaCancelSource.Cancel(); await _channel.SendMessage($"☑️ {e.User.Mention} guessed it! The answer was: **{CurrentQuestion.Answer}**"); if (users[e.User] == WinRequirement) { ShouldStopGame = true; diff --git a/NadekoBot/Commands/TriviaCommand.cs b/NadekoBot/Commands/TriviaCommand.cs index 5137097c..d9e00884 100644 --- a/NadekoBot/Commands/TriviaCommand.cs +++ b/NadekoBot/Commands/TriviaCommand.cs @@ -11,11 +11,12 @@ namespace NadekoBot.Commands { public static ConcurrentDictionary runningTrivias = new ConcurrentDictionary(); public override Func DoFunc() => async e => { - if (!runningTrivias.ContainsKey(e.Server)) { - 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.**"); + TriviaGame trivia; + if (!runningTrivias.TryGetValue(e.Server, out trivia)) { + 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 - 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) { @@ -30,8 +31,9 @@ namespace NadekoBot.Commands { .Alias("tlb") .Alias("-tlb") .Do(async e=> { - if (runningTrivias.ContainsKey(e.Server)) - await e.Channel.SendMessage(runningTrivias[e.Server].GetLeaderboard()); + TriviaGame trivia; + if (runningTrivias.TryGetValue(e.Server, out trivia)) + await e.Channel.SendMessage(trivia.GetLeaderboard()); else 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.") .Alias("-tq") .Do(async e=> { - if (runningTrivias.ContainsKey(e.Server)) { - await runningTrivias[e.Server].StopGame(); + TriviaGame trivia; + if (runningTrivias.TryGetValue(e.Server, out trivia)) { + await trivia.StopGame(); } else await e.Channel.SendMessage("No trivia is running on this server."); });