| | |
| | | using System.Collections.Generic; |
| | | using System.Linq; |
| | | using UnityEngine; |
| | | using LitJson; |
| | | |
| | | |
| | | public class MailManager : GameSystemManager<MailManager> |
| | | { |
| | |
| | | public Redpoint tabRedpoint1; |
| | | public event Action OnUpdateMailListEvent;// 更新邮件列表数据 |
| | | public event Action OnUpdateMailStateChangeEvent;// 更新邮件状态变更 |
| | | public readonly string dateFormat = "yyyy-MM-dd"; |
| | | public readonly string dateFormat = "yyyy.MM.dd"; |
| | | public string nowUuid = string.Empty; |
| | | public int personalMailMaxLimitCount; |
| | | public override void Init() |
| | | { |
| | | tabRedpoint0 = new Redpoint(MainRedDot.MailRepoint, GetTabRedpointId(MailCategory.Personal)); |
| | | tabRedpoint1 = new Redpoint(MainRedDot.MailRepoint, GetTabRedpointId(MailCategory.Global)); |
| | | DTC0102_tagCDBPlayer.beforePlayerDataInitializeEvent += OnBeforePlayerDataInitializeEvent; |
| | | personalMailMaxLimitCount = int.Parse(FuncConfigConfig.Get("PersonalMail").Numerical1); |
| | | } |
| | | |
| | | public override void Release() |
| | |
| | | } |
| | | } |
| | | |
| | | //返回有效期天数(正数=已过期天数,负数=剩余天数) |
| | | public int GetMailExpiryDays(DateTime createDateTime, int limitDays) |
| | | //返回有效期天数(正数=剩余天数,负数=已过期天数) |
| | | public float GetMailExpiryDays(DateTime createDateTime, int limitDays) |
| | | { |
| | | DateTime expiryDate = createDateTime.AddDays(limitDays); |
| | | return (int)(TimeUtility.ServerNow - expiryDate).TotalDays; |
| | | TimeSpan remainingTime = expiryDate - TimeUtility.ServerNow; |
| | | return (float)remainingTime.TotalDays; |
| | | } |
| | | |
| | | public string FormatMailExpiryDays(DateTime createDateTime, int limitDays) |
| | | { |
| | | string result = string.Empty; |
| | | float days = GetMailExpiryDays(createDateTime, limitDays); |
| | | // 防护:处理因客户端与服务器时间戳的微小差异,导致剩余时间略大于有效期上限的情况。 |
| | | // 将计算出的剩余天数限制在邮件的有效期天数之内。 |
| | | if (days > limitDays) |
| | | { |
| | | days = limitDays; |
| | | } |
| | | |
| | | if (days > 1.0f) |
| | | { |
| | | // 大于1天:按向上取整的天数显示。 |
| | | // MathF.Ceiling(1.01f) -> 2.0f |
| | | int daysRounded = (int)MathF.Ceiling(days); |
| | | result = Language.Get("Mail07", (int)MathF.Ceiling(days)); |
| | | } |
| | | else if (days > 0.0f) |
| | | { |
| | | // 小于1天(但未过期):按精确的小时/分钟显示。 |
| | | TimeSpan t = TimeSpan.FromDays(days); |
| | | result = Language.Get("Mail12",StringUtility.Contact( " ", Language.Get("Mail13", t.Hours, t.Minutes))); |
| | | } |
| | | else |
| | | { |
| | | // (预留)已过期:按负数向下取整(向更小的负数取整,以获得已过期天数的整数绝对值) |
| | | // MathF.Floor(-1.01f) -> -2.0f |
| | | // MathF.Floor(-0.5f) -> -1.0f |
| | | // float expiredDaysFloor = MathF.Floor(days); |
| | | // int expiredDays = (int)expiredDaysFloor; |
| | | |
| | | //直接显示已过期 |
| | | result = Language.Get("Mail11"); ; |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | |
| | |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | public int GetMailCount(MailCategory mailCategory) |
| | | { |
| | | var result = new List<string>(); |
| | | foreach (var kvp in mailDataDict) |
| | | { |
| | | if (kvp.Value.Category == mailCategory) |
| | | { |
| | | result.Add(kvp.Key); |
| | | } |
| | | } |
| | | return result.Count; |
| | | } |
| | | |
| | | |
| | | public List<string> GetSortMailScrList(MailCategory mailCategory) |
| | | { |
| | |
| | | IsBind = i.IsBind, |
| | | UserData = i.UserData |
| | | }).ToList(); |
| | | |
| | | if (!mailData.Items.IsNullOrEmpty()) |
| | | { |
| | | mailData.Items = mailData.Items |
| | | .Where(item => ItemConfig.HasKey((int)item.ItemID)) // 防护,确保物品存在 |
| | | .OrderByDescending(item => ItemConfig.Get((int)item.ItemID).ItemColor) // 按品质降序 |
| | | .ThenBy(item => item.Count) // 品质相同按数量升序 |
| | | .ToList(); |
| | | } |
| | | |
| | | mailDataDict[mail.GUID] = mailData; |
| | | } |
| | | |
| | |
| | | public string GUID; //邮件GUID |
| | | public byte Type; //邮件类型,暂时默认0 |
| | | public string CreateTime; //创建时间 |
| | | public DateTime CreateDateTime; |
| | | public DateTime CreateDateTime;//创建时间 |
| | | public byte LimitDays; //有效天数 |
| | | public string Title; //标题 |
| | | public string Text; //内容 |
| | | public byte MailState; //邮件状态: 0-未知;1-未读;2-已读;3-已领; |
| | | public List<MailItemData> Items; //物品信息 |
| | | |
| | | public bool HasAward() |
| | | { |
| | | return Items != null && Items.Count > 0; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 判断是否为模板类型邮件 |
| | |
| | | |
| | | try |
| | | { |
| | | // 参数格式为JSON数组:[参数1, 参数2, ...] |
| | | return JsonUtility.FromJson<List<string>>(Text); |
| | | List<string> result = JsonMapper.ToObject<List<string>>(Text); |
| | | // 如果解析结果为null(例如Text的内容是 "null" 字符串),则返回一个空列表以防后续出错 |
| | | return result ?? new List<string>(); |
| | | } |
| | | catch |
| | | catch (Exception ex) |
| | | { |
| | | Debug.LogError($"使用 LitJson 解析邮件参数失败。 Text: {Text}, 错误: {ex.Message}"); |
| | | return new List<string>(); |
| | | } |
| | | } |
| | | } |
| | | |