using UnityEngine; using UnityEngine.UI; using System; namespace vnxbqy.UI { public class HolidayFlashRushSaleItemCell : MonoBehaviour { [SerializeField] CommonItemBaisc itemBaisc; [SerializeField] Text originalPrice; [SerializeField] Image originalMoneyIcon; [SerializeField] Text presentPrice; [SerializeField] Image presentMoneyIcon; [SerializeField] Image stateImg; [SerializeField] Button flashSaleBtn; [SerializeField] Image btnBgImg; [SerializeField] Text btnStateText; [SerializeField] Text cdText; [SerializeField] Text fullServerRemainNum; [SerializeField] Text personalRemainNum; public const int RefreshFullServerBuyType = 8; HolidayFlashRushToBuyModel rushToBuyModel { get { return ModelCenter.Instance.GetModel(); } } StoreModel storeModel { get { return ModelCenter.Instance.GetModel(); } } OperationFlashRushToBuy.FlashSaleItem saleItem; int buyState = -1; int cdTime = 0; int code = 0; private void OnEnable() { rushToBuyModel.UpdateFullSeverBuyEvent += UpdateFullServerBuy; storeModel.RefreshBuyShopLimitEvent += RefreshBuyShopLimitEvent; rushToBuyModel.UpdateAllAppointmentEvent += UpdateAllAppointmentInfo; rushToBuyModel.UpdateAppointmentEvent += UpdateAppointmentInfo; KnapsackTimeCDMgr.Instance.RefreshNormalCDAct += RefreshCD; } private void OnDisable() { storeModel.RefreshBuyShopLimitEvent -= RefreshBuyShopLimitEvent; rushToBuyModel.UpdateFullSeverBuyEvent -= UpdateFullServerBuy; rushToBuyModel.UpdateAllAppointmentEvent -= UpdateAllAppointmentInfo; rushToBuyModel.UpdateAppointmentEvent -= UpdateAppointmentInfo; KnapsackTimeCDMgr.Instance.RefreshNormalCDAct -= RefreshCD; } public void SetDisplayModel(int index) { if (rushToBuyModel.presentFlashShop == null) return; saleItem = rushToBuyModel.presentFlashShop.items[index]; code = rushToBuyModel.presentFlashShop.dayIndex * 1000 + rushToBuyModel.presentFlashShop.timeIndex * 100 + index; ItemCellModel cellModel = new ItemCellModel(saleItem.itemId,true,(ulong)saleItem.itemCount); itemBaisc.Init(cellModel); itemBaisc.button.RemoveAllListeners(); itemBaisc.button.AddListener(()=> { ItemTipUtility.Show(saleItem.itemId); }); 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 shopId) { if (saleItem == null || saleItem.shopId != shopId) return; UpdateSaleItemSellState(); } private void UpdateAllAppointmentInfo() { UpdateSaleItem(); } private void UpdateAppointmentInfo(int shopGuid) { if (saleItem == null || shopGuid != saleItem.shopGuid) return; if(saleItem.isAppointment == 1) { cdTime = 10; KnapsackTimeCDMgr.Instance.Register(this.code,cdTime); } UpdateSaleItem(); } private void UpdateSaleItem() { if (rushToBuyModel.presentFlashShop == null) return; flashSaleBtn.RemoveAllListeners(); var operation = rushToBuyModel.GetOperationFlashRushToBuy(); int seconds = 0; buyState = operation.GetBuyTimeState(TimeUtility.ServerNow,rushToBuyModel.presentFlashShop.dayIndex, rushToBuyModel.presentFlashShop.timeIndex,out seconds); btnBgImg.SetSprite("SecondBtn1"); btnStateText.SetActive(true); cdText.SetActive(false); btnBgImg.material = MaterialUtility.GetUIDefaultGraphicMaterial(); switch (buyState) { case -1: if(saleItem.isAppointment == 0) { btnStateText.text = Language.Get("FlashRushToBuy101"); flashSaleBtn.AddListener(() => { rushToBuyModel.SendFlashSaleAppointment(saleItem.shopGuid, 1); }); } else { btnBgImg.SetSprite("BlackBtn"); RefreshCD(code,KnapsackTimeCDMgr.Instance.GetNormalCDTime(code)); btnStateText.SetActive(false); cdText.SetActive(true); flashSaleBtn.AddListener(CancelAppointment); } break; case 0: btnStateText.text = Language.Get("FlashRushToBuy102"); flashSaleBtn.AddListener(SendBuyShopItem); break; } } public void SendBuyShopItem() { if (saleItem == null) return; if(UIHelper.GetMoneyCnt(saleItem.moneyType) >= (ulong)saleItem.moneyNumber) { C0803_tagCBuyItemList buyShop = new C0803_tagCBuyItemList(); buyShop.NPCID = 0; buyShop.BuyItemIndex = (ushort)saleItem.shopId; buyShop.BuyCount = (ushort)1; GameNetSystem.Instance.SendInfo(buyShop); } else { WindowCenter.Instance.Open(); } } private void RefreshCD(int code,int time) { if (this.code != code) return; cdTime = time; if (cdTime <= 0) { btnBgImg.material = MaterialUtility.GetUIDefaultGraphicMaterial(); cdText.text = Language.Get("FlashRushToBuy103"); } else { btnBgImg.material = MaterialUtility.GetDefaultSpriteGrayMaterial(); cdText.text = Language.Get("FlashRushToBuy104",time); } } private void CancelAppointment() { if(cdTime == 0) { rushToBuyModel.SendFlashSaleAppointment(saleItem.shopGuid, 0); } else { } } private void UpdateSaleItemSellState() { int fullSeverRemain = 0; int personalRemain = 0; int sellState = GetSellSate(out fullSeverRemain,out personalRemain); string fullSeverRemainStr = fullSeverRemain > 0 ? UIHelper.AppendColor(TextColType.Green, fullSeverRemain.ToString(),true) : UIHelper.AppendColor(TextColType.Red, fullSeverRemain.ToString()); string personalRemainStr = personalRemain > 0 ? UIHelper.AppendColor(TextColType.Green, personalRemain.ToString(),true) : UIHelper.AppendColor(TextColType.Red, personalRemain.ToString()); fullServerRemainNum.text = fullSeverRemainStr; personalRemainNum.text = personalRemainStr; switch (sellState) { case 0: stateImg.SetActive(false); flashSaleBtn.SetActive(true); break; case 1: stateImg.SetActive(true); stateImg.SetSprite("XT_KF_80"); flashSaleBtn.SetActive(false); break; case 2: stateImg.SetActive(true); stateImg.SetSprite("XT_KF_81"); flashSaleBtn.SetActive(false); break; } } /// /// 0 秒杀 1个人购买次数已用光 2 全服已抢光 /// /// private int GetSellSate(out int fullRemainNum,out int personalRemainNum) { fullRemainNum = 0; personalRemainNum = 0; var buyInfo = storeModel.GetBuyShopLimit((uint)saleItem.shopId); if (buyState != -1) { personalRemainNum = saleItem.limitNum; fullRemainNum = saleItem.fullServerLimitNum - rushToBuyModel.GetFullServerBuyCntById(saleItem.shopId); if (buyInfo != null) { personalRemainNum -= buyInfo.BuyCnt; } if (personalRemainNum <= 0) { personalRemainNum = 0; return 1; } if (fullRemainNum <= 0) { fullRemainNum = 0; return 2; } } else { personalRemainNum = saleItem.limitNum; fullRemainNum = saleItem.fullServerLimitNum; } return 0; } } }