聊天频道相关

This commit is contained in:
bob
2025-08-08 18:21:11 +08:00
parent 61496d4616
commit 7a93c0f8f1
23 changed files with 256 additions and 95 deletions

View File

@@ -10,7 +10,7 @@ public sealed class
protected override async FTask Run(ChatUnit chatUnit, C2Chat_SendMessageRequest request,
Caht2C_SendMessageResponse response, Action reply)
{
ChatSceneHelper.Broadcast(chatUnit.Scene, request.Message);
// ChatSceneHelper.Broadcast(chatUnit.Scene, request.Message);
await FTask.CompletedTask;
}
}

View File

@@ -5,16 +5,18 @@ using NB.Game;
namespace NB.Chat;
public class G2Chat_EnterRequestHandler: RouteRPC<Scene, G2Chat_EnterRequest, Chat2G_EnterResponse>
public class G2Chat_EnterRequestHandler : RouteRPC<Scene, G2Chat_EnterRequest, Chat2G_EnterResponse>
{
protected override async FTask Run(Scene scene, G2Chat_EnterRequest request, Chat2G_EnterResponse response,
Action reply)
{
Log.Debug($"收到 G2Chat_EnterRequestHandler {request.AccountId}");
var roleId = request.Role.RoleId;
Log.Debug($"收到 G2Chat_EnterRequestHandler {roleId}");
// 在缓存中检查该账号是否存在
var chatUnitManageComponent = scene.GetComponent<ChatUnitManageComponent>();
var account = await chatUnitManageComponent.Online(scene, request.AccountId, request.GateRouteId);
var account = await chatUnitManageComponent.Online(scene, request.Role, request.GateRouteId);
response.RoleRouteId = account.RuntimeId;
await FTask.CompletedTask;

View File

@@ -1,39 +0,0 @@
using Fantasy;
namespace NB.Chat;
public static class ChatChannelHelper
{
/// <summary>
/// 申请一个频道
/// </summary>
/// <param name="scene"></param>
/// <param name="channelId"></param>
/// <returns></returns>
public static ChatChannelComponent Apply(Scene scene, long channelId)
{
return scene.GetComponent<ChatChannelCenterComponent>().Apply(channelId);
}
/// <summary>
/// 尝试获取一个频道
/// </summary>
/// <param name="scene"></param>
/// <param name="channelId"></param>
/// <param name="channel"></param>
/// <returns></returns>
public static bool TryGet(Scene scene, long channelId, out ChatChannelComponent? channel)
{
return scene.GetComponent<ChatChannelCenterComponent>().TryGet(channelId, out channel);
}
/// <summary>
/// 解散频道
/// </summary>
/// <param name="scene"></param>
/// <param name="channelId"></param>
public static void Disband(Scene scene, long channelId)
{
scene.GetComponent<ChatChannelCenterComponent>().Disband(channelId);
}
}

View File

@@ -6,17 +6,29 @@ namespace NB.Chat;
public static class ChatSceneHelper
{
#region
/// <summary>
/// 广播消息
/// 广播记录更新
/// </summary>
/// <param name="scene"></param>
/// <param name="record"></param>
public static void BroadcastRecord(Scene scene, ChatContentRecord record)
{
}
/// <summary>
/// 广播消息给所有人
/// </summary>
/// <param name="scene"></param>
/// <param name="message"></param>
public static void Broadcast(Scene scene, string message)
public static void Broadcast(Scene scene, ChatMessageInfo message)
{
Log.Info("广播消息===");
//发送给所有Gate服务器让Gate转发给其他客户端
var gateConfigs = SceneConfigData.Instance.GetSceneBySceneType(SceneType.Gate);
var sendMessage = new Chat2G_ChatMessage()
{
Message = message
@@ -29,7 +41,12 @@ public static class ChatSceneHelper
}
}
public static async FTask<long> Online(Scene scene, long accountID, long gateRuntimeId)
#endregion
#region 线线
public static async FTask<long> Online(Scene scene, RoleSimpleInfo roleSimple, long gateRuntimeId)
{
var gameSceneConfig = GetSceneConfig();
var gameRouteId = gameSceneConfig.RouteId;
@@ -37,7 +54,7 @@ public static class ChatSceneHelper
var gameResponse = (Chat2G_EnterResponse)await scene.NetworkMessagingComponent.CallInnerRoute(
gameRouteId, new G2Chat_EnterRequest()
{
AccountId = accountID,
Role = roleSimple,
GateRouteId = gateRuntimeId
});
@@ -48,14 +65,13 @@ public static class ChatSceneHelper
return gameResponse.RoleRouteId;
}
private static SceneConfig GetSceneConfig()
{
var gameSceneConfigs = SceneConfigData.Instance.GetSceneBySceneType(SceneType.Chat);
return gameSceneConfigs.First();
}
#endregion
}

View File

@@ -1,23 +1,27 @@
using Fantasy.Entitas;
using Fantasy.Helper;
namespace NB.Chat;
public static class ChatChannelCenterComponentSystem
{
/// <summary>
/// 申请
/// 申请创建一个频道
/// </summary>
/// <param name="self"></param>
/// <param name="unit"></param>
/// <param name="channelId"></param>
/// <returns></returns>
public static ChatChannelComponent Apply(this ChatChannelCenterComponent self, long channelId)
public static ChatChannel Apply(this ChatChannelCenterComponent self, ChatUnit unit, long channelId)
{
if (self.Channels.TryGetValue(channelId, out var channel))
{
return channel;
}
channel = Entity.Create<ChatChannelComponent>(self.Scene, channelId, true, true);
channel = Entity.Create<ChatChannel>(self.Scene, channelId, true, true);
channel.Creator = unit.RoleId;
channel.CreateTime = TimeHelper.Now;
self.Channels.Add(channelId, channel);
return channel;
}
@@ -29,7 +33,7 @@ public static class ChatChannelCenterComponentSystem
/// <param name="channelId"></param>
/// <param name="channel"></param>
/// <returns></returns>
public static bool TryGet(this ChatChannelCenterComponent self, long channelId, out ChatChannelComponent? channel)
public static bool TryGet(this ChatChannelCenterComponent self, long channelId, out ChatChannel? channel)
{
return self.Channels.TryGetValue(channelId, out channel);
}

View File

@@ -13,21 +13,29 @@ public static class ChatUnitManageComponentSystem
/// </summary>
/// <param name="self"></param>
/// <param name="scene"></param>
/// <param name="accountId"></param>
/// <param name="roleSimpleInfo"></param>
/// <param name="gateRouteId"></param>
public static async FTask<ChatUnit> Online(this ChatUnitManageComponent self, Scene scene, long accountId,
public static async FTask<ChatUnit?> Online(this ChatUnitManageComponent self, Scene scene, RoleSimpleInfo roleSimpleInfo,
long gateRouteId)
{
var accountId = roleSimpleInfo.RoleId;
if (!self.TryGet(accountId, out var account))
{
account = ChatUnitFactory.Create(scene, accountId);
self.Add(account);
}
account.GateRouteId = gateRouteId;
if (account != null)
{
account.GateRouteId = gateRouteId;
account.RoleId = accountId;
account.Head = roleSimpleInfo.Head;
account.Level = roleSimpleInfo.Level;
account.NickName = roleSimpleInfo.NickName;
account.Country = roleSimpleInfo.Country;
}
await FTask.CompletedTask;
return account;
}

View File

@@ -23,6 +23,7 @@ public class G2Game_EnterRequestHandler : RouteRPC<Scene, G2Game_EnterRequest, G
var account = await gameAccountManageComponent.Online(scene, request.AccountId, request.GateRouteId);
response.RoleRouteId = account.RuntimeId;
response.RoleInfo = account.GetRoleSimpleInfo();
await FTask.CompletedTask;
}
}

View File

@@ -15,7 +15,7 @@ public static class GameSceneHelper
return gameSceneConfigs.First();
}
public static async FTask<long> Online(Scene scene, long accountID, long gateRuntimeId)
public static async FTask<(long, RoleSimpleInfo?)> Online(Scene scene, long accountID, long gateRuntimeId)
{
var gameSceneConfig = GetSceneConfig();
var gameRouteId = gameSceneConfig.RouteId;
@@ -29,9 +29,9 @@ public static class GameSceneHelper
if (gameResponse.ErrorCode != 0)
{
return 0;
return (0, null);
}
return gameResponse.RoleRouteId;
return (gameResponse.RoleRouteId, gameResponse.RoleInfo);
}
}

