LuckyBoxDataManager.cs 15 KB

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