123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- using System.Collections.Generic;
- using Excel2Json;
- using Fort23.Core;
- using Fort23.UTool;
- using GameLogic.Bag;
- using Utility;
- public class ShopManger : Singleton<ShopManger>
- {
- bool isBuy = false;
- Dictionary<int, List<ShopConfig>> shopDic = new Dictionary<int, List<ShopConfig>>();
- private Dictionary<string, string> productDic = new Dictionary<string, string>();
- #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
- /// <summary>
- /// 初始化数据
- /// </summary>
- public ShopManger()
- {
- ShopConfig[] shopConfigs = ConfigComponent.Instance.GetAll<ShopConfig>();
- foreach (var config in shopConfigs)
- {
- List<ShopConfig> shopConfigList = null;
- if (!shopDic.ContainsKey(config.shopGroup))
- {
- shopConfigList = new List<ShopConfig>();
- 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<Product>
- if (json != null && json != "")
- {
- List<object> data = Json.Deserialize(json) as List<object>;
- foreach (var o in data)
- {
- Dictionary<string, object> deserialize = o as Dictionary<string, object>;
- 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<AccountFileInfo.ShopData> 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<ShopGroupConfig>(a.id);
- ShopGroupConfig bb = ConfigComponent.Instance.Get<ShopGroupConfig>(b.id);
- return aa.sortNum.CompareTo(bb.sortNum);
- });
- AccountFileInfo.Instance.SavePlayerData();
- }
- public void checkShopItemUnlock(AccountFileInfo.ShopData shopData)
- {
- List<AccountFileInfo.ShopItem> removeItems = new List<AccountFileInfo.ShopItem>();
- 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<ShopGroupConfig>(id);
- shopData.id = id;
- if (shopGroupConfig.RefreshType == 0)
- {
- shopData.refreshTime = -1;
- }
- else
- {
- shopData.refreshTime = TimeHelper.GetBaseRefreshTime(PlayerManager.Instance.serverTime);
- }
- shopDic.TryGetValue(id, out List<ShopConfig> 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<List<ItemInfo>> BuyItem(int shopid, int count)
- {
- if (isBuy)
- {
- return null;
- }
- isBuy = true;
- ShopConfig shopConfig = ConfigComponent.Instance.Get<ShopConfig>(shopid);
- ShopItemConfig shopItemConfig = ConfigComponent.Instance.Get<ShopItemConfig>(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<string, string>();
- 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<string, string>();
- dic.Add("buyItem", shopItemConfig.ID.ToString());
- // YouLoftSDK.Instance.CustomEvent("OnclickAds", dic);
- // isOk = await YouLoftSDK.Instance.ShowAd();
- isOk = true;
- if (isOk)
- {
- dic = new Dictionary<string, string>();
- 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<ItemInfo> itemList = new List<ItemInfo>();
- 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<string, string>();
- dic.Add("ShopItemId", shopItemConfig.ID.ToString());
- // YouLoftSDK.Instance.CustomEvent("BuyItemOver", dic);
- }
- // EventManager.Instance.Dispatch(CustomEventType.RefenceRed, null);
- return itemList;
- }
- else
- {
- isBuy = false;
- return null;
- }
- }
- }
|