269 lines
11 KiB
C#
Executable File
269 lines
11 KiB
C#
Executable File
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using MongoDB.Driver;
|
|
|
|
namespace ET
|
|
{
|
|
|
|
public class MailComponentAwakeSystem : AwakeSystem<MailComponent>
|
|
{
|
|
public override void Awake(MailComponent self)
|
|
{
|
|
MailComponent.Instance = self;
|
|
}
|
|
}
|
|
|
|
public class MailComponentDestroySystem : DestroySystem<MailComponent>
|
|
{
|
|
public override void Destroy(MailComponent self)
|
|
{
|
|
//self.MailLibDic.Clear();
|
|
}
|
|
}
|
|
public class Mail_Per1DayEvent : AEvent<EventType.UpdatePer1DayOfMonth>
|
|
{
|
|
public override async ETTask Run(EventType.UpdatePer1DayOfMonth args)
|
|
{
|
|
long now = args.now;
|
|
|
|
UpdateResult ret = await DBComponent.Instance.UpdateJson<MailLib>("{}", $"{{'$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)
|
|
{
|
|
MailLib 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<Mail> mails)
|
|
{
|
|
MailLib mailLib = await self.Quary(unitId);
|
|
foreach (Mail 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<MailLib> Quary(this MailComponent self, long id)
|
|
{
|
|
//if (!self.MailLibDic.TryGetValue(id, out var mailLib))
|
|
//{
|
|
MailLib mailLib = await DBComponent.Instance.Query<MailLib>(id);
|
|
if (mailLib == null)
|
|
{
|
|
mailLib = EntityFactory.CreateWithId<MailLib>(self, id);
|
|
}
|
|
//self.MailLibDic[id] = mailLib;
|
|
//}
|
|
return mailLib;
|
|
|
|
}
|
|
public static async ETTask<int> GetMail(this MailComponent self, long id, int page, System.Collections.Generic.List<Mail> mailList)
|
|
{
|
|
MailLib mailLib = await self.Quary(id);
|
|
int index = -1, count = MailCount_Per_Page;
|
|
foreach (Mail 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<Mail> mailList)
|
|
{
|
|
MailLib mailLib = await self.Quary(unit.Id);
|
|
if (!mailLib.Dic.TryGetValue(mailId, out Mail 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 ListComponent<(int, long)> listComponent = ListComponent<(int, long)>.Create();
|
|
foreach (MailItem 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 (MailItem 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<Bag>(unit);
|
|
UnitHelper.Save<NumericComponent>(unit);
|
|
//!获取更新后的邮件列表
|
|
int index = -1, count = MailCount_Per_Page;
|
|
foreach (Mail _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<Mail> mailList)
|
|
{
|
|
MailLib mailLib = await self.Quary(unit.Id);
|
|
string addItemRet = null;
|
|
using ListComponent<(int, long)> listComponent = ListComponent<(int, long)>.Create();
|
|
foreach (Mail mail in mailLib.Dic.Values)
|
|
{
|
|
if (mail.State != MailState.UnReceive)
|
|
{
|
|
continue;
|
|
}
|
|
mail.State = MailState.Received;
|
|
listComponent.List.Clear();
|
|
foreach (MailItem 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 (MailItem 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<Bag>(unit);
|
|
UnitHelper.Save<NumericComponent>(unit);
|
|
//!获取更新后的邮件列表
|
|
int index = -1, count = MailCount_Per_Page;
|
|
int page = 0;
|
|
foreach (Mail _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)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
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<Mail> mailList)
|
|
{
|
|
MailLib 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 (Mail _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<int> DeleteAllMail(this MailComponent self, long id, System.Collections.Generic.List<Mail> mailList)
|
|
{
|
|
MailLib mailLib = await self.Quary(id);
|
|
List<long> needRemoveList = new List<long>();
|
|
foreach (Mail mail in mailLib.Dic.Values)
|
|
{
|
|
if (mail.State == MailState.Received)
|
|
{
|
|
needRemoveList.Add(mail.Id);
|
|
}
|
|
}
|
|
foreach (long item in needRemoveList)
|
|
{
|
|
mailLib.Dic.Remove(item);
|
|
}
|
|
self.Save(mailLib).Coroutine();
|
|
//!获取更新后的邮件列表
|
|
int index = -1, count = MailCount_Per_Page;
|
|
int page = 0;
|
|
foreach (Mail _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;
|
|
}
|
|
|
|
}
|
|
|
|
}
|