This commit is contained in:
2025-07-27 12:34:04 +08:00
parent 6311c7cb12
commit 743c1d2baa
194 changed files with 81685 additions and 696 deletions

View File

@@ -1,49 +0,0 @@
using Fantasy.Entitas.Interface;
namespace NB.Gate.System;
public sealed class GameAccountManageComponentDestroySystem : DestroySystem<GameAccountManageComponent>
{
protected override void Destroy(GameAccountManageComponent self)
{
foreach (var (_, gameAccount) in self.Accounts)
{
gameAccount.Dispose();
}
self.Accounts.Clear();
}
}
public static class GameAccountManageComponentSystem
{
public static void Add(this GameAccountManageComponent self, GameAccount account)
{
self.Accounts.Add(account.Id, account);
}
public static GameAccount? Get(this GameAccountManageComponent self, long accountId)
{
return self.Accounts.GetValueOrDefault(accountId);
}
public static bool TryGet(this GameAccountManageComponent self, long accountId, out GameAccount? account)
{
return self.Accounts.TryGetValue(accountId, out account);
}
public static void Remove(this GameAccountManageComponent self, long accountId, bool isDispose = true)
{
if (!self.Accounts.Remove(accountId, out var account))
{
return;
}
if (!isDispose)
{
return;
}
account.Dispose();
}
}

View File

@@ -2,9 +2,9 @@ using Fantasy.Entitas.Interface;
namespace NB.Gate;
public sealed class GameAccountDestroySystem : DestroySystem<GameAccount>
public sealed class PlayerDestroySystem : DestroySystem<Player>
{
protected override void Destroy(GameAccount self)
protected override void Destroy(Player self)
{
self.CreateTime = 0;
self.LoginTime = 0;

View File

@@ -5,20 +5,20 @@ using Fantasy.Helper;
namespace NB.Gate;
public static class GameAccountFactory
public static class PlayerFactory
{
/// <summary>
/// 创建一个新的GameAccount
/// 创建一个新的Player
/// </summary>
/// <param name="scene"></param>
/// <param name="aId">ToKen令牌传递过来的aId</param>
/// <param name="isSaveDataBase">是否在创建的过程中保存到数据库</param>
/// <returns></returns>
public static async FTask<GameAccount> Create(Scene scene, long aId, bool isSaveDataBase = true)
public static async FTask<Player> Create(Scene scene, long aId, bool isSaveDataBase = true)
{
var gameAccount = Entity.Create<GameAccount>(scene, aId, false, false);
var gameAccount = Entity.Create<Player>(scene, aId, false, false);
gameAccount.LoginTime = gameAccount.CreateTime = TimeHelper.Now;
if (isSaveDataBase)
{
await gameAccount.SaveDataBase();

View File

@@ -2,15 +2,15 @@ using Fantasy.Entitas.Interface;
namespace NB.Gate;
public sealed class GameAccountFlagComponentDestroySystem : DestroySystem<GameAccountFlagComponent>
public sealed class PlayerFlagComponentDestroySystem : DestroySystem<PlayerFlagComponent>
{
protected override void Destroy(GameAccountFlagComponent self)
protected override void Destroy(PlayerFlagComponent self)
{
if (self.AccountID != 0)
{
// 执行下线过程、并且要求在5分钟后完成缓存清理。也就是5分钟后会保存数据到数据库。
// 由于5分钟太长了、咱们测试的时候不方便测试也可以把这个时间改短一些比如10秒。
GameAccountHelper.Disconnect(self.Scene, self.AccountID, 1000 * 60 * 5).Coroutine();
PlayerHelper.Disconnect(self.Scene, self.AccountID, 1000 * 60 * 5).Coroutine();
self.AccountID = 0;
}

View File

@@ -5,7 +5,7 @@ using Fantasy.Network;
namespace NB.Gate;
public static class GameAccountHelper
public static class PlayerHelper
{
/// <summary>
/// 从数据库中读取GameAccount
@@ -13,9 +13,9 @@ public static class GameAccountHelper
/// <param name="scene"></param>
/// <param name="accountId">账号Id</param>
/// <returns></returns>
public static async FTask<GameAccount?> LoadDataBase(Scene scene, long accountId)
public static async FTask<Player?> LoadDataBase(Scene scene, long accountId)
{
var account = await scene.World.DataBase.First<GameAccount>(d => d.Id == accountId);
var account = await scene.World.DataBase.First<Player>(d => d.Id == accountId);
if (account == null)
{
return null;
@@ -29,7 +29,7 @@ public static class GameAccountHelper
/// 保存账号到数据库中
/// </summary>
/// <param name="self"></param>
public static async FTask SaveDataBase(this GameAccount self)
public static async FTask SaveDataBase(this Player self)
{
await self.Scene.World.DataBase.Save(self);
}
@@ -38,12 +38,12 @@ public static class GameAccountHelper
/// 执行该账号的断开逻辑,不要非必要不要使用这个接口,这个接口是内部使用。
/// </summary>
/// <param name="self"></param>
public static async FTask Disconnect(this GameAccount self)
public static async FTask Disconnect(this Player self)
{
// 保存该账号信息到数据库中。
await SaveDataBase(self);
// 在缓存中移除自己并且执行自己的Dispose方法。
self.Scene.GetComponent<GameAccountManageComponent>().Remove(self.Id);
self.Scene.GetComponent<PlayerManageComponent>().Remove(self.Id);
}
/// <summary>
@@ -62,7 +62,7 @@ public static class GameAccountHelper
// 这样的话是不是可以在登录的时候给这个组件把AccountId存到这个组件呢
// 要检查当前缓存中是否存在该账号的数据
var gameAccountManageComponent = scene.GetComponent<GameAccountManageComponent>();
var gameAccountManageComponent = scene.GetComponent<PlayerManageComponent>();
if (!gameAccountManageComponent.TryGet(accountId, out var account))
{
// 如果缓存中没有、那表示已经下线或者根本不存在该账号,应该在打印一个警告,因为正常的情况下是不会出现的。
@@ -98,7 +98,7 @@ public static class GameAccountHelper
/// </summary>
/// <param name="self"></param>
/// <returns></returns>
public static GameAccountInfo GetGameAccountInfo(this GameAccount self)
public static GameAccountInfo GetGameAccountInfo(this Player self)
{
// 其实可以不用每次都NEW一个新的GameAccountInfo
// 可以在当前账号下创建一个GameAccountInfo每次变动会提前通知这个GameAccountInfo

View File

@@ -0,0 +1,49 @@
using Fantasy.Entitas.Interface;
namespace NB.Gate.System;
public sealed class PlayerManageComponentDestroySystem : DestroySystem<PlayerManageComponent>
{
protected override void Destroy(PlayerManageComponent self)
{
foreach (var (_, gameAccount) in self.Players)
{
gameAccount.Dispose();
}
self.Players.Clear();
}
}
public static class PlayerManageComponentSystem
{
public static void Add(this PlayerManageComponent self, Player account)
{
self.Players.Add(account.Id, account);
}
public static Player Get(this PlayerManageComponent self, long accountId)
{
return self.Players.GetValueOrDefault(accountId);
}
public static bool TryGet(this PlayerManageComponent self, long accountId, out Player? account)
{
return self.Players.TryGetValue(accountId, out account);
}
public static void Remove(this PlayerManageComponent self, long accountId, bool isDispose = true)
{
if (!self.Players.Remove(accountId, out var account))
{
return;
}
if (!isDispose)
{
return;
}
account.Dispose();
}
}