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.

410 lines
12 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>();
private Dictionary<int, List<AnswerTip>> _answerTipDict = new Dictionary<int, List<AnswerTip>>();
// Start is called before the first frame update
public void Init()
{
switch (GameRoot.Instance.ABType)
{
case 0:
InitAnswerConfig("answerConfig");
InitAnswerTip("answerTip");
break;
case 1:
InitAnswerConfig("answerConfig_B");
InitAnswerTip("answerTip_B");
break;
}
InitPassConfig("passInfo");
}
private void InitAnswerConfig(string path)
{
TextAsset xml = Resources.Load<TextAsset>(path);
if (!xml)
{
Debug.Log("Xml" + path + "加载失败");
}
else
{
Debug.Log("Xml" + path + "加载");
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 InitAnswerTip(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<AnswerTip> answers = new List<AnswerTip>();
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<AnswerTip>();
index = PassIndex;
}
AnswerTip data = new AnswerTip(PassIndex);
foreach (XmlElement e in nodLst[i].ChildNodes)
{
switch (e.Name)
{
case "questionIndex":
data.QuestionIndex = int.Parse(e.InnerText);
break;
case "answer":
data.answer = e.InnerText;
if (data.answer.Contains("r"))
{
data.answer= data.answer.Replace('r', '\n');
}
//if (data.answer.Contains("R"))
//{
// data.answer = data.answer.Replace('R', '\n');
//}
if (data.answer.Contains("1."))
{
data.answer = data.answer.Replace("1.", "(1)");
}
break;
if (data.answer.Contains("2."))
{
data.answer = data.answer.Replace("2.", "(2)");
}
break;
if (data.answer.Contains("3."))
{
data.answer = data.answer.Replace("3.", "(3)");
}
break;
if (data.answer.Contains("4."))
{
data.answer = data.answer.Replace("4.", "(4)");
}
break;
if (data.answer.Contains("5."))
{
data.answer = data.answer.Replace("5.", "(5)");
}
break;
}
}
answers.Add(data);
if (_answerDict.ContainsKey(PassIndex))
{
_answerTipDict[PassIndex] = answers;
}
else
{
_answerTipDict.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 = float.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())
{
Debug.Log("关卡"+passIndex+"小题"+questionIndex+"得分为"+answers[i].score);
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)
{
Debug.Log("关卡" + passIndex + "小题" + questionIndex + "得分为" + answers[i].score);
return true;
}
else
{
Debug.Log("第"+passIndex+"关第"+questionIndex+"题答题为" + str + "答案为" + answers[i].answer);
}
}
}
}
Debug.Log("第" + passIndex + "关第" + questionIndex + "多选题错误" );
return false;
}
public float GetSomePassScore(int index,int[] passIndexs)
{
float allScore = 0;
string scoreStr = "";
for (int i = 0; i < passIndexs.Length; i++)
{
List<Answer> list = new List<Answer>();
float score = 0;
if (_answerDict.TryGetValue(passIndexs[i], out list))
{
foreach (var item in list)
{
score += item.score;
scoreStr += score.ToString();
scoreStr += "+";
}
}
allScore += score;
}
Debug.Log("计算密卷" + index + "的总分值为:"+scoreStr +"="+ allScore);
return allScore;
}
public List<AnswerTip> GetAnswerTip(int passIndex)
{
List<AnswerTip> tips = new List<AnswerTip>();
if (_answerTipDict.TryGetValue(passIndex, out tips))
{
return tips;
}
return null;
}
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;
}
}