public async Task <IActionResult> ValidateQuestion(ValidateQuestionViewModel inputModel) { var user = await _userManager.GetUserAsync(User); // We need to get the selected choice(s) in case it's a multiple choice question var selectedChoices = new List <string>(); if (!string.IsNullOrWhiteSpace(inputModel.SelectedRBChoice)) { selectedChoices.Add(inputModel.SelectedRBChoice); } else if (inputModel.Choices?.Count > 0) { foreach (var choice in inputModel.Choices) { selectedChoices.Add(choice); } } var results = await assignedQuestionProvider.ValidateQuestion(inputModel.QuestionId, mapper.Map <AzureChallenge.Models.Profile.UserProfile>(user), selectedChoices); if (results.All(p => p.Value)) { // User has successfully responded to the question, so update the database var userChallengeResponse = await userChallengesProvider.GetItemAsync(user.Id); // Get the current challenge var challenge = userChallengeResponse.Item2.Challenges.Where(p => p.ChallengeId == inputModel.ChallengeId).FirstOrDefault(); if (inputModel.QuestionIndex == challenge.CurrentIndex) { user.AccumulatedPoint += inputModel.Difficulty; challenge.AccumulatedXP += inputModel.Difficulty; challenge.CurrentIndex += 1; // End of challenge if (inputModel.NextQuestionId == null) { var aggregateReponse = await aggregateProvider.GetItemAsync(inputModel.ChallengeId); aggregateReponse.Item2.ChallengeUsers.Finished += 1; aggregateReponse.Item2.ChallengeUsers.Started -= 1; await aggregateProvider.AddItemAsync(aggregateReponse.Item2); challenge.Completed = true; challenge.endTimeUTC = DateTime.Now.ToUniversalTime(); } else { challenge.CurrentQuestion = inputModel.NextQuestionId; } await userChallengesProvider.AddItemAsync(userChallengeResponse.Item2); await _userManager.UpdateAsync(user); } } return(Ok(results)); }
public async Task <IActionResult> ValidateQuestion(string questionId) { var user = await userManager.GetUserAsync(User); var results = await assignedQuestionProvider.ValidateQuestion(questionId, mapper.Map <AzureChallenge.Models.Profile.UserProfile>(user), new List <string>()); return(Ok(results)); }
public async Task <IActionResult> ValidateQuestion(ValidateQuestionViewModel inputModel) { var user = await _userManager.GetUserAsync(User); // We need to get the selected choice(s) in case it's a multiple choice question var selectedChoices = new List <string>(); if (!string.IsNullOrWhiteSpace(inputModel.SelectedRBChoice)) { selectedChoices.Add(inputModel.SelectedRBChoice); } else if (inputModel.Choices?.Count > 0) { foreach (var choice in inputModel.Choices) { selectedChoices.Add(choice); } } var results = await assignedQuestionProvider.ValidateQuestion(inputModel.QuestionId, mapper.Map <AzureChallenge.Models.Profile.UserProfile>(user), selectedChoices); if (results.All(p => p.Value)) { // User has successfully responded to the question, so update the database var userChallengeResponse = await userChallengesProvider.GetItemAsync(user.Id); // Get the current challenge var challenge = userChallengeResponse.Item2.Challenges.Where(p => p.ChallengeId == inputModel.ChallengeId).FirstOrDefault(); // Get the current challenge definition var challengeDefinition = await challengesProvider.GetItemAsync(inputModel.ChallengeId); if (inputModel.QuestionIndex == challenge.CurrentIndex) { // If the challenge has penalties if (challengeDefinition.Item2.TrackAndDeductPoints) { // User can only get (1 - NumOfEfforts * 0.25) * Difficult points var pointsToAdd = (int)((1 - challenge.NumOfEfforts * 0.25) * inputModel.Difficulty); user.AccumulatedPoint += pointsToAdd; challenge.AccumulatedXP += pointsToAdd; } else { user.AccumulatedPoint += inputModel.Difficulty; challenge.AccumulatedXP += inputModel.Difficulty; } challenge.CurrentIndex += 1; // Reset number of efforts challenge.NumOfEfforts = 0; // End of challenge if (inputModel.NextQuestionId == null) { var aggregateReponse = await aggregateProvider.GetItemAsync(inputModel.ChallengeId); aggregateReponse.Item2.ChallengeUsers.Finished += 1; aggregateReponse.Item2.ChallengeUsers.Started -= 1; await aggregateProvider.AddItemAsync(aggregateReponse.Item2); challenge.Completed = true; challenge.endTimeUTC = DateTime.Now.ToUniversalTime(); } else { challenge.CurrentQuestion = inputModel.NextQuestionId; } await userChallengesProvider.AddItemAsync(userChallengeResponse.Item2); await _userManager.UpdateAsync(user); } } else { // User hasn't responded correctly // Check to see if the challenge penalizes incorrect answers var challengeDefinition = await challengesProvider.GetItemAsync(inputModel.ChallengeId); if (!challengeDefinition.Item1.IsError && challengeDefinition.Item2.TrackAndDeductPoints) { // Challenge penalizes, so we need to update the user record var userChallengeResponse = await userChallengesProvider.GetItemAsync(user.Id); // Get the current challenge var challenge = userChallengeResponse.Item2.Challenges.Where(p => p.ChallengeId == inputModel.ChallengeId).FirstOrDefault(); // Check if the number of efforts is less than 5 if (challenge.NumOfEfforts < 5) { // Increase the number of efforts challenge.NumOfEfforts += 1; // Update the record await userChallengesProvider.AddItemAsync(userChallengeResponse.Item2); } } } return(Ok(results)); }