ItemDataManager.cs 13 KB

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