yyl
2 天以前 973edc44a04dceb8b48a32ca912e6167f86189d4
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
using LitJson;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
 
 
public class HappyXBModel : GameSystemManager<HappyXBModel>
{
    //寻宝产出库
    private Dictionary<int, Dictionary<int, XBGetItemConfig>> xbGetItemDict = new Dictionary<int, Dictionary<int, XBGetItemConfig>>(); //奖池的所有物品(按类型+等级)
    private Dictionary<int, List<XBGetItemConfig>> xbTypeItemDict = new Dictionary<int, List<XBGetItemConfig>>(); //奖池的所有物品(按类型)
 
    float xbLastTime = 0;   //等待服务端回复 如果太长就重置为不等待
    bool m_IsWaitServerXB = false;  // 等待服务端回复
    public bool isXBCoolTime
    {
        get
        {
            #if UNITY_EDITOR
            if (Time.time - xbLastTime > 1)
            #else
            if (Time.time - xbLastTime > 10)
            #endif
            {
                m_IsWaitServerXB = false;
                return m_IsWaitServerXB;
            }
 
            return m_IsWaitServerXB;
        }
 
        set
        {
            m_IsWaitServerXB = value;
            xbLastTime = Time.time;
        }
    }  
    private Dictionary<int, XBTypeInfo> xbTypeInfoDict = new Dictionary<int, XBTypeInfo>(); //抽奖状态相关的 服务器记录
 
    public override void Init()
    {
        DTC0102_tagCDBPlayer.beforePlayerDataInitializeEvent += OnBeforePlayerDataInitialize;
        FuncOpen.Instance.OnFuncStateChangeEvent += UpdateFuncState;
        PackManager.Instance.RefreshItemEvent += RefreshXBTool;
 
        xbGetItemDict.Clear();
        xbTypeItemDict.Clear();
        List<XBGetItemConfig> list = XBGetItemConfig.GetValues();
        for (int i = 0; i < list.Count; i++)
        {
            if (!xbGetItemDict.ContainsKey(list[i].TreasureType))
            {
                xbGetItemDict.Add(list[i].TreasureType, new Dictionary<int, XBGetItemConfig>());
            }
            xbGetItemDict[list[i].TreasureType].Add(list[i].MinLV, list[i]);
 
 
            if (!xbTypeItemDict.ContainsKey(list[i].TreasureType))
            {
                xbTypeItemDict.Add(list[i].TreasureType, new List<XBGetItemConfig>());
            }
 
            xbTypeItemDict[list[i].TreasureType].Add(list[i]);
        }
    }
 
    public override void Release()
    {
        DTC0102_tagCDBPlayer.beforePlayerDataInitializeEvent -= OnBeforePlayerDataInitialize;
        FuncOpen.Instance.OnFuncStateChangeEvent -= UpdateFuncState;
        PackManager.Instance.RefreshItemEvent -= RefreshXBTool;
    }
 
 
 
    public void OnBeforePlayerDataInitialize()
    {
        isXBCoolTime = false;
        xbTypeInfoDict.Clear();
    }
 
 
    public XBGetItemConfig GetXBItemConfigByType(int type)
    {
        int lv = 0;
        List<XBGetItemConfig> configlist = null;
        xbTypeItemDict.TryGetValue(type, out configlist);
        if (configlist != null)
        {
            for (int i = configlist.Count - 1; i > -1; i--)
            {
                if (PlayerDatas.Instance.baseData.LV >= configlist[i].MinLV)
                {
                    lv = configlist[i].MinLV;
                    return configlist[i];
                }
            }
        }
        return null;
    }
 
