using System; using System.Collections; using System.Collections.Generic; using UnityEngine; namespace Snxxz.UI { public class TreasureSkillModel : Model, IBeforePlayerDataInitialize, IPlayerLoginOk { Dictionary treasureSkills = new Dictionary(); Dictionary> jobTreasureSkills = new Dictionary>(); public List skillLevelUpItems = new List(); int m_SelectSkill; public int selectSkill { get { return m_SelectSkill; } set { if (m_SelectSkill != value) { m_SelectSkill = value; if (selectRefresh != null) { selectRefresh(); } } } } bool serverInited = false; public bool requireRemind { get; private set; } public readonly Redpoint redpoint = new Redpoint(103, 10304); public const int REDPOINTID_BASE = 10304000; public static int redpointIndex = 100; PackModel packModel { get { return ModelCenter.Instance.GetModel(); } } public event Action selectRefresh; public event Action treasureSkillRefresh; public event Action skillLevelUpRefresh; public event Action potentialLevelRefresh; public override void Init() { ParseConfig(); packModel.refreshItemCountEvent += RefreshItemCountAct; FuncOpen.Instance.OnFuncStateChangeEvent += OnFuncStateChangeEvent; } public void OnBeforePlayerDataInitialize() { foreach (var skill in treasureSkills.Values) { skill.Reset(); } serverInited = false; } public void OnPlayerLoginOk() { serverInited = true; requireRemind = true; UpdateRedpoint(); } public override void UnInit() { packModel.refreshItemCountEvent -= RefreshItemCountAct; FuncOpen.Instance.OnFuncStateChangeEvent -= OnFuncStateChangeEvent; } private void OnFuncStateChangeEvent(int id) { if (id == 82) { UpdateRedpoint(); } } private void RefreshItemCountAct(PackType packType, int arg2, int itemId) { if (packType == PackType.Item && skillLevelUpItems.Contains(itemId)) { if (serverInited) { requireRemind = true; } UpdateRedpoint(); } } void ParseConfig() { var configs = TreasureSkillConfig.GetValues(); var index = 0; foreach (var config in configs) { TreasureSkill treasureSkill; if (!treasureSkills.TryGetValue(config.limitSkillId, out treasureSkill)) { treasureSkill = new TreasureSkill(config.limitSkillId, index); treasureSkills.Add(config.limitSkillId, treasureSkill); index++; } treasureSkill.potentials.Add(new TreasurePotential(config.id, 0, config.limitLevel, treasureSkill.redpoint.id)); var skillConfig = SkillConfig.Get(config.limitSkillId); List skills; if (!jobTreasureSkills.TryGetValue(skillConfig.UseType, out skills)) { skills = new List(); jobTreasureSkills.Add(skillConfig.UseType, skills); } if (!skills.Contains(config.limitSkillId)) { skills.Add(config.limitSkillId); } } foreach (var skill in treasureSkills.Values) { for (int i = 1; i <= skill.maxLevel; i++) { var config = skill.GetSkillConfig(i); if (!skillLevelUpItems.Contains(config.ExAttr4)) { skillLevelUpItems.Add(config.ExAttr4); } } foreach (var potential in skill.potentials) { for (int i = 1; i <= potential.maxLevel; i++) { var config = potential.GetSkillConfig(i); if (!skillLevelUpItems.Contains(config.ExAttr4)) { skillLevelUpItems.Add(config.ExAttr4); } } } } } public bool TryGetSkills(out List skills) { var job = PlayerDatas.Instance.baseData.Job; var useType = (int)Mathf.Pow(2, job); return jobTreasureSkills.TryGetValue(useType, out skills); } public bool TryGetSkill(int skillId, out TreasureSkill treasureSkill) { return treasureSkills.TryGetValue(skillId, out treasureSkill); } public bool TryGetPotential(int skillId, out TreasurePotential potential) { var treasureSkillId = 0; potential = null; if (ContainsSkill(skillId, out treasureSkillId)) { var treasureSkill = treasureSkills[treasureSkillId]; potential = treasureSkill.potentials.Find((x) => { return x.id == skillId; }); return potential != null; } return false; } public bool IsPotentialUnlock(int skillId) { var treasureSkillId = 0; if (ContainsSkill(skillId, out treasureSkillId)) { var treasureSkill = treasureSkills[treasureSkillId]; TreasurePotential potential; if (TryGetPotential(skillId, out potential)) { return treasureSkill.level >= potential.openLevel; } } return false; } public void OnReceivePackage(int oldSkillID, int newSkillID) { var config = SkillConfig.Get(newSkillID); var skillTypeId = config.SkillTypeID; var treasureSkillId = 0; if (ContainsSkill(skillTypeId, out treasureSkillId)) { var treasureSkill = treasureSkills[treasureSkillId]; var level = 0; if (skillTypeId == treasureSkillId) { level = treasureSkill.level; } else { TreasurePotential potential; if (TryGetPotential(skillTypeId, out potential)) { level = potential.level; } } treasureSkill.Refresh(newSkillID); bool levelUp = false; if (skillTypeId == treasureSkillId) { levelUp = treasureSkill.level > level; } else { TreasurePotential potential; if (TryGetPotential(skillTypeId, out potential)) { levelUp = potential.level > level; } if (potential != null && potentialLevelRefresh != null) { potentialLevelRefresh(skillTypeId, potential.level); } } if (levelUp && serverInited) { if (skillLevelUpRefresh != null) { skillLevelUpRefresh(skillTypeId); } } if (treasureSkillRefresh != null) { treasureSkillRefresh(skillTypeId); } UpdateRedpoint(); } } public bool TryLevelUpTreasureSkill(int skillId, out int error) { error = 0; TreasureSkill skill; if (TryGetSkill(skillId, out skill)) { if (skill.level >= skill.maxLevel) { error = 1; return false; } var config = skill.GetSkillConfig(skill.level); var count = packModel.GetItemCountByID(PackType.Item, config.ExAttr4); if (count < config.ExAttr5) { error = 2; return false; } } return true; } public bool TryLevelUpPotential(int skillId, out int error) { error = 0; TreasurePotential potential; if (TryGetPotential(skillId, out potential)) { if (potential.level >= potential.maxLevel) { error = 11; return false; } var config = potential.GetSkillConfig(potential.level); if (config.LearnSkillReq != 0 && config.LearnSkillLV > 0) { TreasurePotential requirePotential; if (!TryGetPotential(config.LearnSkillReq, out requirePotential) || requirePotential.level < config.LearnSkillLV) { error = 12; return false; } } var count = packModel.GetItemCountByID(PackType.Item, config.ExAttr4); if (count < config.ExAttr5) { error = 13; return false; } } return true; } public void DisplayLevelUpError(int error) { switch (error) { case 1: break; case 2: SysNotifyMgr.Instance.ShowTip("TreasureSkillLevelUpError2"); break; case 11: break; case 12: break; case 13: break; } } public bool ContainsSkill(int skillId, out int treasureSkillId) { treasureSkillId = 0; foreach (var skill in treasureSkills.Values) { if (skill.skillId == skillId) { treasureSkillId = skill.skillId; return true; } if (skill.potentials.FindIndex((x) => { return x.id == skillId; }) != -1) { treasureSkillId = skill.skillId; return true; } } return false; } public void SetAlreadyRemind() { requireRemind = false; UpdateRedpoint(); } void UpdateRedpoint() { var funcOpen = FuncOpen.Instance.IsFuncOpen(82); foreach (var skill in treasureSkills.Values) { var error = 0; if (requireRemind && funcOpen && TryLevelUpTreasureSkill(skill.skillId, out error) && skill.level > 0) { skill.levelUpRedpoint.state = RedPointState.Simple; } else { skill.levelUpRedpoint.state = RedPointState.None; } foreach (var potential in skill.potentials) { if (requireRemind && funcOpen && IsPotentialUnlock(potential.id) && TryLevelUpPotential(potential.id, out error) && skill.level > 0) { potential.levelUpRedpoint.state = RedPointState.Simple; } else { potential.levelUpRedpoint.state = RedPointState.None; } } } } } public class TreasureSkill { public readonly int skillId; public readonly int maxLevel; public int level { get; private set; } public Redpoint redpoint { get; private set; } public Redpoint levelUpRedpoint { get; private set; } public List potentials { get; private set; } public TreasureSkill(int skillId, int redpointIndex) { this.skillId = skillId; potentials = new List(); var config = SkillConfig.Get(skillId); maxLevel = config.SkillMaxLV; redpoint = new Redpoint(10304, TreasureSkillModel.REDPOINTID_BASE + redpointIndex); levelUpRedpoint = new Redpoint(redpoint.id, TreasureSkillModel.REDPOINTID_BASE + TreasureSkillModel.redpointIndex++); } public SkillConfig GetSkillConfig(int level) { if (level > 0) { return SkillConfig.Get(skillId + level - 1); } else { return SkillConfig.Get(skillId + level); } } public void Refresh(int newSkillId) { var config = SkillConfig.Get(newSkillId); if (config.SkillTypeID == skillId) { level = config.SkillLV; } else { var index = potentials.FindIndex((x) => { return x.id == config.SkillTypeID; }); if (index != -1) { potentials[index].Refresh(newSkillId); } } } public void Reset() { level = 0; foreach (var potential in potentials) { potential.Reset(); } } } public class TreasurePotential { public readonly int id; public readonly int openLevel; public readonly int maxLevel; public int level { get; private set; } public Redpoint levelUpRedpoint { get; private set; } public TreasurePotential(int id, int level, int openLevel, int redpointBase) { this.id = id; this.level = level; this.openLevel = openLevel; var config = SkillConfig.Get(id); maxLevel = config.SkillMaxLV; levelUpRedpoint = new Redpoint(redpointBase, TreasureSkillModel.REDPOINTID_BASE + TreasureSkillModel.redpointIndex++); } public SkillConfig GetSkillConfig(int level) { if (level > 0) { return SkillConfig.Get(id + level - 1); } else { return SkillConfig.Get(id + level); } } public void Refresh(int newSkillId) { var config = SkillConfig.Get(newSkillId); if (config.SkillTypeID == id) { level = config.SkillLV; } } public void Reset() { level = 0; } } }