ItemDataManager.cs 16 KB

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