using System.Collections.Generic; using Excel2Json; using Fort23.Core; using Fort23.UTool; using GameLogic.Bag; using Utility; public class ShopManger : Singleton { bool isBuy = false; Dictionary> shopDic = new Dictionary>(); private Dictionary productDic = new Dictionary(); #if UNITY_ANDROID && YouLoftSDK public string[] adsIdentifiers = new[] { "com.xy001.ads.18", "com.xy001.gem.6", "com.xy001.gem.30", "com.xy001.gem.68", "com.xy001.gem.128", "com.xy001.gem.328", "com.xy001.super1.6" }; #elif UNITY_ANDROID && Google public string[] adsIdentifiers = new[] { "com.taptap.xy001.ads.18", "com.taptap.xy001.gem.6", "com.taptap.xy001.gem.30", "com.taptap.xy001.gem.68", "com.taptap.xy001.gem.128", "com.taptap.xy001.gem.328", "com.taptap.xy001.super1.6" }; #endif /// /// 初始化数据 /// public ShopManger() { ShopConfig[] shopConfigs = ConfigComponent.Instance.GetAll(); foreach (var config in shopConfigs) { List shopConfigList = null; if (!shopDic.ContainsKey(config.shopGroup)) { shopConfigList = new List(); shopDic.Add(config.shopGroup, shopConfigList); } else { shopConfigList = shopDic[config.shopGroup]; } shopConfigList.Add(config); } } public async CTask CustomInit() { #if UNITY_ANDROID && !UNITY_EDITOR && YouLoftSDK string json = await YouLoftSDK.Instance.QuerySkuDetail(adsIdentifiers); LogTool.Error(json); // 将 JSON 字符串直接反序列化为 List if (json != null && json != "") { List data = Json.Deserialize(json) as List; foreach (var o in data) { Dictionary deserialize = o as Dictionary; Product product = new Product(); product.productId = (string)deserialize["productId"]; product.type = (string)deserialize["type"]; product.title = (string)deserialize["title"]; product.name = (string)deserialize["name"]; product.description = (string)deserialize["description"]; product.price = (string)deserialize["price"]; product.priceAmountMicros = (long)deserialize["price_amount_micros"]; product.priceCurrencyCode = (string)deserialize["price_currency_code"]; product.skuDetailsToken = (string)deserialize["skuDetailsToken"]; if (!productDic.ContainsKey(product.productId)) productDic.Add(product.productId, product); } } #elif UNITY_ANDROID && Google GetPrice(); #endif InitShop(); } public void GetPrice() { #if UNITY_ANDROID && Google foreach (var adsIdentifier in adsIdentifiers) { if (!productDic.ContainsKey(adsIdentifier)) { string price = IAPManager.Instance.GetPrice(adsIdentifier); if (price != "") productDic.Add(adsIdentifier, price); } } #endif } public string GetPrice(string productId) { if (productDic.ContainsKey(productId)) return productDic[productId]; return ""; } public List GetAllShopConfig() { GetPrice(); InitShop(); return AccountFileInfo.Instance.playerData.shopDatas; } public void InitShop() { foreach (var keyValuePair in shopDic) { AccountFileInfo.ShopData shopData = GetDbShop(keyValuePair.Key); if (shopData != null && shopData.refreshTime != -1 && PlayerManager.Instance.serverTime > shopData.refreshTime) { AccountFileInfo.Instance.playerData.shopDatas.Remove(shopData); shopData = null; } //初始化商店 if (shopData == null) { shopData = RefenceShop(keyValuePair.Key); AccountFileInfo.Instance.playerData.shopDatas.Add(shopData); } else { checkShopItemUnlock(shopData); } } AccountFileInfo.Instance.playerData.shopDatas.Sort((AccountFileInfo.ShopData a, AccountFileInfo.ShopData b) => { ShopGroupConfig aa = ConfigComponent.Instance.Get(a.id); ShopGroupConfig bb = ConfigComponent.Instance.Get(b.id); return aa.sortNum.CompareTo(bb.sortNum); }); AccountFileInfo.Instance.SavePlayerData(); } public void checkShopItemUnlock(AccountFileInfo.ShopData shopData) { List removeItems = new List(); foreach (var shopItem in shopData.shopItemList) { if (shopItem.endTime != -1 && PlayerManager.Instance.serverTime >= shopItem.endTime) { removeItems.Add(shopItem); } } foreach (var removeItem in removeItems) { shopData.shopItemList.Remove(removeItem); } } public AccountFileInfo.ShopData RefenceShop(int id) { AccountFileInfo.ShopData shopData = new AccountFileInfo.ShopData(); ShopGroupConfig shopGroupConfig = ConfigComponent.Instance.Get(id); shopData.id = id; if (shopGroupConfig.RefreshType == 0) { shopData.refreshTime = -1; } else { shopData.refreshTime = TimeHelper.GetBaseRefreshTime(PlayerManager.Instance.serverTime); } shopDic.TryGetValue(id, out List shopConfigList); foreach (var shopConfig in shopConfigList) { AccountFileInfo.ShopItem shopItem = new AccountFileInfo.ShopItem(); shopItem.id = shopConfig.ID; shopItem.buyCount = 0; if (shopConfig.continueTime > 0) { shopItem.endTime = PlayerManager.Instance.serverTime + shopConfig.continueTime * 60 * 1000; } shopData.shopItemList.Add(shopItem); } return shopData; } public AccountFileInfo.ShopData GetDbShop(int id) { foreach (var playerDataShopData in AccountFileInfo.Instance.playerData.shopDatas) { if (playerDataShopData.id == id) { return playerDataShopData; } } return null; } public AccountFileInfo.ShopItem GetShopItem(int id) { foreach (var shopData in AccountFileInfo.Instance.playerData.shopDatas) { foreach (var shopItem in shopData.shopItemList) { if (shopItem.id == id) { return shopItem; } } } return null; } public async CTask> BuyItem(int shopid, int count) { if (isBuy) { return null; } isBuy = true; ShopConfig shopConfig = ConfigComponent.Instance.Get(shopid); ShopItemConfig shopItemConfig = ConfigComponent.Instance.Get(shopConfig.shopItemID); AccountFileInfo.ShopData shopData = GetDbShop(shopConfig.shopGroup); AccountFileInfo.ShopItem shopItem = null; foreach (var s in shopData.shopItemList) { if (s.id == shopid) { shopItem = s; break; } } if (shopData == null || shopItem == null || (shopData.refreshTime != -1 && PlayerManager.Instance.serverTime > shopData.refreshTime) || (shopItem.endTime != -1 && PlayerManager.Instance.serverTime > shopItem.endTime) || (shopItemConfig.buyCount != -1 && shopItem.buyCount >= shopItemConfig.buyCount) ) { isBuy = false; return null; } bool isOk = false; //免费 if (shopItemConfig.costItemId == 0) { isOk = true; } //人名币购买 else if (shopItemConfig.costItemId == 1031) { #if UNITY_ANDROID && !UNITY_EDITOR var dic = new Dictionary(); dic.Add("ShopItemId",shopItemConfig.ID.ToString()); YouLoftSDK.Instance.CustomEvent("OnclickBuyItem",dic); isOk = await YouLoftSDK.Instance.Buy(shopItemConfig.giftID); #elif UNITY_EDITOR isOk = true; #endif } //看广告 else if (shopItemConfig.costItemId == 1032) { var dic = new Dictionary(); dic.Add("buyItem", shopItemConfig.ID.ToString()); // YouLoftSDK.Instance.CustomEvent("OnclickAds", dic); // isOk = await YouLoftSDK.Instance.ShowAd(); isOk = true; if (isOk) { dic = new Dictionary(); dic.Add("buyItem", shopItemConfig.ID.ToString()); // YouLoftSDK.Instance.CustomEvent("AdsPlayOver", dic); } } else { if (PlayerManager.Instance.BagController.DeductItem(shopItemConfig.costItemId, shopItemConfig.price * count)) { isOk = true; } } if (isOk) { List itemList = new List(); shopItem.buyCount += count; for (var i = 0; i < shopItemConfig.itemId.Length; i++) { ItemInfo itemInfo = new ItemInfo(shopItemConfig.itemId[i], shopItemConfig.itemCount[i] * count); itemList.Add(itemInfo); } PlayerManager.Instance.BagController.AddItem(itemList); //更新商店数据 AccountFileInfo.Instance.SavePlayerData(); isBuy = false; if (shopItemConfig.costItemId == 1031) { var dic = new Dictionary(); dic.Add("ShopItemId", shopItemConfig.ID.ToString()); // YouLoftSDK.Instance.CustomEvent("BuyItemOver", dic); } // EventManager.Instance.Dispatch(CustomEventType.RefenceRed, null); return itemList; } else { isBuy = false; return null; } } }