123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 |
- /// #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
- {
- /// <summary>
- /// UI逻辑处理类
- /// </summary>
- /// <typeparam name=""></typeparam>
- public partial class ItemGetItemPanelCtrl : UIController<ItemGetItemPanelVM>
- {
- private ITimer _delayTimer; // 延迟展示高战力装备
- private List<long> _waitEquipIdList; // 穿戴|替换等待服务器消息返回的装备id列表
- private List<KindItem> _dropItems;
- protected override void OnEnable(object intent)
- {
- AddUIListenres();
- AddEventLisenter();
- ShowUI(intent as List<KindItem>);
- }
- 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);
- }
- /// <summary>
- /// 一键处理
- /// </summary>
- /// <param name="context"></param>
- 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<KindItem> 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;
- }
- /// <summary>
- /// 打开恭喜获得界面是否需要显示一键处理按钮
- /// </summary>
- /// <returns></returns>
- 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<KindItem> equipItemList = _dropItems.FindAll(item => item.kind == 1);
- var highEquipMap = new Dictionary<EEquipType, EquipAttr>();
- 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);
- }
- }
- /// <summary>
- /// 等待掉落道具显示后再冒泡展示高战力装备替换界面
- /// </summary>
- /// <param name="highEquipMap"></param>
- private void DelayToShowHighEquip(Dictionary<EEquipType, EquipAttr> highEquipMap)
- {
- ClearTimer();
- _delayTimer = Context.Time.AddDelayTimer(1000, () =>
- {
- ShowHighEquipUI(highEquipMap);
- });
- }
- /// <summary>
- /// 是否高战力装备
- /// </summary>
- /// <param name="equipItem"></param>
- /// <returns></returns>
- 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<EEquipType, EquipAttr> highEquipMap)
- {
- if (highEquipMap?.Count == 0)
- {
- ShowBtn(true);
- return;
- }
- if (_waitEquipIdList?.Count > 0)
- _waitEquipIdList.Clear();
- else
- _waitEquipIdList = new List<long>();
- foreach (var item in highEquipMap)
- {
- _waitEquipIdList.Add(item.Value.Id);
- }
- highEquipMap.Clear();
- ShowEquipEquipPanel();
- }
- /// <summary>
- /// 显示装备穿戴|替换界面
- /// </summary>
- 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();
- }
- }
- /// <summary>
- /// 成功穿戴|替换装备
- /// </summary>
- /// <param name="eventId"></param>
- /// <param name="args"></param>
- private void WearEquipSucess(int eventId, object args)
- {
- var changEquipIdList = args as HashSet<long>;
- if (changEquipIdList != null && _waitEquipIdList?.Count > 0)
- {
- foreach (var item in changEquipIdList)
- {
- _waitEquipIdList.Remove(item);
- }
- }
- //RefreshUI();
- }
- /// <summary>
- /// 装备替换后刷新界面UI
- /// </summary>
- private void RefreshUI()
- {
- OnSortByEquipPower();
- ShowDropUI();
- }
- /// <summary>
- /// 穿戴移除只会发生在身上装备栏空
- /// </summary>
- /// <param name="eventId"></param>
- /// <param name="args"></param>
- 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));
- }
- }
- }
- /// <summary>
- /// 成功穿戴|替换装备消息流程结束
- /// </summary>
- /// <param name="eventId"></param>
- /// <param name="args"></param>
- private void RefreshTemporaryEquip(int eventId, object args)
- {
- ShowEquipEquipPanel();
- }
- /// <summary>
- /// 监听装备穿戴|替换界面关闭事件
- /// </summary>
- /// <param name="eventId"></param>
- /// <param name="args"></param>
- 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();
- }
- }
- }
- /// <summary>
- /// 一键出售,拥有高战力装备的二次确认框按钮监听事件
- /// </summary>
- /// <param name="eventId"></param>
- /// <param name="args"></param>
- private void ConfirmationToSell()
- {
- HashSet<int> sellIds = new HashSet<int>();
- foreach (var item in _dropItems)
- {
- if (item.count == 0)
- {
- sellIds.Add(item.id);
- }
- }
- DragonEggService.Instance.SellEquip(sellIds.ToArray());
- sellIds.Clear();
- OnClosePanel();
- }
- /// <summary>
- /// 一键出售临时装备
- /// </summary>
- 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();
- }
- /// <summary>
- /// 是否有高战力装备未处理
- /// </summary>
- /// <returns></returns>
- 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;
- }
- }
- }
|