房间创建和进入相关逻辑和协议

This commit is contained in:
2025-08-27 00:04:40 +08:00
parent 7325e268ce
commit f6d85a1e0a
45 changed files with 1246 additions and 729 deletions

View File

@@ -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;
}
}

View 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;
}
}

View 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();
}
}

View 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;
}
}

View 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
}

View 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
{
}

View 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
}

View 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
}