// *********************************************************************** // Assembly : Game // Author : Kimch // Created : 2021-12-25 // Description : // Last Modified By : // Last Modified On : // *********************************************************************** // // // *********************************************************************** using CodeStage.AntiCheat.ObscuredTypes; using System.Collections; using System.Collections.Generic; using UnityEngine; namespace G { /// /// 货币管理 /// public class MoneyProxy : F.GameProxy { class MoneyInfo { public ObscuredInt lastAmount; public ObscuredInt lastChangeAmount; public int lastChangeSource; } #region Field /// /// /// readonly Dictionary _moneyInfoDict = new Dictionary(); #endregion #region Method /// /// 金币 /// public int coin { get { return GetMoney(Item.Id.kCoin); } } /// /// /// /// public void AddCoin(int add, int source = 0) { AddMoney(Item.Id.kCoin, add); } /// ///钻石 /// public int gem { get { return GetMoney(Item.Id.kGem); } } /// /// /// /// public void AddGem(int add) { AddMoney(Item.Id.kGem, add); } /// /// 碎片 /// public int chip { get { return GetMoney(Item.Id.kChip); } } /// /// /// /// public void AddChip(int add) { AddMoney(Item.Id.kChip, add); } /// /// 套装石头 /// public int stone { get { return GetMoney(Item.Id.kStone); } } /// /// /// /// public void AddStone(int add) { AddMoney(Item.Id.kStone, add); } /// /// 货币类型 /// /// /// public int GetMoney(int id) { return ArchiveProxy.Instance.GetInt(id); } /// /// /// /// /// public void AddMoney(int id, int value, int source = 0) { if (value == 0) return; int money = 0; int lastMoney = 0; if (id == Item.Id.kEnergy) { money = PlayerProxy.Instance.energy + value; PlayerProxy.Instance.energy = money; } else if (id == Item.Id.kMotility) { money = PlayerProxy.Instance.motility + value; PlayerProxy.Instance.motility = money; } else { lastMoney = ArchiveProxy.Instance.GetInt(id); money = Mathf.Max(0, lastMoney + value); ArchiveProxy.Instance.SetInt(id, money); if (id == Item.Id.kCoin || id == Item.Id.kStone || id == Item.Id.kGem || id == Item.Id.kChip || id == Item.Id.kLuckyBag || id == Item.Id.kAdTicket) { if (_moneyInfoDict.TryGetValue(id, out var moneyInfo)) { moneyInfo.lastAmount = lastMoney; moneyInfo.lastChangeAmount = value; moneyInfo.lastChangeSource = source; } else { moneyInfo = new MoneyInfo { lastAmount = lastMoney, lastChangeAmount = value, lastChangeSource = source, }; _moneyInfoDict.Add(id, moneyInfo); } SaveMoney(); } if (id == Item.Id.kCoin && value < 0) { MissionProxy.Instance.OnEvent(MissionProxy.累计金币消耗类型, -value); } } var moneyItem = ItemProxy.Instance.GetStaticItem(id); if (moneyItem != null) KStatistics.Instance.ReportEvent_Money(id, moneyItem.name, value > 0 ? 1 : 0, value, money, source.ToString()); } /// /// /// /// /// public bool CheckMoney(Item.ItemInfo itemInfo) { return GetMoney(itemInfo.id) >= itemInfo.count; } /// /// /// /// /// /// public bool CheckMoney(int id, int count) { return GetMoney(id) >= count; } /// /// /// /// /// public bool CheckMoney(int[] price) { var itemInfos = F.ListPool.Get(); Item.ItemInfo.FromArray(itemInfos, price); bool result = CheckMoney(itemInfos); F.ListPool.Release(itemInfos); return result; } /// /// /// /// /// public bool CheckMoney(IList itemInfos) { foreach (var itemInfo in itemInfos) { if (!CheckMoney(itemInfo)) { return false; } } return true; } /// /// 返回不足的id /// /// /// public int CheckMoney2(IList itemInfos) { foreach (var itemInfo in itemInfos) { if (GetMoney(itemInfo.id) < itemInfo.count) { return itemInfo.id; } } return 0; } /// /// /// /// /// public void CostMoney(int id, int value, int source = 0) { if (value > 0) AddMoney(id, -value, source); } /// /// /// /// public void CostMoney(Item.ItemInfo itemInfo, int source = 0) { CostMoney(itemInfo.id, itemInfo.count, source); } /// /// /// /// public void CostMoney(IList itemInfos, int source = 0) { foreach (var itemInfo in itemInfos) { CostMoney(itemInfo.id, itemInfo.count, source); } } /// /// /// /// public void CostMoney(int[] price, int source = 0) { var itemInfos = F.ListPool.Get(); Item.ItemInfo.FromArray(itemInfos, price); CostMoney(itemInfos, source); F.ListPool.Release(itemInfos); } /// /// /// /// /// public bool CheckAndCostMoney(Item.ItemInfo costInfo, int source = 0) { bool enough = CheckMoney(costInfo); if (enough) { CostMoney(costInfo, source); return true; } return false; } /// /// /// /// /// public bool CheckAndCostMoney(IList itemInfos, int source = 0) { bool enough = CheckMoney(itemInfos); if (enough) { CostMoney(itemInfos, source); return true; } return false; } /// /// /// /// /// public bool CheckAndCostMoney(int[] cost, int source = 0) { var itemInfos = F.ListPool.Get(); Item.ItemInfo.FromArray(itemInfos, cost); bool result = CheckAndCostMoney(itemInfos, source); F.ListPool.Release(itemInfos); return result; } #endregion #region Unity && Proxy && Archive private static MoneyProxy _Instance; public static MoneyProxy Instance => _Instance;// GetInstance(); // Use this for initialization private void Awake() { _Instance = this; } public override void ReadArchive() { LoadMoney(); } const string ARCHIVE_KEY_V1 = "money_v1"; void InitMoney() { } void LoadMoney() { _moneyInfoDict.Clear(); var dataList = ArchiveProxy.Instance.GetIntList(ARCHIVE_KEY_V1); if (dataList != null && dataList.Count > 0) { int maxIndex = dataList.Count - 4; for (int i = 0; i < maxIndex; i += 4) { int mId = dataList[i]; int mAmount = dataList[i + 1]; int mChangeAmount = dataList[i + 2]; int mChangeSource = dataList[i + 3]; _moneyInfoDict.Add(mId, new MoneyInfo { lastAmount = mAmount, lastChangeAmount = mChangeAmount, lastChangeSource = mChangeSource, }); } } foreach (var item in _moneyInfoDict) { var money = ArchiveProxy.Instance.GetInt(item.Key); int checkMoney = item.Value.lastAmount + item.Value.lastChangeAmount; if (Mathf.Abs(money - checkMoney) > 1) { ArchiveProxy.Instance.SetInt(item.Key, Mathf.Max(0, Mathf.Min(checkMoney, money))); } } } void SaveMoney() { var dataList = ArchiveProxy.Instance.GetIntList(ARCHIVE_KEY_V1); if (dataList != null) { dataList.Clear(); foreach (var item in _moneyInfoDict) { dataList.Add(item.Key); dataList.Add(item.Value.lastAmount); dataList.Add(item.Value.lastChangeAmount); dataList.Add(item.Value.lastChangeSource); } ArchiveProxy.Instance.SetIntList(ARCHIVE_KEY_V1, dataList); } } #endregion } }