yyl
3 天以前 bcd1dcef07dc129d68539ca2d562c5df53007f36
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
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
 
public class TimeRushManager : GameSystemManager<TimeRushManager>, IOpenServerActivity
{
 
 
    public Action PlayAnimationSync;
 
    private bool isPlayAnimation = false;
    public bool IsPlayAnimation
    {
        get
        {
            return isPlayAnimation;
        }
        set
        {
            isPlayAnimation = value;
            if (isPlayAnimation)
            {
                PlayAnimationSync?.Invoke();
            }
            isPlayAnimation = false;
        }
    }
 
    public const int activityType = (int)OpenServerActivityCenter.ActivityType.AT_Activity2;
    public const int activityID = (int)NewDayActivityID.TimeRushAct;
    public int actNum = 10;
    public static OperationType operaType = OperationType.default47;
    public Redpoint redPoint = new Redpoint(MainRedDot.TimeRushRepoint);
 
    public bool IsOpen => OperationTimeHepler.Instance.SatisfyOpenCondition(operaType);
 
    public bool IsAdvance => OperationTimeHepler.Instance.SatisfyAdvanceCondition(operaType);
 
    public bool priorityOpen => redPoint.state == RedPointState.Simple;
 
    public event Action<int> onStateUpdate;
 
    //类型对应功能ID
    public Dictionary<int, int> funcIdDict = new Dictionary<int, int>();
 
    public bool TryGetFuncIdByRoundType(int roundType, out int funcId)
    {
        return funcIdDict.TryGetValue(roundType, out funcId);
    }
 
    //类型对应窗口ID
    public Dictionary<int, int> windowIDDict = new Dictionary<int, int>();
 
    public bool TryGetWindowIDByRoundType(int roundType, out int windowID)
    {
        return windowIDDict.TryGetValue(roundType, out windowID);
    }
    public Dictionary<int, int> moneyTypeDict = new Dictionary<int, int>();
    public bool TryGetMoneyTypeByRoundType(int roundType, out int windowID)
    {
        return moneyTypeDict.TryGetValue(roundType, out windowID);
    }
    public override void Init()
    {
        OperationTimeHepler.Instance.operationTimeUpdateEvent += OperationTimeUpdateEvent;
        OperationTimeHepler.Instance.operationStartEvent += OperationStartEvent;
        OperationTimeHepler.Instance.operationEndEvent += OperationEndEvent;
        OperationTimeHepler.Instance.operationAdvanceEvent += OperationAdvanceEvent;
        StoreModel.Instance.RefreshBuyShopLimitEvent += RefreshBuyShopLimitEvent;
        //storeModel.RefreshBuyShopLimitEvent += RefreshBuyShopLimitEvent;
        OpenServerActivityCenter.Instance.Register(activityID, this, activityType);
 
        var config = FuncConfigConfig.Get("TimeRush");
        windowIDDict = ConfigParse.ParseIntDict(config.Numerical1);
        funcIdDict = ConfigParse.ParseIntDict(config.Numerical2);
        moneyTypeDict = ConfigParse.ParseIntDict(config.Numerical3);
 
    }
 
    public override void Release()
    {
        OperationTimeHepler.Instance.operationTimeUpdateEvent -= OperationTimeUpdateEvent;
        OperationTimeHepler.Instance.operationStartEvent -= OperationStartEvent;
        OperationTimeHepler.Instance.operationEndEvent -= OperationEndEvent;
        OperationTimeHepler.Instance.operationAdvanceEvent -= OperationAdvanceEvent;
        StoreModel.Instance.RefreshBuyShopLimitEvent -= RefreshBuyShopLimitEvent;
    }
 
    private void OperationTimeUpdateEvent(OperationType type)
    {
        if (type == operaType)
        {
            UpdateRedpoint();
        }
    }
 
    private void RefreshBuyShopLimitEvent()
    {
        UpdateRedpoint();
    }
 
