633 lines
18 KiB
C#
633 lines
18 KiB
C#
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using cfg.LegionCfg;
|
||
using Framework;
|
||
using Gameplay;
|
||
using Gameplay.Common;
|
||
using Gameplay.Net;
|
||
using NLDPB;
|
||
using PhxhSDK;
|
||
using Sirenix.Utilities;
|
||
using UnityEngine;
|
||
|
||
public partial class LegionManager: Singlenton<LegionManager>, IInitable
|
||
{
|
||
public bool Belong2Legion => _myLegion != null;
|
||
public enum MemberOpType
|
||
{
|
||
All,
|
||
Add,
|
||
Update,
|
||
Remove
|
||
}
|
||
|
||
public enum EMyApplyStatus
|
||
{
|
||
None,
|
||
Already,
|
||
Refused,
|
||
}
|
||
|
||
#region API
|
||
|
||
public void Init()
|
||
{
|
||
_RegisterEvents();
|
||
}
|
||
|
||
public void Release()
|
||
{
|
||
_UnregisterEvents();
|
||
}
|
||
|
||
public void ClearMyLegion()
|
||
{
|
||
_myLegion = null;
|
||
_myLegionMemberList?.Clear();
|
||
_applyMemberList?.Clear();
|
||
}
|
||
/// <summary>
|
||
/// 设置客户端的 MyLegion Base数据
|
||
/// </summary>
|
||
public void SetMyLegionBase()
|
||
{
|
||
if (_myLegion != null)
|
||
{
|
||
_myLegion._ParseLegionBase(NetLegionBaseData);
|
||
EventManager.Instance.Send(EventManager.EventName.LegionBaseData);
|
||
return;
|
||
}
|
||
_myLegion = _GetParseLegionData(NetLegionBaseData);
|
||
EventManager.Instance.Send(EventManager.EventName.LegionBaseData);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 设置客户端的 MyLegion Member数据, 0全部刷新,1增加,2刷新,3减少
|
||
/// </summary>
|
||
public void SetMyLegionMembers(bool isAll = false)
|
||
{
|
||
if (_myLegionMemberList == null)
|
||
{
|
||
_myLegionMemberList = new();
|
||
}
|
||
else if(isAll)
|
||
{
|
||
_myLegionMemberList.Clear();
|
||
}
|
||
|
||
foreach (var memberData in NetLegionMemberList)
|
||
{
|
||
if (isAll)
|
||
{
|
||
_myLegionMemberList.Add(new ActorLegionData(memberData));
|
||
continue;
|
||
}
|
||
|
||
var index = _myLegionMemberList.FindIndex(item => item.ActorID == memberData.ActorId);
|
||
if (memberData.IsDelete > 0)
|
||
{
|
||
if (index > -1)
|
||
{
|
||
_myLegionMemberList.RemoveAt(index);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (index > -1)
|
||
{
|
||
_myLegionMemberList[index] = new ActorLegionData(memberData);
|
||
}
|
||
else
|
||
{
|
||
_myLegionMemberList.Add(new ActorLegionData(memberData));
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
NetLegionMemberList.Clear();
|
||
EventManager.Instance.Send(EventManager.EventName.LegionMemberData);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 设置客户端的 MyLegion ApplyMember数据
|
||
/// </summary>
|
||
public void SetMyLegionApplyMembers(bool isALl = false)
|
||
{
|
||
if (_applyMemberList == null)
|
||
{
|
||
_applyMemberList = new();
|
||
}
|
||
else if(isALl)
|
||
{
|
||
_applyMemberList.Clear();
|
||
}
|
||
|
||
if (isALl)
|
||
{
|
||
foreach (var applyData in NetLegionApplyDataList)
|
||
{
|
||
_applyMemberList.Add(new ActorLegionData(applyData));
|
||
}
|
||
}
|
||
else
|
||
{
|
||
foreach (var applyData in NetLegionApplyDataList)
|
||
{
|
||
var idx = _applyMemberList.FindIndex(member => member.ActorID == applyData.ActorId);
|
||
// if (applyData.IsDelete > 0)
|
||
// {
|
||
// if (idx > -1)
|
||
// {
|
||
// _applyMemberList.RemoveAt(idx);
|
||
// }
|
||
// }
|
||
// else
|
||
// {
|
||
// _applyMemberList.Add(new ActorLegionData(applyData));
|
||
// }
|
||
if (idx > -1)
|
||
{
|
||
_applyMemberList[idx] = new ActorLegionData(applyData);
|
||
}
|
||
else
|
||
{
|
||
_applyMemberList.Add(new ActorLegionData(applyData));
|
||
}
|
||
}
|
||
}
|
||
|
||
NetLegionApplyDataList.Clear();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 自己申请的军团列表,
|
||
/// </summary>
|
||
/// <param name="isAll"></param>
|
||
public void SetMyApplyLegionList(bool isAll = false)
|
||
{
|
||
if (_myApplyLegionDatas == null)
|
||
{
|
||
_myApplyLegionDatas = new();
|
||
}
|
||
else if(isAll)
|
||
{
|
||
_myApplyLegionDatas.Clear();
|
||
}
|
||
|
||
foreach (var applyData in NetMyApplyLegionList)
|
||
{
|
||
if (isAll)
|
||
{
|
||
_myApplyLegionDatas.Add(applyData.LegionId, new ApplyLegionData(applyData));
|
||
continue;
|
||
}
|
||
|
||
if (_myApplyLegionDatas.TryGetValue(applyData.LegionId, out var myApplyData))
|
||
{
|
||
if (applyData.Status == (uint) LegionExamineStatus.LgPass)
|
||
{
|
||
_myApplyLegionDatas.Remove(applyData.LegionId);
|
||
continue;
|
||
}
|
||
myApplyData.ExamineStatus = (LegionExamineStatus)applyData.Status;
|
||
myApplyData.OpTime = applyData.OpTime;
|
||
}
|
||
else
|
||
{
|
||
_myApplyLegionDatas.Add(applyData.LegionId, new ApplyLegionData(applyData));
|
||
}
|
||
}
|
||
NetMyApplyLegionList.Clear();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 设置客户端的 MyLegion 记录数据
|
||
/// </summary>
|
||
public void SetMyLegionLogs()
|
||
{
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 刷新军团列表
|
||
/// </summary>
|
||
public void SetLegionList()
|
||
{
|
||
_legions.Clear();
|
||
var legionDataList = NetLegionInfoDataList;
|
||
foreach (var legionData in legionDataList)
|
||
{
|
||
var legion = ParseLegion(legionData);
|
||
CorrectLegionData(legion);
|
||
_legions.Add(legion);
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 确认是否有权限
|
||
/// </summary>
|
||
/// <param name="eventID"></param>
|
||
/// <returns></returns>
|
||
public bool CheckIsPermission(Permission eventID)
|
||
{
|
||
if (_myLegion == null)
|
||
return false;
|
||
var eventCfg = TableManager.Instance.Tables.LegionPermissions.GetOrDefault(eventID);
|
||
if (eventCfg != null)
|
||
{
|
||
var post = GetMyPost();
|
||
if (post != Position.None)
|
||
{
|
||
switch (post)
|
||
{
|
||
case Position.Member:
|
||
{
|
||
return eventCfg.Member > 0;
|
||
}
|
||
case Position.RegimentalCommander:
|
||
{
|
||
return eventCfg.RegimentalCommander > 0;
|
||
}
|
||
case Position.AssistantRegimentalCommander:
|
||
{
|
||
return eventCfg.AssistantRegimentalCommander > 0;
|
||
}
|
||
case Position.Leroy:
|
||
{
|
||
return eventCfg.Leroy > 0;
|
||
}
|
||
case Position.Diplomatist:
|
||
{
|
||
return eventCfg.Diplomatist > 0;
|
||
}
|
||
|
||
case Position.Commander:
|
||
{
|
||
return eventCfg.Commander > 0;
|
||
}
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
public Position GetMyPost()
|
||
{
|
||
if (_myLegion == null)
|
||
return Position.None;
|
||
if (_myLegionMemberList == null || _myLegionMemberList.Count <= 0)
|
||
return Position.None;
|
||
|
||
var myLegionActorData = _myLegionMemberList.Find(
|
||
data => data.ActorID == Actor.Instance.Base.GetProp(ActorProp.ActorId));
|
||
if (myLegionActorData != null)
|
||
{
|
||
return myLegionActorData.Post;
|
||
}
|
||
|
||
return Position.None;
|
||
}
|
||
|
||
public string GetPostStr(Position post)
|
||
{
|
||
switch (post)
|
||
{
|
||
case Position.Commander:
|
||
return CommonUtils.GetLocalizeText("legion_post_commander");
|
||
case Position.RegimentalCommander:
|
||
return CommonUtils.GetLocalizeText("legion_post_president");
|
||
case Position.AssistantRegimentalCommander:
|
||
return CommonUtils.GetLocalizeText("legion_post_assist_president");
|
||
case Position.Leroy:
|
||
return CommonUtils.GetLocalizeText("legion_post_leroy");
|
||
case Position.Diplomatist:
|
||
return CommonUtils.GetLocalizeText("legion_post_diplomatist");
|
||
case Position.Member:
|
||
return CommonUtils.GetLocalizeText("legion_post_member");
|
||
default:
|
||
return "";
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 是否合法军团标签
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public bool CheckIsLegalLabelList(List<int> idxList)
|
||
{
|
||
for (int i = 0; i < idxList.Count; i++)
|
||
{
|
||
var tabIdx = idxList[i];
|
||
if (!CheckIsLegalLabel(tabIdx))
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
public bool CheckIsLegalLabel(int idx)
|
||
{
|
||
if (!GLConfig.Inst.data.LegionLabelList.Contains(idx))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 是否合法军团宣言
|
||
/// </summary>
|
||
/// <param name="idx"></param>
|
||
/// <returns></returns>
|
||
public bool CheckIsLegalManifesto(int idx)
|
||
{
|
||
if (GLConfig.Inst.data.LegionDeclarationList.Contains(idx))
|
||
{
|
||
return true;
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
public void CorrectMyLegion()
|
||
{
|
||
CorrectLegionData(_myLegion);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 校正军团不符合配置的数据
|
||
/// </summary>
|
||
/// <param name="legion"></param>
|
||
public void CorrectLegionData(Legion legion)
|
||
{
|
||
CorrectTabList(legion);
|
||
CorrectManifesto(legion);
|
||
}
|
||
|
||
public void CorrectManifesto(Legion legion)
|
||
{
|
||
var manifestoList = GLConfig.Inst.data.LegionDeclarationList;
|
||
if (!manifestoList.Contains(legion.ManifestoID))
|
||
{
|
||
legion.ManifestoID = manifestoList[0];
|
||
}
|
||
}
|
||
|
||
public void CorrectTabList(Legion legion)
|
||
{
|
||
var count = legion.TabIdxList.Count;
|
||
var labelList = GLConfig.Inst.data.LegionLabelList;
|
||
if (count > labelList.Count)
|
||
{
|
||
legion.TabIdxList.Clear();
|
||
legion.TabIdxList = new List<int>(labelList);
|
||
return;
|
||
}
|
||
|
||
for (int i = 0; i < count; i++)
|
||
{
|
||
var idx = legion.TabIdxList[i];
|
||
if (!CheckIsLegalLabel(idx))
|
||
{
|
||
legion.TabIdxList.Remove(idx);
|
||
}
|
||
}
|
||
|
||
if (legion.TabIdxList.Count < 1)
|
||
{
|
||
legion.TabIdxList.Add(labelList[0]);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取宣言文本
|
||
/// </summary>
|
||
/// <param name="idx"></param>
|
||
/// <returns></returns>
|
||
public string GetLegionLabelStr(int idx)
|
||
{
|
||
var labelIdxList = GLConfig.Inst.data.LegionLabelList;
|
||
var index = labelIdxList.Find(item => item == idx);
|
||
if (index > -1)
|
||
{
|
||
return CommonUtils.GetLocalizeText(E_LocalizePrefix.Legion_Label, idx);
|
||
}
|
||
|
||
DebugUtil.LogError($"无法获取军团标签文本, ID:{idx}");
|
||
return "";
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取
|
||
/// </summary>
|
||
/// <param name="idx"></param>
|
||
/// <returns></returns>
|
||
public string GetLegionManifestoStr(int idx)
|
||
{
|
||
var manifestoIdxList = GLConfig.Inst.data.LegionDeclarationList;
|
||
var index = manifestoIdxList.Find(item => item == idx);
|
||
if (index > -1)
|
||
{
|
||
return CommonUtils.GetLocalizeText(E_LocalizePrefix.Legion_Declaration, idx);
|
||
}
|
||
|
||
DebugUtil.LogError($"无法获取军团宣言文本, ID:{idx}");
|
||
return "";
|
||
}
|
||
|
||
/// <summary>
|
||
/// 随机获取一个宣言
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public int GetRandomManifestoIdx(int curIdx)
|
||
{
|
||
var manifestoIdxList = GLConfig.Inst.data.LegionDeclarationList;
|
||
if (manifestoIdxList.Count > 0)
|
||
{
|
||
//var ranIdx = Random.Range(0, manifestoIdxList.Count);
|
||
var ranIdx = curIdx + 1 >= manifestoIdxList.Count ? 0 : ++curIdx;
|
||
return manifestoIdxList[ranIdx];
|
||
}
|
||
|
||
return 0;
|
||
}
|
||
|
||
public EMyApplyStatus GetApplyStatus(uint legionID)
|
||
{
|
||
if (_myApplyLegionDatas.TryGetValue(legionID, out var myApplyLegionData))
|
||
{
|
||
if (myApplyLegionData.ExamineStatus == LegionExamineStatus.LgNone)
|
||
{
|
||
return EMyApplyStatus.Already;
|
||
}
|
||
|
||
if (myApplyLegionData.ExamineStatus == LegionExamineStatus.LgReject)
|
||
{
|
||
return EMyApplyStatus.Refused;
|
||
}
|
||
|
||
}
|
||
|
||
return EMyApplyStatus.None;
|
||
}
|
||
|
||
public bool CheckPostLimit(Position position)
|
||
{
|
||
var count = 0;
|
||
var numData = TableManager.Instance.Tables.LegionPermissions.DataList[0];
|
||
var maxNum = 0;
|
||
switch (position)
|
||
{
|
||
case Position.RegimentalCommander:
|
||
return !IsPresident;//maxNum = numData.RegimentalCommander;
|
||
break;
|
||
case Position.AssistantRegimentalCommander:
|
||
maxNum = numData.AssistantRegimentalCommander;
|
||
break;
|
||
case Position.Commander:
|
||
maxNum = numData.Commander;
|
||
break;
|
||
case Position.Leroy:
|
||
maxNum = numData.Leroy;
|
||
break;
|
||
case Position.Diplomatist:
|
||
maxNum = numData.Diplomatist;
|
||
break;
|
||
default:
|
||
maxNum = 20;
|
||
break;
|
||
}
|
||
|
||
if (maxNum == 1)
|
||
{
|
||
return false;
|
||
}
|
||
if (_myLegion != null)
|
||
{
|
||
if (_myLegionMemberList != null)
|
||
{
|
||
foreach (var actorData in _myLegionMemberList)
|
||
{
|
||
if (actorData.Post == position)
|
||
{
|
||
++count;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
return count >= maxNum;
|
||
}
|
||
#endregion
|
||
|
||
#region Events
|
||
|
||
private void _RegisterEvents()
|
||
{
|
||
EventManager.Instance.Register(EventManager.EventName.LegionBaseData, _OnLegionBaseUpdate);
|
||
EventManager.Instance.Register(EventManager.EventName.LegionMemberData, _OnLegionMemberUpdate);
|
||
EventManager.Instance.Register(EventManager.EventName.LegionApplyData, _OnLegionApplyListUpdate);
|
||
EventManager.Instance.Register(EventManager.EventName.LegionCreateSuccess, _OnCreateSuccess);
|
||
EventManager.Instance.Register(EventManager.EventName.LegionQuitSuccess, _OnQuitSuccess);
|
||
EventManager.Instance.Register(EventManager.EventName.LegionJoinSendSuccess, _OnJoinApplySendSuccess);
|
||
EventManager.Instance.Register(EventManager.EventName.LegionMemberPostUpdate, _OnMemberPostUpdate);
|
||
EventManager.Instance.Register(EventManager.EventName.LegionJoinPermitted, _OnLegionJoinSuccess);
|
||
EventManager.Instance.Register(EventManager.EventName.LegionLose, _OnLegionLose);
|
||
}
|
||
|
||
private void _UnregisterEvents()
|
||
{
|
||
EventManager.Instance.Unregister(EventManager.EventName.LegionBaseData, _OnLegionBaseUpdate);
|
||
EventManager.Instance.Unregister(EventManager.EventName.LegionMemberData, _OnLegionMemberUpdate);
|
||
EventManager.Instance.Unregister(EventManager.EventName.LegionApplyData, _OnLegionApplyListUpdate);
|
||
EventManager.Instance.Unregister(EventManager.EventName.LegionCreateSuccess, _OnCreateSuccess);
|
||
EventManager.Instance.Unregister(EventManager.EventName.LegionQuitSuccess, _OnQuitSuccess);
|
||
EventManager.Instance.Unregister(EventManager.EventName.LegionJoinSendSuccess, _OnJoinApplySendSuccess);
|
||
EventManager.Instance.Unregister(EventManager.EventName.LegionMemberPostUpdate, _OnMemberPostUpdate);
|
||
EventManager.Instance.Unregister(EventManager.EventName.LegionJoinPermitted, _OnLegionJoinSuccess);
|
||
EventManager.Instance.Unregister(EventManager.EventName.LegionLose, _OnLegionLose);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 军团BaseData更新
|
||
/// </summary>
|
||
private void _OnLegionBaseUpdate()
|
||
{
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 军团成员列表更新
|
||
/// </summary>
|
||
private void _OnLegionMemberUpdate()
|
||
{
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 军团申请成员列表更新
|
||
/// </summary>
|
||
private void _OnLegionApplyListUpdate()
|
||
{
|
||
|
||
}
|
||
|
||
private void _OnLegionLogUpdate()
|
||
{
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 创建军团成功
|
||
/// </summary>
|
||
private void _OnCreateSuccess()
|
||
{
|
||
DebugUtil.LogError("创建军团成功!");
|
||
if (IsPresident)
|
||
{
|
||
DebugUtil.LogG($"军团ID:{_myLegion.ID}");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 退出军团成功
|
||
/// </summary>
|
||
private void _OnQuitSuccess()
|
||
{
|
||
ClearMyLegion();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 加入军团成功
|
||
/// </summary>
|
||
private void _OnJoinApplySendSuccess()
|
||
{
|
||
DebugUtil.LogError("申请加入军团成功!!!");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 修改成员职位
|
||
/// </summary>
|
||
private void _OnMemberPostUpdate()
|
||
{
|
||
//DebugUtil.LogError("成员职位修改成功!!!");
|
||
}
|
||
|
||
private void _OnLegionJoinSuccess()
|
||
{
|
||
ReqMyLegionPart(LegionPart.Member);
|
||
}
|
||
|
||
private void _OnLegionLose()
|
||
{
|
||
//ReqMyLegionPart(LegionPart.Member);
|
||
ClearMyLegion();
|
||
EventManager.Instance.Send(EventManager.EventName.LegionClear);
|
||
}
|
||
#endregion
|
||
} |