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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Linq;
 
 
public partial class AchievementManager : GameSystemManager<AchievementManager>
{
    //成就类型:条件(可以是空):进度
    Dictionary<int, Dictionary<string, int>> achivementDict = new Dictionary<int, Dictionary<string, int>>();
    public event Action<int> OnAchievementUpdateEvent;
    //key:第几个记录值 每个key存31个succid   0-30为0, 31-61为1..; value: 根据成就ID位判断是否已领取
    Dictionary<int, int> achivementAwardDict = new Dictionary<int, int>();
    public override void Init()
    {
        DTC0102_tagCDBPlayer.beforePlayerDataInitializeEvent += OnBeforePlayerDataInitialize;
    }
 
    public override void Release()
    {
        DTC0102_tagCDBPlayer.beforePlayerDataInitializeEvent -= OnBeforePlayerDataInitialize;
    }
 
    void OnBeforePlayerDataInitialize()
    {
    }
 
    public void UpdateAchievement(HA340_tagSCSuccessInfoList netPack)
    {
        List<int> types = new List<int>();
        for (int i = 0; i < netPack.Count; i++)
        {
            var info = netPack.SuccessInfoList[i];
            if (!achivementDict.ContainsKey(info.SuccType))
            {
                achivementDict[info.SuccType] = new Dictionary<string, int>();
            }
 
            string key = info.CLen > 0 ? info.Conds.ToString() : "";
            achivementDict[info.SuccType][key] = (int)info.CurValue;
 
            OnAchievementUpdateEvent?.Invoke(info.SuccType);
            if (!types.Contains(info.SuccType))
            {
                types.Add(info.SuccType);
            }
        }
        UpdateRedpoint(types);
    }
 
    public int GetAchievementProgress(int type)
    {
        if (achivementDict.ContainsKey(type))
        {
            var condKey = SuccessConfig.GetConditionString(type);
            if (achivementDict[type].ContainsKey(condKey))
            {
                return achivementDict[type][condKey];
            }
        }
        return 0;
 
    }
 
    public void UpdateAchievementAward(HA342_tagSCSuccessAwardRecordList netPack)
    {
        List<int> types = new List<int>();
        for (int i = 0; i < netPack.RecordCnt; i++)
        {
            var record = netPack.RecordList[i];
            achivementAwardDict[record.RecordIndex] = (int)record.Record;
            var startID = record.RecordIndex * 31;
            var endID = startID + 30;
            for (int j = startID; j <= endID; j++)
            {
                if (SuccessConfig.HasKey(j))
                {
                    var type = SuccessConfig.Get(j).Type;
                    if (!types.Contains(type))
                    {
                        types.Add(type);
                    }
                }
            }
        }
        OnAchievementUpdateEvent?.Invoke(-1);
        UpdateRedpoint(types);
    }
 
    //成就是否已领取 
    //每个key存31个succid   0-30为0, 31-61为1..; 根据成就ID位判断是否已领取
    public bool IsAchievementAwarded(int id)
    {
        var index = id / 31;
        var bit = id % 31;
        if (achivementAwardDict.ContainsKey(index))
        {
            return (achivementAwardDict[index] & (1 << bit)) != 0;
        }
        return false;
    }
 
    //获取成就状态  0: 未领取 1: 未达成 2: 已领取
    public int GetAchievementState(int id)
    {
        var process = GetAchievementProgress(id);
        var config = SuccessConfig.Get(id);
        if (process < config.NeedCnt)
        {
            return 1;
        }
        if (IsAchievementAwarded(id))
        {
            return 2;
        }
        return 0;
    }
 
 
    //主线章节
    Redpoint mainLevelRedpoint = new Redpoint(MainRedDot.RedPoint_DailyKey, MainRedDot.RedPoint_MainMissionKey);
    //类型:红点id
    Dictionary<int, int> redpointDict = new Dictionary<int, int>()
    {
        {1, MainRedDot.RedPoint_MainMissionKey}
    };
 
    void UpdateRedpoint(List<int> _types)
    {
        if (_types.IsNullOrEmpty())
        {
            _types = redpointDict.Keys.ToList();
        }
 
        foreach (var type in _types)
        {
            var redpoint = RedpointCenter.Instance.GetRedpoint(redpointDict[type]);
            redpoint.state = RedPointState.None;
            //根据ID判断是否有可领取的
            var allAchivement = SuccessConfig.GetTypeToIDDict(type);
            var process = GetAchievementProgress(type);
            foreach (var id in allAchivement)
            {
                var config = SuccessConfig.Get(id);
                if (config.NeedCnt <= process)
                {
                    if (!IsAchievementAwarded(id))
                    {
                        redpoint.state = RedPointState.Simple;
                        break;
                    }
                }
            }
 
        }
    }
 
    //根据类型获取所有成就ID, 且是排序后的 未领取>未达成>已领取
    public List<int> GetAchievementIDs(int type)
    {
        var ids = SuccessConfig.GetTypeToIDDict(type);
        ids.Sort(CmpIds);
        return ids;
    }
 
    int CmpIds(int a, int b)
    {
        var stateA = GetAchievementState(a);
        var stateB = GetAchievementState(b);
 
        if (stateA != stateB)
        {
            return stateA - stateB;
        }
 
        return a - b;
    }
 
    public void SendGetAward(int id)
    {
        var config = SuccessConfig.Get(id);
        //简单判断背包
        if (PackManager.Instance.GetEmptyGridCount(PackType.Item) < config.AwardItemList.Length)
        {
            SysNotifyMgr.Instance.ShowTip("GeRen_lhs_202580");
            return;
        }
        
        var pack = new CA504_tagCMPlayerGetReward();
        pack.RewardType = 59;
        pack.DataEx = (uint)id;
        GameNetSystem.Instance.SendInfo(pack);
    }
}