Eintoo/GameServer/Server/Hotfix/Outter/Gate/GameAccount/GameAccountManageComponentSystem.cs

144 lines
4.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Fantasy;
using Fantasy.Async;
using Fantasy.Entitas.Interface;
using Fantasy.Network;
using Hotfix.Helper;
namespace Hotfix;
public static class GameAccountManageComponentSystem
{
public static void Add(this GameAccountManageComponent self,long accountId, GameAccount gameAccount)
{
if (!self.Accounts.ContainsKey(accountId))
{
self.Accounts.Add(accountId, gameAccount);
}
else
{
self.Accounts[accountId] = gameAccount;
}
}
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 bool Remove(this GameAccountManageComponent self, long accountId, bool isDispose = true)
{
if (!self.Accounts.Remove(accountId,out var gameAccount))
{
return false;
}
if (isDispose)
{
gameAccount.Dispose();
}
return true;
}
public static async FTask<(uint error,GameAccount gameAccount)> LoginAccountGame(this GameAccountManageComponent self,Session session,long accountId)
{
var scene = self.Scene;
//GameAccount gameAccount = null;
using (await scene.CoroutineLockComponent.Wait((int)ELockType.GateGetGameAccount,accountId))
{
// gameAccount = self.Get(accountId);
if (!self.TryGet(accountId,out var gameAccount))
{
gameAccount = await GameAccountHelper.LoadGameAccountFromDatabase(scene,accountId);
if (gameAccount == null)
{
Log.Debug("Gate创建新的账号account并保存到数据库");
gameAccount = await GameAccountFactory.Create(scene, accountId);
}
self.Add(accountId, gameAccount);
}
else
{
Log.Debug("Gate account already exists账号已存在缓存中");
if ( gameAccount.SessionRuntimeId == session.RuntimeId)
{
return (GameErrorCode.GateRepeatedLogin,gameAccount);
}
Log.Debug($"Gate 检测当前帐号和当前Session 不是同一个,{gameAccount.SessionRuntimeId},${session.RuntimeId}");
if (scene.TryGetEntity<Session>( gameAccount.SessionRuntimeId,out var oldSession))
{
Log.Debug($"Gate 如果当前Session 存在 需要 发送 给 oldSession 一个 重复登录的消息 并且要断开这个Session");
oldSession.GetComponent<GameAccountFlagComponent>().AccountId = 0;
oldSession.Send(new G2C_LoginRepeatedMessage());
oldSession.SetTimeout(3000);
}
}
var flagCom = session.AddComponent<GameAccountFlagComponent>();
flagCom.AccountId = accountId;
flagCom.Account = gameAccount;
Log.Debug($"Gate 当前缓存中的 SessionID {session.RuntimeId}");
gameAccount.SessionRuntimeId = session.RuntimeId;
return (GameErrorCode.GateLoginSuccess,gameAccount);
}
}
public static async FTask DisConnect(this GameAccountManageComponent self, long accountId,long timeout = 1000 * 60 * 5 )
{
var scene = self?.Scene;
if (scene == null)
{
Log.Debug("scene is null");
}
if (!self.TryGet(accountId,out var gameAccount))
{
Log.Warning("Gate:下线操作时失败,账号缓存库里面并不存在这个账号,逻辑出现错误了");
return;
}
if (!scene.TryGetEntity<Session>(gameAccount.SessionRuntimeId,out var session))
{
Log.Warning("Gate:下线操作时失败,Session 未找到 已经实现了断开了逻辑,逻辑出现错误了");
return;
}
if (gameAccount.IsHasTimeout())
{
Log.Debug("Gate 已经存在了销毁组件");
return;
}
if (timeout < 0 )
{
await gameAccount.DisConnect();
return;
}
gameAccount.SetTimeout(timeout,gameAccount.DisConnect);
}
}
public class GameAccountManageComponentDestroySystem : DestroySystem<GameAccountManageComponent>
{
protected override void Destroy(GameAccountManageComponent self)
{
foreach (var (_,account) in self.Accounts.ToArray())
{
account.Dispose();
}
self.Accounts.Clear();
}
}