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

151 lines
3.7 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;
using PhxhSDK;
using System.Collections.Generic;
public class MailSystem : Singlenton<MailSystem>, IInitable
{
2024-04-15 19:34:15 +08:00
public Dictionary<ulong, SingleMail> DicMail;
2023-12-11 18:56:57 +08:00
public bool isHaveAll;
2023-12-12 19:35:47 +08:00
2024-04-15 19:34:15 +08:00
public const int MailStatus_New = 0;
public const int MailStatus_Opened = 1;
public const int MailStatus_Cliamed = 2;
2023-12-11 18:56:57 +08:00
public void Init()
{
2024-04-15 19:34:15 +08:00
DicMail = new Dictionary<ulong, SingleMail>();
2023-12-11 18:56:57 +08:00
isHaveAll = false;
2023-12-25 16:48:52 +08:00
RegisterEvent();
2023-12-11 18:56:57 +08:00
}
2024-04-15 19:34:15 +08:00
2023-12-25 16:48:52 +08:00
private void RegisterEvent()
{
EventManager.Instance.Register(EventManager.EventName.HaveNewMail, OnHaveNewMail);
}
private void OnHaveNewMail()
{
2024-04-15 19:34:15 +08:00
NetHelper.RequestMailList(!Instance.isHaveAll);
2023-12-25 16:48:52 +08:00
}
private void UnregisterEvent()
{
EventManager.Instance.Unregister(EventManager.EventName.HaveNewMail, OnHaveNewMail);
2023-12-11 18:56:57 +08:00
2023-12-25 16:48:52 +08:00
}
2024-04-15 19:34:15 +08:00
2023-12-11 18:56:57 +08:00
public void Release()
{
2023-12-25 16:48:52 +08:00
UnregisterEvent();
2024-04-15 19:34:15 +08:00
isHaveAll = false;
DicMail.Clear();
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
2023-12-11 18:56:57 +08:00
public void AddMail(Mail mail)
{
2024-04-15 19:34:15 +08:00
if (DicMail.TryGetValue(mail.Id, out SingleMail singleMail))
singleMail.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-15 19:34:15 +08:00
SingleMail mailDelect = null;
foreach (SingleMail temp in DicMail.Values)
2023-12-25 16:48:52 +08:00
{
2024-04-15 19:34:15 +08:00
if (mailDelect == temp)
mailDelect = temp;
if (temp.Status < mailDelect.Status)
continue;
if (temp.Status > mailDelect.Status)
{
mailDelect = temp;
continue;
}
if (mailDelect.SendTime < temp.SendTime)
continue;
if (mailDelect.SendTime > temp.SendTime)
{
mailDelect = temp;
continue;
}
if (temp.MailID < mailDelect.MailID)
continue;
if (temp.MailID > mailDelect.MailID)
2023-12-25 16:48:52 +08:00
{
2024-04-15 19:34:15 +08:00
mailDelect = temp;
continue;
2023-12-25 16:48:52 +08:00
}
2024-04-15 19:34:15 +08:00
}
if (mailDelect != null)
RemoveMail(mailDelect.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-15 19:34:15 +08:00
public void SetStatus(ulong id, uint status)
{
DicMail[id].SetStatus(status);
2023-12-11 18:56:57 +08:00
}
2024-04-15 19:34:15 +08:00
2023-12-12 19:35:47 +08:00
public void HandleIds(ulong id)
{
2024-04-15 19:34:15 +08:00
if (id == 0) // 全部已领取
2023-12-12 19:35:47 +08:00
{
2024-04-15 19:34:15 +08:00
foreach (SingleMail singleMail in DicMail.Values)
2023-12-12 19:35:47 +08:00
{
2024-04-15 19:34:15 +08:00
if (singleMail.Status != MailStatus_Opened)
continue;
if (singleMail.MailData == null)
continue;
singleMail.SetStatus(MailStatus_Cliamed);
2023-12-12 19:35:47 +08:00
}
}
2024-04-15 19:34:15 +08:00
else
DicMail[id].SetStatus(MailStatus_Cliamed);
2023-12-18 19:59:06 +08:00
}
public SingleMail GetMail(uint mailID)
{
2024-04-15 19:34:15 +08:00
if (DicMail.TryGetValue(mailID, out SingleMail 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
public bool IsHaveGetMail()
2023-12-19 14:19:07 +08:00
{
2024-04-15 19:34:15 +08:00
foreach (SingleMail singleMail in DicMail.Values)
2023-12-19 14:19:07 +08:00
{
2024-04-15 19:34:15 +08:00
if (singleMail.Status != MailStatus_Cliamed)
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-15 19:34:15 +08:00
}