hch
6 天以前 ee3196b5a04c15d025d1a9eab825d5d14987758d
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
using System;
using System.Collections.Generic;
using System.Linq;
 
using UnityEngine;
using LitJson;
using System.Collections;
 
 
 
public class ShopItemInfo
{
    public int itemId;
    public int count;
 
    public ShopItemInfo(int id, int count)
    {
        this.itemId = id;
        this.count = count;
    }
}
 
public class StoreModel : GameSystemManager<StoreModel>
{
    public Dictionary<int, List<StoreData>> storeTypeDict { get; private set; } //所有显示商品
    public Dictionary<int, int> shopRefreshCntDict = new Dictionary<int, int>();    //刷新次数
    private Dictionary<int, int> shopItemlimitDict = new Dictionary<int, int>();    //已购买次数
    public event Action RefreshBuyShopLimitEvent;
    public event Action RefreshShopEvent;
 
    public StoreFunc selectStoreFuncType = StoreFunc.Normal;
 
 
    public int buyShopID;
 
    //配置
    public int heroSoulRefreshMoneyType;
    public int heroSoulRefreshMoney;
    public int heroSoulRefreshFreeCount;
 
    Dictionary<int, List<int>> freeShopDict = new Dictionary<int, List<int>>();
    public Dictionary<int, int> shopMoneyTypeDict = new Dictionary<int, int>();
 
    public override void Init()
    {
        RedpointCenter.Instance.AddParent(MainRedDot.MainGuildRedpoint, MainRedDot.Store_REDPOINT);
        FuncOpen.Instance.OnFuncStateChangeEvent += FuncStateChange;
        TimeMgr.Instance.OnDayEvent += RefreshDay;
        GuildManager.Instance.EnterOrQuitGuildEvent += EnterOrQuitGuildEvent;
        DTC0102_tagCDBPlayer.beforePlayerDataInitializeEventOnRelogin += OnBeforePlayerDataInitializeRelogin;
        DTC0102_tagCDBPlayer.beforePlayerDataInitializeEvent += OnBeforePlayerDataInitialize;    
 
        ParseFuncConfig();
        ParseStoreConfig();
    }
 
    public override void Release()
    {
        FuncOpen.Instance.OnFuncStateChangeEvent -= FuncStateChange;
        TimeMgr.Instance.OnDayEvent -= RefreshDay;
        GuildManager.Instance.EnterOrQuitGuildEvent -= EnterOrQuitGuildEvent;
        DTC0102_tagCDBPlayer.beforePlayerDataInitializeEventOnRelogin -= OnBeforePlayerDataInitializeRelogin;
        DTC0102_tagCDBPlayer.beforePlayerDataInitializeEvent -= OnBeforePlayerDataInitialize;
    }
 
    void OnBeforePlayerDataInitialize()
    {
        shopItemlimitDict.Clear();
        storeTypeDict.Remove((int)StoreFunc.Hero);  //由服务器通知
        selectStoreFuncType = StoreFunc.Normal;
    }
 
    void OnBeforePlayerDataInitializeRelogin()
    {
        buyItemCheckDict.Clear();
        
    }
 
 
    #region 解析本地数据
    private void ParseFuncConfig()
    {
        var config = FuncConfigConfig.Get("StoreHeroSoul");
        var tmpArr = ConfigParse.GetMultipleStr<int>(config.Numerical2);
        heroSoulRefreshMoneyType = tmpArr[0];
        heroSoulRefreshMoney = tmpArr[1];
        heroSoulRefreshFreeCount = int.Parse(config.Numerical3);
    }
 
    
    private void ParseStoreConfig()
    {
        freeShopDict.Clear();
        storeTypeDict = new Dictionary<int, List<StoreData>>();
        List<StoreConfig> storeConfigs = StoreConfig.GetValues();
        for (int i = 0; i < storeConfigs.Count; i++)
        {
            var config = storeConfigs[i];
            //UnlockType 2 由服务端通知
            if (config.ShopType != 0 && config.UnlockType != 2)
            {
                //构建商品数据
                StoreData storeData = new StoreData();
                storeData.shopId = config.ID;
                storeData.storeConfig = config;
                if (!storeTypeDict.ContainsKey(config.ShopType))
                {
                    List<StoreData> storeDatas = new List<StoreData>();
                    storeDatas.Add(storeData);
                    storeTypeDict.Add(config.ShopType, storeDatas);
                }
                else
                {
                    storeTypeDict[config.ShopType].Add(storeData);
                }
 
                //构建免费商品数据
                if (config.MoneyNum == 0)
                {
                    if (!freeShopDict.ContainsKey(config.ShopType))
                    {
                        freeShopDict.Add(config.ShopType, new List<int>());
                    }
                    freeShopDict[config.ShopType].Add(config.ID);
                }
            }
 
            //构建商品价格类型
            if (!shopMoneyTypeDict.ContainsKey(config.ShopType))
            {
                shopMoneyTypeDict[config.ShopType] = config.MoneyType;
            }
        }
 
        foreach (var type in storeTypeDict.Keys)
        {
            var configs = storeTypeDict[type];
            configs.Sort(CompareByShopSort);
        }
    }
 
    
 
