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
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Linq;
 
public class HeroManager : GameSystemManager<HeroManager>
{
    //武将和图鉴的关系
    //同一个武将图鉴,可能有多个武将(物品)数据,或者没有武将,但是有图鉴信息
    //初始创建(0725),后续跟随背包事件增加删除  key = guid
    protected Dictionary<string, HeroInfo> heroInfoDict = new Dictionary<string, HeroInfo>();
 
    public Action<HeroInfo> onHeroChangeEvent;
 
    public Action<int> onHeroDeleteEvent;
 
    public override void Init()
    {
        base.Init();
 
        PackManager.Instance.ChangeItemEvent += ChangeHero;
        PackManager.Instance.DeleteItemEvent += DeleteHero;
        DTC0102_tagCDBPlayer.beforePlayerDataInitializeEventOnRelogin += OnBeforePlayerDataInitialize;
    }
 
    public override void Release()
    {
        base.Release();
        PackManager.Instance.ChangeItemEvent -= ChangeHero;
        PackManager.Instance.DeleteItemEvent -= DeleteHero;
        DTC0102_tagCDBPlayer.beforePlayerDataInitializeEventOnRelogin -= OnBeforePlayerDataInitialize;
    }
 
    void OnBeforePlayerDataInitialize()
    {
 
        heroInfoDict.Clear();
    }
 
 
    // isCreate bool:true代表创建 false 刷新触发
    void ChangeHero(PackType packType, string guid, bool isCreate)
    {
        if (packType == PackType.Hero)
        {
            HeroInfo heroInfo = null;
            if (!heroInfoDict.TryGetValue(guid, out heroInfo))
            {
                heroInfo = new HeroInfo(PackManager.Instance.GetItemByGuid(guid));
                heroInfoDict.Add(guid, heroInfo);
            }
            else
            {
                heroInfo.UpdateHero(PackManager.Instance.GetItemByGuid(guid));
            }
 
            onHeroChangeEvent?.Invoke(heroInfo);
        }
    }
 
    void DeleteHero(PackType packType, string guid, int itemID, int index, int clearType)
    {
        if (packType == PackType.Hero)
        {
            HeroInfo heroInfo = null;
            heroInfoDict.TryGetValue(guid, out heroInfo);
 
            heroInfoDict.Remove(guid);
 
            onHeroDeleteEvent?.Invoke(itemID);
        }
    }
 
    public HeroInfo GetHero(string guid)
    {
        if (!heroInfoDict.ContainsKey(guid))
            return null;
        return heroInfoDict[guid];
    }
 
    public List<HeroInfo> GetHeroList()
    {
        return heroInfoDict.Values.ToList();
    }
 
    public List<string> GetHeroGuidList()
    {
        return heroInfoDict.Keys.ToList();
    }
 
    /// 参数: 职业,国家,伤害类型,6大战斗属性,特殊属性
    public List<string> GetHeroGuidList(List<int> selectList = null)
    {
        if (selectList.IsNullOrEmpty())
            return heroInfoDict.Keys.ToList();
 
        int job = selectList[0];
        int country = selectList[1];
        int hurtType = selectList[2];
        int fightAttrType = selectList[3];
        int specialAttrType = selectList[4];
 
        List<string> retGuidList = new List<string>();
        foreach (string guid in heroInfoDict.Keys)
        {
            HeroInfo heroInfo = heroInfoDict[guid];
 
            //0代表全部, 同级别是可复选,不同级别为且的关系
            bool isMatch = true;
            if (job != 0)
            {
                isMatch = isMatch && (job & (1 << heroInfo.heroConfig.Class)) > 0;
            }
            if (country != 0)
            {
                isMatch = isMatch && (country & (1 << heroInfo.heroConfig.Country)) > 0;
            }
            if (hurtType != 0)
            {
                isMatch = isMatch && (hurtType & (1 << heroInfo.heroConfig.HurtType)) > 0;
            }
            if (fightAttrType != 0)
            {
                isMatch = isMatch && (fightAttrType & (1 << heroInfo.heroConfig.Specialty)) > 0;
            }
            if (specialAttrType != 0)
            {
                bool isMatch2 = false;
                for (int i = 0; i < heroInfo.heroConfig.Specialty2.Length; i++)
                {
                    isMatch2 = (specialAttrType & (1 << heroInfo.heroConfig.Specialty2[i])) > 0;
                    if (isMatch2)
                        break;
                }
                isMatch = isMatch && isMatch2;
            }
            if (isMatch)
            {
                retGuidList.Add(guid);
            }
 
        }
        return retGuidList;
    }
 
    public List<HeroInfo> GetPowerfulHeroList()
    {
        List<HeroInfo> heroList = new List<HeroInfo>(heroInfoDict.Values);
 
        heroList.Sort((a, b) =>
        {
            long power1 = a.CalculateFightPower(false);
            long power2 = b.CalculateFightPower(false);
 
            if (power1 == power2)
            {
                return 0;
            }
 
            return power1 > power2 ? -1 : 1;
        });
 
        List<HeroInfo> retList = new List<HeroInfo>();
 
        for (int i = 0; i < TeamConst.MaxTeamHeroCount && i < heroList.Count; i++)
        {
            retList.Add(heroList[i]);
        }
 
        return retList;
    }
 
 
    public int GetHeroCount()
    {
        return heroInfoDict.Count;
    }
 
    public bool HasHero(int heroID)
    {
        return PackManager.Instance.GetSinglePack(PackType.Hero).HasItem(heroID);
    }
 
    public int GetHeroCountByID(int heroID)
    {
        return (int)PackManager.Instance.GetSinglePack(PackType.Hero).GetCountById(heroID);
    }
 
    //获得生效的武将数量
    public int GetAttrActiveHeroCount()
    {
        int count = 0;
        foreach (HeroInfo heroInfo in heroInfoDict.Values)
        {
            if (heroInfo.isAttrActive)
            {
                count++;
            }
        }
        return count;
    }
 
    //获得未生效的武将数量
    public int GetNotActiveHeroCount()
    {
        int count = 0;
        foreach (HeroInfo heroInfo in heroInfoDict.Values)
        {
            if (!heroInfo.isAttrActive)
            {
                count++;
            }
        }
        return count;
    }
 
    //获得指定ID且属性生效的武将
    public HeroInfo GetHeroByID(int heroID)
    {
        foreach (HeroInfo heroInfo in heroInfoDict.Values)
        {
            if (heroInfo.heroId == heroID && heroInfo.isAttrActive)
            {
                return heroInfo;
            }
        }
        return null;
    }
 
}