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; /// /// 关卡信息 /// [TableList(MaxScrollViewHeight = 20)] public List m_LevelItems; /// /// 关卡标题对应数据 /// Dictionary> tmpDicDataInfos; /// /// 目前正在进行的关卡 /// private LevelItem m_CurrentLevelItem; /// /// 闯关完成 /// private bool m_ChallengeFinish; /// /// 配置所有关卡 /// public void GetChallengeProcessElements() { List processElements = ProcessManagement.Instance.processes; m_LevelItems = new List(); 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); } /// /// 闯关开始时间 /// private DateTime m_StartTime; /// /// 闯关结束时间 /// private DateTime m_EndTime; private void Awake() { instance = this; } /// /// 初始化 /// public void Init() { ProcessManagement.Instance.processElementActiveEvent += ProcessElementActive; ProcessManagement.Instance.processElementDisActiveEvent += ProcessElementDisActive; StepListProxy tmpStepListProxy = DAL.Instance.Get(); tmpDicDataInfos = new Dictionary>(); ChallengeConfigurationManager tmpChallengeConfigurationManager = null; Dictionary> 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; } /// /// 开始/切换关卡 /// public void StartAndSwitchLevel() { foreach (var item in m_LevelItems) { if (!item.finish) { m_CurrentLevelItem = item; item.StartLeve(StartAndSwitchLevel); UIKit.OpenPanel(new TimeForCompletingLevelsPanelData() { m_time = item.time }); return; } } EndChallenge(); } /// /// 步骤进入 /// /// private void ProcessElementActive(ProcessElement element) { } /// /// 步骤退出 /// /// private void ProcessElementDisActive(ProcessElement element) { m_CurrentLevelItem.SetElementStateByFinishID(element.stepID); } /// /// 记录错误次数 /// /// 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(); if (UIKit.GetPanel()) UIKit.ClosePanel(); EndChallenge(); } } /// /// 结束闯关 /// public void EndChallenge() { if (m_ChallengeFinish) return; m_EndTime = DateTime.Now; Debug.Log("闯关结束"); m_ChallengeFinish = true; //UIKit.GetPanel().StopAllCoroutines(); UIKit.OpenPanel(); } public string GetChallengeTotalTime() { double tmpTotalSecond = ConverOldTiemAndNewTiemDuration(m_StartTime, m_EndTime); return ConversionTime((float)tmpTotalSecond); } /// /// 获取总错误次数 /// /// public int GetTotalErrorNum(bool containNoFinish = false) { int tmpErrorNmu = 0; foreach (var leveItem in m_LevelItems) { tmpErrorNmu += leveItem.GetErrorCount(containNoFinish); } return tmpErrorNmu; } /// /// 是否完成 /// /// public int IsSuccessfullyPassedTheLevel() { return m_LevelItems.FindAll(t => t.finish == false) == null ? 0 : 1; } /// /// 考试用时 /// /// 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); } /// /// 是否上传完成 /// /// 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; } } /// /// 关卡信息 /// public class LevelItem { /// /// 关卡名称 /// public string leveName; /// /// 开始时间 /// public DateTime startTime; /// /// 结束时间 /// public DateTime endTime; /// /// 闯关时间 /// public float time; /// /// 关卡包含步骤 /// public List challengeProcessElements = new List(); /// /// 目前操作步骤 /// public ChallengeProcessElement currentChallengeProcessElement; /// /// 是否完成 /// public bool finish; /// /// 完成的回调 /// private Action m_FinishCallBack; /// /// 是否步骤ID /// /// public bool ContainChallengeId(int id) { return challengeProcessElements.Find(t => t.stepInfo.id == id.ToString()) != null; } /// /// 通过完成的ID设置 /// /// 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(); } } /// /// 开启关卡 /// public void StartLeve(Action action) { startTime = DateTime.Now; endTime = DateTime.Now; m_FinishCallBack = action; SwitchToNextLevel(); } /// /// 切换下一关卡 /// 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; } /// /// 获取闯关用时 /// /// public float GetTimeConsumption() { return (float)(endTime - startTime).TotalSeconds; } /// /// 获取未开始Operation,0表示全部完成 /// /// public ChallengeProcessElement GetReadyOperation() { foreach (var item in challengeProcessElements) { if (!item.finish) { currentChallengeProcessElement = item; return item; } } return null; } /// /// 获取关卡用时 /// /// public float GetLevelTime() { return (float)(endTime - startTime).TotalSeconds; } /// /// 获取错误次数 /// /// 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; } /// /// 切换到指定步骤 /// public void StartOperationByID(ChallengeProcessElement challengeProcessElement) { ProcessManagement.Instance.JumpProcessState(int.Parse(challengeProcessElement.stepInfo.id)); ProcessManagement.Instance.InitProcess(); } } [Serializable] public class ChallengeProcessElement { /// /// 错误次数 /// public int faultCount = 0; /// /// 操作时间 /// public DateTime operationTime; /// /// 错误原因 /// public string errorReason; /// /// 截图名称 /// public List screenShotNames = new List(); /// /// 步骤信息 /// public OperationStepDataInfo stepInfo; /// /// 是否完成 /// [Sirenix.OdinInspector.ReadOnly] public bool finish = false; /// /// 正在上传的任务 /// public List m_UpLoading = new List(); }