/// <summary> /// Loads all question sets from a program's folder /// </summary> /// <param name="errors">Returns list of potential errors</param> /// <returns>List of question sets</returns> public static ObservableCollection <QSet> LoadQuestionSets(out List <string> errors) { ObservableCollection <QSet> qSets = new ObservableCollection <QSet>(); QSet qSet = null; errors = new List <string>(); string[] files = Directory.GetFiles(dataDir, "*.csv"); foreach (string file in files) { try { qSet = LoadQSetFromFile(file); if (qSet != null) { qSets.Add(qSet); } } catch (Exception ex) { errors.Add(ex.Message); } } return(qSets); }
/// <summary> /// Loads one question set from a file /// </summary> /// <param name="path">File location</param> /// <returns>Question set or null if file's content is invalid</returns> public static QSet LoadQSetFromFile(string path) { QSet qSet = new QSet(); qSet.Path = path; using (StreamReader sr = new StreamReader(path)) { string s = sr.ReadLine(); if (s == null) //checks wheter the first line is not null { throw new ArgumentException(path + " First line is null, can't load"); } qSet.Name = s.TrimEnd(';'); sr.ReadLine(); Difficulty difficulty = Difficulty.Easy; s = sr.ReadLine(); int line = 3; while (s != null) { if (s.StartsWith(TAG_FIRST_CHAR)) //checks wheter higher difficulty section was reached { if (difficulty < Difficulty.Hard) //checks wheter highest difficulty level was reached { difficulty++; s = sr.ReadLine(); } else { throw new ArgumentException(path + " Špatný počet značek pro změnu obtížnosti, nelze načíst."); } } string[] split = s.Split(';'); if (split.Length != 5) //checks wheter the line contains exactly 5 strings { throw new ArgumentException(path + $" Špatný počet řetězců na řádku {line}, nelze načíst."); } qSet.AddQuestion(difficulty, split[0], split[1], split[2], split[3], split[4]); line++; s = sr.ReadLine(); } } if (qSet.EasyQuestions.Count() >= 5 && qSet.MediumQuestions.Count() >= 5 && qSet.HardQuestions.Count() >= 5) { return(qSet); } else { throw new ArgumentException(path + " Nedostatek otázek stejné obtížnosti, nelze načíst."); } }
public override void LoadQuestions(List <QSet> selectedQSets) { GameQSet = new QSet(); QSetsNames = new List <string>(); foreach (QSet qSet in selectedQSets) //load questions from selected qsets { GameQSet.EasyQuestions.AddRange(qSet.EasyQuestions); GameQSet.MediumQuestions.AddRange(qSet.MediumQuestions); GameQSet.HardQuestions.AddRange(qSet.HardQuestions); QSetsNames.Add(qSet.Name); } currentQList = GameQSet.EasyQuestions; }
/// <summary> /// Save given qSet to destination in his Path property /// </summary> /// <param name="qSet"></param> public static void SaveQSet(QSet qSet) { if (qSet.Path == null) { qSet.Path = GenerateFilePath(qSet.Name); } using (StreamWriter writer = new StreamWriter(qSet.Path, false, Encoding.UTF8)) { writer.WriteLine(qSet.Name); writer.WriteLine(EASY_SECTION_TAG); //difficulty section tag List <Question> questions = qSet.EasyQuestions; //start with writing easy questions string[] parts = new string[5]; Difficulty difficulty = Difficulty.Easy; bool loop = true; while (loop) { foreach (Question question in questions) { parts[0] = question.QuestionSentence; parts[1] = question.RightAnswer; parts[2] = question.WrongAnswer1; parts[3] = question.WrongAnswer2; parts[4] = question.WrongAnswer3; writer.WriteLine(string.Join(";", parts)); } difficulty++; if (difficulty == Difficulty.Medium) { writer.WriteLine(MEDIUM_SECTION_TAG); questions = qSet.MediumQuestions; } else if (difficulty == Difficulty.Hard) { writer.WriteLine(HARD_SECTION_TAG); questions = qSet.HardQuestions; } else { loop = false; } } } }
/// <summary> /// Checks number of questions in QSet /// </summary> /// <param name="qSet"></param> /// <returns>Error message; null if QSet is ok</returns> public static string CheckQSet(QSet qSet) { if (qSet.EasyQuestions.Count() < MINIMUM_OF_QUESTIONS) { return($"Nedostatek jednoduchých otázek. Musí jich být alespoň {MINIMUM_OF_QUESTIONS}."); } if (qSet.MediumQuestions.Count() < MINIMUM_OF_QUESTIONS) { return($"Nedostatek středně těžkých otázek. Musí jich být alespoň {MINIMUM_OF_QUESTIONS}."); } if (qSet.HardQuestions.Count() < MINIMUM_OF_QUESTIONS) { return($"Nedostatek těžkých otázek. Musí jich být alespoň {MINIMUM_OF_QUESTIONS}."); } return(null); }