/// #pkgName FGUI包名
/// #panelName UIPanel名字
/// #UIName = $"{#pkgName}{#panelName}" UIKey名字
/// 该脚本由模板创建
/// created by cb 2024
using FairyGUI;
using FL.Data;
using FL.Data.Items;
using FL.Network;
using System.Collections.Generic;
using System.Linq;
using XGame.Database;
using XGame.Framework;
using XGame.Framework.Time;
using XGame.Framework.UI;
namespace FL.FGUI
{
///
/// UI逻辑处理类
///
///
public partial class ItemGetItemPanelCtrl : UIController
{
private ITimer _delayTimer; // 延迟展示高战力装备
private List _waitEquipIdList; // 穿戴|替换等待服务器消息返回的装备id列表
private List _dropItems;
protected override void OnEnable(object intent)
{
AddUIListenres();
AddEventLisenter();
ShowUI(intent as List);
}
protected override void OnDisable()
{
RemoveUIListenres();
RemoveEventLisenter();
ClearTimer();
_waitEquipIdList?.Clear();
_waitEquipIdList = null;
_dropItems?.Clear();
_dropItems = null;
EquipData.Instance.ClearTemporaryEquipList();
}
private void ClearTimer()
{
_delayTimer?.Cancel();
_delayTimer = null;
}
#region UI事件
private void AddUIListenres()
{
VM.OpenBagBtn.onClick.Add(OnClickOpenBagBtn);
}
private void RemoveUIListenres()
{
VM.OpenBagBtn.onClick.Remove(OnClickOpenBagBtn);
}
///
/// 一键处理
///
///
private void OnClickOpenBagBtn(EventContext context)
{
SendToSellTemporaryEquip();
}
#endregion
private void AddEventLisenter()
{
Context.AddListener(EventDefine.WearEquipSucess, WearEquipSucess);
Context.AddListener(EventDefine.RemoveTemporaryEquip, RemoveTemporaryEquip);
Context.AddListener(EventDefine.RefreshTemporaryEquip, RefreshTemporaryEquip);
FrameworkEvent.Instance.AddListener(XGame.Framework.EventDefine.UI_CLOSED, OnUIClosed);
}
private void RemoveEventLisenter()
{
Context.RemoveListener(EventDefine.WearEquipSucess, WearEquipSucess);
Context.RemoveListener(EventDefine.RemoveTemporaryEquip, RemoveTemporaryEquip);
Context.RemoveListener(EventDefine.RefreshTemporaryEquip, RefreshTemporaryEquip);
FrameworkEvent.Instance.RemoveListener(XGame.Framework.EventDefine.UI_CLOSED, OnUIClosed);
}
private void OnClosePanel()
{
Context.ClosePanel();
}
private void ShowUI(List dropItems)
{
if (dropItems == null) return;
_dropItems = dropItems;
ShowBtn(false);
if (IsShowOneKeyBtn())
{
ShowEquipUI();
}
else
ShowDropUI();
}
private void ShowDropUI()
{
//XGame.Log.Info($"道具的数量:{_dropItems.Count}");
bool bLess = _dropItems.Count < 6; // 单行
VM.ItemList.visible = !bLess;
VM.LessItemList.visible = bLess;
if (bLess)
{
VM.LessItemList.BindDatas(_dropItems);
}
else
{
VM.ItemList.BindDatas(_dropItems);
}
VM.EmptyComCtrl.selectedIndex = bLess ? 0 : (_dropItems.Count > 15 ? 2 : 1);
VM.ItemList.scrollPane.bouncebackEffect = _dropItems.Count > 15 ? true : false;
}
///
/// 打开恭喜获得界面是否需要显示一键处理按钮
///
///
private bool IsShowOneKeyBtn()
{
bool bShowBtn = false;
if (_dropItems.Count > 0)
{
for (int i = 0; i < _dropItems.Count; i++)
{
if (_dropItems[i].kind == 1)
{
return true;
}
}
}
return bShowBtn;
}
private void ShowBtn(bool bShow)
{
VM.OpenBagBtn.visible = bShow;
}
private void ShowEquipUI()
{
// 装备类型
List equipItemList = _dropItems.FindAll(item => item.kind == 1);
var highEquipMap = new Dictionary();
foreach (var item in equipItemList)
{
var equipItem = EquipData.Instance.GetTemporaryEquipItem(item.id);
if (equipItem != null && IsHighEquip(equipItem))
{
if (highEquipMap.ContainsKey(equipItem.EquipType))
{
if (highEquipMap[equipItem.EquipType].FightingPower < equipItem.FightingPower)
{
highEquipMap[equipItem.EquipType] = equipItem;
}
}
else
{
highEquipMap.Add(equipItem.EquipType, equipItem);
}
}
}
RefreshUI();
if (highEquipMap.Count > 0)
{
DelayToShowHighEquip(highEquipMap);
}
else
{
ShowBtn(true);
}
}
///
/// 等待掉落道具显示后再冒泡展示高战力装备替换界面
///
///
private void DelayToShowHighEquip(Dictionary highEquipMap)
{
ClearTimer();
_delayTimer = Context.Time.AddDelayTimer(1000, () =>
{
ShowHighEquipUI(highEquipMap);
});
}
///
/// 是否高战力装备
///
///
///
private bool IsHighEquip(EquipAttr equipItem)
{
EquipAttr wearEquip = EquipData.Instance.GetWearEquipData(equipItem.Table.Part);
if (wearEquip == null) return true;
return equipItem.FightingPower > wearEquip.FightingPower;
}
private void ShowHighEquipUI(Dictionary highEquipMap)
{
if (highEquipMap?.Count == 0)
{
ShowBtn(true);
return;
}
if (_waitEquipIdList?.Count > 0)
_waitEquipIdList.Clear();
else
_waitEquipIdList = new List();
foreach (var item in highEquipMap)
{
_waitEquipIdList.Add(item.Value.Id);
}
highEquipMap.Clear();
ShowEquipEquipPanel();
}
///
/// 显示装备穿戴|替换界面
///
private void ShowEquipEquipPanel()
{
if (_waitEquipIdList?.Count > 0)
{
EquipData.Instance.BubblingEquipUid = _waitEquipIdList[0];
EquipAttr equipData = EquipData.Instance.GetTemporaryEquipItem(_waitEquipIdList[0]);
if (equipData != null)
{
var equipBase = new EquipItemBase();
equipBase.InitById(equipData.Id);
Context.UI.OpenAsync(UIKeys.EquipEquipPanel, equipBase);
}
}
else
{
ShowBtn(true);
RefreshUI();
}
}
///
/// 成功穿戴|替换装备
///
///
///
private void WearEquipSucess(int eventId, object args)
{
var changEquipIdList = args as HashSet;
if (changEquipIdList != null && _waitEquipIdList?.Count > 0)
{
foreach (var item in changEquipIdList)
{
_waitEquipIdList.Remove(item);
}
}
//RefreshUI();
}
///
/// 装备替换后刷新界面UI
///
private void RefreshUI()
{
OnSortByEquipPower();
ShowDropUI();
}
///
/// 穿戴移除只会发生在身上装备栏空
///
///
///
private void RemoveTemporaryEquip(int eventId, object args)
{
int[] removeIds = args as int[];
if (removeIds.Length > 0)
{
foreach(var id in removeIds)
{
_waitEquipIdList?.Remove(id);
_dropItems = _dropItems.FindAll(item =>(item.count > 0 || item.id != id));
}
}
}
///
/// 成功穿戴|替换装备消息流程结束
///
///
///
private void RefreshTemporaryEquip(int eventId, object args)
{
ShowEquipEquipPanel();
}
///
/// 监听装备穿戴|替换界面关闭事件
///
///
///
private void OnUIClosed(int eventId, object args)
{
var uiKey = args as UIKey;
if (uiKey != null && uiKey == UIKeys.EquipEquipPanel)
{
if (EquipData.Instance.BubblingEquipUid > 0)
{
_waitEquipIdList?.Remove(EquipData.Instance.BubblingEquipUid);
EquipData.Instance.BubblingEquipUid = 0;
ShowEquipEquipPanel();
}
}
}
///
/// 一键出售,拥有高战力装备的二次确认框按钮监听事件
///
///
///
private void ConfirmationToSell()
{
HashSet sellIds = new HashSet();
foreach (var item in _dropItems)
{
if (item.count == 0)
{
sellIds.Add(item.id);
}
}
DragonEggService.Instance.SellEquip(sellIds.ToArray());
sellIds.Clear();
OnClosePanel();
}
///
/// 一键出售临时装备
///
private void SendToSellTemporaryEquip()
{
if (IsHighFighingPower())
{
//XGame.Log.Warn($"当前要出售的装备{_newEquip.FightingPower}战力高于身上穿戴的{_wearEquip.FightingPower}");
ConfirmationBoxParam dialogParam = new ConfirmationBoxParam()
{
cnt = StringDefine.sellHightPowerEquip,
//tipsKey = EDayTipsKey.SellEquip,
onCancelCallback = OnClosePanel,
onPromiseCallback = ConfirmationToSell
};
Context.UI.OpenAsync(UIKeys.CommonConfirmationBox, dialogParam);
return;
}
ConfirmationToSell();
OnClosePanel();
}
///
/// 是否有高战力装备未处理
///
///
private bool IsHighFighingPower()
{
for (int i = 0; i < _dropItems.Count; i++)
{
if (_dropItems[i]?.count == 0)
{
EquipAttr equipData = EquipData.Instance.GetBagEquipItem(_dropItems[i].id);
if (equipData != null)
{
EquipAttr wearEquip = EquipData.Instance.GetWearEquipData(equipData.EquipType);
if ( wearEquip == null || equipData.FightingPower > wearEquip.FightingPower)
{
return true;
}
}
}
}
return false;
}
private void OnSortByEquipPower()
{
_dropItems.Sort((aItem, bItem) =>
{
int aEquipFlag = aItem.kind == 1 ? 1 : 0;
int bEquipFlag = bItem.kind == 1 ? 1 : 0;
if (aEquipFlag == bEquipFlag)
{
if (aEquipFlag == 1)
{
var aEquipData = EquipData.Instance.GetTemporaryEquipItem(aItem.id);
var bEquipData = EquipData.Instance.GetTemporaryEquipItem(bItem.id);
var aHighPower = GetCompareValue(aEquipData);
var bHighPower = GetCompareValue(bEquipData);
if (aHighPower == bHighPower)
{
if (aEquipData.FightingPower == bEquipData.FightingPower)
{
return aEquipData.Id.CompareTo(bEquipData.Id); // 升序
}
return bEquipData.FightingPower.CompareTo(aEquipData.FightingPower); //降序
}
else
return bHighPower - aHighPower;
}
return aItem.count.CompareTo(bItem.count);
}
return bEquipFlag - aEquipFlag;//降序
});
}
private int GetCompareValue(EquipAttr equipData)
{
int compareFlag = 0;
if (equipData?.Table.Part > 0)
{
var equipItem = EquipData.Instance.GetWearEquipData(equipData.Table.Part);
if (equipData.FightingPower > (equipItem?.FightingPower ?? 0) )
compareFlag = 1;
}
return compareFlag;
}
}
}