    private void OperationStartEvent(OperationType type, int state)
    {
        if (type == operaType && state == 0)
        {
            if (onStateUpdate != null)
            {
                onStateUpdate(activityID);
            }
            UpdateRedpoint();
        }
    }
    private void OperationEndEvent(OperationType type, int state)
    {
        if (type == operaType)
        {
            if (onStateUpdate != null)
            {
                onStateUpdate(activityID);
            }
 
            if (UIManager.Instance.IsOpened<TimeRushWin>())
            {
                UIManager.Instance.CloseWindow<TimeRushWin>();
            }
            UpdateRedpoint();
        }
    }
    private void OperationAdvanceEvent(OperationType type)
    {
        if (type == operaType)
        {
            if (onStateUpdate != null)
            {
                onStateUpdate(activityID);
            }
            UpdateRedpoint();
        }
    }
 
    public bool TryGetOperationInfo(out OperationCycleHall act)
    {
        act = null;
        return OperationTimeHepler.Instance.TryGetOperation(operaType, out act) && act != null;
    }
 
    public bool TryGetInListIndexById(int id, out int index)
    {
        index = -1;
        var list = GetTabIDList();
        if (list.IsNullOrEmpty() || !list.Contains(id))
            return false;
        index = list.IndexOf(id);
        return index >= 0;
    }
 
    public bool IsFuncOpen()
    {
        if (!FuncOpen.Instance.IsFuncOpen((int)FuncOpenEnum.TimeRush))
            return false;
        if (!IsOpen)
            return false;
        if (!TryGetOperationInfo(out OperationCycleHall act))
            return false;
        List<int> list = GetTabIDList();
        if (list.IsNullOrEmpty())
            return false;
        return true;
    }
 
 
    private int m_NowTabId;
    public event Action OnNowTabIdChangeEvent;
    public int nowTabId
    {
        get { return m_NowTabId; }
        set
        {
            if (m_NowTabId != value)
                m_NowTabId = value;
            OnNowTabIdChangeEvent?.Invoke();
        }
    }
 
    public List<int> GetTabIDList()
    {
        if (!TryGetOperationInfo(out OperationCycleHall act))
            return null;
        var list = ActLunhuidianTypeConfig.GetTabList();
        if (list.IsNullOrEmpty())
            return null;
 
        List<int> resList = new List<int>();
        for (int i = 0; i < list.Count; i++)
        {
            int id = list[i];
            if (!ActLunhuidianTypeConfig.TryGetConfig(id, out var config))
                continue;
            if (!act.TryGetRound(config.RoundType, out var info))
                continue;
 
            //没配功能ID直接加入
            if (!funcIdDict.TryGetValue(config.RoundType, out var funcId))
            {
                if (!resList.Contains(id))
                    resList.Add(id);
                continue;
            }
 
            //配了功能ID,判断功能是否开启
            if (FuncOpen.Instance.IsFuncOpen(funcId))
            {
                if (!resList.Contains(id))
                    resList.Add(id);
            }
        }
        return resList;
    }
    public Dictionary<int, HAA89_tagMCActLunhuidianPlayerInfo> playerInfoDict = new Dictionary<int, HAA89_tagMCActLunhuidianPlayerInfo>();
    public bool TryGetPlayerInfo(int roundType, out HAA89_tagMCActLunhuidianPlayerInfo playerInfo)
    {
        return playerInfoDict.TryGetValue(roundType, out playerInfo) && playerInfo != null;
    }
 
    public event Action OnUpdatePlayerInfoEvent;
    public void UpdatePlayerInfo(HAA89_tagMCActLunhuidianPlayerInfo netPack)
    {
        if (actNum != netPack.ActNum)
            return;
        if (!playerInfoDict.ContainsKey(netPack.RoundType))
            playerInfoDict[netPack.RoundType] = new HAA89_tagMCActLunhuidianPlayerInfo();
        playerInfoDict[netPack.RoundType].RoundType = netPack.RoundType;
        playerInfoDict[netPack.RoundType].CurRound = netPack.CurRound;
        playerInfoDict[netPack.RoundType].CurValue = netPack.CurValue;
        playerInfoDict[netPack.RoundType].AwardRecord = netPack.AwardRecord;
        CheckNewAwardHave(netPack.RoundType, (int)netPack.CurRound, (int)netPack.CurValue);
        OnUpdatePlayerInfoEvent?.Invoke();
        UpdateRedpoint();
    }
 
    public int newRoundType;
    public int newAwardType;
    public int newAwardTypeValue;
    public int newAwardIndex;
    // <轮回类型, 上一次的CurRound>
    public Dictionary<int, int> lastCurRoundDict = new Dictionary<int, int>();
 
