新增相关协议

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

@@ -0,0 +1,66 @@
using Fantasy;
using Fantasy.Async;
using Fantasy.Network.Interface;
using NB.Chat;
using NB.Game;
namespace NB.Common;
public class G2Common_EnterRequestHandler : RouteRPC<Scene, G2Common_EnterRequest, G2Common_EnterResponse>
{
protected override async FTask Run(Scene scene, G2Common_EnterRequest request, G2Common_EnterResponse response,
Action reply)
{
response.RouteType = request.RouteType;
switch (scene.SceneType)
{
case SceneType.Game:
{
await RunGame(scene, request, response);
break;
}
case SceneType.Social:
{
await RunSocial(scene, request, response);
break;
}
case SceneType.Map:
{
break;
}
}
await FTask.CompletedTask;
}
private async FTask RunGame(Scene scene, G2Common_EnterRequest request, G2Common_EnterResponse response)
{
// 在缓存中检查该账号是否存在
var gameAccountManageComponent = scene.GetComponent<PlayerManageComponent>();
var account = await gameAccountManageComponent.Online(scene, request.AccountId, request.GateRouteId);
if (account == null)
{
response.ErrorCode = ErrorCode.ErrServer;
return;
}
response.RoleRouteId = account.RuntimeId;
}
private async FTask RunSocial(Scene scene, G2Common_EnterRequest request, G2Common_EnterResponse response)
{
// 在缓存中检查该账号是否存在
var chatUnitManageComponent = scene.GetComponent<SocialUnitManageComponent>();
var account = await chatUnitManageComponent.Online(scene, request.AccountId, request.GateRouteId);
if (account == null)
{
response.ErrorCode = ErrorCode.ErrServer;
return;
}
response.RoleRouteId = account.RuntimeId;
}
}

View File

@@ -0,0 +1,55 @@
using Fantasy;
using Fantasy.Async;
using Fantasy.Network.Interface;
using NB.Chat;
namespace NB.Common;
public class G2Common_ExitRequestHandler : RouteRPC<Scene, G2Common_ExitRequest, Common2G_ExitResponse>
{
protected override async FTask Run(Scene scene, G2Common_ExitRequest request, Common2G_ExitResponse response,
Action reply)
{
switch (scene.SceneType)
{
case SceneType.Game:
{
await RunGame(scene, request, response);
break;
}
case SceneType.Social:
{
await RunSocial(scene, request, response);
break;
}
case SceneType.Map:
{
break;
}
}
await FTask.CompletedTask;
}
private async FTask RunGame(Scene scene, G2Common_ExitRequest request, Common2G_ExitResponse response)
{
// // 在缓存中检查该账号是否存在
// var gameAccountManageComponent = scene.GetComponent<PlayerManageComponent>();
// var account = await gameAccountManageComponent.Online(scene, request.AccountId, request.GateRouteId);
// if (account == null)
// {
// response.ErrorCode = ErrorCode.ErrServer;
// return;
// }
//
// response.RoleRouteId = account.RuntimeId;
}
private async FTask RunSocial(Scene scene, G2Common_ExitRequest request, Common2G_ExitResponse response)
{
// 在缓存中检查该账号是否存在
var chatUnitManageComponent = scene.GetComponent<SocialUnitManageComponent>();
await chatUnitManageComponent.Offline(scene, request.AccountId, request.GateRouteId);
}
}

View File

@@ -0,0 +1,9 @@
using Fantasy;
using Fantasy.Async;
namespace NB.Common;
public static class LoginHelper
{
}

View File

@@ -0,0 +1,14 @@
using Fantasy;
using Fantasy.Platform.Net;
namespace NB.Common;
public class SceneConfigHelper
{
public static SceneConfig GetConfig(int sceneType)
{
var gameSceneConfigs = SceneConfigData.Instance.GetSceneBySceneType(sceneType);
return gameSceneConfigs.First();
}
}

View File

