// *********************************************************************** // Assembly : Game // Author : Kimch // Created : 2020-12-08 // Description : // Last Modified By : // Last Modified On : // *********************************************************************** // // // *********************************************************************** using CodeStage.AntiCheat.ObscuredTypes; using F; using System.Collections; using System.Collections.Generic; using UnityEngine; namespace G { /// /// 商城 /// public class RmbShopProxy : F.GameProxy { /// /// 充值 /// public class ChargeInfo { public readonly ItemShopCharge item; public int id => item.id; private readonly ObscuredInt _rmbPrice; /// /// 加密过的 /// public int rmbPrice => _rmbPrice; private ObscuredInt _chargeCount; /// /// /// public int chargeCount => _chargeCount; public readonly Item.ItemInfo _exchange; /// /// 兑换元宝 /// public Item.ItemInfo exchange { get { var result = _exchange; //if (_chargeCount == 0) // _exchange.Apply(2); return result; } } public Item.ItemInfo exchange2 { get { var result = _exchange; //if (_chargeCount == 0) result.Apply(2); return result; } } /// /// 保存数据 /// public int status { get => _chargeCount; set => _chargeCount = value; } /// /// /// public int remainTimes { get { if (item.timeId > 0) return TimeProxy.Instance.GetTimes(item.timeId); return int.MaxValue; } } public ChargeInfo(ItemShopCharge chargeItem) { this.item = chargeItem; _rmbPrice = item.price; _exchange = Item.ItemInfo.Convert(item.exchange); } public void Charge() { _chargeCount++; if (item.timeId > 0) { TimeProxy.Instance.CostTimes(item.timeId); } } } /// /// 特权 /// public class SpecialInfo { public readonly ItemShopSpecial item; public int id => item.id; private readonly ObscuredInt _obPrice; /// /// 加密过的 /// public int rmbPrice => _obPrice; public int validityDay => item.validity; private ObscuredInt _remainRewardDay; /// /// 剩余天数 /// public int remainRewardDay { get { return _remainRewardDay; } set { _remainRewardDay = Mathf.Max(0, value); } } private ObscuredInt _activeTimestamp; public int activeTimestamp { get => _activeTimestamp; set { if (value >= 0) { _activeTimestamp = value; } } } public int remainDay { get { if (_activeTimestamp > 0) { if (item.validity > 0) { return item.validity - ((Launch.Timestamp - _activeTimestamp) / 86400); } return int.MaxValue; } return 0; } } public string remainDayText { get { if (item.validity > 0) { int remainSeconds = item.validity * 86400 + _activeTimestamp - Launch.Timestamp - 1; if (remainSeconds > 86400) return $"有效时间:{remainSeconds / 86400}天"; else if (remainSeconds > 0) return $"有效时间:{F.Utils.Time.ToTimeString(remainSeconds)}"; else return ""; } else { return "永久激活"; } } } /// /// 领取次数 /// public int rewardTimes { get { if (item.timeId > 0) return TimeProxy.Instance.GetTimes(item.timeId); return 0; } } /// /// 每次 /// public readonly Item.ItemInfo exchange; /// /// /// public readonly Item.ItemInfo[] dailyExchange; public int status { get => _remainRewardDay; set { _remainRewardDay = value; } } public SpecialInfo(ItemShopSpecial specialItem) { this.item = specialItem; _obPrice = item.price; exchange = Item.ItemInfo.Convert(item.exchange); dailyExchange = Item.ItemInfo.FromArray(item.dailyExchange); } public void Buy() { if (item.validity > 0) { _remainRewardDay = item.validity; _activeTimestamp = Launch.Timestamp; CheckValidity(); if (item.vip > 0) { VipProxy.Instance.AddExtraVip(item.vip, item.validity); } } else { _remainRewardDay = int.MaxValue; _activeTimestamp = Launch.Timestamp; if (item.vip > 0) { VipProxy.Instance.AddExtraVip(item.vip, int.MaxValue); } } } /// /// /// public bool CheckValidity() { if (_activeTimestamp > 0) { int passDay = (Launch.Timestamp - _activeTimestamp) / 86400; if (item.validity > 0 && passDay >= 0) { _remainRewardDay = Mathf.Min(_remainRewardDay, item.validity); int remainDay = Mathf.Max(0, item.validity - passDay); if (remainDay == 0) { _activeTimestamp = 0; } passDay = (Launch.Timestamp - _activeTimestamp + Launch.TodayRemainSeconds) / 86400; int remainRewadDay = Mathf.Max(0, item.validity - passDay); while (_remainRewardDay > 0 && _remainRewardDay >= remainRewadDay) { if (item.rewardMailId > 0 && _remainRewardDay > 0) MailProxy.Instance.PostLocalMail(0, item.rewardMailId, 1); _remainRewardDay -= 1; } return true; } } else { //兼容老版本 if (_remainRewardDay > 9999) { _remainRewardDay = int.MaxValue; _activeTimestamp = Launch.Timestamp; return true; } } return false; } public bool GetDaily() { if (item.timeId > 0 && _remainRewardDay > 0) { _remainRewardDay -= 1; return TimeProxy.Instance.CostTimes(item.timeId); } return false; } } /// /// 礼包 /// public class GiftBoxInfo { public readonly ItemShopGift item; /// /// /// public int id => item.id; /// /// /// public int type => item.type; /// /// /// public string name => item.name; /// /// /// public string description => item.name; /// /// /// public int remainTimes { get { if (item.timeId > 0) return TimeProxy.Instance.GetTimes(item.timeId); return int.MaxValue; } } /// /// 可以购买 /// public bool isValid { get { bool parentValid = false; if (item.prev > 0) { var prevGB = RmbShopProxy.Instance.GetGiftBoxInfo(item.prev); parentValid = prevGB.isValid; int loopCount = 0; while (!parentValid && prevGB.item.prev > 0 && loopCount++ < 10) { prevGB = RmbShopProxy.Instance.GetGiftBoxInfo(prevGB.item.prev); parentValid = prevGB.isValid; } #if UNITY_EDITOR if (loopCount >= 10) Debug.Log("礼包配置表错误"); #endif } return !parentValid && remainTimes > 0; } } /// /// /// public Item.ItemInfo price; /// /// /// public readonly Item.ItemInfo[] exchanges; public int rmbPrice { get => price.count; } public GiftBoxInfo(ItemShopGift giftItem) { this.item = giftItem; price = Item.ItemInfo.Convert(item.price); exchanges = Item.ItemInfo.FromArray(item.exchange); } public void Buy() { if (item.timeId > 0) { TimeProxy.Instance.CostTimes(item.timeId); } } } #region Field /// /// /// Dictionary _chargeInfos; /// /// /// Dictionary _specialInfos; /// /// 礼包 /// Dictionary _giftBoxInfos; #endregion #region Method public bool IsRemoveAd() { if (_specialInfos.TryGetValue(3, out var specialInfo)) { if (specialInfo.remainDay > 0) return true; } if (_specialInfos.TryGetValue(4, out var specialInfo2)) { if (specialInfo2.remainDay > 0) return true; } return false; } public int IsActivitionCard() { if (_specialInfos.TryGetValue(3, out var specialInfo)) { if (specialInfo.remainDay > 0) return 3; } if (_specialInfos.TryGetValue(4, out var specialInfo2)) { if (specialInfo2.remainDay > 0) return 4; } return 0; } public void CheckSpecialReward() { bool save = false; foreach (var specialInfo in _specialInfos.Values) { if (specialInfo.CheckValidity()) { save = true; } } if (save) { SaveShop(); } } /// /// 获取充值信息 /// /// public IReadOnlyCollection GetChargeInfos() { return _chargeInfos.Values; } /// /// 特权信息 /// /// public IReadOnlyCollection GetSpecialInfos() { return _specialInfos.Values; } /// /// 获取礼包信息 /// /// public IReadOnlyCollection GetGiftBoxInfos() { return _giftBoxInfos.Values; } /// /// /// /// /// public GiftBoxInfo GetGiftBoxInfo(int id) { _giftBoxInfos.TryGetValue(id, out var result); return result; } /// /// /// /// /// /// public void Recharge(int id, Callback2 callback) { _payCallback = callback; if (_chargeInfos.TryGetValue(id, out var chargeInfo)) { if (chargeInfo.remainTimes <= 0) { callback?.Invoke(1, "不能重复购买"); return; } var payInfo = new PayProxy.PayInfo { payId = chargeInfo.id, rmbPrice = chargeInfo.rmbPrice, buyAmount = chargeInfo.rmbPrice * 10, payName = chargeInfo.item.name, payType = "ShopCharge" }; if (chargeInfo.rmbPrice <= 0) { chargeInfo.Charge(); SaveShop(); RewardProxy.Instance.GetRewardWithUI(chargeInfo.exchange, callback); return; } PayProxy.Instance.Pay(payInfo, (error, message) => { if (error == ErrorCode.SUCCESS) { chargeInfo.Charge(); SaveShop(); MissionProxy.Instance.OnEvent(MissionProxy.累计充值, payInfo.rmbPrice); MissionProxy.Instance.OnClampEvent2(MissionProxy.单笔充值, payInfo.rmbPrice); RewardProxy.Instance.GetRewardWithUI(chargeInfo.exchange2, callback); PostNotification(GlobalDefine.EVENT_MONEY_CHANGED); } else { callback?.Invoke(error, message); } }); } } void RechargeIndirect(int id, int amount, Callback2 callback) { if (amount > 0 && _chargeInfos.TryGetValue(id, out var chargeInfo)) { for (int i = 0; i < amount; i++) { chargeInfo.Charge(); } SaveShop(); RewardProxy.Instance.GetRewardWithUI(chargeInfo.exchange2, _payCallback); MissionProxy.Instance.OnEvent(MissionProxy.累计充值, chargeInfo.rmbPrice * amount); MissionProxy.Instance.OnClampEvent2(MissionProxy.单笔充值, chargeInfo.rmbPrice); PostNotification(GlobalDefine.EVENT_MONEY_CHANGED); } } /// /// /// /// /// /// public void BuySpecial(int id, Callback2 callback) { _payCallback = callback; if (_specialInfos.TryGetValue(id, out var specialInfo)) { if (specialInfo.remainDay > 0) { callback?.Invoke(1, "不能重复购买"); return; } var payInfo = new PayProxy.PayInfo { payId = specialInfo.id, rmbPrice = specialInfo.rmbPrice, buyAmount = specialInfo.rmbPrice * 10, payName = specialInfo.item.name, payType = "ShopSpecial" }; PayProxy.Instance.Pay(payInfo, (error, message) => { if (error == ErrorCode.SUCCESS) { specialInfo.Buy(); SaveShop(); MissionProxy.Instance.OnEvent(MissionProxy.累计充值, payInfo.rmbPrice); MissionProxy.Instance.OnClampEvent2(MissionProxy.单笔充值, payInfo.rmbPrice); RewardProxy.Instance.GetRewardWithUI(specialInfo.exchange, callback); PostNotification(GlobalDefine.EVENT_MONEY_CHANGED); } else { callback?.Invoke(error, message); } }); } } void BuySpecialIndirect(int id, int amount, Callback2 callback) { if (amount > 0 && _specialInfos.TryGetValue(id, out var specialInfo)) { if (specialInfo.remainDay > 0) { _payCallback?.Invoke(1, "不能重复购买"); return; } specialInfo.Buy(); SaveShop(); RewardProxy.Instance.GetRewardWithUI(specialInfo.exchange, _payCallback); MissionProxy.Instance.OnEvent(MissionProxy.累计充值, specialInfo.rmbPrice); MissionProxy.Instance.OnClampEvent2(MissionProxy.单笔充值, specialInfo.rmbPrice); PostNotification(GlobalDefine.EVENT_MONEY_CHANGED); } } /// /// /// /// /// /// public void DailyGetSpecial(int id, Callback2 callback) { if (_specialInfos.TryGetValue(id, out var specialInfo)) { if (specialInfo.GetDaily()) { SaveShop(); RewardProxy.Instance.GetRewardsWithUI(specialInfo.dailyExchange, callback); } else { callback?.Invoke(1, "不能重复领取"); } } } private Callback2 _payCallback; /// /// 购买礼包 /// /// /// public void BuyGiftBox(int id, Callback2 callback) { _payCallback = callback; if (_giftBoxInfos.TryGetValue(id, out var giftBoxInfo)) { if (giftBoxInfo.remainTimes <= 0) { callback?.Invoke(1, "不能重复购买"); return; } if (giftBoxInfo.rmbPrice <= 0) { giftBoxInfo.Buy(); SaveShop(); RewardProxy.Instance.GetRewardsWithUI(giftBoxInfo.exchanges, callback); return; } var payInfo = new PayProxy.PayInfo { payId = giftBoxInfo.id, rmbPrice = giftBoxInfo.rmbPrice, buyAmount = giftBoxInfo.rmbPrice * 10, payName = giftBoxInfo.item.name, payType = "ShopGift" }; PayProxy.Instance.Pay(payInfo, (error, message) => { if (error == ErrorCode.SUCCESS) { giftBoxInfo.Buy(); SaveShop(); MissionProxy.Instance.OnEvent(MissionProxy.累计充值, payInfo.rmbPrice); MissionProxy.Instance.OnClampEvent2(MissionProxy.单笔充值, payInfo.rmbPrice); RewardProxy.Instance.GetRewardsWithUI(giftBoxInfo.exchanges, callback); PostNotification(GlobalDefine.EVENT_MONEY_CHANGED); } else { callback?.Invoke(error, message); } }); } } void BuyGiftBoxIndirect(int id, int amount, Callback2 callback) { if (amount > 0 && _giftBoxInfos.TryGetValue(id, out var giftBoxInfo)) { if (giftBoxInfo.remainTimes <= 0) { _payCallback?.Invoke(1, "不能重复购买"); return; } if (giftBoxInfo.rmbPrice <= 0) { giftBoxInfo.Buy(); SaveShop(); RewardProxy.Instance.GetRewardsWithUI(giftBoxInfo.exchanges, callback); return; } giftBoxInfo.Buy(); SaveShop(); MissionProxy.Instance.OnEvent(MissionProxy.累计充值, giftBoxInfo.rmbPrice); MissionProxy.Instance.OnClampEvent2(MissionProxy.单笔充值, giftBoxInfo.rmbPrice); RewardProxy.Instance.GetRewardsWithUI(giftBoxInfo.exchanges, _payCallback); PostNotification(GlobalDefine.EVENT_MONEY_CHANGED); } } /// /// 重新购买 /// /// public void IndirectBuy(Dictionary result, Callback2 callback) { foreach (var item in result) { var payKey = item.Key; var payCount = item.Value; if (payKey.StartsWith("ShopCharge")) { if (int.TryParse(payKey.Substring(10), out var payId)) { RechargeIndirect(payId, payCount, callback); } } else if (payKey.StartsWith("ShopSpecial")) { if (int.TryParse(payKey.Substring(11), out var payId)) { BuySpecialIndirect(payId, payCount, callback); } } else if (payKey.StartsWith("ShopGift")) { if (int.TryParse(payKey.Substring(8), out var payId)) { BuyGiftBoxIndirect(payId, payCount, callback); } } } } private void InitShop() { var chargeItems = ItemProxy.Instance.GetStaticItems(); _chargeInfos = new Dictionary(chargeItems.Count); foreach (var item in chargeItems) { _chargeInfos.Add(item.id, new ChargeInfo(item)); } var specialItems = ItemProxy.Instance.GetStaticItems(); _specialInfos = new Dictionary(specialItems.Count); foreach (var item in specialItems) { _specialInfos.Add(item.id, new SpecialInfo(item)); } var giftItems = ItemProxy.Instance.GetStaticItems(); _giftBoxInfos = new Dictionary(giftItems.Count); foreach (var item in giftItems) { _giftBoxInfos.Add(item.id, new GiftBoxInfo(item)); } } const string ARCHIVE_KEY_V1 = "rmb_shop_v1"; const string ARCHIVE_KEY_V2 = "rmb_shop_v2"; private void LoadShop() { var dataList = ArchiveProxy.Instance.GetIntList(ARCHIVE_KEY_V2); if (dataList != null && dataList.Count > 0) { int index = 0; int chargeCount = dataList.SafeGet(index++); for (int i = 0; i < chargeCount && index < dataList.Count; i++) { int id = dataList.SafeGet(index++); int status = dataList.SafeGet(index++); if (_chargeInfos.TryGetValue(id, out var chargeInfo)) chargeInfo.status = status; } int specialCount = dataList.SafeGet(index++); for (int i = 0; i < specialCount && index < dataList.Count; i++) { int id = dataList.SafeGet(index++); int remainDay = dataList.SafeGet(index++); int activeTimestamp = dataList.SafeGet(index++); if (_specialInfos.TryGetValue(id, out var specialInfo)) { specialInfo.remainRewardDay = remainDay; specialInfo.activeTimestamp = activeTimestamp; } } } else { dataList = ArchiveProxy.Instance.GetIntList(ARCHIVE_KEY_V1); if (dataList != null && dataList.Count > 0) { int index = 0; int chargeCount = dataList.SafeGet(index++); for (int i = 0; i < chargeCount && index < dataList.Count; i++) { int id = dataList.SafeGet(index++); int status = dataList.SafeGet(index++); if (_chargeInfos.TryGetValue(id, out var chargeInfo)) chargeInfo.status = status; } int specialCount = dataList.SafeGet(index++); for (int i = 0; i < specialCount && index < dataList.Count; i++) { int id = dataList.SafeGet(index++); int status = dataList.SafeGet(index++); if (_specialInfos.TryGetValue(id, out var specialInfo)) specialInfo.status = status; } } ArchiveProxy.Instance.RemoveIntList(ARCHIVE_KEY_V1); } } private void SaveShop() { var dataList = ArchiveProxy.Instance.GetIntList(ARCHIVE_KEY_V2); if (dataList != null) { dataList.Clear(); dataList.Add(_chargeInfos.Count); foreach (var kv in _chargeInfos) { dataList.Add(kv.Key); dataList.Add(kv.Value.status); } dataList.Add(_specialInfos.Count); foreach (var kv in _specialInfos) { dataList.Add(kv.Key); dataList.Add(kv.Value.remainRewardDay); dataList.Add(kv.Value.activeTimestamp); } ArchiveProxy.Instance.SetIntList(ARCHIVE_KEY_V2, dataList); } } #endregion #region Unity & PROXY public override int priority => 19; public static RmbShopProxy Instance; // Use this for initialization private void Awake() { Instance = this; } public override void ReadArchive() { InitShop(); LoadShop(); } #endregion } }