    #region 处理服务端数据
    public event Action RefreshXBTypeInfoAct;
    public event Action RefreshXBResultAct;
    public int addXBScore { get; private set; }
    public int addXBScoreType { get; private set; } //寻宝积分货币类型
    public int addXBLuckValue { get; private set; }
    public Dictionary<int, XBGetItem> xbResultDict { get; private set; } = new Dictionary<int, XBGetItem>(); //奖品顺序:奖品
    public void GetServerXBResult(HA350_tagMCTreasureResult result)
    {
        xbResultDict.Clear();
        addXBScore = result.AddMoneyValue;
        addXBScoreType = result.AddMoneyType;
        addXBLuckValue = result.AddTreasureLuck;
        JsonData resultData = JsonMapper.ToObject(result.TreasureResult);
        if (resultData.IsArray)
        {
            for (int i = 0; i < resultData.Count; i++)
            {
                if (resultData[i].IsArray)
                {
                    int index = int.Parse(resultData[i][0].ToString());
                    int itemId = int.Parse(resultData[i][1].ToString());
                    int count = int.Parse(resultData[i][2].ToString());
                    XBGetItem getItem = new XBGetItem();
                    getItem.SetModel(index, itemId, count);
                    if (!xbResultDict.ContainsKey(i))
                    {
                        xbResultDict.Add(i, getItem);
                    }
                    else
                    {
                        xbResultDict[i] = getItem;
                    }
                }
            }
        }
        isXBCoolTime = false;
        if (RefreshXBResultAct != null)
        {
            RefreshXBResultAct();
        }
        if (!UIManager.Instance.IsOpened<HeroCallResultWin>())
        { 
            UIManager.Instance.OpenWindow<HeroCallResultWin>();
        }
    }
 
 
    public int GetCountInResult(int itemID)
    { 
        int count = 0;
        if (xbResultDict != null && xbResultDict.Count > 0)
        {
            foreach (var item in xbResultDict)
            {
                if (item.Value.itemId == itemID)
                {
                    count++;
                }
            }
        }
        return count;
    }
 
 
    public void ClearXBRusltData()
    {
        xbResultDict.Clear();
    }
 
    
    public void GetServerXBInfo(HA351_tagMCTreasureInfo info)
    {
        for (int i = 0; i < info.InfoCount; i++)
        {
            if (!xbTypeInfoDict.ContainsKey(info.TreasuerInfoList[i].TreasureType))
            {
                XBTypeInfo typeInfo = new XBTypeInfo();
                typeInfo.xbType = info.TreasuerInfoList[i].TreasureType;
                typeInfo.luckValue = info.TreasuerInfoList[i].LuckValue;
                typeInfo.freeCountToday = info.TreasuerInfoList[i].FreeCountToday;
                typeInfo.treasureCount = (int)info.TreasuerInfoList[i].TreasureCount;
                typeInfo.treasureCountToday = (int)info.TreasuerInfoList[i].TreasureCountToday;
                typeInfo.treasureCntAward = (int)info.TreasuerInfoList[i].TreasureCntAward;
                if (typeInfo.gridLimitCntDict == null)
                    typeInfo.gridLimitCntDict = new Dictionary<int, int>();
                for (int j = 0; j < info.TreasuerInfoList[i].GridLimitCntList.Length; j++)
                {
                    int num = info.TreasuerInfoList[i].GridLimitCntList[j].GridNum;
                    int cnt = info.TreasuerInfoList[i].GridLimitCntList[j].GridCnt;
                    typeInfo.gridLimitCntDict[num] = cnt;
                }
                xbTypeInfoDict.Add(info.TreasuerInfoList[i].TreasureType, typeInfo);
            }
            else
            {
                xbTypeInfoDict[info.TreasuerInfoList[i].TreasureType].luckValue = info.TreasuerInfoList[i].LuckValue;
                xbTypeInfoDict[info.TreasuerInfoList[i].TreasureType].freeCountToday = info.TreasuerInfoList[i].FreeCountToday;
                xbTypeInfoDict[info.TreasuerInfoList[i].TreasureType].treasureCount = (int)info.TreasuerInfoList[i].TreasureCount;
                xbTypeInfoDict[info.TreasuerInfoList[i].TreasureType].treasureCountToday = (int)info.TreasuerInfoList[i].TreasureCountToday;
                xbTypeInfoDict[info.TreasuerInfoList[i].TreasureType].treasureCntAward = (int)info.TreasuerInfoList[i].TreasureCntAward;
                if (xbTypeInfoDict[info.TreasuerInfoList[i].TreasureType].gridLimitCntDict == null)
                    xbTypeInfoDict[info.TreasuerInfoList[i].TreasureType].gridLimitCntDict = new Dictionary<int, int>();
                for (int j = 0; j < info.TreasuerInfoList[i].GridLimitCntList.Length; j++)
                {
                    int num = info.TreasuerInfoList[i].GridLimitCntList[j].GridNum;
                    int cnt = info.TreasuerInfoList[i].GridLimitCntList[j].GridCnt;
                    xbTypeInfoDict[info.TreasuerInfoList[i].TreasureType].gridLimitCntDict[num] = cnt;
                }
            }
        }
 
 
        if (RefreshXBTypeInfoAct != null)
        {
            RefreshXBTypeInfoAct();
        }
 
        HeroCallRedPoint();
    }
 
