ItemDataManager.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using ET;
  5. using UnityEngine;
  6. namespace GFGGame
  7. {
  8. public class ItemDataManager
  9. {
  10. private static Dictionary<int, ItemData> _dataDic = new Dictionary<int, ItemData>();
  11. private static Dictionary<int, int> _itemExchangeDic = new Dictionary<int, int>();
  12. delegate object MemberGetDelegate(ItemCfg p);
  13. public static void Clear()
  14. {
  15. _itemExchangeDic.Clear();
  16. }
  17. public static void Add(ItemInfoProto itemInfoProto)
  18. {
  19. //初始化时禁止使用物品配置,会造成卡顿!!!
  20. var itemID = itemInfoProto.ConfigId;
  21. ItemData itemData = null;
  22. if (_dataDic.ContainsKey(itemID))
  23. {
  24. itemData = _dataDic[itemID];
  25. }
  26. else
  27. {
  28. itemData = ItemDataPool.GetItemData(itemID);
  29. itemData.itemType = itemInfoProto.Type;
  30. itemData.subType = itemInfoProto.SubType;
  31. itemData.rarity = itemInfoProto.Rarity;
  32. _dataDic.Add(itemID, itemData);
  33. }
  34. itemData.num = itemInfoProto.Count;
  35. if (itemInfoProto.Type == ConstItemType.DRESS_UP && itemID % GameConst.MAX_COUNT_EVERY_TYPE_ITEM > 0)
  36. {
  37. DressUpMenuItemDataManager.Add(itemInfoProto);
  38. //游戏角色初始数据完成后才执行
  39. if (GameGlobal.PreDataInited)
  40. {
  41. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemID);
  42. FunctionOpenCfg functionOpenCfg = FunctionOpenCfgArray.Instance.GetCfg(typeof(ClothingListView).Name);
  43. if (itemCfg.rarity == ConstDressRarity.Rarity_TIANYI
  44. && StorageDataManager.Instance.GetStorageValue(ConstStorageId.FUNCTION_OPEN +
  45. functionOpenCfg.index) == 0)
  46. {
  47. FunctionOpenDataManager.Instance.CheckHasSpecialFunOpen();
  48. }
  49. }
  50. }
  51. if ((itemInfoProto.Type == ConstItemType.ITEM || itemInfoProto.Type == ConstItemType.USEABLE) &&
  52. itemData.num > 0)
  53. {
  54. BagDataManager.Instance.Add(itemData);
  55. }
  56. if (itemInfoProto.Type == ConstItemType.HEAD)
  57. {
  58. RoleInfoManager.Instance.Add(itemInfoProto);
  59. if (GameGlobal.PreDataInited)
  60. {
  61. RoleInfoManager.Instance.AddNew(itemID);
  62. EventAgent.DispatchEvent(ConstMessage.RED_CHANGE);
  63. }
  64. }
  65. PhotographDataManager.Instance.Add(itemInfoProto);
  66. if(GameGlobal.PreDataInited)
  67. {
  68. EventAgent.DispatchEvent(ConstMessage.ITEM_CHANGED, itemID);
  69. }
  70. }
  71. public static void Remove(int itemID, long itemNum)
  72. {
  73. if (_dataDic.ContainsKey(itemID))
  74. {
  75. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemID);
  76. ItemData itemData = _dataDic[itemID];
  77. itemData.num -= itemNum;
  78. if (itemData.num <= 0)
  79. {
  80. itemData.num = 0;
  81. _dataDic.Remove(itemID);
  82. if (ItemUtilCS.IsDressUpItem(itemID))
  83. {
  84. DressUpMenuItemDataManager.Remove(itemID);
  85. }
  86. if (itemCfg.itemType == ConstItemType.ITEM)
  87. {
  88. BagDataManager.Instance.Remove(itemID);
  89. if(itemCfg.subType == ConstItemSubType.MATERIAL_SKILL_BOOK)
  90. DecomposeDataManager.Instance.RemoveMaterial(itemID);
  91. }
  92. if (itemCfg.itemType == ConstItemType.USEABLE)
  93. {
  94. BagDataManager.Instance.Remove(itemID);
  95. }
  96. if (itemCfg.itemType == ConstItemType.HEAD)
  97. {
  98. RoleInfoManager.Instance.Remove(itemID);
  99. }
  100. }
  101. if (itemCfg.itemType == ConstItemType.DRESS_UP)//&& !DressUpMenuItemCfg1Array.Instance.CheckIsSceneType(itemID)
  102. {
  103. DecomposeDataManager.Instance.Remove(itemID);
  104. }
  105. EventAgent.DispatchEvent(ConstMessage.ITEM_CHANGED, itemID);
  106. }
  107. }
  108. public static long GetItemNum(int itemId)
  109. {
  110. int numericType = NumericUtil.GetNumericTypeByItemId(itemId);
  111. if (numericType != 0)
  112. {
  113. return GameGlobal.myNumericComponent.GetAsInt(numericType);
  114. }
  115. int leagueType = NumericUtil.GetLeagueNumericTypeByItemId(itemId);
  116. if (leagueType != 0)
  117. {
  118. return LeagueDataManager.Instance.GetNumeriValue(leagueType);
  119. }
  120. if (_dataDic.TryGetValue(itemId, out var itemData))
  121. {
  122. return itemData.num;
  123. }
  124. return 0;
  125. }
  126. public static int GetItemType(int itemId)
  127. {
  128. _dataDic.TryGetValue(itemId, out var value);
  129. if(value != null)
  130. {
  131. return value.itemType;
  132. }
  133. //Debug.Log($"ItemDataManager GetItemType itemId {itemId}");
  134. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  135. return itemCfg.itemType;
  136. }
  137. public static int[][] GetItemSyntheticSuitArr(int itemId)
  138. {
  139. _dataDic.TryGetValue(itemId, out var value);
  140. if (value != null && value.syntheticMateriarsArr != null && value.syntheticMateriarsArr.Length > 0)
  141. {
  142. return value.syntheticMateriarsArr;
  143. }
  144. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  145. return itemCfg.syntheticMateriarsArr;
  146. }
  147. public static int GetItemSubType(int itemId)
  148. {
  149. _dataDic.TryGetValue(itemId, out var value);
  150. if (value != null)
  151. {
  152. return value.subType;
  153. }
  154. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  155. return itemCfg.subType;
  156. }
  157. public static int GetItemRarity(int itemId)
  158. {
  159. _dataDic.TryGetValue(itemId, out var value);
  160. if (value != null)
  161. {
  162. return value.rarity;
  163. }
  164. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  165. return itemCfg.rarity;
  166. }
  167. public static int GetItemSkillId(int itemId)
  168. {
  169. _dataDic.TryGetValue(itemId, out var value);
  170. if (value != null && value.param2Arr != null)
  171. {
  172. return value.param2Arr[0];
  173. }
  174. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  175. return itemCfg.param2Arr[0];
  176. }
  177. public static void InitServerData(List<ItemInfoProto> items)
  178. {
  179. _dataDic.Clear();
  180. foreach (ItemInfoProto roleItem in items)
  181. {
  182. //LogHelper.LogEditor($"ItemDataManager InitServerData {roleItem.ConfigId}");
  183. Add(roleItem);
  184. }
  185. //DressUpMenuItemDataManager.StartPreLoadItemCfg();
  186. //_ = DressUpMenuItemDataManager.GetAllDressUpGuideIdListBySubTypeAsync();
  187. }
  188. public static void InitServerDataItemAttribute(List<ItemAttributeProto> infos)
  189. {
  190. foreach (var info in infos)
  191. {
  192. if (_dataDic.TryGetValue(info.ConfigId, out var itemData))
  193. {
  194. itemData.SetAttributes(info.Ks, info.Vs);
  195. }
  196. }
  197. }
  198. public static void InitItemExchange(int itemId, int exchangTimes)
  199. {
  200. if (_itemExchangeDic.ContainsKey(itemId))
  201. {
  202. _itemExchangeDic[itemId] = exchangTimes;
  203. }
  204. else
  205. {
  206. _itemExchangeDic.Add(itemId, exchangTimes);
  207. }
  208. }
  209. //获取物品已兑换次数
  210. public static int GetItemExchangeTimes(int itemId)
  211. {
  212. if (_itemExchangeDic.ContainsKey(itemId) == false)
  213. {
  214. InitItemExchange(itemId, 0);
  215. }
  216. return _itemExchangeDic[itemId];
  217. }
  218. public static void SetAttribute(int itemId, int key, int value)
  219. {
  220. if (_dataDic.TryGetValue(itemId, out var itemData))
  221. {
  222. itemData.SetAttribute(key, value);
  223. }
  224. }
  225. /// <summary>
  226. /// 获取表格配置的基础属性
  227. /// </summary>
  228. /// <param name="itemId"></param>
  229. /// <param name="scoreType"></param>
  230. /// <returns></returns>
  231. public static int GetItemBaseScoreValue(int itemId, int scoreType)
  232. {
  233. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  234. if (scoreType == 1)
  235. {
  236. return itemCfg.score1;
  237. }
  238. else if (scoreType == 2)
  239. {
  240. return itemCfg.score2;
  241. }
  242. else if (scoreType == 3)
  243. {
  244. return itemCfg.score3;
  245. }
  246. else if (scoreType == 4)
  247. {
  248. return itemCfg.score4;
  249. }
  250. return 0;
  251. }
  252. /// <summary>
  253. /// 获取当前(养护/升级/升星...后)的属性
  254. /// </summary>
  255. /// <param name="itemId"></param>
  256. /// <param name="scoreType"></param>
  257. /// <returns></returns>
  258. public static int GetItemAdditionScore(int itemId, int scoreType, string[] tags = null)
  259. {
  260. if (_dataDic.TryGetValue(itemId, out var itemData))
  261. {
  262. int scroe = 0;
  263. if (tags != null)
  264. {
  265. scroe += GetItemTagScore(itemId, tags);
  266. }
  267. scroe += itemData.GetScore(scoreType);
  268. return scroe;
  269. }
  270. return 0;
  271. }
  272. /// <summary>
  273. /// 获取茶话会item物品的属性
  274. /// </summary>
  275. /// <param name="itemId"></param>
  276. /// <param name="scoreType"></param>
  277. /// <returns></returns>
  278. public static int GetItemAddTeaPartyTagsScore(int itemId)//, int scoreType, string[] tags = null)
  279. {
  280. if (_dataDic.TryGetValue(itemId, out var itemData))
  281. {
  282. int scroe = 0;
  283. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  284. if (itemCfg.tagsArr != null)
  285. {
  286. foreach (var info in itemCfg.tagsArr)
  287. {
  288. scroe += Convert.ToInt32(info[1]);
  289. }
  290. }
  291. return scroe;
  292. }
  293. return 0;
  294. }
  295. /// <summary>
  296. /// 获取一个换装部件对应的标签分数
  297. /// </summary>
  298. /// <param name="itemId"></param>
  299. /// <param name="tags"></param>
  300. /// <returns></returns>
  301. public static int GetItemTagScore(int itemId, string[] tags)
  302. {
  303. int score = 0;
  304. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  305. if (itemCfg == null)
  306. {
  307. ET.Log.Error("物品:" + itemId + "不存在");
  308. return score;
  309. }
  310. for (int i = 0; i < itemCfg.tagsArr.Length; i++)
  311. {
  312. for (int j = 0; j < tags.Length; j++)
  313. {
  314. if (itemCfg.tagsArr[i][0] == tags[j])
  315. {
  316. score += int.Parse(itemCfg.tagsArr[i][1]);
  317. }
  318. }
  319. }
  320. return score;
  321. }
  322. /// <summary>
  323. /// 检测一件服装是否包含要求的标签
  324. /// </summary>
  325. /// <returns></returns>
  326. public static bool CheckItemTagsRight(int itemId, string[] tags)
  327. {
  328. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  329. for (int i = 0; i < itemCfg.tagsArr.Length; i++)
  330. {
  331. for (int j = 0; j < tags.Length; j++)
  332. {
  333. if (itemCfg.tagsArr[i][0] == tags[j]) return true;
  334. }
  335. }
  336. return false;
  337. }
  338. /// <summary>
  339. /// 检测背包中是否存在礼包
  340. /// </summary>
  341. /// <returns></returns>
  342. public static bool BagIsExistGiftBag()
  343. {
  344. var isExistGiftBag = _dataDic.Values.Any(a =>
  345. ((a.itemType == ConstItemType.USEABLE && a.subType == ConstItemSubType.USEABLE_GIFT_BAG_SELECTABLE) ||
  346. (a.itemType == ConstItemType.USEABLE && a.subType == ConstItemSubType.USEABLE_GIFT_BAG_RANDOM))
  347. && a.id != 6003001 && a.id != 6003002);
  348. return isExistGiftBag;
  349. }
  350. }
  351. }