451 lines
16 KiB
C#
451 lines
16 KiB
C#
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace ET
|
|
{
|
|
|
|
public class FamilyDestroySystem : DestroySystem<Family>
|
|
{
|
|
public override void Destroy(Family self)
|
|
{
|
|
self.level = 0;
|
|
self.hornor = 0;
|
|
self.RequestList.Clear();
|
|
self.notice = null;
|
|
self.name = null;
|
|
self.positionMap.Clear();
|
|
self.contributionDic.Clear();
|
|
self.bossRewardDic.Clear();
|
|
}
|
|
}
|
|
public static class FamilySystem
|
|
{
|
|
private const int Max_FamilyMember_Count = 30;
|
|
private const int Max_RequestFamilyMember_Count = 20;
|
|
private readonly static Dictionary<FamilyPosition, float> ContributionArr = new Dictionary<FamilyPosition, float>
|
|
{
|
|
{ FamilyPosition.FamilyLeader,0.5f },
|
|
{ FamilyPosition.FamilyDeputyLeader,0.4f },
|
|
{ FamilyPosition.FamilyMember,0.3f },
|
|
};
|
|
public static void Add(this Family self, FamilyPosition familyPosition, long id)
|
|
{
|
|
if (!self.contributionDic.TryAdd(id, 0))
|
|
{
|
|
Log.Error($"dic has the key == {id}");
|
|
}
|
|
if (!self.positionMap.ContainsKey(familyPosition))
|
|
{
|
|
self.positionMap[familyPosition] = new List<long>();
|
|
}
|
|
self.positionMap[familyPosition].Add(id);
|
|
}
|
|
public static void AddContribution(this Family self, long id, int contribute)
|
|
{
|
|
if (!self.contributionDic.TryGetValue(id, out int old))
|
|
old = 0;
|
|
old += contribute;
|
|
self.contributionDic[id] = old;
|
|
|
|
float percent = ContributionArr[self.GetFamilyPosition(id)];
|
|
self.AddHornor((int)(old * percent));
|
|
}
|
|
public static void AddHornor(this Family self, int hornor)
|
|
{
|
|
self.hornor += hornor;
|
|
}
|
|
|
|
public static async ETTask AutoTransferLeader(this Family self)
|
|
{
|
|
if (!self.positionMap.TryGetValue(FamilyPosition.FamilyDeputyLeader, out List<long> deputyLeader))
|
|
{
|
|
if (!self.positionMap.TryGetValue(FamilyPosition.FamilyMember, out List<long> memberList))
|
|
{
|
|
//!解散
|
|
await FamilyComponent.Instance.DeleteFamily(self);
|
|
return;
|
|
}
|
|
long id = memberList[0];
|
|
if (memberList.Count > 1)
|
|
{
|
|
memberList.RemoveAt(0);
|
|
}
|
|
else
|
|
self.positionMap.Remove(FamilyPosition.FamilyMember);
|
|
self.positionMap[FamilyPosition.FamilyLeader][0] = id;
|
|
}
|
|
else
|
|
{
|
|
long id = deputyLeader[0];
|
|
if (deputyLeader.Count > 1)
|
|
{
|
|
deputyLeader.RemoveAt(0);
|
|
}
|
|
else
|
|
self.positionMap.Remove(FamilyPosition.FamilyDeputyLeader);
|
|
self.positionMap[FamilyPosition.FamilyLeader][0] = id;
|
|
}
|
|
}
|
|
public static void TransferPosition(this Family self)
|
|
{
|
|
|
|
}
|
|
public static void AddReward(this Family self, int bossId, long id, int itemId, int count)
|
|
{
|
|
if (!self.bossRewardDic.TryGetValue(bossId, out List<KeyValuePair<long, List<KeyValuePair<int, int>>>> dic))
|
|
{
|
|
self.bossRewardDic[bossId] = dic = new List<KeyValuePair<long, List<KeyValuePair<int, int>>>>();
|
|
}
|
|
int index = dic.FindIndex(t => t.Key == id);
|
|
KeyValuePair<long, List<KeyValuePair<int, int>>> kv;
|
|
if (index == -1)
|
|
{
|
|
kv = KeyValuePair.Create(id, new List<KeyValuePair<int, int>>() { KeyValuePair.Create(itemId, count) });
|
|
dic.Add(kv);
|
|
}
|
|
else
|
|
{
|
|
kv = dic[index];
|
|
kv.Deconstruct(out _, out List<KeyValuePair<int, int>> list);
|
|
list.Add(KeyValuePair.Create(itemId, count));
|
|
dic[index] = KeyValuePair.Create(id, list);
|
|
}
|
|
}
|
|
public static void ClearReward(this Family self, int bossId, long id)
|
|
{
|
|
if (!self.bossRewardDic.TryGetValue(bossId, out List<KeyValuePair<long, List<KeyValuePair<int, int>>>> dic))
|
|
{
|
|
return;
|
|
}
|
|
for (int i = dic.Count - 1; i >= 0; i--)
|
|
{
|
|
KeyValuePair<long, List<KeyValuePair<int, int>>> kv = dic[i];
|
|
if (kv.Key == id)
|
|
{
|
|
kv.Value.Clear();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
public static bool HasReward(this Family self, int bossId, long id)
|
|
{
|
|
if (!self.bossRewardDic.TryGetValue(bossId, out List<KeyValuePair<long, List<KeyValuePair<int, int>>>> dic))
|
|
{
|
|
return false;
|
|
}
|
|
for (int i = dic.Count - 1; i >= 0; i--)
|
|
{
|
|
KeyValuePair<long, List<KeyValuePair<int, int>>> kv = dic[i];
|
|
if (kv.Key == id)
|
|
{
|
|
return kv.Value.Count > 0;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static FamilyPosition GetFamilyPosition(this Family self, long id)
|
|
{
|
|
if (self.IsLeader(id))
|
|
return FamilyPosition.FamilyLeader;
|
|
else
|
|
if (self.IsManager(id))
|
|
return FamilyPosition.FamilyDeputyLeader;
|
|
else
|
|
return FamilyPosition.FamilyMember;
|
|
}
|
|
public static bool IsManager(this Family self, long id)
|
|
{
|
|
return self.IsLeader(id) ||
|
|
self.IsViteLeader(id);
|
|
}
|
|
/// <summary>
|
|
/// 族长
|
|
/// </summary>
|
|
/// <param name="self"></param>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public static bool IsLeader(this Family self, long id)
|
|
{
|
|
if (!self.positionMap.TryGetValue(FamilyPosition.FamilyLeader, out List<long> list))
|
|
{
|
|
return false;
|
|
}
|
|
long leader = list[0];
|
|
return leader == id;
|
|
}
|
|
/// <summary>
|
|
/// 副族长
|
|
/// </summary>
|
|
/// <param name="self"></param>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public static bool IsViteLeader(this Family self, long id)
|
|
{
|
|
if (!self.positionMap.TryGetValue(FamilyPosition.FamilyDeputyLeader, out List<long> leaderList))
|
|
{
|
|
return false;
|
|
}
|
|
if (leaderList != null)
|
|
{
|
|
foreach (long item in leaderList)
|
|
{
|
|
if (item == id)
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
public static IEnumerable<long> GetAllId(this Family self)
|
|
{
|
|
return self.contributionDic.Keys;
|
|
}
|
|
/// <summary>
|
|
/// 收集成员信息
|
|
/// </summary>
|
|
/// <param name="self"></param>
|
|
/// <param name="family"></param>
|
|
/// <param name="list"></param>
|
|
/// <param name="requestInfoList"></param>
|
|
/// <returns></returns>
|
|
public static async ETTask CollectFamilyMemberInfo(this Family self, System.Collections.Generic.List<FamilyMemberInfo> list, System.Collections.Generic.List<RequestAddFriendInfo> requestInfoList)
|
|
{
|
|
if (self == null)
|
|
{
|
|
Log.Error($"family==null");
|
|
return;
|
|
}
|
|
if (list == null) return;
|
|
foreach (long userId in self.contributionDic.Keys)
|
|
{
|
|
long lastLoginTime = -1;
|
|
User user = await UserHelper.Query(userId);
|
|
if (user != null)
|
|
lastLoginTime = user.lastLoginTime;
|
|
|
|
if (user.IsOnline())
|
|
{
|
|
lastLoginTime = 0;
|
|
}
|
|
else
|
|
{
|
|
|
|
}
|
|
FamilyMemberInfo info = new FamilyMemberInfo
|
|
{
|
|
Id = user.Id,
|
|
Level = user.Level,
|
|
Name = user.NickName,
|
|
Job = JobHelper.GetJobType(user.JobId),
|
|
LastLginTime = lastLoginTime
|
|
};
|
|
list.Add(info);
|
|
foreach (KeyValuePair<long, int> kv in self.contributionDic)
|
|
{
|
|
info.ContributionList.Add(new FamilyContributionMap
|
|
{
|
|
Id = kv.Key,
|
|
Value = kv.Value
|
|
});
|
|
}
|
|
}
|
|
requestInfoList?.AddRange(self.RequestList);
|
|
}
|
|
/// <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 string RequestEnterFamily(this Family self, User user)
|
|
{
|
|
Family family = self;
|
|
|
|
if (family.contributionDic.Count >= Max_FamilyMember_Count)
|
|
{
|
|
return "此家族成员已达上限";
|
|
}
|
|
if (family.RequestList.Count >= Max_RequestFamilyMember_Count)
|
|
{
|
|
return "申请列表达到上限";
|
|
}
|
|
//!被邀请列表
|
|
bool hasRequest = false;
|
|
foreach (RequestAddFriendInfo item in family.RequestList)
|
|
{
|
|
if (item.Id == user.Id)
|
|
hasRequest = true;
|
|
}
|
|
if (hasRequest)
|
|
{
|
|
return "已经申请过了,请等待对方审核!";
|
|
}
|
|
family.RequestList.Add(new RequestAddFriendInfo
|
|
{
|
|
Id = user.Id,
|
|
Name = user.NickName,
|
|
Level = user.Level,
|
|
Job = JobHelper.GetJobType(user.JobId),
|
|
Sex = JobHelper.GetSexType(user.JobId),
|
|
});
|
|
|
|
return null;
|
|
}
|
|
public static bool Caintains(this Family self,long id)
|
|
{
|
|
return self.contributionDic.ContainsKey(id);
|
|
}
|
|
/// <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 Family self, User user, bool isAgree, long unitId, System.Collections.Generic.List<FamilyMemberInfo> list, System.Collections.Generic.List<RequestAddFriendInfo> requestInfoList)
|
|
{
|
|
Family family = self;
|
|
if (!family.IsManager(user.Id))
|
|
{
|
|
return "权限不足";
|
|
}
|
|
if (family.contributionDic.Count >= Max_FamilyMember_Count)
|
|
{
|
|
return "家族人数已满";
|
|
}
|
|
if (!isAgree)
|
|
{
|
|
await RemoveInrequestList(self, family, user, unitId);
|
|
return null;
|
|
}
|
|
if (family.Caintains(unitId))
|
|
{
|
|
return "该玩家已经加入家族";
|
|
}
|
|
|
|
//!添加别人
|
|
User otherUser = await UserComponent.Instance.Query(unitId);
|
|
if (!string.IsNullOrEmpty(otherUser.Family))
|
|
{
|
|
return "对方已有家族";
|
|
}
|
|
otherUser.Family = family.name;
|
|
UserComponent.Instance.Save(otherUser).Coroutine();
|
|
|
|
family.Add(FamilyPosition.FamilyMember, otherUser.Id);
|
|
|
|
await RemoveInrequestList(self, family, user, unitId);
|
|
|
|
//!更新后的列表
|
|
await self.CollectFamilyMemberInfo(list, requestInfoList);
|
|
return null;
|
|
|
|
//!从申请列表中移除
|
|
static async ETTask RemoveInrequestList(Family self, Family family, User user, long unitId)
|
|
{
|
|
List<RequestAddFriendInfo> mineList = family.RequestList;
|
|
for (int i = mineList.Count - 1; i >= 0; i--)
|
|
{
|
|
if (mineList[i].Id == unitId) mineList.RemoveAt(i);
|
|
}
|
|
await self.GetParent<FamilyComponent>().Save(family);
|
|
}
|
|
}
|
|
/// <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 Family self, Unit unit, long unitId, System.Collections.Generic.List<FamilyMemberInfo> list)
|
|
{
|
|
Family family = self;
|
|
if (!family.IsManager(unit.Id))
|
|
{
|
|
return "权限不足";
|
|
}
|
|
if (family.IsManager(unitId) &&
|
|
!family.IsLeader(unit.Id))
|
|
{
|
|
return "权限不足";
|
|
}
|
|
family.contributionDic.Remove(unitId);
|
|
foreach (KeyValuePair<FamilyPosition, List<long>> kv in family.positionMap)
|
|
{
|
|
kv.Value.Remove(unitId);
|
|
}
|
|
User user = await UserComponent.Instance.Query(unitId);
|
|
if (user != null)
|
|
{
|
|
user.Family = "";
|
|
UserComponent.Instance.Save(user).Coroutine();
|
|
}
|
|
//!更新后的列表
|
|
await self.CollectFamilyMemberInfo(list, null);
|
|
return null;
|
|
}
|
|
public static async ETTask ForceDeleteMember(this Family self, long unitId)
|
|
{
|
|
Family family = self;
|
|
if (family.IsLeader(unitId))
|
|
{
|
|
await FamilyComponent.Instance.DeleteFamily(family);
|
|
return;
|
|
}
|
|
family.contributionDic.Remove(unitId);
|
|
foreach (KeyValuePair<FamilyPosition, List<long>> kv in family.positionMap)
|
|
{
|
|
kv.Value.Remove(unitId);
|
|
}
|
|
User user = await UserComponent.Instance.Query(unitId);
|
|
if (user != null)
|
|
{
|
|
user.Family = "";
|
|
UserComponent.Instance.Save(user).Coroutine();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 离开家族
|
|
/// </summary>
|
|
/// <param name="self"></param>
|
|
/// <param name="unit"></param>
|
|
/// <returns></returns>
|
|
public static async ETTask<string> LeaveFamily(this Family self, User user)
|
|
{
|
|
Family family = self;
|
|
List<long> leader = family.positionMap[FamilyPosition.FamilyLeader];
|
|
if (leader == null)
|
|
{
|
|
Log.Error($"leader == null where family name is {family.name}");
|
|
return "系统错误";
|
|
}
|
|
if (leader[0] == user.Id)
|
|
{
|
|
//!自动转让族长
|
|
await family.AutoTransferLeader();
|
|
}
|
|
user.Family = "";
|
|
UserComponent.Instance.Save(user).Coroutine();
|
|
if (family != null)
|
|
{
|
|
if (family.contributionDic.Count == 0)
|
|
return null;
|
|
family.contributionDic.Remove(user.Id);
|
|
await self.GetParent<FamilyComponent>().Save(family);
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|