LuckyBoxDataManager.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. using System;
  2. using System.Collections.Generic;
  3. namespace GFGGame
  4. {
  5. public class LuckyBoxDataManager : SingletonBase<LuckyBoxDataManager>
  6. {
  7. // public const int BOX_ID_1 = 1;
  8. public const int BOX_ID_2 = 2;//常驻奖池2
  9. public const int BOX_ID_3 = 3;//常驻奖池2
  10. public const int ONCE_TIME = 1;
  11. public const int TEN_TIME = 10;
  12. private List<ItemData> _rewardsList;//当前奖励,每次抽奖后刷新
  13. private Dictionary<int, ItemData> _firstRewardsList = new Dictionary<int, ItemData>();//首次获得的奖励
  14. private Dictionary<int, List<LuckyBoxBonusData>> _dicShowList = new Dictionary<int, List<LuckyBoxBonusData>>();
  15. public List<int> luckyBoxIds = new List<int>();//奖池列表
  16. public long startTime = 1668873600000;
  17. public long endTime = 1672156799000;
  18. public int times = 0;
  19. public int luckyBoxId;
  20. public List<ItemData> RewardList
  21. {
  22. get { return _rewardsList; }
  23. set
  24. {
  25. _rewardsList = value;
  26. _firstRewardsList.Clear();
  27. int dicIndex = 0;
  28. foreach (ItemData itemData in _rewardsList)
  29. {
  30. if (ItemDataManager.GetItemNum(itemData.id) == 1)
  31. {
  32. _firstRewardsList.Add(dicIndex, itemData);
  33. }
  34. dicIndex++;
  35. }
  36. }
  37. }
  38. public Dictionary<int, ItemData> FirstRewardList
  39. {
  40. get { return _firstRewardsList; }
  41. set { _firstRewardsList = value; }
  42. }
  43. private int _currentBoxId = 2;
  44. public int currentBoxId
  45. {
  46. get
  47. {
  48. return _currentBoxId;
  49. }
  50. set
  51. {
  52. if (_currentBoxId != value)
  53. {
  54. _currentBoxId = value;
  55. // InitData(_currentBoxId);
  56. }
  57. }
  58. }
  59. public void InitData(int boxId)
  60. {
  61. LuckyBoxCfg luckyBoxCfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  62. if (!_dicShowList.ContainsKey(boxId))
  63. {
  64. _dicShowList[boxId] = InitBonusDataList(luckyBoxCfg.dropId);
  65. }
  66. }
  67. private List<LuckyBoxBonusData> InitBonusDataList(int dropId)
  68. {
  69. Dictionary<int, LuckyBoxBonusData> dic = new Dictionary<int, LuckyBoxBonusData>();
  70. AddToBonusDataDic(dropId, dic);
  71. List<LuckyBoxBonusData> list = new List<LuckyBoxBonusData>();
  72. foreach(var t in dic)
  73. {
  74. list.Add(t.Value);
  75. }
  76. list.Sort(CompareBonusData);
  77. return list;
  78. }
  79. private void AddToBonusDataDic(int dropId, Dictionary<int, LuckyBoxBonusData> dic)
  80. {
  81. List<DropOutCfg> cfgs = DropOutCfgArray.Instance.GetCfgsByid(dropId);
  82. if (cfgs == null)
  83. {
  84. return;
  85. }
  86. foreach (DropOutCfg cfg in cfgs)
  87. {
  88. if (cfg.item > ConstItemID.MAX_ITEM_ID)//掉落id
  89. {
  90. AddToBonusDataDic(cfg.item, dic);
  91. }
  92. else
  93. {
  94. var group = cfg.group;
  95. if (cfg.group <= 0)
  96. {
  97. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(cfg.item);
  98. if (itemCfg == null || itemCfg.suitId <= 0)
  99. {
  100. continue;
  101. }
  102. group = itemCfg.suitId;
  103. }
  104. LuckyBoxBonusData luckyBoxBonusData = GetBonusData(group, dic);
  105. if (luckyBoxBonusData == null)
  106. {
  107. continue;
  108. }
  109. luckyBoxBonusData.itemList.Add(ItemUtil.createItemData(cfg.item, 1));
  110. }
  111. }
  112. }
  113. private LuckyBoxBonusData GetBonusData(int group, Dictionary<int, LuckyBoxBonusData> dic)
  114. {
  115. dic.TryGetValue(group, out LuckyBoxBonusData luckyBoxBonusData);
  116. if(luckyBoxBonusData != null)
  117. {
  118. return luckyBoxBonusData;
  119. }
  120. luckyBoxBonusData = new LuckyBoxBonusData();
  121. luckyBoxBonusData.id = group;
  122. BonusListCfg bonusListCfg = BonusListCfgArray.Instance.GetCfg(group);
  123. if (bonusListCfg != null)
  124. {
  125. luckyBoxBonusData.name = bonusListCfg.name;
  126. luckyBoxBonusData.order = bonusListCfg.sort;
  127. }
  128. else
  129. {
  130. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(group);
  131. if (suitCfg == null)
  132. {
  133. return null;
  134. }
  135. luckyBoxBonusData.name = suitCfg.name;
  136. luckyBoxBonusData.order = 9999;
  137. }
  138. dic.Add(group, luckyBoxBonusData);
  139. return luckyBoxBonusData;
  140. }
  141. public int CompareBonusData(LuckyBoxBonusData a, LuckyBoxBonusData b)
  142. {
  143. if(b.order < a.order)
  144. {
  145. return 1;
  146. }
  147. if(b.order > a.order)
  148. {
  149. return -1;
  150. }
  151. return a.id - b.id;
  152. }
  153. public void GetOwnedCount(int boxId, out int count, out int totalCount)
  154. {
  155. count = 0;
  156. totalCount = 0;
  157. foreach (LuckyBoxBonusData luckyBoxBonusData in _dicShowList[boxId])
  158. {
  159. foreach (ItemData itemData in luckyBoxBonusData.itemList)
  160. {
  161. if (ItemDataManager.GetItemNum(itemData.id) > 0)
  162. {
  163. count++;
  164. }
  165. totalCount++;
  166. }
  167. }
  168. }
  169. public List<LuckyBoxBonusData> GetCurrentShowList(int boxId)
  170. {
  171. return _dicShowList[boxId];
  172. }
  173. // //获取首次获得的服装的列表
  174. // public List<ItemData> GetFirstClothingList()
  175. // {
  176. // ItemData[] cardArray = new ItemData[LuckyBoxDataManager.Instance.FirstRewardList.Count];
  177. // LuckyBoxDataManager.Instance.FirstRewardList.Values.CopyTo(cardArray, 0);
  178. // List<ItemData> cardList = new List<ItemData>(cardArray);
  179. // return cardList;
  180. // }
  181. public void CheckItemEnough(int boxId, int times, Action onSuccess)
  182. {
  183. int costId = LuckyBoxCfgArray.Instance.GetCfg(boxId).costID;
  184. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(costId);
  185. int costNum = GetCostNum(boxId, times);
  186. long hasNum = ItemDataManager.GetItemNum(itemCfg.id);
  187. if (hasNum >= costNum)
  188. {
  189. AlertUI.Show(string.Format("是否花费{0}个{1}摘星{2}次?", costNum, itemCfg.name, times)).SetLeftButton(true).SetRightButton(true, "确定", (object data) => { onSuccess(); });
  190. }
  191. else
  192. {
  193. long needNum = costNum - hasNum;
  194. // ItemExchangeCfgArray.Instance.GetMoneyIdAndNum(costId, ItemDataManager.GetItemExchangeTimes(costId), times, out int costItemId, out int costItemNeedNum, out int buyNum);
  195. ItemExchangeCfg itemExchangeCfg = ItemExchangeCfgArray.Instance.GetCfg(costId);
  196. int exchangedTimes = ItemDataManager.GetItemExchangeTimes(costId);
  197. int costItemNeedNum = 0;
  198. int costbuyNum = 0;
  199. int buyNum = 0;
  200. for (var i = exchangedTimes + 1; i <= exchangedTimes + times; i++)
  201. {
  202. ItemExchangeCfgArray.Instance.GetCostAndBuyNum(itemExchangeCfg, i, out int _costNum, out int _buyNum);
  203. costItemNeedNum += _costNum;
  204. costbuyNum += _buyNum;
  205. buyNum += itemExchangeCfg.num;
  206. if (buyNum >= needNum) break;
  207. }
  208. long costHasNum = ItemDataManager.GetItemNum(itemExchangeCfg.costId);
  209. if (costHasNum >= costItemNeedNum)//购买消耗品的道具足够,提示购买
  210. {
  211. if (costId == ConstItemID.GOLD)
  212. {
  213. ItemUtil.ExchangeItemById(costId, needNum, false, onSuccess);
  214. }
  215. else
  216. {
  217. BuyTipsController.Show(costId, needNum, onSuccess);
  218. }
  219. }
  220. else//购买消耗品的道具不足,提示购买 购买消耗品的道具
  221. {
  222. ItemUtil.ExchangeItemById(itemExchangeCfg.costId, costItemNeedNum - costHasNum, true, null, true, GameConst.MAX_COUNT_TO_BUY_DIAMOND_RED, true);
  223. }
  224. }
  225. }
  226. private int GetCostNum(int boxId, int times)
  227. {
  228. LuckyBoxCfg cfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  229. if (times == LuckyBoxDataManager.ONCE_TIME)
  230. {
  231. return cfg.costNum;
  232. }
  233. else if (times == LuckyBoxDataManager.TEN_TIME)
  234. {
  235. return cfg.costNumTen;
  236. }
  237. return 0;
  238. }
  239. }
  240. }