hch
2025-07-30 bae41593e19d32046f77ed1f036089e015380b99
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
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
 
 
 
public class TeamManager : GameSystemManager<TeamManager>
{
    protected Dictionary<TeamType, TeamBase> teamDict = new Dictionary<TeamType, TeamBase>();
 
    public override void Init()
    {
        base.Init();
        
        HeroManager.Instance.onHeroChangeEvent += onHeroChangeEvent;
        HeroManager.Instance.onHeroDeleteEvent += onHeroDeleteEvent;
        DTC0102_tagCDBPlayer.beforePlayerDataInitializeEvent += OnBeforePlayerDataInitialize;
    }
 
    public override void Release()
    {
        base.Release();
        HeroManager.Instance.onHeroChangeEvent += onHeroChangeEvent;
        HeroManager.Instance.onHeroDeleteEvent += onHeroDeleteEvent;
        DTC0102_tagCDBPlayer.beforePlayerDataInitializeEvent += OnBeforePlayerDataInitialize;
    }
 
    protected void OnBeforePlayerDataInitialize()
    {
        teamDict.Clear();
    }
 
    protected void onHeroChangeEvent(HeroInfo heroInfo)
    {
        //  英雄当前所有在的队伍
        List<int> heroTeams = heroInfo.itemHero.GetUseData(81);
 
        //  当前英雄所在的队伍信息 <队伍类型, <队形, 位置>>
        Dictionary<TeamType, KeyValuePair<int, int>> teamTypeShapeTypePositionDict = new Dictionary<TeamType, KeyValuePair<int, int>>();
 
 
        //  处理当前记录在英雄信息里的队伍信息
        if (null != heroTeams)
        {
            foreach (var teamMsg in heroTeams)
            {
                // 所在阵容信息列表 [阵容类型*10000+阵型类型*100+位置编号, ...] 
                int teamType = teamMsg / 10000;
                int shapeType = (teamMsg % 10000) / 100;
                int positionIndex = teamMsg % 100 - 1;    //布阵位置:服务端为 1  客户端为0
 
                if (teamTypeShapeTypePositionDict.ContainsKey((TeamType)teamType))
                {
                    //  队伍类型相同,更新阵型和位置
                    Debug.LogError("当前英雄拥有两个相同的队伍信息: " + teamType + " " + shapeType + " " + positionIndex + ", hero guid is " + heroInfo.itemHero.guid);
                }
                else
                {
                    //  队伍类型不同,添加新的
                    KeyValuePair<int, int> shapeTypePosition = new KeyValuePair<int, int>(shapeType, positionIndex);
                    teamTypeShapeTypePositionDict.Add((TeamType)teamType, shapeTypePosition);
                }
            }
        }
 
 
        //  遍历当前所有队伍 判断当前队伍里是否有该英雄
        //  如果有的话 根据英雄里的信息当前是否该英雄还在队伍里 是否发生变化
        //      =>1.阵型发生变化 2.位置发生变化
        //  如果没有的话 就说明该英雄被移出队伍了
        foreach (var team in teamDict.Values)
        {
            //  检查一下当前队伍是否有该英雄
            //  如果有的话 读取一下当前是否该英雄还在队伍里 位置是否发生变化
            TeamHero teamHero = team.GetHero(heroInfo.itemHero.guid);
            if (teamHero != null)
            {
                if ((teamTypeShapeTypePositionDict.ContainsKey(team.teamType)))
                {
                    KeyValuePair<int, int> shapeTypePosition = teamTypeShapeTypePositionDict[team.teamType];
                    //  更新队伍信息 
                    // 可以判断teamHero的positionNum是否跟shapeTypePosition.Value一致 判断是否变位置了
                    // 可以判断teamHero的ServerShapeType是否跟shapeTypePosition.Key一致 判断是否变阵型了
                    team.RefreshServerData(shapeTypePosition.Key, shapeTypePosition.Value, heroInfo);
                }
                else
                {
                    //  队伍里有这个英雄,但是在队伍信息里没有了 置空 (被移出队伍)
                    team.RemoveHero(teamHero.positionNum);
                }
            }
            //    原来队伍里没这个英雄
            else
            {
                //  如果当前队伍类型在英雄的所在阵容信息列表里有的话
                //    就说明队伍里新增了这个英雄 (新增进队伍)
                if (teamTypeShapeTypePositionDict.ContainsKey(team.teamType))
                {
                    KeyValuePair<int, int> shapeTypePosition = teamTypeShapeTypePositionDict[team.teamType];
                    team.RefreshServerData(shapeTypePosition.Key, shapeTypePosition.Value, heroInfo);
                }
            }
        }
 
 
        //  遍历英雄所在的队伍信息列表 新增一下当前储存的队伍里没有的队伍
        foreach (var teamTypeShapeTypePosition in teamTypeShapeTypePositionDict)
        {
            //  如果当前队伍类型在队伍字典里没有的话
            if (!teamDict.ContainsKey(teamTypeShapeTypePosition.Key))
            {
                //  新建一个队伍
                TeamBase team = new TeamBase(teamTypeShapeTypePosition.Key);
                team.RefreshServerData(teamTypeShapeTypePosition.Value.Key, teamTypeShapeTypePosition.Value.Value, heroInfo);
                teamDict.Add(teamTypeShapeTypePosition.Key, team);
            }
        }
 
    } 
 
 
    protected void onHeroDeleteEvent(HeroInfo heroInfo)
    {
        List<int> heroTeams = heroInfo.itemHero.GetUseData(81);
 
        foreach (int teamMsg in heroTeams)
        {
            // 所在阵容信息列表 [阵容类型*10000+阵型类型*100+位置编号, ...] 
            int teamType = teamMsg / 10000;
            int shapeType = (teamMsg % 10000) / 100;
            int positionIndex = teamMsg % 100 - 1; //布阵位置:服务端为 1  客户端为0
 
            TeamBase team = GetTeam((TeamType)teamType);
 
            if (team != null)
            {
                team.RefreshServerData(shapeType, positionIndex, null);
            }
        }
    }
 
    public bool HasTeam(TeamType teamType)
    {
        return teamDict.ContainsKey(teamType);
    }
 
    public TeamBase GetTeam(TeamType teamType)
    {
        TeamBase team = null;
 
        if (!teamDict.TryGetValue(teamType, out team))
        {
            team = new TeamBase(teamType);
            // team.CreateDefault(HeroManager.Instance.GetPowerfulHeroList());
            teamDict.Add(teamType, team);
        }
 
        return team;
    }
}