新增角色相关信息
This commit is contained in:
@@ -1,13 +0,0 @@
|
||||
using Fantasy.Entitas.Interface;
|
||||
|
||||
namespace NB.Gate;
|
||||
|
||||
public sealed class PlayerDestroySystem : DestroySystem<Player>
|
||||
{
|
||||
protected override void Destroy(Player self)
|
||||
{
|
||||
self.CreateTime = 0;
|
||||
self.LoginTime = 0;
|
||||
self.SessionRunTimeId = 0;
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
using Fantasy;
|
||||
using Fantasy.Async;
|
||||
using Fantasy.Entitas;
|
||||
using Fantasy.Helper;
|
||||
|
||||
namespace NB.Gate;
|
||||
|
||||
public static class PlayerFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// 创建一个新的Player
|
||||
/// </summary>
|
||||
/// <param name="scene"></param>
|
||||
/// <param name="aId">ToKen令牌传递过来的aId</param>
|
||||
/// <param name="isSaveDataBase">是否在创建的过程中保存到数据库</param>
|
||||
/// <returns></returns>
|
||||
public static async FTask<Player> Create(Scene scene, long aId, bool isSaveDataBase = true)
|
||||
{
|
||||
var gameAccount = Entity.Create<Player>(scene, aId, false, false);
|
||||
gameAccount.LoginTime = gameAccount.CreateTime = TimeHelper.Now;
|
||||
|
||||
if (isSaveDataBase)
|
||||
{
|
||||
await gameAccount.SaveDataBase();
|
||||
}
|
||||
|
||||
return gameAccount;
|
||||
}
|
||||
}
|
||||
@@ -1,113 +0,0 @@
|
||||
using NB.Gate.System;
|
||||
using Fantasy;
|
||||
using Fantasy.Async;
|
||||
using Fantasy.Network;
|
||||
|
||||
namespace NB.Gate;
|
||||
|
||||
public static class PlayerHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 从数据库中读取GameAccount
|
||||
/// </summary>
|
||||
/// <param name="scene"></param>
|
||||
/// <param name="accountId">账号Id</param>
|
||||
/// <returns></returns>
|
||||
public static async FTask<Player> LoadDataBase(Scene scene, long accountId)
|
||||
{
|
||||
var account = await scene.World.DataBase.First<Player>(d => d.Id == accountId);
|
||||
if (account == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
account.Deserialize(scene);
|
||||
return account;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存账号到数据库中
|
||||
/// </summary>
|
||||
/// <param name="self"></param>
|
||||
public static async FTask SaveDataBase(this Player self)
|
||||
{
|
||||
await self.Scene.World.DataBase.Save(self);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行该账号的断开逻辑,不要非必要不要使用这个接口,这个接口是内部使用。
|
||||
/// </summary>
|
||||
/// <param name="self"></param>
|
||||
public static async FTask Disconnect(this Player self)
|
||||
{
|
||||
// 保存该账号信息到数据库中。
|
||||
await SaveDataBase(self);
|
||||
// 在缓存中移除自己,并且执行自己的Dispose方法。
|
||||
self.Scene.GetComponent<PlayerManageComponent>().Remove(self.Id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 账号完整的断开逻辑,执行了这个接口后,该账号会完全下线。
|
||||
/// </summary>
|
||||
/// <param name="scene"></param>
|
||||
/// <param name="accountId"></param>
|
||||
/// <param name="timeOut"></param>
|
||||
public static async FTask Disconnect(Scene scene, long accountId, int timeOut = 1000 * 60 * 3)
|
||||
{
|
||||
// 调用该方法有如下几种情况:
|
||||
// 1、客户端主动断开,如:退出游戏、切换账号、等。 客户端会主动发送一个协议给服务器通知服务器断开。
|
||||
// 2、客户端断断线 客户端不会主动发送一个协议给服务器,是由服务器的心跳来检测是否断开了。
|
||||
// 如果是心跳检测断开的Session,我怎么能拿到当前的这个账号来进行下线处理呢?
|
||||
// 通过给当前的Session挂载一个组件,当销毁这个Session时候呢,也会销毁这个组件。
|
||||
// 这样的话,是不是可以在登录的时候,给这个组件把AccountId存到这个组件呢?
|
||||
|
||||
// 要检查当前缓存中是否存在该账号的数据
|
||||
var gameAccountManageComponent = scene.GetComponent<PlayerManageComponent>();
|
||||
if (!gameAccountManageComponent.TryGet(accountId, out var account))
|
||||
{
|
||||
// 如果缓存中没有、那表示已经下线或者根本不存在该账号,应该在打印一个警告,因为正常的情况下是不会出现的。
|
||||
Log.Warning($"GameAccountHelper Disconnect accountId : {accountId} not found");
|
||||
return;
|
||||
}
|
||||
// 为了防止逻辑的错误,加一个警告来排除下
|
||||
if (!scene.TryGetEntity<Session>(account.SessionRunTimeId, out var session))
|
||||
{
|
||||
// 如果没有找到对应的Session,那只有一种可能就是当前的链接会话已经断开了,一般的情况下也不会出现的,所以咱们也要打印一个警告。
|
||||
Log.Warning($"GameAccountHelper Disconnect accountId : {accountId} SessionRunTimeId : {account.SessionRunTimeId} not found");
|
||||
return;
|
||||
}
|
||||
// 如果不存在定时任务的组件,那就添加并设置定时任务
|
||||
if (account.IsTimeOutComponent())
|
||||
{
|
||||
// 如果已经存在了,那就表示当然已经有一个延时断开的任务了,那就不需要重复添加了
|
||||
return;
|
||||
}
|
||||
// 立即下线处理
|
||||
if (timeOut <= 0)
|
||||
{
|
||||
// 如果timeOut销毁或等会0的情况下,执行立即下线。
|
||||
await account.Disconnect();
|
||||
return;
|
||||
}
|
||||
// 设置延迟下线
|
||||
account.SetTimeout(timeOut, account.Disconnect);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获得GameAccountInfo
|
||||
/// </summary>
|
||||
/// <param name="self"></param>
|
||||
/// <returns></returns>
|
||||
public static GameAccountInfo GetGameAccountInfo(this Player self)
|
||||
{
|
||||
// 其实可以不用每次都NEW一个新的GameAccountInfo
|
||||
// 可以在当前账号下创建一个GameAccountInfo,每次变动会提前通知这个GameAccountInfo
|
||||
// 又或者每次调用该方法的时候,把值重新赋值一下。
|
||||
|
||||
return new GameAccountInfo()
|
||||
{
|
||||
CreateTime = self.CreateTime,
|
||||
LoginTime = self.LoginTime
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -2,18 +2,18 @@ using Fantasy.Entitas.Interface;
|
||||
|
||||
namespace NB.Gate;
|
||||
|
||||
public sealed class PlayerFlagComponentDestroySystem : DestroySystem<PlayerFlagComponent>
|
||||
public sealed class SessionPlayerComponentDestroySystem : DestroySystem<SessionPlayerComponent>
|
||||
{
|
||||
protected override void Destroy(PlayerFlagComponent self)
|
||||
protected override void Destroy(SessionPlayerComponent self)
|
||||
{
|
||||
if (self.AccountID != 0)
|
||||
{
|
||||
// 执行下线过程、并且要求在5分钟后完成缓存清理。也就是5分钟后会保存数据到数据库。
|
||||
// 由于5分钟太长了、咱们测试的时候,不方便测试,也可以把这个时间改短一些,比如10秒。
|
||||
PlayerHelper.Disconnect(self.Scene, self.AccountID, 1000 * 60 * 5).Coroutine();
|
||||
self.AccountID = 0;
|
||||
// PlayerHelper.Disconnect(self.Scene, self.AccountID, 1000 * 60 * 5).Coroutine();
|
||||
// self.AccountID = 0;
|
||||
}
|
||||
|
||||
self.Player = null;
|
||||
// self.Player = null;
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user