@@ -1,5 +1,6 @@
using Fantasy;
using Fantasy.Async;
using NB.Common;
namespace NB.Game;
@@ -13,7 +14,7 @@ public static class CacheHandler
/// <returns></returns>
public static async FTask<List<RoleSimpleInfo>> GetPlayerBasicCacheInfos(Scene scene, List<long> id)
{
var gameSceneConfig = GameSceneHelper.GetSceneConfig();
var gameSceneConfig = SceneConfigHelper.GetConfig(SceneType.Game);
var gameRouteId = gameSceneConfig.RouteId;
//连接到游戏中心服
var gameResponse = (G2S_GetPlayerBasicInfoResponse)await scene.NetworkMessagingComponent.CallInnerRoute(

View File

@@ -1,29 +1,29 @@
using Fantasy;
using Fantasy.Async;
using Fantasy.Entitas;
using Fantasy.Helper;
using Fantasy.Network;
using Fantasy.Network.Interface;
using NB;
using NB.Gate;
namespace NB.Game;
public class G2Game_EnterRequestHandler : RouteRPC<Scene, G2Game_EnterRequest, Game2G_EnterResponse>
{
protected override async FTask Run(Scene scene, G2Game_EnterRequest request, Game2G_EnterResponse response,
Action reply)
{
Log.Debug("收到 G2Game_EnterRequestHandler");
// 在缓存中检查该账号是否存在
var gameAccountManageComponent = scene.GetComponent<PlayerManageComponent>();
var account = await gameAccountManageComponent.Online(scene, request.AccountId, request.GateRouteId);
response.RoleRouteId = account.RuntimeId;
response.RoleInfo = account.GetRoleSimpleInfo();
await FTask.CompletedTask;
}
}
// using Fantasy;
// using Fantasy.Async;
// using Fantasy.Entitas;
// using Fantasy.Helper;
// using Fantasy.Network;
// using Fantasy.Network.Interface;
// using NB;
// using NB.Gate;
//
// namespace NB.Game;
//
// public class G2Game_EnterRequestHandler : RouteRPC<Scene, G2Game_EnterRequest, Game2G_EnterResponse>
// {
// protected override async FTask Run(Scene scene, G2Game_EnterRequest request, Game2G_EnterResponse response,
// Action reply)
// {
// Log.Debug("收到 G2Game_EnterRequestHandler");
//
//
// // 在缓存中检查该账号是否存在
// var gameAccountManageComponent = scene.GetComponent<PlayerManageComponent>();
//
// var account = await gameAccountManageComponent.Online(scene, request.AccountId, request.GateRouteId);
//
// response.RoleRouteId = account.RuntimeId;
// response.RoleInfo = account.GetRoleSimpleInfo();
// await FTask.CompletedTask;
// }
// }

View File

@@ -1,48 +0,0 @@
using System.Net;
using Fantasy;
using Fantasy.Async;
using Fantasy.Network;
using Fantasy.Platform.Net;
namespace NB.Game;
public static class GameSceneHelper
{
#region 线 线
public static SceneConfig GetSceneConfig()
{
var gameSceneConfigs = SceneConfigData.Instance.GetSceneBySceneType(SceneType.Game);
return gameSceneConfigs.First();
}
public static async FTask<(long, RoleSimpleInfo?)> Online(Scene scene, long accountID, long gateRuntimeId)
{
var gameSceneConfig = GetSceneConfig();
var gameRouteId = gameSceneConfig.RouteId;
//连接到游戏中心服
var gameResponse = (Game2G_EnterResponse)await scene.NetworkMessagingComponent.CallInnerRoute(
gameRouteId, new G2Game_EnterRequest()
{
AccountId = accountID,
GateRouteId = gateRuntimeId
});
if (gameResponse.ErrorCode != 0)
{
return (0, null);
}
return (gameResponse.RoleRouteId, gameResponse.RoleInfo);
}
public static async FTask Offline(Scene scene, long accountId, long gateRuntimeId)
{
}
#endregion
}

View File

@@ -42,7 +42,8 @@ public static class PlayerManageComponentSystem
/// <param name="self"></param>
/// <param name="scene"></param>
/// <param name="accountId"></param>
public static async FTask<Player> Online(this PlayerManageComponent self, Scene scene, long accountId,
/// <param name="gateRouteId"></param>
public static async FTask<Player?> Online(this PlayerManageComponent self, Scene scene, long accountId,
long gateRouteId)
{
Log.Debug("检查账号是否在缓存中");

View File

@@ -61,7 +61,7 @@ public static class PlayerHelper
/// <param name="scene"></param>
/// <param name="accountId">账号Id</param>
/// <returns></returns>
public static async FTask<Player> LoadDataBase(Scene scene, long accountId)
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)

View File

@@ -42,32 +42,5 @@ public sealed class C2G_LoginRequestHandler : MessageRPC<C2G_LoginRequest, G2C_L
response.ErrorCode = await GateLoginHelper.Online(gateUnit);
Log.Debug($"当前的Gate服务器:{session.Scene.SceneConfigId} accountId:{accountId}");
// var gameSceneConfig = GameSceneHelper.GetSceneConfig(session);
//
// // 通过chatSceneConfig拿到这个Scene的RouteId
// var gameRouteId = gameSceneConfig.RouteId;
// //连接到游戏中心服
// var gameResponse = (Game2G_EnterResponse)await session.Scene.NetworkMessagingComponent.CallInnerRoute(
// gameRouteId, new G2Game_EnterRequest()
// {
// AccountId = accountId,
// GateRouteId = session.RuntimeId
// });
//
// if (gameResponse.ErrorCode != 0)
// {
// // 如果ErrorCode不是0表示请求的协议发生错误应该提示给客户端。
// response.ErrorCode = gameResponse.ErrorCode;
// return;
// }
//
// // 要实现Route协议的转发需要给Session添加一个RouteComponent这个非常重要。
// var routeComponent = session.AddComponent<RouteComponent>();
// routeComponent.AddAddress(RouteType.GameRoute, gameResponse.RoleRouteId);
//
// // 给当前Session添加一个组件当Session销毁的时候会销毁这个组件。
// var accountFlagComponent = session.AddComponent<SessionPlayerComponent>();
// accountFlagComponent.AccountID = accountId;
}
}

View File

@@ -5,6 +5,8 @@ using Fantasy.Platform.Net;
using NB.Chat;
using NB.Game;
// ReSharper disable ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
namespace NB.Gate;
public static class GateLoginHelper
@@ -37,43 +39,13 @@ public static class GateLoginHelper
var routeComponent = session.GetComponent<RouteComponent>();
if (routeComponent == null)
{
routeComponent = session.AddComponent<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 SocialSceneHelper.Online(session.Scene, roleSimpleInfo, session.RuntimeId);
if (chatRouteId <= 0)
{
return ErrorCode.OnlineSceneFailed;
}
routeComponent.AddAddress(RouteType.SocialRoute, chatRouteId);
gateUnit.ChatSceneRouteId = sceneRouteId;
Log.Info($"连接聊天服成功gameRouteId:{gameRouteId}");
var gameSceneConfigs = SceneConfigData.Instance.GetSceneBySceneType(SceneType.Map);
var mapSceneConfig = gameSceneConfigs.First();
routeComponent.AddAddress(RouteType.MapRoute, mapSceneConfig.RouteId);
return ErrorCode.Successful;
return await gateUnit.Online(RouteType.GameRoute, RouteType.SocialRoute, RouteType.MapRoute);
}
#endregion
@@ -88,11 +60,13 @@ public static class GateLoginHelper
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 SocialSceneHelper.Offline(gateUnit.Scene, gateUnit.AccountID, sessionId,
gateUnit.ChatSceneRouteId);
return ErrorCode.Successful;
Log.Info($"断线的session id={sessionId}");
var ret = await gateUnit.Offline(sessionId, RouteType.GameRoute, RouteType.SocialRoute, RouteType.MapRoute);
// await GameSceneHelper.Offline(gateUnit.Scene, gateUnit.AccountID, sessionId);
// await SocialSceneHelper.Offline(gateUnit.Scene, gateUnit.AccountID, sessionId,
// gateUnit.ChatSceneRouteId);
return ret;
}
#endregion

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
}

