hch
2026-01-26 aa84cb62bebb9c8a4e586bcc1ec28eb7a16a8860
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;
using System.Collections.Generic;
using LitJson;
using UnityEngine;
 
public class EquipRecordManager : GameSystemManager<EquipRecordManager>
{
    private List<EquipRecordData> recordList = new List<EquipRecordData>();
    public int maxCnt;
    public override void Init()
    {
        DTC0102_tagCDBPlayer.beforePlayerDataInitializeEventOnRelogin += OnBeforePlayerDataInitializeEventOnRelogin;
        DTC0403_tagPlayerLoginLoadOK.playerLoginOkEvent += OnPlayerLoginOk;
 
        var config = FuncConfigConfig.Get("AutoGuaji1");
        maxCnt = int.Parse(config.Numerical5);
    }
 
    public override void Release()
    {
        DTC0102_tagCDBPlayer.beforePlayerDataInitializeEventOnRelogin -= OnBeforePlayerDataInitializeEventOnRelogin;
        DTC0403_tagPlayerLoginLoadOK.playerLoginOkEvent -= OnPlayerLoginOk;
    }
 
 
 
    private void OnBeforePlayerDataInitializeEventOnRelogin()
    {
    }
 
    private void OnPlayerLoginOk()
    {
        LoadRecords();
    }
 
    string Key { get { return $"EquipRecordData_{PlayerDatas.Instance.PlayerId}"; } }
    private void SaveRecords()
    {
        // 持久化存储
        var json = JsonMapper.ToJson(recordList);
        LocalSave.SetString(Key, json);
    }
 
    private void LoadRecords()
    {
        var json = LocalSave.GetString(Key);
        if (string.IsNullOrEmpty(json))
        {
            recordList = new List<EquipRecordData>();
            return;
        }
 
        try
        {
            recordList = JsonMapper.ToObject<List<EquipRecordData>>(json);
            // 排序规则:时间戳小的靠前,时间戳相同则索引小的靠前
            recordList.Sort((a, b) =>
            {
                int timeCompare = a.timestamp.CompareTo(b.timestamp);
                if (timeCompare != 0)
                    return timeCompare;
                return 0;
            });
        }
        catch (Exception ex)
        {
            Debug.LogError($"加载装备记录失败: {ex.Message}");
            recordList = new List<EquipRecordData>();
        }
    }
 
    /// <summary>
    /// 获取装备详情
    /// </summary>
    private EquipDetail GetEquipDetail(ItemModel item)
    {
        if (item == null) return null;
 
        var detail = new EquipDetail
        {
            itemId = item.itemId,
            lv = item.GetUseDataFirstValue(22),
            guid = item.guid,
        };
        return detail;
    }
 
    public List<EquipRecordData> GetRecordList(bool isSort = false)
    {
        if (recordList == null)
        {
            return null;
        }
        if (isSort)
        {
            // 排序规则:时间戳小的靠前,时间戳相同则索引小的靠前
            recordList.Sort((a, b) =>
            {
                int timeCompare = a.timestamp.CompareTo(b.timestamp);
                if (timeCompare != 0)
                    return timeCompare;
                return 0;
            });
        }
        return recordList;
    }
 
    public event Action OnUpdateRecordListEvent;
 
    public void AddRecord(int recordType, ItemModel newEquip, ItemModel oldEquip)
    {
        if (newEquip == null)
            return;
        long showFightPower = FightPowerManager.Instance.GetFightPowerChange(newEquip);
        // 新装备战力低于旧装备战力,不允许添加
        if (showFightPower <= 0)
            return;
 
        var newDetail = GetEquipDetail(newEquip);
        var oldDetail = GetEquipDetail(oldEquip);
 
        if (recordList == null)
            return;
 
        // 遍历现有记录,检查是否存在匹配的记录(不限制顺序)
        foreach (var existingRecord in recordList)
        {
            if (IsSameRecord(existingRecord, newDetail, oldDetail))
            {
                // 找到匹配的记录,不添加
                return;
            }
        }
 
        var record = new EquipRecordData
        {
            recordType = (int)recordType,
            newEquip = GetEquipDetail(newEquip),
            oldEquip = GetEquipDetail(oldEquip),
            fightPower = showFightPower,
            timestamp = TimeUtility.AllSeconds,
        };
 
        if (recordList.Count >= maxCnt)
        {
            // 移除最早加入的记录(排序后第一个)
            recordList.RemoveAt(0);
        }
 
        recordList.Add(record);
        SaveRecords();
        OnUpdateRecordListEvent?.Invoke();
    }
 
    /// <summary>
    /// 判断两条记录是否一致(不限制 new/old 顺序)
    /// </summary>
    private bool IsSameRecord(EquipRecordData existingRecord, EquipDetail newDetail, EquipDetail oldDetail)
    {
        // 参数校验
        if (newDetail == null && oldDetail == null)
            return true;
        if (existingRecord == null)
            return true;
 
        // 计算各字段是否为 null
        bool newIsNull = IsNullOrEmptyGuid(newDetail);
        bool oldIsNull = IsNullOrEmptyGuid(oldDetail);
        bool recordNewIsNull = IsNullOrEmptyGuid(existingRecord.newEquip);
        bool recordOldIsNull = IsNullOrEmptyGuid(existingRecord.oldEquip);
 
        // 如果两边都为空,则视为相同
        if (newIsNull && oldIsNull && recordNewIsNull && recordOldIsNull)
            return true;
 
        // 如果只有一边为空,则不同
        if (newIsNull != recordNewIsNull || oldIsNull != recordOldIsNull)
            return false;
 
        // 检查顺序是否匹配
        bool orderMatch = IsGuidMatch(existingRecord.newEquip, newDetail) &&
                          IsGuidMatch(existingRecord.oldEquip, oldDetail);
 
        // 检查顺序是否相反
        bool reverseMatch = IsGuidMatch(existingRecord.newEquip, oldDetail) &&
                            IsGuidMatch(existingRecord.oldEquip, newDetail);
 
        return orderMatch || reverseMatch;
    }
 
    // 判断装备详情是否为 null 或 guid 为空
    private bool IsNullOrEmptyGuid(EquipDetail detail)
    {
        return detail == null || string.IsNullOrEmpty(detail.guid);
    }
 
    // 比较两个装备详情的 guid 是否相等(安全比较,避免 null 异常)
    private bool IsGuidMatch(EquipDetail a, EquipDetail b)
    {
        // 任一对象为 null 则不匹配
        if (a == null || b == null)
            return false;
 
        return a.guid == b.guid;
    }
 
    // 装备记录数据类型
    public class EquipRecordData
    {
        public int recordType;        // 记录类型
        public EquipDetail newEquip;         // 新装备详情
        public EquipDetail oldEquip;         // 旧装备详情
        public long fightPower;                 // 战力差
        public int timestamp;               // 时间戳
    }
 
    public class EquipDetail
    {
        public string guid;                     // 装备唯一ID
        public int itemId;                      // 物品ID
        public int lv;                          // 等级
    }
 
 
    public enum EquipOPType
    {
        Decompose = 1,     // 分解
        Equip = 2,        // 装备
    }
}