ItemUtil.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. using ET;
  2. using FairyGUI;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using cfg;
  8. using cfg.GfgCfg;
  9. using UI.CommonGame;
  10. using UnityEngine;
  11. namespace GFGGame
  12. {
  13. public static class ItemUtil
  14. {
  15. /// <param name="showTips">是否弹购买成功飘字,默认是</param>
  16. /// <param name="openSource">是否打开来源界面。默认否</param>
  17. /// <param name="showTxtBuyTips">是否显示购买提示</param>
  18. public static void ExchangeItemById(int itemId, long num, bool showTips = true, Action onSuccess = null,
  19. bool openSource = false, int maxCount = 9990, bool showTxtBuyTips = false, string prefix = "")
  20. {
  21. if (!BuyCurrency(itemId, num, onSuccess))
  22. {
  23. BuyItemConteoller.Show(itemId, num, ConstBuyType.TYPE_ITEM, onSuccess, showTips, openSource, maxCount);
  24. BuyItemConteoller.showTxtBuyTips = showTxtBuyTips;
  25. }
  26. }
  27. public static bool BuyCurrency(int itemId, long num, Action onSuccess = null)
  28. {
  29. switch (itemId)
  30. {
  31. case ConstItemID.GOLD:
  32. AddGold(onSuccess);
  33. return true;
  34. case ConstItemID.POWER:
  35. AddPower();
  36. return true;
  37. case ConstItemID.DIAMOND_RED:
  38. AddDiamondRed(num);
  39. return true;
  40. case ConstItemID.DIAMOND_PURPLE:
  41. ItemUtil.AddDiamondPurple();
  42. return true;
  43. }
  44. return false;
  45. }
  46. public static void AddPower(Action onSuccess = null, int type = 0)
  47. {
  48. EnduringGiftBoxController.Show(ConstItemID.POWER, 1, () =>
  49. {
  50. if (onSuccess != null)
  51. {
  52. onSuccess();
  53. }
  54. }, "", type);
  55. }
  56. public static void AddGold(Action onSuccess = null, int type = 0)
  57. {
  58. EnduringGiftBoxController.Show(ConstItemID.GOLD, 1, () =>
  59. {
  60. if (onSuccess != null)
  61. {
  62. onSuccess();
  63. }
  64. }, "", type);
  65. }
  66. public static void AddDiamondPurple()
  67. {
  68. //ViewManager.Show<RechargeStoreView>();
  69. ViewManager.Show<StoreView>(new object[] { ConstStoreTabId.STORE_CHARGE, ConstStoreSubId.STORE_CHARGE });
  70. EventAgent.DispatchEvent(ConstMessage.CLOSE_SHOPPING_TIP);
  71. }
  72. //红钻(鲛绡)
  73. public static void AddDiamondRed(long value = 0, Action onSuccess = null)
  74. {
  75. ItemExchangeCfg
  76. currencyRatioCfg = CommonDataManager.Tables.TblItemExchangeCfg.GetOrDefault(ConstItemID.DIAMOND_RED);
  77. BuyItemConteoller.Show(currencyRatioCfg.Id, value > 0 ? value : 1, ConstBuyType.TYPE_ITEM, onSuccess, true,
  78. true);
  79. BuyItemConteoller.showTxtBuyTips = true;
  80. }
  81. public static string GetItemName(int itemId)
  82. {
  83. ItemCfg itemCfg = CommonDataManager.Tables.TblItemCfg.GetOrDefault(itemId);
  84. return itemCfg.Name;
  85. }
  86. public static string GetSuitName(int suitId)
  87. {
  88. SuitCfg suitCfg = CommonDataManager.Tables.TblSuitCfg.GetOrDefault(suitId);
  89. return suitCfg.Name;
  90. }
  91. public static int GetItemRarity(int itemId)
  92. {
  93. ItemCfg itemCfg = CommonDataManager.Tables.TblItemCfg.GetOrDefault(itemId);
  94. return itemCfg.Rarity;
  95. }
  96. public static List<ItemData> CreateItemDataList(List<ItemInfoProto> items)
  97. {
  98. List<ItemData> itemList = new List<ItemData>();
  99. for (int i = 0; i < items.Count; i++)
  100. {
  101. ItemData itemData = createItemData(items[i].ConfigId, items[i].Count);
  102. itemList.Add(itemData);
  103. }
  104. return itemList;
  105. }
  106. public static List<ItemData> CreateItemDataList(List<ItemParamProto> bonus, bool isOnceBonus = false)
  107. {
  108. List<ItemData> itemList = new List<ItemData>();
  109. foreach (ItemParamProto item in bonus)
  110. {
  111. ItemData itemData = createItemData(item);
  112. itemList.Add(itemData);
  113. itemData.isOnceBonus = isOnceBonus;
  114. }
  115. return itemList;
  116. }
  117. public static List<ItemData> CreateItemDataList(List<ItemParamProto> bonus, long num)
  118. {
  119. List<ItemData> itemList = new List<ItemData>();
  120. foreach (var item in bonus)
  121. {
  122. int count = 0;
  123. if (item != null)
  124. {
  125. count = item.Count;
  126. }
  127. else
  128. {
  129. count = 1;
  130. }
  131. ItemData itemData = createItemData(item.ItemId, count * num);
  132. itemList.Add(itemData);
  133. }
  134. return itemList;
  135. }
  136. public static List<ItemData> CreateItemDataList(int itemId, long count)
  137. {
  138. List<ItemData> itemList = new List<ItemData>();
  139. ItemData itemData = createItemData(itemId, count);
  140. itemList.Add(itemData);
  141. return itemList;
  142. }
  143. public static ItemData createItemData(ItemParamProto bonus)
  144. {
  145. int itemID = bonus.ItemId;
  146. int itemNum = bonus.Count;
  147. return createItemData(itemID, itemNum);
  148. }
  149. public static ItemData createItemData(int itemId, long count, long countRandomeMin = 0)
  150. {
  151. ItemData itemData = ItemDataPool.GetItemData(itemId);
  152. itemData.num = count;
  153. itemData.numRandomeMin = countRandomeMin;
  154. return itemData;
  155. }
  156. public static Boolean CheckItemEnough(int itemId, long num, bool showTips = false)
  157. {
  158. long hasNum = ItemDataManager.GetItemNum(itemId);
  159. if (hasNum < num && showTips)
  160. {
  161. ItemCfg itemCfg = CommonDataManager.Tables.TblItemCfg.GetOrDefault(itemId);
  162. PromptController.Instance.ShowFloatTextPrompt(string.Format("【{0}】 不足", itemCfg.Name));
  163. }
  164. return hasNum >= num;
  165. }
  166. public static Boolean CheckItemEnough(int[][] items, int count = 1)
  167. {
  168. return items.All(item => CheckItemEnough(item[0], item[1] * count));
  169. }
  170. public static Boolean CheckItemEnough(System.Collections.Generic.List<cfg.ItemParam> items)
  171. {
  172. return items.All(item => CheckItemEnough(item.ItemId, item.Count));
  173. }
  174. public static bool CheckMenuType1(int needItemId, int needSuitId, int typeId)
  175. {
  176. DressUpMenuItemCfg1 cfg1 = CommonDataManager.Tables.TblDressUpMenuItemCfg1.GetOrDefault(typeId);
  177. if (needItemId > 0)
  178. {
  179. int subType = ItemUtilCS.GetItemSubType(needItemId);
  180. if (subType == cfg1.Type)
  181. {
  182. return true;
  183. }
  184. else if (cfg1.SubMenus.Count > 0)
  185. {
  186. foreach (int id2 in cfg1.SubMenus)
  187. {
  188. DressUpMenuItemCfg2 cfg2 = CommonDataManager.Tables.TblDressUpMenuItemCfg2.GetOrDefault(id2);
  189. if (cfg2.Type == subType)
  190. {
  191. return true;
  192. }
  193. }
  194. }
  195. }
  196. else if (needSuitId > 0 && cfg1.Type == ConstDressUpItemType.TAO_ZHUANG)
  197. {
  198. return true;
  199. }
  200. return false;
  201. }
  202. public static void SortItemIdsByOwned(int[] array)
  203. {
  204. Array.Sort(array, (int a, int b) =>
  205. {
  206. bool ownedA = ItemDataManager.GetItemNum(a) > 0;
  207. bool ownedB = ItemDataManager.GetItemNum(b) > 0;
  208. if (!ownedB && ownedA)
  209. {
  210. return 1;
  211. }
  212. else if (ownedB && !ownedA)
  213. {
  214. return -1;
  215. }
  216. return 0;
  217. });
  218. }
  219. public static string GetItemResExt(int itemType, int type, bool isIcon = false)
  220. {
  221. if (itemType == ConstItemType.DRESS_UP && type == ConstDressUpItemType.BEI_JING && !isIcon)
  222. {
  223. return "jpg";
  224. }
  225. return "png";
  226. }
  227. public static void UpdateItemNumAndNeedNum(GObject obj, int itemId, int needNum, bool ChangeColor = false)
  228. {
  229. UI_ComCostCurrencyWithHas com = UI_ComCostCurrencyWithHas.Proxy(obj);
  230. long hasNum = ItemDataManager.GetItemNum(itemId);
  231. string needStrColor = ChangeColor ? "#B99F7B" : "#FDEED4";
  232. string strHasNum = StringUtil.GetColorText(hasNum.ToString(), hasNum < needNum ? "#C5645A" : needStrColor);
  233. hasNum.ToString();
  234. // com.m_txtNeed.text = needNum.ToString();
  235. com.m_txtCount.text = string.Format("{0}{1}{2}", strHasNum, StringUtil.GetColorText("/", needStrColor),
  236. StringUtil.GetColorText(needNum.ToString(), needStrColor));
  237. ItemCfg cfg1 = CommonDataManager.Tables.TblItemCfg.GetOrDefault(itemId);
  238. com.m_loaIcon.url = ResPathUtil.GetCommonGameResPath(cfg1.Res);
  239. }
  240. public static void UpdateItemAndNeed(GObject obj, int itemId, int needNum, bool ChangeColor = false)
  241. {
  242. UI_ComCostCurrencyWithHas com = UI_ComCostCurrencyWithHas.Proxy(obj);
  243. long hasNum = ItemDataManager.GetItemNum(itemId);
  244. string needStrColor = ChangeColor ? "#B99F7B" : "#FDEED4";
  245. string strHasNum = StringUtil.GetColorText(hasNum.ToString(), hasNum < needNum ? "#C5645A" : needStrColor);
  246. hasNum.ToString();
  247. // com.m_txtNeed.text = needNum.ToString();
  248. com.m_txtCount.text = string.Format("{0}{1}{2}", strHasNum, StringUtil.GetColorText("/", needStrColor),
  249. StringUtil.GetColorText(needNum.ToString(), needStrColor));
  250. ItemCfg cfg1 = CommonDataManager.Tables.TblItemCfg.GetOrDefault(itemId);
  251. com.m_loaIcon.url = ResPathUtil.GetIconPath(cfg1.Res, "png");
  252. }
  253. public static void UpdateItemNeedNum(GObject obj, int itemId, int needNum, bool checkNum = true,
  254. string color = "#716B59")
  255. {
  256. UI_ComCostCurrency com = UI_ComCostCurrency.Proxy(obj);
  257. com.m_c1.selectedIndex = needNum == 0 ? 0 : 1;
  258. long hasNum = ItemDataManager.GetItemNum(itemId);
  259. string strNeedNum = !checkNum
  260. ? StringUtil.GetColorText(needNum.ToString(), color)
  261. : StringUtil.GetColorText(needNum.ToString(), hasNum < needNum ? "#D0A09B" : color);
  262. needNum.ToString();
  263. com.m_txtNeed.text = strNeedNum;
  264. ItemCfg cfg = CommonDataManager.Tables.TblItemCfg.GetOrDefault(itemId);
  265. com.m_loaIcon.url = ResPathUtil.GetIconPath(cfg);
  266. UI_ComCostCurrency.ProxyEnd();
  267. }
  268. public static void UpdateItemNeedNum(GObject obj, ItemParamProto cost, bool checkNum = true,
  269. string color = "#716B59")
  270. {
  271. int itemId = cost.ItemId;
  272. int needNum = cost.Count;
  273. UI_ComCostCurrency com = UI_ComCostCurrency.Proxy(obj);
  274. com.m_c1.selectedIndex = needNum == 0 ? 0 : 1;
  275. long hasNum = ItemDataManager.GetItemNum(itemId);
  276. string strNeedNum = !checkNum
  277. ? StringUtil.GetColorText(needNum.ToString(), color)
  278. : StringUtil.GetColorText(needNum.ToString(), hasNum < needNum ? "#D0A09B" : color);
  279. needNum.ToString();
  280. com.m_txtNeed.text = strNeedNum;
  281. ItemCfg cfg = CommonDataManager.Tables.TblItemCfg.GetOrDefault(itemId);
  282. com.m_loaIcon.url = ResPathUtil.GetCommonGameResPath(cfg.Res);
  283. UI_ComCostCurrency.ProxyEnd();
  284. }
  285. public static void UpdateTag(GObject obj, string tag)
  286. {
  287. UI_ComTag item = UI_ComTag.Proxy(obj);
  288. int tagType = CommonDataManager.Tables.TblTagCfg.GetOrDefault(tag).Type;
  289. item.m_txtTag.text = tag;
  290. item.m_loaTag.url = ResPathUtil.GetCommonGameResPath("fzd_bqbq_" + tagType);
  291. UI_ComTag.ProxyEnd();
  292. }
  293. /// <summary>
  294. /// 合并奖励配置,会将同一物品id的奖励合并
  295. /// </summary>
  296. public static List<ItemParamProto> MergeBonus(List<ItemParamProto> item1, List<ItemParamProto> item2)
  297. {
  298. Dictionary<int, int> dictionary = new Dictionary<int, int>();
  299. if (item1 != null)
  300. {
  301. foreach (var item in item1)
  302. {
  303. if (dictionary.ContainsKey(item.ItemId))
  304. {
  305. dictionary[item.ItemId] += item.Count;
  306. }
  307. else
  308. {
  309. dictionary.Add(item.ItemId, item.Count);
  310. }
  311. }
  312. }
  313. if (item2 != null)
  314. {
  315. foreach (var item in item2)
  316. {
  317. if (dictionary.ContainsKey(item.ItemId))
  318. {
  319. dictionary[item.ItemId] += item.Count;
  320. }
  321. else
  322. {
  323. dictionary.Add(item.ItemId, item.Count);
  324. }
  325. }
  326. }
  327. List<ItemParamProto> result = new List<ItemParamProto>();
  328. int i = 0;
  329. foreach (KeyValuePair<int, int> item in dictionary)
  330. {
  331. result.Add(new ItemParamProto() { ItemId = item.Key, Count = item.Value });
  332. }
  333. return result;
  334. }
  335. public static void CreateItemView(ItemParamProto item, GComponent component)
  336. {
  337. var itemData = createItemData(item);
  338. component.data ??= new ItemView(component);
  339. (component.data as ItemView)?.SetData(itemData);
  340. (component.data as ItemView)?.ChangeTxtCountStyle();
  341. }
  342. public static List<ItemParamProto> ToGfgGameItemParam(this List<cfg.ItemParam> items)
  343. {
  344. List<ItemParamProto> result = new List<ItemParamProto>();
  345. foreach (cfg.ItemParam item in items)
  346. {
  347. result.Add(new ItemParamProto
  348. {
  349. ItemId = item.ItemId,
  350. Count = item.Count,
  351. });
  352. }
  353. return result;
  354. }
  355. public static ItemParamProto ToGfgGameItemParam(this cfg.ItemParam item)
  356. {
  357. ItemParamProto result = new ItemParamProto() { ItemId = item.ItemId, Count = item.Count };
  358. return result;
  359. }
  360. }
  361. }