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.
363 lines
13 KiB
363 lines
13 KiB
3 years ago
|
using UnityEngine;
|
||
|
using System.Collections;
|
||
|
using UnityEngine.UI;
|
||
|
using System.Collections.Generic;
|
||
|
using AX.TrackRecord;
|
||
|
using UnityEngine.SceneManagement;
|
||
|
using System;
|
||
|
using System.IO;
|
||
|
using AX.NetworkSystem;
|
||
|
using AX.MessageSystem;
|
||
|
/// <summary>
|
||
|
/// 考试预制体信息类
|
||
|
/// </summary>
|
||
|
///
|
||
|
public enum ExamState
|
||
|
{
|
||
|
None=0,
|
||
|
ToExam=1,
|
||
|
Examing=2,
|
||
|
ExamOver=3
|
||
|
}
|
||
|
public class ExamItemManager : BaseItemManager
|
||
|
{
|
||
|
string[] files;
|
||
|
public bool StartExam = false;//
|
||
|
public bool ExamOver = false;
|
||
|
public ExamState CurExamState;
|
||
|
public override void SetItemPrefab()
|
||
|
{
|
||
|
ItemPrefab = Resources.Load<GameObject>("UIPrefab/Examination/QuestionPrefab");
|
||
|
TiShi = transform.Find("TiShi").gameObject;
|
||
|
|
||
|
MessageDispatcher.AddListener("REFRESH_TIME", RefreshTime);
|
||
|
deltaTimeBetweenLocalAndServer = TimeSpan.Zero;//////////////////**********
|
||
|
|
||
|
}
|
||
|
|
||
|
void Start()
|
||
|
{
|
||
|
//判断当前考试是否已经开始答题,从而确定计时,每次加载场景时执行一次
|
||
|
if (TimeManager.CurExam != null)//不为空,证明考试已经开始
|
||
|
{
|
||
|
if (TimeManager.CurExam.ID == exam.ID)//当前正在作答的考试
|
||
|
{
|
||
|
if (TimeManager.RemainSeconds > 0)//考试还未结束,正常计时
|
||
|
{
|
||
|
RemainSeconds = TimeManager.RemainSeconds;
|
||
|
CurExamState = ExamState.Examing;
|
||
|
}
|
||
|
else//考试已经结束,自动交卷
|
||
|
{
|
||
|
CurExamState = ExamState.ExamOver;
|
||
|
ShowTimeAccordingSeconds(0);
|
||
|
OKSubmit();
|
||
|
}
|
||
|
}
|
||
|
else//当前没有作答的考试
|
||
|
{
|
||
|
CurExamState = ExamState.ToExam;
|
||
|
}
|
||
|
}
|
||
|
else//为空,证明考试还没有开始
|
||
|
{
|
||
|
CurExamState = ExamState.ToExam;
|
||
|
}
|
||
|
|
||
|
MessageDispatcher.AddListener("SubmitExam", SubmitExam);
|
||
|
}
|
||
|
|
||
|
private void SubmitExam(IMessage message)
|
||
|
{
|
||
|
if (exam.ID == (long)message.Data)
|
||
|
{
|
||
|
OKSubmit();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void OnDestroy()
|
||
|
{
|
||
|
MessageDispatcher.RemoveListener("REFRESH_TIME", RefreshTime);
|
||
|
MessageDispatcher.RemoveListener("SubmitExam", SubmitExam);
|
||
|
}
|
||
|
|
||
|
|
||
|
void Update()
|
||
|
{
|
||
|
|
||
|
switch (CurExamState)
|
||
|
{
|
||
|
case ExamState.ToExam:
|
||
|
var deltaTime = exam.Deadline - (DateTime.Now - deltaTimeBetweenLocalAndServer);
|
||
|
RemainSeconds = (int)deltaTime.TotalSeconds < exam.Duration * 60 ? (int)deltaTime.TotalSeconds : exam.Duration * 60;
|
||
|
if (RemainSeconds > 0)
|
||
|
{
|
||
|
ShowTimeAccordingSeconds((int)RemainSeconds);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
ShowTimeAccordingSeconds(0);
|
||
|
OKSubmit();
|
||
|
}
|
||
|
//ShowTimeAccordingSeconds((int)RemainSeconds);
|
||
|
break;
|
||
|
case ExamState.Examing:
|
||
|
RemainSeconds -= Time.deltaTime;
|
||
|
if (RemainSeconds > 0)
|
||
|
{
|
||
|
ShowTimeAccordingSeconds((int)RemainSeconds);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
ShowTimeAccordingSeconds(0);
|
||
|
OKSubmit();
|
||
|
}
|
||
|
|
||
|
break;
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
public void StartAnswerQuestionButton()
|
||
|
{
|
||
|
ExamInfoHelpClass.loadSceneMode = ExamInfoHelpClass.LoadSceneMode.ExamineMode;
|
||
|
ExamInfoHelpClass.selectSceneID = (ExamInfoHelpClass.Scene)question.SceneType;//给Selected.selectSceneID赋值
|
||
|
ExamInfoHelpClass.ExamID = exam.ID;
|
||
|
CurExam = exam;
|
||
|
CurQues = question;
|
||
|
//SceneManager.LoadScene(sceneName);//加载考题对应的场景
|
||
|
//使用服务端时间对比
|
||
|
if (DateTime.Now - deltaTimeBetweenLocalAndServer < exam.StartingTime )//考试时间未到,不能开始答题
|
||
|
{
|
||
|
TiShi.gameObject.SetActive(true);
|
||
|
TiShi.GetComponent<UITiShiManager>().Get("没有到开始时间,请耐心等待");
|
||
|
}
|
||
|
//else if ( DateTime.Now - deltaTimeBetweenLocalAndServer < exam.Deadline)//未结束,可以答题
|
||
|
else if(RemainSeconds == -10 || RemainSeconds > TimeManager.TipTime)
|
||
|
{
|
||
|
if(TimeManager.CurExam != null && TimeManager.CurExam.ID!= exam.ID)
|
||
|
{
|
||
|
Debug.Log("请先提交当前考试:" + TimeManager.CurExam.Name);
|
||
|
TiShi.gameObject.SetActive(true);
|
||
|
TiShi.GetComponent<UITiShiManager>().Get("请先提交当前考试:" + TimeManager.CurExam.Name);
|
||
|
return;
|
||
|
}
|
||
|
switch (CurExamState)
|
||
|
{
|
||
|
case ExamState.Examing:
|
||
|
case ExamState.ToExam:
|
||
|
TimeManager.RemainSeconds = RemainSeconds;
|
||
|
MySceneManager.MyLoadScene(question.SceneType.ToString());
|
||
|
TimeManager.CurExam = exam;
|
||
|
TimeManager.CurQues = question;
|
||
|
TimeManager.LastTime = DateTime.Now;
|
||
|
break;
|
||
|
case ExamState.ExamOver:
|
||
|
//
|
||
|
|
||
|
break;
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
//MySceneManager.MyLoadScene(question.SceneType.ToString());
|
||
|
//if (!StartExam)
|
||
|
//{
|
||
|
// var deltaTime = exam.Deadline - (DateTime.Now - deltaTimeBetweenLocalAndServer);
|
||
|
// RemainSeconds = (int)deltaTime.TotalSeconds < exam.Duration * 60 ? (int)deltaTime.TotalSeconds : exam.Duration * 60;
|
||
|
//}
|
||
|
//TimeManager.RemainSeconds = RemainSeconds;
|
||
|
//TimeManager.CurExam = exam;
|
||
|
//StartExam = true;
|
||
|
}
|
||
|
else//结束,不能答题了
|
||
|
{
|
||
|
//ToDo...
|
||
|
//if (RemainSeconds > -10 && RemainSeconds < TimeManager.TipTime)
|
||
|
//{
|
||
|
Debug.Log("考试时间不足,无法进入!");
|
||
|
TiShi.gameObject.SetActive(true);
|
||
|
TiShi.GetComponent<UITiShiManager>().Get("考试时间不足,无法进入!");
|
||
|
return;
|
||
|
//}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 查看考题作答
|
||
|
/// </summary>
|
||
|
public void CheckQuestionButton()
|
||
|
{
|
||
|
if (TimeManager.CurExam != null)
|
||
|
{
|
||
|
TiShi.gameObject.SetActive(true);
|
||
|
TiShi.GetComponent<UITiShiManager>().Get("已开始答题,无法查看!");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
CurExam = exam;
|
||
|
CurQues = question;
|
||
|
Debug.Log("CheckExam!");
|
||
|
ExamInfoHelpClass.loadSceneMode = ExamInfoHelpClass.LoadSceneMode.CheckQuestion;
|
||
|
ExamInfoHelpClass.selectSceneID = (ExamInfoHelpClass.Scene)question.SceneType;//给Selected.selectSceneID赋值
|
||
|
//SceneManager.LoadScene(sceneName);//加载考题对应的场景
|
||
|
MySceneManager.MyLoadScene(question.SceneType.ToString());
|
||
|
}
|
||
|
|
||
|
public void SubmitExamButton()
|
||
|
{
|
||
|
if (AnswerList.Count < 1)
|
||
|
{
|
||
|
MessageDispatcher.SendMessage("Operatinghints", (object)"提交前先答题!");
|
||
|
return;
|
||
|
}
|
||
|
CurExam = exam;
|
||
|
CurQues = question;
|
||
|
GameObject TipWindow = Instantiate(Resources.Load<GameObject>("UIPrefab/TipWindow"));
|
||
|
TipWindow.GetComponent<TipWindowManager>().SetWindow(
|
||
|
"确定提交?", new UnityEngine.Events.UnityAction(OKSubmit), new UnityEngine.Events.UnityAction(NOSubmit));
|
||
|
}
|
||
|
private void OKSubmit()
|
||
|
{
|
||
|
Debug.Log("submit");
|
||
|
//提交答案
|
||
|
//上传答案到服务器,删除本地答案(清空当前用户工作目录下的question文件夹及answer文件夹)
|
||
|
//ToDo...
|
||
|
if (TimeManager.CurExam != null && TimeManager.CurExam.ID == exam.ID)
|
||
|
{
|
||
|
if (AnswerList.Count < 1)
|
||
|
{
|
||
|
AnswerInfo answer = new AnswerInfo();
|
||
|
answer.FilePath = exam.ID + "_" + BaseItemManager.CurQues.ID + ".xml";//答案文件用考试ID+考题ID命名
|
||
|
answer.CreatorName = MySelf.mySelf.Name;
|
||
|
answer.CreatorID = MySelf.mySelf.ID;
|
||
|
answer.ExaminationID = exam.ID;
|
||
|
answer.QuestionID = BaseItemManager.CurQues.ID;
|
||
|
answer.PaperID = exam.PaperID;
|
||
|
if (!BaseItemManager.AnswerList.Contains(answer))
|
||
|
{
|
||
|
BaseItemManager.AnswerList.Add(answer);//添加到待上传答案数组
|
||
|
}
|
||
|
//调用函数,创建答案xml文件;保存在对应考试ID下,避免多场考试引用同一考题,造成回答一个问题,所有考试考题都有答案
|
||
|
string filename_local = "Answers/" + answer.FilePath;//////////
|
||
|
//RecordManager.Instance.SubmitAnswer(filename_local);//提交答案
|
||
|
Record_One_root record = new Record_One_root();
|
||
|
record.AlreadyAnswered = false;
|
||
|
TrackRecordHelpClass.SerializeRecordToXML(filename_local, record);
|
||
|
}
|
||
|
for(int i = 0; i < AnswerList.Count; i++)
|
||
|
{
|
||
|
NetworkManager.Default.SendRequestAsync("ANSWER_SAVE_REQUEST", AnswerList[i]);//最后提交考试的时候再发送请求
|
||
|
}
|
||
|
//重置考试状态
|
||
|
CurExamState = ExamState.ExamOver;
|
||
|
TimeManager.CurExam = null;
|
||
|
TimeManager.CurQues = null;
|
||
|
TimeManager.RemainSeconds = -10;
|
||
|
RemainSeconds = -10;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
transform.GetComponent<ANSWER_SAVE_REPLY>().ExamOver();
|
||
|
CurExamState = ExamState.ExamOver;
|
||
|
}
|
||
|
}
|
||
|
public void AutoSubmit()
|
||
|
{
|
||
|
OKSubmit();
|
||
|
}
|
||
|
private void NOSubmit()
|
||
|
{
|
||
|
Debug.Log("NOsubmit");
|
||
|
}
|
||
|
public void DownloadQuestion()
|
||
|
{
|
||
|
//下载文件
|
||
|
if (GameObject.Find("FileTransTipWindow"))//如果已经存在文件传输窗口,不再相应
|
||
|
return;
|
||
|
GameObject FileTransTipWin = Instantiate(Resources.Load<GameObject>("UIPrefab/FileTransTipWin"));
|
||
|
FileTransTipWin.GetComponent<FileTransTipWinManager>().SetWindow(
|
||
|
"Download", files, TransferType.Download);
|
||
|
|
||
|
FileTransTipWinManager.task.TransferTaskCompleted += Task_TransferTaskCompleted;
|
||
|
}
|
||
|
|
||
|
private void Task_TransferTaskCompleted()
|
||
|
{
|
||
|
FileTransTipWinManager.task.TransferTaskCompleted -= Task_TransferTaskCompleted;
|
||
|
SetButtonActive(true);
|
||
|
HasQuestionFiles = true;
|
||
|
transform.Find("ExamTip").gameObject.SetActive(true);
|
||
|
foreach (Transform item in ParentGrid.transform)
|
||
|
{
|
||
|
if (item.Find("Toggle").GetComponent<Toggle>().isOn)
|
||
|
{
|
||
|
transform.Find("StartAnswerQuestionButton").gameObject.SetActive(true);
|
||
|
transform.Find("CheckQuestionButton").gameObject.SetActive(true);
|
||
|
transform.Find("ExamTip").gameObject.SetActive(false);
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void SetButtonActive(bool canStartExam)
|
||
|
{
|
||
|
//transform.FindChild("StartAnswerQuestionButton").gameObject.SetActive(canStartExam);
|
||
|
//transform.FindChild("CheckQuestionButton").gameObject.SetActive(canStartExam);
|
||
|
//transform.FindChild("SubmitExamButton").gameObject.SetActive(canStartExam);
|
||
|
transform.Find("StartDownloadQuestionButton").gameObject.SetActive(!canStartExam);
|
||
|
transform.Find("ExamTip").gameObject.SetActive(canStartExam);
|
||
|
}
|
||
|
public override void CheckIfHasFile(List<QuestionInfo> items)
|
||
|
{
|
||
|
if (ExamOver)
|
||
|
return;
|
||
|
//判断本地哪些考题文件没有,需要下载
|
||
|
var fileList = new List<string>();
|
||
|
for (int i = 0; i < items.Count; i++)
|
||
|
{
|
||
|
var filename = "Questions/" + items[i].ID + "/" + items[i].FilePath;
|
||
|
var filepath = ExamInfoHelpClass.CurrentWorkPath + filename;//文件完整路径
|
||
|
|
||
|
if (!File.Exists(filepath))
|
||
|
{
|
||
|
fileList.Add(filename);
|
||
|
}
|
||
|
}
|
||
|
files = fileList.ToArray();
|
||
|
if (fileList.Count > 0)
|
||
|
{
|
||
|
SetButtonActive(false);
|
||
|
HasQuestionFiles = false;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
SetButtonActive(true);
|
||
|
HasQuestionFiles = true;
|
||
|
}
|
||
|
|
||
|
//判断是否可以答题
|
||
|
}
|
||
|
|
||
|
public override void CheckIfShowSubmitButton()
|
||
|
{
|
||
|
if (ExamOver)
|
||
|
return;
|
||
|
|
||
|
var list = AnswerList.FindAll(a => { return a.FilePath.Contains(exam.ID.ToString()); });
|
||
|
if (list.Count > 0)
|
||
|
{
|
||
|
transform.Find("SubmitExamButton").gameObject.SetActive(true);
|
||
|
if (transform.Find("ExamTip").gameObject.activeInHierarchy)
|
||
|
{
|
||
|
transform.Find("ExamTip").gameObject.SetActive(false);
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
transform.Find("SubmitExamButton").gameObject.SetActive(false);
|
||
|
}
|
||
|
}
|
||
|
}
|