using LitJson;
|
using vnxbqy.UI;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
|
class PetHorseAwakingModel : ILModel<PetHorseAwakingModel>
|
{
|
PetModel petModel;
|
MountModel horseModel;
|
PackModel packModel;
|
|
public int selectId;
|
public bool[] dataDirtys;
|
Redpoint redpoint;
|
bool isServerPrepare = false;
|
bool redpointDirty = false;
|
bool dailyRemind = false;
|
|
public int selectSkinLevel;
|
|
public event Action onFuncStateUpdate;
|
public event Action onSelectUpdate;
|
public event Action onSelectSkinUpdate;
|
|
Dictionary<int, PetHorseAwakingData> datas = new Dictionary<int, PetHorseAwakingData>();
|
Dictionary<int, string> qualityLabels = new Dictionary<int, string>();
|
public List<int> displayIds = new List<int>();
|
public List<int> devourItems;
|
|
public int[] awarkMaterialArr;
|
|
protected override void Init()
|
{
|
this.ParseConfig();
|
petModel = ModelCenter.Instance.GetModelEx<PetModel>();
|
petModel.onPetInfoUpdate += FuncStateUpdate;
|
horseModel = ModelCenter.Instance.GetModelEx<MountModel>();
|
horseModel.onHorseInfoUpdate += FuncStateUpdate;
|
packModel = ModelCenter.Instance.GetModelEx<PackModel>();
|
packModel.refreshItemCountEvent += OnRefreshItemCountEvent;
|
|
GlobalTimeEvent.Instance.secondEvent += OnPerSecond;
|
GameEvent.playerLoginOkEvent += OnPlayerLoginOk;
|
GameEvent.beforePlayerDataInitializeEvent += OnBeforePlayerDataInitialize;
|
|
redpoint = new Redpoint(105, 10503);
|
dataDirtys = new bool[] { false, false };
|
}
|
|
protected override void UnInit()
|
{
|
petModel.onPetInfoUpdate -= FuncStateUpdate;
|
horseModel.onHorseInfoUpdate -= FuncStateUpdate;
|
packModel.refreshItemCountEvent -= OnRefreshItemCountEvent;
|
GlobalTimeEvent.Instance.secondEvent -= OnPerSecond;
|
GameEvent.playerLoginOkEvent -= OnPlayerLoginOk;
|
GameEvent.beforePlayerDataInitializeEvent -= OnBeforePlayerDataInitialize;
|
}
|
|
public void AddFuncStateUpdate(PetWin petWin)
|
{
|
onFuncStateUpdate += petWin.OnFuncStateUpdate;
|
}
|
|
public void RemoveFuncStateUpdate(PetWin petWin)
|
{
|
onFuncStateUpdate -= petWin.OnFuncStateUpdate;
|
}
|
|
void OnBeforePlayerDataInitialize()
|
{
|
var keyList = datas.Keys.ToList();
|
for (int i = 0; i < keyList.Count; i++)
|
{
|
datas[keyList[i]].Reset();
|
}
|
this.isServerPrepare = false;
|
}
|
|
void OnPlayerLoginOk()
|
{
|
this.isServerPrepare = true;
|
}
|
|
void OnPerSecond()
|
{
|
if (this.redpointDirty)
|
{
|
this.redpointDirty = false;
|
this.UpdateRedpoint();
|
}
|
}
|
|
void PerSecond()
|
{
|
if (redpointDirty)
|
{
|
this.redpointDirty = false;
|
this.UpdateRedpoint();
|
}
|
}
|
|
public void ParseConfig()
|
{
|
var configs = ILPetHorseAwakingConfig.GetValues();
|
configs.Sort((x, y) =>
|
{
|
if (x.ID == y.ID)
|
{
|
if (x.SkinLV != y.SkinLV)
|
return x.SkinLV.CompareTo(y.SkinLV);
|
}
|
return x.Key.CompareTo(y.Key);
|
});
|
|
for (int i = 0; i < configs.Count; i++)
|
{
|
var config = configs[i];
|
PetHorseAwakingData data;
|
if (!this.datas.TryGetValue(config.ID, out data))
|
{
|
data = new PetHorseAwakingData(10503);
|
this.datas.Add(config.ID, data);
|
}
|
data.Parse(config);
|
}
|
|
var funcConfig = FuncConfigConfig.Get("PetQuality");
|
var quailyArray = funcConfig.Numerical1.Split('|');
|
var labelArray = funcConfig.Numerical2.Split('|');
|
var length = Math.Min(quailyArray.Length, labelArray.Length);
|
for (int i = 0; i < length; i++)
|
{
|
var quality = int.Parse(quailyArray[i]);
|
this.qualityLabels[quality] = labelArray[i];
|
}
|
|
awarkMaterialArr = JsonMapper.ToObject<int[]>(FuncConfigConfig.Get("pethorsesort").Numerical1);
|
}
|
|
void FuncStateUpdate()
|
{
|
onFuncStateUpdate?.Invoke();
|
this.UpdateRedpoint();
|
}
|
|
public void AddEvent(int type, Action action)
|
{
|
if (type == 1)
|
this.onSelectUpdate += action;
|
else if (type == 2)
|
this.onSelectSkinUpdate += action;
|
}
|
|
public void RemoveEvent(int type, Action action)
|
{
|
if (type == 1)
|
this.onSelectUpdate -= action;
|
else if (type == 2)
|
this.onSelectSkinUpdate -= action;
|
}
|
|
public void ExecuteEvent(int type)
|
{
|
if (type == 1)
|
this.onSelectUpdate?.Invoke();
|
else if (type == 2)
|
this.onSelectSkinUpdate?.Invoke();
|
}
|
|
public void RefreshDisplays()
|
{
|
displayIds.Clear();
|
var keyList = datas.Keys.ToList();
|
for (int i = 0; i < keyList.Count; i++)
|
{
|
var data = datas[keyList[i]];
|
var type = data.Type;
|
var id = data.Id;
|
if (type == 1 && horseModel.IsHorseSkinCanAwake(id))
|
displayIds.Add(id);
|
else if (type == 2 && petModel.IsPetUnlock(id))
|
displayIds.Add(id);
|
}
|
displayIds.Sort(Compare);
|
}
|
|
public PetHorseAwakingData GetData(int id)
|
{
|
return this.datas[id];
|
}
|
|
public string GetQualityLabel(int quality)
|
{
|
return this.qualityLabels[quality];
|
}
|
|
public List<int> GetDevourItems()
|
{
|
if (devourItems != null)
|
{
|
devourItems.Sort(CompareDevour);
|
return devourItems;
|
}
|
devourItems = new List<int>();
|
var configs = ILPetHorseDevourConfig.GetValues();
|
for (int i = 0; i < configs.Count; i++)
|
{
|
var config = configs[i];
|
devourItems.Add(config.itemId);
|
}
|
devourItems.Sort(CompareDevour);
|
return devourItems;
|
}
|
|
public ILPetHorseAwakeSkinConfig GetPetHorseSkinConfig(int id, int skinIndex)
|
{
|
var configs = ILPetHorseAwakeSkinConfig.GetValues();
|
for (int i = 0; i < configs.Count; i++)
|
{
|
var config = configs[i];
|
if (config.id == id && config.skinIndex == skinIndex)
|
return config;
|
}
|
return null;
|
}
|
|
public ILPetHorseAwakingConfig GetPetHorseAwakeConfig(int id, int level)
|
{
|
var configs = ILPetHorseAwakingConfig.GetValues();
|
for (int i = 0; i < configs.Count; i++)
|
{
|
var config = configs[i];
|
if (config.ID == id && config.SkinLV == level)
|
return config;
|
}
|
return null;
|
}
|
|
public int GetPetHorseEffect(int id, int skinIdex)
|
{
|
var configs = ILPetHorseAwakeSkinConfig.GetValues();
|
for (int i = 0; i < configs.Count; i++)
|
{
|
var config = configs[i];
|
if (config.id == id && config.skinIndex == skinIdex)
|
return config.effect;
|
}
|
return 0;
|
}
|
|
public string GetPetHorseEffectNode(int id, int skinIndex)
|
{
|
var configs = ILPetHorseAwakeSkinConfig.GetValues();
|
for (int i = 0; i < configs.Count; i++)
|
{
|
var config = configs[i];
|
if (config.id == id && config.skinIndex == skinIndex)
|
return config.effectNode;
|
}
|
return "";
|
}
|
|
public bool IsFirstTimeDevour()
|
{
|
var isFirstTimeDevour = true;
|
var keyList = datas.Keys.ToList();
|
for (int i = 0; i < keyList.Count; i++)
|
{
|
var data = datas[keyList[i]];
|
if (data.Level > 0 || data.Exp > 0)
|
{
|
isFirstTimeDevour = false;
|
break;
|
}
|
}
|
return isFirstTimeDevour;
|
}
|
|
public bool IsPetOrHorseUnlock(int id, int type)
|
{
|
if (type == 1)
|
return horseModel.IsHorseSkinCanAwake(id);
|
else if (type == 2)
|
return petModel.IsPetUnlock(id);
|
return false;
|
}
|
|
public bool IsOpen()
|
{
|
this.RefreshDisplays();
|
return displayIds.Count > 0;
|
}
|
|
|
int Compare(int lhs, int rhs)
|
{
|
var lhs_data = this.datas[lhs];
|
var rhs_data = this.datas[rhs];
|
var lhs_maxLevel = lhs_data.IsMaxLevel;
|
var rhs_maxLevel = rhs_data.IsMaxLevel;
|
if (lhs_maxLevel != rhs_maxLevel)
|
return lhs_maxLevel.CompareTo(rhs_maxLevel);
|
|
var lhs_id = lhs_data.Id;
|
var rhs_id = rhs_data.Id;
|
var lhs_type = lhs_data.Type;
|
var rhs_type = rhs_data.Type;
|
//if (lhs_type != rhs_type)
|
// return rhs_type.CompareTo(lhs_type);
|
|
var lhs_quality = 0;
|
var rhs_quality = 0;
|
if (lhs_type == 1)
|
{
|
var config = HorseConfig.Get(lhs_id);
|
lhs_quality = config.Quality;
|
}
|
else if (lhs_type == 2)
|
{
|
var config = PetInfoConfig.Get(lhs_id);
|
lhs_quality = config.Quality;
|
}
|
|
if (rhs_type == 1)
|
{
|
var config = HorseConfig.Get(rhs_id);
|
rhs_quality = config.Quality;
|
}
|
else if (rhs_type == 2)
|
{
|
var config = PetInfoConfig.Get(rhs_id);
|
rhs_quality = config.Quality;
|
}
|
|
if (lhs_quality != rhs_quality)
|
return lhs_quality.CompareTo(rhs_quality);
|
|
return lhs_id.CompareTo(rhs_id);
|
}
|
|
int CompareDevour(int lhs, int rhs)
|
{
|
//碎片优先
|
var lhs_itemConfig = ItemConfig.Get(lhs);
|
var rhs_itemConfig = ItemConfig.Get(rhs);
|
|
var lhs_count_zero = packModel.GetItemCountByID(PackType.Item, lhs) == 0;
|
var rhs_count_zero = packModel.GetItemCountByID(PackType.Item, rhs) == 0;
|
|
if (lhs_count_zero != rhs_count_zero)
|
return lhs_count_zero.CompareTo(rhs_count_zero);
|
|
|
//var lhs_devour = ILPetHorseDevourConfig.Get(lhs);
|
//var rhs_devour = ILPetHorseDevourConfig.Get(rhs);
|
//var lhs_activemat = GetUnlockItem(lhs_devour.type, lhs_devour.id) == lhs;
|
//var rhs_activemat = GetUnlockItem(rhs_devour.type, rhs_devour.id) == rhs;
|
//if (lhs_activemat != rhs_activemat)
|
// return lhs_activemat.CompareTo(rhs_activemat);
|
|
//碎片0 完整1
|
int lhs_type = awarkMaterialArr.Contains(lhs_itemConfig.Type) ? 0 : 1;
|
int rhs_type = awarkMaterialArr.Contains(rhs_itemConfig.Type) ? 0 : 1;
|
if (lhs_type != rhs_type)
|
return lhs_type.CompareTo(rhs_type);
|
|
if (lhs_itemConfig.ItemColor != rhs_itemConfig.ItemColor)
|
return lhs_itemConfig.ItemColor.CompareTo(rhs_itemConfig.ItemColor);
|
|
return lhs_itemConfig.ID.CompareTo(rhs_itemConfig.ID);
|
}
|
|
int GetUnlockItem(int type, int id)
|
{
|
if (type == 1)
|
{
|
var config = HorseConfig.Get(id);
|
return config.ItemID;
|
}
|
else if (type == 2)
|
{
|
var config = PetInfoConfig.Get(id);
|
return config.UnLockNeedItemID;
|
}
|
return -1;
|
}
|
|
public void SetRemind()
|
{
|
this.dailyRemind = true;
|
this.UpdateRedpoint();
|
}
|
|
bool HasDevourItem(int itemId)
|
{
|
var configs = ILPetHorseDevourConfig.GetValues();
|
for (int i = 0; i < configs.Count; i++)
|
{
|
var config = configs[i];
|
if (config.itemId == itemId)
|
return true;
|
}
|
return false;
|
}
|
|
bool HasEnoughItem()
|
{
|
var items = this.GetDevourItems();
|
for (int i = 0; i < items.Count; i++)
|
{
|
var config = ILPetHorseDevourConfig.Get(items[i]);
|
if (IsPetOrHorseUnlock(config.id, config.type))
|
{
|
var count = packModel.GetItemCountByID(PackType.Item, items[i]);
|
return count > 0;
|
}
|
}
|
return false;
|
}
|
|
void UpdateRedpoint()
|
{
|
var isopen = IsOpen();
|
var hasItem = HasEnoughItem();
|
var keyList = datas.Keys.ToList();
|
for (int i = 0; i < keyList.Count; i++)
|
{
|
var id = keyList[i];
|
var data = datas[keyList[i]];
|
var redpointAble = false;
|
if (isopen && this.isServerPrepare && !this.dailyRemind)
|
{
|
var isHorsePetMaxLevel = false;
|
if (data.Type == 1 && horseModel.IsHorseMaxLevel(id))
|
isHorsePetMaxLevel = true;
|
else if (data.Type == 2 && petModel.IsPetMaxLevel(id))
|
isHorsePetMaxLevel = true;
|
if (this.IsPetOrHorseUnlock(id, data.Type) && isHorsePetMaxLevel && !data.IsMaxLevel && hasItem)
|
redpointAble = true;
|
}
|
|
if (redpointAble)
|
data.Redpoint.state = RedPointState.Simple;
|
else
|
data.Redpoint.state = RedPointState.None;
|
}
|
}
|
|
void RefreshItemCountEvent(PackType packType, int index, int itemId)
|
{
|
if (packType == PackType.Item && this.isServerPrepare)
|
if (this.HasDevourItem(itemId))
|
{
|
this.dailyRemind = false;
|
this.redpointDirty = true;
|
}
|
}
|
|
void OnRefreshItemCountEvent(PackType type, int index, int itemId)
|
{
|
if (type == PackType.Item && this.isServerPrepare)
|
{
|
if (this.HasDevourItem(itemId))
|
{
|
this.dailyRemind = false;
|
this.redpointDirty = true;
|
}
|
}
|
}
|
|
public void ReceivePackage(IL_HA312_tagMCHorsePetSkinData package)
|
{
|
var length = package.Num;
|
for (int i = 0; i < length; i++)
|
{
|
var info = package.InfoList[i];
|
var data = this.GetData((int)info.ID);
|
if (data != null)
|
{
|
data.Level = info.SkinLV;
|
data.Exp = (int)info.Exp;
|
data.SkinIdex = info.SkinIndex;
|
}
|
}
|
|
for (int i = 0; i < dataDirtys.Length; i++)
|
{
|
this.dataDirtys[i] = true;
|
}
|
|
if (this.isServerPrepare)
|
this.redpointDirty = true;
|
}
|
|
public void SendDevour(int id, int itemId)
|
{
|
var data = this.GetData(id);
|
var package = new IL_CA529_tagCMHorsePetAwake();
|
package.Type = (ushort)data.Type;
|
package.ID = (uint)id;
|
package.EatItemID = (uint)itemId;
|
GameNetSystem.Instance.SendInfo(package);
|
}
|
|
public void SendSelectSkin(int id, int skinIndex)
|
{
|
var data = this.GetData(id);
|
var package = new IL_CA530_tagCMHorsePetSkinSelect();
|
package.Type = (ushort)data.Type;
|
package.ID = (uint)id;
|
package.SkinIndex = (byte)skinIndex;
|
GameNetSystem.Instance.SendInfo(package);
|
}
|
|
public int GetDisplayId(int index)
|
{
|
return 0;
|
}
|
}
|
|