103 lines
2.6 KiB
C#
103 lines
2.6 KiB
C#
using Fantasy;
|
|
using Fantasy.Async;
|
|
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 ChatUnitManageComponent 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<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);
|
|
}
|
|
|
|
if (account != null)
|
|
{
|
|
account.GateRouteId = gateRouteId;
|
|
account.Role = roleSimpleInfo;
|
|
}
|
|
|
|
await FTask.CompletedTask;
|
|
return account;
|
|
}
|
|
|
|
public static async FTask Offline(this ChatUnitManageComponent 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 ChatUnitManageComponent self, ChatUnit account)
|
|
{
|
|
self.ChatUnits.Add(account.Id, account);
|
|
}
|
|
|
|
public static ChatUnit? Get(this ChatUnitManageComponent self, long accountId)
|
|
{
|
|
return self.ChatUnits.GetValueOrDefault(accountId);
|
|
}
|
|
|
|
public static bool TryGet(this ChatUnitManageComponent self, long accountId, out ChatUnit? account)
|
|
{
|
|
return self.ChatUnits.TryGetValue(accountId, out account);
|
|
}
|
|
|
|
public static void Remove(this ChatUnitManageComponent self, long accountId, bool isDispose = true)
|
|
{
|
|
if (!self.ChatUnits.Remove(accountId, out var account))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!isDispose)
|
|
{
|
|
return;
|
|
}
|
|
|
|
account.Dispose();
|
|
}
|
|
|
|
#endregion
|
|
} |