ItemDataManager.cs 16 KB

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