Compare commits
1 Commits
main
...
feature/it
| Author | SHA1 | Date |
|---|---|---|
|
|
6eb2b9da3a |
|
|
@ -0,0 +1,117 @@
|
||||||
|
|
||||||
|
using UnityEngine.Serialization;
|
||||||
|
|
||||||
|
namespace Guru
|
||||||
|
{
|
||||||
|
using System;
|
||||||
|
using UnityEngine;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
|
||||||
|
[Serializable]
|
||||||
|
public class GuruInventoryModel: GuruModelBase
|
||||||
|
{
|
||||||
|
public Dictionary<string, UserItemInfo> data;
|
||||||
|
public Dictionary<string, UserItemInfo> Data => data ??= new Dictionary<string, UserItemInfo>(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;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 道具总量
|
||||||
|
/// </summary>
|
||||||
|
public int TotalAmount => free_amount + paid_amount;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 增减道具量
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="amount"></param>
|
||||||
|
/// <param name="isPaid"></param>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 强制设置数量
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="freeAmount"></param>
|
||||||
|
/// <param name="paidAmount"></param>
|
||||||
|
public void Set(int freeAmount, int paidAmount = 0)
|
||||||
|
{
|
||||||
|
paid_amount = paidAmount;
|
||||||
|
free_amount = freeAmount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 数值清零
|
||||||
|
/// </summary>
|
||||||
|
public void Clean()
|
||||||
|
{
|
||||||
|
paid_amount = 0;
|
||||||
|
free_amount = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 4d8be99a0fca428da9c6e70bc9cbea36
|
||||||
|
timeCreated: 1706787422
|
||||||
|
|
@ -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;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加载数据 IO 接口
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="saveKey"></param>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <returns></returns>
|
||||||
|
internal static T LoadIO<T>(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<T>(json);
|
||||||
|
return JsonParser.ToObject<T>(json);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new T();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 保存数据 IO 接口
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="saveKey"></param>
|
||||||
|
protected void SaveIO(string saveKey = "")
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(saveKey)) saveKey = DefaultSaveKey;
|
||||||
|
var json = JsonParser.ToJson(this);
|
||||||
|
PlayerPrefs.SetString(saveKey, json);
|
||||||
|
PlayerPrefs.Save();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 保存数据
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="force"></param>
|
||||||
|
public virtual void Save(bool force = false)
|
||||||
|
{
|
||||||
|
bool save = force || (Time.realtimeSinceStartup - _lastSavedTime>= _saveInterval);
|
||||||
|
if (save)
|
||||||
|
{
|
||||||
|
_lastSavedTime = Time.realtimeSinceStartup;
|
||||||
|
SaveIO();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: c99bee149c95459f93ccf3fe587788a0
|
||||||
|
timeCreated: 1706787492
|
||||||
|
|
@ -10,9 +10,8 @@ namespace Guru
|
||||||
|
|
||||||
|
|
||||||
[Serializable]
|
[Serializable]
|
||||||
internal class GuruSDKModel
|
internal class GuruSDKModel: GuruModelBase
|
||||||
{
|
{
|
||||||
private const float SaveInterval = 3;
|
|
||||||
private const string SaveKey = "com.guru.sdk.model.save";
|
private const string SaveKey = "com.guru.sdk.model.save";
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -21,7 +20,7 @@ namespace Guru
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if (null == _instance) _instance = Load();
|
if (null == _instance) _instance = Init();
|
||||||
return _instance;
|
return _instance;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -35,7 +34,7 @@ namespace Guru
|
||||||
public List<PurchasedProduct> purchased;
|
public List<PurchasedProduct> purchased;
|
||||||
//-------------- data ---------------
|
//-------------- data ---------------
|
||||||
|
|
||||||
private float _lastSavedTime = 0;
|
|
||||||
|
|
||||||
public int SuccessLevelCount
|
public int SuccessLevelCount
|
||||||
{
|
{
|
||||||
|
|
@ -108,39 +107,13 @@ namespace Guru
|
||||||
|
|
||||||
#region 初始化
|
#region 初始化
|
||||||
|
|
||||||
|
private static GuruSDKModel Init()
|
||||||
public static GuruSDKModel Load()
|
|
||||||
{
|
{
|
||||||
GuruSDKModel model = null;
|
GuruSDKModel model = LoadIO<GuruSDKModel>(SaveKey);
|
||||||
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();
|
model.InitProperties();
|
||||||
return model;
|
return model;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 保存至数据
|
|
||||||
/// </summary>
|
|
||||||
private void SaveToPlayerPrefs()
|
|
||||||
{
|
|
||||||
var json = JsonUtility.ToJson(this);
|
|
||||||
PlayerPrefs.SetString(SaveKey, json);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void InitProperties()
|
public void InitProperties()
|
||||||
{
|
{
|
||||||
_successLevel = new BindableProperty<int>(b_level, OnLevelChanged);
|
_successLevel = new BindableProperty<int>(b_level, OnLevelChanged);
|
||||||
|
|
@ -151,20 +124,6 @@ namespace Guru
|
||||||
purchased = new List<PurchasedProduct>(20);
|
purchased = new List<PurchasedProduct>(20);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <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
|
#endregion
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -262,6 +262,21 @@ namespace Guru
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region 经济打点
|
||||||
|
|
||||||
|
public static void EarnVirtualCurrency()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void SpendVirtualCurrency()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue