136 lines
3.8 KiB
C#
136 lines
3.8 KiB
C#
using Fantasy;
|
|
using Fantasy.Async;
|
|
using Fantasy.Entitas.Interface;
|
|
using Fantasy.Network;
|
|
using NB.Common;
|
|
|
|
// ReSharper disable ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
|
|
|
|
namespace NB.Gate;
|
|
|
|
public class GateUnitDestroySystem : DestroySystem<GateUnit>
|
|
{
|
|
protected override void Destroy(GateUnit self)
|
|
{
|
|
self.AccountID = 0;
|
|
self.Kick = false;
|
|
}
|
|
}
|
|
|
|
public static class GateUnitSystem
|
|
{
|
|
#region Address
|
|
|
|
private static void RemoveAddress(this GateUnit self, int routType, long routeId)
|
|
{
|
|
if (self.SceneRoutes.TryGetValue(routType, out var route))
|
|
{
|
|
if (route == routeId)
|
|
{
|
|
self.SceneRoutes.Remove(routType);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void AddAddress(this GateUnit self, int routType, long routeId)
|
|
{
|
|
self.SceneRoutes[routType] = routeId;
|
|
}
|
|
|
|
private static long GetAddress(this GateUnit self, int routType)
|
|
{
|
|
return self.SceneRoutes.GetValueOrDefault(routType, 0);
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
#region 上线
|
|
|
|
public static async FTask<uint> Online(this GateUnit self, params int[] routeType)
|
|
{
|
|
if (routeType != null && routeType.Length > 0)
|
|
{
|
|
foreach (var route in routeType)
|
|
{
|
|
var ret = await self.Online(route);
|
|
if (ret != ErrorCode.Successful) return ret;
|
|
}
|
|
}
|
|
|
|
return ErrorCode.Successful;
|
|
}
|
|
|
|
public static async FTask<uint> Online(this GateUnit self, int routeType)
|
|
{
|
|
Session session = self.Session;
|
|
var routeComponent = session.GetComponent<RouteComponent>();
|
|
if (routeComponent == null)
|
|
{
|
|
routeComponent = session.AddComponent<RouteComponent>();
|
|
}
|
|
|
|
var gameSceneConfig = SceneConfigHelper.GetConfig(routeType);
|
|
var gameRouteId = gameSceneConfig.RouteId;
|
|
//连接到游戏中心服
|
|
var gameResponse = (G2Common_EnterResponse)await self.Scene.NetworkMessagingComponent.CallInnerRoute(
|
|
gameRouteId, new G2Common_EnterRequest()
|
|
{
|
|
AccountId = self.AccountID,
|
|
GateRouteId = session.RuntimeId
|
|
});
|
|
|
|
if (gameResponse.ErrorCode != 0)
|
|
{
|
|
return gameResponse.ErrorCode;
|
|
}
|
|
|
|
routeComponent.AddAddress(routeType, gameRouteId);
|
|
self.AddAddress(routeType, gameRouteId);
|
|
|
|
return ErrorCode.Successful;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 下线
|
|
|
|
public static async FTask<uint> Offline(this GateUnit self, long sessionId, params int[] routeType)
|
|
{
|
|
if (routeType != null && routeType.Length > 0)
|
|
{
|
|
foreach (var route in routeType)
|
|
{
|
|
var ret = await self.Offline(sessionId, route);
|
|
if (ret != ErrorCode.Successful) return ret;
|
|
}
|
|
}
|
|
|
|
return ErrorCode.Successful;
|
|
}
|
|
|
|
public static async FTask<uint> Offline(this GateUnit self, long sessionId, int routeType)
|
|
{
|
|
var sceneRouteId = self.GetAddress(routeType);
|
|
if (sceneRouteId < 1) return ErrorCode.Successful;
|
|
for (int i = 0; i < 10; i++)
|
|
{
|
|
var gameResponse = (Common2G_ExitResponse)await self.Scene.NetworkMessagingComponent.CallInnerRoute(
|
|
sceneRouteId, new G2Common_ExitRequest()
|
|
{
|
|
AccountId = self.AccountID,
|
|
GateRouteId = sessionId
|
|
});
|
|
if (gameResponse.ErrorCode == 0)
|
|
{
|
|
self.RemoveAddress(routeType, sceneRouteId);
|
|
return ErrorCode.Successful;
|
|
}
|
|
}
|
|
|
|
Log.Error($"重试多次还是退出失败,需检查,sessionId={sessionId} route={routeType}");
|
|
return ErrorCode.ErrServer;
|
|
}
|
|
|
|
#endregion
|
|
} |