大修改

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

@@ -1,45 +0,0 @@
using System.Text;
using Fantasy;
using Fantasy.Async;
using Fantasy.Network.Interface;
namespace NB.Chat;
public sealed class
C2Chat_SendMessageRequestHandler : RouteRPC<ChatUnit, C2Chat_SendMessageRequest, Caht2C_SendMessageResponse>
{
protected override async FTask Run(ChatUnit chatUnit, C2Chat_SendMessageRequest request,
Caht2C_SendMessageResponse response, Action reply)
{
if (request.Target < 1)
{
response.ErrorCode = ErrorCode.ErrArgs;
return;
}
if (request.Type == 0) //频道聊天
{
ChatSceneHelper.BroadcastChannel(chatUnit.Scene, request.Target, new ChatMessageInfo()
{
Content = request.Message,
});
}
else if (request.Type == 1) //私聊
{
//发送私聊
ChatSceneHelper.PrivateMessage(chatUnit.Scene, chatUnit.Id, request.Target, new ChatMessageInfo()
{
Content = request.Message,
});
}
else if (request.Type == 3) //广播聊天
{
ChatSceneHelper.BroadcastAll(chatUnit.Scene, new ChatMessageInfo()
{
Content = request.Message,
});
}
await FTask.CompletedTask;
}
}

View File

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

View File

@@ -105,21 +105,6 @@ public static class PlayerManageComponentSystem
account.Statistics.LoginTime = TimeHelper.Now;
if (account.GetComponent<MailComponent>() == null)
{
var mailComponent = await scene.World.DataBase.Query<MailComponent>(account.Id, true);
if (mailComponent == null)
{
//如果没有邮件组件
account.AddComponent<MailComponent>();
}
else
{
account.AddComponent(mailComponent);
}
}
await account.Save();
return account;

View File

@@ -7,11 +7,11 @@ using Fantasy.PacketParser;
namespace NB.Gate;
public class Chat2G_ChatMessageHandler : Route<Scene, Chat2G_ChatMessage>
public class S2G_ChatMessageHandler : Route<Scene, S2G_ChatMessage>
{
protected override async FTask Run(Scene scene, Chat2G_ChatMessage message)
protected override async FTask Run(Scene scene, S2G_ChatMessage message)
{
var chatMessage = new Chat2C_Message()
var chatMessage = new S2C_Message()
{
Message = message.Message,
};

View File

@@ -58,13 +58,13 @@ public static class GateLoginHelper
//安排进入的聊天服
var (chatRouteId, sceneRouteId) =
await ChatSceneHelper.Online(session.Scene, roleSimpleInfo, session.RuntimeId);
await SocialSceneHelper.Online(session.Scene, roleSimpleInfo, session.RuntimeId);
if (chatRouteId <= 0)
{
return ErrorCode.OnlineSceneFailed;
}
routeComponent.AddAddress(RouteType.ChatRoute, chatRouteId);
routeComponent.AddAddress(RouteType.SocialRoute, chatRouteId);
gateUnit.ChatSceneRouteId = sceneRouteId;
Log.Info($"连接聊天服成功gameRouteId:{gameRouteId}");
return ErrorCode.Successful;
@@ -84,7 +84,7 @@ public static class GateLoginHelper
//通知服务器下线
Log.Info($"断线的session id={sessionId} ChatSceneRouteId={gateUnit.ChatSceneRouteId}");
await GameSceneHelper.Offline(gateUnit.Scene, gateUnit.AccountID, sessionId);
await ChatSceneHelper.Offline(gateUnit.Scene, gateUnit.AccountID, sessionId,
await SocialSceneHelper.Offline(gateUnit.Scene, gateUnit.AccountID, sessionId,
gateUnit.ChatSceneRouteId);
return ErrorCode.Successful;
}

View File

@@ -67,7 +67,7 @@ public sealed class OnCreateSceneEvent : AsyncEventSystem<OnCreateScene>
Log.Debug($"Map Scene SceneRuntimeId:{scene.RuntimeId}");
break;
}
case SceneType.Chat:
case SceneType.Social:
{
break;
}

View File

@@ -38,10 +38,10 @@ public class OnSceneCreate_Init : AsyncEventSystem<OnCreateScene>
scene.AddComponent<PlayerManageComponent>();
break;
}
case SceneType.Chat:
case SceneType.Social:
{
//用于管理玩家的组件
scene.AddComponent<ChatUnitManageComponent>();
scene.AddComponent<SocialUnitManageComponent>();
scene.AddComponent<ChatChannelCenterComponent>();
break;
}

