LuckyBoxDataManager.cs 15 KB

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