using LitJson; using Snxxz.UI; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; namespace Snxxz.UI { public class RealmModel : Model, IPlayerLoginOk, IBeforePlayerDataInitialize { Dictionary> m_RealmProperties = new Dictionary>(); Dictionary> m_RealmPreviewEquips = new Dictionary>(); Dictionary m_RealmEquipConditions = new Dictionary(); Dictionary> m_RecommandEquips = new Dictionary>(); List> m_RealmStages = new List>(); public int realmMaxLevel { get; private set; } public int realmPoolOpenLevel { get; private set; } public int realmEquipDisplayLevel { get; private set; } public int realmLevelUpReikiPoint { get; private set; } public bool isBossPass { get; private set; } public int realmExpTime { get; private set; } public long startExp { get; private set; } public uint buffAddRate { get; private set; } public uint buffSeconds { get; private set; } public int realmUpgrateNoEnoughReason = 0; //境界提升不足提示 1。换装备,2 升星 public long totalExp { get { if (PlayerDatas.Instance.baseData.realmLevel < realmPoolOpenLevel) { return startExp; } float exp = startExp; var config = RealmConfig.Get(PlayerDatas.Instance.baseData.realmLevel); if (exp >= config.expLimit) { return config.expLimit; } var tick = (TimeUtility.ServerNow - expStartTime).Ticks; var singleTick = realmExpTime * TimeSpan.TicksPerSecond; var times = tick / singleTick; float expRate = config.expRate; long buffTimes = 0; if (buffSeconds > 0) { var buffTick = buffSeconds * TimeSpan.TicksPerSecond; var buffTicks = tick > buffTick ? buffTick : tick; buffTimes = buffTicks / singleTick; } if (buffTimes > 0) { var rate = expRate + expRate * (buffAddRate / 10000f); exp += rate * buffTimes; } var normalTimes = times - buffTimes; if (normalTimes > 0) { exp += expRate * normalTimes; } exp = exp > config.expLimit ? config.expLimit : exp; return (long)exp; } } public DateTime expStartTime { get; private set; } int realmPoolRedpointPercent = 100; UIEffect realmEffect; int realmEffectCount = 0; public const int REALM_DUNGEON_ID = 31110; public readonly Redpoint levelUpRedpoint = new Redpoint(114, 11401); public readonly Redpoint challengeRedpoint = new Redpoint(114, 11402); //public readonly Redpoint realmPoolRedpoint = new Redpoint(114, 11403); public readonly Redpoint realmDailyRedpoint = new Redpoint(114, 11404); int m_SelectRealm = 0; public int selectRealm { get { return m_SelectRealm; } set { if (m_SelectRealm != value) { m_SelectRealm = value; if (selectRealmRefresh != null) { selectRealmRefresh(); } } } } int cacheMapId = 0; bool redpointDirty = false; bool serverInited = false; public List displayRealms = new List(); public int displayRealmLevel { get { if (displayRealms.Count > 0) { return displayRealms[0]; } return PlayerDatas.Instance.baseData.realmLevel; } } public event Action selectRealmRefresh; public event Action realmExpRefresh; EquipModel equipModel { get { return ModelCenter.Instance.GetModel(); } } EquipStarModel equipStarModel { get { return ModelCenter.Instance.GetModel(); } } PackModel packModel { get { return ModelCenter.Instance.GetModel(); } } ActivitiesPushModel pushModel { get { return ModelCenter.Instance.GetModel(); } } public override void Init() { StageLoad.Instance.onStageLoadFinish += OnStageLoadFinish; PlayerDatas.Instance.playerDataRefreshEvent += PlayerDataRefreshEvent; GlobalTimeEvent.Instance.secondEvent += PerSecond; packModel.refreshItemCountEvent += RefreshItemCountEvent; FuncOpen.Instance.OnFuncStateChangeEvent += OnFuncStateChangeEvent; ParseConfig(); } public void OnBeforePlayerDataInitialize() { isBossPass = false; expStartTime = DateTime.Now; startExp = 0; serverInited = false; buffSeconds = 0; buffAddRate = 0; realmEffectCount = 0; SysNotifyMgr.Instance.OnSystemNotifyEvent -= OnSystemNotifyEvent; } public void OnPlayerLoginOk() { redpointDirty = true; serverInited = true; SysNotifyMgr.Instance.OnSystemNotifyEvent -= OnSystemNotifyEvent; SysNotifyMgr.Instance.OnSystemNotifyEvent += OnSystemNotifyEvent; RefreshRedpoint(); TryPushRealmPoolMessage(); } public override void UnInit() { StageLoad.Instance.onStageLoadFinish -= OnStageLoadFinish; PlayerDatas.Instance.playerDataRefreshEvent -= PlayerDataRefreshEvent; packModel.refreshItemCountEvent -= RefreshItemCountEvent; GlobalTimeEvent.Instance.secondEvent -= PerSecond; FuncOpen.Instance.OnFuncStateChangeEvent -= OnFuncStateChangeEvent; } private void OnFuncStateChangeEvent(int id) { if (id == (int)FuncOpenEnum.Realm) { RefreshRedpoint(); } } private void PerSecond() { if (redpointDirty) { RefreshRedpoint(); redpointDirty = false; } //if (serverInited) //{ // RefreshRealmPoolRedpoint(); //} TryPlayRealmEffect(); } void ParseConfig() { realmMaxLevel = 0; List stages = new List(); m_RealmStages.Add(stages); var configs = RealmConfig.GetValues(); foreach (var config in configs) { if (config.Lv > realmMaxLevel) { realmMaxLevel = config.Lv; } if (config.AddAttrType != null && config.AddAttrType.Length > 0) { Dictionary dict = new Dictionary(); for (int i = 0; i < config.AddAttrType.Length; i++) { dict.Add(config.AddAttrType[i], config.AddAttrNum[i]); } m_RealmProperties.Add(config.Lv, dict); } if (config.Lv != 0) { stages.Add(config.Lv); if (config.BossID != 0) { stages = new List(); m_RealmStages.Add(stages); } } if (!string.IsNullOrEmpty(config.equips)) { var json = LitJson.JsonMapper.ToObject(config.equips); Dictionary dict = new Dictionary(); foreach (var jobKey in json.Keys) { var job = int.Parse(jobKey); var equipArray = LitJson.JsonMapper.ToObject(json[jobKey].ToJson()); dict.Add(job, equipArray); } m_RealmPreviewEquips.Add(config.Lv, dict); } if (!string.IsNullOrEmpty(config.recommandEquips)) { var json = LitJson.JsonMapper.ToObject(config.recommandEquips); Dictionary dict = new Dictionary(); foreach (var jobKey in json.Keys) { var job = int.Parse(jobKey); var equipArray = LitJson.JsonMapper.ToObject(json[jobKey].ToJson()); dict.Add(job, equipArray); } m_RecommandEquips.Add(config.Lv, dict); } if (realmPoolOpenLevel == 0 && config.expRate != 0) { realmPoolOpenLevel = config.Lv; } if (config.NeedEquip != null && config.NeedEquip.Length > 1) { m_RealmEquipConditions.Add(config.Lv, new RealmLevelUpEquipCondition() { level = config.NeedEquip[0], starLevel = config.NeedEquip[1], isSuit = config.NeedEquip[2] == 1, itemColor = config.NeedEquip.Length > 3 ? config.NeedEquip[3] : 0, }); } } var funcConfig = FuncConfigConfig.Get("RealmExpTime"); realmExpTime = int.Parse(funcConfig.Numerical1); funcConfig = FuncConfigConfig.Get("RealmEqiupDisplayLevel"); realmEquipDisplayLevel = int.Parse(funcConfig.Numerical1); realmPoolRedpointPercent = int.Parse(funcConfig.Numerical2); } public bool TryGetRealmProperty(int level, out Dictionary propertyDict) { return m_RealmProperties.TryGetValue(level, out propertyDict); } public bool TryGetRealmStages(int index, out List stages) { stages = null; if (index < m_RealmStages.Count) { stages = m_RealmStages[index]; return true; } return false; } public bool TryGetRealmPreviewEquips(int level, int job, out int[] equips) { equips = null; if (m_RealmPreviewEquips.ContainsKey(level)) { return m_RealmPreviewEquips[level].TryGetValue(job, out equips); } return false; } public bool TryGetRecommandEquips(int level, int job, out int[] equips) { equips = null; if (m_RecommandEquips.ContainsKey(level)) { return m_RecommandEquips[level].TryGetValue(job, out equips); } return false; } public bool TryGetRealmEquipCondition(int level, out RealmLevelUpEquipCondition equipCondition) { return m_RealmEquipConditions.TryGetValue(level, out equipCondition); } public bool TryLevelUp(out int error) { error = 0; var realmLevel = PlayerDatas.Instance.baseData.realmLevel; if (realmLevel >= realmMaxLevel) { error = 100; return false; } var config = RealmConfig.Get(realmLevel); if (PlayerDatas.Instance.baseData.LV < config.NeedLV) { error = 1; return false; } if (config.BossID != 0 && !isBossPass) { error = 2; return false; } int equipError = 0; if (!SatisfyEquipCondition(realmLevel, out equipError)) { error = equipError; return false; } if (config.NeedGood != 0) { var count = packModel.GetItemCountByID(PackType.Item, config.NeedGood); if (count < config.NeedNum) { error = 3; return false; } } return true; } private void TryPlayRealmEffect() { if (realmEffect != null) { if (realmEffect.IsPlaying) { return; } else { realmEffectCount--; realmEffect = null; } } if (realmEffectCount > 0) { if (WindowCenter.Instance.IsOpen() && !WindowCenter.Instance.ExistAnyFullScreenOrMaskWin()) { realmEffect = EffectMgr.Instance.PlayUIEffect(7047, 1800, WindowCenter.Instance.uiRoot.baseCanvas, false); } } } public bool IsUnlockEquipRealm(int realmLevel, out int level) { level = 0; var equipSets = equipModel.GetAllEquipSets(); var index = equipSets.FindIndex((x) => { var equipSet = equipModel.GetEquipSet(x); if (equipSet != null) { return equipSet.realm == realmLevel; } return false; }); if (index != -1) { level = equipSets[index]; return true; } return false; } public bool IsBuffActive(DateTime time) { return buffSeconds > 0 && expStartTime.AddTicks(TimeSpan.TicksPerSecond * buffSeconds) > time; } public bool SatisfyEquipCondition(int realmLevel, out List notSatisfyPlaces) { notSatisfyPlaces = null; RealmLevelUpEquipCondition equipCondition; if (!TryGetRealmEquipCondition(realmLevel, out equipCondition)) { return true; } notSatisfyPlaces = new List(); for (int i = 1; i <= 8; i++) { var equipGuid = equipModel.GetEquip(new Int2(equipCondition.level, i)); var itemModel = packModel.GetItemByGuid(equipGuid); if (itemModel != null) { if (equipCondition.isSuit) { if (itemModel.config.SuiteiD == 0) { notSatisfyPlaces.Add(i); continue; } } else { if (itemModel.config.ItemColor < equipCondition.itemColor) { notSatisfyPlaces.Add(i); continue; } } var starLevel = equipStarModel.GetEquipStarLevel(new Int2(equipCondition.level, i)); if (starLevel < equipCondition.starLevel) { notSatisfyPlaces.Add(i); } } else { notSatisfyPlaces.Add(i); } } return notSatisfyPlaces.Count == 0; } public bool SatisfyEquipCondition(int realmLevel, out int error) { error = 0; RealmLevelUpEquipCondition equipCondition; if (!TryGetRealmEquipCondition(realmLevel, out equipCondition)) { return true; } for (int i = 1; i <= 8; i++) { var equipGuid = equipModel.GetEquip(new Int2(equipCondition.level, i)); var itemModel = packModel.GetItemByGuid(equipGuid); if (itemModel != null) { if (equipCondition.isSuit) { if (itemModel.config.SuiteiD == 0) { error = 4; return false; } } else { if (itemModel.config.ItemColor < equipCondition.itemColor) { error = 4; return false; } } var starLevel = equipStarModel.GetEquipStarLevel(new Int2(equipCondition.level, i)); if (starLevel < equipCondition.starLevel) { error = 5; return false; } } else { error = 4; return false; } } return true; } public bool HasEquipCondition(int realmLevel) { return m_RealmEquipConditions.ContainsKey(realmLevel); } public int GetRealmStage(int realmLevel) { if (realmLevel == 0) { return 0; } for (int i = 0; i < m_RealmStages.Count; i++) { var stages = m_RealmStages[i]; var index = stages.IndexOf(realmLevel); if (index != -1) { if (stages.Count == 1) { return i - 1; } return i; } } return m_RealmStages.Count - 1; } public int GetRealmStageEffect(int realmLevel) { var config = RealmConfig.Get(realmLevel); if (config != null) { switch (config.Quality) { case 1: return 7028; case 2: return 7029; case 3: return 7030; case 4: return 7031; case 5: return 7032; case 6: return 7033; } } return 7028; } public int GetRealmCoverEffect(int realmLevel) { var config = RealmConfig.Get(realmLevel); if (config != null) { switch (config.Quality) { case 1: return 7039; case 2: return 7040; case 3: return 7041; case 4: return 7042; case 5: return 7043; case 6: return 7044; } } return 7039; } public int GetRealmLineEffect(int realmLevel) { var config = RealmConfig.Get(realmLevel); if (config != null) { switch (config.Quality) { case 1: return 7034; case 2: return 7059; case 3: return 7060; case 4: return 7061; case 5: return 7062; case 6: return 7063; } } return 7034; } public bool GetBossEffectShow(int realmLevel) { var playerId = PlayerDatas.Instance.baseData.PlayerID; return LocalSave.GetBool(StringUtility.Contact("RealmBossEffect_", realmLevel, "_", playerId)); } public void SetBossEffectShow(int realmLevel) { var playerId = PlayerDatas.Instance.baseData.PlayerID; LocalSave.SetBool(StringUtility.Contact("RealmBossEffect_", realmLevel, "_", playerId), true); } public bool SatisfyChallengeBoss(int reamLevel) { var config = RealmConfig.Get(reamLevel); if (config != null) { return config.BossID != 0 && !isBossPass && PlayerDatas.Instance.baseData.LV >= config.NeedLV; } return false; } public void SendLevelUpRealm() { CA523_tagCMRealmLVUp pak = new CA523_tagCMRealmLVUp(); GameNetSystem.Instance.SendInfo(pak); } public void TryPushRealmPoolMessage() { return; var remainTime = 0f; var realmLevel = PlayerDatas.Instance.baseData.realmLevel; var config = RealmConfig.Get(realmLevel); if (realmLevel >= realmPoolOpenLevel && config.expRate != 0) { if (totalExp < config.expLimit) { float surplusExp = config.expLimit - totalExp; if (buffSeconds > 0) { var expRate = config.expRate + config.expRate * (buffAddRate / 10000f); var times = buffSeconds / realmExpTime; if (times * expRate >= surplusExp) { remainTime = Mathf.CeilToInt(surplusExp / expRate) * realmExpTime; surplusExp = 0; } else { remainTime = times * realmExpTime; surplusExp = surplusExp - times * expRate; } } if (surplusExp > 0) { remainTime += Mathf.CeilToInt(surplusExp / config.expRate) * realmExpTime; } } } if (remainTime > 0f) { var json = pushModel.GetPushJsonData(11403, (int)remainTime, Language.Get("RealmPoolGeTui_Title"), Language.Get("RealmPoolGeTui_Content")); pushModel.SendPushInfo(json); } else { pushModel.RemovePushInfo(11403); } } public void ReceivePackage(HA311_tagMCSyncRealmInfo package) { isBossPass = package.IsPass == 1; RefreshRedpoint(); } public void ReceivePackage(HA327_tagMCRealmExpInfo package) { expStartTime = TimeUtility.GetTime(package.BeginTime); startExp = package.CurExpPoint * (long)Constants.ExpPointValue + package.CurExp; buffAddRate = package.BuffAddRate; buffSeconds = package.BuffTime; if (realmExpRefresh != null) { realmExpRefresh(); } //RefreshRealmPoolRedpoint(); TryPushRealmPoolMessage(); } private void OnStageLoadFinish() { if (!(StageLoad.Instance.currentStage is DungeonStage)) { cacheMapId = 0; } else { var mapId = PlayerDatas.Instance.baseData.MapID; if (cacheMapId == REALM_DUNGEON_ID && mapId != REALM_DUNGEON_ID) { SnxxzGame.Instance.StartCoroutine(Co_TryOpenRealmWin()); } cacheMapId = mapId; } } private void OnSystemNotifyEvent(string key) { if (key == "BigRealmUpSuccess") { if (WindowCenter.Instance.IsOpen() && !WindowCenter.Instance.ExistAnyFullScreenOrMaskWin()) { if (realmEffectCount < 2) { realmEffectCount++; TryPlayRealmEffect(); } } } } private void PlayerDataRefreshEvent(PlayerDataType dataType) { if (dataType == PlayerDataType.RealmLevel || dataType == PlayerDataType.LV || dataType == PlayerDataType.FightPoint) { redpointDirty = true; } } private void RefreshItemCountEvent(PackType packType, int arg2, int itemId) { if (packType == PackType.Item) { var config = RealmConfig.Get(PlayerDatas.Instance.baseData.realmLevel); if (config != null && config.NeedGood == itemId) { redpointDirty = true; } } if (packType == PackType.Equip) { redpointDirty = true; } } IEnumerator Co_TryOpenRealmWin() { yield return WaitingForSecondConst.WaitMS1000; TryOpenRealmWin(); } private void TryOpenRealmWin() { if (NewBieCenter.Instance.inGuiding) { return; } var treasureModel = ModelCenter.Instance.GetModel(); if (treasureModel.newGotShowing) { return; } if (WindowCenter.Instance.ExistAnyFullScreenOrMaskWin()) { return; } WindowCenter.Instance.Open(); } public void SetDayRemind() { if (realmDailyRedpoint.state == RedPointState.Simple) { DayRemind.Instance.SetDayRemind(DayRemind.DAILYREALMREDPOINT, true); RefreshRedpoint(); } } void RefreshRedpoint() { var levelUpable = false; var challengeable = false; var dailyRedpointable = false; if (FuncOpen.Instance.IsFuncOpen((int)FuncOpenEnum.Realm)) { var realmLevel = PlayerDatas.Instance.baseData.realmLevel; if (realmLevel < realmMaxLevel) { var config = RealmConfig.Get(realmLevel); if (SatisfyChallengeBoss(realmLevel)) { challengeable = PlayerDatas.Instance.baseData.FightPoint >= (ulong)config.FightPower; } var error = 0; if (TryLevelUp(out error)) { levelUpable = true; } } if (realmLevel >= realmPoolOpenLevel) { if (!DayRemind.Instance.GetDayRemind(DayRemind.DAILYREALMREDPOINT)) { dailyRedpointable = true; } } } levelUpRedpoint.state = levelUpable ? RedPointState.Simple : RedPointState.None; challengeRedpoint.state = challengeable ? RedPointState.Simple : RedPointState.None; realmDailyRedpoint.state = dailyRedpointable ? RedPointState.Simple : RedPointState.None; //RefreshRealmPoolRedpoint(); } //void RefreshRealmPoolRedpoint() //{ // var isPoolFull = false; // if (FuncOpen.Instance.IsFuncOpen((int)FuncOpenEnum.Realm)) // { // var realmLevel = PlayerDatas.Instance.baseData.realmLevel; // if (realmLevel >= realmPoolOpenLevel) // { // var config = RealmConfig.Get(PlayerDatas.Instance.baseData.realmLevel); // if (totalExp >= config.expLimit * (realmPoolRedpointPercent / 100f)) // { // isPoolFull = true; // } // } // } // realmPoolRedpoint.state = isPoolFull ? RedPointState.Simple : RedPointState.None; //} } public struct RealmLevelUpEquipCondition { public int level; public int starLevel; public bool isSuit; public int itemColor; } }