/// #pkgName FGUI包名
/// #panelName UIPanel名字
/// #UIName = $"{#pkgName}{#panelName}" UIKey名字
/// 该脚本由模板创建
/// created by cb 2024
using System.Collections.Generic;
using System.Runtime.Remoting.Contexts;
using FairyGUI;
using FL.Data;
using XGame.Database;
using XGame.Framework.Persistence;
using XGame.Framework.UI;
namespace FL.FGUI
{
public class PartnerSbBagListParam
{
///
/// 道具类型,为0则是标题,否则是列表
///
public int Type;
public string Title;
public List SbList;
}
///
/// UI逻辑处理类
///
///
public partial class PartnerSbBagPanelCtrl : UIController
{
protected override void OnEnable(object intent)
{
AddUIListenres();
InitView();
VM.OpenSelect.selectedIndex = 1;
ShowUI();
}
protected override void OnDisable()
{
RemoveUIListenres();
}
#region UI事件
private void AddUIListenres()
{
VM.OnStrongBtn.onClick.Add(OnClickOnStrongBtn);
VM.SortBtn.onClick.Add(OnClickSortBtn);
VM.ShangZhenBtn.onClick.Add(OnClickSortTypeBtn);
VM.PinZhiBtn.onClick.Add(OnClickSortTypeBtn);
VM.DengJiBtn.onClick.Add(OnClickSortTypeBtn);
VM.IDBtn.onClick.Add(OnClickSortTypeBtn);
VM.PanelEvent.Add(OnClickPanel);
Context.AddListener(EventDefine.PartnerDown, OnUpdateList);
Context.AddListener(EventDefine.PartnerUp, OnUpdateList);
Context.AddListener(EventDefine.PartnerUpLevel, OnUpdateList);
Context.AddListener(EventDefine.PartnerStrongAll, OnUpdateList);
Context.AddListener(EventDefine.PartnerUpStar, OnUpdateList);
Context.AddListener(EventDefine.PartnerBinding, OnUpdateList);
}
private void RemoveUIListenres()
{
VM.OnStrongBtn.onClick.Remove(OnClickOnStrongBtn);
VM.SortBtn.onClick.Remove(OnClickSortBtn);
VM.ShangZhenBtn.onClick.Remove(OnClickSortTypeBtn);
VM.PinZhiBtn.onClick.Remove(OnClickSortTypeBtn);
VM.DengJiBtn.onClick.Remove(OnClickSortTypeBtn);
VM.IDBtn.onClick.Remove(OnClickSortTypeBtn);
VM.PanelEvent.Remove(OnClickPanel);
Context.RemoveListener(EventDefine.PartnerDown, OnUpdateList);
Context.RemoveListener(EventDefine.PartnerUp, OnUpdateList);
Context.RemoveListener(EventDefine.PartnerUpLevel, OnUpdateList);
Context.RemoveListener(EventDefine.PartnerStrongAll, OnUpdateList);
Context.RemoveListener(EventDefine.PartnerUpStar, OnUpdateList);
Context.RemoveListener(EventDefine.PartnerBinding, OnUpdateList);
}
private void OnClickPanel()
{
if (VM.OpenSelect.selectedIndex == 1)
return;
VM.OpenSelect.selectedIndex = 1;
}
private void OnClickSortTypeBtn(EventContext context)
{
context.StopPropagation();
if (_curSortType == VM.CurSelect.selectedIndex)
return;
_curSortType = VM.CurSelect.selectedIndex;
Persistence.Write(
PlayerData.Instance.UID + PersistenceKeys.partner_sort_type,
_curSortType
);
ShowUI();
}
private void OnClickOnStrongBtn(EventContext context)
{
var list = PartnersData.Instance.PartnerList;
var canUp = false;
foreach (var item in list)
{
var table = partnerStarUpTableRepo.GetPartnerStarTable(
item.TableId,
item.RisingStarLv
);
var nextTable = partnerStarUpTableRepo.GetPartnerStarTable(
item.TableId,
item.RisingStarLv + 1
);
XGame.Framework.Assert.IsNotNull(
table,
$"圣兵表升星不存在,id{item.TableId},starlv:{item.RisingStarLv}"
);
if (
nextTable != null
&& ItemService.Instance.IsEnough(table.PetId, table.StarUpCost, false)
)
{
canUp = true;
break;
}
}
if (canUp)
PartnersService.Instance.RequestPartnerOneKeyStarUp();
else
{
Context.ShowTips(StringDefine.PartnerCantStarUpAll);
}
}
private void OnClickSortBtn(EventContext context)
{
context.StopPropagation();
VM.OpenSelect.selectedIndex = VM.OpenSelect.selectedIndex > 0 ? 0 : 1;
}
private void OnUpdateList(int eventId, object args)
{
ShowUI();
}
#endregion
private List _dataList = new List();
private bool _init = false;
private int _curSortType = 0;
private void InitView()
{
if (_init)
return;
_init = true;
VM.CurSelect.selectedIndex = Persistence.Read(
PlayerData.Instance.UID + PersistenceKeys.partner_sort_type,
0
);
VM.List.ListType = EGListType.Virtual;
}
private void ShowUI()
{
var allSbList = PartnerTableRepo.GetAll();
var haveList = new List();
var noHaveList = new List();
foreach (var sb in allSbList)
{
var attr = PartnersData.Instance.GetPartnerAttrByTableId(sb.Id);
if (attr != null)
{
haveList.Add(sb.Id);
}
else
{
noHaveList.Add(sb.Id);
}
}
haveList.Sort(
(a, b) =>
{
PartnersData.Instance.TryGetPartnerAttrByTableId(a, out var aAttr);
PartnersData.Instance.TryGetPartnerAttrByTableId(b, out var bAttr);
switch (VM.CurSelect.selectedIndex)
{
case 0:
if (aAttr.Status != bAttr.Status)
{
return bAttr.Status - aAttr.Status;
}
break;
case 1:
var aTable = PartnerTableRepo.Get(a);
var bTable = PartnerTableRepo.Get(b);
if (aTable.Quality != bTable.Quality)
{
return bTable.Quality - aTable.Quality;
}
break;
case 2:
if (bAttr.Level != aAttr.Level)
{
return bAttr.Level - aAttr.Level;
}
break;
}
return a - b;
}
);
noHaveList.Sort(
(a, b) =>
{
switch (VM.CurSelect.selectedIndex)
{
case 1:
var aTable = PartnerTableRepo.Get(a);
var bTable = PartnerTableRepo.Get(b);
if (aTable.Quality != bTable.Quality)
{
return bTable.Quality - aTable.Quality;
}
break;
}
return a - b;
}
);
_dataList.Clear();
_dataList.Add(new PartnerSbBagListParam() { Type = 0, Title = StringDefine.Have });
var list = new List();
foreach (var have in haveList)
{
list.Add(have);
if (list.Count >= 5)
{
_dataList.Add(new PartnerSbBagListParam() { Type = 1, SbList = list });
list = new List();
}
}
if (list.Count > 0)
{
_dataList.Add(new PartnerSbBagListParam() { Type = 1, SbList = list });
list = new List();
}
_dataList.Add(new PartnerSbBagListParam() { Type = 0, Title = StringDefine.NoHave });
foreach (var noHave in noHaveList)
{
list.Add(noHave);
if (list.Count >= 5)
{
_dataList.Add(new PartnerSbBagListParam() { Type = 1, SbList = list });
list = new List();
}
}
//var dataList = new List();
//foreach (var item in allSbList)
//{
// dataList.Add(item.Id);
//}
//dataList.Sort((a, b) =>
//{
// PartnersData.Instance.TryGetPartnerAttrByTableId(a, out var aAttr);
// PartnersData.Instance.TryGetPartnerAttrByTableId(b, out var bAttr);
// if (aAttr != null && bAttr != null)
// {
// if (aAttr.Status != bAttr.Status)
// {
// return bAttr.Status - aAttr.Status;
// }
// else if (bAttr.Level != aAttr.Level)
// {
// return bAttr.Level - aAttr.Level;
// }
// }
// else if (aAttr == null && bAttr != null)
// {
// return 1;
// }
// else if (aAttr != null && bAttr == null)
// {
// return -1;
// }
// return b - a;
//});
VM.List.BindDatas(_dataList);
}
}
}