ShopManger.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. using System.Collections.Generic;
  2. using Excel2Json;
  3. using Fort23.Core;
  4. using Fort23.UTool;
  5. using GameLogic.Bag;
  6. using Utility;
  7. public class ShopManger : Singleton<ShopManger>
  8. {
  9. bool isBuy = false;
  10. Dictionary<int, List<ShopConfig>> shopDic = new Dictionary<int, List<ShopConfig>>();
  11. private Dictionary<string, string> productDic = new Dictionary<string, string>();
  12. #if UNITY_ANDROID && YouLoftSDK
  13. public string[] adsIdentifiers = new[]
  14. {
  15. "com.xy001.ads.18",
  16. "com.xy001.gem.6",
  17. "com.xy001.gem.30",
  18. "com.xy001.gem.68",
  19. "com.xy001.gem.128",
  20. "com.xy001.gem.328",
  21. "com.xy001.super1.6"
  22. };
  23. #elif UNITY_ANDROID && Google
  24. public string[] adsIdentifiers = new[]
  25. {
  26. "com.taptap.xy001.ads.18",
  27. "com.taptap.xy001.gem.6",
  28. "com.taptap.xy001.gem.30",
  29. "com.taptap.xy001.gem.68",
  30. "com.taptap.xy001.gem.128",
  31. "com.taptap.xy001.gem.328",
  32. "com.taptap.xy001.super1.6"
  33. };
  34. #endif
  35. /// <summary>
  36. /// 初始化数据
  37. /// </summary>
  38. public ShopManger()
  39. {
  40. ShopConfig[] shopConfigs = ConfigComponent.Instance.GetAll<ShopConfig>();
  41. foreach (var config in shopConfigs)
  42. {
  43. List<ShopConfig> shopConfigList = null;
  44. if (!shopDic.ContainsKey(config.shopGroup))
  45. {
  46. shopConfigList = new List<ShopConfig>();
  47. shopDic.Add(config.shopGroup, shopConfigList);
  48. }
  49. else
  50. {
  51. shopConfigList = shopDic[config.shopGroup];
  52. }
  53. shopConfigList.Add(config);
  54. }
  55. }
  56. public async CTask CustomInit()
  57. {
  58. #if UNITY_ANDROID && !UNITY_EDITOR && YouLoftSDK
  59. string json = await YouLoftSDK.Instance.QuerySkuDetail(adsIdentifiers);
  60. LogTool.Error(json);
  61. // 将 JSON 字符串直接反序列化为 List<Product>
  62. if (json != null && json != "")
  63. {
  64. List<object> data = Json.Deserialize(json) as List<object>;
  65. foreach (var o in data)
  66. {
  67. Dictionary<string, object> deserialize = o as Dictionary<string, object>;
  68. Product product = new Product();
  69. product.productId = (string)deserialize["productId"];
  70. product.type = (string)deserialize["type"];
  71. product.title = (string)deserialize["title"];
  72. product.name = (string)deserialize["name"];
  73. product.description = (string)deserialize["description"];
  74. product.price = (string)deserialize["price"];
  75. product.priceAmountMicros = (long)deserialize["price_amount_micros"];
  76. product.priceCurrencyCode = (string)deserialize["price_currency_code"];
  77. product.skuDetailsToken = (string)deserialize["skuDetailsToken"];
  78. if (!productDic.ContainsKey(product.productId))
  79. productDic.Add(product.productId, product);
  80. }
  81. }
  82. #elif UNITY_ANDROID && Google
  83. GetPrice();
  84. #endif
  85. InitShop();
  86. }
  87. public void GetPrice()
  88. {
  89. #if UNITY_ANDROID && Google
  90. foreach (var adsIdentifier in adsIdentifiers)
  91. {
  92. if (!productDic.ContainsKey(adsIdentifier))
  93. {
  94. string price = IAPManager.Instance.GetPrice(adsIdentifier);
  95. if (price != "")
  96. productDic.Add(adsIdentifier, price);
  97. }
  98. }
  99. #endif
  100. }
  101. public string GetPrice(string productId)
  102. {
  103. if (productDic.ContainsKey(productId))
  104. return productDic[productId];
  105. return "";
  106. }
  107. public List<AccountFileInfo.ShopData> GetAllShopConfig()
  108. {
  109. GetPrice();
  110. InitShop();
  111. return AccountFileInfo.Instance.playerData.shopDatas;
  112. }
  113. public void InitShop()
  114. {
  115. foreach (var keyValuePair in shopDic)
  116. {
  117. AccountFileInfo.ShopData shopData = GetDbShop(keyValuePair.Key);
  118. if (shopData != null && shopData.refreshTime != -1 &&
  119. PlayerManager.Instance.serverTime > shopData.refreshTime)
  120. {
  121. AccountFileInfo.Instance.playerData.shopDatas.Remove(shopData);
  122. shopData = null;
  123. }
  124. //初始化商店
  125. if (shopData == null)
  126. {
  127. shopData = RefenceShop(keyValuePair.Key);
  128. AccountFileInfo.Instance.playerData.shopDatas.Add(shopData);
  129. }
  130. else
  131. {
  132. checkShopItemUnlock(shopData);
  133. }
  134. }
  135. AccountFileInfo.Instance.playerData.shopDatas.Sort((AccountFileInfo.ShopData a, AccountFileInfo.ShopData b) =>
  136. {
  137. ShopGroupConfig aa = ConfigComponent.Instance.Get<ShopGroupConfig>(a.id);
  138. ShopGroupConfig bb = ConfigComponent.Instance.Get<ShopGroupConfig>(b.id);
  139. return aa.sortNum.CompareTo(bb.sortNum);
  140. });
  141. AccountFileInfo.Instance.SavePlayerData();
  142. }
  143. public void checkShopItemUnlock(AccountFileInfo.ShopData shopData)
  144. {
  145. List<AccountFileInfo.ShopItem> removeItems = new List<AccountFileInfo.ShopItem>();
  146. foreach (var shopItem in shopData.shopItemList)
  147. {
  148. if (shopItem.endTime != -1 && PlayerManager.Instance.serverTime >= shopItem.endTime)
  149. {
  150. removeItems.Add(shopItem);
  151. }
  152. }
  153. foreach (var removeItem in removeItems)
  154. {
  155. shopData.shopItemList.Remove(removeItem);
  156. }
  157. }
  158. public AccountFileInfo.ShopData RefenceShop(int id)
  159. {
  160. AccountFileInfo.ShopData shopData = new AccountFileInfo.ShopData();
  161. ShopGroupConfig shopGroupConfig = ConfigComponent.Instance.Get<ShopGroupConfig>(id);
  162. shopData.id = id;
  163. if (shopGroupConfig.RefreshType == 0)
  164. {
  165. shopData.refreshTime = -1;
  166. }
  167. else
  168. {
  169. shopData.refreshTime = TimeHelper.GetBaseRefreshTime(PlayerManager.Instance.serverTime);
  170. }
  171. shopDic.TryGetValue(id, out List<ShopConfig> shopConfigList);
  172. foreach (var shopConfig in shopConfigList)
  173. {
  174. AccountFileInfo.ShopItem shopItem = new AccountFileInfo.ShopItem();
  175. shopItem.id = shopConfig.ID;
  176. shopItem.buyCount = 0;
  177. if (shopConfig.continueTime > 0)
  178. {
  179. shopItem.endTime = PlayerManager.Instance.serverTime + shopConfig.continueTime * 60 * 1000;
  180. }
  181. shopData.shopItemList.Add(shopItem);
  182. }
  183. return shopData;
  184. }
  185. public AccountFileInfo.ShopData GetDbShop(int id)
  186. {
  187. foreach (var playerDataShopData in AccountFileInfo.Instance.playerData.shopDatas)
  188. {
  189. if (playerDataShopData.id == id)
  190. {
  191. return playerDataShopData;
  192. }
  193. }
  194. return null;
  195. }
  196. public AccountFileInfo.ShopItem GetShopItem(int id)
  197. {
  198. foreach (var shopData in AccountFileInfo.Instance.playerData.shopDatas)
  199. {
  200. foreach (var shopItem in shopData.shopItemList)
  201. {
  202. if (shopItem.id == id)
  203. {
  204. return shopItem;
  205. }
  206. }
  207. }
  208. return null;
  209. }
  210. public async CTask<List<ItemInfo>> BuyItem(int shopid, int count)
  211. {
  212. if (isBuy)
  213. {
  214. return null;
  215. }
  216. isBuy = true;
  217. ShopConfig shopConfig = ConfigComponent.Instance.Get<ShopConfig>(shopid);
  218. ShopItemConfig shopItemConfig = ConfigComponent.Instance.Get<ShopItemConfig>(shopConfig.shopItemID);
  219. AccountFileInfo.ShopData shopData = GetDbShop(shopConfig.shopGroup);
  220. AccountFileInfo.ShopItem shopItem = null;
  221. foreach (var s in shopData.shopItemList)
  222. {
  223. if (s.id == shopid)
  224. {
  225. shopItem = s;
  226. break;
  227. }
  228. }
  229. if (shopData == null || shopItem == null
  230. || (shopData.refreshTime != -1 && PlayerManager.Instance.serverTime > shopData.refreshTime)
  231. || (shopItem.endTime != -1 && PlayerManager.Instance.serverTime > shopItem.endTime)
  232. || (shopItemConfig.buyCount != -1 && shopItem.buyCount >= shopItemConfig.buyCount)
  233. )
  234. {
  235. isBuy = false;
  236. return null;
  237. }
  238. bool isOk = false;
  239. //免费
  240. if (shopItemConfig.costItemId == 0)
  241. {
  242. isOk = true;
  243. }
  244. //人名币购买
  245. else if (shopItemConfig.costItemId == 1031)
  246. {
  247. #if UNITY_ANDROID && !UNITY_EDITOR
  248. var dic = new Dictionary<string, string>();
  249. dic.Add("ShopItemId",shopItemConfig.ID.ToString());
  250. YouLoftSDK.Instance.CustomEvent("OnclickBuyItem",dic);
  251. isOk = await YouLoftSDK.Instance.Buy(shopItemConfig.giftID);
  252. #elif UNITY_EDITOR
  253. isOk = true;
  254. #endif
  255. }
  256. //看广告
  257. else if (shopItemConfig.costItemId == 1032)
  258. {
  259. var dic = new Dictionary<string, string>();
  260. dic.Add("buyItem", shopItemConfig.ID.ToString());
  261. // YouLoftSDK.Instance.CustomEvent("OnclickAds", dic);
  262. // isOk = await YouLoftSDK.Instance.ShowAd();
  263. isOk = true;
  264. if (isOk)
  265. {
  266. dic = new Dictionary<string, string>();
  267. dic.Add("buyItem", shopItemConfig.ID.ToString());
  268. // YouLoftSDK.Instance.CustomEvent("AdsPlayOver", dic);
  269. }
  270. }
  271. else
  272. {
  273. if (PlayerManager.Instance.BagController.DeductItem(shopItemConfig.costItemId, shopItemConfig.price * count))
  274. {
  275. isOk = true;
  276. }
  277. }
  278. if (isOk)
  279. {
  280. List<ItemInfo> itemList = new List<ItemInfo>();
  281. shopItem.buyCount += count;
  282. for (var i = 0; i < shopItemConfig.itemId.Length; i++)
  283. {
  284. ItemInfo itemInfo = new ItemInfo(shopItemConfig.itemId[i], shopItemConfig.itemCount[i] * count);
  285. itemList.Add(itemInfo);
  286. }
  287. PlayerManager.Instance.BagController.AddItem(itemList);
  288. //更新商店数据
  289. AccountFileInfo.Instance.SavePlayerData();
  290. isBuy = false;
  291. if (shopItemConfig.costItemId == 1031)
  292. {
  293. var dic = new Dictionary<string, string>();
  294. dic.Add("ShopItemId", shopItemConfig.ID.ToString());
  295. // YouLoftSDK.Instance.CustomEvent("BuyItemOver", dic);
  296. }
  297. // EventManager.Instance.Dispatch(CustomEventType.RefenceRed, null);
  298. return itemList;
  299. }
  300. else
  301. {
  302. isBuy = false;
  303. return null;
  304. }
  305. }
  306. }