LuckyBoxDataManager.cs 14 KB

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