LuckyBoxDataManager.cs 13 KB

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