LuckyBoxDataManager.cs 15 KB

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