LuckyBoxDataManager.cs 11 KB

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