大修改

This commit is contained in:
2025-08-14 23:56:51 +08:00
parent 022cc1ac3e
commit d5689258fc
54 changed files with 775 additions and 839 deletions

View File

@@ -0,0 +1,27 @@
using Fantasy;
using Fantasy.Async;
using Fantasy.Network.Interface;
namespace NB.Chat;
/// <summary>
/// 请求创建频道
/// </summary>
public class
C2S_CreateChannelRequestHandler : RouteRPC<SocialUnit, C2S_CreateChannelRequest, S2C_CreateChannelResponse>
{
protected override async FTask Run(SocialUnit entity, C2S_CreateChannelRequest request,
S2C_CreateChannelResponse response, Action reply)
{
var channelCenter = entity.Scene.GetComponent<ChatChannelCenterComponent>();
if (channelCenter == null)
{
response.ErrorCode = ErrorCode.ErrServer;
return;
}
var channel = await channelCenter.Create(entity);
response.ChannelId = channel.Id;
}
}

View File

@@ -0,0 +1,24 @@
using Fantasy;
using Fantasy.Async;
using Fantasy.Network.Interface;
namespace NB.Chat;
public class
C2S_GetOfflineMessageRequestHandler : RouteRPC<SocialUnit, C2S_GetOfflineMessageRequest,
S2C_GetOfflineMessageResponse>
{
protected override async FTask Run(SocialUnit entity, C2S_GetOfflineMessageRequest request,
S2C_GetOfflineMessageResponse response,
Action reply)
{
var chatUnitManage = entity.Scene.GetComponent<SocialUnitManageComponent>();
if (chatUnitManage.NotSendMessage.TryGetValue(entity.Id, out var list))
{
response.Message.AddRange(list);
chatUnitManage.NotSendMessage.RemoveByKey(entity.Id);
}
await FTask.CompletedTask;
}
}

View File

@@ -0,0 +1,60 @@
using Fantasy;
using Fantasy.Async;
using Fantasy.Network.Interface;
namespace NB.Chat;
/// <summary>
/// 请求进入频道
/// </summary>
public class
C2S_JoinChannelRequestHandler : RouteRPC<SocialUnit, C2S_JoinChannelRequest, S2C_JoinChannelResponse>
{
protected override async FTask Run(SocialUnit entity, C2S_JoinChannelRequest request,
S2C_JoinChannelResponse response, Action reply)
{
if (request.Target < 1)
{
response.ErrorCode = ErrorCode.ErrArgs;
return;
}
var channelCenter = entity.Scene.GetComponent<ChatChannelCenterComponent>();
if (channelCenter == null)
{
response.ErrorCode = ErrorCode.ErrServer;
return;
}
var oldChannelId = entity.CurrentChannel;
if (oldChannelId > 0)
{
//退出旧的频道
var oldChannel = await channelCenter.Get(oldChannelId);
if (oldChannel != null)
{
oldChannel.Exit(entity);
}
}
//加入新频道
var newChannel = await channelCenter.Get(request.Target);
if (newChannel != null)
{
if (newChannel.ChannelType == 1)
{
//工会频道需要再工会才能加入
}
else if (newChannel.ChannelType == 0)
{
//地图频道需要判断在这个地图才能加入
}
newChannel.Enter(entity);
}
else
{
response.ErrorCode = ErrorCode.ChatNotChannel;
}
}
}

View File

@@ -0,0 +1,41 @@
using System.Text;
using Fantasy;
using Fantasy.Async;
using Fantasy.Helper;
using Fantasy.Network.Interface;
namespace NB.Chat;
public sealed class
C2S_SendMessageRequestHandler : RouteRPC<SocialUnit, C2S_SendMessageRequest, S2C_SendMessageResponse>
{
protected override async FTask Run(SocialUnit socialUnit, C2S_SendMessageRequest request,
S2C_SendMessageResponse response, Action reply)
{
if (request.Target < 1)
{
response.ErrorCode = ErrorCode.ErrArgs;
return;
}
if (request.Type == 0) //频道聊天
{
SocialSceneHelper.BroadcastChannel(socialUnit.Scene, request.Target, new ChatMessageInfo()
{
SendTime = TimeHelper.Now,
Content = request.Message,
});
}
else if (request.Type == 1) //私聊
{
//发送私聊
SocialSceneHelper.PrivateMessage(socialUnit.Scene, socialUnit.Id, request.Target, new ChatMessageInfo()
{
SendTime = TimeHelper.Now,
Content = request.Message,
});
}
await FTask.CompletedTask;
}
}

