2025-04-21 09:59:18 +08:00

156 lines
4.0 KiB
C#
Raw Blame History

using Newtonsoft.Json;
using QFramework;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using TMPro;
using UnityEngine;
using UnityEngine.SocialPlatforms.Impl;
using XMLTool;
public class ScoreController : MonoSingleton<ScoreController>
{
public class Data
{
public Dictionary<string, ScoreStep> scoreDict;
}
public Dictionary<string, Data> 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<OnModuleStart>(OnStart);
TypeEventSystem.Global.Register<OnModuleQuit>(OnQuit);
}
}
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);
}
}
}
public string GetModuleDictJson(string name = "")
{
var resultDict = new Dictionary<string, object>();
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<string, ScoreStep> 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(<><C3BB><EFBFBD>ҵ<EFBFBD> <20><><EFBFBD>ֵ㣺" + 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;
}
}
/// <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;
}
private void OnQuit(OnModuleQuit quit)
{
TypeEventSystem.Global.UnRegister<OnModuleStart>(OnStart);
TypeEventSystem.Global.UnRegister<OnModuleQuit>(OnQuit);
}
}