ItemDataManager.cs 15 KB

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