少年修仙传客户端代码仓库
Client_PangDeRong
2018-10-17 faabe3e69e7451b195b4a17dc6002fd1b7332746
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
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<FlashRushToBuyModel>(); } }
        StoreModel storeModel { get { return ModelCenter.Instance.GetModel<StoreModel>(); } }
        ItemTipsModel tipsModel { get { return ModelCenter.Instance.GetModel<ItemTipsModel>(); } }
 
        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,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 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;
 
            StoreConfig storeConfig = Config.Instance.Get<StoreConfig>(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,1);
                    });
                    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;
            }
 
        }
        /// <summary>
        /// 0 秒杀 1 已买到 2 已抢光
        /// </summary>
        /// <returns></returns>
        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;
        }
    }
}