CTT/Server/Hotfix/Game/System/User/TeamSystem.cs

213 lines
6.1 KiB
C#
Raw Normal View History

2021-04-12 23:38:54 +08:00
using System;
2021-04-08 20:09:59 +08:00
using System.Collections.Generic;
using System.Linq;
namespace ET
{
2021-04-12 23:38:54 +08:00
public class TeamAwakeSystem: AwakeSystem<Team>
2021-04-08 20:09:59 +08:00
{
public override void Awake(Team self)
{
self.ChangeState(TeamState.None);
}
}
2021-04-12 23:38:54 +08:00
public class TeamDestroy: DestroySystem<Team>
2021-04-08 20:09:59 +08:00
{
public override void Destroy(Team self)
{
self.teamMemberList.Clear();
self.teamMemberIds.Clear();
self.ChangeState(TeamState.None);
2021-04-11 19:50:39 +08:00
self.offLineUnits.Clear();
2021-04-08 20:09:59 +08:00
self.voteList.Clear();
}
}
public static class TeamSystem
{
public static void ChangeState(this Team self, TeamState state)
{
self.TeamState = state;
2021-04-11 19:50:39 +08:00
foreach (Unit kv in self.teamMemberList)
2021-04-08 20:09:59 +08:00
{
kv.teamState = state;
}
}
public static Unit Get(this Team self, long id)
{
2021-04-12 23:38:54 +08:00
return self.teamMemberList.FirstOrDefault(item => item.Id == id);
2021-04-08 20:09:59 +08:00
}
2021-04-12 23:38:54 +08:00
2021-04-08 20:09:59 +08:00
public static LinkedList<Unit> GetUnits(this Team self)
{
return self.teamMemberList;
}
2021-04-12 23:38:54 +08:00
2021-04-08 20:09:59 +08:00
public static List<long> GetUnitIds(this Team self)
{
return self.teamMemberIds;
}
2021-04-12 23:38:54 +08:00
2021-04-08 20:09:59 +08:00
public static string GetMemberName(this Team self)
{
2021-04-12 23:38:54 +08:00
return self.GetUnits().Aggregate<Unit, string>(null, (current, u) => current + u.Id.GetPlayerFormatName());
2021-04-08 20:09:59 +08:00
}
2021-04-12 23:38:54 +08:00
2021-04-08 20:09:59 +08:00
public static bool Contains(this Team self, long id)
{
return self.teamMemberIds.Contains(id);
}
2021-04-12 23:38:54 +08:00
public static bool CanBeInvite(this Team self)
{
return self.MemberCount == 1;
}
2021-05-07 23:50:22 +08:00
public static bool CanInvite(this Team self)
2021-04-08 20:09:59 +08:00
{
2021-05-07 23:50:22 +08:00
return self.MemberCount < ConstDefine.MaxMemberCount;
2021-04-08 20:09:59 +08:00
}
2021-04-12 23:38:54 +08:00
2021-04-08 20:09:59 +08:00
public static void Add(this Team self, Unit unit)
{
unit.TeamLeaderId = self.LeaderId;
self.teamMemberList.AddLast(unit);
self.teamMemberIds.Add(unit.Id);
2021-04-11 19:50:39 +08:00
BrocastComponent newBrocast = unit.GetComponent<BrocastComponent>();
foreach (Unit item in self.teamMemberList)
2021-04-08 20:09:59 +08:00
{
if (item.Id != unit.Id)
item.GetComponent<BrocastComponent>()?.AddInternal(unit);
}
2021-04-12 23:38:54 +08:00
newBrocast.AddInternal(self.teamMemberList);
2021-04-08 20:09:59 +08:00
}
2021-04-12 23:38:54 +08:00
2021-04-08 20:09:59 +08:00
public static void Remove(this Team self, long id)
{
try
{for (LinkedListNode<Unit> item = self.teamMemberList.First; item != null; item = item.Next)
{
if (item.Value.Id == id)
self.teamMemberList.Remove(item);
}
2021-04-12 23:38:54 +08:00
self.teamMemberIds.Remove(id);
self.RemoveAIId(id);
Unit unit = MapUnitComponent.Instance.Get(id);
if (unit)
2021-04-08 20:09:59 +08:00
{
self.offLineUnits.Remove(unit);
BrocastComponent newBrocast = unit.GetComponent<BrocastComponent>();
foreach (Unit member in self.teamMemberList)
2021-04-08 20:09:59 +08:00
{
try
{
newBrocast.RemoveInternal(member.Id);
member.GetComponent<BrocastComponent>()?.RemoveInternal(id);
}
catch (Exception e)
{
Log.Error(e);
}
2021-04-08 20:09:59 +08:00
}
}
else
{
Log.Error($"unit is null where id= {id} unit isDisposed {unit?.IsDisposed}");
}
2021-04-12 23:38:54 +08:00
if (self.MemberCount == 0)
{
self.GetParent<TeamComponent>().Remove(self.LeaderId);
self.Dispose();
}
}
catch (Exception e)
2021-04-08 20:09:59 +08:00
{
Log.Error(e);
2021-04-08 20:09:59 +08:00
}
2021-04-08 20:09:59 +08:00
}
2021-04-12 23:38:54 +08:00
private static void RemoveAIId(this Team self, long id)
2021-04-08 20:09:59 +08:00
{
2021-04-12 23:38:54 +08:00
if (self.LeaderId != id)
MainStoryAIComponent.instance.aiIdDic.Remove(id);
2021-04-08 20:09:59 +08:00
}
2021-04-12 23:38:54 +08:00
2021-04-08 20:09:59 +08:00
public static void AddOffLineId(this Team self, Unit unit)
{
self.offLineUnits.Add(unit);
}
2021-04-12 23:38:54 +08:00
2021-04-08 20:09:59 +08:00
public static bool HasOffLineUnit(this Team self)
{
return self.offLineUnits.Count > 0;
}
2021-04-12 23:38:54 +08:00
2021-04-08 20:09:59 +08:00
public static void ChangeLeader(this Team self, long leaderId)
{
Unit unit = MapUnitComponent.Instance.Get(leaderId);
if (unit)
{
if (self.teamMemberList.Remove(unit))
{
self.teamMemberList.AddFirst(unit);
2021-04-12 23:38:54 +08:00
}
else
2021-04-08 20:09:59 +08:00
Log.Error($"team not has the unit:{leaderId}");
2021-04-12 23:38:54 +08:00
}
else
2021-04-08 20:09:59 +08:00
Log.Error($"cann't find unit:{leaderId}");
}
2021-04-12 23:38:54 +08:00
2021-04-08 20:09:59 +08:00
/// <summary>
/// 选票增加
/// </summary>
/// <param name="self"></param>
2021-04-12 23:38:54 +08:00
/// <param name="Id"></param>
2021-04-08 20:09:59 +08:00
/// <returns></returns>
public static bool AddVote(this Team self, long Id)
{
self.voteList.Add(Id);
Log.Info($"{self.voteList.Count} {self.MemberCount / 2f}");
if (self.voteList.Count >= self.MemberCount / 2f)
{
self.voteList.Clear();
return true;
}
2021-04-12 23:38:54 +08:00
2021-04-08 20:09:59 +08:00
return false;
}
2021-04-12 23:38:54 +08:00
2021-04-08 20:09:59 +08:00
public static Unit GetLeader(this Team self)
{
long leaderId = self.LeaderId;
Unit unit = MapUnitComponent.Instance.Get(leaderId);
if (!unit)
{
Log.Error($"unit is invalid :{unit?.Id}");
return null;
}
2021-04-12 23:38:54 +08:00
2021-04-08 20:09:59 +08:00
return unit;
}
2021-04-12 23:38:54 +08:00
public static int GetIndex(this Team self, long id)
2021-04-10 19:49:32 +08:00
{
int index = -1;
2021-04-11 19:50:39 +08:00
foreach (Unit item in self.teamMemberList)
2021-04-10 19:49:32 +08:00
{
index++;
2021-04-12 23:38:54 +08:00
if (item.Id == id)
2021-04-10 19:49:32 +08:00
{
return index;
}
}
2021-04-12 23:38:54 +08:00
2021-04-10 19:49:32 +08:00
return index;
}
2021-04-08 20:09:59 +08:00
}
2021-04-12 23:38:54 +08:00
}