增加相关协议

This commit is contained in:
2025-07-31 23:57:32 +08:00
parent 6b113cf32b
commit 96f22a3e48
27 changed files with 770 additions and 63 deletions

View File

@@ -33,19 +33,18 @@ public class G2Game_EnterRequestHandler : RouteRPC<Scene, G2Game_EnterRequest, G
// 如果不存在,表示这是一个新的账号,需要创建一下这个账号。
account = await PlayerFactory.Create(scene, accountId);
account.Basic.Level = 99;
account.Basic.NickName = "王麻子";
account.Basic.Country = "cn";
account.Basic.Exp = 999;
account.Basic.Head = "xxx.png";
account.Level = 99;
account.NickName = "王麻子";
account.Country = "cn";
account.Exp = 999;
account.Head = "xxx.png";
for (int i = 0; i < 500; i++)
{
var item = Entity.Create<Item>(scene, true, true);
account.Items.Add(item.Id, item);
}
for (int i = 0; i < 500; i++)
{
var item = Entity.Create<Fish>(scene, true, true);
@@ -86,6 +85,19 @@ public class G2Game_EnterRequestHandler : RouteRPC<Scene, G2Game_EnterRequest, G
account.LoginTime = TimeHelper.Now;
response.RoleRouteId = account.RuntimeId;
if (account.GetComponent<MailComponent>() == null)
{
var mailComponent = await scene.World.DataBase.Query<MailComponent>(account.Id, true);
if (mailComponent == null)
{
//如果没有邮件组件
account.AddComponent<MailComponent>();
}
else
{
account.AddComponent(mailComponent);
}
}
await FTask.CompletedTask;
}

View File

@@ -0,0 +1,36 @@
using Fantasy;
using Fantasy.Entitas.Interface;
namespace NB.Game;
public class AwardItemDestroySystem : DestroySystem<AwardItem>
{
protected override void Destroy(AwardItem self)
{
self.ConfigId = 0;
self.Count = 0;
}
}
public static class AwardItemSystem
{
public static AwardInfo ToAwardInfo(this AwardItem item)
{
return new AwardInfo()
{
ConfigId = item.ConfigId,
Count = item.Count
};
}
public static List<AwardInfo> ToAwardInfo(this List<AwardItem> items)
{
var list = new List<AwardInfo>();
foreach (var item in items)
{
list.Add(item.ToAwardInfo());
}
return list;
}
}

View File

@@ -0,0 +1,62 @@
using Fantasy;
using Fantasy.Entitas.Interface;
namespace NB.Game;
public class ItemDestroySystem : DestroySystem<Item>
{
protected override void Destroy(Item self)
{
self.ConfigId = 0;
self.Count = 0;
self.IsBind = false;
self.ExpirationTime = 0;
self.GetTime = 0;
self.Abrasion = 0;
}
}
public static class ItemSystem
{
public static ItemInfo ToItemInfo(this Item item)
{
return new ItemInfo()
{
ConfigId = item.ConfigId,
Id = item.Id,
Count = item.Count,
// ExpirationTime = item.IsBind;
};
}
public static List<ItemInfo> ToItemInfo(this List<Item> items)
{
var list = new List<ItemInfo>();
foreach (var item in items)
{
list.Add(item.ToItemInfo());
}
return list;
}
public static AwardInfo ToAwardInfo(this Item item)
{
return new AwardInfo()
{
ConfigId = item.ConfigId,
Count = item.Count
};
}
public static List<AwardInfo> ToAwardInfo(this List<Item> items)
{
var list = new List<AwardInfo>();
foreach (var item in items)
{
list.Add(item.ToAwardInfo());
}
return list;
}
}

View File

@@ -0,0 +1,79 @@
using Fantasy;
using Fantasy.Async;
using Fantasy.Entitas.Interface;
using Fantasy.Helper;
namespace NB.Game;
public class MailComponentDestroySystem : DestroySystem<MailComponent>
{
protected override void Destroy(MailComponent self)
{
foreach (var (_, mail) in self.Mails)
{
mail.Dispose();
}
self.Mails.Clear();
self.Timer.Clear();
}
}
public static class MailComponentSystem
{
public static async FTask Add(this MailComponent self, Mail mail, bool sync)
{
mail.CreateTime = TimeHelper.Now;
mail.ExpireTime = TimeHelper.Now + MailComponent.MaxExpireTime;
if (self.Mails.Count >= MailComponent.MaxMailCount)
{
//删除最老的邮件
var (_, value) = self.Timer.First();
foreach (var removeId in value)
{
await self.Remove(removeId, sync);
}
}
self.Mails.Add(mail.Id, mail);
self.Timer.Add(mail.ExpireTime, mail.Id);
if (sync)
{
//同步客户端
self.Scene.NetworkMessagingComponent.SendInnerRoute(0,new Game2C_HaveMail()
{
Mail = mail.ToMailInfo(),
});
}
await self.Scene.World.DataBase.Save(self);
Log.Info($"MailComponent Add id:{self.Id} mailId:{mail.Id} count:{self.Mails.Count}");
}
public static async FTask<uint> Remove(this MailComponent self, long mailId, bool sync)
{
if (!self.Mails.Remove(mailId, out var mail))
{
return 1;
}
self.Timer.RemoveValue(mail.ExpireTime, mail.Id);
mail.Dispose();
if (sync)
{
//同步客户端
self.Scene.NetworkMessagingComponent.SendInnerRoute(0,new Game2C_MailState()
{
MailState = (int)MailState.Deleted,
MailId = mailId,
});
}
await self.Scene.World.DataBase.Save(self);
Log.Info($"MailComponent Remove id:{self.Id} mailId:{mail.Id} count:{self.Mails.Count}");
return 0;
}
}

