提交进入地图和创建房间相关逻辑和协议
This commit is contained in:
@@ -3,10 +3,9 @@ using Fantasy.Async;
|
||||
using Fantasy.Network;
|
||||
using Fantasy.Network.Interface;
|
||||
using Fantasy.Platform.Net;
|
||||
using NB.Common;
|
||||
using NB.Map;
|
||||
|
||||
namespace NB.Gate.Handler;
|
||||
namespace NB.Gate;
|
||||
|
||||
public class C2G_EnterRoomRequestHandler : MessageRPC<C2G_EnterRoomRequest, G2C_EnterRoomResponse>
|
||||
{
|
||||
@@ -41,6 +40,13 @@ public class C2G_EnterRoomRequestHandler : MessageRPC<C2G_EnterRoomRequest, G2C_
|
||||
return;
|
||||
}
|
||||
|
||||
if (gateUnit.RoomCode == request.RoomCode)
|
||||
{
|
||||
response.ErrorCode = ErrorCode.MapRoomEnterRepeated;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
RoomHelper.ParseCode(request.RoomCode, out var serviceId, out var roomId);
|
||||
if (serviceId < 1 || roomId < 1)
|
||||
{
|
||||
@@ -55,9 +61,44 @@ public class C2G_EnterRoomRequestHandler : MessageRPC<C2G_EnterRoomRequest, G2C_
|
||||
return;
|
||||
}
|
||||
|
||||
//先判断是否需要更好地图服务器
|
||||
var mapRouteId = gateUnit.GetAddress(RouteType.MapRoute);
|
||||
//先判断是否需要更换地图服务器
|
||||
var oldServerRouteId = gateUnit.GetAddress(sceneConfig.SceneType);
|
||||
Log.Info($"进入房间信息,mapRouteId={oldServerRouteId}-{sceneConfig.RouteId} serviceId={serviceId}");
|
||||
if (oldServerRouteId != sceneConfig.RouteId)
|
||||
{
|
||||
//先退出旧的服务
|
||||
var ret = await gateUnit.Offline(session.RuntimeId, RouteType.MapRoute);
|
||||
if (ret != 0)
|
||||
{
|
||||
response.ErrorCode = ErrorCode.MapRoomIdError;
|
||||
return;
|
||||
}
|
||||
|
||||
//再进入新的服务
|
||||
ret = await gateUnit.Online(RouteType.MapRoute, sceneConfig);
|
||||
if (ret != 0)
|
||||
{
|
||||
response.ErrorCode = ErrorCode.MapRoomIdError;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
//执行进入房间操作
|
||||
var roomResponse = (Map2G_EnterRoomResponse)await session.Scene.NetworkMessagingComponent.CallInnerRoute(
|
||||
sceneConfig.RouteId, new G2Map_EnterRoomRequest()
|
||||
{
|
||||
AccountId = gateUnit.Id,
|
||||
RoomCode = request.RoomCode,
|
||||
});
|
||||
if (roomResponse.ErrorCode != 0)
|
||||
{
|
||||
response.ErrorCode = ErrorCode.MapRoomIdError;
|
||||
return;
|
||||
}
|
||||
|
||||
gateUnit.RoomCode = request.RoomCode;
|
||||
|
||||
response.RoomCode = request.RoomCode;
|
||||
response.ErrorCode = ErrorCode.Successful;
|
||||
}
|
||||
}
|
||||
82
Hotfix/Gate/Handler/Outer/C2G_ExitRoomRequestHandler.cs
Normal file
82
Hotfix/Gate/Handler/Outer/C2G_ExitRoomRequestHandler.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
using Fantasy;
|
||||
using Fantasy.Async;
|
||||
using Fantasy.Network;
|
||||
using Fantasy.Network.Interface;
|
||||
using Fantasy.Platform.Net;
|
||||
using NB.Map;
|
||||
|
||||
namespace NB.Gate;
|
||||
|
||||
public class C2G_ExitRoomRequestHandler : MessageRPC<C2G_ExitRoomRequest, G2C_ExitRoomResponse>
|
||||
{
|
||||
protected override async FTask Run(Session session, C2G_ExitRoomRequest request, G2C_ExitRoomResponse response,
|
||||
Action reply)
|
||||
{
|
||||
if (string.IsNullOrEmpty(request.RoomCode))
|
||||
{
|
||||
response.ErrorCode = ErrorCode.MapRoomIdError;
|
||||
return;
|
||||
}
|
||||
|
||||
var gateUnitSessionComponent = session.GetComponent<GateUnitSessionComponent>();
|
||||
if (gateUnitSessionComponent == null)
|
||||
{
|
||||
response.ErrorCode = ErrorCode.ErrServer;
|
||||
return;
|
||||
}
|
||||
|
||||
// 在缓存中检查该账号是否存在
|
||||
var gateUnitManageComponent = session.Scene.GetComponent<GateUnitManageComponent>();
|
||||
if (gateUnitManageComponent == null)
|
||||
{
|
||||
response.ErrorCode = ErrorCode.ErrServer;
|
||||
return;
|
||||
}
|
||||
|
||||
var gateUnit = gateUnitManageComponent.Get(gateUnitSessionComponent.AccountID);
|
||||
if (gateUnit == null)
|
||||
{
|
||||
response.ErrorCode = ErrorCode.ErrServer;
|
||||
return;
|
||||
}
|
||||
|
||||
if (gateUnit.RoomCode != request.RoomCode)
|
||||
{
|
||||
response.ErrorCode = ErrorCode.MapRoomIdError;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
RoomHelper.ParseCode(request.RoomCode, out var serviceId, out var roomId);
|
||||
if (serviceId < 1 || roomId < 1)
|
||||
{
|
||||
response.ErrorCode = ErrorCode.MapRoomIdError;
|
||||
return;
|
||||
}
|
||||
|
||||
var sceneConfig = SceneConfigData.Instance.Get(serviceId);
|
||||
if (sceneConfig == null)
|
||||
{
|
||||
response.ErrorCode = ErrorCode.MapRoomIdError;
|
||||
return;
|
||||
}
|
||||
|
||||
//执行退出房间操作
|
||||
var roomResponse = (Map2G_ExiRoomResponse)await session.Scene.NetworkMessagingComponent.CallInnerRoute(
|
||||
sceneConfig.RouteId, new G2Map_ExitRoomRequest()
|
||||
{
|
||||
AccountId = gateUnit.Id,
|
||||
RoomCode = request.RoomCode,
|
||||
});
|
||||
if (roomResponse.ErrorCode != 0)
|
||||
{
|
||||
response.ErrorCode = ErrorCode.MapRoomIdError;
|
||||
return;
|
||||
}
|
||||
|
||||
gateUnit.RoomCode = string.Empty;
|
||||
|
||||
response.RoomCode = request.RoomCode;
|
||||
response.ErrorCode = ErrorCode.Successful;
|
||||
}
|
||||
}
|
||||
@@ -62,10 +62,6 @@ public static class GateLoginHelper
|
||||
//通知服务器下线
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using Fantasy.Async;
|
||||
using Fantasy.Entitas.Interface;
|
||||
using Fantasy.Network;
|
||||
using Fantasy.Platform.Net;
|
||||
using NB.Common;
|
||||
|
||||
// ReSharper disable ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
|
||||
@@ -13,7 +14,9 @@ public class GateUnitDestroySystem : DestroySystem<GateUnit>
|
||||
protected override void Destroy(GateUnit self)
|
||||
{
|
||||
self.AccountID = 0;
|
||||
self.Kick = false;
|
||||
self.Region = 0;
|
||||
self.SceneRoutes.Clear();
|
||||
self.RoomCode = string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,15 +24,9 @@ public static class GateUnitSystem
|
||||
{
|
||||
#region Address
|
||||
|
||||
public static void RemoveAddress(this GateUnit self, int routType, long routeId)
|
||||
public static void RemoveAddress(this GateUnit self, int routType)
|
||||
{
|
||||
if (self.SceneRoutes.TryGetValue(routType, out var route))
|
||||
{
|
||||
if (route == routeId)
|
||||
{
|
||||
self.SceneRoutes.Remove(routType);
|
||||
}
|
||||
}
|
||||
self.SceneRoutes.Remove(routType);
|
||||
}
|
||||
|
||||
public static void AddAddress(this GateUnit self, int routType, long routeId)
|
||||
@@ -61,7 +58,7 @@ public static class GateUnitSystem
|
||||
return ErrorCode.Successful;
|
||||
}
|
||||
|
||||
public static async FTask<uint> Online(this GateUnit self, int routeType)
|
||||
public static async FTask<uint> Online(this GateUnit self, int routeType, SceneConfig? sceneConfig = null)
|
||||
{
|
||||
Session session = self.Session;
|
||||
var routeComponent = session.GetComponent<RouteComponent>();
|
||||
@@ -69,9 +66,13 @@ public static class GateUnitSystem
|
||||
{
|
||||
routeComponent = session.AddComponent<RouteComponent>();
|
||||
}
|
||||
|
||||
var gameSceneConfig = SceneConfigHelper.GetConfigByRouteType(routeType);
|
||||
var gameRouteId = gameSceneConfig.RouteId;
|
||||
|
||||
if (sceneConfig == null)
|
||||
{
|
||||
sceneConfig = SceneConfigHelper.GetConfigByRouteType(routeType);
|
||||
}
|
||||
|
||||
var gameRouteId = sceneConfig.RouteId;
|
||||
|
||||
var gameResponse = (G2Common_EnterResponse)await self.Scene.NetworkMessagingComponent.CallInnerRoute(
|
||||
gameRouteId, new G2Common_EnterRequest()
|
||||
@@ -85,8 +86,10 @@ public static class GateUnitSystem
|
||||
return gameResponse.ErrorCode;
|
||||
}
|
||||
|
||||
routeComponent.RemoveAddress(routeType);
|
||||
routeComponent.AddAddress(routeType, gameResponse.UnitRouteId);
|
||||
self.AddAddress(routeType, gameResponse.UnitRouteId);
|
||||
self.AddAddress(sceneConfig.SceneType, gameRouteId);
|
||||
|
||||
return ErrorCode.Successful;
|
||||
}
|
||||
@@ -111,7 +114,8 @@ public static class GateUnitSystem
|
||||
|
||||
public static async FTask<uint> Offline(this GateUnit self, long sessionId, int routeType)
|
||||
{
|
||||
var sceneRouteId = self.GetAddress(routeType);
|
||||
var sceneConfig = SceneConfigHelper.GetConfigByRouteType(routeType);
|
||||
var sceneRouteId = sceneConfig.RouteId;
|
||||
if (sceneRouteId < 1) return ErrorCode.Successful;
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
@@ -123,7 +127,9 @@ public static class GateUnitSystem
|
||||
});
|
||||
if (gameResponse.ErrorCode == 0)
|
||||
{
|
||||
self.RemoveAddress(routeType, sceneRouteId);
|
||||
//TODO:这里需要清理掉记录的数据
|
||||
self.RemoveAddress(routeType);
|
||||
self.RemoveAddress(sceneConfig.SceneType);
|
||||
return ErrorCode.Successful;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,6 +39,9 @@ public class C2Map_CreateRoomRequestHandler : RouteRPC<MapUnit, C2Map_CreateRoom
|
||||
roomManageComponent.Add(room);
|
||||
Log.Info(
|
||||
$"创建房间=== sId={entity.Scene.SceneConfigId} map:{request.MapId} id={room.RoomId} code={room.Code}");
|
||||
|
||||
response.RoomCode = room.Code;
|
||||
|
||||
await FTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -15,19 +15,7 @@ public class C2Map_EnterMapRequestHandler : RouteRPC<MapUnit, C2Map_EnterMapRequ
|
||||
response.ErrorCode = ErrorCode.ErrServer;
|
||||
return;
|
||||
}
|
||||
|
||||
// if (entity.RoomId > 0)
|
||||
// {
|
||||
// response.ErrorCode = ErrorCode.MapRoomHave;
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// roomManageComponent.Enter(entity, request.RoomId);
|
||||
|
||||
Log.Info($"进入地图=== map:{request.MapId}");
|
||||
entity.MapId = request.MapId;
|
||||
response.MapId = request.MapId;
|
||||
|
||||
await FTask.CompletedTask;
|
||||
response.ErrorCode = await entity.EnterMap(request.MapId);
|
||||
}
|
||||
}
|
||||
@@ -15,13 +15,48 @@ public class G2Map_EnterRequestHandler : RouteRPC<Scene, G2Map_EnterRoomRequest,
|
||||
response.ErrorCode = ErrorCode.ErrServer;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
var mapUnitManage = entity.GetComponent<MapUnitManageComponent>();
|
||||
if (mapUnitManage == null)
|
||||
{
|
||||
response.ErrorCode = ErrorCode.ErrServer;
|
||||
return;
|
||||
}
|
||||
|
||||
var mapUnit = mapUnitManage.Get(request.AccountId);
|
||||
if (mapUnit == null)
|
||||
{
|
||||
response.ErrorCode = ErrorCode.ErrServer;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
RoomHelper.ParseCode(request.RoomCode, out var serviceId, out var roomId);
|
||||
if (serviceId < 1 || roomId < 1)
|
||||
{
|
||||
response.ErrorCode = ErrorCode.MapRoomIdError;
|
||||
return;
|
||||
}
|
||||
// roomManageComponent.Enter();
|
||||
|
||||
var room = roomManageComponent.Get(roomId);
|
||||
if (room == null)
|
||||
{
|
||||
response.ErrorCode = ErrorCode.MapRoomIdError;
|
||||
return;
|
||||
}
|
||||
|
||||
if (mapUnit.MapId != room.Map)
|
||||
{
|
||||
//切换地图
|
||||
response.ErrorCode = await mapUnit.EnterMap(mapUnit.MapId);
|
||||
if (response.ErrorCode != 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
response.ErrorCode = await room.Enter(request.AccountId);
|
||||
response.RoomCode = room.Code;
|
||||
}
|
||||
}
|
||||
20
Hotfix/Map/Helper/MapFactory.cs
Normal file
20
Hotfix/Map/Helper/MapFactory.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using Fantasy;
|
||||
using Fantasy.Entitas;
|
||||
|
||||
namespace NB.Map;
|
||||
|
||||
public class MapFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// 创建一个地图
|
||||
/// </summary>
|
||||
/// <param name="scene"></param>
|
||||
/// <param name="mapId"></param>
|
||||
/// <returns></returns>
|
||||
public static Map? Create(Scene scene, int mapId)
|
||||
{
|
||||
var map = Entity.Create<Map>(scene, true, true);
|
||||
map.MapId = mapId;
|
||||
return map;
|
||||
}
|
||||
}
|
||||
61
Hotfix/Map/System/MapManageComponentSystem.cs
Normal file
61
Hotfix/Map/System/MapManageComponentSystem.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using Fantasy.Entitas;
|
||||
using Fantasy.Entitas.Interface;
|
||||
|
||||
namespace NB.Map;
|
||||
|
||||
public class MapManageComponentDestroySystem : DestroySystem<MapManageComponent>
|
||||
{
|
||||
protected override void Destroy(MapManageComponent self)
|
||||
{
|
||||
foreach (var (_, map) in self.Maps)
|
||||
{
|
||||
map.Dispose();
|
||||
}
|
||||
|
||||
self.Maps.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
public class MapManageComponentAwakeSystem : AwakeSystem<MapManageComponent>
|
||||
{
|
||||
protected override void Awake(MapManageComponent self)
|
||||
{
|
||||
//初始化所有地图
|
||||
for (int i = 1; i < 100; i++)
|
||||
{
|
||||
//初始化100个地图
|
||||
var map = MapFactory.Create(self.Scene, i);
|
||||
if (map != null)
|
||||
{
|
||||
self.Add(map);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class MapManageComponentSystem
|
||||
{
|
||||
#region 增删
|
||||
|
||||
public static void Add(this MapManageComponent self, Map map)
|
||||
{
|
||||
self.Maps[map.MapId] = map;
|
||||
}
|
||||
|
||||
public static bool Remove(this MapManageComponent self, int mapId)
|
||||
{
|
||||
if (self.Maps.TryGetValue(mapId, out var map))
|
||||
{
|
||||
map.Dispose();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static Map? Get(this MapManageComponent self, int mapId)
|
||||
{
|
||||
return self.Maps.GetValueOrDefault(mapId);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
using Fantasy.Entitas.Interface;
|
||||
using Fantasy;
|
||||
using Fantasy.Async;
|
||||
using Fantasy.Entitas.Interface;
|
||||
|
||||
namespace NB.Map;
|
||||
|
||||
@@ -16,4 +18,9 @@ public class MapRoomDestroySystem : DestroySystem<MapRoom>
|
||||
|
||||
public static class MapRoomSystem
|
||||
{
|
||||
public static async FTask<uint> Enter(this MapRoom self, long unitId)
|
||||
{
|
||||
await FTask.CompletedTask;
|
||||
return ErrorCode.Successful;
|
||||
}
|
||||
}
|
||||
16
Hotfix/Map/System/MapSystem.cs
Normal file
16
Hotfix/Map/System/MapSystem.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using Fantasy.Entitas.Interface;
|
||||
|
||||
namespace NB.Map;
|
||||
|
||||
public class MapDestroySystem : DestroySystem<Map>
|
||||
{
|
||||
protected override void Destroy(Map self)
|
||||
{
|
||||
self.MapId = 0;
|
||||
self.Units.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
public static class MapSystem
|
||||
{
|
||||
}
|
||||
38
Hotfix/Map/System/MapUnitSystem.cs
Normal file
38
Hotfix/Map/System/MapUnitSystem.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using Fantasy;
|
||||
using Fantasy.Async;
|
||||
using Fantasy.Entitas.Interface;
|
||||
using Unity.Mathematics;
|
||||
|
||||
namespace NB.Map;
|
||||
|
||||
public class MapUnitSystemDestroySystem : DestroySystem<MapUnit>
|
||||
{
|
||||
protected override void Destroy(MapUnit self)
|
||||
{
|
||||
self.MapId = 0;
|
||||
self.Position = float3.zero;
|
||||
self.Rotation = float4.zero;
|
||||
self.GateRouteId = 0L;
|
||||
}
|
||||
}
|
||||
|
||||
public static class MapUnitSystem
|
||||
{
|
||||
public static async FTask<uint> EnterMap(this MapUnit self, int mapId)
|
||||
{
|
||||
var mapManageComponent = self.Scene.GetComponent<MapManageComponent>();
|
||||
|
||||
var map = mapManageComponent.Get(mapId);
|
||||
if (map == null)
|
||||
{
|
||||
return ErrorCode.MapIdError;
|
||||
}
|
||||
|
||||
// map.en
|
||||
|
||||
await FTask.CompletedTask;
|
||||
|
||||
|
||||
return ErrorCode.Successful;
|
||||
}
|
||||
}
|
||||
@@ -46,7 +46,10 @@ public static class RoomManageComponentSystem
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static MapRoom? Get(this RoomManageComponent self, int roomId)
|
||||
{
|
||||
return self.Rooms.GetValueOrDefault(roomId);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 房间Id
|
||||
@@ -79,15 +82,5 @@ public static class RoomManageComponentSystem
|
||||
|
||||
#endregion
|
||||
|
||||
#region 进入退出
|
||||
|
||||
public static void Enter(this RoomManageComponent self, MapUnit unit, long roomId)
|
||||
{
|
||||
}
|
||||
|
||||
public static void Exit(this RoomManageComponent self, MapUnit unit)
|
||||
{
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user