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, // 装备
|
}
|
}
|