ItemDataManager.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. if (itemCfg.param2Arr.Length == 0)
  177. {
  178. return 0;
  179. }
  180. return itemCfg.param2Arr[0];
  181. }
  182. public static void InitServerData(List<ItemInfoProto> items)
  183. {
  184. _dataDic.Clear();
  185. foreach (ItemInfoProto roleItem in items)
  186. {
  187. //LogUtil.LogEditor($"ItemDataManager InitServerData {roleItem.ConfigId}");
  188. Add(roleItem);
  189. }
  190. DressUpMenuItemDataManager.StartPreLoadItemCfg();
  191. }
  192. public static void InitServerDataItemAttribute(List<ItemAttributeProto> infos)
  193. {
  194. foreach (var info in infos)
  195. {
  196. if (_dataDic.TryGetValue(info.ConfigId, out var itemData))
  197. {
  198. itemData.SetAttributes(info.Ks, info.Vs);
  199. relativeSuitPerPromote(info.ConfigId,0,0,info.Ks, info.Vs);
  200. }
  201. }
  202. }
  203. public static void InitItemExchange(int itemId, int exchangTimes)
  204. {
  205. if (_itemExchangeDic.ContainsKey(itemId))
  206. {
  207. _itemExchangeDic[itemId] = exchangTimes;
  208. }
  209. else
  210. {
  211. _itemExchangeDic.Add(itemId, exchangTimes);
  212. }
  213. }
  214. //获取物品已兑换次数
  215. public static int GetItemExchangeTimes(int itemId)
  216. {
  217. if (_itemExchangeDic.ContainsKey(itemId) == false)
  218. {
  219. InitItemExchange(itemId, 0);
  220. }
  221. return _itemExchangeDic[itemId];
  222. }
  223. public static void SetAttribute(int itemId, int key, int value)
  224. {
  225. if (_dataDic.TryGetValue(itemId, out var itemData))
  226. {
  227. itemData.SetAttribute(key, value);
  228. relativeSuitPerPromote(itemId, 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. /// 飞花令临时接口
  279. public static int GetArenaScore(int itemId, int scoreType, string[] tags = null)
  280. {
  281. if (_dataDic.TryGetValue(itemId, out var itemData))
  282. {
  283. int scroe = 0;
  284. if (tags != null)
  285. {
  286. scroe += GetItemTagScore(itemId, tags);
  287. }
  288. scroe += itemData.GetScore(scoreType);
  289. return scroe;
  290. }
  291. else
  292. {
  293. ItemCfg itemdate = ItemCfgArray.Instance.GetCfg(itemId);
  294. ItemData itemDate = new ItemData() { };
  295. if (itemdate != null)
  296. {
  297. itemDate.id = itemdate.id;
  298. int scroe = 0;
  299. if (tags != null)
  300. {
  301. scroe += ItemDataManager.GetItemTagScore(itemId, tags);
  302. }
  303. scroe += itemDate.GetScore(scoreType);
  304. return scroe;
  305. }
  306. }
  307. return 0;
  308. }
  309. /// <summary>
  310. /// 获取茶话会item物品的属性
  311. /// </summary>
  312. /// <param name="itemId"></param>
  313. /// <param name="scoreType"></param>
  314. /// <returns></returns>
  315. public static int GetItemAddTeaPartyTagsScore(int itemId)//, int scoreType, string[] tags = null)
  316. {
  317. if (_dataDic.TryGetValue(itemId, out var itemData))
  318. {
  319. int scroe = 0;
  320. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  321. if (itemCfg.tagsArr != null)
  322. {
  323. foreach (var info in itemCfg.tagsArr)
  324. {
  325. scroe += Convert.ToInt32(info[1]);
  326. }
  327. }
  328. return scroe;
  329. }
  330. return 0;
  331. }
  332. /// <summary>
  333. /// 获取一个换装部件对应的标签分数
  334. /// </summary>
  335. /// <param name="itemId"></param>
  336. /// <param name="tags"></param>
  337. /// <returns></returns>
  338. public static int GetItemTagScore(int itemId, string[] tags)
  339. {
  340. int score = 0;
  341. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  342. if (itemCfg == null)
  343. {
  344. ET.Log.Error("物品:" + itemId + "不存在");
  345. return score;
  346. }
  347. for (int i = 0; i < itemCfg.tagsArr.Length; i++)
  348. {
  349. for (int j = 0; j < tags.Length; j++)
  350. {
  351. if (itemCfg.tagsArr[i][0] == tags[j])
  352. {
  353. score += int.Parse(itemCfg.tagsArr[i][1]);
  354. }
  355. }
  356. }
  357. return score;
  358. }
  359. /// <summary>
  360. /// 检测一件服装是否包含要求的标签
  361. /// </summary>
  362. /// <returns></returns>
  363. public static bool CheckItemTagsRight(int itemId, string[] tags)
  364. {
  365. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  366. if(itemCfg == null)
  367. {
  368. return false;
  369. }
  370. for (int i = 0; i < itemCfg.tagsArr.Length; i++)
  371. {
  372. for (int j = 0; j < tags.Length; j++)
  373. {
  374. if (itemCfg.tagsArr[i][0] == tags[j]) return true;
  375. }
  376. }
  377. return false;
  378. }
  379. /// <summary>
  380. /// 检测背包中是否存在礼包
  381. /// </summary>
  382. /// <returns></returns>
  383. public static bool BagIsExistGiftBag()
  384. {
  385. var isExistGiftBag = _dataDic.Values.Any(a =>
  386. ((a.itemType == ConstItemType.USEABLE && a.subType == ConstItemSubType.USEABLE_GIFT_BAG_SELECTABLE) ||
  387. (a.itemType == ConstItemType.USEABLE && a.subType == ConstItemSubType.USEABLE_GIFT_BAG_RANDOM))
  388. && a.id != 6003001 && a.id != 6003002);
  389. return isExistGiftBag;
  390. }
  391. /// <summary>
  392. /// 相对应的染色套装也需要乘以(服装升级)相应提升百分比
  393. /// </summary>
  394. /// <returns></returns>
  395. public static void relativeSuitPerPromote(int itemId, int keyInt = 0, int valueInt = 0, List<int> keyList = null, List<int> valueList = null)
  396. {
  397. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  398. List<SuitFosterCfg> fosterCfgs = SuitFosterCfgArray.Instance.GetCfgsBysuitId(itemCfg.suitId);
  399. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(fosterCfgs[0].suitId2);
  400. if (suitCfg == null)
  401. {
  402. Log.Error($"");
  403. }
  404. for (int i = 0; i < suitCfg.partsArr.Length; i++)
  405. {
  406. ItemCfg itemPartCfg = ItemCfgArray.Instance.GetCfg(suitCfg.partsArr[i]);
  407. if (itemPartCfg.subType == itemCfg.subType)
  408. {
  409. if (_dataDic.TryGetValue(itemPartCfg.id, out var itemPartData))
  410. {
  411. if(keyInt > 0)
  412. itemPartData.SetAttribute(keyInt, valueInt);
  413. else if (keyList.Count > 0)
  414. itemPartData.SetAttributes(keyList, valueList);
  415. }
  416. break;
  417. }
  418. }
  419. }
  420. public static bool dataDicOfItemid(int itemId)
  421. {
  422. if (_dataDic.ContainsKey(itemId))
  423. {
  424. return true;
  425. }
  426. return false;
  427. }
  428. }
  429. }