View File

@@ -0,0 +1,22 @@
using Fantasy.Entitas.Interface;
namespace NB.Game;
public class MailBoxDestroySystem : DestroySystem<MailBox>
{
protected override void Destroy(MailBox self)
{
if (self.Mail != null)
{
self.Mail.Dispose();
self.Mail = null;
}
self.BoxType = MailBoxType.None;
self.CreateTime = 0;
self.ExpireTime = 0;
self.SendAccountId = 0;
self.AccountId.Clear();
self.Received.Clear();
}
}

View File

@@ -0,0 +1,53 @@
using Fantasy;
using Fantasy.Entitas.Interface;
namespace NB.Game;
public class MailDestroySystem : DestroySystem<Mail>
{
protected override void Destroy(Mail self)
{
self.OwnerId = 0;
self.Title = string.Empty;
self.Content = string.Empty;
self.ExpireTime = 0;
self.CreateTime = 0;
self.State = MailState.None;
self.MailType = MailType.None;
foreach (var item in self.Items)
{
item.Dispose();
}
self.Items.Clear();
}
}
public static class MailSystem
{
public static MailInfo ToMailInfo(this Mail mail)
{
return new MailInfo()
{
Id = mail.Id,
Title = mail.Title,
Content = mail.Content,
ExpireTime = mail.ExpireTime,
CreateTime = mail.CreateTime,
MailState = (int)mail.State,
MailType = (int)mail.MailType,
Items = mail.Items.ToAwardInfo()
};
}
public static List<MailInfo> ToMailInfo(this List<Mail> mails)
{
var list = new List<MailInfo>();
foreach (var mail in mails)
{
list.Add(mail.ToMailInfo());
}
return list;
}
}

View File

@@ -0,0 +1,55 @@
using Fantasy;
using Fantasy.Entitas;
using Fantasy.Helper;
namespace NB.Game;
public static class MailBoxFactory
{
/// <summary>
/// 创建一个邮件箱
/// </summary>
/// <param name="scene"></param>
/// <param name="sendAccountId"></param>
/// <param name="mail"></param>
/// <param name="expireTime"></param>
/// <param name="accountIds"></param>
/// <returns></returns>
public static MailBox Create(Scene scene, long sendAccountId, Mail mail, int expireTime, List<long> accountIds)
{
var mailBox = Entity.Create<MailBox>(scene, true, true);
mailBox.SendAccountId = sendAccountId;
mailBox.Mail = mail;
mailBox.ExpireTime = TimeHelper.Now + expireTime;
mailBox.CreateTime = TimeHelper.Now;
if (accountIds == null || accountIds.Count <= 0)
{
return mailBox;
}
foreach (var accountId in accountIds)
{
mailBox.AccountId.Add(accountId);
}
return mailBox;
}
/// <summary>
/// 创建一个邮件箱
/// </summary>
/// <param name="scene"></param>
/// <param name="sendAccountId"></param>
/// <param name="expireTime"></param>
/// <param name="accountIds"></param>
/// <param name="title"></param>
/// <param name="content"></param>
/// <param name="items"></param>
/// <returns></returns>
public static MailBox Create(Scene scene, long sendAccountId, int expireTime, List<long> accountIds, string title,
string content, List<AwardItem> items = null)
{
var mail = MailFactory.Create(scene, title, content, items);
return Create(scene, sendAccountId, mail, expireTime, accountIds);
}
}

View File

@@ -0,0 +1,30 @@
using Fantasy;
using Fantasy.Entitas;
using Fantasy.Helper;
using Fantasy.Serialize;
namespace NB.Game;
public static class MailFactory
{
private static readonly ISerialize _serializer = SerializerManager.GetSerializer(FantasySerializerType.Bson);
public static Mail Create(Scene scene, string title, string content, List<AwardItem> items = null)
{
var mail = Entity.Create<Mail>(scene, true, true);
mail.Title = title;
mail.Content = content;
mail.State = MailState.Unread;
mail.CreateTime = TimeHelper.Now;
if (items != null && items.Count > 0)
{
foreach (var item in items)
{
mail.Items.Add(_serializer.Clone(item));
}
}
return mail;
}
}

View File

@@ -0,0 +1,31 @@
using Fantasy;
using Fantasy.Async;
namespace NB.Game;
/// <summary>
/// 发送邮件接口
/// </summary>
public static class MailHelper
{
public static async FTask Send(Scene scene, MailBox mailBox)
{
if (mailBox.BoxType == MailBoxType.None)
{
Log.Error("不支持的邮件类型");
return;
}
switch (mailBox.BoxType)
{
case MailBoxType.All:
{
break;
}
case MailBoxType.Specify:
{
break;
}
}
}
}

View File

@@ -12,4 +12,8 @@
<ProjectReference Include="..\Fantasy\Fantasy.Net\Fantasy.Net\Fantasy.Net.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Game\Mail\Handler\" />
</ItemGroup>
</Project>