369 lines
13 KiB
C#
Executable File
369 lines
13 KiB
C#
Executable File
using Cal.DataTable;
|
|
using ET.EventType;
|
|
using MongoDB.Driver;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace ET
|
|
{
|
|
public class FamilyComponentAwakeSystem: AwakeSystem<FamilyComponent>
|
|
{
|
|
public override void Awake(FamilyComponent self)
|
|
{
|
|
FamilyComponent.Instance = self;
|
|
}
|
|
}
|
|
|
|
public class FamilyComponentDestroySystem: DestroySystem<FamilyComponent>
|
|
{
|
|
public override void Destroy(FamilyComponent self)
|
|
{
|
|
self.FamilyDic.Clear();
|
|
}
|
|
}
|
|
|
|
public static class FamilyComponentSystem
|
|
{
|
|
public static async ETTask<Family> Query(this FamilyComponent self, string name)
|
|
{
|
|
if (string.IsNullOrEmpty(name)) return null;
|
|
if (!self.FamilyDic.TryGetValue(name, out Family friend))
|
|
{
|
|
List<Family> list = await DBComponent.Instance.Query<Family>(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<string> FindFamily(this FamilyComponent self, string name, System.Collections.Generic.List<FamilyMemberInfo> list,
|
|
System.Collections.Generic.List<RequestAddFriendInfo> requestInfoList, FamilyInfo info)
|
|
{
|
|
Family family = await self.Query(name);
|
|
if (family == null)
|
|
{
|
|
return "您想查找的家族不存在,请检查昵称是否正确。";
|
|
}
|
|
|
|
await family.CollectFamilyMemberInfo(list, requestInfoList);
|
|
|
|
info.Name = family.name;
|
|
foreach (KeyValuePair<FamilyPosition, List<long>> 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<string> CreateFamily(this FamilyComponent self, Unit unit, string name,
|
|
System.Collections.Generic.List<FamilyMemberInfo> 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<Family> familyList = await DBComponent.Instance.Query<Family>(t => t.name == name);
|
|
if (familyList != null && familyList.Count > 0)
|
|
{
|
|
return "家族名称已经存在!";
|
|
}
|
|
|
|
family = EntityFactory.Create<Family>(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<string> GetFamily(this FamilyComponent self, Unit unit, System.Collections.Generic.List<FamilyMemberInfo> list,
|
|
System.Collections.Generic.List<RequestAddFriendInfo> 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<FamilyPosition, List<long>> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 解散
|
|
/// </summary>
|
|
/// <param name="self"></param>
|
|
/// <param name="unit"></param>
|
|
/// <returns></returns>
|
|
public static async ETTask<string> 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<string> DeleteFamily(this FamilyComponent self, Family family)
|
|
{
|
|
if (family == null)
|
|
{
|
|
return "您没有家族!";
|
|
}
|
|
|
|
self.FamilyDic.Remove(family.name);
|
|
await DBComponent.Instance.Remove<Family>(family.Id);
|
|
foreach (long unitId in family.contributionDic.Keys)
|
|
{
|
|
User user = await UserComponent.Instance.Query(unitId);
|
|
user.Family = "";
|
|
UserComponent.Instance.Save(user);
|
|
}
|
|
|
|
await DBComponent.Instance.Remove((FamilyBoss t) => t.name.Equals(family.name), nameof (FamilyBoss));
|
|
|
|
family.Dispose();
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 申请加入家族
|
|
/// </summary>
|
|
/// <param name="self"></param>
|
|
/// <param name="unit"></param>
|
|
/// <param name="name"></param>
|
|
/// <param name="list"></param>
|
|
/// <param name="requestInfoList"></param>
|
|
/// <param name="info"></param>
|
|
/// <returns></returns>
|
|
public static async ETTask<string> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 处理加入的申请
|
|
/// </summary>
|
|
/// <param name="self"></param>
|
|
/// <param name="unit"></param>
|
|
/// <param name="isAgree"></param>
|
|
/// <param name="unitId"></param>
|
|
/// <param name="list"></param>
|
|
/// <param name="requestInfoList"></param>
|
|
/// <returns></returns>
|
|
public static async ETTask<string> HandleRequest(this FamilyComponent self, Unit unit, bool isAgree, long unitId,
|
|
System.Collections.Generic.List<FamilyMemberInfo> list, System.Collections.Generic.List<RequestAddFriendInfo> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除成员
|
|
/// </summary>
|
|
/// <param name="self"></param>
|
|
/// <param name="unit"></param>
|
|
/// <param name="unitId"></param>
|
|
/// <param name="list"></param>
|
|
/// <returns></returns>
|
|
public static async ETTask<string> DeleteMember(this FamilyComponent self, Unit unit, long unitId,
|
|
System.Collections.Generic.List<FamilyMemberInfo> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 离开家族
|
|
/// </summary>
|
|
/// <param name="self"></param>
|
|
/// <param name="unit"></param>
|
|
/// <returns></returns>
|
|
public static async ETTask<string> 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<string> GetFamilyBossReward(this FamilyComponent self, Unit unit, int bossId,
|
|
System.Collections.Generic.List<ItemInfo> 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<KeyValuePair<long, List<KeyValuePair<int, int>>>> 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<long, List<KeyValuePair<int, int>>> kv = dic[index];
|
|
for (int i = kv.Value.Count - 1; i >= 0; i--)
|
|
{
|
|
KeyValuePair<int, int> itemKV = kv.Value[i];
|
|
//!删掉
|
|
kv.Value.RemoveAt(i);
|
|
//!给货
|
|
bool isLock = itemKV.Key == 110831;
|
|
BagHelper.AddItem(unit, itemKV.Key, itemKV.Value, isLock, 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;
|
|
}
|
|
}
|
|
} |