private void InitSession() { var xml = new XmlQuestionRepository(Server.MapPath("/App_Data/Questions.xml")); Session[SessionKeys.USER_KEY] = System.Guid.NewGuid().ToString(); Session[SessionKeys.QUESTIONS_KEY] = xml.GetQuestions(); Session[SessionKeys.STEP_KEY] = 0; }
public decimal GetResult(int questionIndex, int answerIndex) { // Logging using (var context = new LoggerContext()) { var log = context.UserAnswerLogs.Where(q => q.QuestionNumber == questionIndex).FirstOrDefault(); if (log == null) { log = new UserAnswerLog() { QuestionNumber = questionIndex, FirstAnswerCount = 0, SecondAnswerCount = 0, ThirdAnswerCount = 0, FourthAnswerCount = 0 }; log = context.UserAnswerLogs.Add(log); context.SaveChanges(); } if (answerIndex == 0) { log.FirstAnswerCount++; } if (answerIndex == 1) { log.SecondAnswerCount++; } if (answerIndex == 2) { log.ThirdAnswerCount++; } if (answerIndex == 3) { log.FourthAnswerCount++; } context.SaveChanges(); } using (var xmlReader = XmlReader.Create(_xmlUrl)) { var questions = new XmlQuestionRepository(xmlReader).GetAll(); if (questions[questionIndex].CorrectAnswer == questions[questionIndex].Answers.ElementAt(answerIndex)) { if (questionIndex != questions.Count - 1) { // Game still on return(-1); } else { // Player wins return(questions[questionIndex].Price); } } else { // Calculating prize as first guaranteed question return(CalculateResult(questionIndex)); } } }
public Question GetQuestion(int index) { using (var xmlReader = XmlReader.Create(_xmlUrl)) { var questions = new XmlQuestionRepository(xmlReader).GetAll(); if (index > questions.Count - 1) { throw new IndexOutOfRangeException(); } return(questions[index]); } }
private decimal CalculateResult(int index) { using (var xmlReader = XmlReader.Create(_xmlUrl)) { var questions = new XmlQuestionRepository(xmlReader).GetAll(); while (true) { index--; if (index == 12 || index == 8 || index == 4) { return(questions[index].Price); } if (index == -1) { return(0); } } } }
public bool[] GetTwoAnswers(int questionIndex) { using (var xmlReader = XmlReader.Create(_xmlUrl)) { var result = new bool[4] { true, true, true, true }; var questions = new XmlQuestionRepository(xmlReader).GetAll(); var correctAnswerIndex = questions[questionIndex].Answers.ToList().IndexOf(questions[questionIndex].CorrectAnswer); result[correctAnswerIndex] = false; using (var context = new LoggerContext()) { var log = context.UserAnswerLogs.Where(l => l.QuestionNumber == questionIndex).FirstOrDefault(); if (log == null) { var rand = new Random(); int i = rand.Next(4); while (i == correctAnswerIndex) { i = rand.Next(4); } result[i] = false; } else { var stats = new int[] { log.FirstAnswerCount, log.SecondAnswerCount, log.ThirdAnswerCount, log.FourthAnswerCount, }; int indexOfMax = 0; int max = -1; for (int i = 0; i < 4; i++) { if (stats[i] >= max && i != correctAnswerIndex) { indexOfMax = i; max = stats[i]; } } result[indexOfMax] = false; } } return(result); } }