public static List<LusherPairScenarioItem> GetSpecialQuestion(List<int> colorNumbers) { using (var lusherPairRepository = new BaseRepository<LusherPairQuestion>()) { List<LusherAnswer> specialColors = new List<LusherAnswer>(); foreach (var colorNumber in colorNumbers) { specialColors.Add(lusherPairRepository.Context.LusherAnswers.FirstOrDefault(x => x.Number == colorNumber)); } LusherPairQuestion specialQuestion = lusherPairRepository.GetAllItems.FirstOrDefault(x => x.Id != 1); if (specialQuestion == null) { throw new QuestionDoesNotExistException(); } List<LusherPairScenarioItem> scenarioItems = new List<LusherPairScenarioItem>(); LusherPairScenarioItem scenarioItem = new LusherPairScenarioItem(); scenarioItem.Steps = new List<LusherPairQuestion>() { specialQuestion }; scenarioItem.Answers = specialColors; scenarioItem.UniqueAnswer = true; scenarioItems.Add(scenarioItem); return scenarioItems; } }
public List<LusherPairScenarioItem> GetAllQuestions() { using (var lusherPairQuestionRepository = new BaseRepository<LusherPairQuestion>()) { List<LusherAnswer> colors = lusherPairQuestionRepository.Context.LusherAnswers.ToList(); if (colors.Count == 0) { throw new AnswerDoesNotExistException(); } LusherPairQuestion mainQuestion = lusherPairQuestionRepository.GetAllItems.FirstOrDefault(); if (mainQuestion == null) { throw new QuestionDoesNotExistException(); } List<List<LusherAnswer>> colorPairs = new List<List<LusherAnswer>>(); Random r = new Random(); for (int i = 0; i < colors.Count; i++) { int j = i + 1; while (j < colors.Count) { List<LusherAnswer> colorPair; if (r.Next()%2 == 0) { colorPair = new List<LusherAnswer>() { colors[i], colors[j++] }; } else { colorPair = new List<LusherAnswer>() { colors[j++], colors[i] }; } colorPairs.Add(colorPair); } } List<List<LusherAnswer>> mixColorPairs = colorPairs.OrderBy(x => r.Next()).ToList(); List<LusherPairScenarioItem> scenarioItems = new List<LusherPairScenarioItem>(); int colorPairIndex = 0; for (int i = 0; i < mixColorPairs.Count; i++) { LusherPairScenarioItem scenarioItem = new LusherPairScenarioItem(); scenarioItem.Steps = new List<LusherPairQuestion>() { mainQuestion }; scenarioItem.Answers = mixColorPairs[colorPairIndex++]; scenarioItem.UniqueAnswer = true; scenarioItems.Add(scenarioItem); } return scenarioItems; } }