    public XBTypeInfo GetXBInfoByType(int type)
    {
        XBTypeInfo typeInfo = null;
        xbTypeInfoDict.TryGetValue(type, out typeInfo);
        return typeInfo;
    }
 
    /// <summary>
    /// 请求寻宝
    /// </summary>
    /// <param name="type">寻宝类型</param>
    /// <param name="index">0 单次寻宝 1 多次寻宝</param>
    /// <param name="costType">0-货币;1-免费次数;2-寻宝道具</param>
    public event Action<int> StartXBEvent;
    public void SendXBQuest(int type, int index, int costType)
    {
        if (isXBCoolTime) return;
        
        isXBCoolTime = true;
        CA568_tagCMRequestTreasure treasure = new CA568_tagCMRequestTreasure();
        treasure.TreasureType = (byte)type;
        treasure.TreasureIndex = (byte)index;
        treasure.CostType = (byte)costType;
        GameNetSystem.Instance.SendInfo(treasure);
        if (StartXBEvent != null)
        {
            StartXBEvent(index);
        }
    }
 
 
 
    public bool CheckIsEmptyGrid(PackType type, int needGrid = 1)
    {
        switch (type)
        {
            // case PackType.Treasure:
            //     SinglePack singlePack = PackManager.Instance.GetSinglePack(type);
            //     if (GeneralDefine.maxXBGridCount - singlePack.GetAllItems().Count < needGrid)
            //     {
            //         SysNotifyMgr.Instance.ShowTip("XBWarehouseFull");
            //         return false;
            //     }
            //     break;
            case PackType.Item:
                if (PackManager.Instance.GetEmptyGridCount(type) < needGrid)
                {
                    SysNotifyMgr.Instance.ShowTip("BagFull");
                    return false;
                }
                break;
 
            case PackType.Hero:
                if (PackManager.Instance.GetEmptyGridCount(type) < needGrid)
                {
                    SysNotifyMgr.Instance.ShowTip("HeroBagFull");
                    return false;
                }
                break;
        }
        return true;
    }
 
