308 lines
8.6 KiB
C#
308 lines
8.6 KiB
C#
|
|
|
|
|
|
namespace Guru
|
|
{
|
|
using System;
|
|
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
[Serializable]
|
|
internal class GuruSDKModel
|
|
{
|
|
private const float SaveInterval = 3;
|
|
private const string SaveKey = "com.guru.sdk.model.save";
|
|
|
|
|
|
private static GuruSDKModel _instance;
|
|
public static GuruSDKModel Instance
|
|
{
|
|
get
|
|
{
|
|
if (null == _instance) _instance = Load();
|
|
return _instance;
|
|
}
|
|
}
|
|
|
|
//-------------- data ---------------
|
|
public string uid = "";
|
|
public int b_level = 0;
|
|
public int b_play = 0;
|
|
public int buy_count = 0;
|
|
public bool no_ads = false;
|
|
|
|
public List<PurchasedProduct> purchased;
|
|
|
|
public Dictionary<string, int> event_priorities;
|
|
|
|
//-------------- data ---------------
|
|
|
|
private float _lastSavedTime = 0;
|
|
|
|
public int SuccessLevelId
|
|
{
|
|
get
|
|
{
|
|
if(_successLevel == null) InitProperties();
|
|
return _successLevel.Value;
|
|
}
|
|
set
|
|
{
|
|
if(_successLevel == null) InitProperties();
|
|
_successLevel.Value = value;
|
|
}
|
|
}
|
|
|
|
public int TotalPlayedCount
|
|
{
|
|
get
|
|
{
|
|
if(_totalPlayed == null) InitProperties();
|
|
return _totalPlayed.Value;
|
|
}
|
|
set
|
|
{
|
|
if(_totalPlayed == null) InitProperties();
|
|
_totalPlayed.Value = value;
|
|
}
|
|
}
|
|
|
|
public int PurchasedCount
|
|
{
|
|
get
|
|
{
|
|
if(_purchasedCount == null) InitProperties();
|
|
return _purchasedCount.Value;
|
|
}
|
|
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;
|
|
}
|
|
}
|
|
|
|
public bool IsIapUser => PurchasedCount > 0;
|
|
|
|
public bool IsNoAds
|
|
{
|
|
get => no_ads;
|
|
set
|
|
{
|
|
no_ads = value;
|
|
Save();
|
|
}
|
|
}
|
|
|
|
private BindableProperty<int> _successLevel;
|
|
private BindableProperty<int> _totalPlayed;
|
|
private BindableProperty<int> _purchasedCount;
|
|
private BindableProperty<string> _uid;
|
|
private BindableProperty<bool> _isIapUser;
|
|
|
|
public BindableProperty<int> PropBLevel => _successLevel;
|
|
public BindableProperty<int> PropBPlay => _totalPlayed;
|
|
public BindableProperty<int> PropBuyCount => _purchasedCount;
|
|
public BindableProperty<string> Uid => _uid;
|
|
|
|
#region 初始化
|
|
|
|
|
|
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)
|
|
{
|
|
UnityEngine.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()
|
|
{
|
|
if (_successLevel == null) _successLevel = new BindableProperty<int>(b_level, OnLevelChanged);
|
|
if (_totalPlayed == null) _totalPlayed = new BindableProperty<int>(b_play, OnPlayedChanged);
|
|
if (_purchasedCount == null) _purchasedCount = new BindableProperty<int>(buy_count, OnPurchasedNumChanged);
|
|
if (_uid == null) _uid = new BindableProperty<string>(uid, OnUidChanged);
|
|
if (purchased == null) purchased = new List<PurchasedProduct>(20);
|
|
if (event_priorities == null) event_priorities = new Dictionary<string, int>(10);
|
|
}
|
|
|
|
/// <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();
|
|
}
|
|
}
|
|
|
|
internal void SetBLevelValue(int value) => OnLevelChanged(value);
|
|
internal void SetBPlayValue(int value) => OnPlayedChanged(value);
|
|
|
|
|
|
#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();
|
|
}
|
|
|
|
private void OnUidChanged(string value)
|
|
{
|
|
uid = value;
|
|
Save();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 启动配置
|
|
|
|
/// <summary>
|
|
/// 从 Streaming 加载 AppServices 配置
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public string LoadDefaltServicesConfigJson()
|
|
{
|
|
try
|
|
{
|
|
var txt = Resources.Load<TextAsset>(GuruSDK.ServicesConfigKey);
|
|
return txt?.text ?? "";
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.Exception(e);
|
|
}
|
|
return "";
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 订单记录
|
|
|
|
|
|
public bool HasPurchasedProduct(string receipt)
|
|
{
|
|
if(purchased == null || purchased.Count == 0) return false;
|
|
return purchased.Exists(p => p.receipt == receipt);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 添加已支付订单
|
|
/// </summary>
|
|
/// <param name="receipt"></param>
|
|
/// <param name="productName"></param>
|
|
/// <param name="productId"></param>
|
|
public void AddReceipt(string receipt, string productName, string productId, bool appleProductIsRestored = false)
|
|
{
|
|
if (purchased == null) purchased = new List<PurchasedProduct>(20);
|
|
if (!HasPurchasedProduct(receipt))
|
|
{
|
|
purchased.Add(new PurchasedProduct()
|
|
{
|
|
receipt = receipt,
|
|
productName = productName,
|
|
productId = productId,
|
|
appleProductIsRestored = appleProductIsRestored
|
|
});
|
|
Save();
|
|
}
|
|
}
|
|
|
|
public string[] GetReceipts(string productName)
|
|
{
|
|
int count = purchased?.Count ?? 0;
|
|
if (count == 0) count = 20;
|
|
if (purchased == null) purchased = new List<PurchasedProduct>(count);
|
|
var receipts = new List<string>(count);
|
|
receipts.AddRange(from purchasedProduct in purchased where purchasedProduct.productName == productName select purchasedProduct.receipt);
|
|
return receipts.ToArray();
|
|
}
|
|
|
|
|
|
public string[] GetReceiptsById(string productId)
|
|
{
|
|
int count = purchased?.Count ?? 0;
|
|
if (count == 0) count = 20;
|
|
if (purchased == null) purchased = new List<PurchasedProduct>(count);
|
|
var receipts = new List<string>(count);
|
|
receipts.AddRange(from purchasedProduct in purchased where purchasedProduct.productId == productId select purchasedProduct.receipt);
|
|
return receipts.ToArray();
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
[Serializable]
|
|
internal class PurchasedProduct
|
|
{
|
|
public string productName;
|
|
public string productId;
|
|
public string receipt;
|
|
public bool appleProductIsRestored;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |