263 lines
		
	
	
		
			6.9 KiB
		
	
	
	
		
			C#
		
	
	
			
		
		
	
	
			263 lines
		
	
	
		
			6.9 KiB
		
	
	
	
		
			C#
		
	
	
| namespace Guru
 | |
| {
 | |
|     using System;
 | |
|     using UnityEngine;
 | |
|     using System.Collections.Generic;
 | |
|     using Newtonsoft.Json;
 | |
|     using System.Linq;
 | |
|     
 | |
|     
 | |
|     [Serializable]
 | |
|     class PurchasedProduct
 | |
|     {
 | |
|         public string productName;
 | |
|         public string productId;
 | |
|         public string receipt;
 | |
|         public bool appleProductIsRestored;
 | |
|     }
 | |
| 
 | |
|     [Serializable]
 | |
|     class GuruSDKSerializedModel
 | |
|     {
 | |
|         //-------------- data ---------------
 | |
|         
 | |
|         [JsonProperty(PropertyName = "uid")] 
 | |
|         public string uid = "";
 | |
|         
 | |
|         [JsonProperty(PropertyName = "b_level")] 
 | |
|         public int bLevel = 0;
 | |
|         
 | |
|         [JsonProperty(PropertyName = "b_play")]
 | |
|         public int bPlay = 0;
 | |
|   
 | |
|         [JsonProperty(PropertyName = "no_ads")]
 | |
|         public bool noAds = false;
 | |
|         
 | |
|         [JsonProperty(PropertyName = "purchased")]
 | |
|         public List<PurchasedProduct> purchased = new List<PurchasedProduct>(10);
 | |
|         
 | |
|         //-------------- data ---------------
 | |
|     }
 | |
|     
 | |
|     [Serializable]
 | |
|     internal class GuruSDKModel
 | |
|     {
 | |
|         private const float SaveInterval = 3;
 | |
|         private const string SaveKey = "com.guru.sdk.model.save";
 | |
|         private float _lastSavedTime = 0;
 | |
|         private bool _noAds = false;
 | |
|         private readonly BindableProperty<int> _bLevel;
 | |
|         private readonly BindableProperty<int> _bPlay;
 | |
|         private string _uid;
 | |
|         private List<PurchasedProduct> _purchased;
 | |
|         
 | |
|         
 | |
|         private static GuruSDKModel _instance;
 | |
|         public static GuruSDKModel Instance
 | |
|         {
 | |
|             get
 | |
|             {
 | |
|                 if (null == _instance) _instance = new GuruSDKModel();
 | |
|                 return _instance;
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         public GuruSDKModel()
 | |
|         {
 | |
|             GuruSDKSerializedModel model = LoadModel();
 | |
|             
 | |
|             _uid = model.uid;
 | |
|             _noAds = model.noAds;
 | |
|             _bLevel = new BindableProperty<int>(model.bLevel);
 | |
|             _bPlay = new BindableProperty<int>(model.bPlay);
 | |
|             _purchased = model.purchased;
 | |
|         }
 | |
| 
 | |
|         public int BLevel
 | |
|         {
 | |
|             get => _bLevel.Value;
 | |
|             set
 | |
|             {
 | |
|                 if (value < _bLevel.Value)
 | |
|                 {
 | |
|                     // b_level 必须比上一次的值大
 | |
|                     Debug.LogWarning($"[SDK] :: Set b_level [{value}] should not be less than original value [{_bLevel.Value}]");
 | |
|                     return;
 | |
|                 }
 | |
|                 _bLevel.Value = value;
 | |
|                 Save();
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         public int BPlay
 | |
|         {
 | |
|             get => _bPlay.Value;
 | |
|             set
 | |
|             {
 | |
|                 _bPlay.Value = value;
 | |
|                 Save();
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         public string UserId
 | |
|         {
 | |
|             get => _uid;
 | |
|             set
 | |
|             {
 | |
|                 _uid = value;
 | |
|                 Save();
 | |
|             }
 | |
|         }
 | |
| 
 | |
| 
 | |
|         public bool IsIapUser => _purchased.Count > 0;
 | |
|         
 | |
|         public bool IsNoAds
 | |
|         {
 | |
|             get => _noAds;
 | |
|             set
 | |
|             {
 | |
|                 _noAds = value;
 | |
|                 Save();
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         public void SetOnBLevelChanged(Action<int> action)
 | |
|         {
 | |
|             _bLevel.OnValueChanged += action;
 | |
|         }
 | |
|         
 | |
|         public void SetOnBPlayChanged(Action<int> action)
 | |
|         {
 | |
|             _bPlay.OnValueChanged += action;
 | |
|         }
 | |
|         
 | |
| 
 | |
|         #region 初始化
 | |
| 
 | |
| 
 | |
|         private GuruSDKSerializedModel LoadModel()
 | |
|         {
 | |
|             GuruSDKSerializedModel model = null;
 | |
|             if (PlayerPrefs.HasKey(SaveKey))
 | |
|             {
 | |
|                 var json = PlayerPrefs.GetString(SaveKey, "");
 | |
|                 if (!string.IsNullOrEmpty(json))
 | |
|                 {
 | |
|                     try
 | |
|                     {
 | |
|                         model =  JsonUtility.FromJson<GuruSDKSerializedModel>(json);
 | |
|                     }
 | |
|                     catch (Exception e)
 | |
|                     {
 | |
|                         Debug.LogError(e);
 | |
|                     }
 | |
|                 }
 | |
|             }
 | |
|             if(model == null) model = new GuruSDKSerializedModel();
 | |
|             return model;
 | |
|         }
 | |
|         
 | |
|         /// <summary>
 | |
|         /// 保存至数据
 | |
|         /// </summary>
 | |
|         private void SaveToPlayerPrefs()
 | |
|         {
 | |
|             var model = new GuruSDKSerializedModel()
 | |
|             {
 | |
|                 uid = _uid,
 | |
|                 bLevel = _bLevel.Value,
 | |
|                 bPlay = _bPlay.Value,
 | |
|                 noAds = _noAds,
 | |
|                 purchased = _purchased,
 | |
|             };
 | |
|             
 | |
|             var json = JsonUtility.ToJson(model);
 | |
|             PlayerPrefs.SetString(SaveKey, json);
 | |
|         }
 | |
|         
 | |
|         /// <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 订单记录
 | |
|         
 | |
|         public bool HasPurchasedProduct(string receipt)
 | |
|         {
 | |
|             if(_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>
 | |
|         /// <param name="appleProductIsRestored"></param>
 | |
|         public void AddReceipt(string receipt, string productName, string productId, bool appleProductIsRestored = false)
 | |
|         {
 | |
|             if (!HasPurchasedProduct(receipt))
 | |
|             {
 | |
|                 _purchased.Add(new PurchasedProduct()
 | |
|                 {
 | |
|                     receipt = receipt,
 | |
|                     productName = productName,
 | |
|                     productId = productId,
 | |
|                     appleProductIsRestored = appleProductIsRestored
 | |
|                 });
 | |
|                Save();
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         public string[] GetReceipts(string productName)
 | |
|         {
 | |
|             var receipts = new List<string>();
 | |
|             receipts.AddRange(from purchasedProduct in _purchased where purchasedProduct.productName == productName select purchasedProduct.receipt);
 | |
|             return receipts.ToArray();
 | |
|         }
 | |
| 
 | |
| 
 | |
|         public string[] GetReceiptsById(string productId)
 | |
|         {
 | |
|             var receipts = new List<string>();
 | |
|             receipts.AddRange(from purchasedProduct in _purchased where purchasedProduct.productId == productId select purchasedProduct.receipt);
 | |
|             return receipts.ToArray();
 | |
|         }
 | |
|         
 | |
|         
 | |
| 
 | |
|         #endregion
 | |
| 
 | |
| 
 | |
|         #region 清除数据
 | |
| 
 | |
|         public void ClearData()
 | |
|         {
 | |
|             PlayerPrefs.DeleteKey(SaveKey);
 | |
|         }
 | |
| 
 | |
|         #endregion
 | |
|     }
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| } |