2023-12-26 03:40:48 +00:00
|
|
|
|
2024-05-07 12:37:57 +00:00
|
|
|
|
2024-01-07 14:59:02 +00:00
|
|
|
|
2023-12-26 03:40:48 +00:00
|
|
|
namespace Guru
|
|
|
|
|
{
|
2023-12-27 12:24:16 +00:00
|
|
|
using System;
|
|
|
|
|
using UnityEngine;
|
2024-05-07 12:37:57 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2023-12-27 12:24:16 +00:00
|
|
|
|
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;
|
2024-05-17 02:28:35 +00:00
|
|
|
|
2024-01-19 12:45:56 +00:00
|
|
|
public List<PurchasedProduct> purchased;
|
2023-12-26 03:40:48 +00:00
|
|
|
//-------------- data ---------------
|
|
|
|
|
|
|
|
|
|
private float _lastSavedTime = 0;
|
|
|
|
|
|
2024-04-02 07:27:34 +00:00
|
|
|
public int SuccessLevelId
|
2023-12-26 03:40:48 +00:00
|
|
|
{
|
|
|
|
|
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
|
|
|
}
|
2024-05-17 11:40:42 +00:00
|
|
|
|
|
|
|
|
public bool IsIapUser => PurchasedCount > 0;
|
2024-05-17 02:28:35 +00:00
|
|
|
|
|
|
|
|
|
2023-12-26 03:40:48 +00:00
|
|
|
|
|
|
|
|
private BindableProperty<int> _successLevel;
|
|
|
|
|
private BindableProperty<int> _totalPlayed;
|
|
|
|
|
private BindableProperty<int> _purchasedCount;
|
2024-01-07 14:59:02 +00:00
|
|
|
private BindableProperty<string> _uid;
|
2024-05-17 02:28:35 +00:00
|
|
|
private BindableProperty<bool> _isIapUser;
|
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)
|
|
|
|
|
{
|
2024-05-07 12:37:57 +00:00
|
|
|
UnityEngine.Debug.LogError(e);
|
2023-12-26 03:40:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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);
|
2024-01-19 12:45:56 +00:00
|
|
|
|
|
|
|
|
purchased = new List<PurchasedProduct>(20);
|
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();
|
|
|
|
|
}
|
2024-05-17 02:28:35 +00:00
|
|
|
|
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
|
|
|
|
2024-01-19 12:45:56 +00:00
|
|
|
#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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-12-27 12:24:16 +00:00
|
|
|
#endregion
|
2023-12-26 03:40:48 +00:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-19 12:45:56 +00:00
|
|
|
[Serializable]
|
|
|
|
|
internal class PurchasedProduct
|
|
|
|
|
{
|
|
|
|
|
public string productName;
|
|
|
|
|
public string productId;
|
|
|
|
|
public string receipt;
|
|
|
|
|
public bool appleProductIsRestored;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-26 03:40:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|