You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

252 lines
7.1 KiB

using System;
using System.Collections.Generic;
using System.Xml;
using UnityEngine;
public class ResSvc : MonoBehaviour
{
private Dictionary<int, List<Answer>> _answerDict = new Dictionary<int, List<Answer>>();
private Dictionary<int, PassInfo> _passInfoDict = new Dictionary<int, PassInfo>();
// Start is called before the first frame update
public void Init()
{
InitAnswerConfig("answerConfig");
InitPassConfig("passInfo");
}
private void InitAnswerConfig(string path)
{
TextAsset xml = Resources.Load<TextAsset>(path);
if (!xml)
{
Debug.Log("Xml" + path + "加载失败");
}
else
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml.text);
XmlNodeList nodLst = doc.SelectSingleNode("root").ChildNodes;
List<Answer> answers = new List<Answer>();
int index = 1;
for (int i = 0; i < nodLst.Count; i++)
{
XmlElement ele = nodLst[i] as XmlElement;
if (ele.GetAttributeNode("PassIndex") == null)
{
continue;
}
int PassIndex = Convert.ToInt32(ele.GetAttributeNode("PassIndex").InnerText);
if (index != PassIndex)
{
answers = new List<Answer>();
index = PassIndex;
}
Answer data = new Answer(PassIndex);
foreach (XmlElement e in nodLst[i].ChildNodes)
{
switch (e.Name)
{
case "questionIndex":
data.QuestionIndex = int.Parse(e.InnerText);
break;
case "answer":
string[] valArr = e.InnerText.Split('_');
if (valArr.Length > 1)
{
for (int j = 0; j < valArr.Length; j++)
{
data.answer += valArr[j];
}
}
else
{
data.answer = valArr[0];
}
break;
case "score":
data.score =float.Parse(e.InnerText);
break;
}
}
answers.Add(data);
if (_answerDict.ContainsKey(PassIndex))
{
_answerDict[PassIndex] = answers;
}
else
{
_answerDict.Add(PassIndex, answers);
}
//Debug.Log(PassIndex + "-" + data.QuestionIndex + "-" + data.answer);
}
}
}
private void InitPassConfig(string path)
{
TextAsset xml = Resources.Load<TextAsset>(path);
if (!xml)
{
Debug.Log("Xml" + path + "加载失败");
}
else
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml.text);
XmlNodeList nodLst = doc.SelectSingleNode("root").ChildNodes;
List<Answer> answers = new List<Answer>();
for (int i = 0; i < nodLst.Count; i++)
{
XmlElement ele = nodLst[i] as XmlElement;
if (ele.GetAttributeNode("PassIndex") == null)
{
continue;
}
int PassIndex = Convert.ToInt32(ele.GetAttributeNode("PassIndex").InnerText);
PassInfo data = new PassInfo(PassIndex);
foreach (XmlElement e in nodLst[i].ChildNodes)
{
switch (e.Name)
{
case "title":
data.info = e.InnerText;
break;
case "score":
data.score = int.Parse(e.InnerText);
break;
case "time":
data.time = int.Parse(e.InnerText);
break;
}
}
_passInfoDict.Add(PassIndex, data);
}
}
}
public bool CheckAnswerIsTrue(int passIndex,int questionIndex,int userAnswer)
{
List<Answer> answers = new List<Answer>();
if(_answerDict.TryGetValue(passIndex,out answers))
{
for (int i = 0; i < answers.Count; i++)
{
if(answers[i].QuestionIndex == questionIndex)
{
int value = int.Parse(answers[i].answer);
Debug.Log("第" + passIndex + "关第" + questionIndex + "题答题为" + userAnswer + "答案为" + answers[i].answer);
if (answers[i].answer == userAnswer.ToString())
{
return true;
}
}
}
}
return false;
}
public bool CheckAnswerIsTrue(int passIndex, int questionIndex, int[] userAnswer)
{
List<Answer> answers = new List<Answer>();
if (_answerDict.TryGetValue(passIndex, out answers))
{
for (int i = 0; i < answers.Count; i++)
{
if (answers[i].QuestionIndex == questionIndex)
{
string value = answers[i].answer;
string str = "";
for (int j = 0; j < userAnswer.Length; j++)
{
str+= userAnswer[j];
}
if (answers[i].answer == str)
{
return true;
}
else
{
Debug.Log("第"+passIndex+"关第"+questionIndex+"题答题为" + str + "答案为" + answers[i].answer);
}
}
}
}
Debug.Log("第" + passIndex + "关第" + questionIndex + "多选题错误" );
return false;
}
public PassInfo GetPassInfo(int passIndex)
{
PassInfo passInfo = null;
if(_passInfoDict.TryGetValue(passIndex,out passInfo))
{
return passInfo;
}
return null;
}
public float GetQuestionScore(int passIndex,int QuestionIndex)
{
List<Answer> list = new List<Answer>();
if( _answerDict.TryGetValue(passIndex, out list))
{
foreach (var item in list)
{
if (item.QuestionIndex == QuestionIndex)
{
return item.score;
}
}
}
return 0;
}
}