using UnityEngine; using UnityEngine.UI; using TableConfig; using System; namespace Snxxz.UI { public class FlashSaleItemCell : 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; 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; int cdTime = 0; int code = 0; private void OnEnable() { battleModel.RefreshGameRecInfoAct += UpdateFullServerBuy; storeModel.RefreshBuyShopLimitEvent += RefreshBuyShopLimitEvent; rushToBuyModel.UpdateAllAppointmentEvent += UpdateAllAppointmentInfo; rushToBuyModel.UpdateAppointmentEvent += UpdateAppointmentInfo; KnapsackTimeCDMgr.Instance.RefreshNormalCDAct += RefreshCD; } private void OnDisable() { storeModel.RefreshBuyShopLimitEvent -= RefreshBuyShopLimitEvent; battleModel.RefreshGameRecInfoAct -= 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,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; if(saleItem.isAppointment == 1) { cdTime = 10; KnapsackTimeCDMgr.Instance.Register(this.code,cdTime); } 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); btnBgImg.SetSprite("SecondBtn1"); btnStateText.gameObject.SetActive(true); cdText.gameObject.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.gameObject.SetActive(false); cdText.gameObject.SetActive(true); flashSaleBtn.AddListener(CancelAppointment); } break; case 0: btnStateText.text = Language.Get("FlashRushToBuy102"); flashSaleBtn.AddListener(() => { storeModel.SendBuyShopItem(storeConfig,saleItem.itemCount); }); break; } } 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 sellState = GetSellSate(out fullSeverRemain); string fullSeverRemainStr = fullSeverRemain > 0 ? fullSeverRemain.ToString() : UIHelper.GetTextColorByItemColor(TextColType.Red, fullSeverRemain.ToString()); fullServerRemainNum.text = fullSeverRemainStr; switch (sellState) { case 0: stateImg.gameObject.SetActive(false); flashSaleBtn.gameObject.SetActive(true); break; case 1: stateImg.gameObject.SetActive(true); stateImg.SetSprite("XT_KF_80"); flashSaleBtn.gameObject.SetActive(false); break; case 2: stateImg.gameObject.SetActive(true); stateImg.SetSprite("XT_KF_81"); 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; if (buyState != -1) { fullRemainNum = saleItem.fullServerLimitNum - rushToBuyModel.GetFullServerBuyCntById(saleItem.shopId); if (buyInfo != null) { buyCount = buyInfo.BuyCnt; } if (buyCount >= saleItem.limitNum) { return 1; } if (fullRemainNum <= 0) { return 2; } } else { fullRemainNum = saleItem.fullServerLimitNum; } return 0; } } }