164 lines
4.4 KiB
C#
Raw Normal View History

using Newtonsoft.Json;
2024-12-14 18:27:59 +08:00
using QFramework;
using System;
using System.Collections.Generic;
2025-03-25 10:46:22 +08:00
using TMPro;
2024-12-14 18:27:59 +08:00
using UnityEngine;
using XMLTool;
public class ScoreController : MonoSingleton<ScoreController>
{
2025-01-19 11:20:01 +08:00
public class Data
{
public Dictionary<string, ScoreStep> scoreDict;
}
public Dictionary<string, Data> moduleDict;
2024-12-14 18:27:59 +08:00
public override void OnSingletonInit()
{
base.OnSingletonInit();
2025-01-19 11:20:01 +08:00
InitData();
2025-01-19 13:33:25 +08:00
}
public void Init()
{
if (Global.Instance.curModule.type == "Exam" || Global.Instance.curModule.type == "All")
2025-01-19 13:33:25 +08:00
{
2025-02-11 16:42:39 +08:00
TypeEventSystem.Global.Register<OnModuleStart>(OnStart);
TypeEventSystem.Global.Register<OnModuleQuit>(OnQuit);
2025-01-19 13:33:25 +08:00
}
2025-02-11 16:42:39 +08:00
2024-12-14 18:27:59 +08:00
}
2025-01-19 11:20:01 +08:00
public void InitData()
{
moduleDict = new Dictionary<string, Data>();
foreach (var item in Global.Instance.appData.Modules)
{
if (item.type == "Exam" || item.type == "All")
{
Data data = new Data() { scoreDict = new Dictionary<string, ScoreStep>() };
moduleDict.Add(item.ModuleName, data);
}
}
}
2025-04-27 14:00:35 +08:00
public string GetModuleDictJson(string name = "", string id = "", string sum = "")
{
var resultDict = new Dictionary<string, object>();
2025-04-27 14:00:35 +08:00
DateTime curTime = DateTime.Now;
resultDict.Add("name", name);
2025-04-27 14:00:35 +08:00
resultDict.Add("id", id);
resultDict.Add("sum", sum);
resultDict.Add("time", curTime.ToString("yyyy.MM.dd"));
resultDict.Add("year", curTime.Year);
resultDict.Add("month", curTime.Month);
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;
2025-04-27 14:00:35 +08:00
string value = "\u00A0";
if (string.IsNullOrEmpty(scoreStep.time) == false)
{
value = scoreStep.time;
}
resultDict.Add(key + "Time", value);
}
}
}
return JsonConvert.SerializeObject(resultDict);
}
2025-01-19 11:20:01 +08:00
2024-12-14 18:27:59 +08:00
private void OnStart(OnModuleStart start)
{
2025-01-19 11:20:01 +08:00
var data = moduleDict[Global.Instance.curModule.ModuleName];
data.scoreDict.Clear();
2024-12-14 18:27:59 +08:00
foreach (var item in Global.Instance.curModule.score.scores)
{
item.value = 0;
2025-01-19 11:20:01 +08:00
data.scoreDict.Add(item.step + item.name, item);
2024-12-14 18:27:59 +08:00
}
2025-01-19 11:20:01 +08:00
2024-12-14 18:27:59 +08:00
}
2025-01-19 11:20:01 +08:00
2025-03-25 10:46:22 +08:00
public Dictionary<string, ScoreStep> GetCurScoreData()
2025-01-19 11:20:01 +08:00
{
var data = moduleDict[Global.Instance.curModule.ModuleName];
return data.scoreDict;
}
2024-12-14 18:27:59 +08:00
public void Add(string key, float value)
{
2025-01-19 11:20:01 +08:00
var data = moduleDict[Global.Instance.curModule.ModuleName];
var scoreDict = data.scoreDict;
2024-12-14 18:27:59 +08:00
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);
2024-12-14 18:27:59 +08:00
}
2025-02-28 14:58:52 +08:00
else
{
Debug.LogError(<><C3BB><EFBFBD>ҵ<EFBFBD> <20><><EFBFBD>ֵ㣺" + key);
}
2024-12-14 18:27:59 +08:00
}
public void Set(string key, float value)
{
2025-01-19 11:20:01 +08:00
var data = moduleDict[Global.Instance.curModule.ModuleName];
var scoreDict = data.scoreDict;
2024-12-14 18:27:59 +08:00
if (scoreDict.ContainsKey(key))
{
scoreDict[key].value = value;
}
}
2025-03-25 10:46:22 +08:00
/// <summary>
/// <20><>ȡ<EFBFBD><C8A1>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>ܷ<EFBFBD>
/// </summary>
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;
}
2024-12-14 18:27:59 +08:00
private void OnQuit(OnModuleQuit quit)
{
2025-01-19 13:33:25 +08:00
TypeEventSystem.Global.UnRegister<OnModuleStart>(OnStart);
TypeEventSystem.Global.UnRegister<OnModuleQuit>(OnQuit);
2024-12-14 18:27:59 +08:00
}
}