public void LoadingQuery(string file) { CSMUFileMgr = new Dictionary<int, CSMUFile>(); string[] data = File.ReadAllLines(file) .Where(empty => !string.IsNullOrEmpty(empty)).ToArray(); if (data.Length < 1) throw new Exception("File is empty."); for (int rowId = 0; rowId < data.Length; rowId++) { List<string> rowData = GetRowData(data[rowId]); CFile = new CSMUFile(); int firstIndex = 0; // get quest type int iType = int.Parse(rowData[0]); // set type switch (iType) { case 1: CFile.questType = QuestType.Single; break; case 2: CFile.questType = QuestType.Multiple; break; case 3: CFile.questType = QuestType.ChoiceImage; break; case 4: CFile.questType = QuestType.TextingImage; break; default: throw new Exception("Unknown type."); } // get quest CFile.Quest = rowData[1]; if (iType == 4) // set image.. CFile.HeaderImage = rowData[2]; else // or number of correct { CFile.NumberOfCorrect = int.Parse(rowData[2]); // get first index firstIndex = rowData.Count - CFile.NumberOfCorrect; } // get last index int lastIndex = rowData.Count - 1; if (iType == 1) { // add corrected to list CFile.corrected.Add(rowData[lastIndex]); // prepare answer's list // remove correct rowData.RemoveRange(lastIndex, 1); // remove first data such as type, quest, headerimage/numberofcorrect rowData.RemoveRange(0, 3); } else if (iType == 2 || iType == 3) { // fill for (int index = lastIndex; index >= firstIndex; index--) CFile.corrected.Add(rowData[index]); // prepare answer's list // remove correct rowData.RemoveRange(firstIndex, CFile.NumberOfCorrect); // remove first data such as type, quest, headerimage/numberofcorrect rowData.RemoveRange(0, 3); } else if (iType == 4) // set answer CFile.ImageAnswer = rowData[3]; // add to dictionary CSMUFileMgr.Add(rowId, CFile); } }
public void LoadingQuery(string file) { CSMUFileMgr = new Dictionary <int, CSMUFile>(); string[] data = File.ReadAllLines(file) .Where(empty => !string.IsNullOrEmpty(empty)).ToArray(); if (data.Length < 1) { throw new Exception("File is empty."); } for (int rowId = 0; rowId < data.Length; rowId++) { List <string> rowData = GetRowData(data[rowId]); CFile = new CSMUFile(); int firstIndex = 0; // get quest type int iType = int.Parse(rowData[0]); // set type switch (iType) { case 1: CFile.questType = QuestType.Single; break; case 2: CFile.questType = QuestType.Multiple; break; case 3: CFile.questType = QuestType.ChoiceImage; break; case 4: CFile.questType = QuestType.TextingImage; break; default: throw new Exception("Unknown type."); } // get quest CFile.Quest = rowData[1]; if (iType == 4) // set image.. { CFile.HeaderImage = rowData[2]; } else // or number of correct { CFile.NumberOfCorrect = int.Parse(rowData[2]); // get first index firstIndex = rowData.Count - CFile.NumberOfCorrect; } // get last index int lastIndex = rowData.Count - 1; if (iType == 1) { // add corrected to list CFile.corrected.Add(rowData[lastIndex]); // prepare answer's list // remove correct rowData.RemoveRange(lastIndex, 1); // remove first data such as type, quest, headerimage/numberofcorrect rowData.RemoveRange(0, 3); } else if (iType == 2 || iType == 3) { // fill for (int index = lastIndex; index >= firstIndex; index--) { CFile.corrected.Add(rowData[index]); } // prepare answer's list // remove correct rowData.RemoveRange(firstIndex, CFile.NumberOfCorrect); // remove first data such as type, quest, headerimage/numberofcorrect rowData.RemoveRange(0, 3); } else if (iType == 4) // set answer { CFile.ImageAnswer = rowData[3]; } Random rnd = new Random(); // random Answers CFile.Answers = rowData.OrderBy(s => rnd.Next()).ToList(); // add to dictionary CSMUFileMgr.Add(rowId, CFile); } // mix quest's CSMUFileMgr = RandomDictionary(CSMUFileMgr); }