新增相关协议

This commit is contained in:
2025-08-26 18:03:52 +08:00
parent e0665aae01
commit 7325e268ce
36 changed files with 1402 additions and 1085 deletions

View File

@@ -1,4 +1,10 @@
using Fantasy.Entitas.Interface;
using Fantasy;
using Fantasy.Async;
using Fantasy.Entitas.Interface;
using Fantasy.Network;
using NB.Common;
// ReSharper disable ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
namespace NB.Gate;
@@ -11,6 +17,120 @@ public class GateUnitDestroySystem : DestroySystem<GateUnit>
}
}
public class GateUnitSystem
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
}