using Newtonsoft.Json; using QFramework; using System; using System.Collections.Generic; using TMPro; using UnityEngine; using XMLTool; public class ScoreController : MonoSingleton { public class Data { public Dictionary scoreDict; } public Dictionary moduleDict; public override void OnSingletonInit() { base.OnSingletonInit(); InitData(); } public void Init() { if (Global.Instance.curModule.type == "Exam" || Global.Instance.curModule.type == "All") { TypeEventSystem.Global.Register(OnStart); TypeEventSystem.Global.Register(OnQuit); } } public void InitData() { moduleDict = new Dictionary(); foreach (var item in Global.Instance.appData.Modules) { if (item.type == "Exam" || item.type == "All") { Data data = new Data() { scoreDict = new Dictionary() }; moduleDict.Add(item.ModuleName, data); } } } public string GetModuleDictJson(string name = "") { var resultDict = new Dictionary(); resultDict.Add("name", name); foreach (var dataPair in moduleDict) { var data = dataPair.Value; if (data.scoreDict != null) { foreach (var scoreStepPair in data.scoreDict) { var scoreStep = scoreStepPair.Value; string key = $"{scoreStep.step}{scoreStep.name}"; resultDict[key] = scoreStep.value; resultDict.Add(key + "Time", string.IsNullOrEmpty(scoreStep.time) == true ? "" : scoreStep.time); } } } return JsonConvert.SerializeObject(resultDict); } private void OnStart(OnModuleStart start) { var data = moduleDict[Global.Instance.curModule.ModuleName]; data.scoreDict.Clear(); foreach (var item in Global.Instance.curModule.score.scores) { item.value = 0; data.scoreDict.Add(item.step + item.name, item); } } public Dictionary GetCurScoreData() { var data = moduleDict[Global.Instance.curModule.ModuleName]; return data.scoreDict; } public void Add(string key, float value) { var data = moduleDict[Global.Instance.curModule.ModuleName]; var scoreDict = data.scoreDict; if (scoreDict.ContainsKey(key)) { scoreDict[key].value += value; var sumScore = float.Parse(scoreDict[key].sum); if (scoreDict[key].value > sumScore) { scoreDict[key].value = sumScore; } if (scoreDict[key].value <= 0) { scoreDict[key].value = 0; } DateTime currentTime = DateTime.Now; scoreDict[key].time = currentTime.ToString(scoreDict[key].timeFormat); } else { Debug.LogError("没有找到 评分点:" + key); } } public void Set(string key, float value) { var data = moduleDict[Global.Instance.curModule.ModuleName]; var scoreDict = data.scoreDict; if (scoreDict.ContainsKey(key)) { scoreDict[key].value = value; } } /// /// 获取当前所得总分 /// public float GetCurScore() { float score = 0; var data = moduleDict[Global.Instance.curModule.ModuleName]; foreach (var item in data.scoreDict) { score += item.Value.value; } return score; } private void OnQuit(OnModuleQuit quit) { TypeEventSystem.Global.UnRegister(OnStart); TypeEventSystem.Global.UnRegister(OnQuit); } }