com.guru.unity.sdk/Runtime/Code/Model/GuruSDKModel.cs

223 lines
5.5 KiB
C#
Raw Normal View History

2023-12-26 03:40:48 +00:00
2024-01-07 14:59:02 +00:00
using UnityEngine.Networking;
2023-12-26 03:40:48 +00:00
namespace Guru
{
2023-12-27 12:24:16 +00:00
using System;
using UnityEngine;
using System.IO;
2023-12-26 03:40:48 +00:00
[Serializable]
internal class GuruSDKModel
{
2023-12-27 12:24:16 +00:00
private const float SaveInterval = 3;
private const string SaveKey = "com.guru.sdk.model.save";
2023-12-26 03:40:48 +00:00
private static GuruSDKModel _instance;
public static GuruSDKModel Instance
{
get
{
if (null == _instance) _instance = Load();
return _instance;
}
}
//-------------- data ---------------
2024-01-07 14:59:02 +00:00
public string uid = "";
2023-12-26 03:40:48 +00:00
public int b_level = 0;
public int b_play = 0;
public int buy_count = 0;
//-------------- data ---------------
private float _lastSavedTime = 0;
public int SuccessLevelCount
{
get
{
if(_successLevel == null) InitProperties();
return _successLevel.Value;
}
2024-01-07 14:59:02 +00:00
set
{
if(_successLevel == null) InitProperties();
_successLevel.Value = value;
}
2023-12-26 03:40:48 +00:00
}
public int TotalPlayedCount
{
get
{
if(_totalPlayed == null) InitProperties();
return _totalPlayed.Value;
}
2024-01-07 14:59:02 +00:00
set
{
if(_totalPlayed == null) InitProperties();
_totalPlayed.Value = value;
}
2023-12-26 03:40:48 +00:00
}
public int PurchasedCount
{
get
{
if(_purchasedCount == null) InitProperties();
return _purchasedCount.Value;
}
2024-01-07 14:59:02 +00:00
set
{
if(_purchasedCount == null) InitProperties();
_purchasedCount.Value = value;
}
}
public string UserId
{
get
{
if(_uid == null) InitProperties();
return _uid.Value;
}
set
{
if(_uid == null) InitProperties();
_uid.Value = value;
}
2023-12-26 03:40:48 +00:00
}
public bool IsIAPUser => PurchasedCount > 0;
private BindableProperty<int> _successLevel;
private BindableProperty<int> _totalPlayed;
private BindableProperty<int> _purchasedCount;
2024-01-07 14:59:02 +00:00
private BindableProperty<string> _uid;
2023-12-26 03:40:48 +00:00
public BindableProperty<int> PropBLevel => _successLevel;
public BindableProperty<int> PropBPlay => _totalPlayed;
public BindableProperty<int> PropBuyCount => _purchasedCount;
2024-01-07 14:59:02 +00:00
public BindableProperty<string> Uid => _uid;
2023-12-26 03:40:48 +00:00
#region 初始化
2023-12-27 12:24:16 +00:00
2023-12-26 03:40:48 +00:00
public static GuruSDKModel Load()
{
GuruSDKModel model = null;
if (PlayerPrefs.HasKey(SaveKey))
{
var json = PlayerPrefs.GetString(SaveKey, "");
if (!string.IsNullOrEmpty(json))
{
try
{
model = JsonUtility.FromJson<GuruSDKModel>(json);
}
catch (Exception e)
{
Debug.LogError(e);
}
}
}
if(model == null) model = new GuruSDKModel();
model.InitProperties();
return model;
}
/// <summary>
/// 保存至数据
/// </summary>
private void SaveToPlayerPrefs()
{
var json = JsonUtility.ToJson(this);
PlayerPrefs.SetString(SaveKey, json);
}
public void InitProperties()
{
_successLevel = new BindableProperty<int>(b_level, OnLevelChanged);
_totalPlayed = new BindableProperty<int>(b_play, OnPlayedChanged);
_purchasedCount = new BindableProperty<int>(buy_count, OnPurchasedNumChanged);
2024-01-07 14:59:02 +00:00
_uid = new BindableProperty<string>(uid, OnUidChanged);
2023-12-26 03:40:48 +00:00
}
/// <summary>
/// 保存数据
/// </summary>
/// <param name="force"></param>
public void Save(bool force = false)
{
bool save = force || (Time.realtimeSinceStartup - _lastSavedTime>= SaveInterval);
if (save)
{
_lastSavedTime = Time.realtimeSinceStartup;
SaveToPlayerPrefs();
}
}
#endregion
#region 数据绑定变化
private void OnLevelChanged(int value)
{
b_level = value;
Save();
}
private void OnPlayedChanged(int value)
{
b_play = value;
Save();
}
private void OnPurchasedNumChanged(int value)
{
buy_count = value;
Save();
}
2024-01-07 14:59:02 +00:00
private void OnUidChanged(string value)
{
uid = value;
Save();
}
2023-12-26 03:40:48 +00:00
#endregion
2023-12-27 12:24:16 +00:00
#region 启动配置
/// <summary>
/// 从 Streaming 加载 AppServices 配置
/// </summary>
/// <returns></returns>
2024-01-07 14:59:02 +00:00
public string LoadDefaltServicesConfigJson()
2023-12-27 12:24:16 +00:00
{
try
{
2024-01-07 14:59:02 +00:00
var txt = Resources.Load<TextAsset>(GuruSDK.ServicesConfigKey);
return txt?.text ?? "";
2023-12-27 12:24:16 +00:00
}
catch (Exception e)
{
Log.Exception(e);
}
return "";
}
2023-12-26 03:40:48 +00:00
2023-12-27 12:24:16 +00:00
#endregion
2023-12-26 03:40:48 +00:00
}
}