View File

@@ -8,6 +8,8 @@ namespace NB.Game;
public static class PlayerHelper
{
#region MyRegion
public static void InitializeChildEntity(this Player self)
{
if (self.Basic == null)
@@ -141,6 +143,11 @@ public static class PlayerHelper
account.SetTimeout(timeOut, account.Disconnect);
}
#endregion
#region
/// <summary>
/// 获得GameAccountInfo
/// </summary>
@@ -158,4 +165,37 @@ public static class PlayerHelper
LoginTime = self.Statistics.LoginTime
};
}
public static RoleSimpleInfo GetRoleSimpleInfo(this Player self)
{
return new RoleSimpleInfo()
{
RoleId = self.Id,
NickName = self.Basic.NickName,
Head = self.Basic.Head,
Country = self.Basic.Country,
Level = self.Basic.Level
};
}
public static RoleInfo GetRoleInfo(this Player self)
{
var info = new RoleInfo();
info.BaseInfo = GetRoleBaseInfo(self);
return info;
}
public static RoleBaseInfo GetRoleBaseInfo(this Player self)
{
return new RoleBaseInfo()
{
NickName = self.Basic.NickName,
Head = self.Basic.Head,
Country = self.Basic.Country,
Level = self.Basic.Level,
Exp = self.Basic.Exp,
};
}
#endregion
}

View File

@@ -43,22 +43,25 @@ public static class GateLoginHelper
//安排游戏服务器,并通知进入
var gameRouteId = await GameSceneHelper.Online(session.Scene, gateUnit.AccountID, session.RuntimeId);
if (gameRouteId <= 0)
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 = await ChatSceneHelper.Online(session.Scene, gateUnit.AccountID, session.RuntimeId);
//安排进入的聊天服
var chatRouteId = await ChatSceneHelper.Online(session.Scene, roleSimpleInfo, session.RuntimeId);
if (chatRouteId <= 0)
{
return ErrorCode.OnlineSceneFailed;
}
routeComponent.AddAddress(RouteType.ChatRoute, chatRouteId);
gateUnit.ChatSceneRouteId = chatRouteId;
Log.Info($"连接聊天服成功gameRouteId:{gameRouteId}");
@@ -66,7 +69,7 @@ public static class GateLoginHelper
}
#endregion
#region 线
/// <summary>
@@ -82,5 +85,4 @@ public static class GateLoginHelper
}
#endregion
}

View File

@@ -12,4 +12,8 @@
<ProjectReference Include="..\Fantasy\Fantasy.Net\Fantasy.Net\Fantasy.Net.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Utils\" />
</ItemGroup>
</Project>