ItemDataManager.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. }
  196. }
  197. }
  198. public static void InitItemExchange(int itemId, int exchangTimes)
  199. {
  200. if (_itemExchangeDic.ContainsKey(itemId))
  201. {
  202. _itemExchangeDic[itemId] = exchangTimes;
  203. }
  204. else
  205. {
  206. _itemExchangeDic.Add(itemId, exchangTimes);
  207. }
  208. }
  209. //获取物品已兑换次数
  210. public static int GetItemExchangeTimes(int itemId)
  211. {
  212. if (_itemExchangeDic.ContainsKey(itemId) == false)
  213. {
  214. InitItemExchange(itemId, 0);
  215. }
  216. return _itemExchangeDic[itemId];
  217. }
  218. public static void SetAttribute(int itemId, int key, int value)
  219. {
  220. if (_dataDic.TryGetValue(itemId, out var itemData))
  221. {
  222. itemData.SetAttribute(key, value);
  223. }
  224. }
  225. /// <summary>
  226. /// 获取表格配置的基础属性
  227. /// </summary>
  228. /// <param name="itemId"></param>
  229. /// <param name="scoreType"></param>
  230. /// <returns></returns>
  231. public static int GetItemBaseScoreValue(int itemId, int scoreType)
  232. {
  233. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  234. if (scoreType == 1)
  235. {
  236. return itemCfg.score1;
  237. }
  238. else if (scoreType == 2)
  239. {
  240. return itemCfg.score2;
  241. }
  242. else if (scoreType == 3)
  243. {
  244. return itemCfg.score3;
  245. }
  246. else if (scoreType == 4)
  247. {
  248. return itemCfg.score4;
  249. }
  250. return 0;
  251. }
  252. /// <summary>
  253. /// 获取当前(养护/升级/升星...后)的属性
  254. /// </summary>
  255. /// <param name="itemId"></param>
  256. /// <param name="scoreType"></param>
  257. /// <returns></returns>
  258. public static int GetItemAdditionScore(int itemId, int scoreType, string[] tags = null)
  259. {
  260. if (_dataDic.TryGetValue(itemId, out var itemData))
  261. {
  262. int scroe = 0;
  263. if (tags != null)
  264. {
  265. scroe += GetItemTagScore(itemId, tags);
  266. }
  267. scroe += itemData.GetScore(scoreType);
  268. return scroe;
  269. }
  270. return 0;
  271. }
  272. /// 飞花令临时接口
  273. public static int GetArenaScore(int itemId, int scoreType, string[] tags = null)
  274. {
  275. if (_dataDic.TryGetValue(itemId, out var itemData))
  276. {
  277. int scroe = 0;
  278. if (tags != null)
  279. {
  280. scroe += GetItemTagScore(itemId, tags);
  281. }
  282. scroe += itemData.GetScore(scoreType);
  283. return scroe;
  284. }
  285. else
  286. {
  287. ItemCfg itemdate = ItemCfgArray.Instance.GetCfg(itemId);
  288. ItemData itemDate = new ItemData() { };
  289. if (itemdate != null)
  290. {
  291. itemDate.id = itemdate.id;
  292. int scroe = 0;
  293. if (tags != null)
  294. {
  295. scroe += ItemDataManager.GetItemTagScore(itemId, tags);
  296. }
  297. scroe += itemDate.GetScore(scoreType);
  298. return scroe;
  299. }
  300. }
  301. return 0;
  302. }
  303. /// <summary>
  304. /// 获取茶话会item物品的属性
  305. /// </summary>
  306. /// <param name="itemId"></param>
  307. /// <param name="scoreType"></param>
  308. /// <returns></returns>
  309. public static int GetItemAddTeaPartyTagsScore(int itemId)//, int scoreType, string[] tags = null)
  310. {
  311. if (_dataDic.TryGetValue(itemId, out var itemData))
  312. {
  313. int scroe = 0;
  314. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  315. if (itemCfg.tagsArr != null)
  316. {
  317. foreach (var info in itemCfg.tagsArr)
  318. {
  319. scroe += Convert.ToInt32(info[1]);
  320. }
  321. }
  322. return scroe;
  323. }
  324. return 0;
  325. }
  326. /// <summary>
  327. /// 获取一个换装部件对应的标签分数
  328. /// </summary>
  329. /// <param name="itemId"></param>
  330. /// <param name="tags"></param>
  331. /// <returns></returns>
  332. public static int GetItemTagScore(int itemId, string[] tags)
  333. {
  334. int score = 0;
  335. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  336. if (itemCfg == null)
  337. {
  338. ET.Log.Error("物品:" + itemId + "不存在");
  339. return score;
  340. }
  341. for (int i = 0; i < itemCfg.tagsArr.Length; i++)
  342. {
  343. for (int j = 0; j < tags.Length; j++)
  344. {
  345. if (itemCfg.tagsArr[i][0] == tags[j])
  346. {
  347. score += int.Parse(itemCfg.tagsArr[i][1]);
  348. }
  349. }
  350. }
  351. return score;
  352. }
  353. /// <summary>
  354. /// 检测一件服装是否包含要求的标签
  355. /// </summary>
  356. /// <returns></returns>
  357. public static bool CheckItemTagsRight(int itemId, string[] tags)
  358. {
  359. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  360. for (int i = 0; i < itemCfg.tagsArr.Length; i++)
  361. {
  362. for (int j = 0; j < tags.Length; j++)
  363. {
  364. if (itemCfg.tagsArr[i][0] == tags[j]) return true;
  365. }
  366. }
  367. return false;
  368. }
  369. /// <summary>
  370. /// 检测背包中是否存在礼包
  371. /// </summary>
  372. /// <returns></returns>
  373. public static bool BagIsExistGiftBag()
  374. {
  375. var isExistGiftBag = _dataDic.Values.Any(a =>
  376. ((a.itemType == ConstItemType.USEABLE && a.subType == ConstItemSubType.USEABLE_GIFT_BAG_SELECTABLE) ||
  377. (a.itemType == ConstItemType.USEABLE && a.subType == ConstItemSubType.USEABLE_GIFT_BAG_RANDOM))
  378. && a.id != 6003001 && a.id != 6003002);
  379. return isExistGiftBag;
  380. }
  381. }
  382. }