using System; using System.Collections.Generic; using Cute; using UnityEngine; using Wizard; public class CardMake : MonoBehaviour { public const int CAN_CREATE_MAX = 3; public const string FORMAT_CARD_CRAFT_PARAM = "{0},{1}"; private IDictionary DestructDict; private IDictionary _craftDict; public Action OnCardSell; public Action OnCardSellId; public Action OnCardBuy; public Action OnClose; private Action _onFinishCardDestruct; public void StartCardDestruct(int cardId) { if (DestructDict == null) { DestructDict = new Dictionary(); } else { DestructDict.Clear(); } int possessionCardNum = GameMgr.GetIns().GetDataMgr().GetPossessionCardNum(cardId, isIncludingSpotCard: false); DestructDict = new Dictionary(); DestructDict.Add(cardId.ToString(), "1," + possessionCardNum); DestructCard(); } private void DestructCard() { CardDestructTask cardDestructTask = GameMgr.GetIns().GetCardDestructTask(); cardDestructTask.SetParameter(DestructDict); StartCoroutine(Toolbox.NetworkManager.Connect(cardDestructTask, OnRequestFinishDestruct, OnError, OnError)); } private void OnRequestFinishDestruct(NetworkTask.ResultCode error) { TriggerUpdateUserDeck(); if (_onFinishCardDestruct != null) { _onFinishCardDestruct(); } } private void TriggerUpdateUserDeck() { List list = new List(DestructDict.Keys); for (int i = 0; i < list.Count; i++) { int obj = int.Parse(list[i]); if (OnCardSellId != null) { OnCardSellId(obj); } } if (OnCardSell != null) { OnCardSell(); } if (OnClose != null) { OnClose(); } list = null; } public void StartCardCraft(int cardId) { if (_craftDict == null) { _craftDict = new Dictionary(); } else { _craftDict.Clear(); } int possessionCardNum = GameMgr.GetIns().GetDataMgr().GetPossessionCardNum(cardId, isIncludingSpotCard: false); _craftDict.Add(cardId.ToString(), $"{1},{possessionCardNum}"); CraftCard(); } private void CraftCard() { CardCreateTask cardCreateTask = GameMgr.GetIns().GetCardCreateTask(); cardCreateTask.SetParameter(_craftDict); StartCoroutine(Toolbox.NetworkManager.Connect(cardCreateTask, OnRequestFinishCraft, OnError, OnError)); } private void OnRequestFinishCraft(NetworkTask.ResultCode error) { if (OnCardBuy != null) { OnCardBuy(); } if (OnClose != null) { OnClose(); } } public void StartDestructAll(IDictionary destructDict, Action onFinishCallBack = null) { _onFinishCardDestruct = onFinishCallBack; DestructDict = destructDict; if (DestructDict.Count > 0) { DestructCard(); } } public void StartDestructAll(IDictionary destructDict, Action callback = null) { Dictionary dictionary = new Dictionary(); foreach (KeyValuePair item in destructDict) { string value = CreateRequestParam(item.Key, item.Value); dictionary.Add(item.Key.ToString(), value); } StartDestructAll(dictionary, callback); } public void StartCraftAll(IDictionary craftDict) { if (_craftDict == null) { _craftDict = new Dictionary(); } else { _craftDict.Clear(); } foreach (KeyValuePair item in craftDict) { if (item.Value > 0) { string value = CreateRequestParam(item.Key, item.Value); _craftDict.Add(item.Key.ToString(), value); } } if (_craftDict.Count > 0) { CraftCard(); } } private string CreateRequestParam(int cardId, int num) { int possessionCardNum = GameMgr.GetIns().GetDataMgr().GetPossessionCardNum(cardId, isIncludingSpotCard: false); return $"{num},{possessionCardNum}"; } private void OnError(NetworkTask.ResultCode code) { OnClose.Call(); } private void OnError(int code) { OnClose.Call(); } }