    public int CompareByShopSort(StoreData start, StoreData end)
    {
        int sort1 = start.storeConfig.ShopSort;
        int sort2 = end.storeConfig.ShopSort;
 
        return sort1.CompareTo(sort2);
    }
 
    public class StoreData
    {
        public int shopId;
        public StoreConfig storeConfig;
    }
    #endregion
 
    #region 获取数据
 
 
    private bool IsSellOut(StoreData storeData)
    {
        int remainNum = 0;
        if (TryGetIsSellOut(storeData.storeConfig, out remainNum))
            return true;
 
        return false;
    }
 
 
    public List<StoreData> TryGetStoreDatas(StoreFunc type)
    {
        List<StoreData> datas = null;
        if (storeTypeDict.TryGetValue((int)type, out datas))
        {
            return datas;
        }
 
        return datas;
    }
 
 
 
 
    public StoreData GetStoreData(int shopId)
    {
        StoreConfig storeConfig = StoreConfig.Get(shopId);
        if (storeConfig == null) return null;
 
        List<StoreData> storeDatas = null;
        storeTypeDict.TryGetValue(storeConfig.ShopType, out storeDatas);
        if (storeDatas != null)
        {
            for (int i = 0; i < storeDatas.Count; i++)
            {
                var storeData = storeDatas[i];
                if (storeData.shopId == shopId)
                {
                    return storeData;
                }
            }
        }
        return null;
    }
 
 
 
 
 
    public bool TryGetIsSellOut(StoreConfig storeConfig, out int remainCnt)
    {
        int canBuyCnt = storeConfig.LimitCnt;
        remainCnt = 0;
 
        //不限购
        if (canBuyCnt == 0) return false;
 
        remainCnt = canBuyCnt;
        var buyCnt = GetShopLimitBuyCount(storeConfig.ID);
        remainCnt -= buyCnt;
 
        if (remainCnt <= 0)
        {
            return true;
        }
        return false;
    }
 
  
 
 
    #endregion
 
 
 
    private void RefreshDay()
    {
        UpdateFreeShopRedpoint();
    }
    
    void EnterOrQuitGuildEvent(bool isEnter)
    {
        UpdateFreeShopRedpoint();
    }
 
 
 
    /// <summary>
    /// 得到物品购买次数
    /// </summary>
    /// <param name="buylimit"></param>
    public void RefreshBuyShopLimitModel(HA802_tagSCShopItemBuyCntInfo buylimit)
    {
        int i = 0;
        for (i = 0; i < buylimit.Count; i++)
        {
            shopItemlimitDict[(int)buylimit.BuyCntList[i].ShopID] = (int)buylimit.BuyCntList[i].BuyCnt;
        }
 
        UpdateFreeShopRedpoint();
        if (RefreshBuyShopLimitEvent != null)
            RefreshBuyShopLimitEvent();
    }
 
    // 限购商品已购买次数
    public int GetShopLimitBuyCount(int shopID)
    {
        if (shopItemlimitDict.ContainsKey(shopID))
        {
            return shopItemlimitDict[shopID];
        }
        return 0;
    }
 
