123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527 |
- using ChivaXR;
- using JetBrains.Annotations;
- using Org.BouncyCastle.Operators;
- using QFramework;
- using Sirenix.OdinInspector;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Networking.Types;
- using UnityEngine.TextCore;
- public class ChallengeManagerForPC : MonoBehaviour
- {
- public static ChallengeManagerForPC instance;
- /// <summary>
- /// 关卡信息
- /// </summary>
- [TableList(MaxScrollViewHeight = 20)]
- public List<LevelItem> m_LevelItems;
- /// <summary>
- /// 关卡标题对应数据
- /// </summary>
- Dictionary<LeveInfo, List<OperationStepDataInfo>> tmpDicDataInfos;
- /// <summary>
- /// 目前正在进行的关卡
- /// </summary>
- private LevelItem m_CurrentLevelItem;
- /// <summary>
- /// 闯关完成
- /// </summary>
- private bool m_ChallengeFinish;
- /// <summary>
- /// 配置所有关卡
- /// </summary>
- public void GetChallengeProcessElements()
- {
- List<ProcessElement> processElements = ProcessManagement.Instance.processes;
- m_LevelItems = new List<LevelItem>();
- foreach (var item in tmpDicDataInfos)
- {
- LevelItem tmpLevelItem = m_LevelItems.Find(t => t.leveName == item.Key.leveName);
- if (tmpLevelItem == null) tmpLevelItem = new LevelItem() { leveName = item.Key.leveName, time = float.Parse(item.Key.leveTime) };
- foreach (var opDataInfoItem in item.Value)
- {
- ChallengeProcessElement tmpChallengeProcessElement = new ChallengeProcessElement()
- {
- stepInfo = opDataInfoItem,
- };
- tmpLevelItem.challengeProcessElements.Add(tmpChallengeProcessElement);
- }
- m_LevelItems.Add(tmpLevelItem);
- }
- Debug.Log(m_LevelItems.Count);
- }
- /// <summary>
- /// 闯关开始时间
- /// </summary>
- private DateTime m_StartTime;
- /// <summary>
- /// 闯关结束时间
- /// </summary>
- private DateTime m_EndTime;
- private void Awake()
- {
- instance = this;
- }
- /// <summary>
- /// 初始化
- /// </summary>
- public void Init()
- {
- ProcessManagement.Instance.processElementActiveEvent += ProcessElementActive;
- ProcessManagement.Instance.processElementDisActiveEvent += ProcessElementDisActive;
- StepListProxy tmpStepListProxy = DAL.Instance.Get<StepListProxy>();
- tmpDicDataInfos = new Dictionary<LeveInfo, List<OperationStepDataInfo>>();
- ChallengeConfigurationManager tmpChallengeConfigurationManager = null;
- Dictionary<string, List<OperationStepDataInfo>> tmpDicOperationStepDataInfos = tmpStepListProxy.ProcessingData();
- foreach (var item in tmpDicOperationStepDataInfos)
- {
- LeveInfo tmpLeveInfo = new LeveInfo();
- tmpLeveInfo.leveTime = 3.ToString();
- tmpLeveInfo.leveName = item.Key.ToString();
- tmpDicDataInfos.Add(tmpLeveInfo, item.Value);
- }
- GetChallengeProcessElements();
- StartAndSwitchLevel();
- m_StartTime = DateTime.Now;
- m_EndTime = DateTime.Now;
- }
- /// <summary>
- /// 开始/切换关卡
- /// </summary>
- public void StartAndSwitchLevel()
- {
- foreach (var item in m_LevelItems)
- {
- if (!item.finish)
- {
- m_CurrentLevelItem = item;
- item.StartLeve(StartAndSwitchLevel);
- UIKit.OpenPanel<TimeForCompletingLevelsPanel>(new TimeForCompletingLevelsPanelData() { m_time = item.time });
- return;
- }
- }
- EndChallenge();
- }
- /// <summary>
- /// 步骤进入
- /// </summary>
- /// <param name="element"></param>
- private void ProcessElementActive(ProcessElement element)
- {
- }
- /// <summary>
- /// 步骤退出
- /// </summary>
- /// <param name="element"></param>
- private void ProcessElementDisActive(ProcessElement element)
- {
- m_CurrentLevelItem.SetElementStateByFinishID(element.stepID);
- }
- /// <summary>
- /// 记录错误次数
- /// </summary>
- /// <param name="fault"></param>
- public void RecordFault(int faultID)
- {
- if (m_ChallengeFinish) return;
- ChallengeProcessElement tmpElement = m_CurrentLevelItem.challengeProcessElements.Find(t => t.stepInfo.id == faultID.ToString());
- if (tmpElement != null)
- {
- tmpElement.faultCount++;
- tmpElement.errorReason = "操作错误";
- }
- string screenShotName = DateTime.UtcNow.Ticks.ToString();
- StartCoroutine(ScrrenCapture(new Rect(0, 0, Screen.width, Screen.height), 0, screenShotName, tmpElement));
- //大于3次结束考试
- if (GetTotalErrorNum(false) >= 3)
- {
- UIKit.ClosePanel<PranticePromptPanel>();
- if (UIKit.GetPanel<TimeForCompletingLevelsPanel>()) UIKit.ClosePanel<TimeForCompletingLevelsPanel>();
- EndChallenge();
- }
- }
- /// <summary>
- /// 结束闯关
- /// </summary>
- public void EndChallenge()
- {
- if (m_ChallengeFinish) return;
- m_EndTime = DateTime.Now;
- Debug.Log("<color=yellow>闯关结束</color>");
- m_ChallengeFinish = true;
- //UIKit.GetPanel<TimeForCompletingLevelsPanel>().StopAllCoroutines();
- UIKit.OpenPanel<ChallengeResultPanel>();
- }
- public string GetChallengeTotalTime()
- {
- double tmpTotalSecond = ConverOldTiemAndNewTiemDuration(m_StartTime, m_EndTime);
- return ConversionTime((float)tmpTotalSecond);
- }
- /// <summary>
- /// 获取总错误次数
- /// </summary>
- /// <returns></returns>
- public int GetTotalErrorNum(bool containNoFinish = false)
- {
- int tmpErrorNmu = 0;
- foreach (var leveItem in m_LevelItems)
- {
- tmpErrorNmu += leveItem.GetErrorCount(containNoFinish);
- }
- return tmpErrorNmu;
- }
- /// <summary>
- /// 是否完成
- /// </summary>
- /// <returns></returns>
- public int IsSuccessfullyPassedTheLevel()
- {
- return m_LevelItems.FindAll(t => t.finish == false) == null ? 0 : 1;
- }
- /// <summary>
- /// 考试用时
- /// </summary>
- /// <param name="time"></param>
- public string ConversionTime(float time)
- {
- TimeSpan ts = new TimeSpan(0, 0, Convert.ToInt32(time));
- string str = "";
- if (ts.Hours > 0)
- {
- str = String.Format("{0:00}", ts.Hours) + ":" + String.Format("{0:00}", ts.Minutes) + ":" + String.Format("{0:00}", ts.Seconds);
- }
- if (ts.Hours == 0 && ts.Minutes > 0)
- {
- str = "00:" + String.Format("{0:00}", ts.Minutes) + ":" + String.Format("{0:00}", ts.Seconds);
- }
- if (ts.Hours == 0 && ts.Minutes == 0)
- {
- str = "00:00:" + String.Format("{0:00}", ts.Seconds);
- }
- return str;
- }
- public double ConverOldTiemAndNewTiemDuration(System.DateTime oldTime, System.DateTime newTime)
- {
- System.TimeSpan ts1 = new System.TimeSpan(oldTime.Ticks);
- System.TimeSpan ts2 = new System.TimeSpan(newTime.Ticks);
- System.TimeSpan tsSub = ts1.Subtract(ts2).Duration();
- return tsSub.TotalSeconds;
- }
- Texture2D screenShot;//保存截取的纹理
- IEnumerator ScrrenCapture(Rect rect, int a, string screenShotName, ChallengeProcessElement challengeProcessElement)
- {
- screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
- yield return new WaitForSeconds(0.5f);
- yield return new WaitForEndOfFrame();
- screenShot.ReadPixels(rect, 0, 0);
- screenShot.Apply();
- ////推送
- //PushHelper.InitBasicData();
- //StartCoroutine(PushHelper.SendFileStreamPushRequest(screenShot, screenShotName, (result, msg, flag, cmnAttachId) =>
- // {
- // if (!result)
- // {
- // Debug.LogError(msg);
- // }
- // else
- // {
- // challengeProcessElement.screenShotNames.Add(cmnAttachId.data.cmnAttachId);
- // }
- // challengeProcessElement.m_UpLoading.Remove(screenShotName);
- // }));
- challengeProcessElement.m_UpLoading.Add(screenShotName);
- }
- /// <summary>
- /// 是否上传完成
- /// </summary>
- /// <returns></returns>
- public bool IsFinishUpLoad()
- {
- foreach (var leveItem in m_LevelItems)
- {
- foreach (var item in leveItem.challengeProcessElements)
- {
- if (item.m_UpLoading.Count > 0)
- {
- return false;
- }
- }
- }
- return true;
- }
- }
- /// <summary>
- /// 关卡信息
- /// </summary>
- public class LevelItem
- {
- /// <summary>
- /// 关卡名称
- /// </summary>
- public string leveName;
- /// <summary>
- /// 开始时间
- /// </summary>
- public DateTime startTime;
- /// <summary>
- /// 结束时间
- /// </summary>
- public DateTime endTime;
- /// <summary>
- /// 闯关时间
- /// </summary>
- public float time;
- /// <summary>
- /// 关卡包含步骤
- /// </summary>
- public List<ChallengeProcessElement> challengeProcessElements = new List<ChallengeProcessElement>();
- /// <summary>
- /// 目前操作步骤
- /// </summary>
- public ChallengeProcessElement currentChallengeProcessElement;
- /// <summary>
- /// 是否完成
- /// </summary>
- public bool finish;
- /// <summary>
- /// 完成的回调
- /// </summary>
- private Action m_FinishCallBack;
- /// <summary>
- /// 是否步骤ID
- /// </summary>
- /// <returns></returns>
- public bool ContainChallengeId(int id)
- {
- return challengeProcessElements.Find(t => t.stepInfo.id == id.ToString()) != null;
- }
- /// <summary>
- /// 通过完成的ID设置
- /// </summary>
- /// <param name="id"></param>
- public void SetElementStateByFinishID(int id)
- {
- ChallengeProcessElement tmpElement = challengeProcessElements.Find(t => t.stepInfo.id == id.ToString());
- if (tmpElement != null)
- {
- tmpElement.operationTime = DateTime.Now;
- tmpElement.finish = true;
- SwitchToNextLevel();
- }
- }
- /// <summary>
- /// 开启关卡
- /// </summary>
- public void StartLeve(Action action)
- {
- startTime = DateTime.Now;
- endTime = DateTime.Now;
- m_FinishCallBack = action;
- SwitchToNextLevel();
- }
- /// <summary>
- /// 切换下一关卡
- /// </summary>
- public void SwitchToNextLevel()
- {
- ChallengeProcessElement tmpChallengeProcessElement = GetReadyOperation();
- if (tmpChallengeProcessElement == null)
- {
- finish = true;
- endTime = DateTime.Now;
- m_FinishCallBack?.Invoke();
- Debug.LogError("当前关卡" + leveName + "已经完成" + ":" + "用时" + ConverOldTiemAndNewTiemDuration(startTime, endTime));
- }
- else
- {
- StartOperationByID(tmpChallengeProcessElement);
- }
- }
- public double ConverOldTiemAndNewTiemDuration(System.DateTime oldTime, System.DateTime newTime)
- {
- System.TimeSpan ts1 = new System.TimeSpan(oldTime.Ticks);
- System.TimeSpan ts2 = new System.TimeSpan(newTime.Ticks);
- System.TimeSpan tsSub = ts1.Subtract(ts2).Duration();
- return tsSub.TotalSeconds;
- }
- /// <summary>
- /// 获取闯关用时
- /// </summary>
- /// <returns></returns>
- public float GetTimeConsumption()
- {
- return (float)(endTime - startTime).TotalSeconds;
- }
- /// <summary>
- /// 获取未开始Operation,0表示全部完成
- /// </summary>
- /// <returns></returns>
- public ChallengeProcessElement GetReadyOperation()
- {
- foreach (var item in challengeProcessElements)
- {
- if (!item.finish)
- {
- currentChallengeProcessElement = item;
- return item;
- }
- }
- return null;
- }
- /// <summary>
- /// 获取关卡用时
- /// </summary>
- /// <returns></returns>
- public float GetLevelTime()
- {
- return (float)(endTime - startTime).TotalSeconds;
- }
-
- /// <summary>
- /// 获取错误次数
- /// </summary>
- /// <returns></returns>
- public int GetErrorCount(bool containNoFinish)
- {
- int tmpError = 0;
- foreach (var item in challengeProcessElements)
- {
- if (containNoFinish && item.faultCount == 0&&!item.finish)
- {
- tmpError += 1;
- }
- else
- {
- //tmpError += 1;
- //错误超出1次都算1次
- tmpError += Mathf.Clamp(item.faultCount, 0, 1);
- }
- }
- return tmpError;
- }
- /// <summary>
- /// 切换到指定步骤
- /// </summary>
- public void StartOperationByID(ChallengeProcessElement challengeProcessElement)
- {
- ProcessManagement.Instance.JumpProcessState(int.Parse(challengeProcessElement.stepInfo.id));
- ProcessManagement.Instance.InitProcess();
- }
- }
- [Serializable]
- public class ChallengeProcessElement
- {
- /// <summary>
- /// 错误次数
- /// </summary>
- public int faultCount = 0;
- /// <summary>
- /// 操作时间
- /// </summary>
- public DateTime operationTime;
- /// <summary>
- /// 错误原因
- /// </summary>
- public string errorReason;
- /// <summary>
- /// 截图名称
- /// </summary>
- public List<string> screenShotNames = new List<string>();
- /// <summary>
- /// 步骤信息
- /// </summary>
- public OperationStepDataInfo stepInfo;
- /// <summary>
- /// 是否完成
- /// </summary>
- [Sirenix.OdinInspector.ReadOnly]
- public bool finish = false;
- /// <summary>
- /// 正在上传的任务
- /// </summary>
- public List<string> m_UpLoading = new List<string>();
- }
|