using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Linq; namespace Snxxz.UI { [XLua.LuaCallCSharp] public class EquipStarModel : Model, IPlayerLoginOk, IBeforePlayerDataInitialize { public readonly LogicInt selectedLevel = new LogicInt(); public readonly LogicInt selectedPlace = new LogicInt(); public readonly LogicInt equipStarLevel = new LogicInt(); public readonly LogicInt equipMaxStarLevel = new LogicInt(); public readonly LogicInt operateMaterialIndex = new LogicInt(); public readonly LogicInt starUpgradeProbability = new LogicInt(); public readonly LogicList candidatePlaces = new LogicList(); public readonly Dictionary materials = new Dictionary() { {1,new LogicString () }, {2,new LogicString () }, {3,new LogicString () }, {4,new LogicString () }, {5,new LogicString () }, }; public readonly LogicList stars = new LogicList(); public readonly LogicInt specialMaterial = new LogicInt(); public readonly LogicInt starResultEffect = new LogicInt(); Dictionary equipStars = new Dictionary(); Redpoint redpoint = new Redpoint(1720000); bool redpointDirty = false; LogicUpdate logicUpdate = new LogicUpdate(1); PackModel packModel { get { return ModelCenter.Instance.GetModel(); } } EquipModel equipModel { get { return ModelCenter.Instance.GetModel(); } } public override void Init() { ParseConfig(); logicUpdate.Start(OnUpdate); packModel.refreshItemCountEvent += OnItemCountRefresh; DTC0721_tagMakeItemAnswer.MakeItemAnswerEvent += OnGetUpgradeStarResult; } public override void UnInit() { packModel.refreshItemCountEvent -= OnItemCountRefresh; DTC0721_tagMakeItemAnswer.MakeItemAnswerEvent -= OnGetUpgradeStarResult; } public void OnBeforePlayerDataInitialize() { } public void OnPlayerLoginOk() { } public void UpdateStarLevels(HA3B1_tagMCEquipPartStarInfo info) { var selectedEquipOldStar = GetEquipStarLevel(new Int2(selectedLevel.value, selectedPlace.value)); for (var i = 0; i < info.InfoList.Length; i++) { var equipPosition = EquipSet.ServerPlaceToClientPlace(info.InfoList[i].EquipPackIndex); var level = equipPosition.x; if (equipStars.ContainsKey(level)) { equipStars[level].UpdateEquipStarLevel(equipPosition.y, info.InfoList[i].Star); } } var starLevel = GetEquipStarLevel(new Int2(selectedLevel.value, selectedPlace.value)); equipStarLevel.value = starLevel; selectedPlace.dirty = true; var maxStarLevel = GetMaxStarLevel(selectedLevel.value); UpdateSelelctedEquipStars(selectedEquipOldStar, starLevel, maxStarLevel); for (int i = 0; i < candidatePlaces.Count; i++) { var candidate = candidatePlaces[i]; candidate.starLevel.value = GetEquipStarLevel(candidate.equipPosition); } redpointDirty = true; } void OnGetUpgradeStarResult(H0721_tagMakeItemAnswer info) { if (info.MakeType == (byte)MakeType.EquipStarUpgrade) { var equipPosition = new Int2(selectedLevel.value, selectedPlace.value); AutoAddMaterials(equipPosition); CalculateStarUpgradeProbability(equipPosition); if (info.Result == 1) { starResultEffect.value = 1; } else { starResultEffect.value = -1; } } } public void DoStarUpgrade(Int2 equipPosition, int starLevel) { if (!equipStars.ContainsKey(equipPosition.x)) { return; } var equip = packModel.GetItemByGuid(equipModel.GetEquip(equipPosition)); if (equip == null) { return; } if (equip.config.ItemColor < 4) { SysNotifyMgr.Instance.ShowTip("StarLevelUp4"); return; } var maxLevel = GetMaxStarLevel(equipPosition.x); if (starLevel > maxLevel) { SysNotifyMgr.Instance.ShowTip("StarLevelUp1"); return; } var config = EquipStarConfig.Get(equipPosition.x, equipPosition.y, starLevel); if (config == null) { return; } if (config.CostItemDict.x > 0 && specialMaterial.value == 0) { SysNotifyMgr.Instance.ShowTip("StarLevelUp2", config.CostItemDict.x); return; } if (config.CostItemDict.x > 0 && packModel.GetItemCountByID(PackType.Item, config.CostItemDict.x) < config.CostItemDict.y) { SysNotifyMgr.Instance.ShowTip("StarLevelUp2", config.CostItemDict.x); return; } var materialIndexs = new List(); var materialItemIds = new List(); foreach (var material in materials.Values) { if (!string.IsNullOrEmpty(material.value)) { var item = packModel.GetItemByGuid(material.value); materialIndexs.Add((ushort)item.gridIndex); materialItemIds.Add((uint)item.itemId); } } if (materialIndexs.Count < config.CostEquipCnt) { SysNotifyMgr.Instance.ShowTip("StarLevelUp3"); return; } var info = new CA5C5_tagCMEquipPartStarUp(); info.EquipPackIndex = (ushort)EquipSet.ClientPlaceToServerPlace(equipPosition); info.CostEquipIndex = materialIndexs.ToArray(); info.CostEquipID = materialItemIds.ToArray(); info.CostEquipCnt = (byte)materialIndexs.Count; GameNetSystem.Instance.SendInfo(info); } public void ResetOperateParams() { selectedLevel.value = 0; selectedPlace.value = 0; equipStarLevel.value = 0; equipMaxStarLevel.value = 0; operateMaterialIndex.value = 0; starUpgradeProbability.value = -1; starResultEffect.value = 0; foreach (var item in materials.Values) { item.value = string.Empty; } stars.Clear(); } public Int2 GetRecommendEquipPosition() { foreach (var starSet in equipStars.Values) { var level = starSet.level; for (var place = 1; place <= 12; place++) { var isRedpoint = starSet.GetRedpointState(place) != RedPointState.None; if (isRedpoint) { return new Int2(level, place); } } } foreach (var starSet in equipStars.Values) { var level = starSet.level; for (var i = 1; i <= 12; i++) { var equipPosition = new Int2(level, i); var item = packModel.GetItemByGuid(equipModel.GetEquip(equipPosition)); if (item == null) { continue; } var starLevel = GetStarLevel(equipPosition); var maxStarLevel = GetMaxStarLevel(item.config.ItemColor, item.config.LV); if (starLevel >= maxStarLevel) { continue; } } } return new Int2(1, 1); } public void SelectLevel(int level) { selectedLevel.value = level; candidatePlaces.Clear(); if (level > 0) { for (var i = 1; i <= 12; i++) { var place = i; var equipPosition = new Int2(level, place); var candidate = new EquipStarUpgradeCandidate(equipPosition); candidate.starLevel.value = GetEquipStarLevel(equipPosition); candidatePlaces.Add(candidate); candidatePlaces.Sort(EquipStarUpgradeCandidateCompare); } SelectPlace(candidatePlaces[0].equipPosition); } else { SelectPlace(Int2.zero); } } public void SelectPlace(Int2 equipPosition) { selectedPlace.value = equipPosition.y; selectedPlace.dirty = true; if (equipPosition.x > 0 && equipPosition.y >= 1 && equipPosition.y <= 12) { for (int i = 0; i < candidatePlaces.Count; i++) { var candidate = candidatePlaces[i]; candidate.selected.value = candidate.equipPosition == equipPosition; } var starLevel = GetEquipStarLevel(equipPosition); var maxStarLevel = GetMaxStarLevel(equipPosition.x); equipStarLevel.value = starLevel; equipMaxStarLevel.value = maxStarLevel; UpdateSelelctedEquipStars(starLevel, starLevel, maxStarLevel); AutoAddMaterials(equipPosition); CalculateStarUpgradeProbability(equipPosition); } } public bool IsEquipPlaceUpgradable(string equipGuid) { if (string.IsNullOrEmpty(equipGuid)) { return false; } var equip = packModel.GetItemByGuid(equipGuid); if (equip == null) { return false; } var maxStarLevel = GetMaxStarLevel(equip.config.ItemColor, equip.config.LV); var currentStarLevel = GetEquipStarLevel(new Int2(equip.config.LV, equip.config.EquipPlace)); return currentStarLevel < maxStarLevel; } public static int GetMaxStarLevel(int quality, int level) { var max = 0; if (GeneralDefine.equipStarLimit.ContainsKey(quality)) { var itemLevelStars = GeneralDefine.equipStarLimit[quality]; if (itemLevelStars.ContainsKey(level)) { max = itemLevelStars[level]; } } return max; } public static int GetMaxStarLevel(int level) { var max = 0; foreach (var itemLevelStars in GeneralDefine.equipStarLimit.Values) { if (itemLevelStars.ContainsKey(level)) { if (itemLevelStars[level] > max) { max = itemLevelStars[level]; } } } return max; } public int GetEquipPositionMaxStarLevel(Int2 equipPosition) { var equipGuid = equipModel.GetEquip(equipPosition); if (equipGuid == null) { return 0; } var equip = packModel.GetItemByGuid(equipGuid); if (equip == null) { return 0; } return GetMaxStarLevel(equip.config.ItemColor, equip.config.LV); } public int GetTotalStarLevel(int level) { if (!equipStars.ContainsKey(level)) { return 0; } return equipStars[level].GetTotalStarLevel(); } /// /// 这是指已经装备位置已经升级到的星级,不考虑当前穿戴的装备 /// /// /// public int GetStarLevel(Int2 equipPosition) { if (!equipStars.ContainsKey(equipPosition.x)) { return 0; } return equipStars[equipPosition.x].GetEquipStarLevel(equipPosition.y); } /// /// 这是指受当前装备限制的星级,比如当前装备位已经升级到了9星,但是穿戴的装备最高可升级到6星,那么返回的值是6 /// /// /// public int GetEquipStarLevel(Int2 equipPosition) { var starLevel = GetStarLevel(equipPosition); var maxLevel = GetEquipPositionMaxStarLevel(equipPosition); return Mathf.Min(starLevel, maxLevel); } public List GetMaterialGetWays(Int2 equipPosition, int starLevel) { var config = EquipStarConfig.Get(equipPosition.x, equipPosition.y, starLevel); if (config == null) { return null; } var getWays = new List(); foreach (int place in config.CostEquipPlace) { var equipControlConfig = EquipControlConfig.Get(equipPosition.x, place); foreach (int getWay in equipControlConfig.getWays) { if (!getWays.Contains(getWay)) { getWays.Add(getWay); } } } return getWays; } public List GetMaterials(Int2 equipPosition, int starLevel) { var config = EquipStarConfig.Get(equipPosition.x, equipPosition.y, starLevel); if (config == null) { return null; } var equipGuid = equipModel.GetEquip(equipPosition); var equip = packModel.GetItemByGuid(equipGuid); if (equip == null) { return null; } var expcetMaterials = packModel.GetItems(PackType.Item, new SinglePack.FilterParams() { qualitys = new List(config.CostEquipColor), equipTypes = new List(config.CostEquipPlace), levels = new List() { equip.config.LV } }); for (var i = 1; i <= 5; i++) { var placedMaterialGuid = GetMaterialLogicStringByIndex(i).value; var materialItem = packModel.GetItemByGuid(placedMaterialGuid); if (materialItem != null) { expcetMaterials.Remove(materialItem); } } var materialGuids = new List(); foreach (var item in expcetMaterials) { materialGuids.Add(item.guid); } return materialGuids; } public void AddMaterial(int index, string itemGuid) { GetMaterialLogicStringByIndex(index).value = itemGuid; CalculateStarUpgradeProbability(new Int2(selectedLevel.value, selectedPlace.value)); } public void RemoveMaterial(int index) { GetMaterialLogicStringByIndex(index).value = string.Empty; CalculateStarUpgradeProbability(new Int2(selectedLevel.value, selectedPlace.value)); } private void AutoAddMaterials(Int2 equipPosition) { var equipGuid = equipModel.GetEquip(equipPosition); var upgradable = IsEquipPlaceUpgradable(equipGuid); if (upgradable) { var config = EquipStarConfig.Get(equipPosition.x, equipPosition.y, GetEquipStarLevel(equipPosition) + 1); if (config.CostEquipPlace.Length == 1 && config.CostEquipColor.Length == 1) { var items = packModel.GetItems(PackType.Item, new SinglePack.FilterParams() { levels = new List() { equipPosition.x }, qualitys = new List() { config.CostEquipColor[0] }, equipTypes = new List() { config.CostEquipPlace[0] } }); if (!items.IsNullOrEmpty()) { var minCount = Mathf.Min(config.CostEquipCnt, items.Count); for (int i = 1; i <= 5; i++) { materials[i].value = i <= minCount ? items[i].guid : string.Empty; } } } { for (int i = 1; i <= 5; i++) { materials[i].value = string.Empty; } } if (config.CostItemDict.x > 0) { var itemId = config.CostItemDict.x; specialMaterial.value = itemId; } else { specialMaterial.value = 0; } } else { foreach (var material in materials.Values) { material.value = string.Empty; } } } private void CalculateStarUpgradeProbability(Int2 equipPosition) { var probability = 0f; var equipGuid = equipModel.GetEquip(equipPosition); var upgradable = IsEquipPlaceUpgradable(equipGuid); if (upgradable) { for (var i = 1; i <= 5; i++) { var itemGuid = GetMaterialLogicStringByIndex(i).value; probability += GetMaterialSuccessRate(equipPosition, itemGuid); } starUpgradeProbability.value = Mathf.RoundToInt(probability * 100); } else { starUpgradeProbability.value = -1; } } public float GetMaterialSuccessRate(Int2 equipPosition, string guid) { if (string.IsNullOrEmpty(guid)) { return 0f; } var item = packModel.GetItemByGuid(guid); if (item == null) { return 0f; } var currentStarLevel = GetEquipStarLevel(equipPosition); var config = EquipStarConfig.Get(equipPosition.x, equipPosition.y, currentStarLevel + 1); if (config == null) { return 0f; } var singleItemRate = (float)config.SuitTotalRate * 0.01f / config.CostEquipCnt; return item.config.SuiteiD > 0 ? singleItemRate : singleItemRate * 0.5f; } public LogicString GetMaterialLogicStringByIndex(int index) { return materials.ContainsKey(index) ? materials[index] : null; } private void UpdateSelelctedEquipStars(int oldStarLevel, int newStarLevel, int maxStarLevel) { stars.Clear(); for (var i = 1; i <= maxStarLevel; i++) { stars.Add(new Star() { actived = i <= newStarLevel, newGet = i <= newStarLevel && i > oldStarLevel, }); } } private void OnItemCountRefresh(PackType type, int index, int itemId) { switch (type) { case PackType.Item: if (ItemLogicUtility.Instance.IsEquip(itemId)) { redpointDirty = true; } var itemConfig = ItemConfig.Get(itemId); if (itemConfig.Type == 34) { redpointDirty = true; } break; case PackType.Equip: var clientEquipPosition = EquipSet.ServerPlaceToClientPlace(index); if (clientEquipPosition.x > 0) { redpointDirty = true; } break; } } private void UpdateStarRedpoint() { var targetEquipPosition = Int2.zero; var minStarLevel = 999; foreach (var trainSet in equipStars.Values) { var level = trainSet.level; for (var place = 1; place <= 12; place++) { var equipPosition = new Int2(level, place); var item = packModel.GetItemByGuid(equipModel.GetEquip(equipPosition)); if (item == null) { continue; } //达到最大星级 var maxStarLevel = GetMaxStarLevel(item.config.ItemColor, item.config.LV); var starLevel = GetStarLevel(new Int2(level, place)); if (starLevel >= maxStarLevel) { continue; } //不是候选者中星级最小的 if (targetEquipPosition != Int2.zero && starLevel >= minStarLevel) { continue; } var config = EquipStarConfig.Get(level, place, starLevel + 1); if (config == null) { continue; } //固定材料不足 if (config.CostItemDict.x > 0 && packModel.GetItemCountByID(PackType.Item, config.CostItemDict.x) < config.CostItemDict.y) { continue; } var materials = packModel.GetItems(PackType.Item, new SinglePack.FilterParams() { qualitys = new List(config.CostEquipColor), equipTypes = new List(config.CostEquipPlace), levels = new List() { item.config.LV } }); for (int j = materials.Count - 1; j >= 0; j--) { var material = materials[j]; if (ItemLogicUtility.Instance.IsFightUp(material.itemId, material.score) == 1) { materials.RemoveAt(j); } } if (materials.Count < config.CostEquipCnt) { continue; } minStarLevel = starLevel; targetEquipPosition = new Int2(level, place); } } foreach (var starSet in equipStars.Values) { var level = starSet.level; if (!equipModel.IsLevelUnLocked(level)) { continue; } for (var place = 1; place <= 12; place++) { var isRedpoint = level == targetEquipPosition.x && place == targetEquipPosition.y; starSet.UpdateRedpoint(place, isRedpoint ? RedPointState.Simple : RedPointState.None); } } } private void OnUpdate() { if (redpointDirty) { redpointDirty = false; UpdateStarRedpoint(); } } private int EquipStarUpgradeCandidateCompare(EquipStarUpgradeCandidate x, EquipStarUpgradeCandidate y) { var guidX = equipModel.GetEquip(x.equipPosition); var guidY = equipModel.GetEquip(y.equipPosition); if (!string.IsNullOrEmpty(guidX) && string.IsNullOrEmpty(guidY)) { return -1; } if (string.IsNullOrEmpty(guidX) && !string.IsNullOrEmpty(guidY)) { return 1; } if (string.IsNullOrEmpty(guidX) && string.IsNullOrEmpty(guidY)) { return x.equipPosition.y.CompareTo(y.equipPosition.y); } var equipX = packModel.GetItemByGuid(guidX); var equipY = packModel.GetItemByGuid(guidY); var canUpgradeStarLevelX = GetMaxStarLevel(equipX.config.ItemColor, equipX.config.LV) - GetStarLevel(x.equipPosition); var canUpgradeStarLevelY = GetMaxStarLevel(equipY.config.ItemColor, equipY.config.LV) - GetStarLevel(y.equipPosition); var compareResult = canUpgradeStarLevelX.CompareTo(canUpgradeStarLevelY); if (compareResult == 0) { return x.equipPosition.y.CompareTo(y.equipPosition.y); } return -compareResult; } private void ParseConfig() { var configs = EquipStarConfig.GetValues(); foreach (var config in configs) { if (!equipStars.ContainsKey(config.Level)) { equipStars[config.Level] = new EquipSetStar(config.Level); } } } public struct Star { public bool actived; public bool newGet; } } }