    public void SendOneXBQuest(int xbType)
    {
        var config = TreasureSetConfig.Get(xbType);
        if (GetXBInfoByType(xbType).treasureCountToday + config.TreasureCountList[0] > config.DailyMaxCount)
        {
            SysNotifyMgr.Instance.ShowTip("XBTodayMax");
            return;
        }
 
        var funcSet = TreasureSetConfig.Get(xbType);
        if (CheckIsEmptyGrid((PackType)config.PackType))
        {
            //道具寻宝
            if (funcSet.CostItemID != 0 && IsHaveOneXBTool(xbType))
            {
                SendXBQuest(xbType, 0, 2);
                return;
            }
 
            //货币寻宝
            int moneyType = funcSet.CostMoneyType;
            int xbOneMoney = funcSet.CostMoneyList[0];
 
            if (UIHelper.GetMoneyCnt(moneyType) >= xbOneMoney)
            {
                //暂定充值货币需要二次确认
                if (moneyType == 1)
                {
                    StoreModel.Instance.UseMoneyCheck(xbOneMoney, moneyType, () =>
                    {
                        SendXBQuest(xbType, 0, 0);
                    }, (int)BuyStoreItemCheckType.HeroCall, fullTip: Language.Get("CostMoneyForItem", funcSet.CostItemID, xbOneMoney,
                    UIHelper.GetIconNameWithMoneyType(moneyType), funcSet.CostItemCountList[0]));
 
                }
                else
                {
                    SendXBQuest(xbType, 0, 0);
                }
            }
            else
            {
                ItemTipUtility.ShowMoneyTip(moneyType);
            }
        }
    }
 
 
    public void SendXBManyQuest(int xbType)
    {
 
        var config = TreasureSetConfig.Get(xbType);
        if (GetXBInfoByType(xbType).treasureCountToday + config.TreasureCountList[1] > config.DailyMaxCount)
        {
            SysNotifyMgr.Instance.ShowTip("XBTodayMax");
            return;
        }
 
        var funcSet = TreasureSetConfig.Get(xbType);
        if (CheckIsEmptyGrid((PackType)config.PackType, 10))
        {
            int toolCnt = 0;
            int needToolCnt = 0;
            int needMoney = 0;
            if (IsHaveManyXBToolEx(xbType, out toolCnt, out needToolCnt, out needMoney))
            {
                SendXBQuest(xbType, 1, 2);
            }
 
            //货币寻宝
            int moneyType = funcSet.CostMoneyType;
            if (UIHelper.GetMoneyCnt(moneyType) >= needMoney)
            {
                //暂定充值货币需要二次确认
                if (moneyType == 1)
                {
                    StoreModel.Instance.UseMoneyCheck(needMoney, moneyType, () =>
                    {
                        //只要有道具就是道具寻宝,不足部分服务端扣货币
                        SendXBQuest(xbType, 1, toolCnt > 0 ? 2 : 0);
                    }, (int)BuyStoreItemCheckType.HeroCall, fullTip: Language.Get("CostMoneyForItem", funcSet.CostItemID, needMoney,
                    UIHelper.GetIconNameWithMoneyType(moneyType), needToolCnt - toolCnt));
                }
                else
                {
                    SendXBQuest(xbType, 1, toolCnt > 0 ? 2 : 0);
                }
            }
            else
            {
                ItemTipUtility.ShowMoneyTip(moneyType);
            }
 
        }
    }
 
 
    #endregion
 
    public bool IsHaveFreeXB(int type)
    {
        int freeCountToday = 0;
        XBTypeInfo typeInfo = GetXBInfoByType(type);
        if (typeInfo != null)
        {
            freeCountToday = typeInfo.freeCountToday;
        }
 
        //判断是否有免费次数,且免费次数是否用完
        var funcSet = TreasureSetConfig.Get(type);
        return freeCountToday < funcSet.DailyFreeCount;
    }
 
 
    public bool CheckIsXBTool(int itemId, int type)
    {
        var funcSet = TreasureSetConfig.Get(type);
        if (funcSet == null) return false;
 
        if (funcSet.CostItemID == itemId)
        {
            return true;
        }
        return false;
    }
    public bool IsHaveOneXBTool(int type)
    {
        var funcSet = TreasureSetConfig.Get(type);
        if (funcSet == null) return false;
 
        int toolCnt = (int)PackManager.Instance.GetItemCountByID(PackType.Item, funcSet.CostItemID);
        if (toolCnt >= funcSet.CostItemCountList[0])
        {
            return true;
        }
 
        return false;
    }
 
 
    //needToolCnt 为配表中需要的道具数量
    // 可获得真正需要补足购买道具的金额
    public bool IsHaveManyXBToolEx(int type, out int toolCnt, out int needToolCnt, out int needMoney)
    {
        toolCnt = 0;
        needToolCnt = 0;    //配置中需要的道具数量
        needMoney = 0;  //真正需要补足购买道具的金额
        var funcSet = TreasureSetConfig.Get(type);
        if (funcSet == null) return false;
 
        toolCnt = (int)PackManager.Instance.GetItemCountByID(PackType.Item, funcSet.CostItemID);
        needToolCnt = funcSet.CostItemCountList[1];
 
        if (toolCnt >= needToolCnt)
        {
            //道具足够
            return true;
        }
 
        if (funcSet.CostMoneyType != 0)
        {
            if (toolCnt != 0 && toolCnt < needToolCnt)
            {
                //部分不足的按单次价格算   
                needMoney = funcSet.CostMoneyList[0] * (needToolCnt - toolCnt);
            }
            else
            { 
                //全部不足的按多次价格算 可能配置了折扣
                needMoney = funcSet.CostMoneyList[1];
            }
        }
        
        return false;
    }
 
