Files
valheim/Assets/_Valheim/Scripts/StatisticsManager.cs
2025-07-04 14:16:14 +08:00

109 lines
3.0 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Mirror;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using UnityEngine;
using UnityEngine.SocialPlatforms.Impl;
/// <summary>
/// 统计管数据
/// </summary>
public class StatisticsDate
{
public int GetPetCount;
public int[] numberIds;
}
public class StatisticsManager : NetworkBehaviour
{
[SyncVar]
public string StatisticsDate = "";
public static StatisticsManager Ins { get; private set; }
void Awake()
{
Ins = this;
}
public void Start()
{
if (isServer)
{
}
}
public void changToJson()
{
StatisticsDate date = new StatisticsDate();
date.GetPetCount = 12;
List<int> ints = new List<int>();
date.numberIds = ints.ToArray();
string strJson = JsonUtility.ToJson(date);
}
public void SetPlayerStatistics(int playerId, string statisticType, int[] values)
{
JObject playerStatsJson = string.IsNullOrEmpty(StatisticsDate) ? new JObject() : JObject.Parse(StatisticsDate);
JObject playerStats = (JObject)playerStatsJson[playerId.ToString()] ?? new JObject();
playerStatsJson[playerId.ToString()] = playerStats;
JArray statsArray = new JArray(values);
playerStats[statisticType] = statsArray;
StatisticsDate = playerStatsJson.ToString(Formatting.None);
}
public void SetPlayerStatistics(int playerId, string statisticType, int value)
{
SetPlayerStatistics(playerId, statisticType, new int[] { value });
}
// public void SetPlayerStatistics(int playerId, string statisticType, int value)
// {
// // 尝试将现有的playerStatistics转换为JObject
// JObject playerStatsJson = string.IsNullOrEmpty(playerStatistics) ? new JObject() : JObject.Parse(playerStatistics);
// // 更新或添加玩家成就数据
// playerStatsJson[playerId.ToString()] = playerStatsJson[playerId.ToString()] ?? new JObject();
// ((JObject)playerStatsJson[playerId.ToString()])[statisticType] = value;
// // 将JObject转换回字符串并赋值给SyncVar
// playerStatistics = playerStatsJson.ToString(Formatting.None);
// }
// public int? GetPlayerStatistic(int playerId, string statisticType)
// {
// try
// {
// JObject playerStatsJson = string.IsNullOrEmpty(playerStatistics) ? null : JObject.Parse(playerStatistics);
// if (playerStatsJson != null && playerStatsJson.ContainsKey(playerId.ToString()) &&
// ((JObject)playerStatsJson[playerId.ToString()]).ContainsKey(statisticType))
// {
// return (int)((JObject)playerStatsJson[playerId.ToString()])[statisticType];
// }
// }
// catch (JsonReaderException ex)
// {
// Debug.LogError($"Error parsing JSON: {ex.Message}");
// }
// return null; // 如果没有找到或发生错误则返回null
// }
}