修改为luban
This commit is contained in:
@@ -1,74 +1,42 @@
|
||||
using Fantasy.Async;
|
||||
using Fantasy.Entitas;
|
||||
using Fantasy.Entitas;
|
||||
using Fantasy.Entitas.Interface;
|
||||
using Fantasy.Helper;
|
||||
|
||||
#pragma warning disable CS8602 // Dereference of a possibly null reference.
|
||||
#pragma warning disable CS8601 // Possible null reference assignment.
|
||||
|
||||
namespace NB.Chat;
|
||||
|
||||
public class ChatChannelCenterComponentAwakeSystem : AwakeSystem<ChatChannelCenterComponent>
|
||||
{
|
||||
protected override void Awake(ChatChannelCenterComponent self)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class ChatChannelCenterComponentDestroySystem : DestroySystem<ChatChannelCenterComponent>
|
||||
public sealed class ChatChannelCenterComponentDestroySystem : DestroySystem<ChatChannelCenterComponent>
|
||||
{
|
||||
protected override void Destroy(ChatChannelCenterComponent self)
|
||||
{
|
||||
foreach (var chatChannelComponent in self.Channels.Values.ToArray())
|
||||
{
|
||||
chatChannelComponent.Dispose();
|
||||
}
|
||||
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)
|
||||
public static ChatChannelComponent Apply(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);
|
||||
}
|
||||
|
||||
channel = Entity.Create<ChatChannelComponent>(self.Scene, channelId, true, true);
|
||||
self.Channels.Add(channelId, 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)
|
||||
public static bool TryGet(this ChatChannelCenterComponent self, long channelId, out ChatChannelComponent channel)
|
||||
{
|
||||
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;
|
||||
return self.Channels.TryGetValue(channelId, out 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))
|
||||
@@ -76,6 +44,6 @@ public static class ChatChannelCenterComponentSystem
|
||||
return;
|
||||
}
|
||||
|
||||
channel?.Dispose();
|
||||
channel.Dispose();
|
||||
}
|
||||
}
|
||||
89
Hotfix/Social/Chat/System/ChatChannelComponentSystem.cs
Normal file
89
Hotfix/Social/Chat/System/ChatChannelComponentSystem.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
using Fantasy;
|
||||
using Fantasy.Entitas;
|
||||
using NBF.Social;
|
||||
|
||||
namespace NB.Chat;
|
||||
|
||||
public class MemoryEntity : Entity
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static class ChatChannelComponentSystem
|
||||
{
|
||||
public static void Send(this ChatChannelComponent self, ChatInfoTree tree)
|
||||
{
|
||||
var chatUnitManageComponent = self.Scene.GetComponent<SocialUnitManageComponent>();
|
||||
var networkMessagingComponent = self.Scene.NetworkMessagingComponent;
|
||||
var chatMessage = new Chat2C_Message()
|
||||
{
|
||||
ChatInfoTree = tree
|
||||
};
|
||||
|
||||
foreach (var unitId in self.Units)
|
||||
{
|
||||
if (!chatUnitManageComponent.Units.TryGetValue(unitId, out var chatUnit))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// networkMessagingComponent.SendInnerRoute(chatUnit.GateRouteId, chatMessage);
|
||||
networkMessagingComponent.Send(chatUnit.GateRouteId, chatMessage);
|
||||
}
|
||||
}
|
||||
|
||||
public static bool JoinChannel(this ChatChannelComponent self, long chatUnitId)
|
||||
{
|
||||
var chatUnitManageComponent = self.Scene.GetComponent<SocialUnitManageComponent>();
|
||||
|
||||
if (!chatUnitManageComponent.TryGet(chatUnitId, out var chatUnit))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// 将当前频道中加入该用户。
|
||||
self.Units.Add(chatUnitId);
|
||||
// 给用户添加频道。
|
||||
if (!chatUnit.Channels.ContainsKey(self.Id))
|
||||
{
|
||||
chatUnit.Channels.Add(self.Id, self);
|
||||
}
|
||||
// 可以在这里给客户端发送一个加入频道成功的消息。
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool IsJoinedChannel(this ChatChannelComponent self, long chatUnitId)
|
||||
{
|
||||
return self.Units.Contains(chatUnitId);
|
||||
}
|
||||
|
||||
public static void ExitChannel(this ChatChannelComponent self, long chatUnitId, bool isRemoveUnitChannel = true)
|
||||
{
|
||||
if (!self.Units.Contains(chatUnitId))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var chatUnitManageComponent = self.Scene.GetComponent<SocialUnitManageComponent>();
|
||||
|
||||
if (!chatUnitManageComponent.TryGet(chatUnitId, out var chatUnit))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (isRemoveUnitChannel)
|
||||
{
|
||||
// 给用户移除频道。
|
||||
chatUnit.Channels.Remove(self.Id);
|
||||
}
|
||||
|
||||
// 在当前频道中移除该用户。
|
||||
self.Units.Remove(chatUnitId);
|
||||
// 如果当前频道中没有用户了,则销毁该频道。
|
||||
if (self.Units.Count == 0)
|
||||
{
|
||||
self.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,132 +1,70 @@
|
||||
using System.Collections.Generic;
|
||||
using Fantasy;
|
||||
using Fantasy.Async;
|
||||
using Fantasy.Entitas;
|
||||
using NB.Game;
|
||||
// using Fantasy;
|
||||
// using Fantasy.Entitas;
|
||||
// using Fantasy.Entitas.Interface;
|
||||
//
|
||||
// #pragma warning disable CS8601 // Possible null reference assignment.
|
||||
//
|
||||
// namespace NB.Chat;
|
||||
//
|
||||
// public sealed class ChatUnitManageComponentDestroySystem : DestroySystem<ChatUnitManageComponent>
|
||||
// {
|
||||
// protected override void Destroy(ChatUnitManageComponent self)
|
||||
// {
|
||||
// foreach (var chatUnit in self.Units.Values.ToArray())
|
||||
// {
|
||||
// chatUnit.Dispose();
|
||||
// }
|
||||
//
|
||||
// self.Units.Clear();
|
||||
// }
|
||||
// }
|
||||
|
||||
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="accountId"></param>
|
||||
/// <param name="gateRouteId"></param>
|
||||
public static async FTask<SocialUnit?> Online(this SocialUnitManageComponent self, Scene scene,
|
||||
long accountId,
|
||||
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
|
||||
}
|
||||
// public static class ChatUnitManageComponentSystem
|
||||
// {
|
||||
// public static ChatUnit Add(this ChatUnitManageComponent self, long unitId, string userName, long gateRouteId)
|
||||
// {
|
||||
// if (!self.Units.TryGetValue(unitId, out var chatUnit))
|
||||
// {
|
||||
// chatUnit = Entity.Create<ChatUnit>(self.Scene, unitId, true, true);
|
||||
// self.Units.Add(unitId, chatUnit);
|
||||
// Log.Debug(
|
||||
// $"Add ChatUnit Count: {self.Units.Count} UnitId: {unitId} UserName: {userName} GateRouteId: {gateRouteId}");
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// Log.Debug($"ChatUnit: {chatUnit.UserName}({chatUnit.GateRouteId})");
|
||||
// }
|
||||
//
|
||||
// chatUnit.UserName = userName;
|
||||
// chatUnit.GateRouteId = gateRouteId;
|
||||
// return chatUnit;
|
||||
// }
|
||||
//
|
||||
// public static ChatUnit? Get(this ChatUnitManageComponent self, long unitId)
|
||||
// {
|
||||
// return self.Units.GetValueOrDefault(unitId);
|
||||
// }
|
||||
//
|
||||
// public static bool TryGet(this ChatUnitManageComponent self, long unitId, out ChatUnit chatUnit)
|
||||
// {
|
||||
// return self.Units.TryGetValue(unitId, out chatUnit);
|
||||
// }
|
||||
//
|
||||
// public static void Remove(this ChatUnitManageComponent self, long unitId, bool isDispose = true)
|
||||
// {
|
||||
// // 由于退出频道的时候,也会检查该玩家是否在ChatUnitManageComponent中,所以这里不做移除操作。
|
||||
// if (!self.Units.TryGetValue(unitId, out var chatUnit))
|
||||
// {
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// if (isDispose)
|
||||
// {
|
||||
// chatUnit.Dispose();
|
||||
// }
|
||||
//
|
||||
// // 因为玩家已经执行了退出频道的操作了,所以要清除一下这个数据。
|
||||
// self.Units.Remove(unitId);
|
||||
// Log.Debug($"Remove ChatUnit: {chatUnit.UserName}({chatUnit.GateRouteId}) Count: {self.Units.Count}");
|
||||
// }
|
||||
// }
|
||||
@@ -1,11 +0,0 @@
|
||||
using Fantasy.Entitas.Interface;
|
||||
|
||||
namespace NB.Chat;
|
||||
|
||||
public class ChatUnitSystem : AwakeSystem<SocialUnit>
|
||||
{
|
||||
protected override void Awake(SocialUnit self)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user