    public int GetCostType(int type)
    {
        var funcSet = TreasureSetConfig.Get(type);
        if (funcSet == null) return 0;
        return funcSet.CostMoneyType;
    }
 
    public int GetCostItemID(int type)
    { 
        var funcSet = TreasureSetConfig.Get(type);
        if (funcSet == null) return 0;
        return funcSet.CostItemID;
    }
 
 
    #region 红点逻辑
    public const int HappyXB_RedKey = 203;
    public const int XBHeroCall1_RedKey = 20300;    //武将免费召唤
 
 
    public Redpoint happyXBRed = new Redpoint(HappyXB_RedKey);
    public Redpoint bestXBFreeRed = new Redpoint(HappyXB_RedKey, XBHeroCall1_RedKey);
    
 
    private void UpdateFuncState(int funcId)
    {
        HeroCallRedPoint();
    }
 
 
    private void RefreshXBTool(PackType type, int index, int id)
    {
        if (type != PackType.Item) return;
        if (!CheckIsXBTool(id, (int)HappXBTitle.HeroCallAdvanced))
            return;
 
        HeroCallRedPoint();
    }
 
    //英雄招募
    public void HeroCallRedPoint()
    {
        if (!FuncOpen.Instance.IsFuncOpen((int)FuncOpenEnum.HappyFindTreasure)) return;
        // 免费 10连 积分
        happyXBRed.state = RedPointState.None;
 
 
        if (IsHaveFreeXB((int)HappXBTitle.HeroCallAdvanced))
        {
            bestXBFreeRed.state = RedPointState.Simple;
            return;
        }
        else
        {
            bestXBFreeRed.state = RedPointState.None;
        }
 
        if (IsHaveManyXBToolEx((int)HappXBTitle.HeroCallAdvanced, out int xbtoolCnt, out int needtoolCnt, out int needMoney))
        {
            happyXBRed.state = RedPointState.Simple;
            return;
        }
 
        //积分足够
 
 
    }
 
    //按格子库配置的奖池 获得获取物品
    public List<int> GetAllGridLibItemIDByType(int type)
    {
        List<int> itemIDListTemp = new List<int>();
        foreach (var kv in GetXBItemConfigByType(type).GridLibInfo)
        {
            itemIDListTemp.AddRange(TreasureItemLibConfig.GetItemIDList(kv.Value));
        }
        return itemIDListTemp.Distinct().ToList();
 
    }
    
    #endregion
    }
 
public class XBTypeInfo
{
    public int xbType;   // 1 极品寻宝 2 符印寻宝 
    public int luckValue;
    public int freeCountToday;      //今日已免费寻宝次数
    public int treasureCount;        //已寻宝总次数
    public int treasureCountToday;        //今日已寻宝总次数
    public int treasureCntAward;        //累计寻宝次数对应奖励领奖状态,按奖励记录索引二进制记录是否已领取
    public Dictionary<int, int> gridLimitCntDict;        //<有限制抽取次数的格子编号,已抽到次数> 有限制抽取次数的格子次数信息
}
 
 
 
public class XBGetItem
{
    public int gridIndex;
    public int itemId;
    public int count;
    public DateTime createTime;
 
    public void SetModel(int index, int id, int count)
    {
        this.gridIndex = index;
        this.itemId = id;
        this.count = count;
        createTime = TimeUtility.ServerNow;
    }
}
 
public enum HappXBTitle
{
    Best = 1,
    Rune = 2,
    //Store = 3,
    //Warehouse = 4,
    GatherSoul = 4, //聚魂寻宝
    Gubao1 = 5,
    Gubao2 = 6,
    Gubao3 = 7,
    Gubao4 = 8,
    HeroCallNormal = 11,    //11-普通招募
    HeroCallAdvanced = 12,  //12-高级招募
    HeroCallScore = 13, //13-积分招募
    YunShi1 = 105,
    YunShi2 = 106,
    YunShi3 = 107,
    YunShi4 = 108,
}