View File

@@ -0,0 +1,22 @@
using Fantasy;
using Fantasy.Async;
using Fantasy.Network.Interface;
using NB.Game;
namespace NB.Chat;
public class G2S_EnterRequestHandler : RouteRPC<Scene, G2S_EnterRequest, S2G_EnterResponse>
{
protected override async FTask Run(Scene scene, G2S_EnterRequest request, S2G_EnterResponse response,
Action reply)
{
var roleId = request.Role.RoleId;
Log.Debug($"收到 G2S_EnterRequestHandler {roleId}");
// 在缓存中检查该账号是否存在
var chatUnitManageComponent = scene.GetComponent<SocialUnitManageComponent>();
var account = await chatUnitManageComponent.Online(scene, request.Role, request.GateRouteId);
response.RoleRouteId = account.RuntimeId;
}
}

View File

@@ -0,0 +1,16 @@
using Fantasy;
using Fantasy.Async;
using Fantasy.Network.Interface;
namespace NB.Chat;
public class G2S_ExitRequestHandler : RouteRPC<Scene, G2S_ExitRequest, S2G_ExitResponse>
{
protected override async FTask Run(Scene scene, G2S_ExitRequest request, S2G_ExitResponse response,
Action reply)
{
// 在缓存中检查该账号是否存在
var chatUnitManageComponent = scene.GetComponent<SocialUnitManageComponent>();
await chatUnitManageComponent.Offline(scene, request.AccountId, request.GateRouteId);
}
}

View File

@@ -0,0 +1,20 @@
using Fantasy;
using Fantasy.Entitas;
namespace NB.Chat;
public static class ChatUnitFactory
{
/// <summary>
/// 创建一个新的Player
/// </summary>
/// <param name="scene"></param>
/// <param name="aId">ToKen令牌传递过来的aId</param>
/// <param name="isSaveDataBase">是否在创建的过程中保存到数据库</param>
/// <returns></returns>
public static SocialUnit Create(Scene scene, long aId)
{
var player = Entity.Create<SocialUnit>(scene, aId, true, true);
return player;
}
}

View File

