ExamManagerForPC.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. using ChivaXR;
  2. using ChivaXR.Op;
  3. using I2.Loc;
  4. using QFramework;
  5. using Sirenix.OdinInspector;
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using UnityEngine;
  11. public enum ErrorReason
  12. {
  13. SelectWrongTool,
  14. SelectWrongTarget,
  15. AnswerWrong
  16. }
  17. public class ExamManagerForPC : MonoBehaviour
  18. {
  19. public static ExamManagerForPC instance;
  20. /// <summary>
  21. /// 考试代理
  22. /// </summary>
  23. public ExamProxy m_ExamProxy;
  24. /// <summary>
  25. /// 记录考试时长
  26. /// </summary>
  27. private int m_RecordExamDuration;
  28. /// <summary>
  29. /// 开始时间
  30. /// </summary>
  31. private DateTime m_StartTime;
  32. /// <summary>
  33. /// 结束时间
  34. /// </summary>
  35. private DateTime m_EndTime;
  36. /// <summary>
  37. /// 允许错误次数
  38. /// </summary>
  39. public int m_maxErrorCount;
  40. private ProcessElement currentProcessElement;
  41. #region 场景配置模块
  42. [BoxGroup("场景配置模块", CenterLabel = true, Order = 0), GUIColor(0, 1, 0, 0.6f)]
  43. [Button("1.获取流程信息")]
  44. public void GetEaxmProcessElements()
  45. {
  46. List<ProcessElement> processElements = ProcessManagement.Instance.processes;
  47. if (m_examProcessElements == null)
  48. {
  49. m_examProcessElements = new List<ExamProcessElement>();
  50. }
  51. ////临时转存,用户数据恢复
  52. List<ExamProcessElement> tmpExamProcessElements = m_examProcessElements;
  53. //重建考试信息列表
  54. m_examProcessElements = new List<ExamProcessElement>();
  55. foreach (var item in processElements)
  56. {
  57. ExamProcessElement examProcessElement = tmpExamProcessElements.Find(t => t.elementDescript == item.processBase.GetPBDescribe());
  58. if (examProcessElement != null)
  59. {
  60. m_examProcessElements.Add(examProcessElement);
  61. }
  62. else
  63. {
  64. StepListProxy tmpProxy = DAL.Instance.Get<StepListProxy>();
  65. OperationStepDataInfo tmpInfo = tmpProxy.GetOpStepDataInfoById(item.stepID);
  66. m_examProcessElements.Add(new ExamProcessElement()
  67. {
  68. id = item.stepID,
  69. elementDescript = item.processBase.GetPBDescribe(),
  70. stepName = tmpInfo.stepName
  71. });
  72. }
  73. }
  74. }
  75. [BoxGroup("场景配置模块", CenterLabel = true, Order = 0), GUIColor(0, 1, 0, 0.6f)]
  76. [Button("2.将所有的CurvedUI和箭头置为禁用(不影响编辑器跳步)")]
  77. public void GetCurvedUIAndDiasable()
  78. {
  79. //将环形UI置为禁用状态
  80. List<ProcessElement> processElements = ProcessManagement.Instance.processes;
  81. //将箭头提示置为禁用状态
  82. JianTouManager.Instance.SetJianTouActive(false);
  83. }
  84. [BoxGroup("场景配置模块", CenterLabel = true, Order = 0), GUIColor(0, 1, 0, 0.6f)]
  85. [Button("3、初始化流程分数")]
  86. public void SetEaxmProcessScore()
  87. {
  88. if (m_examProcessElements.Count <= 0)
  89. {
  90. return;
  91. }
  92. foreach (var item in m_examProcessElements)
  93. {
  94. if (m_examProcessElements.Count <= 100)
  95. {
  96. item.scores = 1;
  97. }
  98. else
  99. {
  100. //item.scores = 100 / examProcessElements.Count;
  101. item.scores = 0.5f;
  102. }
  103. }
  104. RefreshScore();
  105. }
  106. #endregion
  107. [Space(5)]
  108. #region 配分模块
  109. [BoxGroup("配分模块", CenterLabel = true, Order = 1)]
  110. /// <summary>
  111. /// 考试信息
  112. /// </summary>
  113. [TableList(MaxScrollViewHeight = 20)]
  114. public List<ExamProcessElement> m_examProcessElements;
  115. [BoxGroup("配分模块", CenterLabel = true, Order = 1)]
  116. [LabelText("配置总分"), GUIColor(0, 1, 1, 1)]
  117. [Sirenix.OdinInspector.ReadOnly]
  118. public float examSetTotalScore;
  119. [BoxGroup("配分模块", CenterLabel = true, Order = 1)]
  120. [Button("刷新总设定分数")]
  121. public void RefreshScore()
  122. {
  123. examSetTotalScore = 0;
  124. foreach (var item in m_examProcessElements)
  125. {
  126. examSetTotalScore += item.scores;
  127. }
  128. }
  129. #endregion
  130. [Space(5)]
  131. #region 成绩模块
  132. [BoxGroup("成绩模块", CenterLabel = true, Order = 3)]
  133. [LabelText("最终得分"), GUIColor(1, 0.92f, 0.016f, 1)]
  134. [Sirenix.OdinInspector.ReadOnly]
  135. public float score;
  136. [Space(5)]
  137. [BoxGroup("考试时长", CenterLabel = true, Order = 4)]
  138. [LabelText("考试时长(分钟)"), GUIColor(1, 0.1f, 0.016f, 1)]
  139. public int examTime;
  140. #endregion
  141. /// <summary>
  142. /// 是否上传结果
  143. /// </summary>
  144. private bool m_ResultUpload = false;
  145. /// <summary>
  146. /// 服务器下发的分数
  147. /// </summary>
  148. private float m_DistributeScores;
  149. private void Awake()
  150. {
  151. instance = this;
  152. GetEaxmProcessElements();
  153. GetCurvedUIAndDiasable();
  154. //如果没有读到考试数据自动非分/手动配分
  155. AutoEaxmProcessScore();
  156. Init();
  157. }
  158. public void Init()
  159. {
  160. ProcessManagement.Instance.processElementActiveEvent += ProcessElementActive;
  161. ProcessManagement.Instance.processElementDisActiveEvent += ProcessElementDisActive;
  162. ProcessManagement.Instance.processFinishEvent += ExamFinish;
  163. m_ExamProxy = DAL.Instance.Get<ExamProxy>();
  164. m_ExamProxy.StartExam();
  165. m_StartTime = DateTime.Now;
  166. }
  167. /// <summary>
  168. /// 步骤进入
  169. /// </summary>
  170. /// <param name="element"></param>
  171. private void ProcessElementActive(ProcessElement element)
  172. {
  173. currentProcessElement = element;
  174. }
  175. /// <summary>
  176. /// 步骤退出
  177. /// </summary>
  178. /// <param name="element"></param>
  179. private void ProcessElementDisActive(ProcessElement element)
  180. {
  181. JianTouManager.instance.SetJianTouActive(false);
  182. ExamProcessElement tmpElement = m_examProcessElements.Find(t => t.elementDescript == currentProcessElement.processBase.GetPBDescribe());
  183. if (tmpElement.finish == false)
  184. {
  185. tmpElement.finish = true;
  186. tmpElement.result = tmpElement.faultCount > 0 ? false : true;
  187. tmpElement.operationTime = DateTime.Now;
  188. }
  189. SetelementScore();
  190. }
  191. /// <summary>
  192. /// 自动配分
  193. /// </summary>
  194. public void AutoEaxmProcessScore()
  195. {
  196. if (m_examProcessElements.Count <= 0)
  197. {
  198. return;
  199. }
  200. int score = 200 / m_examProcessElements.Count;
  201. int count = 200 % m_examProcessElements.Count;
  202. for (int i = 0; i < m_examProcessElements.Count; i++)
  203. {
  204. if (count > i) m_examProcessElements[i].scores = ((float)score + 1) / 2;
  205. else m_examProcessElements[i].scores = (float)score / 2;
  206. }
  207. RefreshScore();
  208. }
  209. public float GetDistributeScores(StepInfoManager stepInfoManager)
  210. {
  211. float tmpScore = 0;
  212. for (int i = 0; i < m_examProcessElements.Count; i++)
  213. {
  214. m_examProcessElements[i].scores = stepInfoManager.S[i];
  215. }
  216. foreach (var item in stepInfoManager.S)
  217. {
  218. tmpScore += item;
  219. }
  220. RefreshScore();
  221. return tmpScore;
  222. }
  223. /// <summary>
  224. /// 根据接收到的数据进行分数填充
  225. /// </summary>
  226. public void ExamProcessScoreByReceiveData(StepInfoManager stepInfoManager)
  227. {
  228. for (int i = 0; i < m_examProcessElements.Count; i++)
  229. {
  230. m_examProcessElements[i].scores = float.Parse(stepInfoManager.S[i].ToString());
  231. }
  232. }
  233. /// <summary>
  234. /// 记录操作错误
  235. /// </summary>
  236. public void RecordFault(int processId, ErrorReason errorReason)
  237. {
  238. ExamProcessElement tmpElement = m_examProcessElements.Find(t => t.id == processId);
  239. if (tmpElement != null)
  240. {
  241. tmpElement.errorReason = errorReason.ToString();
  242. tmpElement.faultCount++;
  243. m_maxErrorCount--;
  244. UIKit.GetPanel<PC_OperatePanel>().ExamInfo.ErrorCount.text = "x" + m_maxErrorCount.ToString();
  245. if (m_maxErrorCount <= 0)
  246. {
  247. ExamFinish();
  248. }
  249. string screenShotName = DateTime.UtcNow.Ticks.ToString();
  250. Debug.Log(screenShotName);
  251. StartCoroutine(ScrrenCapture(new Rect(0, 0, Screen.width, Screen.height), 0, screenShotName, tmpElement));
  252. }
  253. LocalizedString stepName = processId + "stepName";
  254. string wrongErrson = "操作错误";
  255. switch (errorReason)
  256. {
  257. case ErrorReason.SelectWrongTool:
  258. if (LocalizationManager.CurrentLanguage == "Chinese")
  259. {
  260. wrongErrson = "工器具错误";
  261. }
  262. else
  263. {
  264. wrongErrson = "ToolError";
  265. }
  266. break;
  267. case ErrorReason.SelectWrongTarget:
  268. if (LocalizationManager.CurrentLanguage == "Chinese")
  269. {
  270. wrongErrson = "操作点错误";
  271. }
  272. else
  273. {
  274. wrongErrson = "OperationError";
  275. }
  276. break;
  277. }
  278. UIKit.GetPanel<PC_OperatePanel>().ExamInfo.ShowWrongInfo("<color=#FF9F00>" + stepName + "</color> : " + wrongErrson);
  279. }
  280. /// <summary>
  281. /// 分数结算
  282. /// </summary>
  283. private float SetelementScore()
  284. {
  285. score = 0;
  286. foreach (var item in m_examProcessElements)
  287. {
  288. if (item.finish == true && item.result == true)
  289. {
  290. score += item.scores;
  291. }
  292. }
  293. //StopAllCoroutines();
  294. return score;
  295. }
  296. /// <summary>
  297. /// 上传考核结果
  298. /// </summary>
  299. public void UploadExamResult()
  300. {
  301. ExamProxy examProxy = DAL.Instance.Get<ExamProxy>();
  302. examProxy.UpLoadExamResult(SetelementScore());
  303. m_ResultUpload = true;
  304. }
  305. /// <summary>
  306. /// 成绩展示
  307. /// </summary>
  308. public void ExamFinish()
  309. {
  310. m_EndTime = DateTime.Now;
  311. CountExamDuration();
  312. if (UIKit.GetPanel<PC_OperatePanel>())
  313. {
  314. UIKit.GetPanel<PC_OperatePanel>().ExamEnd(m_examProcessElements, SetelementScore(), m_RecordExamDuration);
  315. }
  316. ProcessManagement.Instance.StopCurrentProcess();
  317. if (!m_ResultUpload)
  318. {
  319. UploadExamResult();
  320. }
  321. if (UIKit.GetPanel<ArrowTipsPanel>())
  322. {
  323. UIKit.GetPanel<ArrowTipsPanel>().Clear();
  324. }
  325. }
  326. /// <summary>
  327. /// 统计考试时长
  328. /// </summary>
  329. private void CountExamDuration()
  330. {
  331. // 记录结束时的时间
  332. DateTime finishTime = DateTime.Now;
  333. m_RecordExamDuration = (int)(finishTime - m_ExamProxy.startTime).TotalSeconds;
  334. }
  335. private void OnDisable()
  336. {
  337. ProcessManagement.Instance.processElementActiveEvent -= ProcessElementActive;
  338. ProcessManagement.Instance.processElementActiveEvent -= ProcessElementDisActive;
  339. ProcessManagement.Instance.processFinishEvent -= ExamFinish;
  340. }
  341. Texture2D screenShot;//保存截取的纹理
  342. IEnumerator ScrrenCapture(Rect rect, int a, string screenShotName, ExamProcessElement examProcessElement)
  343. {
  344. Debug.Log("截图:" + screenShotName);
  345. screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
  346. yield return new WaitForSeconds(0.5f);
  347. yield return new WaitForEndOfFrame();
  348. screenShot.ReadPixels(rect, 0, 0);
  349. screenShot.Apply();
  350. //推送
  351. //PushHelper.InitBasicData();
  352. //StartCoroutine(PushHelper.SendFileStreamPushRequest(screenShot, screenShotName, (result, msg, flag, cmnAttachId) =>
  353. //{
  354. // if (!result)
  355. // {
  356. // Debug.LogError(msg);
  357. // }
  358. // else
  359. // {
  360. // Debug.Log("考试模块图片上传成功" + cmnAttachId.data.cmnAttachId);
  361. // examProcessElement.screenshotList.Add(cmnAttachId.data.cmnAttachId);
  362. // }
  363. // examProcessElement.m_UpLoading.Remove(screenShotName);
  364. //}));
  365. examProcessElement.m_UpLoading.Add(screenShotName);
  366. }
  367. /// <summary>
  368. /// 获取该ID的分数
  369. /// </summary>
  370. /// <param name="id"></param>
  371. /// <returns></returns>
  372. public float GetScoreById(int id)
  373. {
  374. float tmpScore = 0;
  375. //PullProxy tmpPullProxy = DAL.Instance.Get<PullProxy>();
  376. //StepInfoManager tmpStepInfoManager = tmpPullProxy.GetExamStepInfoManager();
  377. //if (tmpStepInfoManager == null)
  378. //{
  379. // tmpScore = m_examProcessElements[id - 1].scores;
  380. //}
  381. //else
  382. //{
  383. // tmpScore = tmpStepInfoManager.S[id - 1];
  384. //}
  385. return tmpScore;
  386. }
  387. /// <summary>
  388. /// 获取总分比值
  389. /// </summary>
  390. public float GetTotalScore()
  391. {
  392. float tmpTotalScore = 0;
  393. foreach (var item in m_examProcessElements)
  394. {
  395. if (item.finish && item.result)
  396. {
  397. tmpTotalScore += item.scores;
  398. }
  399. }
  400. if (m_DistributeScores == 0)
  401. {
  402. return tmpTotalScore / examSetTotalScore;
  403. }
  404. else
  405. {
  406. float ratio = (float)tmpTotalScore / (float)examSetTotalScore;
  407. return ratio;
  408. }
  409. }
  410. /// <summary>
  411. /// 是否上传完成
  412. /// </summary>
  413. /// <returns></returns>
  414. public bool IsFinishUpLoad()
  415. {
  416. foreach (var item in m_examProcessElements)
  417. {
  418. if (item.m_UpLoading.Count > 0)
  419. {
  420. return false;
  421. }
  422. }
  423. return true;
  424. }
  425. }