房间创建和进入相关逻辑和协议
This commit is contained in:
@@ -3,6 +3,7 @@ using Fantasy.Async;
|
||||
using Fantasy.Network.Interface;
|
||||
using NB.Chat;
|
||||
using NB.Game;
|
||||
using NB.Map;
|
||||
|
||||
namespace NB.Common;
|
||||
|
||||
@@ -28,6 +29,7 @@ public class G2Common_EnterRequestHandler : RouteRPC<Scene, G2Common_EnterReques
|
||||
}
|
||||
case SceneType.Map:
|
||||
{
|
||||
await RunMap(scene, request, response);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -39,14 +41,15 @@ public class G2Common_EnterRequestHandler : RouteRPC<Scene, G2Common_EnterReques
|
||||
{
|
||||
// 在缓存中检查该账号是否存在
|
||||
var gameAccountManageComponent = scene.GetComponent<PlayerManageComponent>();
|
||||
var account = await gameAccountManageComponent.Online(scene, request.AccountId, request.GateRouteId);
|
||||
var account = await gameAccountManageComponent.Online(request.AccountId, request.GateRouteId);
|
||||
if (account == null)
|
||||
{
|
||||
response.ErrorCode = ErrorCode.ErrServer;
|
||||
return;
|
||||
}
|
||||
|
||||
response.RoleRouteId = account.RuntimeId;
|
||||
response.UnitRouteId = account.RuntimeId;
|
||||
Log.Info($"登录到游戏服成功,id={response.UnitRouteId}");
|
||||
}
|
||||
|
||||
private async FTask RunSocial(Scene scene, G2Common_EnterRequest request, G2Common_EnterResponse response)
|
||||
@@ -61,6 +64,23 @@ public class G2Common_EnterRequestHandler : RouteRPC<Scene, G2Common_EnterReques
|
||||
return;
|
||||
}
|
||||
|
||||
response.RoleRouteId = account.RuntimeId;
|
||||
response.UnitRouteId = account.RuntimeId;
|
||||
Log.Info($"登录到社交服成功,id={response.UnitRouteId}");
|
||||
}
|
||||
|
||||
private async FTask RunMap(Scene scene, G2Common_EnterRequest request, G2Common_EnterResponse response)
|
||||
{
|
||||
// 在缓存中检查该账号是否存在
|
||||
var chatUnitManageComponent = scene.GetComponent<MapUnitManageComponent>();
|
||||
var account = await chatUnitManageComponent.Online(scene, request.AccountId, request.GateRouteId);
|
||||
|
||||
if (account == null)
|
||||
{
|
||||
response.ErrorCode = ErrorCode.ErrServer;
|
||||
return;
|
||||
}
|
||||
|
||||
response.UnitRouteId = account.RuntimeId;
|
||||
Log.Info($"登录到地图服成功,id={response.UnitRouteId}");
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,8 @@
|
||||
using Fantasy.Async;
|
||||
using Fantasy.Network.Interface;
|
||||
using NB.Chat;
|
||||
using NB.Game;
|
||||
using NB.Map;
|
||||
|
||||
namespace NB.Common;
|
||||
|
||||
@@ -25,6 +27,7 @@ public class G2Common_ExitRequestHandler : RouteRPC<Scene, G2Common_ExitRequest,
|
||||
}
|
||||
case SceneType.Map:
|
||||
{
|
||||
await RunMap(scene, request, response);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -34,16 +37,10 @@ public class G2Common_ExitRequestHandler : RouteRPC<Scene, G2Common_ExitRequest,
|
||||
|
||||
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;
|
||||
// 在缓存中检查该账号是否存在
|
||||
var gameAccountManageComponent = scene.GetComponent<PlayerManageComponent>();
|
||||
await gameAccountManageComponent.Offline(request.AccountId, request.GateRouteId);
|
||||
Log.Info("退出游戏服成功==");
|
||||
}
|
||||
|
||||
private async FTask RunSocial(Scene scene, G2Common_ExitRequest request, Common2G_ExitResponse response)
|
||||
@@ -51,5 +48,14 @@ public class G2Common_ExitRequestHandler : RouteRPC<Scene, G2Common_ExitRequest,
|
||||
// 在缓存中检查该账号是否存在
|
||||
var chatUnitManageComponent = scene.GetComponent<SocialUnitManageComponent>();
|
||||
await chatUnitManageComponent.Offline(scene, request.AccountId, request.GateRouteId);
|
||||
Log.Info("退出聊天服成功==");
|
||||
}
|
||||
|
||||
private async FTask RunMap(Scene scene, G2Common_ExitRequest request, Common2G_ExitResponse response)
|
||||
{
|
||||
// 在缓存中检查该账号是否存在
|
||||
var chatUnitManageComponent = scene.GetComponent<MapUnitManageComponent>();
|
||||
await chatUnitManageComponent.Offline(scene, request.AccountId, request.GateRouteId);
|
||||
Log.Info("退出房间服成功==");
|
||||
}
|
||||
}
|
||||
@@ -3,12 +3,38 @@ using Fantasy.Platform.Net;
|
||||
|
||||
namespace NB.Common;
|
||||
|
||||
public class SceneConfigHelper
|
||||
public static class SceneConfigHelper
|
||||
{
|
||||
public static SceneConfig GetConfigByRouteType(int routeType)
|
||||
{
|
||||
var sceneType = SceneType.Game;
|
||||
if (routeType == RouteType.GameRoute)
|
||||
{
|
||||
sceneType = SceneType.Game;
|
||||
}
|
||||
else if (routeType == RouteType.SocialRoute)
|
||||
{
|
||||
sceneType = SceneType.Social;
|
||||
}
|
||||
else if (routeType == RouteType.MapRoute)
|
||||
{
|
||||
sceneType = SceneType.Map;
|
||||
}
|
||||
else if (routeType == RouteType.GateRoute)
|
||||
{
|
||||
sceneType = SceneType.Gate;
|
||||
}
|
||||
|
||||
return GetConfig(sceneType);
|
||||
}
|
||||
|
||||
public static SceneConfig GetConfig(int sceneType)
|
||||
{
|
||||
|
||||
var gameSceneConfigs = SceneConfigData.Instance.GetSceneBySceneType(sceneType);
|
||||
|
||||
return gameSceneConfigs.First();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
// 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;
|
||||
// }
|
||||
// }
|
||||
@@ -40,24 +40,22 @@ public static class PlayerManageComponentSystem
|
||||
/// 玩家上线
|
||||
/// </summary>
|
||||
/// <param name="self"></param>
|
||||
/// <param name="scene"></param>
|
||||
/// <param name="accountId"></param>
|
||||
/// <param name="gateRouteId"></param>
|
||||
public static async FTask<Player?> Online(this PlayerManageComponent self, Scene scene, long accountId,
|
||||
long gateRouteId)
|
||||
public static async FTask<Player?> Online(this PlayerManageComponent self, long accountId, long gateRouteId)
|
||||
{
|
||||
Log.Debug("检查账号是否在缓存中");
|
||||
if (!self.TryGet(accountId, out var account))
|
||||
{
|
||||
// 首先要先到数据库中查询是否有这个账号
|
||||
account = await PlayerHelper.LoadDataBase(scene, accountId);
|
||||
account = await PlayerHelper.LoadDataBase(self.Scene, accountId);
|
||||
// 如果有的话,就直接加入在缓存中就可以了
|
||||
if (account == null)
|
||||
{
|
||||
Log.Debug("检查到账号没有在数据库中,需要创建一个新的账号并且保存到数据库中");
|
||||
// 如果没有,就要创建一个新的并且保存到数据库。
|
||||
// 如果不存在,表示这是一个新的账号,需要创建一下这个账号。
|
||||
account = PlayerFactory.Create(scene, accountId);
|
||||
account = PlayerFactory.Create(self.Scene, accountId);
|
||||
|
||||
|
||||
account.Level = 99;
|
||||
@@ -111,6 +109,19 @@ public static class PlayerManageComponentSystem
|
||||
return account;
|
||||
}
|
||||
|
||||
public static async FTask Offline(this PlayerManageComponent self, long accountId, long sessionId)
|
||||
{
|
||||
if (self.TryGet(accountId, out var account))
|
||||
{
|
||||
if (account.SessionRunTimeId == sessionId)
|
||||
{
|
||||
//退出的是当前的
|
||||
await account.Save();
|
||||
self.Remove(accountId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 获取&移除
|
||||
|
||||
63
Hotfix/Gate/Handler/Outer/C2G_EnterRoomRequestHandler.cs
Normal file
63
Hotfix/Gate/Handler/Outer/C2G_EnterRoomRequestHandler.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using Fantasy;
|
||||
using Fantasy.Async;
|
||||
using Fantasy.Network;
|
||||
using Fantasy.Network.Interface;
|
||||
using Fantasy.Platform.Net;
|
||||
using NB.Common;
|
||||
using NB.Map;
|
||||
|
||||
namespace NB.Gate.Handler;
|
||||
|
||||
public class C2G_EnterRoomRequestHandler : MessageRPC<C2G_EnterRoomRequest, G2C_EnterRoomResponse>
|
||||
{
|
||||
protected override async FTask Run(Session session, C2G_EnterRoomRequest request, G2C_EnterRoomResponse 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;
|
||||
}
|
||||
|
||||
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 mapRouteId = gateUnit.GetAddress(RouteType.MapRoute);
|
||||
|
||||
//执行进入房间操作
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ using Fantasy.Network.Interface;
|
||||
using Fantasy.Platform.Net;
|
||||
using NB.Game;
|
||||
|
||||
namespace NB.Gate.Handler;
|
||||
namespace NB.Gate;
|
||||
|
||||
public sealed class C2G_LoginRequestHandler : MessageRPC<C2G_LoginRequest, G2C_LoginResponse>
|
||||
{
|
||||
|
||||
@@ -21,7 +21,7 @@ public static class GateUnitSystem
|
||||
{
|
||||
#region Address
|
||||
|
||||
private static void RemoveAddress(this GateUnit self, int routType, long routeId)
|
||||
public static void RemoveAddress(this GateUnit self, int routType, long routeId)
|
||||
{
|
||||
if (self.SceneRoutes.TryGetValue(routType, out var route))
|
||||
{
|
||||
@@ -32,12 +32,12 @@ public static class GateUnitSystem
|
||||
}
|
||||
}
|
||||
|
||||
private static void AddAddress(this GateUnit self, int routType, long routeId)
|
||||
public static void AddAddress(this GateUnit self, int routType, long routeId)
|
||||
{
|
||||
self.SceneRoutes[routType] = routeId;
|
||||
}
|
||||
|
||||
private static long GetAddress(this GateUnit self, int routType)
|
||||
public static long GetAddress(this GateUnit self, int routType)
|
||||
{
|
||||
return self.SceneRoutes.GetValueOrDefault(routType, 0);
|
||||
}
|
||||
@@ -69,10 +69,10 @@ public static class GateUnitSystem
|
||||
{
|
||||
routeComponent = session.AddComponent<RouteComponent>();
|
||||
}
|
||||
|
||||
var gameSceneConfig = SceneConfigHelper.GetConfig(routeType);
|
||||
|
||||
var gameSceneConfig = SceneConfigHelper.GetConfigByRouteType(routeType);
|
||||
var gameRouteId = gameSceneConfig.RouteId;
|
||||
//连接到游戏中心服
|
||||
|
||||
var gameResponse = (G2Common_EnterResponse)await self.Scene.NetworkMessagingComponent.CallInnerRoute(
|
||||
gameRouteId, new G2Common_EnterRequest()
|
||||
{
|
||||
@@ -85,8 +85,8 @@ public static class GateUnitSystem
|
||||
return gameResponse.ErrorCode;
|
||||
}
|
||||
|
||||
routeComponent.AddAddress(routeType, gameRouteId);
|
||||
self.AddAddress(routeType, gameRouteId);
|
||||
routeComponent.AddAddress(routeType, gameResponse.UnitRouteId);
|
||||
self.AddAddress(routeType, gameResponse.UnitRouteId);
|
||||
|
||||
return ErrorCode.Successful;
|
||||
}
|
||||
|
||||
@@ -13,10 +13,8 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Game\Handler\" />
|
||||
<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\" />
|
||||
|
||||
@@ -1,15 +1,44 @@
|
||||
// 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.Entitas;
|
||||
using Fantasy.Network.Interface;
|
||||
|
||||
namespace NB.Map;
|
||||
|
||||
public class C2Map_CreateRoomRequestHandler : RouteRPC<MapUnit, C2Map_CreateRoomRequest, Map2C_CreateRoomResponse>
|
||||
{
|
||||
protected override async FTask Run(MapUnit entity, C2Map_CreateRoomRequest request,
|
||||
Map2C_CreateRoomResponse response,
|
||||
Action reply)
|
||||
{
|
||||
if (entity.MapId != request.MapId)
|
||||
{
|
||||
response.ErrorCode = ErrorCode.MapCreateRoomNotEnter;
|
||||
return;
|
||||
}
|
||||
|
||||
var roomManageComponent = entity.Scene.GetComponent<RoomManageComponent>();
|
||||
if (roomManageComponent == null)
|
||||
{
|
||||
response.ErrorCode = ErrorCode.ErrServer;
|
||||
return;
|
||||
}
|
||||
|
||||
var roomId = roomManageComponent.AllocateId();
|
||||
if (roomId < 1)
|
||||
{
|
||||
response.ErrorCode = ErrorCode.MapCreateRoomMax;
|
||||
return;
|
||||
}
|
||||
|
||||
// roomManageComponent.on
|
||||
var room = Entity.Create<MapRoom>(entity.Scene, true, true);
|
||||
room.Owner = entity.Id;
|
||||
room.RoomId = roomId;
|
||||
room.Code = RoomHelper.GenerateCode(entity.Scene.SceneConfigId, roomId);
|
||||
roomManageComponent.Add(room);
|
||||
Log.Info(
|
||||
$"创建房间=== sId={entity.Scene.SceneConfigId} map:{request.MapId} id={room.RoomId} code={room.Code}");
|
||||
await FTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
33
Hotfix/Map/Handler/C2Map_EnterMapRequestHandler.cs
Normal file
33
Hotfix/Map/Handler/C2Map_EnterMapRequestHandler.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using Fantasy;
|
||||
using Fantasy.Async;
|
||||
using Fantasy.Network.Interface;
|
||||
|
||||
namespace NB.Map;
|
||||
|
||||
public class C2Map_EnterMapRequestHandler : RouteRPC<MapUnit, C2Map_EnterMapRequest, Map2C_EnterMapResponse>
|
||||
{
|
||||
protected override async FTask Run(MapUnit entity, C2Map_EnterMapRequest request, Map2C_EnterMapResponse response,
|
||||
Action reply)
|
||||
{
|
||||
var roomManageComponent = entity.Scene.GetComponent<RoomManageComponent>();
|
||||
if (roomManageComponent == null)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
27
Hotfix/Map/Handler/Inner/G2Map_EnterRequestHandler.cs
Normal file
27
Hotfix/Map/Handler/Inner/G2Map_EnterRequestHandler.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using Fantasy;
|
||||
using Fantasy.Async;
|
||||
using Fantasy.Network.Interface;
|
||||
|
||||
namespace NB.Map.Inner;
|
||||
|
||||
public class G2Map_EnterRequestHandler : RouteRPC<Scene, G2Map_EnterRoomRequest, Map2G_EnterRoomResponse>
|
||||
{
|
||||
protected override async FTask Run(Scene entity, G2Map_EnterRoomRequest request, Map2G_EnterRoomResponse response,
|
||||
Action reply)
|
||||
{
|
||||
var roomManageComponent = entity.GetComponent<RoomManageComponent>();
|
||||
if (roomManageComponent == 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();
|
||||
}
|
||||
}
|
||||
19
Hotfix/Map/Helper/MapUnitFactory.cs
Normal file
19
Hotfix/Map/Helper/MapUnitFactory.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using Fantasy;
|
||||
using Fantasy.Entitas;
|
||||
|
||||
namespace NB.Map;
|
||||
|
||||
public static class MapUnitFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// 创建一个新的Player
|
||||
/// </summary>
|
||||
/// <param name="scene"></param>
|
||||
/// <param name="aId">ToKen令牌传递过来的aId</param>
|
||||
/// <returns></returns>
|
||||
public static MapUnit Create(Scene scene, long aId)
|
||||
{
|
||||
var player = Entity.Create<MapUnit>(scene, aId, true, true);
|
||||
return player;
|
||||
}
|
||||
}
|
||||
69
Hotfix/Map/Helper/RoomHelper.cs
Normal file
69
Hotfix/Map/Helper/RoomHelper.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using System.Text;
|
||||
|
||||
namespace NB.Map;
|
||||
|
||||
public static class RoomHelper
|
||||
{
|
||||
#region 房间id
|
||||
|
||||
private const string Base36Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
private const int RoomSeqBits = 16; // 每个服务最多 65535 个房间
|
||||
|
||||
/// <summary>
|
||||
/// 生成房间码(固定5位)
|
||||
/// </summary>
|
||||
/// <param name="serviceId"></param>
|
||||
/// <param name="roomSeq"></param>
|
||||
/// <returns></returns>
|
||||
public static string GenerateCode(uint serviceId, int roomSeq)
|
||||
{
|
||||
long packed = ((long)serviceId << RoomSeqBits) | (uint)roomSeq;
|
||||
string code = EncodeBase36(packed);
|
||||
|
||||
// 固定5位,不够左侧补0
|
||||
return code.PadLeft(5, '0');
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解析房间码
|
||||
/// </summary>
|
||||
/// <param name="code"></param>
|
||||
/// <param name="serviceId"></param>
|
||||
/// <param name="roomSeq"></param>
|
||||
public static void ParseCode(string code, out uint serviceId, out int roomSeq)
|
||||
{
|
||||
long packed = DecodeBase36(code);
|
||||
roomSeq = (int)(packed & ((1 << RoomSeqBits) - 1));
|
||||
serviceId = (uint)(packed >> RoomSeqBits);
|
||||
}
|
||||
|
||||
private static string EncodeBase36(long value)
|
||||
{
|
||||
if (value == 0) return "0";
|
||||
StringBuilder sb = new StringBuilder();
|
||||
while (value > 0)
|
||||
{
|
||||
int remainder = (int)(value % 36);
|
||||
sb.Insert(0, Base36Chars[remainder]);
|
||||
value /= 36;
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
private static long DecodeBase36(string str)
|
||||
{
|
||||
long result = 0;
|
||||
foreach (char c in str)
|
||||
{
|
||||
int val = Base36Chars.IndexOf(c);
|
||||
if (val < 0) throw new ArgumentException("Invalid Base36 character: " + c);
|
||||
result = result * 36 + val;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
19
Hotfix/Map/System/MapRoomSystem.cs
Normal file
19
Hotfix/Map/System/MapRoomSystem.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using Fantasy.Entitas.Interface;
|
||||
|
||||
namespace NB.Map;
|
||||
|
||||
public class MapRoomDestroySystem : DestroySystem<MapRoom>
|
||||
{
|
||||
protected override void Destroy(MapRoom self)
|
||||
{
|
||||
self.Map = 0;
|
||||
self.Password = string.Empty;
|
||||
self.CreateTime = 0;
|
||||
self.Owner = 0;
|
||||
self.Units.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
public static class MapRoomSystem
|
||||
{
|
||||
}
|
||||
84
Hotfix/Map/System/MapUnitManageComponentSystem.cs
Normal file
84
Hotfix/Map/System/MapUnitManageComponentSystem.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
using Fantasy;
|
||||
using Fantasy.Async;
|
||||
|
||||
namespace NB.Map;
|
||||
|
||||
public static class MapUnitManageComponentSystem
|
||||
{
|
||||
#region 上线下线
|
||||
|
||||
/// <summary>
|
||||
/// 玩家上线
|
||||
/// </summary>
|
||||
/// <param name="self"></param>
|
||||
/// <param name="scene"></param>
|
||||
/// <param name="accountId"></param>
|
||||
/// <param name="gateRouteId"></param>
|
||||
public static async FTask<MapUnit?> Online(this MapUnitManageComponent self, Scene scene,
|
||||
long accountId,
|
||||
long gateRouteId)
|
||||
{
|
||||
if (!self.TryGet(accountId, out var unit))
|
||||
{
|
||||
unit = MapUnitFactory.Create(scene, accountId);
|
||||
self.Add(unit);
|
||||
}
|
||||
|
||||
if (unit != null)
|
||||
{
|
||||
unit.GateRouteId = gateRouteId;
|
||||
}
|
||||
|
||||
await FTask.CompletedTask;
|
||||
return unit;
|
||||
}
|
||||
|
||||
public static async FTask Offline(this MapUnitManageComponent self, Scene scene, long accountId, long gateRouteId)
|
||||
{
|
||||
if (self.TryGet(accountId, out var unit) && unit != null)
|
||||
{
|
||||
if (unit.GateRouteId == gateRouteId)
|
||||
{
|
||||
self.Remove(accountId); //如果当前网关和下线的网关一致
|
||||
}
|
||||
}
|
||||
|
||||
await FTask.CompletedTask;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 获取&移除
|
||||
|
||||
public static void Add(this MapUnitManageComponent self, MapUnit unit)
|
||||
{
|
||||
self.Units.Add(unit.Id, unit);
|
||||
}
|
||||
|
||||
public static MapUnit? Get(this MapUnitManageComponent self, long accountId)
|
||||
{
|
||||
return self.Units.GetValueOrDefault(accountId);
|
||||
}
|
||||
|
||||
public static bool TryGet(this MapUnitManageComponent self, long accountId, out MapUnit? unit)
|
||||
{
|
||||
return self.Units.TryGetValue(accountId, out unit);
|
||||
}
|
||||
|
||||
public static void Remove(this MapUnitManageComponent self, long accountId, bool isDispose = true)
|
||||
{
|
||||
if (!self.Units.Remove(accountId, out var account))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isDispose)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
account.Dispose();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
93
Hotfix/Map/System/RoomManageComponentSystem.cs
Normal file
93
Hotfix/Map/System/RoomManageComponentSystem.cs
Normal file
@@ -0,0 +1,93 @@
|
||||
using Fantasy.Entitas;
|
||||
using Fantasy.Entitas.Interface;
|
||||
|
||||
namespace NB.Map;
|
||||
|
||||
public class RoomManageComponentAwakeSystem : AwakeSystem<RoomManageComponent>
|
||||
{
|
||||
protected override void Awake(RoomManageComponent self)
|
||||
{
|
||||
for (int i = 1; i <= 65535; i++)
|
||||
{
|
||||
self.FreeIds.Enqueue(i, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class RoomManageComponentDestroySystem : DestroySystem<RoomManageComponent>
|
||||
{
|
||||
protected override void Destroy(RoomManageComponent self)
|
||||
{
|
||||
foreach (var (_, room) in self.Rooms)
|
||||
{
|
||||
room.Dispose();
|
||||
}
|
||||
|
||||
self.Rooms.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
public static class RoomManageComponentSystem
|
||||
{
|
||||
#region 增删
|
||||
|
||||
public static void Add(this RoomManageComponent self, MapRoom room)
|
||||
{
|
||||
self.Rooms[room.RoomId] = room;
|
||||
}
|
||||
|
||||
public static bool Remove(this RoomManageComponent self, int roomId)
|
||||
{
|
||||
if (self.Rooms.TryGetValue(roomId, out var room))
|
||||
{
|
||||
self.ReleaseId(room.RoomId);
|
||||
room.Dispose();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 房间Id
|
||||
|
||||
/// <summary>
|
||||
/// 分配一个新的房间ID
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
public static int AllocateId(this RoomManageComponent self)
|
||||
{
|
||||
if (self.FreeIds.Count == 0) return 0;
|
||||
int id = self.FreeIds.Dequeue();
|
||||
self.InUseID.Add(id);
|
||||
return id;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放房间ID
|
||||
/// </summary>
|
||||
/// <param name="self"></param>
|
||||
/// <param name="id"></param>
|
||||
public static void ReleaseId(this RoomManageComponent self, int id)
|
||||
{
|
||||
if (self.InUseID.Remove(id))
|
||||
{
|
||||
self.FreeIds.Enqueue(id, id);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 进入退出
|
||||
|
||||
public static void Enter(this RoomManageComponent self, MapUnit unit, long roomId)
|
||||
{
|
||||
}
|
||||
|
||||
public static void Exit(this RoomManageComponent self, MapUnit unit)
|
||||
{
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -6,6 +6,7 @@ using Fantasy.Event;
|
||||
using Fantasy.Helper;
|
||||
using Fantasy.Serialize;
|
||||
using NB.Game;
|
||||
using NB.Map;
|
||||
using ProtoBuf;
|
||||
|
||||
namespace NB;
|
||||
@@ -66,6 +67,14 @@ public sealed class OnCreateSceneEvent : AsyncEventSystem<OnCreateScene>
|
||||
case SceneType.Map:
|
||||
{
|
||||
Log.Debug($"Map Scene SceneRuntimeId:{scene.RuntimeId}");
|
||||
// uint serverId = 25255;
|
||||
// for (int i = 1; i < 65535; i++)
|
||||
// {
|
||||
// var roomId = RoomHelper.GenerateCode(serverId, i);
|
||||
// RoomHelper.ParseCode(roomId, out var pId, out var rId);
|
||||
// Log.Info($"生成id测试,房间id={roomId} 原始服务id:{serverId} 原始房间id={i} 解析={pId} {rId}");
|
||||
// }
|
||||
|
||||
break;
|
||||
}
|
||||
case SceneType.Social:
|
||||
@@ -74,7 +83,6 @@ public sealed class OnCreateSceneEvent : AsyncEventSystem<OnCreateScene>
|
||||
}
|
||||
case SceneType.Game:
|
||||
{
|
||||
|
||||
// // Begins transaction
|
||||
// using (var session = mongoClient.StartSession())
|
||||
// {
|
||||
@@ -131,8 +139,8 @@ public sealed class OnCreateSceneEvent : AsyncEventSystem<OnCreateScene>
|
||||
//
|
||||
// stopwatch.Stop();
|
||||
// Log.Info($"创建100个号入库耗时={stopwatch.ElapsedMilliseconds}ms");
|
||||
|
||||
|
||||
|
||||
|
||||
break;
|
||||
}
|
||||
case SceneType.Gate:
|
||||
|
||||
@@ -5,6 +5,7 @@ using NB.Authentication;
|
||||
using NB.Chat;
|
||||
using NB.Game;
|
||||
using NB.Gate;
|
||||
using NB.Map;
|
||||
|
||||
namespace NB;
|
||||
|
||||
@@ -50,6 +51,9 @@ public class OnSceneCreate_Init : AsyncEventSystem<OnCreateScene>
|
||||
case SceneType.Map:
|
||||
{
|
||||
Log.Info("创建地图场景===");
|
||||
scene.AddComponent<MapUnitManageComponent>();
|
||||
scene.AddComponent<RoomManageComponent>();
|
||||
scene.AddComponent<MapManageComponent>();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user