ItemGetItemPanelCtrl.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /// #pkgName FGUI包名
  2. /// #panelName UIPanel名字
  3. /// #UIName = $"{#pkgName}{#panelName}" UIKey名字
  4. /// 该脚本由模板创建
  5. /// created by cb 2024
  6. using FairyGUI;
  7. using FL.Data;
  8. using FL.Data.Items;
  9. using FL.Network;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using XGame.Database;
  13. using XGame.Framework;
  14. using XGame.Framework.Time;
  15. using XGame.Framework.UI;
  16. namespace FL.FGUI
  17. {
  18. /// <summary>
  19. /// UI逻辑处理类
  20. /// </summary>
  21. /// <typeparam name=""></typeparam>
  22. public partial class ItemGetItemPanelCtrl : UIController<ItemGetItemPanelVM>
  23. {
  24. private ITimer _delayTimer; // 延迟展示高战力装备
  25. private List<long> _waitEquipIdList; // 穿戴|替换等待服务器消息返回的装备id列表
  26. private List<KindItem> _dropItems;
  27. protected override void OnEnable(object intent)
  28. {
  29. AddUIListenres();
  30. AddEventLisenter();
  31. ShowUI(intent as List<KindItem>);
  32. }
  33. protected override void OnDisable()
  34. {
  35. RemoveUIListenres();
  36. RemoveEventLisenter();
  37. ClearTimer();
  38. _waitEquipIdList?.Clear();
  39. _waitEquipIdList = null;
  40. _dropItems?.Clear();
  41. _dropItems = null;
  42. EquipData.Instance.ClearTemporaryEquipList();
  43. }
  44. private void ClearTimer()
  45. {
  46. _delayTimer?.Cancel();
  47. _delayTimer = null;
  48. }
  49. #region UI事件
  50. private void AddUIListenres()
  51. {
  52. VM.OpenBagBtn.onClick.Add(OnClickOpenBagBtn);
  53. }
  54. private void RemoveUIListenres()
  55. {
  56. VM.OpenBagBtn.onClick.Remove(OnClickOpenBagBtn);
  57. }
  58. /// <summary>
  59. /// 一键处理
  60. /// </summary>
  61. /// <param name="context"></param>
  62. private void OnClickOpenBagBtn(EventContext context)
  63. {
  64. SendToSellTemporaryEquip();
  65. }
  66. #endregion
  67. private void AddEventLisenter()
  68. {
  69. Context.AddListener(EventDefine.WearEquipSucess, WearEquipSucess);
  70. Context.AddListener(EventDefine.RemoveTemporaryEquip, RemoveTemporaryEquip);
  71. Context.AddListener(EventDefine.RefreshTemporaryEquip, RefreshTemporaryEquip);
  72. FrameworkEvent.Instance.AddListener(XGame.Framework.EventDefine.UI_CLOSED, OnUIClosed);
  73. }
  74. private void RemoveEventLisenter()
  75. {
  76. Context.RemoveListener(EventDefine.WearEquipSucess, WearEquipSucess);
  77. Context.RemoveListener(EventDefine.RemoveTemporaryEquip, RemoveTemporaryEquip);
  78. Context.RemoveListener(EventDefine.RefreshTemporaryEquip, RefreshTemporaryEquip);
  79. FrameworkEvent.Instance.RemoveListener(XGame.Framework.EventDefine.UI_CLOSED, OnUIClosed);
  80. }
  81. private void OnClosePanel()
  82. {
  83. Context.ClosePanel();
  84. }
  85. private void ShowUI(List<KindItem> dropItems)
  86. {
  87. if (dropItems == null) return;
  88. _dropItems = dropItems;
  89. ShowBtn(false);
  90. if (IsShowOneKeyBtn())
  91. {
  92. ShowEquipUI();
  93. }
  94. else
  95. ShowDropUI();
  96. }
  97. private void ShowDropUI()
  98. {
  99. //XGame.Log.Info($"道具的数量:{_dropItems.Count}");
  100. bool bLess = _dropItems.Count < 6; // 单行
  101. VM.ItemList.visible = !bLess;
  102. VM.LessItemList.visible = bLess;
  103. if (bLess)
  104. {
  105. VM.LessItemList.BindDatas(_dropItems);
  106. }
  107. else
  108. {
  109. VM.ItemList.BindDatas(_dropItems);
  110. }
  111. VM.EmptyComCtrl.selectedIndex = bLess ? 0 : (_dropItems.Count > 15 ? 2 : 1);
  112. VM.ItemList.scrollPane.bouncebackEffect = _dropItems.Count > 15 ? true : false;
  113. }
  114. /// <summary>
  115. /// 打开恭喜获得界面是否需要显示一键处理按钮
  116. /// </summary>
  117. /// <returns></returns>
  118. private bool IsShowOneKeyBtn()
  119. {
  120. bool bShowBtn = false;
  121. if (_dropItems.Count > 0)
  122. {
  123. for (int i = 0; i < _dropItems.Count; i++)
  124. {
  125. if (_dropItems[i].kind == 1)
  126. {
  127. return true;
  128. }
  129. }
  130. }
  131. return bShowBtn;
  132. }
  133. private void ShowBtn(bool bShow)
  134. {
  135. VM.OpenBagBtn.visible = bShow;
  136. }
  137. private void ShowEquipUI()
  138. {
  139. // 装备类型
  140. List<KindItem> equipItemList = _dropItems.FindAll(item => item.kind == 1);
  141. var highEquipMap = new Dictionary<EEquipType, EquipAttr>();
  142. foreach (var item in equipItemList)
  143. {
  144. var equipItem = EquipData.Instance.GetTemporaryEquipItem(item.id);
  145. if (equipItem != null && IsHighEquip(equipItem))
  146. {
  147. if (highEquipMap.ContainsKey(equipItem.EquipType))
  148. {
  149. if (highEquipMap[equipItem.EquipType].FightingPower < equipItem.FightingPower)
  150. {
  151. highEquipMap[equipItem.EquipType] = equipItem;
  152. }
  153. }
  154. else
  155. {
  156. highEquipMap.Add(equipItem.EquipType, equipItem);
  157. }
  158. }
  159. }
  160. RefreshUI();
  161. if (highEquipMap.Count > 0)
  162. {
  163. DelayToShowHighEquip(highEquipMap);
  164. }
  165. else
  166. {
  167. ShowBtn(true);
  168. }
  169. }
  170. /// <summary>
  171. /// 等待掉落道具显示后再冒泡展示高战力装备替换界面
  172. /// </summary>
  173. /// <param name="highEquipMap"></param>
  174. private void DelayToShowHighEquip(Dictionary<EEquipType, EquipAttr> highEquipMap)
  175. {
  176. ClearTimer();
  177. _delayTimer = Context.Time.AddDelayTimer(1000, () =>
  178. {
  179. ShowHighEquipUI(highEquipMap);
  180. });
  181. }
  182. /// <summary>
  183. /// 是否高战力装备
  184. /// </summary>
  185. /// <param name="equipItem"></param>
  186. /// <returns></returns>
  187. private bool IsHighEquip(EquipAttr equipItem)
  188. {
  189. EquipAttr wearEquip = EquipData.Instance.GetWearEquipData(equipItem.Table.Part);
  190. if (wearEquip == null) return true;
  191. return equipItem.FightingPower > wearEquip.FightingPower;
  192. }
  193. private void ShowHighEquipUI(Dictionary<EEquipType, EquipAttr> highEquipMap)
  194. {
  195. if (highEquipMap?.Count == 0)
  196. {
  197. ShowBtn(true);
  198. return;
  199. }
  200. if (_waitEquipIdList?.Count > 0)
  201. _waitEquipIdList.Clear();
  202. else
  203. _waitEquipIdList = new List<long>();
  204. foreach (var item in highEquipMap)
  205. {
  206. _waitEquipIdList.Add(item.Value.Id);
  207. }
  208. highEquipMap.Clear();
  209. ShowEquipEquipPanel();
  210. }
  211. /// <summary>
  212. /// 显示装备穿戴|替换界面
  213. /// </summary>
  214. private void ShowEquipEquipPanel()
  215. {
  216. if (_waitEquipIdList?.Count > 0)
  217. {
  218. EquipData.Instance.BubblingEquipUid = _waitEquipIdList[0];
  219. EquipAttr equipData = EquipData.Instance.GetTemporaryEquipItem(_waitEquipIdList[0]);
  220. if (equipData != null)
  221. {
  222. var equipBase = new EquipItemBase();
  223. equipBase.InitById(equipData.Id);
  224. Context.UI.OpenAsync(UIKeys.EquipEquipPanel, equipBase);
  225. }
  226. }
  227. else
  228. {
  229. ShowBtn(true);
  230. RefreshUI();
  231. }
  232. }
  233. /// <summary>
  234. /// 成功穿戴|替换装备
  235. /// </summary>
  236. /// <param name="eventId"></param>
  237. /// <param name="args"></param>
  238. private void WearEquipSucess(int eventId, object args)
  239. {
  240. var changEquipIdList = args as HashSet<long>;
  241. if (changEquipIdList != null && _waitEquipIdList?.Count > 0)
  242. {
  243. foreach (var item in changEquipIdList)
  244. {
  245. _waitEquipIdList.Remove(item);
  246. }
  247. }
  248. //RefreshUI();
  249. }
  250. /// <summary>
  251. /// 装备替换后刷新界面UI
  252. /// </summary>
  253. private void RefreshUI()
  254. {
  255. OnSortByEquipPower();
  256. ShowDropUI();
  257. }
  258. /// <summary>
  259. /// 穿戴移除只会发生在身上装备栏空
  260. /// </summary>
  261. /// <param name="eventId"></param>
  262. /// <param name="args"></param>
  263. private void RemoveTemporaryEquip(int eventId, object args)
  264. {
  265. int[] removeIds = args as int[];
  266. if (removeIds.Length > 0)
  267. {
  268. foreach(var id in removeIds)
  269. {
  270. _waitEquipIdList?.Remove(id);
  271. _dropItems = _dropItems.FindAll(item =>(item.count > 0 || item.id != id));
  272. }
  273. }
  274. }
  275. /// <summary>
  276. /// 成功穿戴|替换装备消息流程结束
  277. /// </summary>
  278. /// <param name="eventId"></param>
  279. /// <param name="args"></param>
  280. private void RefreshTemporaryEquip(int eventId, object args)
  281. {
  282. ShowEquipEquipPanel();
  283. }
  284. /// <summary>
  285. /// 监听装备穿戴|替换界面关闭事件
  286. /// </summary>
  287. /// <param name="eventId"></param>
  288. /// <param name="args"></param>
  289. private void OnUIClosed(int eventId, object args)
  290. {
  291. var uiKey = args as UIKey;
  292. if (uiKey != null && uiKey == UIKeys.EquipEquipPanel)
  293. {
  294. if (EquipData.Instance.BubblingEquipUid > 0)
  295. {
  296. _waitEquipIdList?.Remove(EquipData.Instance.BubblingEquipUid);
  297. EquipData.Instance.BubblingEquipUid = 0;
  298. ShowEquipEquipPanel();
  299. }
  300. }
  301. }
  302. /// <summary>
  303. /// 一键出售,拥有高战力装备的二次确认框按钮监听事件
  304. /// </summary>
  305. /// <param name="eventId"></param>
  306. /// <param name="args"></param>
  307. private void ConfirmationToSell()
  308. {
  309. HashSet<int> sellIds = new HashSet<int>();
  310. foreach (var item in _dropItems)
  311. {
  312. if (item.count == 0)
  313. {
  314. sellIds.Add(item.id);
  315. }
  316. }
  317. DragonEggService.Instance.SellEquip(sellIds.ToArray());
  318. sellIds.Clear();
  319. OnClosePanel();
  320. }
  321. /// <summary>
  322. /// 一键出售临时装备
  323. /// </summary>
  324. private void SendToSellTemporaryEquip()
  325. {
  326. if (IsHighFighingPower())
  327. {
  328. //XGame.Log.Warn($"当前要出售的装备{_newEquip.FightingPower}战力高于身上穿戴的{_wearEquip.FightingPower}");
  329. ConfirmationBoxParam dialogParam = new ConfirmationBoxParam()
  330. {
  331. cnt = StringDefine.sellHightPowerEquip,
  332. //tipsKey = EDayTipsKey.SellEquip,
  333. onCancelCallback = OnClosePanel,
  334. onPromiseCallback = ConfirmationToSell
  335. };
  336. Context.UI.OpenAsync(UIKeys.CommonConfirmationBox, dialogParam);
  337. return;
  338. }
  339. ConfirmationToSell();
  340. OnClosePanel();
  341. }
  342. /// <summary>
  343. /// 是否有高战力装备未处理
  344. /// </summary>
  345. /// <returns></returns>
  346. private bool IsHighFighingPower()
  347. {
  348. for (int i = 0; i < _dropItems.Count; i++)
  349. {
  350. if (_dropItems[i]?.count == 0)
  351. {
  352. EquipAttr equipData = EquipData.Instance.GetBagEquipItem(_dropItems[i].id);
  353. if (equipData != null)
  354. {
  355. EquipAttr wearEquip = EquipData.Instance.GetWearEquipData(equipData.EquipType);
  356. if ( wearEquip == null || equipData.FightingPower > wearEquip.FightingPower)
  357. {
  358. return true;
  359. }
  360. }
  361. }
  362. }
  363. return false;
  364. }
  365. private void OnSortByEquipPower()
  366. {
  367. _dropItems.Sort((aItem, bItem) =>
  368. {
  369. int aEquipFlag = aItem.kind == 1 ? 1 : 0;
  370. int bEquipFlag = bItem.kind == 1 ? 1 : 0;
  371. if (aEquipFlag == bEquipFlag)
  372. {
  373. if (aEquipFlag == 1)
  374. {
  375. var aEquipData = EquipData.Instance.GetTemporaryEquipItem(aItem.id);
  376. var bEquipData = EquipData.Instance.GetTemporaryEquipItem(bItem.id);
  377. var aHighPower = GetCompareValue(aEquipData);
  378. var bHighPower = GetCompareValue(bEquipData);
  379. if (aHighPower == bHighPower)
  380. {
  381. if (aEquipData.FightingPower == bEquipData.FightingPower)
  382. {
  383. return aEquipData.Id.CompareTo(bEquipData.Id); // 升序
  384. }
  385. return bEquipData.FightingPower.CompareTo(aEquipData.FightingPower); //降序
  386. }
  387. else
  388. return bHighPower - aHighPower;
  389. }
  390. return aItem.count.CompareTo(bItem.count);
  391. }
  392. return bEquipFlag - aEquipFlag;//降序
  393. });
  394. }
  395. private int GetCompareValue(EquipAttr equipData)
  396. {
  397. int compareFlag = 0;
  398. if (equipData?.Table.Part > 0)
  399. {
  400. var equipItem = EquipData.Instance.GetWearEquipData(equipData.Table.Part);
  401. if (equipData.FightingPower > (equipItem?.FightingPower ?? 0) )
  402. compareFlag = 1;
  403. }
  404. return compareFlag;
  405. }
  406. }
  407. }