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

315 lines
12 KiB
C#
Raw Normal View History

2021-04-08 20:09:59 +08:00
using Cal.DataTable;
using System;
using System.Collections.Generic;
using System.Text;
namespace ET
{
public class TeamComponentAwakeSystem : AwakeSystem<TeamComponent>
{
public override void Awake(TeamComponent self)
{
TeamComponent.Instance = self;
}
}
public class TeamComponentDestroySystem : DestroySystem<TeamComponent>
{
public override void Destroy(TeamComponent self)
{
self.teamDic.Clear();
}
}
public static class TeamComponentSystem
{
public static Team CreateTeam(this TeamComponent self, long id)
{
2021-04-11 19:50:39 +08:00
Team team = EntityFactory.CreateWithParent<Team>(self);
2021-04-08 20:09:59 +08:00
team.LeaderId = id;
2021-04-11 19:50:39 +08:00
Unit unit = MapUnitComponent.Instance.Get(id);
2021-04-08 20:09:59 +08:00
team.Add(unit);
self.teamDic[team.LeaderId] = team;
MainStoryMap.Instance.InitBattleInteractiveInfo(id);
return team;
}
/// <summary>
/// 判断能否进入对方队伍
/// </summary>
/// <param name="self"></param>
/// <param name="learId">对方Id</param>
/// <returns></returns>
public static string CheckCanRequest(this TeamComponent self, Unit unit, Unit targetUnit)
{
2021-04-11 19:50:39 +08:00
Team selfTeam = self.Get(unit.TeamLeaderId);
2021-04-08 20:09:59 +08:00
if (selfTeam == null)
{
Log.Error($"【{UserComponent.Instance.Get(unit.Id)?.NickName}({unit.Id})】没有队伍");
return "系统错误,自己队伍消失";
}
2021-04-11 19:50:39 +08:00
string canTeamRet = CanTeam(unit);
2021-04-08 20:09:59 +08:00
if (canTeamRet != null)
{
return canTeamRet;
}
if (selfTeam.MemberCount > 1)
{
return "自己有队伍了,不能加入其他队伍!";
}
2021-04-11 19:50:39 +08:00
Team team = self.Get(targetUnit.TeamLeaderId);
2021-04-08 20:09:59 +08:00
if (team == null)
{
Log.Error($"【{UserComponent.Instance.Get(targetUnit.Id)?.NickName}({targetUnit.Id})】没有队伍");
return "对方没有队伍,无法进入!";
}
if (team.Contains(unit.Id))
{
return "对方同队!";
}
2021-05-07 23:50:22 +08:00
if (team.MemberCount < 0 || team.MemberCount >= ConstDefine.MaxMemberCount)
2021-04-08 20:09:59 +08:00
{
return "对方队伍已满,无法进入!";
}
if (team.TeamState == TeamState.Fight)
{
return "对方在战斗中,无法进入!";
}
return null;
}
private static string CanTeam(Unit unit)
{
UnitScene unitScene = unit.GetComponent<UnitScene>();
if (unitScene == null)
{
Log.Error($"uniscene == null where id = {unit.Id}");
return "系统错误";
}
Sys_Scene scene = Sys_SceneCategory.Instance.Get(unitScene.MapId / 100);
if (scene == null)
{
return "系统错误";
}
if (!scene.CanTeam)
{
return "此地图禁止组队!";
}
return null;
}
/// <summary>
/// 判断是否可以邀请对方
/// </summary>
/// <param name="self"></param>
/// <param name="learId">对方Id</param>
/// <returns></returns>
public static string CheckCanInvite(this TeamComponent self, Unit unit)
{
2021-04-11 19:50:39 +08:00
Team team = self.Get(unit.TeamLeaderId);
2021-04-08 20:09:59 +08:00
if (team == null)
{
Log.Error($"【{UserComponent.Instance.Get(unit.Id)?.NickName}({unit.Id})】没有队伍");
return null;
}
2021-04-11 19:50:39 +08:00
string canTeamRet = CanTeam(unit);
2021-04-08 20:09:59 +08:00
if (canTeamRet != null)
{
return canTeamRet;
}
if (team.MemberCount > 1)
{
return "对方已有队伍,无法邀请!";
}
if (team.TeamState == TeamState.Fight)
{
return "对方在战斗中,无法邀请!";
}
return null;
}
/// <summary>
///
/// </summary>
/// <param name="self"></param>
/// <param name="learId">自己的Id</param>
/// <param name="targetUnit">对方</param>
public static void InviteTeam(this TeamComponent self, long learId, Unit targetUnit)
{
MessageHelper.SendActor(targetUnit, new M2C_InviteList() { UnitId = learId, TimeOut = 10000 });
}
/// <summary>
///
/// </summary>
/// <param name="self"></param>
/// <param name="learId">对方的ID</param>
/// <param name="myUnit">自己</param>
public static void RequestTeam(this TeamComponent self, long learId, Unit myUnit)
{
MessageHelper.SendActor(MapUnitComponent.Instance.Get(learId), new M2C_RequestList() { UnitId = myUnit.Id, TimeOut = 10000 });
}
/// <summary>
/// 将别人加入自己队伍
/// </summary>
/// <param name="self"></param>
/// <param name="unit">自己</param>
/// <param name="targetId">目标玩家</param>
2021-04-08 20:09:59 +08:00
/// <returns></returns>
public static Team HandleRequestTeam(this TeamComponent self, Unit unit, long targetId)
{
2021-04-11 19:50:39 +08:00
Team team = self.Get(unit.TeamLeaderId);
2021-04-08 20:09:59 +08:00
if (team == null) return null;
2021-04-11 19:50:39 +08:00
string canTeamRet = CanTeam(unit);
2021-04-08 20:09:59 +08:00
if (canTeamRet != null)
{
return null;
}
if (team.TeamState == TeamState.Fight)
return null;
2021-05-07 23:50:22 +08:00
if (!team.CanInvite()) return null;
2021-04-08 20:09:59 +08:00
if (team.Contains(targetId))
return null;
2021-04-11 19:50:39 +08:00
Unit targetUnit = MapUnitComponent.Instance.Get(targetId);
2021-04-08 20:09:59 +08:00
if (targetUnit == null)
return null;
2021-05-07 23:50:22 +08:00
Team targetTeam =self.Get(targetUnit.TeamLeaderId);
if (targetTeam == null) return null;
if (targetTeam.TeamState == TeamState.Fight)
return null;
if (!targetTeam.CanBeInvite()) return null;
2021-04-08 20:09:59 +08:00
self.Remove(targetUnit.TeamLeaderId);
team.Add(targetUnit);
return team;
}
/// <summary>
/// 将自己加入别人队伍
/// </summary>
/// <param name="self"></param>
/// <param name="unit">被邀请人</param>
/// <param name="targetId">目标队长</param>
2021-04-08 20:09:59 +08:00
/// <returns></returns>
public static Team HandleIniviteTeam(this TeamComponent self, Unit unit, long targetId)
{
//!targetId == targetLeaderId
2021-05-07 23:50:22 +08:00
Team team = self.GetByUserId(targetId);
2021-04-08 20:09:59 +08:00
if (team == null) return null;
2021-04-11 19:50:39 +08:00
string canTeamRet = CanTeam(unit);
2021-04-08 20:09:59 +08:00
if (canTeamRet != null)
{
return null;
}
if (team.TeamState == TeamState.Fight)
return null;
Team selfTeam = self.Get(unit.TeamLeaderId);
if (selfTeam == null) return null;
if (selfTeam.TeamState == TeamState.Fight)
return null;
if (!selfTeam.CanBeInvite()) return null;
2021-05-07 23:50:22 +08:00
if (!team.CanInvite()) return null;
2021-04-08 20:09:59 +08:00
if (team.Contains(unit.Id))
return null;
self.Remove(unit.TeamLeaderId);
team.Add(unit);
return team;
}
2021-05-07 23:50:22 +08:00
public static Team GetByUserId(this TeamComponent self, long userId)
{
Unit unit = MapUnitComponent.Instance.Get(userId);
if (!unit) return null;
self.teamDic.TryGetValue(unit.TeamLeaderId, out Team team);
return team;
}
2021-04-08 20:09:59 +08:00
public static Team Get(this TeamComponent self, long learId)
{
2021-04-11 19:50:39 +08:00
self.teamDic.TryGetValue(learId, out Team team);
2021-04-08 20:09:59 +08:00
return team;
}
public static void Update(this TeamComponent self, long oldLearId, Team team)
{
self.teamDic.Remove(oldLearId);
self.teamDic.Add(team.LeaderId, team);
}
public static void Remove(this TeamComponent self, long leaderId)
{
self.teamDic.Remove(leaderId);
MainStoryMap.Instance.RemoveBattleInteractiveInfo(leaderId);
}
public static void QuitTeam(this TeamComponent self, Unit unit, Team team = null)
{
team ??= self.Get(unit.TeamLeaderId);
if (team == null) return;
team.Remove(unit.Id);
//队长特殊处理
if (unit.Id == unit.TeamLeaderId)
{
MainStoryAIComponent.instance.DestoryAI(unit);
2021-04-11 19:50:39 +08:00
LinkedList<Unit> unitIds = team.GetUnits();
2021-04-08 20:09:59 +08:00
long leaderId = unitIds.First.Value.Id;
team.LeaderId = leaderId;
//更新信息
self.Update(unit.Id, team);
2021-04-11 19:50:39 +08:00
foreach (Unit item in team.GetUnits())
2021-04-08 20:09:59 +08:00
{
item.TeamLeaderId = leaderId;
}
team.ChangeLeader(leaderId);
//更新旧队伍交互信息
MainStoryMap.Instance.UpdateBattleInteractiveInfo(unit.TeamLeaderId, team.LeaderId);
}
//创建新队伍
2021-04-11 19:50:39 +08:00
Team newTeam = self.CreateTeam(unit.Id);
2021-04-08 20:09:59 +08:00
TeamHelper.SendTeamMember(newTeam);
TeamHelper.SendTeamMember(team);
}
public static void DestroyTeam(this TeamComponent self, Unit unit, Team team = null)
{
team ??= self.Get(unit.TeamLeaderId);
if (team == null) return;
//如果人数==0 ,自动清理
team.Remove(unit.Id);
if(team)
{
//队长特殊处理,移除了自己,所以只要还有人就做处理
if (unit.Id == unit.TeamLeaderId)
{
MainStoryAIComponent.instance.DestoryAI(unit);
2021-04-11 19:50:39 +08:00
LinkedList<Unit> unitIds = team.GetUnits();
2021-04-08 20:09:59 +08:00
long leaderId = unitIds.First.Value.Id;
team.LeaderId = leaderId;
//更新信息
self.Update(unit.Id, team);
2021-04-11 19:50:39 +08:00
foreach (Unit item in team.GetUnits())
2021-04-08 20:09:59 +08:00
{
item.TeamLeaderId = leaderId;
}
team.ChangeLeader(leaderId);
//更新旧队伍交互信息
MainStoryMap.Instance.UpdateBattleInteractiveInfo(unit.TeamLeaderId, team.LeaderId);
}
TeamHelper.SendTeamMember(team);
}
BattleIdleMap.Instance.Remove(unit.TeamLeaderId);
}
public static void RemoveOffLineId(this TeamComponent self, Team team, Unit unit)
{
team.offLineUnits.Remove(unit);
}
public static async ETTask RemoveAllOffLineId(this TeamComponent self, Team team)
{
2021-04-11 19:50:39 +08:00
using ListComponent<Unit> listComponent = ListComponent<Unit>.Create();
listComponent.List.AddRange(team.offLineUnits);
foreach (Unit unit in listComponent.List)
2021-04-08 20:09:59 +08:00
{
if (unit.IsOffline == false)
continue;
if (team.MemberCount == 1)
_=true;
else
self.QuitTeam(unit);
await Game.EventSystem.Publish(new EventType.UnitOffline { unit = unit });
}
}
}
}