LuckyBoxDataManager.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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_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. public const int FIFTY_TIME = 50;
  13. public const float ANIMATION_TIME = 1.2f; //翻牌获得物品展示时间
  14. public bool CHECK_TIPS_OPEN = false; //提示弹窗是否打开
  15. public bool OPEN_LUCKY_DISCONT = false; //抽奖满20次幸运折扣弹窗是否打开
  16. private List<ItemData> _rewardsList;//当前奖励,每次抽奖后刷新
  17. private Dictionary<int, ItemData> _firstRewardsList = new Dictionary<int, ItemData>();//首次获得的奖励
  18. private Dictionary<int, List<LuckyBoxBonusData>> _dicShowList = new Dictionary<int, List<LuckyBoxBonusData>>();
  19. public List<int> luckyBoxIds = new List<int>();//奖池列表
  20. public int RotatingId = 0;//轮换活动id。0为未开启
  21. public long startTime = 1668873600000;
  22. public long endTime = 1672156799000;
  23. public List<int> KsActivityId = new List<int>(); //活动ids--只存放开启中的许愿池活动id
  24. public List<bool> VsStatus = new List<bool>(); //true已进行许愿 false未进行许愿
  25. //存储奖池免费时间,大于存储时间免费
  26. public Dictionary<int, long> luckyBoxFreeTimeMillDic = new Dictionary<int, long>();
  27. public int times = 0;
  28. public int luckyBoxIndex;
  29. public List<ItemData> RewardList
  30. {
  31. get { return _rewardsList; }
  32. set
  33. {
  34. _rewardsList = value;
  35. _firstRewardsList.Clear();
  36. int dicIndex = 0;
  37. foreach (ItemData itemData in _rewardsList)
  38. {
  39. if (ItemDataManager.GetItemNum(itemData.id) == 1)
  40. {
  41. _firstRewardsList.Add(dicIndex, itemData);
  42. }
  43. dicIndex++;
  44. }
  45. }
  46. }
  47. public Dictionary<int, ItemData> FirstRewardList
  48. {
  49. get { return _firstRewardsList; }
  50. set { _firstRewardsList = value; }
  51. }
  52. private int _currentBoxId = 2;
  53. public int currentBoxId
  54. {
  55. get
  56. {
  57. return _currentBoxId;
  58. }
  59. set
  60. {
  61. if (_currentBoxId != value)
  62. {
  63. _currentBoxId = value;
  64. // InitData(_currentBoxId);
  65. }
  66. }
  67. }
  68. public void InitServerData(S2C_GetLuckyBoxInfo response)
  69. {
  70. luckyBoxFreeTimeMillDic.Clear();
  71. int count = response.KsLuckyBoxId.Count;
  72. for (var i = 0; i < count; i++)
  73. {
  74. luckyBoxFreeTimeMillDic[response.KsLuckyBoxId[i]] = response.VsFreeTime[i];
  75. }
  76. }
  77. public void UpdateFreeTime(int luckyBoxId, long freeTimeMill)
  78. {
  79. luckyBoxFreeTimeMillDic[luckyBoxId] = freeTimeMill;
  80. EventAgent.DispatchEvent(ConstMessage.LUCKY_BOX_FREE_TIME_CHANGED);
  81. }
  82. //返回奖池免费时间,如果是0,则不免费,大约0并且当前时间大于存储时间则免费
  83. public long GetFreeTime(int luckyBoxId)
  84. {
  85. luckyBoxFreeTimeMillDic.TryGetValue(luckyBoxId, out var freeTime);
  86. return freeTime;
  87. }
  88. public void InitData(int boxId)
  89. {
  90. LuckyBoxCfg luckyBoxCfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  91. if (!_dicShowList.ContainsKey(boxId))
  92. {
  93. _dicShowList[boxId] = InitBonusDataList(luckyBoxCfg.dropId);
  94. }
  95. }
  96. private List<LuckyBoxBonusData> InitBonusDataList(int dropId)
  97. {
  98. Dictionary<int, LuckyBoxBonusData> dic = new Dictionary<int, LuckyBoxBonusData>();
  99. AddToBonusDataDic(dropId, dic);
  100. List<LuckyBoxBonusData> list = new List<LuckyBoxBonusData>();
  101. foreach (var t in dic)
  102. {
  103. list.Add(t.Value);
  104. }
  105. list.Sort(CompareBonusData);
  106. return list;
  107. }
  108. private void AddToBonusDataDic(int dropId, Dictionary<int, LuckyBoxBonusData> dic)
  109. {
  110. List<DropOutCfg> cfgs = DropOutCfgArray.Instance.GetCfgsByid(dropId);
  111. if (cfgs == null)
  112. {
  113. return;
  114. }
  115. foreach (DropOutCfg cfg in cfgs)
  116. {
  117. if (cfg.item > ConstItemID.MAX_ITEM_ID)//掉落id
  118. {
  119. AddToBonusDataDic(cfg.item, dic);
  120. }
  121. else
  122. {
  123. var group = cfg.group;
  124. if (cfg.group <= 0)
  125. {
  126. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(cfg.item);
  127. if (itemCfg == null || itemCfg.suitId <= 0)
  128. {
  129. continue;
  130. }
  131. group = itemCfg.suitId;
  132. }
  133. LuckyBoxBonusData luckyBoxBonusData = GetBonusData(group, dic);
  134. if (luckyBoxBonusData == null)
  135. {
  136. continue;
  137. }
  138. luckyBoxBonusData.itemList.Add(ItemUtil.createItemData(cfg.item, 1));
  139. }
  140. }
  141. }
  142. private LuckyBoxBonusData GetBonusData(int group, Dictionary<int, LuckyBoxBonusData> dic)
  143. {
  144. dic.TryGetValue(group, out LuckyBoxBonusData luckyBoxBonusData);
  145. if (luckyBoxBonusData != null)
  146. {
  147. return luckyBoxBonusData;
  148. }
  149. luckyBoxBonusData = new LuckyBoxBonusData();
  150. luckyBoxBonusData.id = group;
  151. BonusListCfg bonusListCfg = BonusListCfgArray.Instance.GetCfg(group);
  152. if (bonusListCfg != null)
  153. {
  154. luckyBoxBonusData.name = bonusListCfg.name;
  155. luckyBoxBonusData.order = bonusListCfg.sort;
  156. }
  157. else
  158. {
  159. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(group);
  160. if (suitCfg == null)
  161. {
  162. return null;
  163. }
  164. luckyBoxBonusData.name = suitCfg.name;
  165. luckyBoxBonusData.order = 9999;
  166. }
  167. dic.Add(group, luckyBoxBonusData);
  168. return luckyBoxBonusData;
  169. }
  170. public int CompareBonusData(LuckyBoxBonusData a, LuckyBoxBonusData b)
  171. {
  172. if (b.order < a.order)
  173. {
  174. return 1;
  175. }
  176. if (b.order > a.order)
  177. {
  178. return -1;
  179. }
  180. return a.id - b.id;
  181. }
  182. public void GetOwnedCount(int boxId, out int count, out int totalCount)
  183. {
  184. count = 0;
  185. totalCount = 0;
  186. foreach (LuckyBoxBonusData luckyBoxBonusData in _dicShowList[boxId])
  187. {
  188. foreach (ItemData itemData in luckyBoxBonusData.itemList)
  189. {
  190. if (ItemDataManager.GetItemNum(itemData.id) > 0)
  191. {
  192. count++;
  193. }
  194. totalCount++;
  195. }
  196. }
  197. }
  198. public List<LuckyBoxBonusData> GetCurrentShowList(int boxId)
  199. {
  200. return _dicShowList[boxId];
  201. }
  202. // //获取首次获得的服装的列表
  203. // public List<ItemData> GetFirstClothingList()
  204. // {
  205. // ItemData[] cardArray = new ItemData[LuckyBoxDataManager.Instance.FirstRewardList.Count];
  206. // LuckyBoxDataManager.Instance.FirstRewardList.Values.CopyTo(cardArray, 0);
  207. // List<ItemData> cardList = new List<ItemData>(cardArray);
  208. // return cardList;
  209. // }
  210. public void CheckItemEnough(int boxId, int times, Action onSuccess)
  211. {
  212. int costId = LuckyBoxCfgArray.Instance.GetCfg(boxId).costID;
  213. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(costId);
  214. int costNum = GetCostNum(boxId, times);
  215. long hasNum = ItemDataManager.GetItemNum(itemCfg.id);
  216. long freeTime = LuckyBoxDataManager.Instance.GetFreeTime(boxId);
  217. if (freeTime > 0 && freeTime < TimeHelper.ServerNow() && times == LuckyBoxDataManager.ONCE_TIME) {
  218. onSuccess();
  219. }
  220. else if (hasNum >= costNum)
  221. {
  222. if(LuckyBoxDataManager.Instance.CHECK_TIPS_OPEN == false)
  223. AlertUI.Show(string.Format("是否花费{0}个{1}摘星{2}次?", costNum, itemCfg.name, times)).SetLeftButton(true).SetRightButton(true, "确定", (object data) => { onSuccess(); }).SetShowCheck(true);
  224. else
  225. onSuccess();
  226. }
  227. else
  228. {
  229. long needNum = costNum - hasNum;
  230. // ItemExchangeCfgArray.Instance.GetMoneyIdAndNum(costId, ItemDataManager.GetItemExchangeTimes(costId), times, out int costItemId, out int costItemNeedNum, out int buyNum);
  231. ItemExchangeCfg itemExchangeCfg = ItemExchangeCfgArray.Instance.GetCfg(costId);
  232. int exchangedTimes = ItemDataManager.GetItemExchangeTimes(costId);
  233. int costItemNeedNum = 0;
  234. int costbuyNum = 0;
  235. int buyNum = 0;
  236. for (var i = exchangedTimes + 1; i <= exchangedTimes + times; i++)
  237. {
  238. ItemExchangeCfgArray.Instance.GetCostAndBuyNum(itemExchangeCfg, i, out int _costNum, out int _buyNum);
  239. costItemNeedNum += _costNum;
  240. costbuyNum += _buyNum;
  241. buyNum += itemExchangeCfg.num;
  242. if (buyNum >= needNum) break;
  243. }
  244. long costHasNum = ItemDataManager.GetItemNum(itemExchangeCfg.costId);
  245. if (costHasNum >= costItemNeedNum)//购买消耗品的道具足够,提示购买
  246. {
  247. if (costId == ConstItemID.GOLD)
  248. {
  249. ItemUtil.ExchangeItemById(costId, needNum, false, onSuccess);
  250. }
  251. else {
  252. if (LuckyBoxDataManager.Instance.CHECK_TIPS_OPEN == false)
  253. BuyTipsController.Show(costId, needNum, onSuccess, true);
  254. else
  255. {
  256. OnBuyItem(costId, needNum, onSuccess);
  257. }
  258. }
  259. }
  260. else//购买消耗品的道具不足,提示购买 购买消耗品的道具
  261. {
  262. ItemUtil.ExchangeItemById(itemExchangeCfg.costId, costItemNeedNum - costHasNum, true, null, true, GameConst.MAX_COUNT_TO_BUY_DIAMOND_RED, true);
  263. }
  264. }
  265. }
  266. private async void OnBuyItem(int itemId, long count, Action onSuccess = null)
  267. {
  268. var _result = await ItemExchangeSProxy.ItemExchange(itemId, count);
  269. if (_result)
  270. BonusController.TryShowBonusList(ItemUtil.CreateItemDataList(itemId, count), onSuccess);
  271. }
  272. private int GetCostNum(int boxId, int times)
  273. {
  274. LuckyBoxCfg cfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  275. if (times == LuckyBoxDataManager.ONCE_TIME)
  276. {
  277. return cfg.costNum;
  278. }
  279. else if (times == LuckyBoxDataManager.TEN_TIME)
  280. {
  281. return cfg.costNumTen;
  282. }
  283. else if (times == LuckyBoxDataManager.FIFTY_TIME)
  284. {
  285. return cfg.costNumfifty;
  286. }
  287. return 0;
  288. }
  289. public bool RedBtnLeft(int curIndex)
  290. {
  291. int index = curIndex - 1;
  292. for (int i = index; i >= BOX_ID_2 - 1; i--)
  293. {
  294. int boxId = LuckyBoxDataManager.Instance.luckyBoxIds[i];
  295. long freeTime = LuckyBoxDataManager.Instance.GetFreeTime(boxId);
  296. if (freeTime > 0 && freeTime - TimeHelper.ServerNow() < 0)
  297. return true;
  298. if (index == 1 && RedDotDataManager.Instance.GetActLuckyBoxRewardRed(ConstLimitTimeActivityType.ActLimitTsy))
  299. return true;
  300. }
  301. return false;
  302. }
  303. public bool RedBtnRight(int curIndex)
  304. {
  305. var luckyBoxIds = LuckyBoxDataManager.Instance.luckyBoxIds;
  306. for (int i = curIndex + 1; i < luckyBoxIds.Count; i++) {
  307. int boxId = luckyBoxIds[i];
  308. long freeTime = LuckyBoxDataManager.Instance.GetFreeTime(boxId);
  309. if (freeTime > 0 && freeTime - TimeHelper.ServerNow() < 0)
  310. return true;
  311. if (curIndex == 1 && RedDotDataManager.Instance.GetActLuckyBoxRewardRed(ConstLimitTimeActivityType.ActLimitTsy))
  312. return true;
  313. }
  314. return false;
  315. }
  316. //天市垣开服活动是否开启
  317. public bool GetActLimitTsyOpen()
  318. {
  319. int activityId = ActivityDataManager.Instance.GetCurOpenActiveByType(ConstLimitTimeActivityType.ActLimitTsy);
  320. var activityCfg = ActivityOpenCfgArray.Instance.GetCfg(activityId);
  321. long endTime = 0;
  322. if (activityCfg != null)
  323. endTime = TimeUtil.DateTimeToTimestamp(activityCfg.endTime);
  324. long curTime = TimeHelper.ServerNow();
  325. if (endTime < curTime)
  326. return false;
  327. return true;
  328. }
  329. }
  330. }