93 lines
2.8 KiB
C#
93 lines
2.8 KiB
C#
using Fantasy;
|
||
using Fantasy.Async;
|
||
using Fantasy.Network;
|
||
using NB.Chat;
|
||
using NB.Game;
|
||
|
||
namespace NB.Gate;
|
||
|
||
public static class GateLoginHelper
|
||
{
|
||
#region 上线
|
||
|
||
/// <summary>
|
||
/// 网关通知其他服务器上线
|
||
/// </summary>
|
||
/// <param name="gateUnit"></param>
|
||
public static async FTask<uint> Online(GateUnit gateUnit)
|
||
{
|
||
if (gateUnit == null)
|
||
{
|
||
return ErrorCode.ErrArgs;
|
||
}
|
||
|
||
Session session = gateUnit.Session;
|
||
if (session == null)
|
||
{
|
||
return ErrorCode.ErrArgs;
|
||
}
|
||
|
||
var gateUnitSessionComponent = session.GetComponent<GateUnitSessionComponent>();
|
||
if (gateUnitSessionComponent == null)
|
||
{
|
||
gateUnitSessionComponent = session.AddComponent<GateUnitSessionComponent>();
|
||
}
|
||
|
||
var routeComponent = session.GetComponent<RouteComponent>();
|
||
if (routeComponent == null)
|
||
{
|
||
routeComponent = session.AddComponent<RouteComponent>();
|
||
}
|
||
|
||
gateUnitSessionComponent.AccountID = gateUnit.AccountID;
|
||
gateUnitSessionComponent.SessionId = session.RuntimeId;
|
||
|
||
|
||
//安排游戏服务器,并通知进入
|
||
var (gameRouteId, roleSimpleInfo) =
|
||
await GameSceneHelper.Online(session.Scene, gateUnit.AccountID, session.RuntimeId);
|
||
if (gameRouteId <= 0 || roleSimpleInfo == null)
|
||
{
|
||
return ErrorCode.OnlineSceneFailed;
|
||
}
|
||
|
||
Log.Info($"连接游戏服成功,gameRouteId:{gameRouteId}");
|
||
routeComponent.AddAddress(RouteType.GameRoute, gameRouteId);
|
||
gateUnit.GameSceneRouteId = gameRouteId;
|
||
|
||
|
||
//安排进入的聊天服
|
||
var (chatRouteId, sceneRouteId) =
|
||
await ChatSceneHelper.Online(session.Scene, roleSimpleInfo, session.RuntimeId);
|
||
if (chatRouteId <= 0)
|
||
{
|
||
return ErrorCode.OnlineSceneFailed;
|
||
}
|
||
|
||
routeComponent.AddAddress(RouteType.ChatRoute, chatRouteId);
|
||
gateUnit.ChatSceneRouteId = sceneRouteId;
|
||
Log.Info($"连接聊天服成功,gameRouteId:{gameRouteId}");
|
||
return ErrorCode.Successful;
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 下线
|
||
|
||
/// <summary>
|
||
/// 网关通知其他服务器下线
|
||
/// </summary>
|
||
/// <param name="gateUnit"></param>
|
||
/// <param name="sessionId"></param>
|
||
public static async FTask<uint> Offline(GateUnit gateUnit, long sessionId)
|
||
{
|
||
//通知服务器下线
|
||
Log.Info($"断线的session id={sessionId} ChatSceneRouteId={gateUnit.ChatSceneRouteId}");
|
||
await GameSceneHelper.Offline(gateUnit.Scene, gateUnit.AccountID, sessionId);
|
||
await ChatSceneHelper.Offline(gateUnit.Scene, gateUnit.AccountID, sessionId,
|
||
gateUnit.ChatSceneRouteId);
|
||
return ErrorCode.Successful;
|
||
}
|
||
|
||
#endregion
|
||
} |