ItemDataManager.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using ET;
  4. using UnityEngine;
  5. namespace GFGGame
  6. {
  7. public class ItemDataManager
  8. {
  9. private static Dictionary<int, ItemData> _dataDic = new Dictionary<int, ItemData>();
  10. private static Dictionary<int, int> _itemExchangeDic = new Dictionary<int, int>();
  11. delegate object MemberGetDelegate(ItemCfg p);
  12. public static void Clear()
  13. {
  14. _itemExchangeDic.Clear();
  15. }
  16. public static void Add(ItemInfoProto itemInfoProto)
  17. {
  18. //初始化时禁止使用物品配置,会造成卡顿!!!
  19. var itemID = itemInfoProto.ConfigId;
  20. ItemData itemData = null;
  21. if (_dataDic.ContainsKey(itemID))
  22. {
  23. itemData = _dataDic[itemID];
  24. }
  25. else
  26. {
  27. itemData = ItemDataPool.GetItemData(itemID);
  28. itemData.itemType = itemInfoProto.Type;
  29. itemData.subType = itemInfoProto.SubType;
  30. itemData.rarity = itemInfoProto.Rarity;
  31. _dataDic.Add(itemID, itemData);
  32. }
  33. itemData.num = itemInfoProto.Count;
  34. if (itemInfoProto.Type == ConstItemType.DRESS_UP && itemID % GameConst.MAX_COUNT_EVERY_TYPE_ITEM > 0)
  35. {
  36. DressUpMenuItemDataManager.Add(itemInfoProto);
  37. if (!DressUpMenuItemCfg1Array.Instance.CheckIsSceneType(itemInfoProto))
  38. {
  39. DecomposeDataManager.Instance.Add(itemID);
  40. }
  41. //游戏角色初始数据完成后才执行
  42. if (GameGlobal.DataInited)
  43. {
  44. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemID);
  45. FunctionOpenCfg functionOpenCfg = FunctionOpenCfgArray.Instance.GetCfg(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.DataInited)
  63. {
  64. RoleInfoManager.Instance.AddNew(itemID);
  65. EventAgent.DispatchEvent(ConstMessage.RED_CHANGE);
  66. }
  67. }
  68. PhotographDataManager.Instance.Add(itemInfoProto);
  69. if(GameGlobal.DataInited)
  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 = ItemCfgArray.Instance.GetCfg(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. }
  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 &&
  103. !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. 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 void InitServerData(List<ItemInfoProto> items)
  169. {
  170. _dataDic.Clear();
  171. foreach (ItemInfoProto roleItem in items)
  172. {
  173. Add(roleItem);
  174. }
  175. DressUpMenuItemDataManager.StartPreLoadItemCfg();
  176. }
  177. public static void InitServerDataItemAttribute(List<ItemAttributeProto> infos)
  178. {
  179. foreach (var info in infos)
  180. {
  181. if (_dataDic.TryGetValue(info.ConfigId, out var itemData))
  182. {
  183. itemData.SetAttributes(info.Ks, info.Vs);
  184. }
  185. }
  186. }
  187. public static void InitItemExchange(int itemId, int exchangTimes)
  188. {
  189. if (_itemExchangeDic.ContainsKey(itemId))
  190. {
  191. _itemExchangeDic[itemId] = exchangTimes;
  192. }
  193. else
  194. {
  195. _itemExchangeDic.Add(itemId, exchangTimes);
  196. }
  197. }
  198. //获取物品已兑换次数
  199. public static int GetItemExchangeTimes(int itemId)
  200. {
  201. if (_itemExchangeDic.ContainsKey(itemId) == false)
  202. {
  203. InitItemExchange(itemId, 0);
  204. }
  205. return _itemExchangeDic[itemId];
  206. }
  207. public static void SetAttribute(int itemId, int key, int value)
  208. {
  209. if (_dataDic.TryGetValue(itemId, out var itemData))
  210. {
  211. itemData.SetAttribute(key, value);
  212. }
  213. }
  214. /// <summary>
  215. /// 获取表格配置的基础属性
  216. /// </summary>
  217. /// <param name="itemId"></param>
  218. /// <param name="scoreType"></param>
  219. /// <returns></returns>
  220. public static int GetItemBaseScoreValue(int itemId, int scoreType)
  221. {
  222. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  223. if (scoreType == 1)
  224. {
  225. return itemCfg.score1;
  226. }
  227. else if (scoreType == 2)
  228. {
  229. return itemCfg.score2;
  230. }
  231. else if (scoreType == 3)
  232. {
  233. return itemCfg.score3;
  234. }
  235. else if (scoreType == 4)
  236. {
  237. return itemCfg.score4;
  238. }
  239. return 0;
  240. }
  241. /// <summary>
  242. /// 获取当前(养护/升级/升星...后)的属性
  243. /// </summary>
  244. /// <param name="itemId"></param>
  245. /// <param name="scoreType"></param>
  246. /// <returns></returns>
  247. public static int GetItemAdditionScore(int itemId, int scoreType, string[] tags = null)
  248. {
  249. if (_dataDic.TryGetValue(itemId, out var itemData))
  250. {
  251. int scroe = 0;
  252. if (tags != null)
  253. {
  254. scroe += GetItemTagScore(itemId, tags);
  255. }
  256. scroe += itemData.GetScore(scoreType);
  257. return scroe;
  258. }
  259. return 0;
  260. }
  261. /// <summary>
  262. /// 获取一个换装部件对应的标签分数
  263. /// </summary>
  264. /// <param name="itemId"></param>
  265. /// <param name="tags"></param>
  266. /// <returns></returns>
  267. public static int GetItemTagScore(int itemId, string[] tags)
  268. {
  269. int score = 0;
  270. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  271. if (itemCfg == null)
  272. {
  273. ET.Log.Error("物品:" + itemId + "不存在");
  274. return score;
  275. }
  276. for (int i = 0; i < itemCfg.tagsArr.Length; i++)
  277. {
  278. for (int j = 0; j < tags.Length; j++)
  279. {
  280. if (itemCfg.tagsArr[i][0] == tags[j])
  281. {
  282. score += int.Parse(itemCfg.tagsArr[i][1]);
  283. }
  284. }
  285. }
  286. return score;
  287. }
  288. /// <summary>
  289. /// 检测一件服装是否包含要求的标签
  290. /// </summary>
  291. /// <returns></returns>
  292. public static bool CheckItemTagsRight(int itemId, string[] tags)
  293. {
  294. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  295. for (int i = 0; i < itemCfg.tagsArr.Length; i++)
  296. {
  297. for (int j = 0; j < tags.Length; j++)
  298. {
  299. if (itemCfg.tagsArr[i][0] == tags[j]) return true;
  300. }
  301. }
  302. return false;
  303. }
  304. /// <summary>
  305. /// 检测背包中是否存在礼包
  306. /// </summary>
  307. /// <returns></returns>
  308. public static bool BagIsExistGiftBag()
  309. {
  310. var isExistGiftBag = _dataDic.Values.Any(a =>
  311. (a.itemType == ConstItemType.USEABLE && a.subType == ConstItemSubType.USEABLE_GIFT_BAG_SELECTABLE) ||
  312. (a.itemType == ConstItemType.USEABLE && a.subType == ConstItemSubType.USEABLE_GIFT_BAG_RANDOM));
  313. return isExistGiftBag;
  314. }
  315. }
  316. }