using MongoDB.Driver; using System; using System.Collections.Generic; namespace ET { public class FriendComponentAwakeSystem : AwakeSystem { public override void Awake(FriendComponent self) { FriendComponent.Instance = self; } } public class FriendComponentDestroySystem : DestroySystem { public override void Destroy(FriendComponent self) { self.FriendDic.Clear(); } } public static class FriendComponentSystem { private const int Max_Friend_Count = 50; public static async ETTask Query(this FriendComponent self, long id) { if (!self.FriendDic.TryGetValue(id, out var friend)) { friend = await DBComponent.Instance.Query(id); if (friend == null) { friend = EntityFactory.CreateWithId(self, id); } self.FriendDic[id] = friend; } return friend; } public static async ETTask Save(this FriendComponent self, Friend mineFriend, Friend otherFriend) { var friendList = new List { mineFriend, otherFriend }; await DBComponent.Instance.Save(mineFriend.Id, friendList); } public static async ETTask<(string, long)> FindFriend(this FriendComponent self, string name) { var list = await DBComponent.Instance.Query(t => t.NickName.Equals(name)); if (list == null || list.Count == 0) { return ("您想查找的玩家不存在,请检查昵称是否正确。", 0); } if (list.Count > 1) { Log.Error($"【重大错误】 数据库中存在多个相同昵称【{name}】,数量为{list.Count}"); } var user = list[0]; return (string.Empty, user.Id); } public static async ETTask GetFriend(this FriendComponent self, long id, System.Collections.Generic.List list, System.Collections.Generic.List requestInfoList) { Friend friend = await self.Query(id); await self.CollectFriendInfo(friend, list, requestInfoList); return string.Empty; } public static async ETTask> 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 list, System.Collections.Generic.List requestInfoList) { var dic = friend.FriendDic; foreach (var info in dic.Values) { long lastLoginTime = -1; if (MapUnitComponent.Instance.IsOnLine(info.Id)) { lastLoginTime = 0; } else { var 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 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 HandleAddFriend(this FriendComponent self, Unit unit, bool isAgree, long friendId, System.Collections.Generic.List list, System.Collections.Generic.List requestInfoList) { Friend mineFriend = await self.Query(unit.Id); if (mineFriend.FriendDic.TryGetValue(friendId, out _)) { return "此好友已经是您的好友"; } Friend otherFriend = await self.Query(friendId); var 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) { var 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 DeleteFriend(this FriendComponent self, Unit unit, long friendId, System.Collections.Generic.List list) { var 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; } /// /// 删除一方 /// /// /// /// /// 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); } } }