    public void UpdateRefreshItemInfo(HA803_tagSCShopRefreshItemInfo netPack)
    {
        storeTypeDict.Remove(netPack.ShopType);
        shopRefreshCntDict[netPack.ShopType] = netPack.RefreshCnt;
        List<int> resortList = new List<int>();
        for (int i = 0; i < netPack.ShopIDList.Length; i++)
        {
            var config = StoreConfig.Get((int)netPack.ShopIDList[i]);
            //UnlockType 2 由服务端通知
            StoreData storeData = new StoreData();
            storeData.shopId = config.ID;
            storeData.storeConfig = config;
            if (!storeTypeDict.ContainsKey(config.ShopType))
            {
                List<StoreData> storeDatas = new List<StoreData>();
                storeDatas.Add(storeData);
                storeTypeDict.Add(config.ShopType, storeDatas);
            }
            else
            {
                storeTypeDict[config.ShopType].Add(storeData);
            }
        }
 
        if (storeTypeDict.ContainsKey(netPack.ShopType))
        {
            var configs = storeTypeDict[netPack.ShopType];
            configs.Sort(CompareByShopSort);
        }
 
        RefreshShopEvent?.Invoke();
    }
 
 
 
    //和GetShopItemlistEx 一样 获取商店物品列表,只是返回类型不同
    public List<ShopItemInfo> GetShopItemlistByIndex(StoreConfig storeConfig)
    {
        if (storeConfig == null) return null;
        List<ShopItemInfo> shopItemlist = new List<ShopItemInfo>();
 
        if (storeConfig.ItemID != 0)
        {
            ShopItemInfo shopItem = new ShopItemInfo(storeConfig.ID, storeConfig.ItemCnt);
            shopItemlist.Add(shopItem);
        }
        var extraItem = storeConfig.ItemListEx;
        if (extraItem != null)
        {
            for (int i = 0; i < extraItem.Length; i++)
            {
                ShopItemInfo shop = new ShopItemInfo(extraItem[i][0], extraItem[i][1]);
                shopItemlist.Add(shop);
            }
        }
        return shopItemlist;
    }
 
    //和GetShopItemlistByIndex 一样 获取商店物品列表,只是返回类型不同
    public List<int[]> GetShopItemlistEx(StoreConfig storeConfig)
    {
        if (storeConfig == null) return null;
 
        List<int[]> shopItemlist = new List<int[]>();
        if (storeConfig.ItemID != 0)
        {
            shopItemlist.Add(new int[] { storeConfig.ID, storeConfig.ItemCnt});
        }
        var extraItem = storeConfig.ItemListEx;
        for (int i = 0; i < extraItem.Length; i++)
        {
            shopItemlist.Add(extraItem[i]);
        }
        return shopItemlist;
    }
 
    public bool TryGetShopItemInfo(int _id, out List<ShopItemInfo> _shopItems)
    {
        _shopItems = GetShopItemlistByIndex(StoreConfig.Get(_id));
        return _shopItems != null;
    }
 
 
    public void SendBuyShopItem(StoreConfig model, int count)
    {
        if (UIHelper.CheckMoneyCount(model.MoneyType, model.MoneyNum * count, 2))
        {
            CA310_tagCSBuyItem buyShop = new CA310_tagCSBuyItem();
            buyShop.ShopID = (ushort)model.ID;
            buyShop.BuyCount = (uint)count;
            GameNetSystem.Instance.SendInfo(buyShop);
        }
 
    }
 
    //仙玉购买物品的二次确认框,一级货币只有仙玉 默认为仙玉即可
    Dictionary<int, bool> buyItemCheckDict = new Dictionary<int, bool>();   //记录勾选信息
    //eventType 二次确认框类型,对应枚举 BuyStoreItemCheckType
    public void SendBuyShopItemWithPopCheck(StoreConfig model, int count, int eventType = 0)
    {
        if (model.MoneyNum == 0)
        {
            //免费的
            SendBuyShopItem(model, count);
            return;
        }
 
        if (buyItemCheckDict.ContainsKey(eventType) && buyItemCheckDict[eventType])
        {
            SendBuyShopItem(model, count);
            return;
        }
 
        ConfirmCancel.ToggleConfirmCancel(Language.Get("Mail101"), Language.Get("CostMoney", model.MoneyNum, model.MoneyType),
        Language.Get("ConfirmCancel102"), (bool isOk, bool isToggle) =>
        {
            if (isOk)
            {
                SendBuyShopItem(model, count);
                buyItemCheckDict[eventType] = isToggle;
            }
            
        });
    }
 
