diff --git a/Runtime/Code/Model/GuruInventoryModel.cs b/Runtime/Code/Model/GuruInventoryModel.cs new file mode 100644 index 0000000..d721aa7 --- /dev/null +++ b/Runtime/Code/Model/GuruInventoryModel.cs @@ -0,0 +1,117 @@ + +using UnityEngine.Serialization; + +namespace Guru +{ + using System; + using UnityEngine; + using System.Collections.Generic; + + + [Serializable] + public class GuruInventoryModel: GuruModelBase + { + public Dictionary data; + public Dictionary Data => data ??= new Dictionary(20); + + public bool RegisterItem(string itemName, int initCount = 0, string type = "") + { + if (!HasItem(itemName)) + { + Data[itemName] = new UserItemInfo() + { + key = itemName, + free_amount = initCount, + paid_amount = 0, + type = type + }; + return true; + } + return false; + } + + + public bool HasItem(string itemName) + { + return Data.ContainsKey(itemName); + } + + public UserItemInfo GetItemInfo(string itemName) + { + if (Data.TryGetValue(itemName, out var item)) + { + return item; + } + return null; + } + + } + + + [Serializable] + public class UserItemInfo + { + public bool enableZeroProtection = true; // 默认开启 0 值保护 + public string key; + public int free_amount; + public int paid_amount; + public string type; + + /// + /// 道具总量 + /// + public int TotalAmount => free_amount + paid_amount; + + /// + /// 增减道具量 + /// + /// + /// + public bool Change(int amount, bool isPaid = false) + { + if (amount == 0) return false; + + if (isPaid) + { + if (amount < 0 && enableZeroProtection && paid_amount + amount < 0) + return false; + + paid_amount += amount; + } + else + { + if (amount < 0 && enableZeroProtection && free_amount + amount < 0) + return false; + + free_amount += amount; + } + + return true; + } + + /// + /// 强制设置数量 + /// + /// + /// + public void Set(int freeAmount, int paidAmount = 0) + { + paid_amount = paidAmount; + free_amount = freeAmount; + } + + /// + /// 数值清零 + /// + public void Clean() + { + paid_amount = 0; + free_amount = 0; + } + + + } + + + +} \ No newline at end of file diff --git a/Runtime/Code/Model/GuruInventoryModel.cs.meta b/Runtime/Code/Model/GuruInventoryModel.cs.meta new file mode 100644 index 0000000..dcd8f4f --- /dev/null +++ b/Runtime/Code/Model/GuruInventoryModel.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 4d8be99a0fca428da9c6e70bc9cbea36 +timeCreated: 1706787422 \ No newline at end of file diff --git a/Runtime/Code/Model/GuruModelBase.cs b/Runtime/Code/Model/GuruModelBase.cs new file mode 100644 index 0000000..66b38fc --- /dev/null +++ b/Runtime/Code/Model/GuruModelBase.cs @@ -0,0 +1,74 @@ + + +namespace Guru +{ + + using UnityEngine; + + + public abstract class GuruModelBase + { + + + #region DataIO + + private double _lastSavedTime = 0; + + protected double _saveInterval = 3; + protected string DefaultSaveKey => GetType().Name; + + /// + /// 加载数据 IO 接口 + /// + /// + /// + /// + internal static T LoadIO(string saveKey = "") where T : GuruModelBase, new() + { + if (string.IsNullOrEmpty(saveKey)) saveKey = typeof(T).Name; + if (PlayerPrefs.HasKey(saveKey)) + { + var json = PlayerPrefs.GetString(saveKey, ""); + if (!string.IsNullOrEmpty(json)) + { + // return JsonUtility.FromJson(json); + return JsonParser.ToObject(json); + } + } + return new T(); + } + + /// + /// 保存数据 IO 接口 + /// + /// + protected void SaveIO(string saveKey = "") + { + if (string.IsNullOrEmpty(saveKey)) saveKey = DefaultSaveKey; + var json = JsonParser.ToJson(this); + PlayerPrefs.SetString(saveKey, json); + PlayerPrefs.Save(); + } + + /// + /// 保存数据 + /// + /// + public virtual void Save(bool force = false) + { + bool save = force || (Time.realtimeSinceStartup - _lastSavedTime>= _saveInterval); + if (save) + { + _lastSavedTime = Time.realtimeSinceStartup; + SaveIO(); + } + } + + + + #endregion + + + + } +} \ No newline at end of file diff --git a/Runtime/Code/Model/GuruModelBase.cs.meta b/Runtime/Code/Model/GuruModelBase.cs.meta new file mode 100644 index 0000000..f5bae58 --- /dev/null +++ b/Runtime/Code/Model/GuruModelBase.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: c99bee149c95459f93ccf3fe587788a0 +timeCreated: 1706787492 \ No newline at end of file diff --git a/Runtime/Code/Model/GuruSDKModel.cs b/Runtime/Code/Model/GuruSDKModel.cs index c65f581..4954412 100644 --- a/Runtime/Code/Model/GuruSDKModel.cs +++ b/Runtime/Code/Model/GuruSDKModel.cs @@ -10,9 +10,8 @@ namespace Guru [Serializable] - internal class GuruSDKModel + internal class GuruSDKModel: GuruModelBase { - private const float SaveInterval = 3; private const string SaveKey = "com.guru.sdk.model.save"; @@ -21,7 +20,7 @@ namespace Guru { get { - if (null == _instance) _instance = Load(); + if (null == _instance) _instance = Init(); return _instance; } } @@ -35,7 +34,7 @@ namespace Guru public List purchased; //-------------- data --------------- - private float _lastSavedTime = 0; + public int SuccessLevelCount { @@ -108,39 +107,13 @@ namespace Guru #region 初始化 - - public static GuruSDKModel Load() + private static GuruSDKModel Init() { - GuruSDKModel model = null; - if (PlayerPrefs.HasKey(SaveKey)) - { - var json = PlayerPrefs.GetString(SaveKey, ""); - if (!string.IsNullOrEmpty(json)) - { - try - { - model = JsonUtility.FromJson(json); - } - catch (Exception e) - { - Debug.LogError(e); - } - } - } - if(model == null) model = new GuruSDKModel(); + GuruSDKModel model = LoadIO(SaveKey); model.InitProperties(); return model; } - /// - /// 保存至数据 - /// - private void SaveToPlayerPrefs() - { - var json = JsonUtility.ToJson(this); - PlayerPrefs.SetString(SaveKey, json); - } - public void InitProperties() { _successLevel = new BindableProperty(b_level, OnLevelChanged); @@ -150,20 +123,6 @@ namespace Guru purchased = new List(20); } - - /// - /// 保存数据 - /// - /// - public void Save(bool force = false) - { - bool save = force || (Time.realtimeSinceStartup - _lastSavedTime>= SaveInterval); - if (save) - { - _lastSavedTime = Time.realtimeSinceStartup; - SaveToPlayerPrefs(); - } - } #endregion diff --git a/Runtime/Code/SDK/GuruSDK.Analytics.cs b/Runtime/Code/SDK/GuruSDK.Analytics.cs index abc60e9..850e599 100644 --- a/Runtime/Code/SDK/GuruSDK.Analytics.cs +++ b/Runtime/Code/SDK/GuruSDK.Analytics.cs @@ -262,6 +262,21 @@ namespace Guru } + #endregion + + #region 经济打点 + + public static void EarnVirtualCurrency() + { + + } + + public static void SpendVirtualCurrency() + { + + } + + #endregion } } \ No newline at end of file