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 Dictionary<int, LogicString> materials = new Dictionary<int, LogicString>() {
|
{1,new LogicString () },
|
{2,new LogicString () },
|
{3,new LogicString () },
|
{4,new LogicString () },
|
{5,new LogicString () },
|
};
|
|
public readonly LogicList<Star> stars = new LogicList<Star>();
|
|
public readonly LogicInt specialMaterial = new LogicInt();
|
public readonly LogicInt starResultEffect = new LogicInt();
|
|
Dictionary<int, EquipSetStar> equipStars = new Dictionary<int, EquipSetStar>();
|
Dictionary<int, EquipStarUpgradeCandidate> candidatePlaces = new Dictionary<int, EquipStarUpgradeCandidate>();
|
|
PackModel packModel { get { return ModelCenter.Instance.GetModel<PackModel>(); } }
|
EquipModel equipModel { get { return ModelCenter.Instance.GetModel<EquipModel>(); } }
|
|
public override void Init()
|
{
|
ParseConfig();
|
DTC0721_tagMakeItemAnswer.MakeItemAnswerEvent += OnGetUpgradeStarResult;
|
}
|
|
public override void UnInit()
|
{
|
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);
|
|
foreach (var candidate in candidatePlaces.Values)
|
{
|
candidate.starLevel.value = GetEquipStarLevel(candidate.equipPosition);
|
}
|
}
|
|
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<ushort>();
|
var materialItemIds = new List<uint>();
|
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 void SelectLevel(int level)
|
{
|
selectedLevel.value = level;
|
candidatePlaces.Clear();
|
|
var places = GetStarUpgradablePlaces(level);
|
if (places.IsNullOrEmpty())
|
{
|
SelectPlace(new Int2(level, 0));
|
}
|
else
|
{
|
for (var i = 0; i < places.Count; i++)
|
{
|
var place = places[i];
|
var equipPosition = new Int2(level, place);
|
var candidate = new EquipStarUpgradeCandidate(equipPosition);
|
candidate.starLevel.value = GetEquipStarLevel(equipPosition);
|
candidatePlaces[place] = candidate;
|
if (i == 0)
|
{
|
SelectPlace(equipPosition);
|
}
|
}
|
}
|
}
|
|
public void SelectPlace(Int2 equipPosition)
|
{
|
selectedPlace.value = equipPosition.y;
|
selectedPlace.dirty = true;
|
foreach (var candidate in candidatePlaces.Values)
|
{
|
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);
|
}
|
|
private List<int> GetStarUpgradablePlaces(int level)
|
{
|
if (level <= 0)
|
{
|
return null;
|
}
|
|
var equipSet = equipModel.GetEquipSet(level);
|
if (equipSet == null)
|
{
|
return null;
|
}
|
|
var places = new List<int>();
|
for (var i = 1; i <= 12; i++)
|
{
|
var unLocked = equipSet.IsSlotUnLocked(i);
|
if (unLocked)
|
{
|
places.Add(i);
|
}
|
}
|
|
return places;
|
}
|
|
public List<int> GetCandidatePlaces()
|
{
|
return new List<int>(candidatePlaces.Keys);
|
}
|
|
public EquipStarUpgradeCandidate GetCandidatePlace(int index)
|
{
|
if (candidatePlaces.ContainsKey(index))
|
{
|
return candidatePlaces[index];
|
}
|
else
|
{
|
return null;
|
}
|
}
|
|
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();
|
}
|
|
/// <summary>
|
/// 这是指已经装备位置已经升级到的星级,不考虑当前穿戴的装备
|
/// </summary>
|
/// <param name="equipPosition"></param>
|
/// <returns></returns>
|
public int GetStarLevel(Int2 equipPosition)
|
{
|
if (!equipStars.ContainsKey(equipPosition.x))
|
{
|
return 0;
|
}
|
|
return equipStars[equipPosition.x].GetEquipStarLevel(equipPosition.y);
|
}
|
|
/// <summary>
|
/// 这是指受当前装备限制的星级,比如当前装备位已经升级到了9星,但是穿戴的装备最高可升级到6星,那么返回的值是6
|
/// </summary>
|
/// <param name="equipPosition"></param>
|
/// <returns></returns>
|
public int GetEquipStarLevel(Int2 equipPosition)
|
{
|
var starLevel = GetStarLevel(equipPosition);
|
var maxLevel = GetEquipPositionMaxStarLevel(equipPosition);
|
|
return Mathf.Min(starLevel, maxLevel);
|
}
|
|
public List<int> GetMaterialGetWays(Int2 equipPosition, int starLevel)
|
{
|
var config = EquipStarConfig.Get(equipPosition.x, equipPosition.y, starLevel);
|
if (config == null)
|
{
|
return null;
|
}
|
|
var getWays = new List<int>();
|
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<string> 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() { });
|
for (var i = expcetMaterials.Count - 1; i >= 0; i--)
|
{
|
var item = expcetMaterials[i];
|
if (item.config.RealmLimit != equip.config.RealmLimit)
|
{
|
expcetMaterials.RemoveAt(i);
|
}
|
else if (!config.CostEquipPlace.Contains(item.config.EquipPlace))
|
{
|
expcetMaterials.RemoveAt(i);
|
}
|
else if (!config.CostEquipColor.Contains(item.config.ItemColor))
|
{
|
expcetMaterials.RemoveAt(i);
|
}
|
}
|
|
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<string>();
|
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<int>() { equipPosition.x },
|
qualitys = new List<int>() { config.CostEquipColor[0] },
|
equipTypes = new List<int>() { 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 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;
|
}
|
|
}
|
}
|