using ET;
using FairyGUI;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using cfg;
using cfg.GfgCfg;
using UI.CommonGame;
using UnityEngine;
namespace GFGGame
{
public static class ItemUtil
{
/// 是否弹购买成功飘字,默认是
/// 是否打开来源界面。默认否
/// 是否显示购买提示
public static void ExchangeItemById(int itemId, long num, bool showTips = true, Action onSuccess = null,
bool openSource = false, int maxCount = 9990, bool showTxtBuyTips = false, string prefix = "")
{
if (!BuyCurrency(itemId, num, onSuccess))
{
BuyItemConteoller.Show(itemId, num, ConstBuyType.TYPE_ITEM, onSuccess, showTips, openSource, maxCount);
BuyItemConteoller.showTxtBuyTips = showTxtBuyTips;
}
}
public static bool BuyCurrency(int itemId, long num, Action onSuccess = null)
{
switch (itemId)
{
case ConstItemID.GOLD:
AddGold(onSuccess);
return true;
case ConstItemID.POWER:
AddPower();
return true;
case ConstItemID.DIAMOND_RED:
AddDiamondRed(num);
return true;
case ConstItemID.DIAMOND_PURPLE:
ItemUtil.AddDiamondPurple();
return true;
}
return false;
}
public static void AddPower(Action onSuccess = null, int type = 0)
{
EnduringGiftBoxController.Show(ConstItemID.POWER, 1, () =>
{
if (onSuccess != null)
{
onSuccess();
}
}, "", type);
}
public static void AddGold(Action onSuccess = null, int type = 0)
{
EnduringGiftBoxController.Show(ConstItemID.GOLD, 1, () =>
{
if (onSuccess != null)
{
onSuccess();
}
}, "", type);
}
public static void AddDiamondPurple()
{
//ViewManager.Show();
ViewManager.Show(new object[] { ConstStoreTabId.STORE_CHARGE, ConstStoreSubId.STORE_CHARGE });
EventAgent.DispatchEvent(ConstMessage.CLOSE_SHOPPING_TIP);
}
//红钻(鲛绡)
public static void AddDiamondRed(long value = 0, Action onSuccess = null)
{
ItemExchangeCfg
currencyRatioCfg = CommonDataManager.Tables.TblItemExchangeCfg.GetOrDefault(ConstItemID.DIAMOND_RED);
BuyItemConteoller.Show(currencyRatioCfg.Id, value > 0 ? value : 1, ConstBuyType.TYPE_ITEM, onSuccess, true,
true);
BuyItemConteoller.showTxtBuyTips = true;
}
public static string GetItemName(int itemId)
{
ItemCfg itemCfg = CommonDataManager.Tables.TblItemCfg.GetOrDefault(itemId);
return itemCfg.Name;
}
public static string GetSuitName(int suitId)
{
SuitCfg suitCfg = CommonDataManager.Tables.TblSuitCfg.GetOrDefault(suitId);
return suitCfg.Name;
}
public static int GetItemRarity(int itemId)
{
ItemCfg itemCfg = CommonDataManager.Tables.TblItemCfg.GetOrDefault(itemId);
return itemCfg.Rarity;
}
public static List CreateItemDataList(List items)
{
List itemList = new List();
for (int i = 0; i < items.Count; i++)
{
ItemData itemData = createItemData(items[i].ConfigId, items[i].Count);
itemList.Add(itemData);
}
return itemList;
}
public static List CreateItemDataList(List bonus, bool isOnceBonus = false)
{
List itemList = new List();
foreach (ItemParamProto item in bonus)
{
ItemData itemData = createItemData(item);
itemList.Add(itemData);
itemData.isOnceBonus = isOnceBonus;
}
return itemList;
}
public static List CreateItemDataList(List bonus, long num)
{
List itemList = new List();
foreach (var item in bonus)
{
int count = 0;
if (item != null)
{
count = item.Count;
}
else
{
count = 1;
}
ItemData itemData = createItemData(item.ItemId, count * num);
itemList.Add(itemData);
}
return itemList;
}
public static List CreateItemDataList(int itemId, long count)
{
List itemList = new List();
ItemData itemData = createItemData(itemId, count);
itemList.Add(itemData);
return itemList;
}
public static ItemData createItemData(ItemParamProto bonus)
{
int itemID = bonus.ItemId;
int itemNum = bonus.Count;
return createItemData(itemID, itemNum);
}
public static ItemData createItemData(int itemId, long count, long countRandomeMin = 0)
{
ItemData itemData = ItemDataPool.GetItemData(itemId);
itemData.num = count;
itemData.numRandomeMin = countRandomeMin;
return itemData;
}
public static Boolean CheckItemEnough(int itemId, long num, bool showTips = false)
{
long hasNum = ItemDataManager.GetItemNum(itemId);
if (hasNum < num && showTips)
{
ItemCfg itemCfg = CommonDataManager.Tables.TblItemCfg.GetOrDefault(itemId);
PromptController.Instance.ShowFloatTextPrompt(string.Format("【{0}】 不足", itemCfg.Name));
}
return hasNum >= num;
}
public static Boolean CheckItemEnough(int[][] items, int count = 1)
{
return items.All(item => CheckItemEnough(item[0], item[1] * count));
}
public static Boolean CheckItemEnough(System.Collections.Generic.List items)
{
return items.All(item => CheckItemEnough(item.ItemId, item.Count));
}
public static bool CheckMenuType1(int needItemId, int needSuitId, int typeId)
{
DressUpMenuItemCfg1 cfg1 = CommonDataManager.Tables.TblDressUpMenuItemCfg1.GetOrDefault(typeId);
if (needItemId > 0)
{
int subType = ItemUtilCS.GetItemSubType(needItemId);
if (subType == cfg1.Type)
{
return true;
}
else if (cfg1.SubMenus.Count > 0)
{
foreach (int id2 in cfg1.SubMenus)
{
DressUpMenuItemCfg2 cfg2 = CommonDataManager.Tables.TblDressUpMenuItemCfg2.GetOrDefault(id2);
if (cfg2.Type == subType)
{
return true;
}
}
}
}
else if (needSuitId > 0 && cfg1.Type == ConstDressUpItemType.TAO_ZHUANG)
{
return true;
}
return false;
}
public static void SortItemIdsByOwned(int[] array)
{
Array.Sort(array, (int a, int b) =>
{
bool ownedA = ItemDataManager.GetItemNum(a) > 0;
bool ownedB = ItemDataManager.GetItemNum(b) > 0;
if (!ownedB && ownedA)
{
return 1;
}
else if (ownedB && !ownedA)
{
return -1;
}
return 0;
});
}
public static string GetItemResExt(int itemType, int type, bool isIcon = false)
{
if (itemType == ConstItemType.DRESS_UP && type == ConstDressUpItemType.BEI_JING && !isIcon)
{
return "jpg";
}
return "png";
}
public static void UpdateItemNumAndNeedNum(GObject obj, int itemId, int needNum, bool ChangeColor = false)
{
UI_ComCostCurrencyWithHas com = UI_ComCostCurrencyWithHas.Proxy(obj);
long hasNum = ItemDataManager.GetItemNum(itemId);
string needStrColor = ChangeColor ? "#B99F7B" : "#FDEED4";
string strHasNum = StringUtil.GetColorText(hasNum.ToString(), hasNum < needNum ? "#C5645A" : needStrColor);
hasNum.ToString();
// com.m_txtNeed.text = needNum.ToString();
com.m_txtCount.text = string.Format("{0}{1}{2}", strHasNum, StringUtil.GetColorText("/", needStrColor),
StringUtil.GetColorText(needNum.ToString(), needStrColor));
ItemCfg cfg1 = CommonDataManager.Tables.TblItemCfg.GetOrDefault(itemId);
com.m_loaIcon.url = ResPathUtil.GetCommonGameResPath(cfg1.Res);
}
public static void UpdateItemAndNeed(GObject obj, int itemId, int needNum, bool ChangeColor = false)
{
UI_ComCostCurrencyWithHas com = UI_ComCostCurrencyWithHas.Proxy(obj);
long hasNum = ItemDataManager.GetItemNum(itemId);
string needStrColor = ChangeColor ? "#B99F7B" : "#FDEED4";
string strHasNum = StringUtil.GetColorText(hasNum.ToString(), hasNum < needNum ? "#C5645A" : needStrColor);
hasNum.ToString();
// com.m_txtNeed.text = needNum.ToString();
com.m_txtCount.text = string.Format("{0}{1}{2}", strHasNum, StringUtil.GetColorText("/", needStrColor),
StringUtil.GetColorText(needNum.ToString(), needStrColor));
ItemCfg cfg1 = CommonDataManager.Tables.TblItemCfg.GetOrDefault(itemId);
com.m_loaIcon.url = ResPathUtil.GetIconPath(cfg1.Res, "png");
}
public static void UpdateItemNeedNum(GObject obj, int itemId, int needNum, bool checkNum = true,
string color = "#716B59")
{
UI_ComCostCurrency com = UI_ComCostCurrency.Proxy(obj);
com.m_c1.selectedIndex = needNum == 0 ? 0 : 1;
long hasNum = ItemDataManager.GetItemNum(itemId);
string strNeedNum = !checkNum
? StringUtil.GetColorText(needNum.ToString(), color)
: StringUtil.GetColorText(needNum.ToString(), hasNum < needNum ? "#D0A09B" : color);
needNum.ToString();
com.m_txtNeed.text = strNeedNum;
ItemCfg cfg = CommonDataManager.Tables.TblItemCfg.GetOrDefault(itemId);
com.m_loaIcon.url = ResPathUtil.GetIconPath(cfg);
UI_ComCostCurrency.ProxyEnd();
}
public static void UpdateItemNeedNum(GObject obj, ItemParamProto cost, bool checkNum = true,
string color = "#716B59")
{
int itemId = cost.ItemId;
int needNum = cost.Count;
UI_ComCostCurrency com = UI_ComCostCurrency.Proxy(obj);
com.m_c1.selectedIndex = needNum == 0 ? 0 : 1;
long hasNum = ItemDataManager.GetItemNum(itemId);
string strNeedNum = !checkNum
? StringUtil.GetColorText(needNum.ToString(), color)
: StringUtil.GetColorText(needNum.ToString(), hasNum < needNum ? "#D0A09B" : color);
needNum.ToString();
com.m_txtNeed.text = strNeedNum;
ItemCfg cfg = CommonDataManager.Tables.TblItemCfg.GetOrDefault(itemId);
com.m_loaIcon.url = ResPathUtil.GetCommonGameResPath(cfg.Res);
UI_ComCostCurrency.ProxyEnd();
}
public static void UpdateTag(GObject obj, string tag)
{
UI_ComTag item = UI_ComTag.Proxy(obj);
int tagType = CommonDataManager.Tables.TblTagCfg.GetOrDefault(tag).Type;
item.m_txtTag.text = tag;
item.m_loaTag.url = ResPathUtil.GetCommonGameResPath("fzd_bqbq_" + tagType);
UI_ComTag.ProxyEnd();
}
///
/// 合并奖励配置,会将同一物品id的奖励合并
///
public static List MergeBonus(List item1, List item2)
{
Dictionary dictionary = new Dictionary();
if (item1 != null)
{
foreach (var item in item1)
{
if (dictionary.ContainsKey(item.ItemId))
{
dictionary[item.ItemId] += item.Count;
}
else
{
dictionary.Add(item.ItemId, item.Count);
}
}
}
if (item2 != null)
{
foreach (var item in item2)
{
if (dictionary.ContainsKey(item.ItemId))
{
dictionary[item.ItemId] += item.Count;
}
else
{
dictionary.Add(item.ItemId, item.Count);
}
}
}
List result = new List();
int i = 0;
foreach (KeyValuePair item in dictionary)
{
result.Add(new ItemParamProto() { ItemId = item.Key, Count = item.Value });
}
return result;
}
public static void CreateItemView(ItemParamProto item, GComponent component)
{
var itemData = createItemData(item);
component.data ??= new ItemView(component);
(component.data as ItemView)?.SetData(itemData);
(component.data as ItemView)?.ChangeTxtCountStyle();
}
public static List ToGfgGameItemParam(this List items)
{
List result = new List();
foreach (cfg.ItemParam item in items)
{
result.Add(new ItemParamProto
{
ItemId = item.ItemId,
Count = item.Count,
});
}
return result;
}
public static ItemParamProto ToGfgGameItemParam(this cfg.ItemParam item)
{
ItemParamProto result = new ItemParamProto() { ItemId = item.ItemId, Count = item.Count };
return result;
}
}
}