CTT/Server/Hotfix/Game/System/UI/FriendComponentSystem.cs

223 lines
8.6 KiB
C#
Executable File

using MongoDB.Driver;
using System;
using System.Collections.Generic;
namespace ET
{
public class FriendComponentAwakeSystem : AwakeSystem<FriendComponent>
{
public override void Awake(FriendComponent self)
{
FriendComponent.Instance = self;
}
}
public class FriendComponentDestroySystem : DestroySystem<FriendComponent>
{
public override void Destroy(FriendComponent self)
{
self.FriendDic.Clear();
}
}
public static class FriendComponentSystem
{
private const int Max_Friend_Count = 50;
public static async ETTask<Friend> Query(this FriendComponent self, long id)
{
if (!self.FriendDic.TryGetValue(id, out Friend friend))
{
friend = await DBComponent.Instance.Query<Friend>(id);
if (friend == null)
{
friend = EntityFactory.CreateWithId<Friend>(self, id);
}
self.FriendDic[id] = friend;
}
return friend;
}
public static async ETTask Save(this FriendComponent self, Friend mineFriend, Friend otherFriend)
{
List<Entity> friendList = new List<Entity> { mineFriend, otherFriend };
await DBComponent.Instance.Save(mineFriend.Id, friendList);
}
public static async ETTask<(string, long)> FindFriend(this FriendComponent self, string name)
{
List<User> list = await DBComponent.Instance.Query<User>(t => t.NickName.Equals(name));
if (list == null || list.Count == 0)
{
return ("您想查找的玩家不存在,请检查昵称是否正确。", 0);
}
if (list.Count > 1)
{
Log.Error($"【重大错误】 数据库中存在多个相同昵称【{name}】,数量为{list.Count}");
}
User user = list[0];
return (string.Empty, user.Id);
}
public static async ETTask<string> GetFriend(this FriendComponent self, long id, System.Collections.Generic.List<FriendInfo> list, System.Collections.Generic.List<RequestAddFriendInfo> requestInfoList)
{
Friend friend = await self.Query(id);
await self.CollectFriendInfo(friend, list, requestInfoList);
return string.Empty;
}
public static async ETTask<IEnumerable<long>> GetFriends(this FriendComponent self, long id)
{
Friend friend = await self.Query(id);
return friend.FriendDic.Keys;
}
private static async ETTask CollectFriendInfo(this FriendComponent self,Friend friend,System.Collections.Generic.List<FriendInfo> list, System.Collections.Generic.List<RequestAddFriendInfo> requestInfoList)
{
Dictionary<long, FriendInfo> dic = friend.FriendDic;
foreach (FriendInfo info in dic.Values)
{
long lastLoginTime = -1;
if (MapUnitComponent.Instance.IsOnLine(info.Id))
{
lastLoginTime = 0;
}
else
{
User user = await UserHelper.Query(info.Id);
if (user != null)
lastLoginTime = user.lastLoginTime;
}
list.Add(new FriendInfo
{
Id = info.Id,
Level = info.Level,
Name = info.Name,
Job = info.Job,
LastLginTime = lastLoginTime
});
}
requestInfoList?.AddRange(friend.RequestList);
}
public static async ETTask<string> AddFriend(this FriendComponent self, Unit unit, long friendId)
{
if(friendId == unit.Id)
{
return "不能添加自己为好友";
}
//!判断能否添加对方
Friend mineFriend = await self.Query(unit.Id);
if (mineFriend.FriendDic.TryGetValue(friendId, out _))
{
return "此好友已经添加";
}
Friend otherFriend = await self.Query(friendId);
if (mineFriend.FriendDic.Count >= Max_Friend_Count)
{
return "您的好友已达上限";
}
if (otherFriend.FriendDic.Count >= Max_Friend_Count)
{
return "对方的好友已达上限";
}
//!被邀请列表
User user = UserComponent.Instance.Get(unit.Id);
otherFriend.RequestList.Add(new RequestAddFriendInfo
{
Id = unit.Id,
Name = user.NickName,
Level = user.Level,
Job = JobHelper.GetJobType(user.JobId),
Sex = JobHelper.GetSexType(user.JobId),
});
await self.Save(mineFriend, otherFriend);
return string.Empty;
}
public static async ETTask<string> HandleAddFriend(this FriendComponent self, Unit unit, bool isAgree, long friendId, System.Collections.Generic.List<FriendInfo> list, System.Collections.Generic.List<RequestAddFriendInfo> requestInfoList)
{
Friend mineFriend = await self.Query(unit.Id);
if (mineFriend.FriendDic.TryGetValue(friendId, out _))
{
return "此好友已经是您的好友";
}
Friend otherFriend = await self.Query(friendId);
User otherUser = await UserComponent.Instance.Query(friendId);
if (otherUser == null)
{
Log.Error($"玩家id = {otherUser.Id} 想要处理添加好友 Id = {friendId},但是不存在");
return "系统错误";
}
if (!isAgree)
{
await RemoveInrequestList(self, unit, friendId, mineFriend, otherFriend);
return string.Empty;
}
//!添加别人
mineFriend.FriendDic.Add(friendId, new FriendInfo
{
Id = friendId,
Level = otherUser.Level,
Name = otherUser.NickName,
Job = JobHelper.GetJobType(otherUser.JobId),
LastLginTime = MapUnitComponent.Instance.IsOnLine(otherUser.Id) ? 0 : otherUser.lastLoginTime
});
//!添加自己
User user = UserComponent.Instance.Get(unit.Id);
otherFriend.FriendDic.Add(unit.Id, new FriendInfo
{
Id = user.Id,
Level = user.Level,
Name = user.NickName,
Job = JobHelper.GetJobType(user.JobId),
LastLginTime = MapUnitComponent.Instance.IsOnLine(user.Id) ? 0 : user.lastLoginTime
});
await RemoveInrequestList(self, unit, friendId, mineFriend, otherFriend);
//!更新后的列表
await self.CollectFriendInfo(mineFriend, list, requestInfoList);
return string.Empty;
//!从申请列表中移除
static async ETTask RemoveInrequestList(FriendComponent self, Unit unit, long friendId, Friend mineFriend, Friend otherFriend)
{
List<RequestAddFriendInfo> mineList = mineFriend.RequestList;
for (int i = mineList.Count - 1; i >= 0; i--)
{
if (mineList[i].Id == friendId) mineList.RemoveAt(i);
}
await self.Save(mineFriend, otherFriend);
}
}
public static async ETTask<string> DeleteFriend(this FriendComponent self, Unit unit, long friendId, System.Collections.Generic.List<FriendInfo> list)
{
long id = unit.Id;
//!删除别人
Friend mineFriend = await self.Query(id);
mineFriend.FriendDic.Remove(friendId);
//!删除自己
Friend otherFriend = await self.Query(friendId);
otherFriend.FriendDic.Remove(id);
//!更新后的列表
await self.CollectFriendInfo(mineFriend, list, null);
return string.Empty;
}
/// <summary>
/// 删除一方
/// </summary>
/// <param name="self"></param>
/// <param name="id"></param>
/// <param name="friendId"></param>
/// <returns></returns>
public static async ETTask DeleteFriend(this FriendComponent self, long id, long friendId)
{
//!删除别人
Friend mineFriend = await self.Query(id);
mineFriend.FriendDic.Remove(friendId);
await DBComponent.Instance.Save(mineFriend);
}
}
}