WXGame/box1/Assets/Script/DataManager.cs

298 lines
7.3 KiB
C#
Raw Permalink Normal View History

2024-11-26 15:44:50 +08:00
using System;
2024-11-05 18:15:49 +08:00
using System.Collections;
using System.Collections.Generic;
2024-11-08 18:21:41 +08:00
using System.IO;
2024-12-16 17:53:28 +08:00
using TTSDK;
2024-12-25 20:37:39 +08:00
using TTSDK.UNBridgeLib.LitJson;
2024-11-05 18:15:49 +08:00
using UnityEngine;
public static class DataManager
{
public static int Level
{
get
{
2024-11-26 15:44:50 +08:00
return GetInt("Level");
2024-11-05 18:15:49 +08:00
}
set
{
2024-11-26 15:44:50 +08:00
SetInt("Level",value);
2024-11-05 18:15:49 +08:00
}
}
2024-11-08 18:21:41 +08:00
public static int chapter
{
get
{
2024-11-26 15:44:50 +08:00
return GetInt("chapter");
2024-11-08 18:21:41 +08:00
}
set
{
2024-11-26 15:44:50 +08:00
SetInt("chapter",value);
2024-11-08 18:21:41 +08:00
}
}
public static bool chapterLock(int id)
{
2024-11-26 15:44:50 +08:00
return GetInt("chapterUnLock"+id)==1;
2024-11-08 18:21:41 +08:00
}
public static void chapterLock(int id, bool bo)
{
2024-11-26 15:44:50 +08:00
SetInt("chapterUnLock"+id,bo?1:0);
2024-11-08 18:21:41 +08:00
}
2024-11-26 15:44:50 +08:00
2024-12-18 11:12:52 +08:00
public static void STTAnalytics(string key,int num)
{
SetInt("tt" + key, num);
}
public static int GTTAnalytics(string key)
{
return GetInt("tt" + key);
}
2024-11-08 18:21:41 +08:00
private static Dictionary<int, bool> _directory=new Dictionary<int, bool>();
public static bool LevelOpen(int id)
{
if (_directory.TryGetValue(id, out var open)) return open;
2024-11-26 15:44:50 +08:00
var bo = GetInt("level"+id.ToString())==1;
2024-11-21 10:26:47 +08:00
if (id==0)
{
bo = true;
}
2024-11-08 18:21:41 +08:00
_directory.Add(id,bo);
return _directory[id];
}
public static void SetLevelOpen(int id,bool bo)
{
_directory[id] = bo;
2024-11-26 15:44:50 +08:00
SetInt("level"+id.ToString(),bo?1:0);
}
public static int HighestRecordPoints(int id)
{
return GetInt("HighestPoints"+id);
}
public static int LastTimePoints(int id)
{
return GetInt("LastPoints"+id);
}
public static void SetPointsData(int id, int num)
{
if (GetInt("HighestPoints"+id)<num)
{
SetInt("HighestPoints"+id,num);
}
SetInt("LastPoints" + id, num);
}
/// <summary>
/// 任务数据存储
/// </summary>
/// <param name="id"></param>
/// <param name="eventID"></param>
/// <param name="bo"></param>
public static void SetTask(int id,int eventID,bool bo)
{
SetInt(id + "_" + eventID, bo ? 1 : 0);
}
public static bool GetTask(int id,int eventID)
{
return GetInt(id + "_" + eventID) == 1;
}
public static void SaveAllStarNum()
{
int num = 0;
foreach (var bChapter in JsonTab.Instance.tables.Chapter.DataList)
{
foreach (var levelId in bChapter.Contain)
{
var lData = JsonTab.Instance.tables.Level.Get(levelId);
if (GetTask(lData.ID, lData.Maintasks))
{
num++;
}
foreach (var additionaltask in lData.Additionaltasks)
{
if (GetTask(lData.ID, additionaltask))
{
num++;
}
}
}
}
SetInt("star", num);
}
public static int GetStar()
{
return GetInt("star")-GetSpend();
}
public static int GetAllStar()
{
return GetInt("star");
}
public static void SetSpend(int num)
{
SetInt("Spend",num);
}
public static int GetSpend()
{
return GetInt("Spend");
}
public static void SetHomeFurnitureID(string key,int id)
{
SetInt("Furniture-" + key, id);
}
public static int GetHomeFurnitureID(string key)
{
return GetInt("Furniture-" + key);
}
2024-12-16 17:25:10 +08:00
//时间戳换算成时间
private static DateTime ConvertIntDatetime(Int64 utc)
{
2024-12-17 13:37:43 +08:00
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1,0,0,0));
2024-12-16 17:25:10 +08:00
startTime = startTime.AddSeconds(utc);
return startTime;
}
public static bool NextDay(string key)
{
2024-12-18 11:12:52 +08:00
var testTime2 =GetTimeStamp (DateTime.Now.ToUniversalTime());
2024-12-16 17:25:10 +08:00
var testTime = GetTimeStamp(GetTime(key));
var testTimeLingchen = testTime - ((testTime + 8 * 3600) % 86400);
if (testTime2 > testTime)
{
if (testTime2 - testTimeLingchen < 24 * 60 * 60)
{
BoxDebug.Log("进入时间 和 上次进入时间,在同一天,且在上次进入时间之后");
return false;
}
else
{
BoxDebug.Log("进入时间 和 上次进入时间,不同一天,且在上次进入时间之后");
return true;
}
}
else if (testTime2 < testTime)
{
if (testTime2 - testTimeLingchen < 24 * 60 * 60)
{
BoxDebug.Log("进入时间 和 上次进入时间,在同一天,且在上次进入时间之前");
return false;
}
else
{
BoxDebug.Log("进入时间 和 上次进入时间,不同一天,且在上次进入时间之前");
return false;
}
}
else
{
BoxDebug.Log("进入时间 和 上次进入时间,是同一个时刻");
return false;
}
}
public static DateTime GetTime(string key)
{
return ConvertIntDatetime(GetInt("time" + key));;
}
public static int GetMinute(string key)
{
2024-12-17 13:37:43 +08:00
TimeSpan timeSpan = new TimeSpan(DateTimeOffset.Now.Ticks - GetTime(key).Ticks);
return (int)timeSpan.TotalMinutes;
2024-12-16 17:25:10 +08:00
}
public static int GetHours(string key)
{
//计算两个时间间隔
2024-12-17 13:37:43 +08:00
TimeSpan timeSpan = new TimeSpan(DateTimeOffset.Now.Ticks - GetTime(key).Ticks);
2024-12-16 17:25:10 +08:00
return timeSpan.Hours;
}
public static int GetDay(string key)
{
//计算两个时间间隔
TimeSpan timeSpan = new TimeSpan(DateTime.Now.Ticks - GetTime(key).Ticks);
return timeSpan.Days;
}
2024-11-26 15:44:50 +08:00
2024-12-16 17:25:10 +08:00
public static int GetPhysical()
{
return GetInt("timePhysical");
}
public static void AddPhysical()
{
var p = (float)GetMinute("physical")/10;
if (p>1)
{
2024-12-17 13:37:43 +08:00
NewPhysicalEvent();
2024-12-16 17:25:10 +08:00
SetPhysical((int)p);
}
}
2024-12-17 13:37:43 +08:00
public static void RemovePhysical(int num)
{
NewPhysicalEvent();
var p = GetInt("timePhysical") - num;
SetInt("timePhysical", p);
}
public static void NewPhysicalEvent()
{
SetTime("physical");
}
2024-12-16 17:25:10 +08:00
public static void SetPhysical(int i)
{
var num = GetInt("timePhysical") + i;
if (num>30)
{
num = 30;
}
SetInt("timePhysical", num);
}
public static void SetTime(string key)
{
2024-12-17 13:37:43 +08:00
var ts = GetTimeStamp(DateTime.Now.ToUniversalTime());
2024-12-16 17:25:10 +08:00
SetInt("time" + key, ts);
}
//获取时间戳
private static int GetTimeStamp(DateTime dt)
{
TimeSpan ts = dt - new DateTime(1970, 1, 1, 0, 0, 0);
return (int)System.Convert.ToInt64(ts.TotalSeconds);
}
2024-11-26 15:44:50 +08:00
public static void SetInt(string key, int num)
{
2024-12-16 17:53:28 +08:00
TT.PlayerPrefs.SetInt(key, num);
2024-11-26 15:44:50 +08:00
}
public static int GetInt(string key)
{
2024-12-16 17:53:28 +08:00
return TT.PlayerPrefs.GetInt(key);
2024-11-08 18:21:41 +08:00
}
2024-12-28 15:00:14 +08:00
public static void SetString(string key, String num)
{
PlayerPrefs.SetString(key, num);
}
public static string GetString(string key)
{
return PlayerPrefs.GetString(key);
}
2024-11-05 18:15:49 +08:00
}