@@ -0,0 +1,173 @@
using Fantasy;
using Fantasy.Async;
using Fantasy.Platform.Net;
namespace NB.Chat;
public static class SocialSceneHelper
{
#region
/// <summary>
/// 发送一条私聊
/// </summary>
/// <param name="scene"></param>
/// <param name="selfId"></param>
/// <param name="targetId"></param>
/// <param name="message"></param>
public static void PrivateMessage(Scene scene, long selfId, long targetId, ChatMessageInfo message)
{
var chatUnitManage = scene.GetComponent<SocialUnitManageComponent>();
if (chatUnitManage == null) return;
var chatUnit = chatUnitManage.Get(targetId);
message.Type = 1;
message.Source = selfId;
if (chatUnit != null)
{
//对方在线
BroadcastAll(scene, message, [targetId]);
}
else
{
//对方不在线,存入待领取列表,等待玩家下次登录领取
chatUnitManage.SaveOfflineMessage(targetId, message);
}
}
/// <summary>
/// 发送一条地图消息
/// </summary>
/// <param name="scene"></param>
/// <param name="mapId"></param>
/// <param name="message"></param>
public static void BroadcastMap(Scene scene, long mapId, ChatMessageInfo message)
{
var chatUnitManage = scene.GetComponent<SocialUnitManageComponent>();
if (chatUnitManage == null) return;
HashSet<long> targets = new HashSet<long>();
foreach (var (_, chatUnit) in chatUnitManage.Units)
{
if (chatUnit.MapId == mapId)
{
targets.Add(chatUnit.Id);
}
}
if (targets.Count < 1)
{
Log.Info("地图在线人数为0群发取消");
return;
}
BroadcastAll(scene, message, targets);
}
/// <summary>
/// 发送一条频道消息
/// </summary>
/// <param name="scene"></param>
/// <param name="channelId"></param>
/// <param name="message"></param>
public static void BroadcastChannel(Scene scene, long channelId, ChatMessageInfo message)
{
var chatUnitManage = scene.GetComponent<SocialUnitManageComponent>();
if (chatUnitManage == null) return;
HashSet<long> targets = new HashSet<long>();
foreach (var (_, chatUnit) in chatUnitManage.Units)
{
if (chatUnit.CurrentChannel == channelId)
{
targets.Add(chatUnit.Id);
}
}
if (targets.Count < 1)
{
Log.Info("频道在线人数为0群发取消");
return;
}
BroadcastAll(scene, message, targets);
}
/// <summary>
/// 广播消息给所有人
/// </summary>
/// <param name="scene"></param>
/// <param name="message"></param>
/// <param name="targets"></param>
public static void BroadcastAll(Scene scene, ChatMessageInfo message, HashSet<long>? targets = null)
{
//发送给所有Gate服务器让Gate转发给其他客户端
var gateConfigs = SceneConfigData.Instance.GetSceneBySceneType(SceneType.Gate);
var sendMessage = new S2G_ChatMessage()
{
Message = message,
};
if (targets != null && targets.Count > 0)
{
sendMessage.IdList.AddRange(targets);
}
var networkMessagingComponent = scene.NetworkMessagingComponent;
foreach (var config in gateConfigs)
{
//发送给Gate服务器转发消息
networkMessagingComponent.SendInnerRoute(config.RouteId, sendMessage);
}
}
#endregion
#region 线线
public static async FTask<(long, long)> Online(Scene scene, RoleSimpleInfo roleSimple, long gateRuntimeId)
{
var gameSceneConfig = GetSceneConfig();
var gameRouteId = gameSceneConfig.RouteId;
//连接到游戏中心服
var gameResponse = (S2G_EnterResponse)await scene.NetworkMessagingComponent.CallInnerRoute(
gameRouteId, new G2S_EnterRequest()
{
Role = roleSimple,
GateRouteId = gateRuntimeId
});
if (gameResponse.ErrorCode != 0)
{
return (0, 0);
}
return (gameResponse.RoleRouteId, gameRouteId);
// return gameRouteId;
}
public static async FTask Offline(Scene scene, long accountId, long gateRuntimeId, long sceneRouteId)
{
for (int i = 0; i < 10; i++)
{
var gameResponse = (S2G_ExitResponse)await scene.NetworkMessagingComponent.CallInnerRoute(
sceneRouteId, new G2S_ExitRequest()
{
AccountId = accountId,
GateRouteId = gateRuntimeId
});
if (gameResponse.ErrorCode == 0)
{
return;
}
}
Log.Error("重试多次还是退出失败,需检查");
}
private static SceneConfig GetSceneConfig()
{
var gameSceneConfigs = SceneConfigData.Instance.GetSceneBySceneType(SceneType.Social);
return gameSceneConfigs.First();
}
#endregion
}

View File

@@ -0,0 +1,81 @@
using Fantasy.Async;
using Fantasy.Entitas;
using Fantasy.Entitas.Interface;
using Fantasy.Helper;
namespace NB.Chat;
public class ChatChannelCenterComponentAwakeSystem : AwakeSystem<ChatChannelCenterComponent>
{
protected override void Awake(ChatChannelCenterComponent self)
{
}
}
public class ChatChannelCenterComponentDestroySystem : DestroySystem<ChatChannelCenterComponent>
{
protected override void Destroy(ChatChannelCenterComponent self)
{
self.Channels.Clear();
}
}
public static class ChatChannelCenterComponentSystem
{
/// <summary>
/// 获取一个频道
/// </summary>
/// <param name="self"></param>
/// <param name="channelId"></param>
/// <returns></returns>
public static async FTask<ChatChannel?> Get(this ChatChannelCenterComponent self, long channelId)
{
if (self.Channels.TryGetValue(channelId, out var channel))
{
return channel;
}
//查数据库
channel = await self.Scene.World.DataBase.Query<ChatChannel>(channelId, true);
if (channel != null)
{
self.Channels.Add(channel.Id, channel);
}
return channel;
}
/// <summary>
/// 申请创建一个频道
/// </summary>
/// <param name="self"></param>
/// <param name="unit"></param>
/// <returns></returns>
public static async FTask<ChatChannel> Create(this ChatChannelCenterComponent self, SocialUnit unit)
{
var channel = Entity.Create<ChatChannel>(self.Scene, true, true);
channel.Creator = unit.Role.RoleId;
channel.CreateTime = TimeHelper.Now;
self.Channels.Add(channel.Id, channel);
//保存到数据库
await self.Scene.World.DataBase.Save(channel);
return channel;
}
/// <summary>
/// 解散
/// </summary>
/// <param name="self"></param>
/// <param name="channelId"></param>
public static void Disband(this ChatChannelCenterComponent self, long channelId)
{
if (self.Channels.Remove(channelId, out var channel))
{
return;
}
channel?.Dispose();
}
}

