LuckyBoxDataManager.cs 14 KB

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