/// <summary> /// Here we'll check the users answer and post the next question until there are no more questions /// </summary> /// <param name="context">The current chat context</param> /// <param name="result">The IAwaitable result</param> /// <returns></returns> private async Task MessageReceivedAsync(IDialogContext context, IAwaitable <object> result) { var activity = (IMessageActivity)await result; int usersAnswer = -1; if (int.TryParse(activity.Text, out usersAnswer)) { await context.PostAsync($"You chose: {activity.Text}"); if (_game.Answer(usersAnswer)) { await context.PostAsync("Correct!"); } else { await context.PostAsync("Sorry, that's wrong :-("); } await context.PostAsync($"Your score is: {_game.Score()}/{_game._questions.Count}. Next question!"); TriviaQuestion nextQuestion = _game.MoveToNextQuestion(); if (nextQuestion != null) { // post the question in separate message so that it doesn't get cut off await context.PostAsync(_game.CurrentQuestion().Question); await context.PostAsync(MakeChoiceCard(context, nextQuestion)); context.Wait(MessageReceivedAsync); } else { await context.PostAsync("That's it! Thanks for playing :-)"); // see if the user will take our survey PromptDialog.Confirm(context, AfterAskAboutSurvey, new PromptOptions <string>( "Would you like to take a survey?", "Sorry, I didnt't get that", "Hmm...it seems I am having some difficult, let's forget about that survey.", null, 3)); } } else { try { // send to luis to see if we can pick up the users intet await context.Forward(new MyLuisDialog(), AfterLuis, activity, CancellationToken.None); } catch (Exception e) { await context.PostAsync($"Tried to start LUIS but got: {e.Message}"); } } }
public async Task ProcessAnswer(CommandContext ctx, int index) { if (!Throttle.ContainsKey(ctx.Member.DisplayName)) { Throttle.Add(ctx.Member.DisplayName, new ThrottleModel { Timer = new Timer(), CanAnswer = true }); Throttle[ctx.Member.DisplayName].Timer.Elapsed += (o, e) => { ctx.RespondAsync($"{ctx.Member.DisplayName} can now answer"); Throttle[ctx.Member.DisplayName].CanAnswer = true; }; Throttle[ctx.Member.DisplayName].Timer.Interval = 4000; Throttle[ctx.Member.DisplayName].Timer.AutoReset = false; } if (!Throttle[ctx.Member.DisplayName].CanAnswer) { return; } Throttle[ctx.Member.DisplayName].CanAnswer = false; Throttle[ctx.Member.DisplayName].Timer.Start(); if (!_CanAnswer) { return; } if (index < 1 || index > 4) { return; } string member = ctx.Member.DisplayName; if (_Trivia.Answer(member, Answers[index - 1])) { _CanAnswer = false; await _CmdCtx.RespondAsync($"Good job, {member}! **{index}** is correct.\n{_Trivia.GetScores()}"); if (_Trivia.GetRemainingQuestion() == 0) { await Task.Delay(4000); EndGame(); return; } await AskQuestion(); } }
/// <summary> /// Here we'll check the user's answer and post the next question until there are no more questions /// </summary> /// <param name="context">The current chat context</param> /// <param name="result">The IAwaitable result</param> private async Task MessageReceivedAsync(IDialogContext context, IAwaitable <object> result) { var activity = (IMessageActivity)await result; // Reply back to the user with thier answer await context.PostAsync($"You chose: {activity.Text}"); // Let them know if they got it right if (int.TryParse(activity.Text, out var usersAnswer)) { if (_game.Answer(usersAnswer)) { await context.PostAsync("Correct!"); } else { await context.PostAsync("Sorry, that's wrong :-("); } // Show the user thier current score await context.PostAsync($"Your score is: {_game.Score()}/{_game.Questions.Count}. Next question!"); // Move to the next question var nextQuestion = _game.MoveToNextQuestion(); // ...until we run out of questions if (nextQuestion != null) { await context.PostAsync(nextQuestion.Question); await context.PostAsync(MakeChoiceCard(context, nextQuestion)); context.Wait(MessageReceivedAsync); } else { // Thank the user for playing await context.PostAsync("That's it! Thanks for playing :-)"); context.Done(""); } } else { // Handle input validations await context.PostAsync("I didn't quite get that, I am only programmed to accept numbers :-("); context.Wait(MessageReceivedAsync); } }
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable <object> result) { var activity = (IMessageActivity)await result; int usersAnswer = -1; string answer = activity.Text.Replace("trivia", string.Empty); if (int.TryParse(answer, out usersAnswer)) { await context.PostAsync($"You chose: {answer}"); if (_game.Answer(usersAnswer)) { await context.PostAsync("Correct!"); } else { await context.PostAsync("Sorry, that's wrong :-("); } await context.PostAsync($"Your score is: {_game.Score()}/{_game._questions.Count}. Next question!"); TriviaQuestion nextQuestion = _game.MoveToNextQuestion(); if (nextQuestion != null) { await context.PostAsync(nextQuestion.Question); await context.PostAsync(MakeChoiceCard(context, nextQuestion)); context.Wait(MessageReceivedAsync); } else { await context.PostAsync("That's it! :-)"); context.Done(""); _game = null; } } else if (activity.Text != "trivia") { await context.PostAsync("I didn't quite get that, I am only programmed to accept numbers :-("); context.Wait(MessageReceivedAsync); } }