using System; using System.Collections.Generic; namespace ET { public class MailComponentAwakeSystem : AwakeSystem { public override void Awake(MailComponent self) { MailComponent.Instance = self; } } public class MailComponentDestroySystem : DestroySystem { public override void Destroy(MailComponent self) { //self.MailLibDic.Clear(); } } public class Mail_Per1DayEvent : AEvent { public override async ETTask Run(EventType.UpdatePer1DayOfMonth args) { var now = args.now; var ret = await DBComponent.Instance.UpdateJson("{}", $"{{'$pull':{{'Dic':{{'v.RemainTime':{{ $lt:{now}}}}}}}}}"); } } public static class MailComponentSystem { private const int MailCount_Per_Page = 10; public static async ETTask AddMail(this MailComponent self, long id,string title,string content ,string sender = "系统") { await ETTask.CompletedTask; } public static async ETTask AddMail(this MailComponent self, long unitId, Mail mail) { var mailLib = await self.Quary(unitId); mailLib.Dic.Add(mail.Id, mail); await self.Save(mailLib); } public static async ETTask AddMail(this MailComponent self, long unitId, IEnumerable mails) { var mailLib = await self.Quary(unitId); foreach (var item in mails) { mailLib.Dic.Add(item.Id, item); } self.Save(mailLib).Coroutine(); } public static async ETTask Save(this MailComponent self, MailLib mailLib) { await DBComponent.Instance.Save(mailLib); } public static async ETTask Quary(this MailComponent self, long id) { //if (!self.MailLibDic.TryGetValue(id, out var mailLib)) //{ var mailLib = await DBComponent.Instance.Query(id); if (mailLib == null) { mailLib = EntityFactory.CreateWithId(self, id); } //self.MailLibDic[id] = mailLib; //} return mailLib; } public static async ETTask GetMail(this MailComponent self, long id, int page, System.Collections.Generic.List mailList) { var mailLib = await self.Quary(id); int index = -1, count = MailCount_Per_Page; foreach (var mail in mailLib.Dic.Values) { if (mail.RemainTime < TimeHelper.ClientNow()) continue; if (count <= 0) break; index++; if (((index - page * MailCount_Per_Page) | (((page + 1) * MailCount_Per_Page) - index - 1)) >= 0) { count--; mailList.Add(mail); } } return mailLib.Dic.Count / MailCount_Per_Page + 1; } public static async ETTask<(string, int)> ReceiveMail(this MailComponent self, Unit unit, long mailId, int page, System.Collections.Generic.List mailList) { var mailLib = await self.Quary(unit.Id); if (!mailLib.Dic.TryGetValue(mailId, out var mail)) { Log.Error($"【{ UserComponent.Instance.Get(unit.Id)?.NickName} ({ unit.Id})】没有 mailId ={mailId}的邮件"); return ("系统错误", 0); } if (mail.State != MailState.UnReceive) { Log.Error($"【{ UserComponent.Instance.Get(unit.Id)?.NickName} ({ unit.Id})】 要领取 mailId ={mailId}的邮件,但是邮件以领取,突破客户端验证"); return ("系统错误", 0); } mail.State = MailState.Received; await self.Save(mailLib); string addItemRet = null; using var listComponent = ListComponent<(int, long)>.Create(); foreach (var item in mail.RewordArr) { listComponent.List.Add((item.ItemId, item.Count)); } if (!ItemComponent.Instance.CanAddItem(unit, listComponent.List)) { mail.State = MailState.UnReceive; await self.Save(mailLib); return (ConstDefine.BagFullError,0); } foreach (var item in mail.RewordArr) { if (item.IsHasItem) addItemRet = BagHelper.AddItem(unit, item.ItemId, item.Count, item.IsLock, item.Item, getSource: item.Item.getSource); else addItemRet = BagHelper.AddItem(unit, item.ItemId, item.Count, true, getSource: item.getSource); if (addItemRet != null) { Log.Error($"{addItemRet}"); return (addItemRet, 0); } } UnitHelper.Save(unit).Coroutine(); UnitHelper.Save(unit).Coroutine(); //!获取更新后的邮件列表 int index = -1, count = MailCount_Per_Page; foreach (var _mail in mailLib.Dic.Values) { if (_mail.RemainTime < TimeHelper.ClientNow()) continue; if (count <= 0) break; index++; if (((index - page * MailCount_Per_Page) | (((page + 1) * MailCount_Per_Page) - index - 1)) >= 0) { count--; mailList.Add(_mail); } } return (addItemRet, mailLib.Dic.Count / MailCount_Per_Page + 1); } public static async ETTask<(string, int)> ReceiveAllMail(this MailComponent self, Unit unit, System.Collections.Generic.List mailList) { var mailLib = await self.Quary(unit.Id); string addItemRet = null; using var listComponent = ListComponent<(int, long)>.Create(); foreach (var mail in mailLib.Dic.Values) { if (mail.State != MailState.UnReceive) { continue; } mail.State = MailState.Received; listComponent.List.Clear(); foreach (var item in mail.RewordArr) { listComponent.List.Add((item.ItemId, item.Count)); } if (!ItemComponent.Instance.CanAddItem(unit, listComponent.List)) { mail.State = MailState.UnReceive; await self.Save(mailLib); return (ConstDefine.BagFullError, 0); } foreach (var item in mail.RewordArr) { if (item.IsHasItem) addItemRet = BagHelper.AddItem(unit, item.ItemId, item.Count, item.IsLock, item.Item, getSource: item.Item.getSource); else addItemRet = BagHelper.AddItem(unit, item.ItemId, item.Count, true, getSource: item.getSource); } if (addItemRet != null) { Log.Error($"{addItemRet}"); return (addItemRet, 0); } } await self.Save(mailLib); UnitHelper.Save(unit).Coroutine(); UnitHelper.Save(unit).Coroutine(); //!获取更新后的邮件列表 int index = -1, count = MailCount_Per_Page; int page = 0; foreach (var _mail in mailLib.Dic.Values) { if (_mail.RemainTime < TimeHelper.ClientNow()) continue; if (count <= 0) break; index++; if (((index - page * MailCount_Per_Page) | (((page + 1) * MailCount_Per_Page) - index - 1)) >= 0) { count--; mailList.Add(_mail); } } return (addItemRet, mailLib.Dic.Count / MailCount_Per_Page + 1); } public static async ETTask<(string, int)> DeleteMail(this MailComponent self, long id, long mailId, int page, System.Collections.Generic.List mailList) { var mailLib = await self.Quary(id); if (!mailLib.Dic.TryGetValue(mailId, out _)) { Log.Error($"玩家Id ={id} 没有 mailId ={mailId}的邮件"); return ("系统错误", 0); } mailLib.Dic.Remove(mailId); self.Save(mailLib).Coroutine(); //!获取更新后的邮件列表 int index = -1, count = MailCount_Per_Page; foreach (var _mail in mailLib.Dic.Values) { if (_mail.RemainTime < TimeHelper.ClientNow()) continue; if (count <= 0) break; index++; if (((index - page * MailCount_Per_Page) | (((page + 1) * MailCount_Per_Page) - index - 1)) >= 0) { count--; mailList.Add(_mail); } } return (string.Empty, mailLib.Dic.Count / MailCount_Per_Page + 1); } public static async ETTask DeleteAllMail(this MailComponent self, long id, System.Collections.Generic.List mailList) { var mailLib = await self.Quary(id); List needRemoveList = new List(); foreach (var mail in mailLib.Dic.Values) { if (mail.State == MailState.Received) { needRemoveList.Add(mail.Id); } } foreach (var item in needRemoveList) { mailLib.Dic.Remove(item); } self.Save(mailLib).Coroutine(); //!获取更新后的邮件列表 int index = -1, count = MailCount_Per_Page; int page = 0; foreach (var _mail in mailLib.Dic.Values) { if (_mail.RemainTime < TimeHelper.ClientNow()) continue; if (count <= 0) break; index++; if (((index - page * MailCount_Per_Page) | (((page + 1) * MailCount_Per_Page) - index - 1)) >= 0) { count--; mailList.Add(_mail); } } return mailLib.Dic.Count / MailCount_Per_Page + 1; } } }