View File

@@ -13,8 +13,10 @@
</ItemGroup>
<ItemGroup>
<Folder Include="Game\Helper\" />
<Folder Include="Map\Helper\" />
<Folder Include="Map\System\" />
<Folder Include="Room\" />
<Folder Include="Social\Chat\" />
<Folder Include="Social\Club\" />
<Folder Include="Social\Mail\Handler\Inner\" />

View File

@@ -1,15 +1,15 @@
using Fantasy;
using Fantasy.Async;
using Fantasy.Network.Interface;
namespace NB.Map;
public class C2Map_CreateRoomRequestHandler : RouteRPC<Scene, C2Map_CreateRoomRequest, Map2C_CreateRoomResponse>
{
protected override async FTask Run(Scene entity, C2Map_CreateRoomRequest request, Map2C_CreateRoomResponse response,
Action reply)
{
Log.Info($"创建房间=== map:{request.MapId} pass:{request.Password}");
await FTask.CompletedTask;
}
}
// using Fantasy;
// using Fantasy.Async;
// using Fantasy.Network.Interface;
//
// namespace NB.Map;
//
// public class C2Map_CreateRoomRequestHandler : RouteRPC<Scene, C2Map_CreateRoomRequest, Map2C_CreateRoomResponse>
// {
// protected override async FTask Run(Scene entity, C2Map_CreateRoomRequest request, Map2C_CreateRoomResponse response,
// Action reply)
// {
// Log.Info($"创建房间=== map:{request.MapId} pass:{request.Password}");
// await FTask.CompletedTask;
// }
// }

View File

@@ -1,22 +0,0 @@
using Fantasy;
using Fantasy.Async;
using Fantasy.Network.Interface;
using NB.Game;
namespace NB.Chat;
public class G2S_EnterRequestHandler : RouteRPC<Scene, G2S_EnterRequest, S2G_EnterResponse>
{
protected override async FTask Run(Scene scene, G2S_EnterRequest request, S2G_EnterResponse response,
Action reply)
{
var roleId = request.Role.RoleId;
Log.Debug($"收到 G2S_EnterRequestHandler {roleId}");
// 在缓存中检查该账号是否存在
var chatUnitManageComponent = scene.GetComponent<SocialUnitManageComponent>();
var account = await chatUnitManageComponent.Online(scene, request.Role, request.GateRouteId);
response.RoleRouteId = account.RuntimeId;
}
}

