ItemDataManager.cs 13 KB

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