ItemDataManager.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. var itemID = itemInfoProto.ConfigId;
  19. ItemData itemData = null;
  20. if (_dataDic.ContainsKey(itemID))
  21. {
  22. itemData = _dataDic[itemID];
  23. }
  24. else
  25. {
  26. itemData = ItemDataPool.GetItemData(itemID);
  27. itemData.itemType = itemInfoProto.Type;
  28. itemData.subType = itemInfoProto.SubType;
  29. itemData.rarity = itemInfoProto.Rarity;
  30. _dataDic.Add(itemID, itemData);
  31. }
  32. itemData.num = itemInfoProto.Count;
  33. if (itemInfoProto.Type == ConstItemType.DRESS_UP && itemID % GameConst.MAX_COUNT_EVERY_TYPE_ITEM > 0)
  34. {
  35. DressUpMenuItemDataManager.Add(itemInfoProto);
  36. if (!DressUpMenuItemCfg1Array.Instance.CheckIsSceneType(itemInfoProto))
  37. {
  38. DecomposeDataManager.Instance.Add(itemID);
  39. }
  40. //游戏角色初始数据完成后才执行
  41. if (GameGlobal.DataInited)
  42. {
  43. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemID);
  44. FunctionOpenCfg functionOpenCfg = FunctionOpenCfgArray.Instance.GetCfg(typeof(ClothingListView).Name);
  45. if (itemCfg.rarity == ConstDressRarity.Rarity_TIANYI
  46. && StorageDataManager.Instance.GetStorageValue(ConstStorageId.FUNCTION_OPEN +
  47. functionOpenCfg.index) == 0)
  48. {
  49. FunctionOpenDataManager.Instance.CheckHasSpecialFunOpen();
  50. }
  51. }
  52. }
  53. if ((itemInfoProto.Type == ConstItemType.ITEM || itemInfoProto.Type == ConstItemType.USEABLE) &&
  54. itemData.num > 0)
  55. {
  56. BagDataManager.Instance.Add(itemData);
  57. }
  58. if (itemInfoProto.Type == ConstItemType.HEAD)
  59. {
  60. RoleInfoManager.Instance.Add(itemInfoProto);
  61. if (GameGlobal.DataInited)
  62. {
  63. RoleInfoManager.Instance.AddNew(itemID);
  64. EventAgent.DispatchEvent(ConstMessage.RED_CHANGE);
  65. }
  66. }
  67. PhotographDataManager.Instance.Add(itemInfoProto);
  68. if(GameGlobal.DataInited)
  69. {
  70. EventAgent.DispatchEvent(ConstMessage.ITEM_CHANGED, itemID);
  71. }
  72. }
  73. public static void Remove(int itemID, long itemNum)
  74. {
  75. if (_dataDic.ContainsKey(itemID))
  76. {
  77. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemID);
  78. ItemData itemData = _dataDic[itemID];
  79. itemData.num -= itemNum;
  80. if (itemData.num <= 0)
  81. {
  82. itemData.num = 0;
  83. _dataDic.Remove(itemID);
  84. if (ItemUtilCS.IsDressUpItem(itemID))
  85. {
  86. DressUpMenuItemDataManager.Remove(itemID);
  87. }
  88. if (itemCfg.itemType == ConstItemType.ITEM)
  89. {
  90. BagDataManager.Instance.Remove(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 &&
  102. !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. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  135. return itemCfg.itemType;
  136. }
  137. public static int GetItemSubType(int itemId)
  138. {
  139. _dataDic.TryGetValue(itemId, out var value);
  140. if (value != null)
  141. {
  142. return value.subType;
  143. }
  144. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  145. return itemCfg.subType;
  146. }
  147. public static int GetItemRarity(int itemId)
  148. {
  149. _dataDic.TryGetValue(itemId, out var value);
  150. if (value != null)
  151. {
  152. return value.rarity;
  153. }
  154. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  155. return itemCfg.rarity;
  156. }
  157. public static void InitServerData(List<ItemInfoProto> items)
  158. {
  159. _dataDic.Clear();
  160. foreach (ItemInfoProto roleItem in items)
  161. {
  162. Add(roleItem);
  163. }
  164. }
  165. public static void InitServerDataItemAttribute(List<ItemAttributeProto> infos)
  166. {
  167. foreach (var info in infos)
  168. {
  169. if (_dataDic.TryGetValue(info.ConfigId, out var itemData))
  170. {
  171. itemData.SetAttributes(info.Ks, info.Vs);
  172. }
  173. }
  174. }
  175. public static void InitItemExchange(int itemId, int exchangTimes)
  176. {
  177. if (_itemExchangeDic.ContainsKey(itemId))
  178. {
  179. _itemExchangeDic[itemId] = exchangTimes;
  180. }
  181. else
  182. {
  183. _itemExchangeDic.Add(itemId, exchangTimes);
  184. }
  185. }
  186. //获取物品已兑换次数
  187. public static int GetItemExchangeTimes(int itemId)
  188. {
  189. if (_itemExchangeDic.ContainsKey(itemId) == false)
  190. {
  191. InitItemExchange(itemId, 0);
  192. }
  193. return _itemExchangeDic[itemId];
  194. }
  195. public static void SetAttribute(int itemId, int key, int value)
  196. {
  197. if (_dataDic.TryGetValue(itemId, out var itemData))
  198. {
  199. itemData.SetAttribute(key, value);
  200. }
  201. }
  202. /// <summary>
  203. /// 获取表格配置的基础属性
  204. /// </summary>
  205. /// <param name="itemId"></param>
  206. /// <param name="scoreType"></param>
  207. /// <returns></returns>
  208. public static int GetItemBaseScoreValue(int itemId, int scoreType)
  209. {
  210. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  211. if (scoreType == 1)
  212. {
  213. return itemCfg.score1;
  214. }
  215. else if (scoreType == 2)
  216. {
  217. return itemCfg.score2;
  218. }
  219. else if (scoreType == 3)
  220. {
  221. return itemCfg.score3;
  222. }
  223. else if (scoreType == 4)
  224. {
  225. return itemCfg.score4;
  226. }
  227. return 0;
  228. }
  229. /// <summary>
  230. /// 获取当前(养护/升级/升星...后)的属性
  231. /// </summary>
  232. /// <param name="itemId"></param>
  233. /// <param name="scoreType"></param>
  234. /// <returns></returns>
  235. public static int GetItemAdditionScore(int itemId, int scoreType, string[] tags = null)
  236. {
  237. if (_dataDic.TryGetValue(itemId, out var itemData))
  238. {
  239. int scroe = 0;
  240. if (tags != null)
  241. {
  242. scroe += GetItemTagScore(itemId, tags);
  243. }
  244. scroe += itemData.GetScore(scoreType);
  245. return scroe;
  246. }
  247. return 0;
  248. }
  249. /// <summary>
  250. /// 获取一个换装部件对应的标签分数
  251. /// </summary>
  252. /// <param name="itemId"></param>
  253. /// <param name="tags"></param>
  254. /// <returns></returns>
  255. public static int GetItemTagScore(int itemId, string[] tags)
  256. {
  257. int score = 0;
  258. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  259. if (itemCfg == null)
  260. {
  261. ET.Log.Error("物品:" + itemId + "不存在");
  262. return score;
  263. }
  264. for (int i = 0; i < itemCfg.tagsArr.Length; i++)
  265. {
  266. for (int j = 0; j < tags.Length; j++)
  267. {
  268. if (itemCfg.tagsArr[i][0] == tags[j])
  269. {
  270. score += int.Parse(itemCfg.tagsArr[i][1]);
  271. }
  272. }
  273. }
  274. return score;
  275. }
  276. /// <summary>
  277. /// 检测一件服装是否包含要求的标签
  278. /// </summary>
  279. /// <returns></returns>
  280. public static bool CheckItemTagsRight(int itemId, string[] tags)
  281. {
  282. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  283. for (int i = 0; i < itemCfg.tagsArr.Length; i++)
  284. {
  285. for (int j = 0; j < tags.Length; j++)
  286. {
  287. if (itemCfg.tagsArr[i][0] == tags[j]) return true;
  288. }
  289. }
  290. return false;
  291. }
  292. }
  293. }