using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using LitJson;
|
using UnityEngine;
|
|
|
public class ItemLogicUtility : Singleton<ItemLogicUtility>
|
{
|
|
PackManager packModel { get { return PackManager.Instance; } }
|
|
|
|
/// <summary>
|
/// <param name="packType 背包类型"></param>
|
/// <param name="itemId 物品ID"></param>
|
/// <param name="needCount 需要数量"></param>
|
/// <param name="needTips">0 不响应 1 弹提示 2 弹获取途径tips</param>
|
/// <returns></returns>
|
public static bool CheckItemCount(PackType packType, int itemId, long needCount, int needTips = 0)
|
{
|
if (needCount <= 0)
|
{
|
return true;
|
}
|
|
var haveCount = PackManager.Instance.GetItemCountByID(packType, itemId);
|
|
bool isEnough = haveCount >= needCount;
|
|
if (!isEnough)
|
{
|
if (needTips == 1)
|
{
|
SysNotifyMgr.Instance.ShowTip("ItemNotEnough", itemId);
|
}
|
else if (needTips == 2)
|
{
|
ItemTipUtility.Show(itemId, true);
|
}
|
}
|
|
return isEnough;
|
}
|
|
|
|
#region 得到物品的品质颜色
|
public int GetItemQuality(int itemId, Dictionary<int, List<int>> useDataDic = null)
|
{
|
ItemConfig itemConfig = ItemConfig.Get(itemId);
|
|
return itemConfig.ItemColor;
|
}
|
#endregion
|
|
|
|
#region 物品是否过期
|
|
public bool IsOverdue(string guid)
|
{
|
var item = packModel.GetItemByGuid(guid);
|
if (item == null)
|
{
|
return false;
|
}
|
|
if (item.isAuction)
|
{
|
return false;//item.auctionSurplusTime < 0;
|
}
|
else
|
{
|
var isoverdue = false;
|
switch ((ItemTimeType)item.config.EndureReduceType)
|
{
|
case ItemTimeType.EquipedTime:
|
isoverdue = item.GetUseDataFirstValue(44) > 0 && item.overdueSurplusTime < 0;
|
break;
|
case ItemTimeType.RealityTime:
|
isoverdue = item.overdueSurplusTime < 0;
|
break;
|
}
|
|
return isoverdue;
|
}
|
}
|
|
#endregion
|
|
#region 获得英雄物品展示立绘
|
public List<int> poplhHeroIdList = new List<int>();
|
|
public void SetHeroList(List<Item> items)
|
{
|
if (items.IsNullOrEmpty())
|
return;
|
foreach (var item in items)
|
{
|
int itemId = item.id;
|
if (HeroConfig.HasKey(itemId) &&
|
HeroConfig.Get(itemId).Quality >= HappyXBModel.Instance.lhQuality)
|
{
|
poplhHeroIdList.Add(itemId);
|
}
|
}
|
}
|
|
public void ShowHeroLHWin(List<Item> items)
|
{
|
SetHeroList(items);
|
if (poplhHeroIdList.Count > 0)
|
{
|
if (!UIManager.Instance.IsOpenedInList<HeroShowLHWin>())
|
{
|
UIManager.Instance.OpenWindow<HeroShowLHWin>();
|
}
|
}
|
}
|
|
#endregion
|
|
public bool isNameShow { get; private set; } // 是否展示物品名字
|
|
// 如果同时有多种奖励封包,同一个事件归集,不同事件直接顶掉显示最新
|
public Dictionary<Int2, Item> totalShowItems = new Dictionary<Int2, Item>(); //Int2 物品ID+useType
|
public List<int> totalDeleteItemIDs = new List<int>(); //删除的物品ID
|
public event Action OnGetItemShowEvent;
|
public string getItemEventName;
|
public string sourceTip; //领奖原因
|
|
// isMergeItem 是否合并相同ID的物品 默认合并
|
// 通用显示获得的物品
|
public void ShowGetItem(List<Item> items, string eventName = "default", bool isNameShow = true,
|
bool isMergeItem = true, List<int> deleteItemIDs = null)
|
{
|
if (getItemEventName != eventName)
|
{
|
if (UIManager.Instance.IsOpenedInList<CommonGetItemWin>())
|
{
|
//立即关闭
|
UIManager.Instance.CloseWindow<CommonGetItemWin>();
|
}
|
totalShowItems.Clear();
|
totalDeleteItemIDs.Clear();
|
getItemEventName = eventName;
|
}
|
if (!deleteItemIDs.IsNullOrEmpty())
|
{
|
for (int i = 0; i < deleteItemIDs.Count; i++)
|
{
|
if (!totalDeleteItemIDs.Contains(deleteItemIDs[i]))
|
{
|
totalDeleteItemIDs.Add(deleteItemIDs[i]);
|
}
|
}
|
}
|
|
//相同ID 合并数量显示
|
for (int i = 0; i < items.Count; i++)
|
{
|
var id = items[i].id;
|
if (totalDeleteItemIDs.Contains(id))
|
{
|
continue;
|
}
|
|
var useType = isMergeItem ? 0 : items[i].useType;
|
Int2 idInfo = new Int2(id, useType);
|
|
if (totalShowItems.ContainsKey(idInfo))
|
{
|
totalShowItems[idInfo] = new Item(id, items[i].countEx + totalShowItems[idInfo].countEx, useType, items[i].quality);
|
}
|
else
|
{
|
totalShowItems.Add(idInfo, new Item(id, items[i].countEx, useType, items[i].quality));
|
}
|
|
}
|
|
if (GeneralDefine.commonAwardTipDict.ContainsKey(eventName))
|
{
|
sourceTip = GeneralDefine.commonAwardTipDict[eventName];
|
}
|
else
|
{
|
sourceTip = "";
|
}
|
|
this.isNameShow = isNameShow;
|
OnGetItemShowEvent?.Invoke();
|
if (!UIManager.Instance.IsOpenedInList<CommonGetItemWin>())
|
{
|
UIManager.Instance.OpenWindow<CommonGetItemWin>();
|
}
|
|
ShowHeroLHWin(items);//有传奇以上英雄显示立绘
|
}
|
|
|
public void ClearGetItem()
|
{
|
//不清理物品,下次收到数据会自动清理,只改事件方便打开界面测试
|
getItemEventName = "";
|
}
|
|
public bool UseItem(string guid, int useCnt = 1, int extra = 0)
|
{
|
var item = packModel.GetItemByGuid(guid);
|
if (item == null)
|
{
|
return false;
|
}
|
|
var error = 0;
|
if (CanUseItem(guid, useCnt, out error))
|
{
|
UseItemSendServer(guid, useCnt, extra);
|
return true;
|
}
|
else
|
{
|
switch (error)
|
{
|
case 1:
|
SysNotifyMgr.Instance.ShowTip("EverydayUseLimit");
|
break;
|
case 2:
|
SysNotifyMgr.Instance.ShowTip("UseCntLimit");
|
break;
|
case 3:
|
SysNotifyMgr.Instance.ShowTip("GeRen_chenxin_749572");
|
break;
|
default:
|
break;
|
}
|
return false;
|
}
|
}
|
|
public bool CanUseItem(string guid, int useCnt, out int error)
|
{
|
var item = packModel.GetItemByGuid(guid);
|
if (item == null)
|
{
|
error = 999;
|
return false;
|
}
|
|
|
if (item.config.Type == 145 && item.config.CDTime > 0)
|
{
|
var createTime = item.GetUseDataFirstValue((int)ItemUseDataKey.createTime);
|
if (createTime > 0)
|
{
|
DateTime useTime;
|
if (item.config.CDTypeEx == 1)
|
{
|
//itemConfig.CDTime 为天,过0点可用
|
useTime = TimeUtility.GetTime((uint)createTime).AddDays(item.config.CDTime);
|
useTime = TimeUtility.GetDayStartTime(useTime.Year, useTime.Month, useTime.Day);
|
}
|
else
|
{
|
useTime = TimeUtility.GetTime((uint)createTime).AddSeconds(item.config.CDTime);
|
}
|
if (TimeUtility.ServerNow < useTime)
|
{
|
error = 3;
|
return false;
|
}
|
}
|
}
|
|
|
var usedCountToday = packModel.GetItemUsedTimesToday(item.itemId);
|
if (item.config.MaxSkillCnt > 0 && item.config.MaxSkillCnt <= usedCountToday)
|
{
|
error = 1;
|
return false;
|
}
|
|
|
error = 0;
|
return true;
|
}
|
|
void UseItemSendServer(string guid, int useCnt, int extra)
|
{
|
var itemModel = packModel.GetItemByGuid(guid);
|
if (itemModel == null)
|
{
|
return;
|
}
|
|
var useItem = new CA323_tagCMUseItems();
|
useItem.ItemIndex = (byte)itemModel.gridIndex;
|
useItem.UseCnt = (ushort)useCnt;
|
useItem.ExData = (uint)extra;
|
GameNetSystem.Instance.SendInfo(useItem); //使用物品
|
}
|
|
|
/// <summary>
|
/// 打开批量使用物品界面
|
/// </summary>
|
/// <param name="itemID">要使用的物品</param>
|
/// <param name="showTipEvent">参数为要使用的物品数量,返回值为文本</param>
|
public void ShowItemBatchUseWin(int itemID, Func<long, string> showTipEvent, string btnName)
|
{
|
if (UIManager.Instance.IsOpened<ItemBatchUseWin>())
|
{
|
UIManager.Instance.CloseWindow<ItemBatchUseWin>();
|
}
|
//提示类型后续有变化,要约定下如何提示
|
if (!CheckItemCount(PackType.Item, itemID, 1, 1))
|
{
|
return;
|
}
|
ItemBatchUseWin.itemID = itemID;
|
ItemBatchUseWin.ShowTextEvent = showTipEvent;
|
ItemBatchUseWin.btnName = btnName;
|
UIManager.Instance.OpenWindow<ItemBatchUseWin>();
|
}
|
|
}
|