using Snxxz.UI; using System; using System.Collections.Generic; using System.Linq; using TableConfig; using UnityEngine; using UnityEngine.UI; namespace Snxxz.UI { public class ComposeTicketWin : Window { [SerializeField] ScrollerController cellCtrl; [SerializeField] GameObject containCompose; [SerializeField] GameObject containUnCompose; [SerializeField] GameObject containOneMat; [SerializeField] GameObject containTwoMat; [SerializeField] Text successRateText; [SerializeField] Text _moneyCountText; [SerializeField] GameObject costMoneyObj; [SerializeField] Button composeBtn; [SerializeField] Button allComposeBtn; [SerializeField] UIEffect twoMatComposeEffect; [SerializeField] UIEffect oneMatComposeEffect; [Header("一个固定物品和非固定物品")] [SerializeField] List oneMatFixedAndUnfixeds = new List(); [Header("非一个固定物品和非固定物品")] [SerializeField] List twoMatFixedAndUnfixeds = new List(); [Header("合成物品")] [SerializeField] List makeItems = new List(); ComposeWinModel _composeWinModel; ComposeWinModel composeWinModel { get { return _composeWinModel ?? (_composeWinModel = ModelCenter.Instance.GetModel()); } } PlayerPackModel _playerPack; PlayerPackModel playerPack { get { return _playerPack ?? (_playerPack = ModelCenter.Instance.GetModel()); } } PackModelInterface _modelInterface; PackModelInterface modelInterface { get { return _modelInterface ?? (_modelInterface = ModelCenter.Instance.GetModel()); } } ComposeWinModel.ComposeFirstTypeData firstTypeData; ComposeWinModel.ComposeSecondTypeData secondTypeData; ComposeWinModel.ComposeThirdTypeData thirdTypeData; private int curSecondType = 0; private int curThirdType = 0; private int preSecondType = 0; [SerializeField] int initSuccessRate = 10000; ItemCompoundConfig compoundModel = null; private Dictionary> fixedItemIndexDict = new Dictionary>(); AchievementGuideEffect guidEffect = null; protected override void BindController() { cellCtrl.OnRefreshCell += RefreshTypeCell; cellCtrl.vertical = true; cellCtrl.lockType = EnhanceLockType.KeepVertical; composeWinModel.ResetModelEvent += ResetModel; } protected override void AddListeners() { composeBtn.AddListener(OnClickComposeBtn); allComposeBtn.AddListener(ClickAllComposeBtn); } protected override void OnPreOpen() { DTCA814_tagMCMakeItemAnswer.MakeItemAnswerEvent += OnComposeAnswer; composeWinModel.UpdateSecondTypeEvent += UpdateSecondType; composeWinModel.UpdateThirdTypeEvent += UpdateThirdType; Display(); } protected override void OnAfterOpen() { cellCtrl.vertical = true; this.transform.SetAsLastSibling(); } protected override void OnPreClose() { composeWinModel.UpdateSecondTypeEvent -= UpdateSecondType; composeWinModel.UpdateThirdTypeEvent -= UpdateThirdType; DTCA814_tagMCMakeItemAnswer.MakeItemAnswerEvent -= OnComposeAnswer; } protected override void OnAfterClose() { } #region 新的逻辑 private void Display() { composeWinModel.TryGetFirstTypeData((int)ComposeFuncType.Ticket, out firstTypeData); if (firstTypeData == null) return; DefaultSelect(); CreateTypeCell(); JumpIndex(); } private void DefaultSelect() { bool isJumpTo = false; var achieveId = AchievementGoto.guideAchievementId; SuccessConfig successConfig = Config.Instance.Get(achieveId); if(successConfig != null && successConfig.Type == 69) { isJumpTo = true; curSecondType = composeWinModel.secondType; curThirdType = composeWinModel.thirdType; composeWinModel.ResetJumpToModel(); } else { if (composeWinModel.secondType != 0) { isJumpTo = true; curSecondType = composeWinModel.secondType; curThirdType = composeWinModel.thirdType; composeWinModel.ResetJumpToModel(); } } composeWinModel.TryGetThirdTypeData((int)ComposeFuncType.Ticket, curSecondType, curThirdType, out thirdTypeData); if (thirdTypeData != null) { RefreshUI(thirdTypeData.itemCompound); if (isJumpTo) { isJumpTo = false; bool isCanCompose = true; if (!composeWinModel.IsEnoughFixedMat(thirdTypeData.itemCompound) || !composeWinModel.IsEnoughUnfixedMat(thirdTypeData.itemCompound)) { MessageWin.Inst.ShowFixedTip(Language.Get("Compose101")); isCanCompose = false; } else if (!composeWinModel.IsEnoughMoney(thirdTypeData.itemCompound)) { MessageWin.Inst.ShowFixedTip(Language.Get("Z1011")); isCanCompose = false; } if (isCanCompose) { guidEffect = AchievementGuideEffectPool.Require(1); guidEffect.transform.SetParentEx(composeBtn.transform, Vector3.zero, Quaternion.identity, Vector3.one); } } } else { RefreshUI(null); } } private void CreateTypeCell() { if (firstTypeData == null) return; cellCtrl.Refresh(); foreach (var second in firstTypeData.secondTypeDict.Keys) { cellCtrl.AddCell(ScrollerDataType.Header, second); if (second == curSecondType) { var thirdTypeDict = firstTypeData.secondTypeDict[second].thirdTypeDict; foreach (var third in thirdTypeDict.Keys) { if (third != 0) { cellCtrl.AddCell(ScrollerDataType.Normal, third); } } } } cellCtrl.Restart(); composeWinModel.TryGetThirdTypeData((int)ComposeFuncType.Ticket, curSecondType, curThirdType, out thirdTypeData); if (thirdTypeData != null) { var fixItemIds = thirdTypeData.itemCompound.itemID; if (fixItemIds != null) { if (fixItemIds.Length < 2) { UpdateUIShow(true, false, true, false); } else { UpdateUIShow(true, false, false, true); } } RefreshUI(thirdTypeData.itemCompound); } else { RefreshUI(null); UpdateUIShow(false,true, false,false); } } private void UpdateUIShow(params bool[] isShows) { containCompose.SetActive(isShows[0]); containUnCompose.SetActive(isShows[1]); containOneMat.SetActive(isShows[2]); containTwoMat.SetActive(isShows[3]); } private void JumpIndex() { if (firstTypeData == null) return; var secondTypeDict = firstTypeData.secondTypeDict; if (secondTypeDict.Count >= 1 && curSecondType != 0) { cellCtrl.JumpIndex(GetCurIndex(curSecondType)); } } private int GetCurIndex(int secondType) { if (firstTypeData == null) return 0; int curIndex = 0; var secondTypeDict = firstTypeData.secondTypeDict; List secondlist = secondTypeDict.Keys.ToList(); for (int i = 0; i < secondlist.Count; i++) { if (secondType == secondlist[i]) { curIndex = i; break; } } if (curThirdType >= 4) { curIndex = curIndex + curThirdType - 2; } return curIndex; } private void RefreshTypeCell(ScrollerDataType type, CellView cell) { switch (type) { case ScrollerDataType.Header: ComposeWinModel.ComposeSecondTypeData _secondTypeData = null; ComposeFirstTypeCell firstTypeCell = cell.GetComponent(); composeWinModel.TryGetSecondTypeData((int)ComposeFuncType.Ticket, cell.index, out _secondTypeData); if (_secondTypeData != null) { var thirdTypeDict = _secondTypeData.thirdTypeDict; foreach (var third in thirdTypeDict.Keys) { firstTypeCell.SetDisplay((int)ComposeFuncType.Ticket, cell.index, third, curSecondType); break; } } break; case ScrollerDataType.Normal: ComposeSecondTypeCell secondTypeCell = cell.GetComponent(); secondTypeCell.SetDisplay((int)ComposeFuncType.Ticket, curSecondType, cell.index, curThirdType); break; } } private void UpdateSecondType(int secondType) { curThirdType = 0; curSecondType = secondType; composeWinModel.TryGetSecondTypeData((int)ComposeFuncType.Ticket, secondType, out secondTypeData); OnClickSecondType(); CreateTypeCell(); JumpIndex(); } private void OnClickSecondType() { if (secondTypeData == null) return; var thirdTypeDict = secondTypeData.thirdTypeDict; if (curThirdType == 0) { foreach (var thirdType in thirdTypeDict.Keys) { if (thirdType != 0) { if (preSecondType == curSecondType) { curSecondType = 0; preSecondType = 0; } else { curThirdType = thirdType; } } break; } } if (preSecondType != curSecondType) { preSecondType = curSecondType; if (guidEffect != null) { AchievementGuideEffectPool.Recycle(guidEffect); guidEffect = null; } } } private void UpdateThirdType(int thirdType) { composeWinModel.TryGetThirdTypeData((int)ComposeFuncType.Ticket, curSecondType, thirdType, out thirdTypeData); curThirdType = thirdType; CreateTypeCell(); } private void UpdateComposeMat() { int[] makeIds = compoundModel.makeID; for (int i = 0; i < makeItems.Count; i++) { var matCell = makeItems[i]; if(makeIds != null && i < makeIds.Length) { matCell.SetDisplay(compoundModel, NeedMatType.MakeItem,false,"",makeIds[i]); } else { matCell.SetDisplay(compoundModel, NeedMatType.MakeItem,true); } } int[] fixedIds = compoundModel.itemID; int[] fixedDisplays = compoundModel.itemDisplay; int[] unfixedIds = compoundModel.unfixedItemID; int[] unfixedDisplays = compoundModel.unfixedItemDisplay; if(fixedIds != null && fixedIds.Length < 2) { for (int i = 0; i < oneMatFixedAndUnfixeds.Count; i++) { var matCell = oneMatFixedAndUnfixeds[i]; matCell.SetDisplay(compoundModel, NeedMatType.Nothing, true); } for (int i = 0; i < fixedDisplays.Length; i++) { var fixedDisplay = fixedDisplays[i]; if (fixedDisplay != 0) { var matCell = oneMatFixedAndUnfixeds[fixedDisplay - 1]; int fixedId = 0; composeWinModel.TryGetIdByDisplay(NeedMatType.fixedItem, compoundModel, fixedDisplay, out fixedId); matCell.SetDisplay(compoundModel, NeedMatType.fixedItem, false, "", fixedId); } } for (int i = 0; i < unfixedDisplays.Length; i++) { var unfixedDisplay = unfixedDisplays[i]; if (unfixedDisplay != 0) { var matCell = oneMatFixedAndUnfixeds[unfixedDisplay - 1]; matCell.SetDisplay(compoundModel, NeedMatType.unfixedItem, false); } } } else if(fixedIds != null) { for (int i = 0; i < twoMatFixedAndUnfixeds.Count; i++) { var matCell = twoMatFixedAndUnfixeds[i]; matCell.SetDisplay(compoundModel, NeedMatType.Nothing, true); } for (int i = 0; i < fixedDisplays.Length; i++) { var fixedDisplay = fixedDisplays[i]; if (fixedDisplay != 0) { var matCell = twoMatFixedAndUnfixeds[fixedDisplay - 1]; int fixedId = 0; composeWinModel.TryGetIdByDisplay(NeedMatType.fixedItem, compoundModel, fixedDisplay, out fixedId); matCell.SetDisplay(compoundModel, NeedMatType.fixedItem, false, "", fixedId); } } for (int i = 0; i < unfixedDisplays.Length; i++) { var unfixedDisplay = unfixedDisplays[i]; if (unfixedDisplay != 0) { var matCell = twoMatFixedAndUnfixeds[unfixedDisplay - 1]; matCell.SetDisplay(compoundModel, NeedMatType.unfixedItem, false); } } } _moneyCountText.text = modelInterface.OnChangeCoinsUnit((ulong)compoundModel.money); } private UIEffect GetPlayMatEffect() { if (compoundModel == null) return null; int[] fixedIds = compoundModel.itemID; if(fixedIds != null) { if(fixedIds.Length < 2) { return oneMatComposeEffect; } else { return twoMatComposeEffect; } } return null; } #endregion private void ResetModel() { curSecondType = 0; curThirdType = 0; compoundModel = null; if (!WindowJumpMgr.Instance.IsJumpState) { preSecondType = 0; } } private void RefreshUI(ItemCompoundConfig compoundModel) { this.compoundModel = compoundModel; if (compoundModel == null) return; successRateText.text = Language.Get("HallowsWin_Success", StringUtility.Contact(compoundModel.successRate / 100, "%")); composeWinModel.SetCurComposeModel(compoundModel); UpdateComposeMat(); } private void ClickAllComposeBtn() { fixedItemIndexDict.Clear(); if (compoundModel == null) return; bool isTrailer = composeWinModel.IsTrailerByLevel(compoundModel); if (isTrailer) { MessageWin.Inst.ShowFixedTip(Language.Get("FuncOpenLv", compoundModel.levelNeed)); return; } List composeCountlist = new List(); SinglePackModel singlePack = playerPack.GetSinglePackModel(PackType.rptItem); int[] fixedIds = compoundModel.itemID; int[] fixedCounts = compoundModel.itemCount; for (int i = 0; i < fixedIds.Length; i++) { int haveCount = playerPack.GetItemCountByID(PackType.rptItem, fixedIds[i]); int canComposeCount = haveCount / fixedCounts[i]; composeCountlist.Add(canComposeCount); List itemIndexlist = null; if (singlePack != null) { itemIndexlist = singlePack.ItemIndexlist(fixedIds[i], fixedCounts[i] * canComposeCount); } fixedItemIndexDict.Add(fixedIds[i], itemIndexlist); } composeCountlist.Sort(); int composeCnt = 0; for (int i = composeCountlist[0]; i > -1; i--) { ulong needMoney = (ulong)(i * compoundModel.money); if (UIHelper.GetMoneyCnt(3) >= needMoney) { composeCnt = i; break; } } composeWinModel.SendComposeRequest(compoundModel, fixedItemIndexDict,GetPlayMatEffect(), composeCnt); } private void OnClickComposeBtn() { if (compoundModel == null) return; bool isTrailer = composeWinModel.IsTrailerByLevel(compoundModel); if (isTrailer) { MessageWin.Inst.ShowFixedTip(Language.Get("FuncOpenLv", compoundModel.levelNeed)); return; } SetFixedItemIndexDic(); composeWinModel.SendComposeRequest(compoundModel, fixedItemIndexDict,GetPlayMatEffect(),1); } private void SetFixedItemIndexDic() { fixedItemIndexDict.Clear(); SinglePackModel singlePack = playerPack.GetSinglePackModel(PackType.rptItem); if (singlePack == null || compoundModel == null) return; int minBindCnt = 0; int minNoBindCnt = 0; composeWinModel.GetBindOrNoBindMinCnt(out minBindCnt,out minNoBindCnt); int[] fixedIds = compoundModel.itemID; int[] fixedCounts = compoundModel.itemCount; for (int i = 0; i < fixedIds.Length; i++) { List itemIndexlist = null; if (minBindCnt >= fixedCounts[i]) { itemIndexlist = singlePack.ItemIndexlistByIsBind(fixedIds[i],fixedCounts[i], 1); } else if(minNoBindCnt >= fixedCounts[i]) { itemIndexlist = singlePack.ItemIndexlistByIsBind(fixedIds[i],fixedCounts[i],0); } else { itemIndexlist = singlePack.ItemIndexlist(fixedIds[i],fixedCounts[i]); } if (itemIndexlist != null) { fixedItemIndexDict.Add(fixedIds[i], itemIndexlist); } } } private void OnComposeAnswer(HA814_tagMCMakeItemAnswer answer) { if (answer.MakeType != (int)MakeType.Def_mitItemCompound) return; if (answer.Result == 1) { MessageWin.Inst.ShowFixedTip(Language.Get("Compose102")); } else { MessageWin.Inst.ShowFixedTip(Language.Get("Compose103")); } RefreshUI(compoundModel); } } }