View File

@@ -1,16 +0,0 @@
using Fantasy;
using Fantasy.Async;
using Fantasy.Network.Interface;
namespace NB.Chat;
public class G2S_ExitRequestHandler : RouteRPC<Scene, G2S_ExitRequest, S2G_ExitResponse>
{
protected override async FTask Run(Scene scene, G2S_ExitRequest request, S2G_ExitResponse response,
Action reply)
{
// 在缓存中检查该账号是否存在
var chatUnitManageComponent = scene.GetComponent<SocialUnitManageComponent>();
await chatUnitManageComponent.Offline(scene, request.AccountId, request.GateRouteId);
}
}

View File

@@ -122,52 +122,51 @@ public static class SocialSceneHelper
#region 线线
public static async FTask<(long, long)> Online(Scene scene, RoleSimpleInfo roleSimple, long gateRuntimeId)
{
var gameSceneConfig = GetSceneConfig();
var gameRouteId = gameSceneConfig.RouteId;
//连接到游戏中心服
var gameResponse = (S2G_EnterResponse)await scene.NetworkMessagingComponent.CallInnerRoute(
gameRouteId, new G2S_EnterRequest()
{
Role = roleSimple,
GateRouteId = gateRuntimeId
});
if (gameResponse.ErrorCode != 0)
{
return (0, 0);
}
return (gameResponse.RoleRouteId, gameRouteId);
// return gameRouteId;
}
public static async FTask Offline(Scene scene, long accountId, long gateRuntimeId, long sceneRouteId)
{
for (int i = 0; i < 10; i++)
{
var gameResponse = (S2G_ExitResponse)await scene.NetworkMessagingComponent.CallInnerRoute(
sceneRouteId, new G2S_ExitRequest()
{
AccountId = accountId,
GateRouteId = gateRuntimeId
});
if (gameResponse.ErrorCode == 0)
{
return;
}
}
Log.Error("重试多次还是退出失败,需检查");
}
private static SceneConfig GetSceneConfig()
{
var gameSceneConfigs = SceneConfigData.Instance.GetSceneBySceneType(SceneType.Social);
return gameSceneConfigs.First();
}
// public static async FTask<(long, long)> Online(Scene scene, RoleSimpleInfo roleSimple, long gateRuntimeId)
// {
// var gameSceneConfig = GetSceneConfig();
// var gameRouteId = gameSceneConfig.RouteId;
// //连接到游戏中心服
// var gameResponse = (S2G_EnterResponse)await scene.NetworkMessagingComponent.CallInnerRoute(
// gameRouteId, new G2S_EnterRequest()
// {
// Role = roleSimple,
// GateRouteId = gateRuntimeId
// });
//
// if (gameResponse.ErrorCode != 0)
// {
// return (0, 0);
// }
//
// return (gameResponse.RoleRouteId, gameRouteId);
// }
//
// public static async FTask Offline(Scene scene, long accountId, long gateRuntimeId, long sceneRouteId)
// {
// for (int i = 0; i < 10; i++)
// {
// var gameResponse = (S2G_ExitResponse)await scene.NetworkMessagingComponent.CallInnerRoute(
// sceneRouteId, new G2S_ExitRequest()
// {
// AccountId = accountId,
// GateRouteId = gateRuntimeId
// });
// if (gameResponse.ErrorCode == 0)
// {
// return;
// }
// }
//
// Log.Error("重试多次还是退出失败,需检查");
// }
//
// private static SceneConfig GetSceneConfig()
// {
// var gameSceneConfigs = SceneConfigData.Instance.GetSceneBySceneType(SceneType.Social);
//
// return gameSceneConfigs.First();
// }
#endregion
}

View File

@@ -29,13 +29,13 @@ public static class ChatUnitManageComponentSystem
/// </summary>
/// <param name="self"></param>
/// <param name="scene"></param>
/// <param name="roleSimpleInfo"></param>
/// <param name="accountId"></param>
/// <param name="gateRouteId"></param>
public static async FTask<SocialUnit?> Online(this SocialUnitManageComponent self, Scene scene,
RoleSimpleInfo roleSimpleInfo,
long accountId,
long gateRouteId)
{
var accountId = roleSimpleInfo.RoleId;
// var accountId = roleSimpleInfo.RoleId;
if (!self.TryGet(accountId, out var account))
{
account = ChatUnitFactory.Create(scene, accountId);
@@ -46,7 +46,7 @@ public static class ChatUnitManageComponentSystem
{
await account.TryComponent<MailComponent>();
account.GateRouteId = gateRouteId;
account.Role = roleSimpleInfo;
// account.Role = roleSimpleInfo;
}
await FTask.CompletedTask;