    // <轮回类型, 上一次的CurValue>
    public Dictionary<int, int> lastCurValueDict = new Dictionary<int, int>();
 
    public event Action OnNewAwardHaveEvent;
    public void CheckNewAwardHave(int roundType, int curRound, int curValue)
    {
        if (lastCurRoundDict.ContainsKey(roundType) && lastCurValueDict.ContainsKey(roundType))
        {
            int lastCurRound = lastCurRoundDict[roundType];
            int lastCurValue = lastCurValueDict[roundType];
            if (lastCurRound < curRound)
            {
                lastCurValueDict[roundType] = 0;
            }
 
            lastCurValue = lastCurValueDict[roundType];
            if (lastCurValue < curValue)
            {
                if (!TryGetOperationInfo(out var act))
                    return;
                if (act.TryGetRound(roundType, out var round) && round.AwardList != null)
                {
                    for (int i = 0; i < round.AwardList.Length; i++)
                    {
                        var Award = round.AwardList[i];
                        int state = GetAwardState(roundType, Award.AwardIndex);
                        if (Award.NeedValue > lastCurValue && Award.NeedValue <= curValue && state == 1)
                        {
                            newRoundType = roundType;
                            newAwardType = round.AwardType;
                            newAwardTypeValue = (int)round.AwardTypeValue;
                            newAwardIndex = Award.AwardIndex;
                            if (!UIManager.Instance.IsOpened<TimeRushTipWin>())
                            {
                                UIManager.Instance.OpenWindow<TimeRushTipWin>();
                            }
                            OnNewAwardHaveEvent?.Invoke();
                        }
                    }
                }
            }
        }
        lastCurRoundDict[roundType] = curRound;
        lastCurValueDict[roundType] = curValue;
    }
 
 
    public int GetAwardState(int roundType, int awardIndex)
    {
        if (!TryGetPlayerInfo(roundType, out var playerInfo))
            return 0;
        if (!TryGetOperationInfo(out var act))
            return 0;
        if (!act.TryGetRoundInfoByIndex(roundType, awardIndex, out var awardInfo, out int listIndex))
            return 0;
 
        if (playerInfo.CurValue >= awardInfo.NeedValue)
        {
            return (playerInfo.AwardRecord & (1 << awardIndex)) != 0 ? 2 : 1;
        }
        else
        {
            return 0;
        }
    }
 
    public void HaveAllMissionAward(int roundType)
    {
        if (!TryGetOperationInfo(out var act))
            return;
        if (!act.TryGetRound(roundType, out var round) || round.AwardList == null)
            return;
 
        for (int i = 0; i < round.AwardList.Length; i++)
        {
            var award = round.AwardList[i];
            int state = GetAwardState(roundType, award.AwardIndex);
            if (state == 1)
            {
                SendGetAward(roundType, (int)award.NeedValue);
            }
        }
    }
 
    public bool IsCanBuyCTG(int ctgID)
    {
        if (!RechargeManager.Instance.TryGetRechargeCount(ctgID, out var rechargeCount))
            return false;
        if (!CTGConfig.HasKey(ctgID))
            return false;
        CTGConfig config = CTGConfig.Get(ctgID);
        return rechargeCount.totalCount < config.TotalBuyCount;
    }
 
    public bool IsFree(int shopID)
    {
        if (!StoreConfig.HasKey(shopID))
            return false;
        StoreConfig config = StoreConfig.Get(shopID);
        return config.MoneyNum == 0;
    }
 
    public bool IsCanBuyShop(int shopID)
    {
        if (!StoreConfig.HasKey(shopID))
            return false;
        StoreConfig config = StoreConfig.Get(shopID);
 
        int remainNum;
        StoreModel.Instance.TryGetIsSellOut(config, out remainNum);
 
        return remainNum > 0;
    }
 
