NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/UI/Mail/MailSystem.cs

198 lines
4.9 KiB
C#
Raw Normal View History

2023-12-11 18:56:57 +08:00
using Framework;
using Gameplay;
2023-12-25 16:48:52 +08:00
using Gameplay.Common;
2023-12-11 18:56:57 +08:00
using Gameplay.Net;
2024-04-16 14:03:32 +08:00
using NLDPB;
2023-12-11 18:56:57 +08:00
using PhxhSDK;
using System.Collections.Generic;
public class MailSystem : Singlenton<MailSystem>, IInitable
{
2024-04-16 14:03:32 +08:00
/// <summary>
/// 邮件数据字典
/// </summary>
public Dictionary<ulong, MailInfo> DicMail;
/// <summary>
/// 是否有全部邮件
/// </summary>
2023-12-11 18:56:57 +08:00
public bool isHaveAll;
2023-12-12 19:35:47 +08:00
2023-12-11 18:56:57 +08:00
public void Init()
{
2024-04-16 14:03:32 +08:00
DicMail = new Dictionary<ulong, MailInfo>();
2023-12-11 18:56:57 +08:00
isHaveAll = false;
2024-04-15 19:34:15 +08:00
2023-12-25 16:48:52 +08:00
EventManager.Instance.Register(EventManager.EventName.HaveNewMail, OnHaveNewMail);
}
2024-04-16 14:03:32 +08:00
public void Release()
2023-12-25 16:48:52 +08:00
{
EventManager.Instance.Unregister(EventManager.EventName.HaveNewMail, OnHaveNewMail);
2023-12-11 18:56:57 +08:00
2024-04-16 14:03:32 +08:00
isHaveAll = false;
DicMail.Clear();
2023-12-25 16:48:52 +08:00
}
2024-04-15 19:34:15 +08:00
/// <summary>
/// 收到MailInfo的Message表示有新邮件需要更新
/// </summary>
2024-04-16 14:03:32 +08:00
private void OnHaveNewMail()
2023-12-11 18:56:57 +08:00
{
2024-07-01 13:37:31 +08:00
NetHelper.RequestMailList(!Instance.isHaveAll);
2023-12-11 18:56:57 +08:00
}
2024-04-15 19:34:15 +08:00
2023-12-11 18:56:57 +08:00
public void RemoveMail(ulong id)
{
2024-04-15 19:34:15 +08:00
if (!DicMail.Remove(id))
DebugUtil.LogError($"移除id为{id}的邮件失败");
2023-12-11 18:56:57 +08:00
}
2024-04-15 19:34:15 +08:00
2024-04-16 14:03:32 +08:00
/// <summary>
/// 添加邮件
/// </summary>
/// <param name="mail"></param>
2023-12-11 18:56:57 +08:00
public void AddMail(Mail mail)
{
2024-04-16 14:03:32 +08:00
if (DicMail.TryGetValue(mail.Id, out MailInfo mailInfo))
mailInfo.Refresh(mail);
2023-12-11 18:56:57 +08:00
else
{
2024-04-15 19:34:15 +08:00
if (DicMail.Count >= GLConfig.Inst.data.MaxMailCount)
2023-12-25 16:48:52 +08:00
{
2024-04-16 14:03:32 +08:00
MailInfo delectMail = null; // 待删除的邮件信息
2024-04-15 19:34:15 +08:00
2024-04-16 14:03:32 +08:00
foreach (MailInfo temp in DicMail.Values)
2023-12-25 16:48:52 +08:00
{
if (null == delectMail)
{
2024-04-16 14:03:32 +08:00
delectMail = temp;
continue;
}
2024-04-15 19:34:15 +08:00
2024-04-16 14:03:32 +08:00
if (temp.EStatus < delectMail.EStatus)
2024-04-15 19:34:15 +08:00
continue;
2024-04-16 14:03:32 +08:00
if (temp.EStatus > delectMail.EStatus)
2024-04-15 19:34:15 +08:00
{
2024-04-16 14:03:32 +08:00
delectMail = temp;
2024-04-15 19:34:15 +08:00
continue;
}
2024-04-16 15:03:43 +08:00
if (delectMail.SendTime < temp.SendTime)
2024-04-15 19:34:15 +08:00
continue;
2024-04-16 15:03:43 +08:00
if (delectMail.SendTime > temp.SendTime)
2024-04-15 19:34:15 +08:00
{
2024-04-16 14:03:32 +08:00
delectMail = temp;
2024-04-15 19:34:15 +08:00
continue;
}
2024-04-16 14:03:32 +08:00
if (temp.Cfg.Id < delectMail.Cfg.Id)
2024-04-15 19:34:15 +08:00
continue;
2024-04-16 14:03:32 +08:00
if (temp.Cfg.Id > delectMail.Cfg.Id)
2023-12-25 16:48:52 +08:00
{
2024-04-16 14:03:32 +08:00
delectMail = temp;
2024-04-15 19:34:15 +08:00
continue;
2023-12-25 16:48:52 +08:00
}
2024-04-15 19:34:15 +08:00
}
2024-04-16 14:03:32 +08:00
if (delectMail != null)
RemoveMail(delectMail.Id);
2023-12-25 16:48:52 +08:00
}
2024-04-15 19:34:15 +08:00
DicMail[mail.Id] = new(mail);
Instance.isHaveAll = true;
2023-12-11 18:56:57 +08:00
}
}
2024-04-16 14:03:32 +08:00
/// <summary>
/// 设置邮件逻辑状态
/// </summary>
/// <param name="id"></param>
/// <param name="status"></param>
public void SetStatus(ulong id, MailStatus status)
2024-04-15 19:34:15 +08:00
{
2024-04-16 14:03:32 +08:00
if (DicMail.TryGetValue(id, out MailInfo singleMail))
singleMail.SetStatus(status);
2023-12-11 18:56:57 +08:00
}
2024-04-15 19:34:15 +08:00
2024-09-25 14:24:15 +08:00
public void HandleIds(List<ulong> listId)
2023-12-12 19:35:47 +08:00
{
2024-09-25 14:24:15 +08:00
foreach (ulong id in listId)
2023-12-12 19:35:47 +08:00
{
2024-04-16 14:03:32 +08:00
SetStatus(id, MailStatus.Cliamed);
2024-09-25 14:24:15 +08:00
}
2023-12-18 19:59:06 +08:00
}
2024-04-16 14:03:32 +08:00
public MailInfo GetMail(uint mailID)
2023-12-18 19:59:06 +08:00
{
2024-04-16 14:03:32 +08:00
if (DicMail.TryGetValue(mailID, out MailInfo mail))
2023-12-18 19:59:06 +08:00
return mail;
2024-04-15 19:34:15 +08:00
else
2023-12-18 19:59:06 +08:00
{
2024-04-15 19:34:15 +08:00
DebugUtil.LogError($"你莫得Id为{mailID}的邮件,不要找了");
2023-12-18 19:59:06 +08:00
return null;
}
2023-12-12 19:35:47 +08:00
}
2024-04-15 19:34:15 +08:00
2024-04-16 15:03:43 +08:00
/// <summary>
/// 是否有可以领取的邮件
/// </summary>
/// <returns></returns>
2024-04-15 19:34:15 +08:00
public bool IsHaveGetMail()
2023-12-19 14:19:07 +08:00
{
2024-04-16 15:03:43 +08:00
foreach (MailInfo mailInfo in DicMail.Values)
2023-12-19 14:19:07 +08:00
{
2024-04-16 15:03:43 +08:00
if(mailInfo.IsHaveClaim)
2023-12-19 14:19:07 +08:00
return true;
}
2024-04-15 19:34:15 +08:00
2023-12-19 14:19:07 +08:00
return false;
}
2024-04-16 15:03:43 +08:00
public bool IsHaveUnreadMail()
{
foreach (MailInfo mailInfo in DicMail.Values)
{
var status = mailInfo.EStatus;
if (status == MailStatus.New)
return true;
}
return false;
}
2024-04-16 15:03:43 +08:00
/// <summary>
/// 清理所有到期邮件
/// </summary>
public void ClearOverTime()
{
List<ulong> listDelId = new();
foreach (MailInfo mailInfo in DicMail.Values)
{
if (mailInfo.OverTime <= CommonUtils.GetCurTime(CommonUtils.E_TimeType.Server))
{
if (MailStatus.New == mailInfo.EStatus)
mailInfo.SetStatus(MailStatus.Opened);
2024-04-16 15:03:43 +08:00
listDelId.Add(mailInfo.Id);
}
2024-04-16 15:03:43 +08:00
}
foreach (ulong id in listDelId)
{
Instance.RemoveMail(id);
NetHelper.DeleteMail(id);
}
}
2024-12-17 13:56:38 +08:00
public void ClearMailData()
{
if (null == DicMail)
return;
DicMail.Clear();
isHaveAll = false;
}
2024-04-15 19:34:15 +08:00
}