View File

@@ -0,0 +1,28 @@
using Fantasy;
namespace NB.Chat;
public static class ChatChannelSystem
{
/// <summary>
/// 进入频道
/// </summary>
/// <param name="channel"></param>
/// <param name="unit"></param>
public static void Enter(this ChatChannel channel, SocialUnit unit)
{
channel.Units.Add(unit.Id);
unit.CurrentChannel = channel.Id;
}
/// <summary>
/// 离开频道
/// </summary>
/// <param name="channel"></param>
/// <param name="unit"></param>
public static void Exit(this ChatChannel channel, SocialUnit unit)
{
channel.Units.Remove(unit.Id);
unit.CurrentChannel = 0;
}
}

View File

@@ -0,0 +1,131 @@
using Fantasy;
using Fantasy.Async;
using Fantasy.Entitas;
using NB.Game;
namespace NB.Chat;
public static class ChatUnitManageComponentSystem
{
#region
/// <summary>
/// 离线消息,进入待领取队列
/// </summary>
/// <param name="self"></param>
/// <param name="targetId"></param>
/// <param name="message"></param>
public static void SaveOfflineMessage(this SocialUnitManageComponent self, long targetId, ChatMessageInfo message)
{
self.NotSendMessage.Add(targetId, message);
}
#endregion
#region 线线
/// <summary>
/// 玩家上线
/// </summary>
/// <param name="self"></param>
/// <param name="scene"></param>
/// <param name="roleSimpleInfo"></param>
/// <param name="gateRouteId"></param>
public static async FTask<SocialUnit?> Online(this SocialUnitManageComponent 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);
}
if (account != null)
{
await account.TryComponent<MailComponent>();
account.GateRouteId = gateRouteId;
account.Role = roleSimpleInfo;
}
await FTask.CompletedTask;
return account;
}
public static async FTask Offline(this SocialUnitManageComponent self, Scene scene, long accountId, long gateRouteId)
{
if (self.TryGet(accountId, out var unit) && unit != null)
{
if (unit.GateRouteId == gateRouteId)
{
Log.Info("退出当前聊天服==");
self.Remove(accountId); //如果当前网关和下线的网关一致
}
}
await FTask.CompletedTask;
}
#endregion
#region &
public static void Add(this SocialUnitManageComponent self, SocialUnit account)
{
self.Units.Add(account.Id, account);
}
public static SocialUnit? Get(this SocialUnitManageComponent self, long accountId)
{
return self.Units.GetValueOrDefault(accountId);
}
public static bool TryGet(this SocialUnitManageComponent self, long accountId, out SocialUnit? account)
{
return self.Units.TryGetValue(accountId, out account);
}
public static void Remove(this SocialUnitManageComponent self, long accountId, bool isDispose = true)
{
if (!self.Units.Remove(accountId, out var account))
{
return;
}
if (!isDispose)
{
return;
}
account.Dispose();
}
#endregion
#region
/// <summary>
/// 尝试给增加相关组件
/// </summary>
/// <param name="socialUnit"></param>
/// <typeparam name="T"></typeparam>
public static async FTask TryComponent<T>(this SocialUnit socialUnit) where T : Entity, new()
{
if (socialUnit.GetComponent<T>() == null)
{
var mailComponent = await socialUnit.Scene.World.DataBase.Query<T>(socialUnit.Id, true);
if (mailComponent == null)
{
//如果没有邮件组件
socialUnit.AddComponent<T>();
}
else
{
socialUnit.AddComponent(mailComponent);
}
}
}
#endregion
}

View File

@@ -0,0 +1,11 @@
using Fantasy.Entitas.Interface;
namespace NB.Chat;
public class ChatUnitSystem : AwakeSystem<SocialUnit>
{
protected override void Awake(SocialUnit self)
{
}
}