    public List<TimeRushGiftItem> GetGiftItemList(int roundType, bool isSort = false)
    {
        List<TimeRushGiftItem> res = new List<TimeRushGiftItem>();
        if (TryGetOperationInfo(out var act) && act.TryGetRound(roundType, out var round) && round.CTGIDList != null)
        {
            var list = StoreModel.Instance.storeTypeDict[round.ShopType];
            if (!list.IsNullOrEmpty())
            {
                for (int i = 0; i < list.Count; i++)
                {
                    var item = list[i];
                    if (item.storeConfig == null)
                        continue;
                    res.Add(new TimeRushGiftItem
                    {
                        type = 0,
                        id = item.storeConfig.ID,
                    });
                }
            }
 
            for (int i = 0; i < round.CTGIDList.Length; i++)
            {
                var item = round.CTGIDList[i];
                res.Add(new TimeRushGiftItem
                {
                    type = 1,
                    id = item,
                });
            }
 
            if (isSort)
            {
                res = res.OrderBy(item =>
                {
                    bool isCanBuy = item.type == 0 ? IsCanBuyShop(item.id) : IsCanBuyCTG(item.id);
                    return !isCanBuy;
                })
                .ThenBy(item => item.type)
                .ThenBy(item => item.id)
                .ToList();
            }
 
        }
        return res;
    }
 
    public List<HAA88_tagMCActLunhuidianInfo.tagMCActLunhuidianAward> GetTaskList(int roundType, bool isSort = false)
    {
        if (!TryGetOperationInfo(out var act) || !act.TryGetRound(roundType, out var round) || round.AwardList == null)
            return null;
 
        List<HAA88_tagMCActLunhuidianInfo.tagMCActLunhuidianAward> res = round.AwardList.ToList();
 
        if (isSort)
        {
            // 先获取状态并缓存,避免排序时重复调用 GetAwardState
            res = res.Select(award => new
            {
                Data = award,
                State = GetAwardState(roundType, award.AwardIndex)
            }).OrderBy(x =>
            {
                // 定义优先级映射:数字越小,排得越靠前
                if (x.State == 1) return 0; // 最前:可领取
                if (x.State == 0) return 1; // 中间:未达成
                return 2;                   // 最后:已领取 (state == 2)
            })
            .ThenBy(x => x.Data.AwardIndex) // 状态相同时,AwardIndex 小的在前
            .Select(x => x.Data)            // 提取回原始数据结构
           .ToList();
        }
        return res;
    }
 
 
    public bool IsTabShowRed(int id)
    {
        if (!ActLunhuidianTypeConfig.TryGetConfig(id, out var config))
            return false;
        int roundType = config.RoundType;
        int tabType = config.TabType;
        if (tabType == 1)
        {
            var taskList = GetTaskList(roundType);
            if (taskList.IsNullOrEmpty())
                return false;
            for (int i = 0; i < taskList.Count; i++)
            {
                var task = taskList[i];
                int state = GetAwardState(roundType, task.AwardIndex);
                if (state == 1)
                    return true;
            }
            return false;
        }
 
        var list = GetGiftItemList(roundType);
        if (list.IsNullOrEmpty())
            return false;
        for (int i = 0; i < list.Count; i++)
        {
            var item = list[i];
            if (item.type == 0)
            {
                bool isFree = IsFree(item.id);
                bool isCanBuy = IsCanBuyShop(item.id);
                if (isFree && isCanBuy)
                    return true;
 
            }
        }
        return false;
    }
 
    public void SendGetAward(int roundType, int needValue)
    {
        CA504_tagCMPlayerGetReward getReward = new CA504_tagCMPlayerGetReward();
        getReward.RewardType = 78;
        getReward.DataEx = (uint)actNum;
        getReward.DataExStr = StringUtility.Concat(roundType.ToString(), "|", needValue.ToString());
        getReward.DataExStrLen = (byte)getReward.DataExStr.Length;
        GameNetSystem.Instance.SendInfo(getReward);
    }
 
    public void UpdateRedpoint()
    {
        redPoint.state = RedPointState.None;
        if (!IsFuncOpen())
            return;
        if (!TryGetOperationInfo(out var act) || act.roundInfoDict == null)
            return;
        foreach (var roundType in act.roundInfoDict.Keys)
        {
            ActLunhuidianTypeConfig config;
 
            if (ActLunhuidianTypeConfig.TryGetConfig(roundType, 1, out config))
            {
                bool isShow = IsTabShowRed(config.ID);
                if (isShow)
                {
                    redPoint.state = RedPointState.Simple;
                    return;
                }
            }
 
            if (ActLunhuidianTypeConfig.TryGetConfig(roundType, 2, out config))
            {
                bool isShow = IsTabShowRed(config.ID);
                if (isShow)
                {
                    redPoint.state = RedPointState.Simple;
                    return;
                }
            }
        }
 
 
    }
}