聊天频道相关

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,13 +10,14 @@ message G2Game_EnterRequest // IRouteRequest,Game2G_EnterResponse
message Game2G_EnterResponse // IRouteResponse
{
int64 RoleRouteId = 1; //角色实体的路由id
RoleSimpleInfo RoleInfo = 2; //角色信息
}
///通知游戏服角色进入该聊天服
message G2Chat_EnterRequest // IRouteRequest,Game2G_EnterResponse
{
int64 AccountId = 1; //账号id
RoleSimpleInfo Role = 1; //角色信息
int64 GateRouteId = 2;//网关路由地址
}
@@ -27,5 +28,5 @@ message Chat2G_EnterResponse // IRouteResponse
message Chat2G_ChatMessage // IRouteMessage
{
string Message = 1; //聊天内容
ChatMessageInfo Message = 1; //聊天内容
}

View File

@@ -43,6 +43,16 @@ message RoleInfo
repeated SkillInfo Skills = 8; //技能信息
}
/// 角色信息
message RoleSimpleInfo
{
int64 RoleId = 1;
string NickName = 2; //昵称
string Head = 3; //头像
string Country = 4; //国家
int32 Level = 5; //等级
}
/// VIP信息
message VipInfo
{

View File

@@ -9,7 +9,8 @@ message ChatUserInfo
message ChatMessageInfo
{
int32 Type = 1; //消息类型
ChatUserInfo Trigger = 2; //发送者
repeated string Content = 3; //内容
int32 Type = 1; //消息类型
int32 Source = 2; //消息来源
ChatUserInfo Trigger = 3; //触发者
repeated byte Content = 4; //内容
}

View File

@@ -0,0 +1,40 @@
using Fantasy.Entitas;
using MongoDB.Bson.Serialization.Attributes;
namespace NB.Chat;
/// <summary>
/// 聊天频道实体
/// </summary>
public class ChatChannel : Entity
{
[BsonElement("type")] public uint ChannelType;
/// <summary>
/// 频道Id
/// </summary>
[BsonElement("cid")] public long ChannelId;
/// <summary>
/// 频道名称
/// </summary>
[BsonElement("name")] public string Name = "";
/// <summary>
/// 创建者
/// </summary>
[BsonElement("cr")] public long Creator;
/// <summary>
/// 创建时间
/// </summary>
[BsonElement("ct")] public long CreateTime;
/// <summary>
/// 频道地区
/// </summary>
[BsonElement("region")] public int Region;
[BsonElement("ids")] public readonly HashSet<long> Units = new HashSet<long>();
}

View File

@@ -7,5 +7,5 @@ namespace NB.Chat;
/// </summary>
public class ChatChannelCenterComponent : Entity
{
public readonly Dictionary<long, ChatChannelComponent> Channels = new Dictionary<long, ChatChannelComponent>();
public readonly Dictionary<long, ChatChannel> Channels = new Dictionary<long, ChatChannel>();
}

View File

@@ -1,11 +0,0 @@
using Fantasy.Entitas;
namespace NB.Chat;
/// <summary>
/// 聊天频道实体
/// </summary>
public class ChatChannelComponent : Entity
{
public readonly HashSet<long> Units = new HashSet<long>();
}

View File

@@ -0,0 +1,25 @@
namespace NB.Chat;
[Serializable]
public abstract class ChatContentData
{
}
[Serializable]
public class ChatContentNormal : ChatContentData
{
public string Content = "";
}
[Serializable]
public class ChatContentFished : ChatContentData
{
public long Id;
public int Weight;
public int Type;
}
[Serializable]
public class ChatContentRecord : ChatContentData
{
}

View File

@@ -0,0 +1,4 @@
using Fantasy.Entitas;
namespace NB.Chat;

View File

@@ -6,6 +6,16 @@ public sealed class ChatUnit : Entity
{
public long GateRouteId;
public long RoleId;
public string NickName = string.Empty;
public string Head = string.Empty;
public string Country = string.Empty;
public int Level;
public override void Dispose()
{
if (IsDisposed)
@@ -13,7 +23,12 @@ public sealed class ChatUnit : Entity
return;
}
RoleId = 0;
GateRouteId = 0;
NickName = string.Empty;
Head = string.Empty;
Country = string.Empty;
Level = 0;
base.Dispose();
}
}

View File

@@ -151,6 +151,38 @@ namespace Fantasy
public List<SkillInfo> Skills = new List<SkillInfo>();
}
/// <summary>
/// 角色信息
/// </summary>
[ProtoContract]
public partial class RoleSimpleInfo : AMessage, IProto
{
public static RoleSimpleInfo Create(Scene scene)
{
return scene.MessagePoolComponent.Rent<RoleSimpleInfo>();
}
public override void Dispose()
{
RoleId = default;
NickName = default;
Head = default;
Country = default;
Level = default;
#if FANTASY_NET || FANTASY_UNITY
GetScene().MessagePoolComponent.Return<RoleSimpleInfo>(this);
#endif
}
[ProtoMember(1)]
public long RoleId { get; set; }
[ProtoMember(2)]
public string NickName { get; set; }
[ProtoMember(3)]
public string Head { get; set; }
[ProtoMember(4)]
public string Country { get; set; }
[ProtoMember(5)]
public int Level { get; set; }
}
/// <summary>
/// VIP信息
/// </summary>
[ProtoContract]

View File

@@ -47,6 +47,7 @@ namespace Fantasy
public override void Dispose()
{
Type = default;
Source = default;
Trigger = default;
Content.Clear();
#if FANTASY_NET || FANTASY_UNITY
@@ -56,8 +57,10 @@ namespace Fantasy
[ProtoMember(1)]
public int Type { get; set; }
[ProtoMember(2)]
public ChatUserInfo Trigger { get; set; }
public int Source { get; set; }
[ProtoMember(3)]
public List<string> Content = new List<string>();
public ChatUserInfo Trigger { get; set; }
[ProtoMember(4)]
public List<byte> Content = new List<byte>();
}
}

View File

@@ -54,6 +54,7 @@ namespace Fantasy
{
ErrorCode = default;
RoleRouteId = default;
RoleInfo = default;
#if FANTASY_NET || FANTASY_UNITY
GetScene().MessagePoolComponent.Return<Game2G_EnterResponse>(this);
#endif
@@ -62,6 +63,8 @@ namespace Fantasy
[ProtoMember(1)]
public long RoleRouteId { get; set; }
[ProtoMember(2)]
public RoleSimpleInfo RoleInfo { get; set; }
[ProtoMember(3)]
public uint ErrorCode { get; set; }
}
/// <summary>
@@ -76,7 +79,7 @@ namespace Fantasy
}
public override void Dispose()
{
AccountId = default;
Role = default;
GateRouteId = default;
#if FANTASY_NET || FANTASY_UNITY
GetScene().MessagePoolComponent.Return<G2Chat_EnterRequest>(this);
@@ -86,7 +89,7 @@ namespace Fantasy
public Game2G_EnterResponse ResponseType { get; set; }
public uint OpCode() { return InnerOpcode.G2Chat_EnterRequest; }
[ProtoMember(1)]
public long AccountId { get; set; }
public RoleSimpleInfo Role { get; set; }
[ProtoMember(2)]
public long GateRouteId { get; set; }
}
@@ -127,6 +130,6 @@ namespace Fantasy
}
public uint OpCode() { return InnerOpcode.Chat2G_ChatMessage; }
[ProtoMember(1)]
public string Message { get; set; }
public ChatMessageInfo Message { get; set; }
}
}

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>