ItemDataManager.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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.PreDataInited)
  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.PreDataInited)
  60. {
  61. RoleInfoManager.Instance.AddNew(itemID);
  62. EventAgent.DispatchEvent(ConstMessage.RED_CHANGE);
  63. }
  64. }
  65. PhotographDataManager.Instance.Add(itemInfoProto);
  66. if(GameGlobal.PreDataInited)
  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. //Debug.Log($"ItemDataManager GetItemType itemId {itemId}");
  134. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  135. return itemCfg.itemType;
  136. }
  137. public static int[][] GetItemSyntheticSuitArr(int itemId)
  138. {
  139. _dataDic.TryGetValue(itemId, out var value);
  140. if (value != null && value.syntheticMateriarsArr != null && value.syntheticMateriarsArr.Length > 0)
  141. {
  142. return value.syntheticMateriarsArr;
  143. }
  144. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  145. return itemCfg.syntheticMateriarsArr;
  146. }
  147. public static int GetItemSubType(int itemId)
  148. {
  149. _dataDic.TryGetValue(itemId, out var value);
  150. if (value != null)
  151. {
  152. return value.subType;
  153. }
  154. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  155. return itemCfg.subType;
  156. }
  157. public static int GetItemRarity(int itemId)
  158. {
  159. _dataDic.TryGetValue(itemId, out var value);
  160. if (value != null)
  161. {
  162. return value.rarity;
  163. }
  164. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  165. return itemCfg.rarity;
  166. }
  167. public static int GetItemSkillId(int itemId)
  168. {
  169. _dataDic.TryGetValue(itemId, out var value);
  170. if (value != null && value.param2Arr != null)
  171. {
  172. return value.param2Arr[0];
  173. }
  174. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  175. return itemCfg.param2Arr[0];
  176. }
  177. public static void InitServerData(List<ItemInfoProto> items)
  178. {
  179. _dataDic.Clear();
  180. foreach (ItemInfoProto roleItem in items)
  181. {
  182. //LogUtil.LogEditor($"ItemDataManager InitServerData {roleItem.ConfigId}");
  183. Add(roleItem);
  184. }
  185. DressUpMenuItemDataManager.StartPreLoadItemCfg();
  186. }
  187. public static void InitServerDataItemAttribute(List<ItemAttributeProto> infos)
  188. {
  189. foreach (var info in infos)
  190. {
  191. if (_dataDic.TryGetValue(info.ConfigId, out var itemData))
  192. {
  193. itemData.SetAttributes(info.Ks, info.Vs);
  194. }
  195. }
  196. }
  197. public static void InitItemExchange(int itemId, int exchangTimes)
  198. {
  199. if (_itemExchangeDic.ContainsKey(itemId))
  200. {
  201. _itemExchangeDic[itemId] = exchangTimes;
  202. }
  203. else
  204. {
  205. _itemExchangeDic.Add(itemId, exchangTimes);
  206. }
  207. }
  208. //获取物品已兑换次数
  209. public static int GetItemExchangeTimes(int itemId)
  210. {
  211. if (_itemExchangeDic.ContainsKey(itemId) == false)
  212. {
  213. InitItemExchange(itemId, 0);
  214. }
  215. return _itemExchangeDic[itemId];
  216. }
  217. public static void SetAttribute(int itemId, int key, int value)
  218. {
  219. if (_dataDic.TryGetValue(itemId, out var itemData))
  220. {
  221. itemData.SetAttribute(key, value);
  222. }
  223. }
  224. /// <summary>
  225. /// 获取表格配置的基础属性
  226. /// </summary>
  227. /// <param name="itemId"></param>
  228. /// <param name="scoreType"></param>
  229. /// <returns></returns>
  230. public static int GetItemBaseScoreValue(int itemId, int scoreType)
  231. {
  232. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  233. if (scoreType == 1)
  234. {
  235. return itemCfg.score1;
  236. }
  237. else if (scoreType == 2)
  238. {
  239. return itemCfg.score2;
  240. }
  241. else if (scoreType == 3)
  242. {
  243. return itemCfg.score3;
  244. }
  245. else if (scoreType == 4)
  246. {
  247. return itemCfg.score4;
  248. }
  249. return 0;
  250. }
  251. /// <summary>
  252. /// 获取当前(养护/升级/升星...后)的属性
  253. /// </summary>
  254. /// <param name="itemId"></param>
  255. /// <param name="scoreType"></param>
  256. /// <returns></returns>
  257. public static int GetItemAdditionScore(int itemId, int scoreType, string[] tags = null)
  258. {
  259. if (_dataDic.TryGetValue(itemId, out var itemData))
  260. {
  261. int scroe = 0;
  262. if (tags != null)
  263. {
  264. scroe += GetItemTagScore(itemId, tags);
  265. }
  266. scroe += itemData.GetScore(scoreType);
  267. return scroe;
  268. }
  269. return 0;
  270. }
  271. /// <summary>
  272. /// 获取茶话会item物品的属性
  273. /// </summary>
  274. /// <param name="itemId"></param>
  275. /// <param name="scoreType"></param>
  276. /// <returns></returns>
  277. public static int GetItemAddTeaPartyTagsScore(int itemId)//, int scoreType, string[] tags = null)
  278. {
  279. if (_dataDic.TryGetValue(itemId, out var itemData))
  280. {
  281. int scroe = 0;
  282. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  283. if (itemCfg.tagsArr != null)
  284. {
  285. foreach (var info in itemCfg.tagsArr)
  286. {
  287. scroe += Convert.ToInt32(info[1]);
  288. }
  289. }
  290. return scroe;
  291. }
  292. return 0;
  293. }
  294. /// <summary>
  295. /// 获取一个换装部件对应的标签分数
  296. /// </summary>
  297. /// <param name="itemId"></param>
  298. /// <param name="tags"></param>
  299. /// <returns></returns>
  300. public static int GetItemTagScore(int itemId, string[] tags)
  301. {
  302. int score = 0;
  303. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  304. if (itemCfg == null)
  305. {
  306. ET.Log.Error("物品:" + itemId + "不存在");
  307. return score;
  308. }
  309. for (int i = 0; i < itemCfg.tagsArr.Length; i++)
  310. {
  311. for (int j = 0; j < tags.Length; j++)
  312. {
  313. if (itemCfg.tagsArr[i][0] == tags[j])
  314. {
  315. score += int.Parse(itemCfg.tagsArr[i][1]);
  316. }
  317. }
  318. }
  319. return score;
  320. }
  321. /// <summary>
  322. /// 检测一件服装是否包含要求的标签
  323. /// </summary>
  324. /// <returns></returns>
  325. public static bool CheckItemTagsRight(int itemId, string[] tags)
  326. {
  327. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  328. for (int i = 0; i < itemCfg.tagsArr.Length; i++)
  329. {
  330. for (int j = 0; j < tags.Length; j++)
  331. {
  332. if (itemCfg.tagsArr[i][0] == tags[j]) return true;
  333. }
  334. }
  335. return false;
  336. }
  337. /// <summary>
  338. /// 检测背包中是否存在礼包
  339. /// </summary>
  340. /// <returns></returns>
  341. public static bool BagIsExistGiftBag()
  342. {
  343. var isExistGiftBag = _dataDic.Values.Any(a =>
  344. ((a.itemType == ConstItemType.USEABLE && a.subType == ConstItemSubType.USEABLE_GIFT_BAG_SELECTABLE) ||
  345. (a.itemType == ConstItemType.USEABLE && a.subType == ConstItemSubType.USEABLE_GIFT_BAG_RANDOM))
  346. && a.id != 6003001 && a.id != 6003002);
  347. return isExistGiftBag;
  348. }
  349. }
  350. }