View File

@@ -8,10 +8,10 @@ namespace NB.Chat;
/// 请求创建频道
/// </summary>
public class
C2Chat_CreateChannelRequestHandler : RouteRPC<ChatUnit, C2Chat_CreateChannelRequest, Caht2C_CreateChannelResponse>
C2S_CreateChannelRequestHandler : RouteRPC<SocialUnit, C2S_CreateChannelRequest, S2C_CreateChannelResponse>
{
protected override async FTask Run(ChatUnit entity, C2Chat_CreateChannelRequest request,
Caht2C_CreateChannelResponse response, Action reply)
protected override async FTask Run(SocialUnit entity, C2S_CreateChannelRequest request,
S2C_CreateChannelResponse response, Action reply)
{
var channelCenter = entity.Scene.GetComponent<ChatChannelCenterComponent>();
if (channelCenter == null)

View File

@@ -5,14 +5,14 @@ using Fantasy.Network.Interface;
namespace NB.Chat;
public class
C2Chat_GetOfflineMessageRequestHandler : RouteRPC<ChatUnit, C2Chat_GetOfflineMessageRequest,
Caht2C_GetOfflineMessageResponse>
C2S_GetOfflineMessageRequestHandler : RouteRPC<SocialUnit, C2S_GetOfflineMessageRequest,
S2C_GetOfflineMessageResponse>
{
protected override async FTask Run(ChatUnit entity, C2Chat_GetOfflineMessageRequest request,
Caht2C_GetOfflineMessageResponse response,
protected override async FTask Run(SocialUnit entity, C2S_GetOfflineMessageRequest request,
S2C_GetOfflineMessageResponse response,
Action reply)
{
var chatUnitManage = entity.Scene.GetComponent<ChatUnitManageComponent>();
var chatUnitManage = entity.Scene.GetComponent<SocialUnitManageComponent>();
if (chatUnitManage.NotSendMessage.TryGetValue(entity.Id, out var list))
{
response.Message.AddRange(list);

View File

@@ -8,16 +8,17 @@ namespace NB.Chat;
/// 请求进入频道
/// </summary>
public class
C2Chat_JoinChannelRequestHandler : RouteRPC<ChatUnit, C2Chat_JoinChannelRequest, Caht2C_JoinChannelResponse>
C2S_JoinChannelRequestHandler : RouteRPC<SocialUnit, C2S_JoinChannelRequest, S2C_JoinChannelResponse>
{
protected override async FTask Run(ChatUnit entity, C2Chat_JoinChannelRequest request,
Caht2C_JoinChannelResponse response, Action reply)
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)
{
@@ -40,6 +41,15 @@ public class
var newChannel = await channelCenter.Get(request.Target);
if (newChannel != null)
{
if (newChannel.ChannelType == 1)
{
//工会频道需要再工会才能加入
}
else if (newChannel.ChannelType == 0)
{
//地图频道需要判断在这个地图才能加入
}
newChannel.Enter(entity);
}
else

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

@@ -5,16 +5,16 @@ using NB.Game;
namespace NB.Chat;
public class G2Chat_EnterRequestHandler : RouteRPC<Scene, G2Chat_EnterRequest, Chat2G_EnterResponse>
public class G2S_EnterRequestHandler : RouteRPC<Scene, G2S_EnterRequest, S2G_EnterResponse>
{
protected override async FTask Run(Scene scene, G2Chat_EnterRequest request, Chat2G_EnterResponse response,
protected override async FTask Run(Scene scene, G2S_EnterRequest request, S2G_EnterResponse response,
Action reply)
{
var roleId = request.Role.RoleId;
Log.Debug($"收到 G2Chat_EnterRequestHandler {roleId}");
Log.Debug($"收到 G2S_EnterRequestHandler {roleId}");
// 在缓存中检查该账号是否存在
var chatUnitManageComponent = scene.GetComponent<ChatUnitManageComponent>();
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

@@ -12,9 +12,9 @@ public static class ChatUnitFactory
/// <param name="aId">ToKen令牌传递过来的aId</param>
/// <param name="isSaveDataBase">是否在创建的过程中保存到数据库</param>
/// <returns></returns>
public static ChatUnit Create(Scene scene, long aId)
public static SocialUnit Create(Scene scene, long aId)
{
var player = Entity.Create<ChatUnit>(scene, aId, true, true);
var player = Entity.Create<SocialUnit>(scene, aId, true, true);
return player;
}
}

View File

@@ -4,7 +4,7 @@ using Fantasy.Platform.Net;
namespace NB.Chat;
public static class ChatSceneHelper
public static class SocialSceneHelper
{
#region
@@ -17,7 +17,7 @@ public static class ChatSceneHelper
/// <param name="message"></param>
public static void PrivateMessage(Scene scene, long selfId, long targetId, ChatMessageInfo message)
{
var chatUnitManage = scene.GetComponent<ChatUnitManageComponent>();
var chatUnitManage = scene.GetComponent<SocialUnitManageComponent>();
if (chatUnitManage == null) return;
var chatUnit = chatUnitManage.Get(targetId);
message.Type = 1;
@@ -42,10 +42,10 @@ public static class ChatSceneHelper
/// <param name="message"></param>
public static void BroadcastMap(Scene scene, long mapId, ChatMessageInfo message)
{
var chatUnitManage = scene.GetComponent<ChatUnitManageComponent>();
var chatUnitManage = scene.GetComponent<SocialUnitManageComponent>();
if (chatUnitManage == null) return;
HashSet<long> targets = new HashSet<long>();
foreach (var (_, chatUnit) in chatUnitManage.ChatUnits)
foreach (var (_, chatUnit) in chatUnitManage.Units)
{
if (chatUnit.MapId == mapId)
{
@@ -70,10 +70,10 @@ public static class ChatSceneHelper
/// <param name="message"></param>
public static void BroadcastChannel(Scene scene, long channelId, ChatMessageInfo message)
{
var chatUnitManage = scene.GetComponent<ChatUnitManageComponent>();
var chatUnitManage = scene.GetComponent<SocialUnitManageComponent>();
if (chatUnitManage == null) return;
HashSet<long> targets = new HashSet<long>();
foreach (var (_, chatUnit) in chatUnitManage.ChatUnits)
foreach (var (_, chatUnit) in chatUnitManage.Units)
{
if (chatUnit.CurrentChannel == channelId)
{
@@ -100,7 +100,7 @@ public static class ChatSceneHelper
{
//发送给所有Gate服务器让Gate转发给其他客户端
var gateConfigs = SceneConfigData.Instance.GetSceneBySceneType(SceneType.Gate);
var sendMessage = new Chat2G_ChatMessage()
var sendMessage = new S2G_ChatMessage()
{
Message = message,
};
@@ -127,8 +127,8 @@ public static class ChatSceneHelper
var gameSceneConfig = GetSceneConfig();
var gameRouteId = gameSceneConfig.RouteId;
//连接到游戏中心服
var gameResponse = (Chat2G_EnterResponse)await scene.NetworkMessagingComponent.CallInnerRoute(
gameRouteId, new G2Chat_EnterRequest()
var gameResponse = (S2G_EnterResponse)await scene.NetworkMessagingComponent.CallInnerRoute(
gameRouteId, new G2S_EnterRequest()
{
Role = roleSimple,
GateRouteId = gateRuntimeId
@@ -147,8 +147,8 @@ public static class ChatSceneHelper
{
for (int i = 0; i < 10; i++)
{
var gameResponse = (Chat2G_ExitResponse)await scene.NetworkMessagingComponent.CallInnerRoute(
sceneRouteId, new G2Chat_ExitRequest()
var gameResponse = (S2G_ExitResponse)await scene.NetworkMessagingComponent.CallInnerRoute(
sceneRouteId, new G2S_ExitRequest()
{
AccountId = accountId,
GateRouteId = gateRuntimeId
@@ -164,7 +164,7 @@ public static class ChatSceneHelper
private static SceneConfig GetSceneConfig()
{
var gameSceneConfigs = SceneConfigData.Instance.GetSceneBySceneType(SceneType.Chat);
var gameSceneConfigs = SceneConfigData.Instance.GetSceneBySceneType(SceneType.Social);
return gameSceneConfigs.First();
}

View File

@@ -51,7 +51,7 @@ public static class ChatChannelCenterComponentSystem
/// <param name="self"></param>
/// <param name="unit"></param>
/// <returns></returns>
public static async FTask<ChatChannel> Create(this ChatChannelCenterComponent self, ChatUnit unit)
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;

View File

@@ -1,4 +1,6 @@
namespace NB.Chat;
using Fantasy;
namespace NB.Chat;
public static class ChatChannelSystem
{
@@ -7,7 +9,7 @@ public static class ChatChannelSystem
/// </summary>
/// <param name="channel"></param>
/// <param name="unit"></param>
public static void Enter(this ChatChannel channel, ChatUnit unit)
public static void Enter(this ChatChannel channel, SocialUnit unit)
{
channel.Units.Add(unit.Id);
unit.CurrentChannel = channel.Id;
@@ -18,7 +20,7 @@ public static class ChatChannelSystem
/// </summary>
/// <param name="channel"></param>
/// <param name="unit"></param>
public static void Exit(this ChatChannel channel, ChatUnit unit)
public static void Exit(this ChatChannel channel, SocialUnit unit)
{
channel.Units.Remove(unit.Id);
unit.CurrentChannel = 0;

View File

@@ -1,5 +1,6 @@
using Fantasy;
using Fantasy.Async;
using Fantasy.Entitas;
using NB.Game;
namespace NB.Chat;
@@ -14,11 +15,11 @@ public static class ChatUnitManageComponentSystem
/// <param name="self"></param>
/// <param name="targetId"></param>
/// <param name="message"></param>
public static void SaveOfflineMessage(this ChatUnitManageComponent self, long targetId, ChatMessageInfo message)
public static void SaveOfflineMessage(this SocialUnitManageComponent self, long targetId, ChatMessageInfo message)
{
self.NotSendMessage.Add(targetId, message);
}
#endregion
#region 线线
@@ -30,7 +31,7 @@ public static class ChatUnitManageComponentSystem
/// <param name="scene"></param>
/// <param name="roleSimpleInfo"></param>
/// <param name="gateRouteId"></param>
public static async FTask<ChatUnit?> Online(this ChatUnitManageComponent self, Scene scene,
public static async FTask<SocialUnit?> Online(this SocialUnitManageComponent self, Scene scene,
RoleSimpleInfo roleSimpleInfo,
long gateRouteId)
{
@@ -43,6 +44,7 @@ public static class ChatUnitManageComponentSystem
if (account != null)
{
await account.TryComponent<MailComponent>();
account.GateRouteId = gateRouteId;
account.Role = roleSimpleInfo;
}
@@ -51,7 +53,7 @@ public static class ChatUnitManageComponentSystem
return account;
}
public static async FTask Offline(this ChatUnitManageComponent self, Scene scene, long accountId, long gateRouteId)
public static async FTask Offline(this SocialUnitManageComponent self, Scene scene, long accountId, long gateRouteId)
{
if (self.TryGet(accountId, out var unit) && unit != null)
{
@@ -69,24 +71,24 @@ public static class ChatUnitManageComponentSystem
#region &
public static void Add(this ChatUnitManageComponent self, ChatUnit account)
public static void Add(this SocialUnitManageComponent self, SocialUnit account)
{
self.ChatUnits.Add(account.Id, account);
self.Units.Add(account.Id, account);
}
public static ChatUnit? Get(this ChatUnitManageComponent self, long accountId)
public static SocialUnit? Get(this SocialUnitManageComponent self, long accountId)
{
return self.ChatUnits.GetValueOrDefault(accountId);
return self.Units.GetValueOrDefault(accountId);
}
public static bool TryGet(this ChatUnitManageComponent self, long accountId, out ChatUnit? account)
public static bool TryGet(this SocialUnitManageComponent self, long accountId, out SocialUnit? account)
{
return self.ChatUnits.TryGetValue(accountId, out account);
return self.Units.TryGetValue(accountId, out account);
}
public static void Remove(this ChatUnitManageComponent self, long accountId, bool isDispose = true)
public static void Remove(this SocialUnitManageComponent self, long accountId, bool isDispose = true)
{
if (!self.ChatUnits.Remove(accountId, out var account))
if (!self.Units.Remove(accountId, out var account))
{
return;
}
@@ -100,4 +102,30 @@ public static class ChatUnitManageComponentSystem
}
#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)
{
}
}