// Get score for a particular question public float QuestionScore(TestQuestion question) { float score = 0; foreach (var questionInfo in collectedAnswers) { if (questionInfo.Key == question) { var answers = questionInfo.Value; // Mode rules switch (TestState.Mode) { case TestState.ETestMode.Loyal: { foreach (var answer in answers) { if (answer.Correct) { score += question.CorrectValue; } else { score += questionInfo.Key.PunishValue; } } if (score <= 0) { score = 0; } } break; case TestState.ETestMode.Punish: { foreach (var answer in answers) { if (answer.Correct) { score += question.CorrectValue; } else { score += questionInfo.Key.PunishValue; } } } break; default: throw new NotImplementedException("Неизвестный режим работы!\nUnkown test mode!"); } } } return(score); }
// Get collected answers on specific question public IList <TestAnswer> Answered(TestQuestion question) { foreach (var answer in collectedAnswers) { if (answer.Key == question) { return(answer.Value); } } return(new List <TestAnswer>()); }
// Remove answers on question public void ForgetAnswer(TestQuestion question) { foreach (var answer in collectedAnswers) { if (answer.Key == question) { collectedAnswers.Remove(answer); return; } } }
// Load test public void LoadTest(string testFilePath) { StringReader testFile = null; var testDecryptor = new MyTestCreator.TestFileEncDec(testFilePath, MyTestCreator.TestFileEncDec.ETestFileMode.Decode); { var data = testDecryptor.Data; testFile = new StringReader(System.Text.Encoding.UTF8.GetString(data)); } // Read whole text into memory //var file = File.OpenText (testFilePath); //var testFile = new StringReader (file.ReadToEnd ()); //file.Close (); // Current question being parsed TestQuestion question = null; string line = null; //testFile.ReadLine(); do { line = testFile.ReadLine(); // Skip commentary lines if (line == null || line.Length == 0 || line [0] == '#') { continue; } // Parse the rest of the file var keyValue = GetKeyValueFromLine(line); switch (keyValue.Key) { case ETestTags.Test: { name = keyValue.Value; } break; case ETestTags.Author: { author = keyValue.Value; } break; case ETestTags.Date: { var dateStr = keyValue.Value.Split('.'); lastModified = new DateTime(Convert.ToInt32(dateStr [2]), Convert.ToInt32(dateStr [1]), Convert.ToInt32(dateStr [0])); } break; case ETestTags.Time: { // If time is infinite, then time = 0 if (keyValue.Value != "inf") { time = Convert.ToInt32(keyValue.Value); } } break; case ETestTags.Mode: { var modeString = keyValue.Value.ToLower(); if (modeString == ModePunish) { mode = ETestMode.Punish; } else { if (modeString == ModeLoyal) { mode = ETestMode.Loyal; } } } break; case ETestTags.Question: { // Add previous question to the list if (question != null) { questions.Add(question); } question = new TestQuestion(keyValue.Value); } break; case ETestTags.Value: { // Ignore this keyword if no Question keyword encountered if (question != null) { question.Value = Convert.ToSingle(keyValue.Value); } } break; case ETestTags.Answer: { // Ignore this keyword if no Question keyword encountered if (question != null) { var answer = new TestAnswer(keyValue.Value, false); question.Answers.Add(answer); } } break; case ETestTags.AnswerTrue: { // Ignore this keyword if no Question keyword encountered if (question != null) { var answer = new TestAnswer(keyValue.Value, true); question.Answers.Add(answer); } } break; default: continue; } }while(line != null && line.Length != 0); // Add last question we've parsed if (question != null) { if (!questions.Contains(question)) { questions.Add(question); } } testFile.Close(); }
// Answer on question public void Answer(TestQuestion question, IList <TestAnswer> answers) { collectedAnswers.Add(new KeyValuePair <TestQuestion, IList <TestAnswer> > (question, answers)); }