384 lines
13 KiB
C#
384 lines
13 KiB
C#
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<int, TitleConditionInfo> TitleConditionInfo;
|
||
|
||
}
|
||
|
||
|
||
public class StatisticManager : NetworkBehaviour
|
||
{
|
||
public static StatisticManager Ins { get; private set; }
|
||
|
||
public int playerIndex;
|
||
|
||
|
||
[SyncVar]
|
||
public string titleName;
|
||
|
||
|
||
public List<Vector3> lastPositions;
|
||
|
||
public List<float> totalDistanceMoveds;
|
||
// json<6F><6E><EFBFBD>ݿ<EFBFBD>
|
||
private JsonLiteDB DB;
|
||
|
||
private void Awake()
|
||
{
|
||
Ins = this;
|
||
}
|
||
public SyncDictionary<int, StatisticInfo> statisticDescInfo = new SyncDictionary<int, StatisticInfo>();
|
||
|
||
|
||
|
||
|
||
private void Start()
|
||
{
|
||
// DB = new JsonLiteDB();
|
||
// DB.Load(Resources.Load<TextAsset>("MechanicalAgeData").text);
|
||
// TableReader titleConditionInfo = DB["TitleConditionInfo"].GetReader();
|
||
// while (titleConditionInfo.Read())
|
||
// {
|
||
// TitleConditionInfo info = new TitleConditionInfo(titleConditionInfo);
|
||
// titleConditionDescInfos.Add(info.Id, info);
|
||
// }
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// <20><><EFBFBD>ؾ<EFBFBD><D8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Score<72><65><EFBFBD>Եļ<D4B5>
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public int GetMaxScoreKey()
|
||
{
|
||
// <20><><EFBFBD><EFBFBD>ֵ<EFBFBD>Ϊ<EFBFBD>գ<EFBFBD><D5A3><EFBFBD><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC>ֵ
|
||
if (statisticDescInfo.Count == 0)
|
||
{
|
||
return default;
|
||
}
|
||
// ʹ<><CAB9> LINQ <20><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD> StatisticInfo <20><><EFBFBD><EFBFBD><EFBFBD> Score <20><><EFBFBD>Խ<EFBFBD><D4BD>н<EFBFBD><D0BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
var sortedDictionary = statisticDescInfo.OrderByDescending(kvp => kvp.Value.score);
|
||
|
||
return sortedDictionary.First().Key;
|
||
}
|
||
|
||
/// <summary>
|
||
/// <20><><EFBFBD>ؾ<EFBFBD><D8BE><EFBFBD><EFBFBD><EFBFBD>СScore<72><65><EFBFBD>Եļ<D4B5>
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public int GetMinScoreKey()
|
||
{
|
||
|
||
if (statisticDescInfo.Count < 2) return -1;
|
||
if (statisticDescInfo.Count == 0)
|
||
{
|
||
Debug.LogError("Dictionary is empty!");
|
||
return default;
|
||
}
|
||
|
||
// ʹ<><CAB9> LINQ <20><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD> StatisticInfo <20><><EFBFBD><EFBFBD><EFBFBD> Score <20><><EFBFBD>Խ<EFBFBD><D4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
var sortedDictionary = statisticDescInfo.OrderBy(kvp => kvp.Value.score);
|
||
|
||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С Score <20><><EFBFBD>Եļ<D4B5>
|
||
return sortedDictionary.First().Key;
|
||
}
|
||
|
||
public int GetMaxGetPropAmountKey()
|
||
{
|
||
if (statisticDescInfo.Count == 0)
|
||
{
|
||
Debug.LogError("Dictionary is empty!");
|
||
return default;
|
||
}
|
||
|
||
// ʹ<><CAB9> LINQ <20><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD> StatisticInfo <20><><EFBFBD><EFBFBD><EFBFBD> Score <20><><EFBFBD>Խ<EFBFBD><D4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
var sortedDictionary = statisticDescInfo.OrderByDescending(kvp => kvp.Value.getPropAmount);
|
||
|
||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С Score <20><><EFBFBD>Եļ<D4B5>
|
||
return sortedDictionary.First().Key;
|
||
}
|
||
|
||
/// <summary>
|
||
/// <20>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public int GetMaxFarthestDstanceKey()
|
||
{
|
||
if (statisticDescInfo.Count == 0)
|
||
{
|
||
Debug.LogError("Dictionary is empty!");
|
||
return default;
|
||
}
|
||
|
||
// ʹ<><CAB9> LINQ <20><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD> StatisticInfo <20><><EFBFBD><EFBFBD><EFBFBD> Score <20><><EFBFBD>Խ<EFBFBD><D4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
var sortedDictionary = statisticDescInfo.OrderByDescending(kvp => kvp.Value.totalMoveDis);
|
||
|
||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С Score <20><><EFBFBD>Եļ<D4B5>
|
||
return sortedDictionary.First().Key;
|
||
}
|
||
|
||
|
||
public int GetMaxHitRateKey()
|
||
{
|
||
if (statisticDescInfo.Count == 0)
|
||
{
|
||
Debug.LogError("Dictionary is empty!");
|
||
return default;
|
||
}
|
||
|
||
// ʹ<><CAB9> LINQ <20><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD> StatisticInfo <20><><EFBFBD><EFBFBD><EFBFBD> Score <20><><EFBFBD>Խ<EFBFBD><D4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
var sortedDictionary = statisticDescInfo.OrderByDescending(kvp => kvp.Value.hitRate);
|
||
|
||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С Score <20><><EFBFBD>Եļ<D4B5>
|
||
return sortedDictionary.First().Key;
|
||
}
|
||
|
||
/// <summary>
|
||
/// <20><><EFBFBD><EFBFBD><EFBFBD>ǵڶ<C7B5><DAB6><EFBFBD><EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD>
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public bool IsDoubleScoreToSecondPlayer()
|
||
{
|
||
|
||
if (statisticDescInfo.Count < 2) return false;
|
||
if (statisticDescInfo.Count == 0)
|
||
{
|
||
Debug.LogError("Dictionary is empty!");
|
||
return default;
|
||
}
|
||
|
||
// ʹ<><CAB9> LINQ <20><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD> StatisticInfo <20><><EFBFBD><EFBFBD><EFBFBD> Score <20><><EFBFBD>Խ<EFBFBD><D4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
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()
|
||
{
|
||
//<2F>÷<EFBFBD><C3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD>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<int> TitleKeys = new List<int>();
|
||
//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);
|
||
|
||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD>Ԫ<EFBFBD><D4AA>
|
||
//
|
||
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<int, float> lastHitTimestamps = new Dictionary<int, float>();
|
||
|
||
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;
|
||
}
|
||
}
|
||
}
|
||
}
|