private async Task<AnswerCallBackViewModel> ProcessAnswer(string answer, IGame game, string userId) { var answerCallbackVM = new AnswerCallBackViewModel(); var previousQuestion = game.AnswerForCurrentQuestion(answer); answerCallbackVM.wasLastQuestionRightAnswer = previousQuestion.QuestionResult.Value; answerCallbackVM.lastQuestionRightAnswer = previousQuestion.RightAnswer; if (game.HasFinished()) { await ProcessFinishedGame(game, userId, answerCallbackVM); return answerCallbackVM; } answerCallbackVM.nextQuestion = GetNextQuestion(game); answerCallbackVM.hasNextQuestion = true; return answerCallbackVM; }
private async Task ProcessFinishedGame(IGame game, string userId, AnswerCallBackViewModel answerCallbackVM) { var gameResult = game.GetGameResult(); if (userId != Request.AnonymousID) { await _userDataAccess.SaveGameResult(long.Parse(userId), game.GameQuestionsCount, game.GetGameResult()) .ConfigureAwait(false); } else { answerCallbackVM.demoMode = "true"; } answerCallbackVM.hasNextQuestion = false; answerCallbackVM.gameResult = new EndGameViewModel { rightAnswersCount = gameResult, allAnswersCount = game.GameQuestionsCount, demoMode = userId == HttpContext.Request.AnonymousID, answers = game.GetGameAnswers().Select(i => new UserAnswer() { answer = i.UserAnswer, rightAnswer = i.RightAnswer, pictureUrl = i.Picture.ImageUrl, isRightAnswer = i.QuestionResult.Value, pictureName = i.Picture.Name, pictureLink = i.Picture.ImageUrl }).ToArray() }; }