using UnityEngine; using UnityEngine.UI; using EnhancedUI.EnhancedScroller; using TableConfig; using System; namespace Snxxz.UI { public class FlashSaleItemCell : ScrollerUI { [SerializeField] CommonItemBaisc itemBaisc; [SerializeField] Text originalPrice; [SerializeField] Image originalMoneyIcon; [SerializeField] Text presentPrice; [SerializeField] Image presentMoneyIcon; [SerializeField] Image stateImg; [SerializeField] Button flashSaleBtn; [SerializeField] Text btnStateText; [SerializeField] Text fullServerRemainNum; public const int RefreshFullServerBuyType = 8; FlashRushToBuyModel rushToBuyModel { get { return ModelCenter.Instance.GetModel(); } } StoreModel storeModel { get { return ModelCenter.Instance.GetModel(); } } HeavenBattleModel battleModel { get { return ModelCenter.Instance.GetModel(); } } ItemTipsModel tipsModel { get { return ModelCenter.Instance.GetModel(); } } OperationFlashRushToBuy.FlashSaleItem saleItem; int buyState = -1; private void OnEnable() { battleModel.RefreshGameRecInfoAct += UpdateFullServerBuy; storeModel.RefreshBuyShopLimitEvent += RefreshBuyShopLimitEvent; rushToBuyModel.UpdateAllAppointmentEvent += UpdateAllAppointmentInfo; rushToBuyModel.UpdateAppointmentEvent += UpdateAppointmentInfo; } private void OnDisable() { storeModel.RefreshBuyShopLimitEvent -= RefreshBuyShopLimitEvent; battleModel.RefreshGameRecInfoAct -= UpdateFullServerBuy; rushToBuyModel.UpdateAllAppointmentEvent -= UpdateAllAppointmentInfo; rushToBuyModel.UpdateAppointmentEvent -= UpdateAppointmentInfo; } public override void Refresh(CellView cell) { if (rushToBuyModel.presentFlashShop == null) return; saleItem = rushToBuyModel.presentFlashShop.items[cell.index]; ItemCellModel cellModel = new ItemCellModel(saleItem.itemId,true,(ulong)saleItem.itemCount,saleItem.isBind); itemBaisc.Init(cellModel); itemBaisc.cellBtn.RemoveAllListeners(); itemBaisc.cellBtn.AddListener(()=> { ItemAttrData attrData = new ItemAttrData(saleItem.itemId,true, (ulong)saleItem.itemCount, -1,saleItem.isBind); tipsModel.SetItemTipsModel(attrData); }); originalMoneyIcon.SetIconWithMoneyType(saleItem.moneyType); presentMoneyIcon.SetIconWithMoneyType(saleItem.moneyType); originalPrice.text = saleItem.moneyOriginal.ToString(); presentPrice.text = saleItem.moneyNumber.ToString(); UpdateSaleItem(); UpdateSaleItemSellState(); } private void RefreshBuyShopLimitEvent() { UpdateSaleItemSellState(); } private void UpdateFullServerBuy(int type) { if (type != RefreshFullServerBuyType) return; UpdateSaleItemSellState(); } private void UpdateAllAppointmentInfo() { UpdateSaleItem(); } private void UpdateAppointmentInfo(int shopGuid) { if (saleItem == null || shopGuid != saleItem.shopGuid) return; UpdateSaleItem(); } private void UpdateSaleItem() { if (rushToBuyModel.presentFlashShop == null) return; StoreConfig storeConfig = Config.Instance.Get(saleItem.shopId); flashSaleBtn.RemoveAllListeners(); var operation = rushToBuyModel.GetOperationFlashRushToBuy(); int seconds = 0; buyState = operation.GetBuyTimeState(TimeUtility.ServerNow,rushToBuyModel.presentFlashShop.dayIndex, rushToBuyModel.presentFlashShop.timeIndex,out seconds); switch(buyState) { case -1: if(saleItem.isAppointment == 0) { btnStateText.text = "预约"; flashSaleBtn.AddListener(() => { rushToBuyModel.SendFlashSaleAppointment(saleItem.shopGuid, 1); }); } else { btnStateText.text = "取消预约"; flashSaleBtn.AddListener(() => { rushToBuyModel.SendFlashSaleAppointment(saleItem.shopGuid, 0); }); } break; case 0: btnStateText.text = "秒杀"; flashSaleBtn.AddListener(() => { storeModel.SendBuyShopItem(storeConfig,saleItem.itemCount); }); break; } } private void UpdateSaleItemSellState() { int fullSeverRemain = 0; int sellState = GetSellSate(out fullSeverRemain); fullServerRemainNum.text = fullSeverRemain.ToString(); switch (sellState) { case 0: stateImg.gameObject.SetActive(false); flashSaleBtn.gameObject.SetActive(true); break; case 1: stateImg.gameObject.SetActive(true); flashSaleBtn.gameObject.SetActive(false); break; case 2: stateImg.gameObject.SetActive(true); flashSaleBtn.gameObject.SetActive(false); break; } } /// /// 0 秒杀 1 已买到 2 已抢光 /// /// private int GetSellSate(out int fullRemainNum) { fullRemainNum = 0; var buyInfo = storeModel.GetBuyShopLimit((uint)saleItem.shopId); var buyCount = 0; var fullServerInfo = rushToBuyModel.GetFullServerInfo(); if(buyState != -1) { if (fullServerInfo != null && fullServerInfo.Value1 == saleItem.shopId) { fullRemainNum = saleItem.fullServerLimitNum - fullServerInfo.Value2; } else { fullRemainNum = saleItem.fullServerLimitNum; } if (buyInfo != null) { buyCount = buyInfo.BuyCnt; } if (buyCount >= saleItem.limitNum) { return 1; } if (fullRemainNum <= 0) { return 2; } } else { fullRemainNum = saleItem.fullServerLimitNum; } return 0; } } }