using Cal.DataTable; using ET.EventType; using MongoDB.Driver; using System; using System.Collections.Generic; namespace ET { public class FamilyComponentAwakeSystem : AwakeSystem { public override void Awake(FamilyComponent self) { FamilyComponent.Instance = self; } } public class FamilyComponentDestroySystem : DestroySystem { public override void Destroy(FamilyComponent self) { self.FamilyDic.Clear(); } } public static class FamilyComponentSystem { public static async ETTask Query(this FamilyComponent self, string name) { if (string.IsNullOrEmpty(name)) return null; if (!self.FamilyDic.TryGetValue(name, out Family friend)) { List list = await DBComponent.Instance.Query(t => t.name == name); if (list == null || list.Count == 0) { return null; } self.FamilyDic[name] = friend = list[0]; } return friend; } public static async ETTask Save(this FamilyComponent self, Family family) { await DBComponent.Instance.Save(family); } public static async ETTask FindFamily(this FamilyComponent self, string name, System.Collections.Generic.List list, System.Collections.Generic.List requestInfoList, FamilyInfo info) { Family family = await self.Query(name); if (family == null) { return "您想查找的家族不存在,请检查昵称是否正确。"; } await family.CollectFamilyMemberInfo(list, requestInfoList); info.Name = family.name; foreach (KeyValuePair> kp in family.positionMap) { if (kp.Value != null) { foreach (long unitId in kp.Value) { User otherUser = await UserComponent.Instance.Query(unitId); info.PositinMapList.Add(new FamilyPositionMap { Name = otherUser.NickName, Position = kp.Key }); } } }; info.Notice = family.notice; return null; } public static async ETTask CreateFamily(this FamilyComponent self, Unit unit, string name, System.Collections.Generic.List list, FamilyInfo info) { User user = UserComponent.Instance.Get(unit.Id); Family family = await self.Query(user?.Family); if (family != null) { return "您已经有家族了!"; } if (name.Length > 9) { return "家族名称太长!"; } List familyList = await DBComponent.Instance.Query(t => t.name == name); if (familyList != null && familyList.Count > 0) { return "家族名称已经存在!"; } family = EntityFactory.Create(self); family.name = name; user.Family = name; family.Add(FamilyPosition.FamilyLeader, user.Id); family.level = 1; family.notice = "族长很懒,什么都没留下!"; self.FamilyDic.Add(family.name, family); await family.CollectFamilyMemberInfo(list, null); UserComponent.Instance.Save(user); await self.Save(family); //!创建家族Boss await FamilyBossComponent.instance.Create(name); info.Level = family.level; info.Name = name; info.PositinMapList.Add(new FamilyPositionMap { Name = user.NickName, Position = FamilyPosition.FamilyLeader }); info.Notice = family.notice; return null; } public static async ETTask GetFamily(this FamilyComponent self, Unit unit, System.Collections.Generic.List list, System.Collections.Generic.List requestInfoList, FamilyInfo info) { User user = UserComponent.Instance.Get(unit.Id); Family family = await self.Query(user?.Family); if (family == null) { return "您还没有加入家族!"; } await family.CollectFamilyMemberInfo(list, requestInfoList); info.Level = family.level; info.Hornor = family.hornor; info.Name = family.name; foreach (KeyValuePair> kp in family.positionMap) { if (kp.Value != null) { foreach (long unitId in kp.Value) { User otherUser = await UserComponent.Instance.Query(unitId); info.PositinMapList.Add(new FamilyPositionMap { Name = otherUser.NickName, Position = kp.Key }); } } }; info.Notice = family.notice; return null; } /// /// 解散 /// /// /// /// public static async ETTask DeleteFamily(this FamilyComponent self, Unit unit) { User user = UserComponent.Instance.Get(unit.Id); Family family = await self.Query(user?.Family); if (family == null) { return "您没有家族!"; } if (!family.IsLeader(unit.Id)) { return "权限不足"; } await self.DeleteFamily(family); UserComponent.Instance.Save(user); return null; } public static async ETTask DeleteFamily(this FamilyComponent self, Family family) { if (family == null) { return "您没有家族!"; } self.FamilyDic.Remove(family.name); await DBComponent.Instance.Remove(family.Id); foreach (long unitId in family.contributionDic.Keys) { User user = await UserComponent.Instance.Query(unitId); user.Family = ""; UserComponent.Instance.Save(user); } family.Dispose(); return null; } /// /// 申请加入家族 /// /// /// /// /// /// /// /// public static async ETTask RequestEnterFamily(this FamilyComponent self, Unit unit, string name) { User user = UserComponent.Instance.Get(unit.Id); if (!string.IsNullOrEmpty(user?.Family)) { return "已有家族"; } //!判断能否添加对方 Family family = await self.Query(name); if (family == null) { Log.Error($"famuly is invalid where name = {name}"); return "系统错误"; } string ret = family.RequestEnterFamily(user); if (ret != null) return ret; await self.Save(family); return null; } /// /// 处理加入的申请 /// /// /// /// /// /// /// /// public static async ETTask HandleRequest(this FamilyComponent self, Unit unit, bool isAgree, long unitId, System.Collections.Generic.List list, System.Collections.Generic.List requestInfoList) { User user = UserComponent.Instance.Get(unit.Id); Family family = await self.Query(user?.Family); if (family == null) return "您没有家族"; string ret = await family.HandleRequest(user, isAgree, unitId, list, requestInfoList); return ret; } /// /// 删除成员 /// /// /// /// /// /// public static async ETTask DeleteMember(this FamilyComponent self, Unit unit, long unitId, System.Collections.Generic.List list) { if (unitId == unit.Id) { return "不能踢自己!"; } User user = UserComponent.Instance.Get(unit.Id); Family family = await self.Query(user?.Family); if (family == null) return "您没有家族"; string ret = await family.DeleteMember(unit, unitId, list); if (ret != null) return ret; await self.Save(family); Mail mail = new Mail { Id = Game.IdGenerater.GenerateId(), Title = "家族信", Content = $"您已经被移出家族:【{user.Family}】", SenderName = user.NickName, State = MailState.UnReceive, RemainTime = TimeHelper.ClientNow() + (long)TimeSpan.FromDays(7).TotalMilliseconds, }; MailComponent.Instance.AddMail(unitId,mail).Coroutine(); return null; } /// /// 离开家族 /// /// /// /// public static async ETTask LeaveFamily(this FamilyComponent self, long id) { User user = UserComponent.Instance.Get(id); if (string.IsNullOrEmpty(user?.Family)) return "您没有家族"; Family family = await self.Query(user.Family); if (family == null) return "您没有家族"; string ret = await family.LeaveFamily(user); return ret; } public static async ETTask GetFamilyBossReward(this FamilyComponent self, Unit unit,int bossId, System.Collections.Generic.List itemList) { try { User user = UserComponent.Instance.Get(unit.Id); Family family = await self.Query(user?.Family); if (family == null) return "您没有家族"; if (!family.bossRewardDic.TryGetValue(bossId, out List>>> dic)) { Log.Error($"dic == null where bossId = {bossId}"); return "系统错误"; } long id = unit.Id; int index = dic.FindIndex(t => t.Key == id); if (index == -1) { Log.Error($"【{UserComponent.Instance.Get(unit.Id)?.NickName}({unit.Id})】领取不存在礼包"); return "系统错误"; } KeyValuePair>> kv = dic[index]; for (int i = kv.Value.Count - 1; i >= 0; i--) { KeyValuePair itemKV = kv.Value[i]; //!删掉 kv.Value.RemoveAt(i); //!给货 BagHelper.AddItem(unit, itemKV.Key, itemKV.Value, false, getSource: "家族Boss掉落"); try { if (itemKV.Key == BagHelper.CoinId) StatisticsHelper.AddInfo(unit.Id, StatisticComponent.StatisticType.Coin, StatisticsTypes.CoinSources_FamilyBoss, (long)itemKV.Value); else if (itemKV.Key == BagHelper.GemId) StatisticsHelper.AddInfo(unit.Id, StatisticComponent.StatisticType.Gem, StatisticsTypes.GemSources_FamilyBoss, (long)itemKV.Value); } catch (Exception) { } itemList.Add(new ItemInfo { Id = itemKV.Key, Count = itemKV.Value }); } FamilyComponent.Instance.Save(family).Coroutine(); } catch (Exception e) { Log.Error(e); } return null; } } }