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) {
// 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;

View File

@ -11,11 +11,12 @@ namespace NadekoBot.Commands {
public static ConcurrentDictionary<Server, TriviaGame> runningTrivias = new ConcurrentDictionary<Server, TriviaGame>();
public override Func<CommandEventArgs, Task> 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.");
});