LuckyBoxDataManager.cs 11 KB

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