using DragonLi.Core; using Mirror; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using XPlugin.Data.JsonLiteDB; using static XPlugin.Data.JsonLiteDB.JsonLiteDB; public class TitleConditionInfoss { public Dictionary TitleConditionInfo; } public class StatisticManager : NetworkBehaviour { public static StatisticManager Ins { get; private set; } public int playerIndex; [SyncVar] public string titleName; public List lastPositions; public List totalDistanceMoveds; // json���ݿ� private JsonLiteDB DB; private void Awake() { Ins = this; } public SyncDictionary statisticDescInfo = new SyncDictionary(); private void Start() { // DB = new JsonLiteDB(); // DB.Load(Resources.Load("MechanicalAgeData").text); // TableReader titleConditionInfo = DB["TitleConditionInfo"].GetReader(); // while (titleConditionInfo.Read()) // { // TitleConditionInfo info = new TitleConditionInfo(titleConditionInfo); // titleConditionDescInfos.Add(info.Id, info); // } } /// /// ���ؾ������Score���Եļ� /// /// public int GetMaxScoreKey() { // ����ֵ�Ϊ�գ�����Ĭ��ֵ if (statisticDescInfo.Count == 0) { return default; } // ʹ�� LINQ ��ѯ������ StatisticInfo ����� Score ���Խ��н������� var sortedDictionary = statisticDescInfo.OrderByDescending(kvp => kvp.Value.score); return sortedDictionary.First().Key; } /// /// ���ؾ�����СScore���Եļ� /// /// public int GetMinScoreKey() { if (statisticDescInfo.Count < 2) return -1; if (statisticDescInfo.Count == 0) { Debug.LogError("Dictionary is empty!"); return default; } // ʹ�� LINQ ��ѯ������ StatisticInfo ����� Score ���Խ����������� var sortedDictionary = statisticDescInfo.OrderBy(kvp => kvp.Value.score); // ���������ĵ�һ��������������С Score ���Եļ� return sortedDictionary.First().Key; } public int GetMaxGetPropAmountKey() { if (statisticDescInfo.Count == 0) { Debug.LogError("Dictionary is empty!"); return default; } // ʹ�� LINQ ��ѯ������ StatisticInfo ����� Score ���Խ����������� var sortedDictionary = statisticDescInfo.OrderByDescending(kvp => kvp.Value.getPropAmount); // ���������ĵ�һ��������������С Score ���Եļ� return sortedDictionary.First().Key; } /// /// �ƶ�������Զ /// /// public int GetMaxFarthestDstanceKey() { if (statisticDescInfo.Count == 0) { Debug.LogError("Dictionary is empty!"); return default; } // ʹ�� LINQ ��ѯ������ StatisticInfo ����� Score ���Խ����������� var sortedDictionary = statisticDescInfo.OrderByDescending(kvp => kvp.Value.totalMoveDis); // ���������ĵ�һ��������������С Score ���Եļ� return sortedDictionary.First().Key; } public int GetMaxHitRateKey() { if (statisticDescInfo.Count == 0) { Debug.LogError("Dictionary is empty!"); return default; } // ʹ�� LINQ ��ѯ������ StatisticInfo ����� Score ���Խ����������� var sortedDictionary = statisticDescInfo.OrderByDescending(kvp => kvp.Value.hitRate); // ���������ĵ�һ��������������С Score ���Եļ� return sortedDictionary.First().Key; } /// /// �����ǵڶ����Ķ��� /// /// public bool IsDoubleScoreToSecondPlayer() { if (statisticDescInfo.Count < 2) return false; if (statisticDescInfo.Count == 0) { Debug.LogError("Dictionary is empty!"); return default; } // ʹ�� LINQ ��ѯ������ StatisticInfo ����� Score ���Խ����������� var sortedDictionary = statisticDescInfo.OrderByDescending(kvp => kvp.Value.score).ToArray(); if (sortedDictionary[1].Value.score * 2 >= sortedDictionary[0].Value.score) { return true; } return false; } public void SetTitle() { //�÷������ҵ�index int maxKey = GetMaxScoreKey(); int minKey = GetMinScoreKey(); int maxGetPropAmountKey = GetMaxGetPropAmountKey(); int farthestDstanceKey = GetMaxFarthestDstanceKey(); int maxHitRate = GetMaxHitRateKey(); switch (StoryManager.Ins.gameDifficulty) { case 0: case 1: GameManager.Ins.titleConditionDescInfos[(int)TitleConditionType.SimpleMode].IsReach = true; break; case 2: GameManager.Ins.titleConditionDescInfos[(int)TitleConditionType.BegForAbuseMode].IsReach = true; break; } if (GameManager.Ins.isWithinTime) { GameManager.Ins.titleConditionDescInfos[(int)TitleConditionType.CompleteGame].IsReach = true; } else { GameManager.Ins.titleConditionDescInfos[(int)TitleConditionType.FailGame].IsReach = true; } if (playerIndex == maxKey) { GameManager.Ins.titleConditionDescInfos[(int)TitleConditionType.MaxScore].IsReach = true; if (IsDoubleScoreToSecondPlayer()) { GameManager.Ins.titleConditionDescInfos[(int)TitleConditionType.DoubleScoreToSecondPlayer].IsReach = true; } } if (playerIndex == minKey) { GameManager.Ins.titleConditionDescInfos[(int)TitleConditionType.MinScore].IsReach = true; } if (playerIndex == maxGetPropAmountKey) { GameManager.Ins.titleConditionDescInfos[(int)TitleConditionType.GetPropMax].IsReach = true; } if (playerIndex == farthestDstanceKey) { GameManager.Ins.titleConditionDescInfos[(int)TitleConditionType.MoveDistanceMax].IsReach = true; } if (playerIndex == maxHitRate) { GameManager.Ins.titleConditionDescInfos[(int)TitleConditionType.MoveDistanceMax].IsReach = true; } List TitleKeys = new List(); //foreach (var item in titleConditionDescInfos) //{ // Debug.Log($"{item.Key }:" + $"{item.Value.IsReach }:"); //} for (int i = 0; i < GameManager.Ins.TitleDescInfos.Count; i++) { int[] conditionIds = GameManager.Ins.TitleDescInfos[i].ConditionId; bool isOk = conditionIds.All(element => GameManager.Ins.titleConditionDescInfos[element].IsReach); if (isOk) { TitleKeys.Add(GameManager.Ins.TitleDescInfos[i].Id); } } for (int i = 0; i < TitleKeys.Count; i++) { Debug.Log("SADASDA" + TitleKeys[i]); } int randomIndex = Random.Range(0, TitleKeys.Count); // �������ѡ���Ԫ�� // titleName = GameManager.Ins.TitleDescInfos[TitleKeys[randomIndex]].TitleName; } [Server] public void AddStatisticInfo(int index) { if (!statisticDescInfo.ContainsKey(index)) { statisticDescInfo.Add(index, new StatisticInfo()); } } public void AddScore(int index, float damage) { int _shootTime = statisticDescInfo[index].shootTime; float _score = statisticDescInfo[index].score; int _ranking = statisticDescInfo[index].ranking; int _hitRate = statisticDescInfo[index].hitRate; float _totalMoveDis = statisticDescInfo[index].totalMoveDis; int _getPropAmount = statisticDescInfo[index].getPropAmount; _score += damage / 100; statisticDescInfo[index] = new StatisticInfo { score = _score, ranking = _ranking, shootTime = _shootTime, hitRate = _hitRate, totalMoveDis = _totalMoveDis, getPropAmount = _getPropAmount }; } [Server] public void AddShootTime(int index) { int _shootTime = statisticDescInfo[index].shootTime; float _score = statisticDescInfo[index].score; int _ranking = statisticDescInfo[index].ranking; int _hitRate = statisticDescInfo[index].hitRate; float _totalMoveDis = statisticDescInfo[index].totalMoveDis; int _getPropAmount = statisticDescInfo[index].getPropAmount; _shootTime++; statisticDescInfo[index] = new StatisticInfo { score = _score, ranking = _ranking, shootTime = _shootTime, hitRate = _hitRate, totalMoveDis = _totalMoveDis, getPropAmount = _getPropAmount }; } public void AddHitRate(int index) { int _shootTime = statisticDescInfo[index].shootTime; float _score = statisticDescInfo[index].score; int _ranking = statisticDescInfo[index].ranking; int _hitRate = statisticDescInfo[index].hitRate; float _totalMoveDis = statisticDescInfo[index].totalMoveDis; int _getPropAmount = statisticDescInfo[index].getPropAmount; _hitRate++; statisticDescInfo[index] = new StatisticInfo { score = _score, ranking = _ranking, shootTime = _shootTime, hitRate = _hitRate, totalMoveDis = _totalMoveDis, getPropAmount = _getPropAmount }; } [Server] public void SetTotalMoveDis(int index, float value) { int _shootTime = statisticDescInfo[index].shootTime; float _score = statisticDescInfo[index].score; int _ranking = statisticDescInfo[index].ranking; int _hitRate = statisticDescInfo[index].hitRate; int _getPropAmount = statisticDescInfo[index].getPropAmount; float _totalMoveDis = value; statisticDescInfo[index] = new StatisticInfo { score = _score, ranking = _ranking, shootTime = _shootTime, hitRate = _hitRate, totalMoveDis = _totalMoveDis, getPropAmount = _getPropAmount }; } [Server] public void AddGetPropAmount(int index) { int _shootTime = statisticDescInfo[index].shootTime; float _score = statisticDescInfo[index].score; int _ranking = statisticDescInfo[index].ranking; int _hitRate = statisticDescInfo[index].hitRate; float _totalMoveDis = statisticDescInfo[index].totalMoveDis; int _getPropAmount = statisticDescInfo[index].getPropAmount; _getPropAmount++; statisticDescInfo[index] = new StatisticInfo { score = _score, ranking = _ranking, shootTime = _shootTime, hitRate = _hitRate, totalMoveDis = _totalMoveDis, getPropAmount = _getPropAmount }; } private Dictionary lastHitTimestamps = new Dictionary(); public void AddHitRateOnSuccessfulHit(int ownerIndex) { float currentTime = Time.time; if (!lastHitTimestamps.ContainsKey(ownerIndex) || currentTime - lastHitTimestamps[ownerIndex] > hitInterval) { AddHitRate(ownerIndex); lastHitTimestamps[ownerIndex] = currentTime; } } // 命中间隔时间,可根据实际需求调整 private const float hitInterval = 0.5f; // 例如,设定为0.5秒 public bool isGameEnd = false; private void Update() { if (GameManager.Ins == null) return; if (!GameManager.Ins.isGameEnd) { for (int i = 0; i < MRNetworkManager.Ins.roomSlots.Count; i++) { Vector3 currentPosition = new Vector3((int)MRNetworkManager.Ins.roomSlots[i].transform.position.x, (int)MRNetworkManager.Ins.roomSlots[i].transform.position.y , (int)MRNetworkManager.Ins.roomSlots[i].transform.position.z); float distanceMoved = Vector3.Distance(currentPosition, lastPositions[i]); totalDistanceMoveds[i] += distanceMoved; // lastPositions[i] = new Vector3((int)currentPosition.x, (int)currentPosition.y, (int)currentPosition.z); lastPositions[i] = currentPosition; } } else { if (!isGameEnd) { SetTitle(); for (int i = 0; i < MRNetworkManager.Ins.roomSlots.Count; i++) { SetTotalMoveDis(i, totalDistanceMoveds[i]); } isGameEnd = true; } } } }