LuckyBoxDataManager.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. ET.Log.Debug("打印测试-------进入游戏时候的保存---------" + count);
  68. for (var i = 0; i < count; i++)
  69. {
  70. luckyBoxFreeTimeMillDic[response.KsLuckyBoxId[i]] = response.VsFreeTime[i];
  71. }
  72. }
  73. public void UpdateFreeTime(int luckyBoxId, long freeTimeMill)
  74. {
  75. luckyBoxFreeTimeMillDic[luckyBoxId] = freeTimeMill;
  76. EventAgent.DispatchEvent(ConstMessage.LUCKY_BOX_FREE_TIME_CHANGED);
  77. }
  78. //返回奖池免费时间,如果是0,则不免费,大约0并且当前时间大于存储时间则免费
  79. public long GetFreeTime(int luckyBoxId)
  80. {
  81. luckyBoxFreeTimeMillDic.TryGetValue(luckyBoxId, out var freeTime);
  82. return freeTime;
  83. }
  84. public void InitData(int boxId)
  85. {
  86. LuckyBoxCfg luckyBoxCfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  87. if (!_dicShowList.ContainsKey(boxId))
  88. {
  89. _dicShowList[boxId] = InitBonusDataList(luckyBoxCfg.dropId);
  90. }
  91. }
  92. private List<LuckyBoxBonusData> InitBonusDataList(int dropId)
  93. {
  94. Dictionary<int, LuckyBoxBonusData> dic = new Dictionary<int, LuckyBoxBonusData>();
  95. AddToBonusDataDic(dropId, dic);
  96. List<LuckyBoxBonusData> list = new List<LuckyBoxBonusData>();
  97. foreach (var t in dic)
  98. {
  99. list.Add(t.Value);
  100. }
  101. list.Sort(CompareBonusData);
  102. return list;
  103. }
  104. private void AddToBonusDataDic(int dropId, Dictionary<int, LuckyBoxBonusData> dic)
  105. {
  106. List<DropOutCfg> cfgs = DropOutCfgArray.Instance.GetCfgsByid(dropId);
  107. if (cfgs == null)
  108. {
  109. return;
  110. }
  111. foreach (DropOutCfg cfg in cfgs)
  112. {
  113. if (cfg.item > ConstItemID.MAX_ITEM_ID)//掉落id
  114. {
  115. AddToBonusDataDic(cfg.item, dic);
  116. }
  117. else
  118. {
  119. var group = cfg.group;
  120. if (cfg.group <= 0)
  121. {
  122. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(cfg.item);
  123. if (itemCfg == null || itemCfg.suitId <= 0)
  124. {
  125. continue;
  126. }
  127. group = itemCfg.suitId;
  128. }
  129. LuckyBoxBonusData luckyBoxBonusData = GetBonusData(group, dic);
  130. if (luckyBoxBonusData == null)
  131. {
  132. continue;
  133. }
  134. luckyBoxBonusData.itemList.Add(ItemUtil.createItemData(cfg.item, 1));
  135. }
  136. }
  137. }
  138. private LuckyBoxBonusData GetBonusData(int group, Dictionary<int, LuckyBoxBonusData> dic)
  139. {
  140. dic.TryGetValue(group, out LuckyBoxBonusData luckyBoxBonusData);
  141. if (luckyBoxBonusData != null)
  142. {
  143. return luckyBoxBonusData;
  144. }
  145. luckyBoxBonusData = new LuckyBoxBonusData();
  146. luckyBoxBonusData.id = group;
  147. BonusListCfg bonusListCfg = BonusListCfgArray.Instance.GetCfg(group);
  148. if (bonusListCfg != null)
  149. {
  150. luckyBoxBonusData.name = bonusListCfg.name;
  151. luckyBoxBonusData.order = bonusListCfg.sort;
  152. }
  153. else
  154. {
  155. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(group);
  156. if (suitCfg == null)
  157. {
  158. return null;
  159. }
  160. luckyBoxBonusData.name = suitCfg.name;
  161. luckyBoxBonusData.order = 9999;
  162. }
  163. dic.Add(group, luckyBoxBonusData);
  164. return luckyBoxBonusData;
  165. }
  166. public int CompareBonusData(LuckyBoxBonusData a, LuckyBoxBonusData b)
  167. {
  168. if (b.order < a.order)
  169. {
  170. return 1;
  171. }
  172. if (b.order > a.order)
  173. {
  174. return -1;
  175. }
  176. return a.id - b.id;
  177. }
  178. public void GetOwnedCount(int boxId, out int count, out int totalCount)
  179. {
  180. count = 0;
  181. totalCount = 0;
  182. foreach (LuckyBoxBonusData luckyBoxBonusData in _dicShowList[boxId])
  183. {
  184. foreach (ItemData itemData in luckyBoxBonusData.itemList)
  185. {
  186. if (ItemDataManager.GetItemNum(itemData.id) > 0)
  187. {
  188. count++;
  189. }
  190. totalCount++;
  191. }
  192. }
  193. }
  194. public List<LuckyBoxBonusData> GetCurrentShowList(int boxId)
  195. {
  196. return _dicShowList[boxId];
  197. }
  198. // //获取首次获得的服装的列表
  199. // public List<ItemData> GetFirstClothingList()
  200. // {
  201. // ItemData[] cardArray = new ItemData[LuckyBoxDataManager.Instance.FirstRewardList.Count];
  202. // LuckyBoxDataManager.Instance.FirstRewardList.Values.CopyTo(cardArray, 0);
  203. // List<ItemData> cardList = new List<ItemData>(cardArray);
  204. // return cardList;
  205. // }
  206. public void CheckItemEnough(int boxId, int times, Action onSuccess)
  207. {
  208. int costId = LuckyBoxCfgArray.Instance.GetCfg(boxId).costID;
  209. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(costId);
  210. int costNum = GetCostNum(boxId, times);
  211. long hasNum = ItemDataManager.GetItemNum(itemCfg.id);
  212. if (hasNum >= costNum)
  213. {
  214. AlertUI.Show(string.Format("是否花费{0}个{1}摘星{2}次?", costNum, itemCfg.name, times)).SetLeftButton(true).SetRightButton(true, "确定", (object data) => { onSuccess(); });
  215. }
  216. else
  217. {
  218. long needNum = costNum - hasNum;
  219. // ItemExchangeCfgArray.Instance.GetMoneyIdAndNum(costId, ItemDataManager.GetItemExchangeTimes(costId), times, out int costItemId, out int costItemNeedNum, out int buyNum);
  220. ItemExchangeCfg itemExchangeCfg = ItemExchangeCfgArray.Instance.GetCfg(costId);
  221. int exchangedTimes = ItemDataManager.GetItemExchangeTimes(costId);
  222. int costItemNeedNum = 0;
  223. int costbuyNum = 0;
  224. int buyNum = 0;
  225. for (var i = exchangedTimes + 1; i <= exchangedTimes + times; i++)
  226. {
  227. ItemExchangeCfgArray.Instance.GetCostAndBuyNum(itemExchangeCfg, i, out int _costNum, out int _buyNum);
  228. costItemNeedNum += _costNum;
  229. costbuyNum += _buyNum;
  230. buyNum += itemExchangeCfg.num;
  231. if (buyNum >= needNum) break;
  232. }
  233. long costHasNum = ItemDataManager.GetItemNum(itemExchangeCfg.costId);
  234. if (costHasNum >= costItemNeedNum)//购买消耗品的道具足够,提示购买
  235. {
  236. if (costId == ConstItemID.GOLD)
  237. {
  238. ItemUtil.ExchangeItemById(costId, needNum, false, onSuccess);
  239. }
  240. else
  241. {
  242. BuyTipsController.Show(costId, needNum, onSuccess);
  243. }
  244. }
  245. else//购买消耗品的道具不足,提示购买 购买消耗品的道具
  246. {
  247. ItemUtil.ExchangeItemById(itemExchangeCfg.costId, costItemNeedNum - costHasNum, true, null, true, GameConst.MAX_COUNT_TO_BUY_DIAMOND_RED, true);
  248. }
  249. }
  250. }
  251. private int GetCostNum(int boxId, int times)
  252. {
  253. LuckyBoxCfg cfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  254. if (times == LuckyBoxDataManager.ONCE_TIME)
  255. {
  256. return cfg.costNum;
  257. }
  258. else if (times == LuckyBoxDataManager.TEN_TIME)
  259. {
  260. return cfg.costNumTen;
  261. }
  262. return 0;
  263. }
  264. }
  265. }