    //花仙玉购买的二次确认框(本次登录)
    //eventType 二次确认框类型,对应枚举 BuyStoreItemCheckType
    public void UseMoneyCheck(int money, int moneyType, Action func, int eventType = 0, string tip = "CostMoney", string fullTip = "")
    {
        if (money == 0)
        {
            //免费的
            func?.Invoke();
            return;
        }
 
        if (buyItemCheckDict.ContainsKey(eventType) && buyItemCheckDict[eventType])
        {
            func?.Invoke();
            return;
        }
 
        ConfirmCancel.ToggleConfirmCancel(Language.Get("Mail101"), fullTip == "" ? Language.Get(tip, money, moneyType) : fullTip,
            Language.Get("ConfirmCancel102"), (bool isOk, bool isToggle) =>
            {
                if (isOk)
                {
                    func?.Invoke();
                    buyItemCheckDict[eventType] = isToggle;
                }
 
            });
    }
 
 
 
    private void FuncStateChange(int funcId)
    {
        switch ((FuncOpenEnum)funcId)
        {
            case FuncOpenEnum.Store:
                UpdateFreeShopRedpoint();
                break;
        }
    }
 
 
 
    #region 商城红点
    Redpoint storeRedpoint = new Redpoint(MainRedDot.RightFuncRedpoint, MainRedDot.Store_REDPOINT);
    Redpoint normalRedpoint = new Redpoint(MainRedDot.Store_REDPOINT, MainRedDot.Store_REDPOINT * 10 + 1);
    Redpoint guildRedpoint = new Redpoint(MainRedDot.Store_REDPOINT, MainRedDot.Store_REDPOINT * 10 + 2);
 
    public void UpdateFreeShopRedpoint()
    {
        if (!FuncOpen.Instance.IsFuncOpen((int)FuncOpenEnum.Store)) return;
 
        normalRedpoint.state = RedPointState.None;
        guildRedpoint.state = RedPointState.None;
 
        if (freeShopDict.Count == 0) return;
 
        if (freeShopDict.ContainsKey((int)StoreFunc.Normal))
        {
            var shopList = freeShopDict[(int)StoreFunc.Normal];
 
            for (int i = 0; i < shopList.Count; i++)
            {
                var shopID = shopList[i];
                var config = StoreConfig.Get(shopID);
                if (shopItemlimitDict.ContainsKey(shopID))
                {
                    if (shopItemlimitDict[shopID] < config.LimitCnt)
                    {
                        normalRedpoint.state = RedPointState.Simple;
                        break;
                    }
                }
                else
                {
                    normalRedpoint.state = RedPointState.Simple;
                    break;
                }
            }
        }
 
        if (PlayerDatas.Instance.fairyData.HasFairy && freeShopDict.ContainsKey((int)StoreFunc.Guild))
        {
            var shopList = freeShopDict[(int)StoreFunc.Guild];
 
            for (int i = 0; i < shopList.Count; i++)
            {
                var shopID = shopList[i];
                var config = StoreConfig.Get(shopID);
                if (shopItemlimitDict.ContainsKey(shopID))
                {
                    if (shopItemlimitDict[shopID] < config.LimitCnt)
                    {
                        guildRedpoint.state = RedPointState.Simple;
                        break;
                    }
                }
                else
                {
                    guildRedpoint.state = RedPointState.Simple;
                    break;
                }
            }
        }
 
    }
 
 
 
    #endregion
 
    public void RefreshStore(int shopType)
    {
        var pack = new CA232_tagCSRefreshShop();
        pack.ShopType = (ushort)shopType;
        GameNetSystem.Instance.SendInfo(pack);
    }
 
    //获取商品状态  0可购买 1已售罄 2免费 3未解锁
    public int GetShopIDState(int shopID)
    {
        var config = StoreConfig.Get(shopID);
        if (config.UnlockType == 1)
        {
            if (!PlayerDatas.Instance.fairyData.HasFairy ||
            PlayerDatas.Instance.fairyData.fairy.FamilyLV < config.UnlockValue)
            {
                return 3;
            }
        }
 
        if (config.LimitCnt != 0 && GetShopLimitBuyCount(shopID) >= config.LimitCnt)
        {
            return 1;
        }
 
        if (config.MoneyNum == 0)
        {
            return 2;
        }
        
        return 0;
    }
}
 
public enum StoreFunc
{
    Normal = 1, //1:坊市
    Guild = 2, //2:公会
    Hero = 3, //3:将魂
    
}